• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_client.h"
17 #include "media_log.h"
18 #include "media_errors.h"
19 
20 namespace {
21 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN, "PlayerClient"};
22 }
23 
24 namespace OHOS {
25 namespace Media {
Create(const sptr<IStandardPlayerService> & ipcProxy)26 std::shared_ptr<PlayerClient> PlayerClient::Create(const sptr<IStandardPlayerService> &ipcProxy)
27 {
28     std::shared_ptr<PlayerClient> player = std::make_shared<PlayerClient>(ipcProxy);
29 
30     int32_t ret = player->CreateListenerObject();
31     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, nullptr, "failed to create listener object..");
32 
33     return player;
34 }
35 
PlayerClient(const sptr<IStandardPlayerService> & ipcProxy)36 PlayerClient::PlayerClient(const sptr<IStandardPlayerService> &ipcProxy)
37     : playerProxy_(ipcProxy)
38 {
39     MEDIA_LOGD("0x%{public}06" PRIXPTR " Instances create", FAKE_POINTER(this));
40 }
41 
~PlayerClient()42 PlayerClient::~PlayerClient()
43 {
44     std::lock_guard<std::mutex> lock(mutex_);
45     (void)DisableMonitor();
46     callback_ = nullptr;
47     listenerStub_ = nullptr;
48     if (playerProxy_ != nullptr) {
49         (void)playerProxy_->DestroyStub();
50     }
51     MEDIA_LOGD("0x%{public}06" PRIXPTR " Instances destroy", FAKE_POINTER(this));
52 }
53 
CreateListenerObject()54 int32_t PlayerClient::CreateListenerObject()
55 {
56     std::lock_guard<std::mutex> lock(mutex_);
57     listenerStub_ = new(std::nothrow) PlayerListenerStub();
58     CHECK_AND_RETURN_RET_LOG(listenerStub_ != nullptr, MSERR_NO_MEMORY, "failed to new PlayerListenerStub object");
59     CHECK_AND_RETURN_RET_LOG(playerProxy_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
60 
61     (void)listenerStub_->SetMonitor(weak_from_this());
62     sptr<IRemoteObject> object = listenerStub_->AsObject();
63     CHECK_AND_RETURN_RET_LOG(object != nullptr, MSERR_NO_MEMORY, "listener object is nullptr..");
64 
65     MEDIA_LOGD("SetListenerObject");
66     return playerProxy_->SetListenerObject(object);
67 }
68 
MediaServerDied()69 void PlayerClient::MediaServerDied()
70 {
71     std::lock_guard<std::mutex> lock(mutex_);
72     playerProxy_ = nullptr;
73     listenerStub_ = nullptr;
74     if (callback_ != nullptr) {
75         callback_->OnError(MSERR_SERVICE_DIED,
76             "mediaserver is died, please create a new playback instance again");
77     }
78 }
79 
SetSource(const std::string & url)80 int32_t PlayerClient::SetSource(const std::string &url)
81 {
82     std::lock_guard<std::mutex> lock(mutex_);
83     CHECK_AND_RETURN_RET_LOG(playerProxy_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
84     return playerProxy_->SetSource(url);
85 }
86 
SetSource(const std::shared_ptr<IMediaDataSource> & dataSrc)87 int32_t PlayerClient::SetSource(const std::shared_ptr<IMediaDataSource> &dataSrc)
88 {
89     std::lock_guard<std::mutex> lock(mutex_);
90     CHECK_AND_RETURN_RET_LOG(playerProxy_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
91     CHECK_AND_RETURN_RET_LOG(dataSrc != nullptr, MSERR_NO_MEMORY, "data source is nullptr");
92 
93     dataSrcStub_ = new(std::nothrow) MediaDataSourceStub(dataSrc);
94     CHECK_AND_RETURN_RET_LOG(dataSrcStub_ != nullptr, MSERR_NO_MEMORY, "failed to new dataSrcStub object");
95 
96     sptr<IRemoteObject> object = dataSrcStub_->AsObject();
97     CHECK_AND_RETURN_RET_LOG(object != nullptr, MSERR_NO_MEMORY, "listener object is nullptr..");
98     return playerProxy_->SetSource(object);
99 }
100 
SetSource(int32_t fd,int64_t offset,int64_t size)101 int32_t PlayerClient::SetSource(int32_t fd, int64_t offset, int64_t size)
102 {
103     std::lock_guard<std::mutex> lock(mutex_);
104     CHECK_AND_RETURN_RET_LOG(playerProxy_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
105     return playerProxy_->SetSource(fd, offset, size);
106 }
107 
DisableWhenOK(int32_t ret)108 int32_t PlayerClient::DisableWhenOK(int32_t ret)
109 {
110     if (ret == MSERR_OK) {
111         (void)DisableMonitor();
112     }
113     return ret;
114 }
115 
AddSubSource(const std::string & url)116 int32_t PlayerClient::AddSubSource(const std::string &url)
117 {
118     std::lock_guard<std::mutex> lock(mutex_);
119     CHECK_AND_RETURN_RET_LOG(playerProxy_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
120     return playerProxy_->AddSubSource(url);
121 }
122 
AddSubSource(int32_t fd,int64_t offset,int64_t size)123 int32_t PlayerClient::AddSubSource(int32_t fd, int64_t offset, int64_t size)
124 {
125     std::lock_guard<std::mutex> lock(mutex_);
126     CHECK_AND_RETURN_RET_LOG(playerProxy_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
127     return playerProxy_->AddSubSource(fd, offset, size);
128 }
129 
Play()130 int32_t PlayerClient::Play()
131 {
132     std::lock_guard<std::mutex> lock(mutex_);
133     CHECK_AND_RETURN_RET_LOG(playerProxy_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
134     (void)EnableMonitor();
135     return playerProxy_->Play();
136 }
137 
Prepare()138 int32_t PlayerClient::Prepare()
139 {
140     std::lock_guard<std::mutex> lock(mutex_);
141     CHECK_AND_RETURN_RET_LOG(playerProxy_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
142     return playerProxy_->Prepare();
143 }
144 
PrepareAsync()145 int32_t PlayerClient::PrepareAsync()
146 {
147     std::lock_guard<std::mutex> lock(mutex_);
148     CHECK_AND_RETURN_RET_LOG(playerProxy_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
149     return playerProxy_->PrepareAsync();
150 }
151 
Pause()152 int32_t PlayerClient::Pause()
153 {
154     std::lock_guard<std::mutex> lock(mutex_);
155     CHECK_AND_RETURN_RET_LOG(playerProxy_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
156     return DisableWhenOK(playerProxy_->Pause());
157 }
158 
Stop()159 int32_t PlayerClient::Stop()
160 {
161     std::lock_guard<std::mutex> lock(mutex_);
162     CHECK_AND_RETURN_RET_LOG(playerProxy_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
163     return DisableWhenOK(playerProxy_->Stop());
164 }
165 
Reset()166 int32_t PlayerClient::Reset()
167 {
168     std::lock_guard<std::mutex> lock(mutex_);
169     dataSrcStub_ = nullptr;
170     CHECK_AND_RETURN_RET_LOG(playerProxy_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
171     return DisableWhenOK(playerProxy_->Reset());
172 }
173 
Release()174 int32_t PlayerClient::Release()
175 {
176     std::lock_guard<std::mutex> lock(mutex_);
177     callback_ = nullptr;
178     listenerStub_ = nullptr;
179     dataSrcStub_ = nullptr;
180 
181     CHECK_AND_RETURN_RET_LOG(playerProxy_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
182     return DisableWhenOK(playerProxy_->Release());
183 }
184 
ReleaseSync()185 int32_t PlayerClient::ReleaseSync()
186 {
187     std::lock_guard<std::mutex> lock(mutex_);
188     callback_ = nullptr;
189     listenerStub_ = nullptr;
190     dataSrcStub_ = nullptr;
191 
192     CHECK_AND_RETURN_RET_LOG(playerProxy_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
193     return DisableWhenOK(playerProxy_->ReleaseSync());
194 }
195 
SetVolume(float leftVolume,float rightVolume)196 int32_t PlayerClient::SetVolume(float leftVolume, float rightVolume)
197 {
198     std::lock_guard<std::mutex> lock(mutex_);
199     CHECK_AND_RETURN_RET_LOG(playerProxy_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
200     return playerProxy_->SetVolume(leftVolume, rightVolume);
201 }
202 
Seek(int32_t mSeconds,PlayerSeekMode mode)203 int32_t PlayerClient::Seek(int32_t mSeconds, PlayerSeekMode mode)
204 {
205     std::lock_guard<std::mutex> lock(mutex_);
206     CHECK_AND_RETURN_RET_LOG(playerProxy_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
207     return playerProxy_->Seek(mSeconds, mode);
208 }
209 
GetCurrentTime(int32_t & currentTime)210 int32_t PlayerClient::GetCurrentTime(int32_t &currentTime)
211 {
212     std::lock_guard<std::mutex> lock(mutex_);
213     CHECK_AND_RETURN_RET_LOG(playerProxy_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
214     return playerProxy_->GetCurrentTime(currentTime);
215 }
216 
GetVideoTrackInfo(std::vector<Format> & videoTrack)217 int32_t PlayerClient::GetVideoTrackInfo(std::vector<Format> &videoTrack)
218 {
219     std::lock_guard<std::mutex> lock(mutex_);
220     CHECK_AND_RETURN_RET_LOG(playerProxy_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
221     return playerProxy_->GetVideoTrackInfo(videoTrack);
222 }
223 
GetAudioTrackInfo(std::vector<Format> & audioTrack)224 int32_t PlayerClient::GetAudioTrackInfo(std::vector<Format> &audioTrack)
225 {
226     std::lock_guard<std::mutex> lock(mutex_);
227     CHECK_AND_RETURN_RET_LOG(playerProxy_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
228     return playerProxy_->GetAudioTrackInfo(audioTrack);
229 }
230 
GetSubtitleTrackInfo(std::vector<Format> & subtitleTrack)231 int32_t PlayerClient::GetSubtitleTrackInfo(std::vector<Format> &subtitleTrack)
232 {
233     std::lock_guard<std::mutex> lock(mutex_);
234     CHECK_AND_RETURN_RET_LOG(playerProxy_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
235     return playerProxy_->GetSubtitleTrackInfo(subtitleTrack);
236 }
237 
GetVideoWidth()238 int32_t PlayerClient::GetVideoWidth()
239 {
240     std::lock_guard<std::mutex> lock(mutex_);
241     CHECK_AND_RETURN_RET_LOG(playerProxy_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
242     return playerProxy_->GetVideoWidth();
243 }
244 
GetVideoHeight()245 int32_t PlayerClient::GetVideoHeight()
246 {
247     std::lock_guard<std::mutex> lock(mutex_);
248     CHECK_AND_RETURN_RET_LOG(playerProxy_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
249     return playerProxy_->GetVideoHeight();
250 }
251 
SetPlaybackSpeed(PlaybackRateMode mode)252 int32_t PlayerClient::SetPlaybackSpeed(PlaybackRateMode mode)
253 {
254     std::lock_guard<std::mutex> lock(mutex_);
255     CHECK_AND_RETURN_RET_LOG(playerProxy_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
256     return playerProxy_->SetPlaybackSpeed(mode);
257 }
258 
GetPlaybackSpeed(PlaybackRateMode & mode)259 int32_t PlayerClient::GetPlaybackSpeed(PlaybackRateMode &mode)
260 {
261     std::lock_guard<std::mutex> lock(mutex_);
262     CHECK_AND_RETURN_RET_LOG(playerProxy_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
263     return playerProxy_->GetPlaybackSpeed(mode);
264 }
265 
SelectBitRate(uint32_t bitRate)266 int32_t PlayerClient::SelectBitRate(uint32_t bitRate)
267 {
268     std::lock_guard<std::mutex> lock(mutex_);
269     CHECK_AND_RETURN_RET_LOG(playerProxy_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
270     return playerProxy_->SelectBitRate(bitRate);
271 }
272 
GetDuration(int32_t & duration)273 int32_t PlayerClient::GetDuration(int32_t &duration)
274 {
275     std::lock_guard<std::mutex> lock(mutex_);
276     CHECK_AND_RETURN_RET_LOG(playerProxy_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
277     return playerProxy_->GetDuration(duration);
278 }
279 
280 #ifdef SUPPORT_VIDEO
SetVideoSurface(sptr<Surface> surface)281 int32_t PlayerClient::SetVideoSurface(sptr<Surface> surface)
282 {
283     std::lock_guard<std::mutex> lock(mutex_);
284     CHECK_AND_RETURN_RET_LOG(playerProxy_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
285     CHECK_AND_RETURN_RET_LOG(surface != nullptr, MSERR_NO_MEMORY, "surface is nullptr..");
286     return playerProxy_->SetVideoSurface(surface);
287 }
288 #endif
289 
IsPlaying()290 bool PlayerClient::IsPlaying()
291 {
292     std::lock_guard<std::mutex> lock(mutex_);
293     CHECK_AND_RETURN_RET_LOG(playerProxy_ != nullptr, false, "player service does not exist..");
294     return playerProxy_->IsPlaying();
295 }
296 
IsLooping()297 bool PlayerClient::IsLooping()
298 {
299     std::lock_guard<std::mutex> lock(mutex_);
300     CHECK_AND_RETURN_RET_LOG(playerProxy_ != nullptr, false, "player service does not exist..");
301     return playerProxy_->IsLooping();
302 }
303 
SetLooping(bool loop)304 int32_t PlayerClient::SetLooping(bool loop)
305 {
306     std::lock_guard<std::mutex> lock(mutex_);
307     CHECK_AND_RETURN_RET_LOG(playerProxy_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
308     return playerProxy_->SetLooping(loop);
309 }
310 
SetParameter(const Format & param)311 int32_t PlayerClient::SetParameter(const Format &param)
312 {
313     std::lock_guard<std::mutex> lock(mutex_);
314     CHECK_AND_RETURN_RET_LOG(playerProxy_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
315     return playerProxy_->SetParameter(param);
316 }
317 
SetPlayerCallback(const std::shared_ptr<PlayerCallback> & callback)318 int32_t PlayerClient::SetPlayerCallback(const std::shared_ptr<PlayerCallback> &callback)
319 {
320     std::lock_guard<std::mutex> lock(mutex_);
321     CHECK_AND_RETURN_RET_LOG(callback != nullptr, MSERR_NO_MEMORY, "input param callback is nullptr..");
322     CHECK_AND_RETURN_RET_LOG(listenerStub_ != nullptr, MSERR_NO_MEMORY, "listenerStub_ is nullptr..");
323 
324     callback_ = callback;
325     listenerStub_->SetPlayerCallback(callback);
326 
327     CHECK_AND_RETURN_RET_LOG(playerProxy_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
328     return playerProxy_->SetPlayerCallback();
329 }
330 
SelectTrack(int32_t index)331 int32_t PlayerClient::SelectTrack(int32_t index)
332 {
333     std::lock_guard<std::mutex> lock(mutex_);
334     CHECK_AND_RETURN_RET_LOG(playerProxy_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
335     return playerProxy_->SelectTrack(index);
336 }
337 
DeselectTrack(int32_t index)338 int32_t PlayerClient::DeselectTrack(int32_t index)
339 {
340     std::lock_guard<std::mutex> lock(mutex_);
341     CHECK_AND_RETURN_RET_LOG(playerProxy_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
342     return playerProxy_->DeselectTrack(index);
343 }
344 
GetCurrentTrack(int32_t trackType,int32_t & index)345 int32_t PlayerClient::GetCurrentTrack(int32_t trackType, int32_t &index)
346 {
347     std::lock_guard<std::mutex> lock(mutex_);
348     CHECK_AND_RETURN_RET_LOG(playerProxy_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
349     return playerProxy_->GetCurrentTrack(trackType, index);
350 }
351 } // namespace Media
352 } // namespace OHOS
353