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 "player_service_stub.h"
17 #include <unistd.h>
18 #include "player_listener_proxy.h"
19 #include "media_data_source_proxy.h"
20 #include "media_server_manager.h"
21 #include "media_log.h"
22 #include "media_errors.h"
23 #include "media_parcel.h"
24 #include "parameter.h"
25 #include "media_dfx.h"
26 #include "player_xcollie.h"
27 #ifdef SUPPORT_AVSESSION
28 #include "avsession_background.h"
29 #endif
30
31 namespace {
32 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN, "PlayerServiceStub"};
33 }
34
35 namespace OHOS {
36 namespace Media {
Create()37 sptr<PlayerServiceStub> PlayerServiceStub::Create()
38 {
39 sptr<PlayerServiceStub> playerStub = new(std::nothrow) PlayerServiceStub();
40 CHECK_AND_RETURN_RET_LOG(playerStub != nullptr, nullptr, "failed to new PlayerServiceStub");
41
42 int32_t ret = playerStub->Init();
43 CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, nullptr, "failed to player stub init");
44 return playerStub;
45 }
46
PlayerServiceStub()47 PlayerServiceStub::PlayerServiceStub()
48 : taskQue_("PlayerRequest")
49 {
50 (void)taskQue_.Start();
51 MEDIA_LOGD("0x%{public}06" PRIXPTR " Instances create", FAKE_POINTER(this));
52 }
53
~PlayerServiceStub()54 PlayerServiceStub::~PlayerServiceStub()
55 {
56 (void)CancellationMonitor(appPid_);
57 if (playerServer_ != nullptr) {
58 auto task = std::make_shared<TaskHandler<void>>([&, this] {
59 LISTENER((void)playerServer_->Release(); playerServer_ = nullptr,
60 "PlayerServiceStub::~PlayerServiceStub", false)
61 });
62 (void)taskQue_.EnqueueTask(task);
63 (void)task->GetResult();
64 }
65 (void)taskQue_.Stop();
66 MEDIA_LOGD("0x%{public}06" PRIXPTR " Instances destroy", FAKE_POINTER(this));
67 }
68
SetPlayerFuncs()69 void PlayerServiceStub::SetPlayerFuncs()
70 {
71 playerFuncs_[SET_LISTENER_OBJ] = { &PlayerServiceStub::SetListenerObject, "Player::SetListenerObject" };
72 playerFuncs_[SET_SOURCE] = { &PlayerServiceStub::SetSource, "Player::SetSource" };
73 playerFuncs_[SET_MEDIA_DATA_SRC_OBJ] = { &PlayerServiceStub::SetMediaDataSource, "Player::SetMediaDataSource" };
74 playerFuncs_[SET_FD_SOURCE] = { &PlayerServiceStub::SetFdSource, "Player::SetFdSource" };
75 playerFuncs_[ADD_SUB_SOURCE] = { &PlayerServiceStub::AddSubSource, "Player::AddSubSource" };
76 playerFuncs_[ADD_SUB_FD_SOURCE] = { &PlayerServiceStub::AddSubFdSource, "Player::AddSubFdSource" };
77 playerFuncs_[PLAY] = { &PlayerServiceStub::Play, "Player::Play" };
78 playerFuncs_[PREPARE] = { &PlayerServiceStub::Prepare, "Player::Prepare" };
79 playerFuncs_[PREPAREASYNC] = { &PlayerServiceStub::PrepareAsync, "Player::PrepareAsync" };
80 playerFuncs_[PAUSE] = { &PlayerServiceStub::Pause, "Player::Pause" };
81 playerFuncs_[STOP] = { &PlayerServiceStub::Stop, "Player::Stop" };
82 playerFuncs_[RESET] = { &PlayerServiceStub::Reset, "Player::Reset" };
83 playerFuncs_[RELEASE] = { &PlayerServiceStub::Release, "Player::Release" };
84 playerFuncs_[SET_VOLUME] = { &PlayerServiceStub::SetVolume, "Player::SetVolume" };
85 playerFuncs_[SEEK] = { &PlayerServiceStub::Seek, "Player::Seek" };
86 playerFuncs_[GET_CURRENT_TIME] = { &PlayerServiceStub::GetCurrentTime, "Player::GetCurrentTime" };
87 playerFuncs_[GET_DURATION] = { &PlayerServiceStub::GetDuration, "Player::GetDuration" };
88 playerFuncs_[SET_PLAYERBACK_SPEED] = { &PlayerServiceStub::SetPlaybackSpeed, "Player::SetPlaybackSpeed" };
89 playerFuncs_[GET_PLAYERBACK_SPEED] = { &PlayerServiceStub::GetPlaybackSpeed, "Player::GetPlaybackSpeed" };
90 #ifdef SUPPORT_VIDEO
91 playerFuncs_[SET_VIDEO_SURFACE] = { &PlayerServiceStub::SetVideoSurface, "Player::SetVideoSurface" };
92 #endif
93 playerFuncs_[IS_PLAYING] = { &PlayerServiceStub::IsPlaying, "Player::IsPlaying" };
94 playerFuncs_[IS_LOOPING] = { &PlayerServiceStub::IsLooping, "Player::IsLooping" };
95 playerFuncs_[SET_LOOPING] = { &PlayerServiceStub::SetLooping, "Player::SetLooping" };
96 playerFuncs_[SET_RENDERER_DESC] = { &PlayerServiceStub::SetParameter, "Player::SetParameter" };
97 playerFuncs_[DESTROY] = { &PlayerServiceStub::DestroyStub, "Player::DestroyStub" };
98 playerFuncs_[SET_CALLBACK] = { &PlayerServiceStub::SetPlayerCallback, "Player::SetPlayerCallback" };
99 playerFuncs_[GET_VIDEO_TRACK_INFO] = { &PlayerServiceStub::GetVideoTrackInfo, "Player::GetVideoTrackInfo" };
100 playerFuncs_[GET_AUDIO_TRACK_INFO] = { &PlayerServiceStub::GetAudioTrackInfo, "Player::GetAudioTrackInfo" };
101 playerFuncs_[GET_SUBTITLE_TRACK_INFO] = {
102 &PlayerServiceStub::GetSubtitleTrackInfo, "Player::GetSubtitleTrackInfo"
103 };
104 playerFuncs_[GET_VIDEO_WIDTH] = { &PlayerServiceStub::GetVideoWidth, "Player::GetVideoWidth" };
105 playerFuncs_[GET_VIDEO_HEIGHT] = { &PlayerServiceStub::GetVideoHeight, "Player::GetVideoHeight" };
106 playerFuncs_[SELECT_BIT_RATE] = { &PlayerServiceStub::SelectBitRate, "Player::SelectBitRate" };
107 playerFuncs_[SELECT_TRACK] = { &PlayerServiceStub::SelectTrack, "Player::SelectTrack" };
108 playerFuncs_[DESELECT_TRACK] = { &PlayerServiceStub::DeselectTrack, "Player::DeselectTrack" };
109 playerFuncs_[GET_CURRENT_TRACK] = { &PlayerServiceStub::GetCurrentTrack, "Player::GetCurrentTrack" };
110
111 (void)RegisterMonitor(appPid_);
112 }
113
Init()114 int32_t PlayerServiceStub::Init()
115 {
116 if (playerServer_ == nullptr) {
117 playerServer_ = PlayerServer::Create();
118 }
119 CHECK_AND_RETURN_RET_LOG(playerServer_ != nullptr, MSERR_NO_MEMORY, "failed to create PlayerServer");
120
121 appUid_ = IPCSkeleton::GetCallingUid();
122 appPid_ = IPCSkeleton::GetCallingPid();
123 SetPlayerFuncs();
124 return MSERR_OK;
125 }
126
DestroyStub()127 int32_t PlayerServiceStub::DestroyStub()
128 {
129 MediaTrace trace("binder::DestroyStub");
130 playerCallback_ = nullptr;
131 if (playerServer_ != nullptr) {
132 (void)playerServer_->Release();
133 playerServer_ = nullptr;
134 }
135
136 MediaServerManager::GetInstance().DestroyStubObject(MediaServerManager::PLAYER, AsObject());
137 return MSERR_OK;
138 }
139
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)140 int PlayerServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
141 MessageOption &option)
142 {
143 MediaTrace trace("binder::OnRemoteRequest");
144 auto remoteDescriptor = data.ReadInterfaceToken();
145 CHECK_AND_RETURN_RET_LOG(PlayerServiceStub::GetDescriptor() == remoteDescriptor,
146 MSERR_INVALID_OPERATION, "Invalid descriptor");
147
148 auto itFunc = playerFuncs_.find(code);
149 if (itFunc != playerFuncs_.end()) {
150 auto memberFunc = itFunc->second.first;
151 auto funcName = itFunc->second.second;
152 MEDIA_LOGI("Stub: OnRemoteRequest task: %{public}s is received", funcName.c_str());
153 if (memberFunc != nullptr) {
154 auto task = std::make_shared<TaskHandler<int>>([&, this] {
155 (void)IpcRecovery(false);
156 int32_t ret = -1;
157 LISTENER(ret = (this->*memberFunc)(data, reply), funcName, false)
158 return ret;
159 });
160 (void)taskQue_.EnqueueTask(task);
161 auto result = task->GetResult();
162 CHECK_AND_RETURN_RET_LOG(result.HasResult(), MSERR_INVALID_OPERATION,
163 "failed to OnRemoteRequest code: %{public}u", code);
164 return result.Value();
165 }
166 }
167 MEDIA_LOGW("PlayerServiceStub: no member func supporting, applying default process");
168 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
169 }
170
SetListenerObject(const sptr<IRemoteObject> & object)171 int32_t PlayerServiceStub::SetListenerObject(const sptr<IRemoteObject> &object)
172 {
173 MediaTrace trace("binder::SetListenerObject");
174 CHECK_AND_RETURN_RET_LOG(object != nullptr, MSERR_NO_MEMORY, "set listener object is nullptr");
175
176 sptr<IStandardPlayerListener> listener = iface_cast<IStandardPlayerListener>(object);
177 CHECK_AND_RETURN_RET_LOG(listener != nullptr, MSERR_NO_MEMORY, "failed to convert IStandardPlayerListener");
178
179 std::shared_ptr<PlayerCallback> callback = std::make_shared<PlayerListenerCallback>(listener);
180 CHECK_AND_RETURN_RET_LOG(callback != nullptr, MSERR_NO_MEMORY, "failed to new PlayerListenerCallback");
181
182 playerCallback_ = callback;
183 return MSERR_OK;
184 }
185
SetSource(const std::string & url)186 int32_t PlayerServiceStub::SetSource(const std::string &url)
187 {
188 MediaTrace trace("binder::SetSource(url)");
189 CHECK_AND_RETURN_RET_LOG(playerServer_ != nullptr, MSERR_NO_MEMORY, "player server is nullptr");
190 return playerServer_->SetSource(url);
191 }
192
SetSource(const sptr<IRemoteObject> & object)193 int32_t PlayerServiceStub::SetSource(const sptr<IRemoteObject> &object)
194 {
195 MediaTrace trace("binder::SetSource(datasource)");
196 CHECK_AND_RETURN_RET_LOG(object != nullptr, MSERR_NO_MEMORY, "set mediadatasrc object is nullptr");
197 CHECK_AND_RETURN_RET_LOG(playerServer_ != nullptr, MSERR_NO_MEMORY, "player server is nullptr");
198
199 sptr<IStandardMediaDataSource> proxy = iface_cast<IStandardMediaDataSource>(object);
200 CHECK_AND_RETURN_RET_LOG(proxy != nullptr, MSERR_NO_MEMORY, "failed to convert MediaDataSourceProxy");
201
202 std::shared_ptr<IMediaDataSource> mediaDataSrc = std::make_shared<MediaDataCallback>(proxy);
203 CHECK_AND_RETURN_RET_LOG(mediaDataSrc != nullptr, MSERR_NO_MEMORY, "failed to new PlayerListenerCallback");
204
205 return playerServer_->SetSource(mediaDataSrc);
206 }
207
SetSource(int32_t fd,int64_t offset,int64_t size)208 int32_t PlayerServiceStub::SetSource(int32_t fd, int64_t offset, int64_t size)
209 {
210 MediaTrace trace("binder::SetSource(fd)");
211 CHECK_AND_RETURN_RET_LOG(playerServer_ != nullptr, MSERR_NO_MEMORY, "player server is nullptr");
212 return playerServer_->SetSource(fd, offset, size);
213 }
214
AddSubSource(const std::string & url)215 int32_t PlayerServiceStub::AddSubSource(const std::string &url)
216 {
217 MediaTrace trace("binder::AddSubSource(url)");
218 CHECK_AND_RETURN_RET_LOG(playerServer_ != nullptr, MSERR_NO_MEMORY, "player server is nullptr");
219 return playerServer_->AddSubSource(url);
220 }
221
AddSubSource(int32_t fd,int64_t offset,int64_t size)222 int32_t PlayerServiceStub::AddSubSource(int32_t fd, int64_t offset, int64_t size)
223 {
224 MediaTrace trace("binder::AddSubSource(fd)");
225 CHECK_AND_RETURN_RET_LOG(playerServer_ != nullptr, MSERR_NO_MEMORY, "player server is nullptr");
226 return playerServer_->AddSubSource(fd, offset, size);
227 }
228
Play()229 int32_t PlayerServiceStub::Play()
230 {
231 MediaTrace trace("binder::Play");
232 CHECK_AND_RETURN_RET_LOG(playerServer_ != nullptr, MSERR_NO_MEMORY, "player server is nullptr");
233 #ifdef SUPPORT_AVSESSION
234 AVsessionBackground::Instance().AddListener(playerServer_, appUid_);
235 #endif
236 return playerServer_->Play();
237 }
238
Prepare()239 int32_t PlayerServiceStub::Prepare()
240 {
241 MediaTrace trace("binder::Prepare");
242 CHECK_AND_RETURN_RET_LOG(playerServer_ != nullptr, MSERR_NO_MEMORY, "player server is nullptr");
243 return playerServer_->Prepare();
244 }
245
PrepareAsync()246 int32_t PlayerServiceStub::PrepareAsync()
247 {
248 MediaTrace trace("binder::PrepareAsync");
249 CHECK_AND_RETURN_RET_LOG(playerServer_ != nullptr, MSERR_NO_MEMORY, "player server is nullptr");
250 return playerServer_->PrepareAsync();
251 }
252
Pause()253 int32_t PlayerServiceStub::Pause()
254 {
255 MediaTrace trace("binder::Pause");
256 CHECK_AND_RETURN_RET_LOG(playerServer_ != nullptr, MSERR_NO_MEMORY, "player server is nullptr");
257 return playerServer_->Pause();
258 }
259
Stop()260 int32_t PlayerServiceStub::Stop()
261 {
262 MediaTrace trace("binder::Stop");
263 CHECK_AND_RETURN_RET_LOG(playerServer_ != nullptr, MSERR_NO_MEMORY, "player server is nullptr");
264 return playerServer_->Stop();
265 }
266
Reset()267 int32_t PlayerServiceStub::Reset()
268 {
269 MediaTrace trace("binder::Reset");
270 CHECK_AND_RETURN_RET_LOG(playerServer_ != nullptr, MSERR_NO_MEMORY, "player server is nullptr");
271 return playerServer_->Reset();
272 }
273
Release()274 int32_t PlayerServiceStub::Release()
275 {
276 MediaTrace trace("binder::Release");
277 CHECK_AND_RETURN_RET_LOG(playerServer_ != nullptr, MSERR_NO_MEMORY, "player server is nullptr");
278 return playerServer_->Release();
279 }
280
SetVolume(float leftVolume,float rightVolume)281 int32_t PlayerServiceStub::SetVolume(float leftVolume, float rightVolume)
282 {
283 MediaTrace trace("binder::SetVolume");
284 CHECK_AND_RETURN_RET_LOG(playerServer_ != nullptr, MSERR_NO_MEMORY, "player server is nullptr");
285 return playerServer_->SetVolume(leftVolume, rightVolume);
286 }
287
Seek(int32_t mSeconds,PlayerSeekMode mode)288 int32_t PlayerServiceStub::Seek(int32_t mSeconds, PlayerSeekMode mode)
289 {
290 MediaTrace trace("binder::Seek");
291 CHECK_AND_RETURN_RET_LOG(playerServer_ != nullptr, MSERR_NO_MEMORY, "player server is nullptr");
292 return playerServer_->Seek(mSeconds, mode);
293 }
294
GetCurrentTime(int32_t & currentTime)295 int32_t PlayerServiceStub::GetCurrentTime(int32_t ¤tTime)
296 {
297 MediaTrace trace("binder::GetCurrentTime");
298 CHECK_AND_RETURN_RET_LOG(playerServer_ != nullptr, MSERR_NO_MEMORY, "player server is nullptr");
299 return playerServer_->GetCurrentTime(currentTime);
300 }
301
GetVideoTrackInfo(std::vector<Format> & videoTrack)302 int32_t PlayerServiceStub::GetVideoTrackInfo(std::vector<Format> &videoTrack)
303 {
304 MediaTrace trace("binder::GetVideoTrackInfo");
305 CHECK_AND_RETURN_RET_LOG(playerServer_ != nullptr, MSERR_NO_MEMORY, "player server is nullptr");
306 return playerServer_->GetVideoTrackInfo(videoTrack);
307 }
308
GetAudioTrackInfo(std::vector<Format> & audioTrack)309 int32_t PlayerServiceStub::GetAudioTrackInfo(std::vector<Format> &audioTrack)
310 {
311 MediaTrace trace("binder::GetAudioTrackInfo");
312 CHECK_AND_RETURN_RET_LOG(playerServer_ != nullptr, MSERR_NO_MEMORY, "player server is nullptr");
313 return playerServer_->GetAudioTrackInfo(audioTrack);
314 }
315
GetSubtitleTrackInfo(std::vector<Format> & subtitleTrack)316 int32_t PlayerServiceStub::GetSubtitleTrackInfo(std::vector<Format> &subtitleTrack)
317 {
318 MediaTrace trace("binder::GetSubtitleTrackInfo");
319 CHECK_AND_RETURN_RET_LOG(playerServer_ != nullptr, MSERR_NO_MEMORY, "player server is nullptr");
320 return playerServer_->GetSubtitleTrackInfo(subtitleTrack);
321 }
322
GetVideoWidth()323 int32_t PlayerServiceStub::GetVideoWidth()
324 {
325 MediaTrace trace("binder::GetVideoWidth");
326 CHECK_AND_RETURN_RET_LOG(playerServer_ != nullptr, MSERR_NO_MEMORY, "player server is nullptr");
327 return playerServer_->GetVideoWidth();
328 }
329
GetVideoHeight()330 int32_t PlayerServiceStub::GetVideoHeight()
331 {
332 MediaTrace trace("binder::GetVideoHeight");
333 CHECK_AND_RETURN_RET_LOG(playerServer_ != nullptr, MSERR_NO_MEMORY, "player server is nullptr");
334 return playerServer_->GetVideoHeight();
335 }
336
GetDuration(int32_t & duration)337 int32_t PlayerServiceStub::GetDuration(int32_t &duration)
338 {
339 MediaTrace trace("binder::GetDuration");
340 CHECK_AND_RETURN_RET_LOG(playerServer_ != nullptr, MSERR_NO_MEMORY, "player server is nullptr");
341 return playerServer_->GetDuration(duration);
342 }
343
SetPlaybackSpeed(PlaybackRateMode mode)344 int32_t PlayerServiceStub::SetPlaybackSpeed(PlaybackRateMode mode)
345 {
346 MediaTrace trace("binder::SetPlaybackSpeed");
347 CHECK_AND_RETURN_RET_LOG(playerServer_ != nullptr, MSERR_NO_MEMORY, "player server is nullptr");
348 return playerServer_->SetPlaybackSpeed(mode);
349 }
350
GetPlaybackSpeed(PlaybackRateMode & mode)351 int32_t PlayerServiceStub::GetPlaybackSpeed(PlaybackRateMode &mode)
352 {
353 MediaTrace trace("binder::GetPlaybackSpeed");
354 CHECK_AND_RETURN_RET_LOG(playerServer_ != nullptr, MSERR_NO_MEMORY, "player server is nullptr");
355 return playerServer_->GetPlaybackSpeed(mode);
356 }
357
SelectBitRate(uint32_t bitRate)358 int32_t PlayerServiceStub::SelectBitRate(uint32_t bitRate)
359 {
360 MediaTrace trace("binder::SelectBitRate");
361 CHECK_AND_RETURN_RET_LOG(playerServer_ != nullptr, MSERR_NO_MEMORY, "player server is nullptr");
362 return playerServer_->SelectBitRate(bitRate);
363 }
364
365 #ifdef SUPPORT_VIDEO
SetVideoSurface(sptr<Surface> surface)366 int32_t PlayerServiceStub::SetVideoSurface(sptr<Surface> surface)
367 {
368 MediaTrace trace("binder::SetVideoSurface");
369 MEDIA_LOGD("SetVideoSurface");
370 CHECK_AND_RETURN_RET_LOG(playerServer_ != nullptr, MSERR_NO_MEMORY, "player server is nullptr");
371 return playerServer_->SetVideoSurface(surface);
372 }
373 #endif
374
IsPlaying()375 bool PlayerServiceStub::IsPlaying()
376 {
377 MediaTrace trace("binder::IsPlaying");
378 CHECK_AND_RETURN_RET_LOG(playerServer_ != nullptr, false, "player server is nullptr");
379 return playerServer_->IsPlaying();
380 }
381
IsLooping()382 bool PlayerServiceStub::IsLooping()
383 {
384 MediaTrace trace("binder::IsLooping");
385 CHECK_AND_RETURN_RET_LOG(playerServer_ != nullptr, false, "player server is nullptr");
386 return playerServer_->IsLooping();
387 }
388
SetLooping(bool loop)389 int32_t PlayerServiceStub::SetLooping(bool loop)
390 {
391 MediaTrace trace("binder::SetLooping");
392 CHECK_AND_RETURN_RET_LOG(playerServer_ != nullptr, MSERR_NO_MEMORY, "player server is nullptr");
393 return playerServer_->SetLooping(loop);
394 }
395
SetParameter(const Format & param)396 int32_t PlayerServiceStub::SetParameter(const Format ¶m)
397 {
398 MediaTrace trace("binder::SetParameter");
399 CHECK_AND_RETURN_RET_LOG(playerServer_ != nullptr, MSERR_NO_MEMORY, "player server is nullptr");
400 return playerServer_->SetParameter(param);
401 }
402
SetPlayerCallback()403 int32_t PlayerServiceStub::SetPlayerCallback()
404 {
405 MediaTrace trace("binder::SetPlayerCallback");
406 MEDIA_LOGD("SetPlayerCallback");
407 CHECK_AND_RETURN_RET_LOG(playerServer_ != nullptr, MSERR_NO_MEMORY, "player server is nullptr");
408 return playerServer_->SetPlayerCallback(playerCallback_);
409 }
410
DumpInfo(int32_t fd)411 int32_t PlayerServiceStub::DumpInfo(int32_t fd)
412 {
413 CHECK_AND_RETURN_RET_LOG(playerServer_ != nullptr, MSERR_NO_MEMORY, "player server is nullptr");
414 return std::static_pointer_cast<PlayerServer>(playerServer_)->DumpInfo(fd);
415 }
416
DoIpcAbnormality()417 int32_t PlayerServiceStub::DoIpcAbnormality()
418 {
419 MEDIA_LOGI("Enter DoIpcAbnormality.");
420 auto task = std::make_shared<TaskHandler<int>>([&, this] {
421 MEDIA_LOGI("DoIpcAbnormality.");
422 CHECK_AND_RETURN_RET_LOG(playerServer_ != nullptr, static_cast<int>(MSERR_NO_MEMORY),
423 "player server is nullptr");
424 CHECK_AND_RETURN_RET_LOG(IsPlaying(), static_cast<int>(MSERR_INVALID_OPERATION), "Not in playback state");
425 auto playerServer = std::static_pointer_cast<PlayerServer>(playerServer_);
426 int32_t ret = playerServer->BackGroundChangeState(PlayerStates::PLAYER_PAUSED, false);
427 CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "DoIpcAbnormality End.");
428 SetIpcAlarmedFlag();
429 MEDIA_LOGI("DoIpcAbnormality End.");
430 return ret;
431 });
432 (void)taskQue_.EnqueueTask(task);
433 return MSERR_OK;
434 }
435
DoIpcRecovery(bool fromMonitor)436 int32_t PlayerServiceStub::DoIpcRecovery(bool fromMonitor)
437 {
438 MEDIA_LOGI("Enter DoIpcRecovery %{public}d.", fromMonitor);
439 CHECK_AND_RETURN_RET_LOG(playerServer_ != nullptr, MSERR_NO_MEMORY, "player server is nullptr");
440 if (fromMonitor) {
441 auto task = std::make_shared<TaskHandler<int>>([&, this] {
442 MEDIA_LOGI("DoIpcRecovery.");
443 auto playerServer = std::static_pointer_cast<PlayerServer>(playerServer_);
444 int32_t ret = playerServer->BackGroundChangeState(PlayerStates::PLAYER_STARTED, false);
445 CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK || ret == MSERR_INVALID_OPERATION, ret, "Failed to ChangeState");
446 UnSetIpcAlarmedFlag();
447 MEDIA_LOGI("DoIpcRecovery End.");
448 return ret;
449 });
450 (void)taskQue_.EnqueueTask(task);
451 } else {
452 auto playerServer = std::static_pointer_cast<PlayerServer>(playerServer_);
453 int32_t ret = playerServer->BackGroundChangeState(PlayerStates::PLAYER_STARTED, false);
454 CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK || ret == MSERR_INVALID_OPERATION, ret, "Failed to ChangeState");
455 UnSetIpcAlarmedFlag();
456 }
457 return MSERR_OK;
458 }
459
SelectTrack(int32_t index)460 int32_t PlayerServiceStub::SelectTrack(int32_t index)
461 {
462 MediaTrace trace("PlayerServiceStub::SelectTrack");
463 CHECK_AND_RETURN_RET_LOG(playerServer_ != nullptr, MSERR_NO_MEMORY, "player server is nullptr");
464 return playerServer_->SelectTrack(index);
465 }
466
DeselectTrack(int32_t index)467 int32_t PlayerServiceStub::DeselectTrack(int32_t index)
468 {
469 MediaTrace trace("PlayerServiceStub::DeselectTrack");
470 CHECK_AND_RETURN_RET_LOG(playerServer_ != nullptr, MSERR_NO_MEMORY, "player server is nullptr");
471 return playerServer_->DeselectTrack(index);
472 }
473
GetCurrentTrack(int32_t trackType,int32_t & index)474 int32_t PlayerServiceStub::GetCurrentTrack(int32_t trackType, int32_t &index)
475 {
476 MediaTrace trace("PlayerServiceStub::GetCurrentTrack");
477 CHECK_AND_RETURN_RET_LOG(playerServer_ != nullptr, MSERR_NO_MEMORY, "player server is nullptr");
478 return playerServer_->GetCurrentTrack(trackType, index);
479 }
480
SetListenerObject(MessageParcel & data,MessageParcel & reply)481 int32_t PlayerServiceStub::SetListenerObject(MessageParcel &data, MessageParcel &reply)
482 {
483 sptr<IRemoteObject> object = data.ReadRemoteObject();
484 reply.WriteInt32(SetListenerObject(object));
485 return MSERR_OK;
486 }
487
SetSource(MessageParcel & data,MessageParcel & reply)488 int32_t PlayerServiceStub::SetSource(MessageParcel &data, MessageParcel &reply)
489 {
490 std::string url = data.ReadString();
491 reply.WriteInt32(SetSource(url));
492 return MSERR_OK;
493 }
494
SetMediaDataSource(MessageParcel & data,MessageParcel & reply)495 int32_t PlayerServiceStub::SetMediaDataSource(MessageParcel &data, MessageParcel &reply)
496 {
497 sptr<IRemoteObject> object = data.ReadRemoteObject();
498 reply.WriteInt32(SetSource(object));
499 return MSERR_OK;
500 }
501
SetFdSource(MessageParcel & data,MessageParcel & reply)502 int32_t PlayerServiceStub::SetFdSource(MessageParcel &data, MessageParcel &reply)
503 {
504 int32_t fd = data.ReadFileDescriptor();
505 int64_t offset = data.ReadInt64();
506 int64_t size = data.ReadInt64();
507 reply.WriteInt32(SetSource(fd, offset, size));
508 (void)::close(fd);
509 return MSERR_OK;
510 }
511
AddSubSource(MessageParcel & data,MessageParcel & reply)512 int32_t PlayerServiceStub::AddSubSource(MessageParcel &data, MessageParcel &reply)
513 {
514 std::string url = data.ReadString();
515 reply.WriteInt32(AddSubSource(url));
516 return MSERR_OK;
517 }
518
AddSubFdSource(MessageParcel & data,MessageParcel & reply)519 int32_t PlayerServiceStub::AddSubFdSource(MessageParcel &data, MessageParcel &reply)
520 {
521 int32_t fd = data.ReadFileDescriptor();
522 int64_t offset = data.ReadInt64();
523 int64_t size = data.ReadInt64();
524 reply.WriteInt32(AddSubSource(fd, offset, size));
525 (void)::close(fd);
526 return MSERR_OK;
527 }
528
Play(MessageParcel & data,MessageParcel & reply)529 int32_t PlayerServiceStub::Play(MessageParcel &data, MessageParcel &reply)
530 {
531 (void)data;
532 reply.WriteInt32(Play());
533 return MSERR_OK;
534 }
535
Prepare(MessageParcel & data,MessageParcel & reply)536 int32_t PlayerServiceStub::Prepare(MessageParcel &data, MessageParcel &reply)
537 {
538 (void)data;
539 reply.WriteInt32(Prepare());
540 return MSERR_OK;
541 }
542
PrepareAsync(MessageParcel & data,MessageParcel & reply)543 int32_t PlayerServiceStub::PrepareAsync(MessageParcel &data, MessageParcel &reply)
544 {
545 (void)data;
546 reply.WriteInt32(PrepareAsync());
547 return MSERR_OK;
548 }
549
Pause(MessageParcel & data,MessageParcel & reply)550 int32_t PlayerServiceStub::Pause(MessageParcel &data, MessageParcel &reply)
551 {
552 (void)data;
553 reply.WriteInt32(Pause());
554 return MSERR_OK;
555 }
556
Stop(MessageParcel & data,MessageParcel & reply)557 int32_t PlayerServiceStub::Stop(MessageParcel &data, MessageParcel &reply)
558 {
559 (void)data;
560 reply.WriteInt32(Stop());
561 return MSERR_OK;
562 }
563
Reset(MessageParcel & data,MessageParcel & reply)564 int32_t PlayerServiceStub::Reset(MessageParcel &data, MessageParcel &reply)
565 {
566 (void)data;
567 reply.WriteInt32(Reset());
568 return MSERR_OK;
569 }
570
Release(MessageParcel & data,MessageParcel & reply)571 int32_t PlayerServiceStub::Release(MessageParcel &data, MessageParcel &reply)
572 {
573 (void)data;
574 reply.WriteInt32(Release());
575 return MSERR_OK;
576 }
577
SetVolume(MessageParcel & data,MessageParcel & reply)578 int32_t PlayerServiceStub::SetVolume(MessageParcel &data, MessageParcel &reply)
579 {
580 float leftVolume = data.ReadFloat();
581 float rightVolume = data.ReadFloat();
582 reply.WriteInt32(SetVolume(leftVolume, rightVolume));
583 return MSERR_OK;
584 }
585
Seek(MessageParcel & data,MessageParcel & reply)586 int32_t PlayerServiceStub::Seek(MessageParcel &data, MessageParcel &reply)
587 {
588 int32_t mSeconds = data.ReadInt32();
589 int32_t mode = data.ReadInt32();
590 reply.WriteInt32(Seek(mSeconds, static_cast<PlayerSeekMode>(mode)));
591 return MSERR_OK;
592 }
593
GetCurrentTime(MessageParcel & data,MessageParcel & reply)594 int32_t PlayerServiceStub::GetCurrentTime(MessageParcel &data, MessageParcel &reply)
595 {
596 (void)data;
597 int32_t currentTime = -1;
598 int32_t ret = GetCurrentTime(currentTime);
599 reply.WriteInt32(currentTime);
600 reply.WriteInt32(ret);
601 return MSERR_OK;
602 }
603
GetVideoTrackInfo(MessageParcel & data,MessageParcel & reply)604 int32_t PlayerServiceStub::GetVideoTrackInfo(MessageParcel &data, MessageParcel &reply)
605 {
606 (void)data;
607 std::vector<Format> videoTrack;
608 int32_t ret = GetVideoTrackInfo(videoTrack);
609 reply.WriteInt32(static_cast<int32_t>(videoTrack.size()));
610 for (auto iter = videoTrack.begin(); iter != videoTrack.end(); iter++) {
611 (void)MediaParcel::Marshalling(reply, *iter);
612 }
613 reply.WriteInt32(ret);
614
615 return MSERR_OK;
616 }
617
GetAudioTrackInfo(MessageParcel & data,MessageParcel & reply)618 int32_t PlayerServiceStub::GetAudioTrackInfo(MessageParcel &data, MessageParcel &reply)
619 {
620 (void)data;
621 std::vector<Format> audioTrack;
622 int32_t ret = GetAudioTrackInfo(audioTrack);
623 reply.WriteInt32(static_cast<int32_t>(audioTrack.size()));
624 for (auto iter = audioTrack.begin(); iter != audioTrack.end(); iter++) {
625 (void)MediaParcel::Marshalling(reply, *iter);
626 }
627 reply.WriteInt32(ret);
628
629 return MSERR_OK;
630 }
631
GetSubtitleTrackInfo(MessageParcel & data,MessageParcel & reply)632 int32_t PlayerServiceStub::GetSubtitleTrackInfo(MessageParcel &data, MessageParcel &reply)
633 {
634 (void)data;
635 std::vector<Format> subtitleTrack;
636 int32_t ret = GetSubtitleTrackInfo(subtitleTrack);
637 reply.WriteInt32(static_cast<int32_t>(subtitleTrack.size()));
638 for (auto iter = subtitleTrack.begin(); iter != subtitleTrack.end(); iter++) {
639 (void)MediaParcel::Marshalling(reply, *iter);
640 }
641 reply.WriteInt32(ret);
642
643 return MSERR_OK;
644 }
645
GetVideoWidth(MessageParcel & data,MessageParcel & reply)646 int32_t PlayerServiceStub::GetVideoWidth(MessageParcel &data, MessageParcel &reply)
647 {
648 (void)data;
649 int32_t witdh = GetVideoWidth();
650 reply.WriteInt32(witdh);
651
652 return MSERR_OK;
653 }
654
GetVideoHeight(MessageParcel & data,MessageParcel & reply)655 int32_t PlayerServiceStub::GetVideoHeight(MessageParcel &data, MessageParcel &reply)
656 {
657 (void)data;
658 int32_t height = GetVideoHeight();
659 reply.WriteInt32(height);
660
661 return MSERR_OK;
662 }
663
GetDuration(MessageParcel & data,MessageParcel & reply)664 int32_t PlayerServiceStub::GetDuration(MessageParcel &data, MessageParcel &reply)
665 {
666 (void)data;
667 int32_t duration = -1;
668 int32_t ret = GetDuration(duration);
669 reply.WriteInt32(duration);
670 reply.WriteInt32(ret);
671 return MSERR_OK;
672 }
673
SetPlaybackSpeed(MessageParcel & data,MessageParcel & reply)674 int32_t PlayerServiceStub::SetPlaybackSpeed(MessageParcel &data, MessageParcel &reply)
675 {
676 int32_t mode = data.ReadInt32();
677 reply.WriteInt32(SetPlaybackSpeed(static_cast<PlaybackRateMode>(mode)));
678 return MSERR_OK;
679 }
680
GetPlaybackSpeed(MessageParcel & data,MessageParcel & reply)681 int32_t PlayerServiceStub::GetPlaybackSpeed(MessageParcel &data, MessageParcel &reply)
682 {
683 (void)data;
684 PlaybackRateMode mode = SPEED_FORWARD_1_00_X;
685 int32_t ret = GetPlaybackSpeed(mode);
686 reply.WriteInt32(mode);
687 reply.WriteInt32(ret);
688 return MSERR_OK;
689 }
690
SelectBitRate(MessageParcel & data,MessageParcel & reply)691 int32_t PlayerServiceStub::SelectBitRate(MessageParcel &data, MessageParcel &reply)
692 {
693 int32_t bitrate = data.ReadInt32();
694 reply.WriteInt32(SelectBitRate(static_cast<uint32_t>(bitrate)));
695 return MSERR_OK;
696 }
697
698 #ifdef SUPPORT_VIDEO
SetVideoSurface(MessageParcel & data,MessageParcel & reply)699 int32_t PlayerServiceStub::SetVideoSurface(MessageParcel &data, MessageParcel &reply)
700 {
701 sptr<IRemoteObject> object = data.ReadRemoteObject();
702 CHECK_AND_RETURN_RET_LOG(object != nullptr, MSERR_NO_MEMORY, "object is nullptr");
703
704 sptr<IBufferProducer> producer = iface_cast<IBufferProducer>(object);
705 CHECK_AND_RETURN_RET_LOG(producer != nullptr, MSERR_NO_MEMORY, "failed to convert object to producer");
706
707 sptr<Surface> surface = Surface::CreateSurfaceAsProducer(producer);
708 CHECK_AND_RETURN_RET_LOG(surface != nullptr, MSERR_NO_MEMORY, "failed to create surface");
709
710 std::string format = data.ReadString();
711 MEDIA_LOGI("surfaceFormat is %{public}s!", format.c_str());
712 (void)surface->SetUserData("SURFACE_FORMAT", format);
713 reply.WriteInt32(SetVideoSurface(surface));
714 return MSERR_OK;
715 }
716 #endif
717
IsPlaying(MessageParcel & data,MessageParcel & reply)718 int32_t PlayerServiceStub::IsPlaying(MessageParcel &data, MessageParcel &reply)
719 {
720 (void)data;
721 reply.WriteBool(IsPlaying());
722 return MSERR_OK;
723 }
724
IsLooping(MessageParcel & data,MessageParcel & reply)725 int32_t PlayerServiceStub::IsLooping(MessageParcel &data, MessageParcel &reply)
726 {
727 (void)data;
728 reply.WriteBool(IsLooping());
729 return MSERR_OK;
730 }
731
SetLooping(MessageParcel & data,MessageParcel & reply)732 int32_t PlayerServiceStub::SetLooping(MessageParcel &data, MessageParcel &reply)
733 {
734 bool loop = data.ReadBool();
735 reply.WriteInt32(SetLooping(loop));
736 return MSERR_OK;
737 }
738
SetParameter(MessageParcel & data,MessageParcel & reply)739 int32_t PlayerServiceStub::SetParameter(MessageParcel &data, MessageParcel &reply)
740 {
741 Format param;
742 (void)MediaParcel::Unmarshalling(data, param);
743
744 reply.WriteInt32(SetParameter(param));
745
746 return MSERR_OK;
747 }
748
DestroyStub(MessageParcel & data,MessageParcel & reply)749 int32_t PlayerServiceStub::DestroyStub(MessageParcel &data, MessageParcel &reply)
750 {
751 (void)data;
752 reply.WriteInt32(DestroyStub());
753 return MSERR_OK;
754 }
755
SetPlayerCallback(MessageParcel & data,MessageParcel & reply)756 int32_t PlayerServiceStub::SetPlayerCallback(MessageParcel &data, MessageParcel &reply)
757 {
758 (void)data;
759 reply.WriteInt32(SetPlayerCallback());
760 return MSERR_OK;
761 }
762
SelectTrack(MessageParcel & data,MessageParcel & reply)763 int32_t PlayerServiceStub::SelectTrack(MessageParcel &data, MessageParcel &reply)
764 {
765 int32_t index = data.ReadInt32();
766 reply.WriteInt32(SelectTrack(index));
767 return MSERR_OK;
768 }
769
DeselectTrack(MessageParcel & data,MessageParcel & reply)770 int32_t PlayerServiceStub::DeselectTrack(MessageParcel &data, MessageParcel &reply)
771 {
772 int32_t index = data.ReadInt32();
773 reply.WriteInt32(DeselectTrack(index));
774 return MSERR_OK;
775 }
776
GetCurrentTrack(MessageParcel & data,MessageParcel & reply)777 int32_t PlayerServiceStub::GetCurrentTrack(MessageParcel &data, MessageParcel &reply)
778 {
779 int32_t trackType = data.ReadInt32();
780 int32_t index = -1;
781 int32_t ret = GetCurrentTrack(trackType, index);
782 reply.WriteInt32(index);
783 reply.WriteInt32(ret);
784 return MSERR_OK;
785 }
786 } // namespace Media
787 } // namespace OHOS
788