diff --git a/frameworks/innerkitsimpl/native/media_surface/media_surface_impl.cpp b/frameworks/innerkitsimpl/native/media_surface/media_surface_impl.cpp index e52c45c2efa5b9a05f2b49cdc7245a75a10cc458..6c0caf17ed409bf6bc9a85af80819e158003c655 100644 --- a/frameworks/innerkitsimpl/native/media_surface/media_surface_impl.cpp +++ b/frameworks/innerkitsimpl/native/media_surface/media_surface_impl.cpp @@ -17,6 +17,8 @@ #include "media_log.h" #include "media_errors.h" #include "display_type.h" +#include "wm_common.h" +#include "foundation/windowmanager/interfaces/innerkits/wm/window_option.h" namespace { constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN, "MediaSurface"}; @@ -73,31 +75,42 @@ sptr MediaSurfaceImpl::GetSurface(const std::string &id) return nullptr; } +void MediaSurfaceImpl::Release() +{ + MEDIA_LOGD("Release"); + producerSurface_ = nullptr; + mwindow_ =nullptr; + if (previewWindow_ != nullptr) { + previewWindow_->Destroy(); + } + previewWindow_ = nullptr; + surfaceMap_.clear(); +} + sptr MediaSurfaceImpl::GetSurface() { - std::lock_guard lock(mutex_); - sptr wmi = WindowManager::GetInstance(); - CHECK_AND_RETURN_RET_LOG(wmi != nullptr, nullptr, "WindowManager is nullptr!"); + if (previewWindow_ != nullptr || producerSurface_ != nullptr) { + Release(); + } - wmi->Init(); - sptr option = WindowOption::Get(); - CHECK_AND_RETURN_RET_LOG(option != nullptr, nullptr, "WindowOption is nullptr!"); - const int32_t height = 360; - const int32_t width = 640; - (void)option->SetWidth(width); - (void)option->SetHeight(height); - (void)option->SetX(0); - (void)option->SetY(0); - (void)option->SetWindowType(WINDOW_TYPE_NORMAL); - (void)wmi->CreateWindow(mwindow_, option); - CHECK_AND_RETURN_RET_LOG(mwindow_ != nullptr, nullptr, "mwindow_ is nullptr!"); + std::lock_guard lock(mutex_); + sptr option = new Rosen::WindowOption(); + option->SetWindowRect({ 0, 0, 1920, 1080 }); // 1920 is width, 1080 is height + option->SetWindowType(Rosen::WindowType::WINDOW_TYPE_APP_LAUNCHING); + option->SetWindowMode(Rosen::WindowMode::WINDOW_MODE_FLOATING); + std::string winName = "media_player_window" + std::to_string(id_); + previewWindow_ = Rosen::Window::Create(winName, option); + id_++; - sptr producerSurface = mwindow_->GetSurface(); - CHECK_AND_RETURN_RET_LOG(producerSurface != nullptr, nullptr, "producerSurface is nullptr!"); + if (previewWindow_ == nullptr || previewWindow_->GetSurfaceNode() == nullptr) { + MEDIA_LOGE("previewWindow_ is nullptr"); + return nullptr; + } - const std::string format = "SURFACE_FORMAT"; - (void)producerSurface->SetUserData(format, std::to_string(static_cast(PIXEL_FMT_RGBA_8888))); - return producerSurface; + producerSurface_ = previewWindow_->GetSurfaceNode()->GetSurface(); + previewWindow_->Show(); + MEDIA_LOGD("Create MediaSurfaceImpl Surface"); + return producerSurface_; } } } \ No newline at end of file diff --git a/frameworks/innerkitsimpl/native/media_surface/media_surface_impl.h b/frameworks/innerkitsimpl/native/media_surface/media_surface_impl.h index c8f4ae4e8acb81e4f697c3c9cd0e42a51ce3f944..ba6205868fb3b67fbc0ac4e79ed353d96e5101db 100644 --- a/frameworks/innerkitsimpl/native/media_surface/media_surface_impl.h +++ b/frameworks/innerkitsimpl/native/media_surface/media_surface_impl.h @@ -20,6 +20,7 @@ #include #include "media_surface.h" #include "window_manager.h" +#include "foundation/windowmanager/interfaces/innerkits/wm/window.h" namespace OHOS { namespace Media { @@ -31,12 +32,16 @@ public: virtual std::string GetSurfaceId(const sptr &surface) override; virtual sptr GetSurface() override; virtual sptr GetSurface(const std::string &id) override; + virtual void Release() override; private: std::mutex mutex_; int32_t idCount = 0; + int32_t id_ = 0; sptr mwindow_ = nullptr; + sptr previewWindow_ = nullptr; std::map> surfaceMap_; + sptr producerSurface_ = nullptr; }; } } diff --git a/frameworks/kitsimpl/js/player/video_player_napi.cpp b/frameworks/kitsimpl/js/player/video_player_napi.cpp index eac4d595c16dc4e5e81b4f620a7e0b76b9752d09..6fbaa2a3f62223041d2237d37102a19825b33675 100644 --- a/frameworks/kitsimpl/js/player/video_player_napi.cpp +++ b/frameworks/kitsimpl/js/player/video_player_napi.cpp @@ -468,7 +468,7 @@ void VideoPlayerNapi::CompleteAsyncWork(napi_env env, napi_status status, void * asyncContext->env = env; auto cb = std::static_pointer_cast(asyncContext->jsPlayer->jsCallback_); cb->QueueAsyncWork(asyncContext); - + int32_t ret = MSERR_OK; auto player = asyncContext->jsPlayer->nativePlayer_; if (asyncContext->asyncWorkType == AsyncWorkType::ASYNC_WORK_PREPARE) { @@ -697,9 +697,15 @@ napi_value VideoPlayerNapi::Release(napi_env env, napi_callback_info info) if (ret != MSERR_OK) { asyncContext->SignError(MSERR_EXT_OPERATE_NOT_PERMIT, "failed to release"); } + asyncContext->jsPlayer->jsCallback_ = nullptr; asyncContext->jsPlayer->url_.clear(); - + + auto mediaSurface = MediaSurfaceFactory::CreateMediaSurface(); + if (mediaSurface != nullptr) { + mediaSurface->Release(); + } + // async work napi_value resource = nullptr; napi_create_string_utf8(env, "Release", NAPI_AUTO_LENGTH, &resource); diff --git a/interfaces/innerkits/native/media/BUILD.gn b/interfaces/innerkits/native/media/BUILD.gn index 8a437b3dc5e4b26993bd1156f7642c9ad977f549..60626acdf38f5836cac10d96778269dd5b948236 100644 --- a/interfaces/innerkits/native/media/BUILD.gn +++ b/interfaces/innerkits/native/media/BUILD.gn @@ -218,7 +218,6 @@ ohos_shared_library("media_surface") { "-D_FORTIFY_SOURCE=2", "-fvisibility=hidden", "-Wformat=2", - "-Wfloat-equal", "-Wdate-time", ] @@ -228,6 +227,8 @@ ohos_shared_library("media_surface") { "//foundation/graphic/standard:libsurface", "//foundation/graphic/standard:libwmclient", "//foundation/graphic/standard/frameworks/surface:surface", + "//foundation/windowmanager/utils:libwmutil", + "//foundation/windowmanager/wm:libwm", "//utils/native/base:utils", ] diff --git a/interfaces/innerkits/native/media/demo/BUILD.gn b/interfaces/innerkits/native/media/demo/BUILD.gn index 2af41a94c496a7221ac061435ab75b0797380d66..00c9bcb018cc5a2ff10c701776317018217ee912 100644 --- a/interfaces/innerkits/native/media/demo/BUILD.gn +++ b/interfaces/innerkits/native/media/demo/BUILD.gn @@ -46,7 +46,6 @@ ohos_executable("media_demo") { "-D_FORTIFY_SOURCE=2", "-fvisibility=hidden", "-Wformat=2", - "-Wfloat-equal", "-Wdate-time", ] @@ -65,6 +64,8 @@ ohos_executable("media_demo") { "//foundation/graphic/standard:libwmclient", "//foundation/graphic/standard/frameworks/surface:surface", "//foundation/multimedia/image_standard/interfaces/innerkits:image_native", + "//foundation/windowmanager/utils:libwmutil", + "//foundation/windowmanager/wm:libwm", "//third_party/libjpeg:libjpeg_static", "//utils/native/base:utils", ] diff --git a/interfaces/innerkits/native/media/demo/player/player_demo.cpp b/interfaces/innerkits/native/media/demo/player/player_demo.cpp index 572bd62ff83efd4210e73db91ebe71e31af46730..f742ac31df07b681dabb9f466303a711b795ac86 100644 --- a/interfaces/innerkits/native/media/demo/player/player_demo.cpp +++ b/interfaces/innerkits/native/media/demo/player/player_demo.cpp @@ -20,6 +20,8 @@ #include "string_ex.h" #include "media_errors.h" #include "directory_ex.h" +#include "wm_common.h" +#include "foundation/windowmanager/interfaces/innerkits/wm/window_option.h" using namespace OHOS; using namespace OHOS::Media; @@ -127,6 +129,71 @@ void PlayerCallbackDemo::PrintBufferingUpdate(const Format &infoBody) const } } +PlayerDemo::PlayerDemo() +{ +} + +PlayerDemo::~PlayerDemo() +{ + if (previewWindow_ != nullptr) { + previewWindow_->Destroy(); + previewWindow_ = nullptr; + } +} + +sptr PlayerDemo::GetWindowSurface() +{ + if (SetSurfaceSize() != 0) { + cout << "SetSurface Size fail" << endl; + return nullptr; + } + + sptr wmi = WindowManager::GetInstance(); + if (wmi == nullptr) { + cout << "WindowManager is null" << endl; + return nullptr; + } + wmi->Init(); + sptr option = WindowOption::Get(); + if (option == nullptr) { + cout << "WindowOption is null" << endl; + return nullptr; + } + (void)option->SetWidth(width_); + (void)option->SetHeight(height_); + (void)option->SetX(0); + (void)option->SetY(0); + (void)option->SetWindowType(WINDOW_TYPE_NORMAL); + (void)wmi->CreateWindow(mwindow_, option); + if (mwindow_ == nullptr) { + cout << "mwindow_ is null" << endl; + return nullptr; + } + + return mwindow_->GetSurface(); +} + +sptr PlayerDemo::GetSubWindowSurface() +{ + if (SetSurfaceSize() != 0) { + cout << "SetSurface Size fail" << endl; + return nullptr; + } + + sptr option = new Rosen::WindowOption(); + option->SetWindowRect({ 0, 0, width_, height_ }); + option->SetWindowType(Rosen::WindowType::WINDOW_TYPE_APP_LAUNCHING); + option->SetWindowMode(Rosen::WindowMode::WINDOW_MODE_FLOATING); + previewWindow_ = Rosen::Window::Create("xcomponent_window", option); + if (previewWindow_ == nullptr || previewWindow_->GetSurfaceNode() == nullptr) { + cout << "previewWindow_ is nullptr" << endl; + return nullptr; + } + + previewWindow_->Show(); + return previewWindow_->GetSurfaceNode()->GetSurface(); +} + sptr PlayerDemo::GetVideoSurface() { cout << "Please enter the number of mode(default no window):" << endl; @@ -139,32 +206,11 @@ sptr PlayerDemo::GetVideoSurface() if (mode == "0" || mode == "") { return nullptr; } else if (mode == "1") { - sptr wmi = WindowManager::GetInstance(); - if (wmi == nullptr) { - cout << "WindowManager is null" << endl; - return nullptr; - } - wmi->Init(); - sptr option = WindowOption::Get(); - if (option == nullptr) { - cout << "WindowOption is null" << endl; - return nullptr; - } - (void)option->SetWidth(WIDTH); - (void)option->SetHeight(HEIGHT); - (void)option->SetX(0); - (void)option->SetY(0); - (void)option->SetWindowType(WINDOW_TYPE_NORMAL); - (void)wmi->CreateWindow(mwindow_, option); - if (mwindow_ == nullptr) { - cout << "mwindow_ is null" << endl; - return nullptr; - } - producerSurface = mwindow_->GetSurface(); + producerSurface = GetWindowSurface(); } else if (mode == "2") { - cout << "invalid operation" << endl; - return nullptr; + producerSurface = GetSubWindowSurface(); } + if (producerSurface == nullptr) { cout << "producerSurface is nullptr" << endl; return nullptr; @@ -509,6 +555,26 @@ int32_t PlayerDemo::SelectBufferingOut() } } +int32_t PlayerDemo::SetSurfaceSize() +{ + int32_t ret = 0; + cout << "Please enter surface size(width x height):" << endl; + cout << "0:1920 x 1080" << endl; + cout << "1:640 x 360" << endl; + string mode; + (void)getline(cin, mode); + if (mode == "" || mode == "0") { + width_ = 1920; // 1920 for width + height_ = 1080; // 1080 for height + } else if (mode == "1") { + width_ = 640; // 640 for width + height_ = 360; // 360 for height + } else { + ret = -1; + } + return ret; +} + void PlayerDemo::RunCase(const string &path) { player_ = OHOS::Media::PlayerFactory::CreatePlayer(); diff --git a/interfaces/innerkits/native/media/demo/player/player_demo.h b/interfaces/innerkits/native/media/demo/player/player_demo.h index c02bb3039c1113d347430794aa756f36b77f0e9a..0b84a48684cd1d7a583509105e68ee3c072de01a 100644 --- a/interfaces/innerkits/native/media/demo/player/player_demo.h +++ b/interfaces/innerkits/native/media/demo/player/player_demo.h @@ -23,10 +23,9 @@ #include "window_manager.h" #include "nocopyable.h" #include "media_data_source_demo.h" +#include "foundation/windowmanager/interfaces/innerkits/wm/window.h" namespace MediaDemo { - const int32_t HEIGHT = 360; - const int32_t WIDTH = 640; const int32_t POSITION_UPDATE_INTERVAL = 100; const std::map STATE_MAP = { {OHOS::Media::PlayerStates::PLAYER_STATE_ERROR, "Error"}, @@ -44,8 +43,8 @@ namespace OHOS { namespace Media { class PlayerDemo { public: - PlayerDemo() = default; - virtual ~PlayerDemo() = default; + PlayerDemo(); + ~PlayerDemo(); DISALLOW_COPY_AND_MOVE(PlayerDemo); sptr GetVideoSurface(); void RunCase(const std::string &path); @@ -64,14 +63,20 @@ private: int32_t GetPlaybackSpeed() const; int32_t SetDataSrc(const std::string &path, bool seekable); int32_t SelectSource(const std::string &path); + int32_t SetSurfaceSize(); int32_t SelectBufferingOut(); int32_t ChangeModeToSpeed(const PlaybackRateMode &mode, double &rate) const; int32_t ChangeSpeedToMode(const double &rate, PlaybackRateMode &mode) const; + sptr GetWindowSurface(); + sptr GetSubWindowSurface(); void RegisterTable(); sptr mwindow_ = nullptr; + sptr previewWindow_ = nullptr; std::map> playerTable_; std::shared_ptr player_ = nullptr; std::shared_ptr dataSrc_ = nullptr; + int32_t height_ = 0; + int32_t width_ = 0; }; class PlayerCallbackDemo : public PlayerCallback { diff --git a/interfaces/innerkits/native/media/include/media_surface.h b/interfaces/innerkits/native/media/include/media_surface.h index 92b7da86f1f84f6c2041292dc7092bc8dffd04b5..4696ff4745a7aa49a560fc4b4dde82117c9672a1 100644 --- a/interfaces/innerkits/native/media/include/media_surface.h +++ b/interfaces/innerkits/native/media/include/media_surface.h @@ -26,6 +26,7 @@ public: virtual std::string GetSurfaceId(const sptr &surface) = 0; virtual sptr GetSurface() = 0; virtual sptr GetSurface(const std::string &id) = 0; + virtual void Release() = 0; }; class __attribute__((visibility("default"))) MediaSurfaceFactory {