1 /*
2 * Copyright (C) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "gst_player_build.h"
17 #include "media_log.h"
18
19 namespace {
20 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN, "GstPlayerBuild"};
21 }
22
23 namespace OHOS {
24 namespace Media {
25 class GstPlayerSingnalDispatcherFactory {
26 public:
27 GstPlayerSingnalDispatcherFactory() = delete;
28 ~GstPlayerSingnalDispatcherFactory() = delete;
29
Create(GMainContext * context)30 static GstPlayerSignalDispatcher *Create(GMainContext *context)
31 {
32 CHECK_AND_RETURN_RET_LOG(context != nullptr, nullptr, "context is nullptr");
33 return gst_player_g_main_context_signal_dispatcher_new(context);
34 }
35
Destroy(GstPlayerSignalDispatcher * dispatcher)36 static void Destroy(GstPlayerSignalDispatcher *dispatcher)
37 {
38 CHECK_AND_RETURN_LOG(dispatcher != nullptr, "dispatcher is nullptr");
39 gst_object_unref(dispatcher);
40 }
41 };
42
43 class GstPlayerFactory {
44 public:
45 GstPlayerFactory() = delete;
46 ~GstPlayerFactory() = delete;
Create(GstPlayerVideoRenderer * renderer,GstPlayerSignalDispatcher * dispatcher)47 static GstPlayer *Create(GstPlayerVideoRenderer *renderer, GstPlayerSignalDispatcher *dispatcher)
48 {
49 CHECK_AND_RETURN_RET_LOG(renderer != nullptr, nullptr, "renderer is nullptr");
50 CHECK_AND_RETURN_RET_LOG(dispatcher != nullptr, nullptr, "dispatcher is nullptr");
51 return gst_player_new(renderer, dispatcher);
52 }
53
Destroy(GstPlayer * player)54 static void Destroy(GstPlayer *player)
55 {
56 CHECK_AND_RETURN_LOG(player != nullptr, "player is nullptr");
57 gst_object_unref(player);
58 }
59 };
60
GstPlayerBuild()61 GstPlayerBuild::GstPlayerBuild()
62 {
63 MEDIA_LOGD("0x%{public}06" PRIXPTR " Instances create", FAKE_POINTER(this));
64 }
65
~GstPlayerBuild()66 GstPlayerBuild::~GstPlayerBuild()
67 {
68 Release();
69 MEDIA_LOGD("0x%{public}06" PRIXPTR " Instances destroy", FAKE_POINTER(this));
70 }
71
BuildRendererCtrl(sptr<Surface> surface)72 std::shared_ptr<GstPlayerVideoRendererCtrl> GstPlayerBuild::BuildRendererCtrl(sptr<Surface> surface)
73 {
74 if (surface == nullptr) {
75 MEDIA_LOGI("This is an audio scene.");
76 } else {
77 MEDIA_LOGI("This is an video scene.");
78 }
79
80 context_ = g_main_context_new();
81 CHECK_AND_RETURN_RET_LOG(context_ != nullptr, nullptr, "g_main_context_new failed..");
82
83 g_main_context_push_thread_default(context_);
84
85 rendererCtrl_ = std::make_shared<GstPlayerVideoRendererCtrl>(surface);
86 if (rendererCtrl_ == nullptr) {
87 Release();
88 MEDIA_LOGE("rendererCtrl_ is nullptr");
89 return nullptr;
90 }
91
92 return rendererCtrl_;
93 }
94
BuildPlayerCtrl()95 std::shared_ptr<GstPlayerCtrl> GstPlayerBuild::BuildPlayerCtrl()
96 {
97 if (rendererCtrl_ == nullptr) {
98 MEDIA_LOGE("rendererCtrl_ is nullptr");
99 return nullptr;
100 }
101
102 videoRenderer_ = GstPlayerVideoRendererFactory::Create(rendererCtrl_);
103 signalDispatcher_ = GstPlayerSingnalDispatcherFactory::Create(context_);
104 if (signalDispatcher_ == nullptr || videoRenderer_ == nullptr) {
105 Release();
106 MEDIA_LOGE("signalDispatcher_ or videoRenderer_ is nullptr");
107 return nullptr;
108 }
109
110 gstPlayer_ = GstPlayerFactory::Create(videoRenderer_, signalDispatcher_);
111 playerCtrl_ = std::make_shared<GstPlayerCtrl>(gstPlayer_);
112 if (gstPlayer_ == nullptr || playerCtrl_ == nullptr) {
113 Release();
114 MEDIA_LOGE("gstPlayer_ or playerCtrl_ is nullptr");
115 return nullptr;
116 }
117
118 return playerCtrl_;
119 }
120
CreateLoop()121 void GstPlayerBuild::CreateLoop()
122 {
123 MEDIA_LOGI("Create the loop for the current context");
124 CHECK_AND_RETURN_LOG(context_ != nullptr, "context_ is nullptr");
125
126 loop_ = g_main_loop_new(context_, FALSE);
127 CHECK_AND_RETURN_LOG(loop_ != nullptr, "gstPlayer_ is nullptr");
128
129 GSource *source = g_idle_source_new();
130 g_source_set_callback(source, (GSourceFunc)GstPlayerBuild::MainLoopRunCb, this,
131 nullptr);
132 guint ret = g_source_attach(source, context_);
133 CHECK_AND_RETURN_LOG(ret > 0, "add idle source failed");
134 g_source_unref(source);
135
136 g_main_loop_run(loop_);
137 // wait g_main_loop_quit
138 g_main_loop_unref(loop_);
139 loop_ = nullptr;
140 }
141
MainLoopRunCb(GstPlayerBuild * build)142 gboolean GstPlayerBuild::MainLoopRunCb(GstPlayerBuild *build)
143 {
144 if (build == nullptr) {
145 return G_SOURCE_REMOVE;
146 }
147
148 std::unique_lock<std::mutex> lock(build->mutex_);
149 build->needWaiting_ = false;
150 build->cond_.notify_one();
151
152 return G_SOURCE_REMOVE;
153 }
154
WaitMainLoopStart()155 void GstPlayerBuild::WaitMainLoopStart()
156 {
157 std::unique_lock<std::mutex> lock(mutex_);
158 cond_.wait(lock, [this] { return !needWaiting_; }); // wait main loop run done
159 }
160
DestroyLoop() const161 void GstPlayerBuild::DestroyLoop() const
162 {
163 if (loop_ != nullptr && g_main_loop_is_running(loop_)) {
164 MEDIA_LOGI("Main loop still running, quit");
165 g_main_loop_quit(loop_);
166 }
167 }
168
Release()169 void GstPlayerBuild::Release()
170 {
171 playerCtrl_ = nullptr;
172 rendererCtrl_ = nullptr;
173
174 if (gstPlayer_ != nullptr) {
175 GstPlayerFactory::Destroy(gstPlayer_);
176 gstPlayer_ = nullptr;
177 }
178
179 if (signalDispatcher_ != nullptr) {
180 GstPlayerSingnalDispatcherFactory::Destroy(signalDispatcher_);
181 signalDispatcher_ = nullptr;
182 }
183
184 if (videoRenderer_ != nullptr) {
185 GstPlayerVideoRendererFactory::Destroy(videoRenderer_);
186 videoRenderer_ = nullptr;
187 }
188
189 if (context_ != nullptr) {
190 g_main_context_pop_thread_default(context_);
191 g_main_context_unref(context_);
192 context_ = nullptr;
193 }
194 }
195 } // namespace Media
196 } // namespace OHOS
197