• 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_impl.h"
17 #include "i_media_service.h"
18 #include "media_log.h"
19 #include "media_errors.h"
20 #include "scoped_timer.h"
21 #include "player_xcollie.h"
22 #ifdef SUPPORT_AVPLAYER_DRM
23 #include "imedia_key_session_service.h"
24 #endif
25 #include "fd_utils.h"
26 #include "osal/utils/steady_clock.h"
27 
28 namespace {
29 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN_PLAYER, "PlayerImpl"};
30 constexpr int32_t API_VERSION_14 = 14;
31 static int32_t apiVersion_ = -1;
32 constexpr int64_t OVERTIME_WARNING_MS = 10;
33 constexpr int64_t CREATE_AVPLAYER_WARNING_MS = 30;
34 constexpr int64_t RESET_WARNING_MS = 30;
35 constexpr int64_t RELEASE_WARNING_MS = 200;
36 const int32_t TIME_OUT_SECOND = 15;
37 }
38 
39 namespace OHOS {
40 namespace Media {
41 
CreatePlayer()42 std::shared_ptr<Player> PlayerFactory::CreatePlayer()
43 {
44     return CreatePlayer(PlayerProducer::INNER);
45 }
46 
CreatePlayer(const PlayerProducer producer)47 std::shared_ptr<Player> PlayerFactory::CreatePlayer(const PlayerProducer producer)
48 {
49     ScopedTimer timer("CreatePlayer", CREATE_AVPLAYER_WARNING_MS);
50     MEDIA_LOGD("PlayerImpl: CreatePlayer in");
51     std::shared_ptr<PlayerImpl> impl = std::make_shared<PlayerImpl>();
52     CHECK_AND_RETURN_RET_LOG(impl != nullptr, nullptr, "failed to new PlayerImpl");
53 
54     int32_t ret = MSERR_OK;
55     LISTENER(ret = impl->Init(producer), "CreatePlayer", false, TIME_OUT_SECOND);
56     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, nullptr, "failed to init PlayerImpl");
57 
58     return impl;
59 }
60 
GetPlayerPids()61 std::vector<pid_t> PlayerFactory::GetPlayerPids()
62 {
63     std::vector<pid_t> pids = MediaServiceFactory::GetInstance().GetPlayerPids();
64     std::string pidLog = "GetPlayerPids size=" + std::to_string(pids.size());
65     if (pids.size() > 0) {
66         pidLog += ", contents:";
67         for (auto pid : pids) {
68             pidLog += std::to_string(pid) + ", ";
69         }
70     }
71     MEDIA_LOGI("%{public}s", pidLog.c_str());
72     return pids;
73 }
74 
Init(const PlayerProducer producer)75 int32_t PlayerImpl::Init(const PlayerProducer producer)
76 {
77     MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " Init in", FAKE_POINTER(this));
78     int64_t startTime = SteadyClock::GetCurrentTimeMs();
79     HiviewDFX::HiTraceChain::SetId(traceId_);
80     playerService_ = MediaServiceFactory::GetInstance().CreatePlayerService();
81     hiAppEventAgent_ = std::make_shared<HiAppEventAgent>();
82     auto ret = playerService_ == nullptr ? MSERR_UNKNOWN : MSERR_OK;
83     if (hiAppEventAgent_) {
84         hiAppEventAgent_->TraceApiEvent(ret, "CreatePlayer", startTime, traceId_);
85     }
86     if (ret == MSERR_OK) {
87         playerService_->SetPlayerProducer(producer);
88     }
89     return ret;
90 }
91 
GetTraceId()92 HiviewDFX::HiTraceId PlayerImpl::GetTraceId()
93 {
94     return traceId_;
95 }
96 
PlayerImpl()97 PlayerImpl::PlayerImpl()
98 {
99     MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " Instances create", FAKE_POINTER(this));
100     ResetSeekVariables();
101     traceId_ = HiTraceChainGetId();
102     if (!traceId_.IsValid()) {
103         MEDIA_LOGI("PlayerImpl:0x%{public}06" PRIXPTR " traceId not valid", FAKE_POINTER(this));
104         traceId_ = HiviewDFX::HiTraceChain::Begin("PlayerImpl", HITRACE_FLAG_DEFAULT);
105     }
106 }
107 
~PlayerImpl()108 PlayerImpl::~PlayerImpl()
109 {
110     if (playerService_ != nullptr) {
111         (void)MediaServiceFactory::GetInstance().DestroyPlayerService(playerService_);
112         playerService_ = nullptr;
113     }
114     hiAppEventAgent_ = nullptr;
115     ResetSeekVariables();
116     if (traceId_.IsValid()) {
117         MEDIA_LOGI("PlayerImpl:0x%{public}06" PRIXPTR " traceId valid", FAKE_POINTER(this));
118         HiviewDFX::HiTraceChain::End(traceId_);
119     }
120     MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " Instances destroy", FAKE_POINTER(this));
121 }
122 
ResetSeekVariables()123 void PlayerImpl::ResetSeekVariables()
124 {
125     mCurrentPosition = INT32_MIN;
126     mCurrentSeekMode = PlayerSeekMode::SEEK_PREVIOUS_SYNC;
127     mSeekPosition = INT32_MIN;
128     mSeekMode = PlayerSeekMode::SEEK_PREVIOUS_SYNC;
129     isSeeking_ = false;
130 }
131 
SetMediaMuted(OHOS::Media::MediaType mediaType,bool isMuted)132 int32_t PlayerImpl::SetMediaMuted(OHOS::Media::MediaType mediaType, bool isMuted)
133 {
134     int64_t startTime = SteadyClock::GetCurrentTimeMs();
135     ScopedTimer timer("SetMediaMuted", OVERTIME_WARNING_MS);
136     CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_INVALID_VAL, "playerService_ not exist");
137     int32_t ret = MSERR_OK;
138     LISTENER(ret = playerService_->SetMediaMuted(mediaType, isMuted), "SetMediaMuted", false, TIME_OUT_SECOND);
139     CHECK_AND_RETURN_RET_NOLOG(ret != MSERR_OK && hiAppEventAgent_ != nullptr, ret);
140     hiAppEventAgent_->TraceApiEvent(ret, "SetMediaMuted", startTime, traceId_);
141     return ret;
142 }
143 
SetSource(const std::shared_ptr<IMediaDataSource> & dataSrc)144 int32_t PlayerImpl::SetSource(const std::shared_ptr<IMediaDataSource> &dataSrc)
145 {
146     int64_t startTime = SteadyClock::GetCurrentTimeMs();
147     ScopedTimer timer("SetSource dataSrc", OVERTIME_WARNING_MS);
148     MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " SetSource in(dataSrc)", FAKE_POINTER(this));
149     CHECK_AND_RETURN_RET_LOG(dataSrc != nullptr, MSERR_INVALID_VAL, "failed to create data source");
150     int32_t ret = MSERR_OK;
151     LISTENER(ret = playerService_->SetSource(dataSrc), "SetSource dataSrc", false, TIME_OUT_SECOND);
152     CHECK_AND_RETURN_RET_NOLOG(ret != MSERR_OK && hiAppEventAgent_ != nullptr, ret);
153     hiAppEventAgent_->TraceApiEvent(ret, "SetSource dataSrc", startTime, traceId_);
154     return ret;
155 }
156 
SetSource(const std::string & url)157 int32_t PlayerImpl::SetSource(const std::string &url)
158 {
159     int64_t startTime = SteadyClock::GetCurrentTimeMs();
160     ScopedTimer timer("SetSource url", OVERTIME_WARNING_MS);
161     CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
162     CHECK_AND_RETURN_RET_LOG(!url.empty(), MSERR_INVALID_VAL, "url is empty..");
163     int32_t ret = MSERR_OK;
164     LISTENER(ret = playerService_->SetSource(url), "SetSource url", false, TIME_OUT_SECOND);
165     CHECK_AND_RETURN_RET_NOLOG(ret != MSERR_OK && hiAppEventAgent_ != nullptr, ret);
166     hiAppEventAgent_->TraceApiEvent(ret, "SetSource in(url)", startTime, traceId_);
167     return ret;
168 }
169 
SetSource(int32_t fd,int64_t offset,int64_t size)170 int32_t PlayerImpl::SetSource(int32_t fd, int64_t offset, int64_t size)
171 {
172     int64_t startTime = SteadyClock::GetCurrentTimeMs();
173     ScopedTimer timer("SetSource fd", OVERTIME_WARNING_MS);
174     MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " SetSource in(fd)", FAKE_POINTER(this));
175     CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
176     int32_t ret = MSERR_OK;
177     LISTENER(ret = SetSourceTask(fd, offset, size), "SetSource fd", false, TIME_OUT_SECOND);
178     CHECK_AND_RETURN_RET_NOLOG(ret != MSERR_OK && hiAppEventAgent_ != nullptr, ret);
179     hiAppEventAgent_->TraceApiEvent(ret, "SetSource in(fd)", startTime, traceId_);
180     return ret;
181 }
182 
SetSourceTask(int32_t fd,int64_t offset,int64_t size)183 int32_t PlayerImpl::SetSourceTask(int32_t fd, int64_t offset, int64_t size)
184 {
185     int32_t ret = MSERR_OK;
186     FdsanFd reopenFd = FdUtils::ReOpenFd(fd);
187     if (reopenFd.Get() >= 0) {
188         MEDIA_LOGI("SetSourceTask: reopen success");
189         ret = playerService_->SetSource(reopenFd.Get(), offset, size);
190     } else {
191         ret = playerService_->SetSource(fd, offset, size);
192     }
193     CHECK_AND_RETURN_RET_NOLOG(ret == MSERR_OK, ret);
194     int32_t dupFd = dup(fd);
195     MEDIA_LOGI("PlayerImpl:0x%{public}06" PRIXPTR " SetSourceTask dupFd", FAKE_POINTER(this));
196     CHECK_AND_RETURN_RET_LOG(dupFd >= 0, ret, "Dup failed with err.");
197     if (!fdsanFd_) {
198         fdsanFd_ = std::make_unique<FdsanFd>(dupFd);
199     } else {
200         fdsanFd_->Reset(dupFd);
201     }
202     return ret;
203 }
204 
AddSubSource(const std::string & url)205 int32_t PlayerImpl::AddSubSource(const std::string &url)
206 {
207     int64_t startTime = SteadyClock::GetCurrentTimeMs();
208     ScopedTimer timer("AddSubSource url", OVERTIME_WARNING_MS);
209     MEDIA_LOGD("PlayerImpl:0x%{private}06" PRIXPTR " AddSubSource in(url): %{private}s",
210         FAKE_POINTER(this), url.c_str());
211     CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
212     CHECK_AND_RETURN_RET_LOG(!url.empty(), MSERR_INVALID_VAL, "url is empty..");
213     int32_t ret = MSERR_OK;
214     LISTENER(ret = playerService_->AddSubSource(url), "AddSubSource url", false, TIME_OUT_SECOND);
215     CHECK_AND_RETURN_RET_NOLOG(ret != MSERR_OK && hiAppEventAgent_ != nullptr, ret);
216     hiAppEventAgent_->TraceApiEvent(ret, "AddSubSource url", startTime, traceId_);
217     return ret;
218 }
219 
AddSubSource(int32_t fd,int64_t offset,int64_t size)220 int32_t PlayerImpl::AddSubSource(int32_t fd, int64_t offset, int64_t size)
221 {
222     int64_t startTime = SteadyClock::GetCurrentTimeMs();
223     ScopedTimer timer("AddSubSource fd", OVERTIME_WARNING_MS);
224     MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " AddSubSource in(fd)", FAKE_POINTER(this));
225     CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
226     int32_t ret = MSERR_OK;
227     LISTENER(ret = playerService_->AddSubSource(fd, offset, size), "AddSubSource fd", false, TIME_OUT_SECOND);
228     CHECK_AND_RETURN_RET_NOLOG(ret != MSERR_OK && hiAppEventAgent_ != nullptr, ret);
229     hiAppEventAgent_->TraceApiEvent(ret, "AddSubSource fd", startTime, traceId_);
230     return ret;
231 }
232 
Play()233 int32_t PlayerImpl::Play()
234 {
235     int64_t startTime = SteadyClock::GetCurrentTimeMs();
236     ScopedTimer timer("Play", OVERTIME_WARNING_MS);
237     MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " Play in", FAKE_POINTER(this));
238     CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
239     int32_t ret = MSERR_OK;
240     LISTENER(ret = playerService_->Play(), "Play", false, TIME_OUT_SECOND);
241     CHECK_AND_RETURN_RET_NOLOG(ret != MSERR_OK && hiAppEventAgent_ != nullptr, ret);
242     hiAppEventAgent_->TraceApiEvent(ret, "Play", startTime, traceId_);
243     return ret;
244 }
245 
SetPlayRange(int64_t start,int64_t end)246 int32_t PlayerImpl::SetPlayRange(int64_t start, int64_t end)
247 {
248     int64_t startTime = SteadyClock::GetCurrentTimeMs();
249     ScopedTimer timer("SetPlayRange", OVERTIME_WARNING_MS);
250     MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " SetPlayRange in", FAKE_POINTER(this));
251     CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
252     int32_t ret = MSERR_OK;
253     LISTENER(ret = playerService_->SetPlayRange(start, end), "SetPlayRange", false, TIME_OUT_SECOND);
254     CHECK_AND_RETURN_RET_NOLOG(ret != MSERR_OK && hiAppEventAgent_ != nullptr, ret);
255     hiAppEventAgent_->TraceApiEvent(ret, "SetPlayRange", startTime, traceId_);
256     return ret;
257 }
258 
SetPlayRangeWithMode(int64_t start,int64_t end,PlayerSeekMode mode)259 int32_t PlayerImpl::SetPlayRangeWithMode(int64_t start, int64_t end, PlayerSeekMode mode)
260 {
261     int64_t startTime = SteadyClock::GetCurrentTimeMs();
262     ScopedTimer timer("SetPlayRangeWithMode", OVERTIME_WARNING_MS);
263     MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " SetPlayRangeWithMode in", FAKE_POINTER(this));
264     CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
265     int32_t ret = MSERR_OK;
266     LISTENER(
267         ret = playerService_->SetPlayRangeWithMode(start, end, mode), "SetPlayRangeWithMode", false, TIME_OUT_SECOND);
268     CHECK_AND_RETURN_RET_NOLOG(ret != MSERR_OK && hiAppEventAgent_ != nullptr, ret);
269     hiAppEventAgent_->TraceApiEvent(ret, "SetPlayRangeWithMode", startTime, traceId_);
270     return ret;
271 }
272 
SetPlayRangeUsWithMode(int64_t start,int64_t end,PlayerSeekMode mode)273 int32_t PlayerImpl::SetPlayRangeUsWithMode(int64_t start, int64_t end, PlayerSeekMode mode)
274 {
275     const int64_t msToUs = 1000;
276     const int64_t complementNum = 999;
277     return SetPlayRangeWithMode(start / msToUs, (end + complementNum) / msToUs, mode);
278 }
279 
Prepare()280 int32_t PlayerImpl::Prepare()
281 {
282     int64_t startTime = SteadyClock::GetCurrentTimeMs();
283     prepareStartTimeMs_ = startTime;
284     ScopedTimer timer("Prepare", OVERTIME_WARNING_MS);
285     MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " Prepare in", FAKE_POINTER(this));
286     CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
287     int32_t ret = MSERR_OK;
288     LISTENER(ret = playerService_->Prepare(), "Prepare", false, TIME_OUT_SECOND);
289     CHECK_AND_RETURN_RET_NOLOG(ret != MSERR_OK && hiAppEventAgent_ != nullptr, ret);
290     hiAppEventAgent_->TraceApiEvent(ret, "Prepare", startTime, traceId_);
291     return ret;
292 }
293 
SetRenderFirstFrame(bool display)294 int32_t PlayerImpl::SetRenderFirstFrame(bool display)
295 {
296     int64_t startTime = SteadyClock::GetCurrentTimeMs();
297     ScopedTimer timer("SetRenderFirstFrame", OVERTIME_WARNING_MS);
298     MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " SetRenderFirstFrame in, display %{public}d",
299          FAKE_POINTER(this), display);
300     CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
301     int32_t ret = MSERR_OK;
302     LISTENER(ret = playerService_->SetRenderFirstFrame(display), "SetRenderFirstFrame", false, TIME_OUT_SECOND);
303     CHECK_AND_RETURN_RET_NOLOG(ret != MSERR_OK && hiAppEventAgent_ != nullptr, ret);
304     hiAppEventAgent_->TraceApiEvent(ret, "SetRenderFirstFrame", startTime, traceId_);
305     return ret;
306 }
307 
PrepareAsync()308 int32_t PlayerImpl::PrepareAsync()
309 {
310     int64_t startTime = SteadyClock::GetCurrentTimeMs();
311     prepareStartTimeMs_ = startTime;
312     ScopedTimer timer("PrepareAsync", OVERTIME_WARNING_MS);
313     MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " PrepareAsync in", FAKE_POINTER(this));
314     CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
315     int32_t ret = MSERR_OK;
316     LISTENER(ret = playerService_->PrepareAsync(), "PrepareAsync", false, TIME_OUT_SECOND);
317     CHECK_AND_RETURN_RET_NOLOG(ret != MSERR_OK && hiAppEventAgent_ != nullptr, ret);
318     hiAppEventAgent_->TraceApiEvent(ret, "PrepareAsync", startTime, traceId_);
319     return ret;
320 }
321 
Pause()322 int32_t PlayerImpl::Pause()
323 {
324     int64_t startTime = SteadyClock::GetCurrentTimeMs();
325     ScopedTimer timer("Pause", OVERTIME_WARNING_MS);
326     MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " Pause in", FAKE_POINTER(this));
327     CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
328     int32_t ret = MSERR_OK;
329     LISTENER(ret = playerService_->Pause(), "Pause", false, TIME_OUT_SECOND);
330     CHECK_AND_RETURN_RET_NOLOG(ret != MSERR_OK && hiAppEventAgent_ != nullptr, ret);
331     hiAppEventAgent_->TraceApiEvent(ret, "Pause", startTime, traceId_);
332     return ret;
333 }
334 
Stop()335 int32_t PlayerImpl::Stop()
336 {
337     int64_t startTime = SteadyClock::GetCurrentTimeMs();
338     ScopedTimer timer("Stop", OVERTIME_WARNING_MS);
339     MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " Stop in", FAKE_POINTER(this));
340     CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
341     ResetSeekVariables();
342     int32_t ret = MSERR_OK;
343     LISTENER(ret = playerService_->Stop(), "Stop", false, TIME_OUT_SECOND);
344     CHECK_AND_RETURN_RET_NOLOG(ret != MSERR_OK && hiAppEventAgent_ != nullptr, ret);
345     hiAppEventAgent_->TraceApiEvent(ret, "Stop", startTime, traceId_);
346     return ret;
347 }
348 
Reset()349 int32_t PlayerImpl::Reset()
350 {
351     int64_t startTime = SteadyClock::GetCurrentTimeMs();
352     ScopedTimer timer("Reset", RESET_WARNING_MS);
353     MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " Reset in", FAKE_POINTER(this));
354     CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
355     ResetSeekVariables();
356     int32_t ret = MSERR_OK;
357     LISTENER(ret = playerService_->Reset(), "Reset", false, TIME_OUT_SECOND);
358     CHECK_AND_RETURN_RET_NOLOG(ret != MSERR_OK && hiAppEventAgent_ != nullptr, ret);
359     hiAppEventAgent_->TraceApiEvent(ret, "Reset", startTime, traceId_);
360     return ret;
361 }
362 
Release()363 int32_t PlayerImpl::Release()
364 {
365     ScopedTimer timer("Release", RELEASE_WARNING_MS);
366     MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " Release in", FAKE_POINTER(this));
367     CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
368     LISTENER((void)playerService_->Release(), "Release", false, TIME_OUT_SECOND);
369     (void)MediaServiceFactory::GetInstance().DestroyPlayerService(playerService_);
370     playerService_ = nullptr;
371     return MSERR_OK;
372 }
373 
ReleaseSync()374 int32_t PlayerImpl::ReleaseSync()
375 {
376     ScopedTimer timer("ReleaseSync", RELEASE_WARNING_MS);
377     MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " ReleaseSync in", FAKE_POINTER(this));
378     CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
379     LISTENER((void)playerService_->ReleaseSync(), "ReleaseSync", false, TIME_OUT_SECOND);
380     (void)MediaServiceFactory::GetInstance().DestroyPlayerService(playerService_);
381     playerService_ = nullptr;
382     return MSERR_OK;
383 }
384 
SetVolumeMode(int32_t mode)385 int32_t PlayerImpl::SetVolumeMode(int32_t mode)
386 {
387     int64_t startTime = SteadyClock::GetCurrentTimeMs();
388     ScopedTimer timer("SetVolumeMode", OVERTIME_WARNING_MS);
389     MEDIA_LOGD("PlayerImpl::SetVolumeMode mode = %{public}d", mode);
390     CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
391     int32_t ret = MSERR_OK;
392     LISTENER(ret = playerService_->SetVolumeMode(mode), "SetVolumeMode", false, TIME_OUT_SECOND);
393     CHECK_AND_RETURN_RET_NOLOG(ret != MSERR_OK && hiAppEventAgent_ != nullptr, ret);
394     hiAppEventAgent_->TraceApiEvent(ret, "SetVolumeMode", startTime, traceId_);
395     return ret;
396 }
397 
SetVolume(float leftVolume,float rightVolume)398 int32_t PlayerImpl::SetVolume(float leftVolume, float rightVolume)
399 {
400     int64_t startTime = SteadyClock::GetCurrentTimeMs();
401     ScopedTimer timer("SetVolume", OVERTIME_WARNING_MS);
402     MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " SetVolume(%{public}f, %{public}f) in",
403         FAKE_POINTER(this), leftVolume, rightVolume);
404     CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
405     int32_t ret = MSERR_OK;
406     LISTENER(ret = playerService_->SetVolume(leftVolume, rightVolume), "SetVolume", false, TIME_OUT_SECOND);
407     CHECK_AND_RETURN_RET_NOLOG(ret != MSERR_OK && hiAppEventAgent_ != nullptr, ret);
408     hiAppEventAgent_->TraceApiEvent(ret, "SetVolume", startTime, traceId_);
409     return ret;
410 }
411 
Seek(int32_t mSeconds,PlayerSeekMode mode)412 int32_t PlayerImpl::Seek(int32_t mSeconds, PlayerSeekMode mode)
413 {
414     int64_t startTime = SteadyClock::GetCurrentTimeMs();
415     ScopedTimer timer("Seek", OVERTIME_WARNING_MS);
416     MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " Seek in, seek to %{public}d ms, mode is %{public}d",
417         FAKE_POINTER(this), mSeconds, mode);
418     CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
419 
420     std::unique_lock<std::recursive_mutex> lock(recMutex_);
421     // SEEK_CONTINOUS is usually called in batches, and will not report seek done event.
422     if (mode == PlayerSeekMode::SEEK_CONTINOUS) {
423         int32_t ret = MSERR_OK;
424         ret = playerService_->Seek(mSeconds, mode);
425         CHECK_AND_RETURN_RET_NOLOG(ret != MSERR_OK && hiAppEventAgent_ != nullptr, ret);
426         hiAppEventAgent_->TraceApiEvent(ret, "SEEK_CONTINOUS", startTime, traceId_);
427         return ret;
428     }
429     mCurrentPosition = mSeconds;
430     mCurrentSeekMode = mode;
431     if ((mSeekPosition != mCurrentPosition || mSeekMode != mCurrentSeekMode) && !isSeeking_) {
432         MEDIA_LOGI("Start seek once.");
433         isSeeking_ = true;
434         mSeekPosition = mSeconds;
435         mSeekMode = mode;
436         int32_t retCode = MSERR_OK;
437         LISTENER(retCode = playerService_->Seek(mSeconds, mode), "SetVolume", false, TIME_OUT_SECOND);
438         if (retCode != MSERR_OK) {
439             ResetSeekVariables();
440         }
441         MEDIA_LOGI("Start seek once end");
442         CHECK_AND_RETURN_RET_NOLOG(retCode != MSERR_OK && hiAppEventAgent_ != nullptr, retCode);
443         hiAppEventAgent_->TraceApiEvent(retCode, "seek", startTime, traceId_);
444         return retCode;
445     } else {
446         MEDIA_LOGE("Seeking not completed, need wait the lastest seek end, then seek again.");
447     }
448     MEDIA_LOGI("Seeking task end. %{public}d ms, mode is %{public}d", mSeconds, mode);
449     return MSERR_OK;
450 }
451 
HandleSeekDoneInfo(PlayerOnInfoType type,int32_t extra)452 void PlayerImpl::HandleSeekDoneInfo(PlayerOnInfoType type, int32_t extra)
453 {
454     if (type == INFO_TYPE_SEEKDONE) {
455         MEDIA_LOGI("HandleSeekDoneInfo entered");
456         CHECK_AND_RETURN_LOG(playerService_ != nullptr, "player service does not exist..");
457         if (extra == -1) {
458             MEDIA_LOGI("seek error, need reset seek variables");
459             ResetSeekVariables();
460             return;
461         }
462         std::unique_lock<std::recursive_mutex> lock(recMutex_);
463         if (mSeekPosition != mCurrentPosition || mSeekMode != mCurrentSeekMode) {
464             MEDIA_LOGI("Start seek again (%{public}d, %{public}d)", mCurrentPosition, mCurrentSeekMode);
465             mSeekPosition = mCurrentPosition;
466             mSeekMode = mCurrentSeekMode;
467             playerService_->Seek(mCurrentPosition, mCurrentSeekMode);
468         } else {
469             MEDIA_LOGI("All seeks complete - return to regularly scheduled program");
470             ResetSeekVariables();
471         }
472         MEDIA_LOGI("HandleSeekDoneInfo end seekTo(%{public}d, %{public}d)", mCurrentPosition, mCurrentSeekMode);
473     }
474 }
475 
OnInfo(PlayerOnInfoType type,int32_t extra,const Format & infoBody)476 void PlayerImpl::OnInfo(PlayerOnInfoType type, int32_t extra, const Format &infoBody)
477 {
478     HandleSeekDoneInfo(type, extra);
479     std::shared_ptr<PlayerCallback> callback;
480     {
481         std::unique_lock<std::mutex> lock(cbMutex_);
482         callback = callback_;
483     }
484 
485     CHECK_AND_RETURN_LOG(callback != nullptr, "callback is nullptr.");
486     if (type == INFO_TYPE_STATE_CHANGE && hiAppEventAgent_ != nullptr) {
487         PlayerStates state = static_cast<PlayerStates>(extra);
488         if (state == PLAYER_PREPARED) {
489             hiAppEventAgent_->TraceApiEvent(MSERR_OK, "prepared", prepareStartTimeMs_, traceId_);
490         }
491     }
492     if (type == INFO_TYPE_SEEKDONE) {
493         if (extra == -1) {
494             MEDIA_LOGI("seek done error callback, no need report");
495             return;
496         }
497         if (!isSeeking_) {
498             callback->OnInfo(type, extra, infoBody);
499         } else {
500             MEDIA_LOGD("Is seeking to (%{public}d, %{public}d), not update now", mCurrentPosition, mCurrentSeekMode);
501         }
502     } else {
503         callback->OnInfo(type, extra, infoBody);
504     }
505 }
506 
GetCurrentTime(int32_t & currentTime)507 int32_t PlayerImpl::GetCurrentTime(int32_t &currentTime)
508 {
509     int64_t startTime = SteadyClock::GetCurrentTimeMs();
510     ScopedTimer timer("GetCurrentTime", OVERTIME_WARNING_MS);
511     MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " GetCurrentTime in", FAKE_POINTER(this));
512     CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
513     int32_t ret = MSERR_OK;
514     LISTENER(ret = playerService_->GetCurrentTime(currentTime), "GetCurrentTime", false, TIME_OUT_SECOND);
515     CHECK_AND_RETURN_RET_NOLOG(ret != MSERR_OK && hiAppEventAgent_ != nullptr, ret);
516     hiAppEventAgent_->TraceApiEvent(ret, "GetCurrentTime", startTime, traceId_);
517     return ret;
518 }
519 
GetPlaybackPosition(int32_t & playbackPosition)520 int32_t PlayerImpl::GetPlaybackPosition(int32_t &playbackPosition)
521 {
522     int64_t startTime = SteadyClock::GetCurrentTimeMs();
523     ScopedTimer timer("GetPlaybackPosition", OVERTIME_WARNING_MS);
524     MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " GetPlaybackPosition in", FAKE_POINTER(this));
525     CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
526     int32_t ret = MSERR_OK;
527     LISTENER(
528         ret = playerService_->GetPlaybackPosition(playbackPosition), "GetPlaybackPosition", false, TIME_OUT_SECOND);
529     CHECK_AND_RETURN_RET_NOLOG(ret != MSERR_OK && hiAppEventAgent_ != nullptr, ret);
530     hiAppEventAgent_->TraceApiEvent(ret, "GetPlaybackPosition", startTime, traceId_);
531     return ret;
532 }
533 
GetVideoTrackInfo(std::vector<Format> & videoTrack)534 int32_t PlayerImpl::GetVideoTrackInfo(std::vector<Format> &videoTrack)
535 {
536     int64_t startTime = SteadyClock::GetCurrentTimeMs();
537     ScopedTimer timer("GetVideoTrackInfo", OVERTIME_WARNING_MS);
538     MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " GetVideoTrackInfo in", FAKE_POINTER(this));
539     CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
540     int32_t ret = MSERR_OK;
541     LISTENER(ret = playerService_->GetVideoTrackInfo(videoTrack), "GetVideoTrackInfo", false, TIME_OUT_SECOND);
542     CHECK_AND_RETURN_RET_NOLOG(ret != MSERR_OK && hiAppEventAgent_ != nullptr, ret);
543     hiAppEventAgent_->TraceApiEvent(ret, "GetVideoTrackInfo", startTime, traceId_);
544     return ret;
545 }
546 
GetPlaybackInfo(Format & playbackInfo)547 int32_t PlayerImpl::GetPlaybackInfo(Format &playbackInfo)
548 {
549     int64_t startTime = SteadyClock::GetCurrentTimeMs();
550     ScopedTimer timer("GetPlaybackInfo", OVERTIME_WARNING_MS);
551     MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " GetPlaybackInfo in", FAKE_POINTER(this));
552     CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
553     int32_t ret = MSERR_OK;
554     LISTENER(ret = playerService_->GetPlaybackInfo(playbackInfo), "GetPlaybackInfo", false, TIME_OUT_SECOND);
555     CHECK_AND_RETURN_RET_NOLOG(ret != MSERR_OK && hiAppEventAgent_ != nullptr, ret);
556     hiAppEventAgent_->TraceApiEvent(ret, "GetPlaybackInfo", startTime, traceId_);
557     return ret;
558 }
559 
GetAudioTrackInfo(std::vector<Format> & audioTrack)560 int32_t PlayerImpl::GetAudioTrackInfo(std::vector<Format> &audioTrack)
561 {
562     int64_t startTime = SteadyClock::GetCurrentTimeMs();
563     ScopedTimer timer("GetAudioTrackInfo", OVERTIME_WARNING_MS);
564     MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " GetAudioTrackInfo in", FAKE_POINTER(this));
565     CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
566     int32_t ret = MSERR_OK;
567     LISTENER(ret = playerService_->GetAudioTrackInfo(audioTrack), "GetAudioTrackInfo", false, TIME_OUT_SECOND);
568     CHECK_AND_RETURN_RET_NOLOG(ret != MSERR_OK && hiAppEventAgent_ != nullptr, ret);
569     hiAppEventAgent_->TraceApiEvent(ret, "GetAudioTrackInfo", startTime, traceId_);
570     return ret;
571 }
572 
GetSubtitleTrackInfo(std::vector<Format> & subtitleTrack)573 int32_t PlayerImpl::GetSubtitleTrackInfo(std::vector<Format> &subtitleTrack)
574 {
575     int64_t startTime = SteadyClock::GetCurrentTimeMs();
576     ScopedTimer timer("GetSubtitleTrackInfo", OVERTIME_WARNING_MS);
577     MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " GetSubtitleTrackInfo in", FAKE_POINTER(this));
578     CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
579     int32_t ret = MSERR_OK;
580     LISTENER(
581         ret = playerService_->GetSubtitleTrackInfo(subtitleTrack), "GetSubtitleTrackInfo", false, TIME_OUT_SECOND);
582     CHECK_AND_RETURN_RET_NOLOG(ret != MSERR_OK && hiAppEventAgent_ != nullptr, ret);
583     hiAppEventAgent_->TraceApiEvent(ret, "GetSubtitleTrackInfo", startTime, traceId_);
584     return ret;
585 }
586 
GetVideoWidth()587 int32_t PlayerImpl::GetVideoWidth()
588 {
589     ScopedTimer timer("GetVideoWidth", OVERTIME_WARNING_MS);
590     MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " GetVideoWidth in", FAKE_POINTER(this));
591     CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
592     LISTENER(return playerService_->GetVideoWidth(), "GetVideoWidth", false, TIME_OUT_SECOND);
593 }
594 
GetVideoHeight()595 int32_t PlayerImpl::GetVideoHeight()
596 {
597     ScopedTimer timer("GetVideoHeight", OVERTIME_WARNING_MS);
598     MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " GetVideoHeight in", FAKE_POINTER(this));
599     CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
600     LISTENER(return playerService_->GetVideoHeight(), "GetVideoHeight", false, TIME_OUT_SECOND);
601 }
602 
SetPlaybackSpeed(PlaybackRateMode mode)603 int32_t PlayerImpl::SetPlaybackSpeed(PlaybackRateMode mode)
604 {
605     int64_t startTime = SteadyClock::GetCurrentTimeMs();
606     ScopedTimer timer("SetPlaybackSpeed", OVERTIME_WARNING_MS);
607     MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " SetPlaybackSpeed in, mode is %{public}d", FAKE_POINTER(this), mode);
608     CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
609     int32_t ret = MSERR_OK;
610     LISTENER(ret = playerService_->SetPlaybackSpeed(mode), "SetPlaybackSpeed", false, TIME_OUT_SECOND);
611     CHECK_AND_RETURN_RET_NOLOG(ret != MSERR_OK && hiAppEventAgent_ != nullptr, ret);
612     hiAppEventAgent_->TraceApiEvent(ret, "SetPlaybackSpeed", startTime, traceId_);
613     return ret;
614 }
615 
SetPlaybackRate(float rate)616 int32_t PlayerImpl::SetPlaybackRate(float rate)
617 {
618     ScopedTimer timer("SetPlaybackRate", OVERTIME_WARNING_MS);
619     MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " SetPlaybackRate in, mode is %{public}f", FAKE_POINTER(this), rate);
620     CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
621     const double minRate = 0.125f;
622     const double maxRate = 4.0f;
623     const double eps = 1e-15;
624     if ((rate < minRate - eps) || (rate > maxRate + eps)) {
625         return MSERR_INVALID_VAL;
626     }
627     LISTENER(return playerService_->SetPlaybackRate(rate), "SetPlaybackRate", false, TIME_OUT_SECOND);
628 }
629 
630 
SetMediaSource(const std::shared_ptr<AVMediaSource> & mediaSource,AVPlayStrategy strategy)631 int32_t PlayerImpl::SetMediaSource(const std::shared_ptr<AVMediaSource> &mediaSource, AVPlayStrategy strategy)
632 {
633     int64_t startTime = SteadyClock::GetCurrentTimeMs();
634     ScopedTimer timer("SetMediaSource", OVERTIME_WARNING_MS);
635     MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " SetMediaSource in(dataSrc)", FAKE_POINTER(this));
636     CHECK_AND_RETURN_RET_LOG(mediaSource != nullptr, MSERR_INVALID_VAL, "mediaSource is nullptr!");
637     CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
638     int32_t ret = MSERR_OK;
639     LISTENER(ret = playerService_->SetMediaSource(mediaSource, strategy), "SetMediaSource", false, TIME_OUT_SECOND);
640     CHECK_AND_RETURN_RET_NOLOG(ret != MSERR_OK && hiAppEventAgent_ != nullptr, ret);
641     hiAppEventAgent_->TraceApiEvent(ret, "SetMediaSource", startTime, traceId_);
642     return ret;
643 }
644 
GetPlaybackSpeed(PlaybackRateMode & mode)645 int32_t PlayerImpl::GetPlaybackSpeed(PlaybackRateMode &mode)
646 {
647     int64_t startTime = SteadyClock::GetCurrentTimeMs();
648     ScopedTimer timer("GetPlaybackSpeed", OVERTIME_WARNING_MS);
649     MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " GetPlaybackSpeed in", FAKE_POINTER(this));
650     CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
651     int32_t ret = MSERR_OK;
652     LISTENER(ret = playerService_->GetPlaybackSpeed(mode), "GetPlaybackSpeed", false, TIME_OUT_SECOND);
653     CHECK_AND_RETURN_RET_NOLOG(ret != MSERR_OK && hiAppEventAgent_ != nullptr, ret);
654     hiAppEventAgent_->TraceApiEvent(ret, "GetPlaybackSpeed", startTime, traceId_);
655     return ret;
656 }
657 
SelectBitRate(uint32_t bitRate)658 int32_t PlayerImpl::SelectBitRate(uint32_t bitRate)
659 {
660     int64_t startTime = SteadyClock::GetCurrentTimeMs();
661     ScopedTimer timer("SelectBitRate", OVERTIME_WARNING_MS);
662     MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " SelectBitRate(%{public}d) in", FAKE_POINTER(this), bitRate);
663     CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
664     int32_t ret = MSERR_OK;
665     LISTENER(ret = playerService_->SelectBitRate(bitRate), "SelectBitRate", false, TIME_OUT_SECOND);
666     CHECK_AND_RETURN_RET_NOLOG(ret != MSERR_OK && hiAppEventAgent_ != nullptr, ret);
667     hiAppEventAgent_->TraceApiEvent(ret, "SelectBitRate", startTime, traceId_);
668     return ret;
669 }
670 
GetDuration(int32_t & duration)671 int32_t PlayerImpl::GetDuration(int32_t &duration)
672 {
673     int64_t startTime = SteadyClock::GetCurrentTimeMs();
674     ScopedTimer timer("GetDuration", OVERTIME_WARNING_MS);
675     MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " GetDuration in", FAKE_POINTER(this));
676     CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
677     int32_t ret = MSERR_OK;
678     LISTENER(ret = playerService_->GetDuration(duration), "GetDuration", false, TIME_OUT_SECOND);
679     CHECK_AND_RETURN_RET_NOLOG(ret != MSERR_OK && hiAppEventAgent_ != nullptr, ret);
680     hiAppEventAgent_->TraceApiEvent(ret, "GetDuration", startTime, traceId_);
681     return ret;
682 }
683 
GetApiVersion(int32_t & apiVersion)684 int32_t PlayerImpl::GetApiVersion(int32_t &apiVersion)
685 {
686     int64_t startTime = SteadyClock::GetCurrentTimeMs();
687     ScopedTimer timer("GetApiVersion", OVERTIME_WARNING_MS);
688     MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " GetApiVersion in", FAKE_POINTER(this));
689     CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
690     int32_t ret = MSERR_OK;
691     LISTENER(ret = playerService_->GetApiVersion(apiVersion), "GetApiVersion", false, TIME_OUT_SECOND);
692     CHECK_AND_RETURN_RET_NOLOG(ret != MSERR_OK && hiAppEventAgent_ != nullptr, ret);
693     hiAppEventAgent_->TraceApiEvent(ret, "GetApiVersion", startTime, traceId_);
694     return ret;
695 }
696 
697 #ifdef SUPPORT_VIDEO
SetVideoSurface(sptr<Surface> surface)698 int32_t PlayerImpl::SetVideoSurface(sptr<Surface> surface)
699 {
700     int64_t startTime = SteadyClock::GetCurrentTimeMs();
701     ScopedTimer timer("SetVideoSurface", OVERTIME_WARNING_MS);
702     MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " SetVideoSurface in", FAKE_POINTER(this));
703     CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
704     CHECK_AND_RETURN_RET_LOG(surface != nullptr, MSERR_INVALID_VAL, "surface is nullptr");
705     surface_ = surface;
706     int32_t ret = MSERR_OK;
707     LISTENER(ret = playerService_->SetVideoSurface(surface), "SetVideoSurface", false, TIME_OUT_SECOND);
708     CHECK_AND_RETURN_RET_NOLOG(ret != MSERR_OK && hiAppEventAgent_ != nullptr, ret);
709     hiAppEventAgent_->TraceApiEvent(ret, "SetVideoSurface", startTime, traceId_);
710     return ret;
711 }
712 #endif
713 
IsPlaying()714 bool PlayerImpl::IsPlaying()
715 {
716     MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " IsPlaying in", FAKE_POINTER(this));
717     CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, false, "player service does not exist..");
718 
719     return playerService_->IsPlaying();
720 }
721 
IsLooping()722 bool PlayerImpl::IsLooping()
723 {
724     MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " IsLooping in", FAKE_POINTER(this));
725     CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, false, "player service does not exist..");
726 
727     return playerService_->IsLooping();
728 }
729 
SetLooping(bool loop)730 int32_t PlayerImpl::SetLooping(bool loop)
731 {
732     int64_t startTime = SteadyClock::GetCurrentTimeMs();
733     ScopedTimer timer("SetLooping", OVERTIME_WARNING_MS);
734     MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " SetLooping in, loop %{public}d", FAKE_POINTER(this), loop);
735     CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
736     int32_t ret = MSERR_OK;
737     LISTENER(ret = playerService_->SetLooping(loop), "SetLooping", false, TIME_OUT_SECOND);
738     CHECK_AND_RETURN_RET_NOLOG(ret != MSERR_OK && hiAppEventAgent_ != nullptr, ret);
739     hiAppEventAgent_->TraceApiEvent(ret, "SetLooping", startTime, traceId_);
740     return ret;
741 }
742 
SetPlayerCallback(const std::shared_ptr<PlayerCallback> & callback)743 int32_t PlayerImpl::SetPlayerCallback(const std::shared_ptr<PlayerCallback> &callback)
744 {
745     ScopedTimer timer("SetPlayerCallback", OVERTIME_WARNING_MS);
746     MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " SetPlayerCallback in", FAKE_POINTER(this));
747     CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
748     CHECK_AND_RETURN_RET_LOG(callback != nullptr, MSERR_INVALID_VAL, "callback is nullptr");
749     {
750         std::unique_lock<std::mutex> lock(cbMutex_);
751         callback_ = callback;
752     }
753 
754     std::shared_ptr<PlayerCallback> playerCb = std::make_shared<PlayerImplCallback>(callback, shared_from_this());
755     LISTENER(return playerService_->SetPlayerCallback(playerCb), "SetPlayerCallback", false, TIME_OUT_SECOND);
756 }
757 
SetParameter(const Format & param)758 int32_t PlayerImpl::SetParameter(const Format &param)
759 {
760     int64_t startTime = SteadyClock::GetCurrentTimeMs();
761     ScopedTimer timer("SetParameter", OVERTIME_WARNING_MS);
762     MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " SetParameter in", FAKE_POINTER(this));
763     CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
764     int32_t ret = MSERR_OK;
765     LISTENER(ret = playerService_->SetParameter(param), "SetParameter", false, TIME_OUT_SECOND);
766     CHECK_AND_RETURN_RET_NOLOG(ret != MSERR_OK && hiAppEventAgent_ != nullptr, ret);
767     hiAppEventAgent_->TraceApiEvent(ret, "SetParameter", startTime, traceId_);
768     return ret;
769 }
770 
SelectTrack(int32_t index,PlayerSwitchMode mode)771 int32_t PlayerImpl::SelectTrack(int32_t index, PlayerSwitchMode mode)
772 {
773     int64_t startTime = SteadyClock::GetCurrentTimeMs();
774     ScopedTimer timer("SelectTrack", OVERTIME_WARNING_MS);
775     MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " SelectTrack in", FAKE_POINTER(this));
776     CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
777     if (index == prevTrackIndex_) {
778         MEDIA_LOGI("Select the same track, index: %{public}d", index);
779         return 0;
780     }
781     prevTrackIndex_ = index;
782     int32_t ret = MSERR_OK;
783     LISTENER(ret = playerService_->SelectTrack(index, mode), "SelectTrack", false, TIME_OUT_SECOND);
784     CHECK_AND_RETURN_RET_NOLOG(ret != MSERR_OK && hiAppEventAgent_ != nullptr, ret);
785     hiAppEventAgent_->TraceApiEvent(ret, "SelectTrack", startTime, traceId_);
786     return ret;
787 }
788 
DeselectTrack(int32_t index)789 int32_t PlayerImpl::DeselectTrack(int32_t index)
790 {
791     int64_t startTime = SteadyClock::GetCurrentTimeMs();
792     ScopedTimer timer("DeselectTrack", OVERTIME_WARNING_MS);
793     MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " DeselectTrack in", FAKE_POINTER(this));
794     CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
795     int32_t ret = MSERR_OK;
796     LISTENER(ret = playerService_->DeselectTrack(index), "DeselectTrack", false, TIME_OUT_SECOND);
797     CHECK_AND_RETURN_RET_NOLOG(ret != MSERR_OK && hiAppEventAgent_ != nullptr, ret);
798     hiAppEventAgent_->TraceApiEvent(ret, "DeselectTrack", startTime, traceId_);
799     return ret;
800 }
801 
GetCurrentTrack(int32_t trackType,int32_t & index)802 int32_t PlayerImpl::GetCurrentTrack(int32_t trackType, int32_t &index)
803 {
804     int64_t startTime = SteadyClock::GetCurrentTimeMs();
805     ScopedTimer timer("GetCurrentTrack", OVERTIME_WARNING_MS);
806     MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " GetCurrentTrack in", FAKE_POINTER(this));
807     CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
808     int32_t ret = MSERR_OK;
809     LISTENER(ret = playerService_->GetCurrentTrack(trackType, index), "GetCurrentTrack", false, TIME_OUT_SECOND);
810     CHECK_AND_RETURN_RET_NOLOG(ret != MSERR_OK && hiAppEventAgent_ != nullptr, ret);
811     hiAppEventAgent_->TraceApiEvent(ret, "GetCurrentTrack", startTime, traceId_);
812     return ret;
813 }
814 
815 
SetDecryptConfig(const sptr<DrmStandard::IMediaKeySessionService> & keySessionProxy,bool svp)816 int32_t PlayerImpl::SetDecryptConfig(const sptr<DrmStandard::IMediaKeySessionService> &keySessionProxy, bool svp)
817 {
818     MEDIA_LOGI("PlayerImpl DRM SetDecryptConfig");
819 #ifdef SUPPORT_AVPLAYER_DRM
820     ScopedTimer timer("SetDecryptConfig", OVERTIME_WARNING_MS);
821     CHECK_AND_RETURN_RET_LOG(keySessionProxy != nullptr, MSERR_INVALID_VAL, "keysessionproxy is nullptr");
822     CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
823     MEDIA_LOGD("And it's count is: %{public}d in PlayerImpl", keySessionProxy->GetSptrRefCount());
824     LISTENER(return playerService_->SetDecryptConfig(keySessionProxy, svp), "SetDecryptConfig", false, TIME_OUT_SECOND);
825 #else
826     (void)keySessionProxy;
827     (void)svp;
828     return 0;
829 #endif
830 }
831 
SetDeviceChangeCbStatus(bool status)832 int32_t PlayerImpl::SetDeviceChangeCbStatus(bool status)
833 {
834     ScopedTimer timer("SetDeviceChangeCbStatus", OVERTIME_WARNING_MS);
835     MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " SetDeviceChangeCbStatus in, status is %{public}d",
836         FAKE_POINTER(this), status);
837     CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist.");
838     LISTENER(return playerService_->SetDeviceChangeCbStatus(status), "SetDeviceChangeCbStatus", false, TIME_OUT_SECOND);
839 }
840 
SetPlaybackStrategy(AVPlayStrategy playbackStrategy)841 int32_t PlayerImpl::SetPlaybackStrategy(AVPlayStrategy playbackStrategy)
842 {
843     int64_t startTime = SteadyClock::GetCurrentTimeMs();
844     ScopedTimer timer("SetPlaybackStrategy", OVERTIME_WARNING_MS);
845     MEDIA_LOGD("Set playback strategy");
846     CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist.");
847     int32_t ret = MSERR_OK;
848     LISTENER(
849         ret = playerService_->SetPlaybackStrategy(playbackStrategy), "SetPlaybackStrategy", false, TIME_OUT_SECOND);
850     CHECK_AND_RETURN_RET_NOLOG(ret != MSERR_OK && hiAppEventAgent_ != nullptr, ret);
851     hiAppEventAgent_->TraceApiEvent(ret, "SetPlaybackStrategy", startTime, traceId_);
852     return ret;
853 }
854 
SetSuperResolution(bool enabled)855 int32_t PlayerImpl::SetSuperResolution(bool enabled)
856 {
857     int64_t startTime = SteadyClock::GetCurrentTimeMs();
858     ScopedTimer timer("SetSuperResolution", OVERTIME_WARNING_MS);
859     MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " SetSuperResolution in", FAKE_POINTER(this));
860     CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist.");
861     int32_t ret = MSERR_OK;
862     LISTENER(ret = playerService_->SetSuperResolution(enabled), "SetSuperResolution", false, TIME_OUT_SECOND);
863     CHECK_AND_RETURN_RET_NOLOG(ret != MSERR_OK && hiAppEventAgent_ != nullptr, ret);
864     hiAppEventAgent_->TraceApiEvent(ret, "SetSuperResolution", startTime, traceId_);
865     return ret;
866 }
867 
SetVideoWindowSize(int32_t width,int32_t height)868 int32_t PlayerImpl::SetVideoWindowSize(int32_t width, int32_t height)
869 {
870     int64_t startTime = SteadyClock::GetCurrentTimeMs();
871     ScopedTimer timer("SetVideoWindowSize", OVERTIME_WARNING_MS);
872     MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR "SetVideoWindowSize  in", FAKE_POINTER(this));
873     CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist.");
874     int32_t ret = MSERR_OK;
875     LISTENER(ret = playerService_->SetVideoWindowSize(width, height), "SetVideoWindowSize", false, TIME_OUT_SECOND);
876     CHECK_AND_RETURN_RET_NOLOG(ret != MSERR_OK && hiAppEventAgent_ != nullptr, ret);
877     hiAppEventAgent_->TraceApiEvent(ret, "SetVideoWindowSize", startTime, traceId_);
878     return ret;
879 }
880 
SetMaxAmplitudeCbStatus(bool status)881 int32_t PlayerImpl::SetMaxAmplitudeCbStatus(bool status)
882 {
883     int64_t startTime = SteadyClock::GetCurrentTimeMs();
884     ScopedTimer timer("SetMaxAmplitudeCbStatus", OVERTIME_WARNING_MS);
885     MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " SetMaxAmplitudeCbStatus in, status is %{public}d",
886         FAKE_POINTER(this), status);
887     CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist.");
888     int32_t ret = MSERR_OK;
889     LISTENER(ret = playerService_->SetMaxAmplitudeCbStatus(status), "SetMaxAmplitudeCbStatus", false, TIME_OUT_SECOND);
890     CHECK_AND_RETURN_RET_NOLOG(ret != MSERR_OK && hiAppEventAgent_ != nullptr, ret);
891     hiAppEventAgent_->TraceApiEvent(ret, "SetMaxAmplitudeCbStatus", startTime, traceId_);
892     return ret;
893 }
894 
IsSeekContinuousSupported()895 bool PlayerImpl::IsSeekContinuousSupported()
896 {
897     MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " IsSeekContinuousSupported in", FAKE_POINTER(this));
898     CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, false, "player service does not exist.");
899     return playerService_->IsSeekContinuousSupported();
900 }
901 
SetReopenFd(int32_t fd)902 int32_t PlayerImpl::SetReopenFd(int32_t fd)
903 {
904     MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR "SetReopenFd  in", FAKE_POINTER(this));
905     CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist.");
906     MEDIA_LOGD("set reopen fd: %{public}d", fd);
907     LISTENER(return playerService_->SetReopenFd(fd), "SetReopenFd", false, TIME_OUT_SECOND);
908 }
909 
EnableCameraPostprocessing()910 int32_t PlayerImpl::EnableCameraPostprocessing()
911 {
912     MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " EnableCameraPostprocessing  in", FAKE_POINTER(this));
913     ScopedTimer timer("EnableCameraPostprocessing", OVERTIME_WARNING_MS);
914     CHECK_AND_RETURN_RET_LOG(fdsanFd_ != nullptr, MSERR_OK, "source fd does not exist.");
915     int fd = fdsanFd_->Get();
916     MEDIA_LOGD("PlayerImpl EnableCameraPostprocessing reopen fd: %{public}d ", fd);
917     FdsanFd reopenFd = FdUtils::ReOpenFd(fd);
918     CHECK_AND_RETURN_RET_LOG(reopenFd.Get() >= 0, MSERR_UNKNOWN, "EnableCameraPostprocessing: reopen failed");
919     auto ret = SetReopenFd(reopenFd.Get());
920     reopenFd.Reset();
921     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, MSERR_OK, "SetReopenFd failed.");
922     CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist.");
923     LISTENER(return playerService_->EnableCameraPostprocessing(), "EnableCameraPostprocessing", false, TIME_OUT_SECOND);
924 }
925 
SetCameraPostprocessing(bool isOpen)926 int32_t PlayerImpl::SetCameraPostprocessing(bool isOpen)
927 {
928     MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR "SetCameraPostprocessing  in", FAKE_POINTER(this));
929     ScopedTimer timer("SetCameraPostprocessing", OVERTIME_WARNING_MS);
930     CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist.");
931     LISTENER(return playerService_->SetCameraPostprocessing(isOpen), "SetCameraPostprocessing", false, TIME_OUT_SECOND);
932 }
933 
SetSeiMessageCbStatus(bool status,const std::vector<int32_t> & payloadTypes)934 int32_t PlayerImpl::SetSeiMessageCbStatus(bool status, const std::vector<int32_t> &payloadTypes)
935 {
936     int64_t startTime = SteadyClock::GetCurrentTimeMs();
937     ScopedTimer timer("SetSeiMessageCbStatus", OVERTIME_WARNING_MS);
938     MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " SetSeiMessageCbStatus in, status is %{public}d",
939         FAKE_POINTER(this), status);
940     CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist.");
941     int32_t ret = MSERR_OK;
942     LISTENER(ret = playerService_->SetSeiMessageCbStatus(status, payloadTypes),
943         "SetSeiMessageCbStatus", false, TIME_OUT_SECOND);
944     CHECK_AND_RETURN_RET_NOLOG(ret != MSERR_OK && hiAppEventAgent_ != nullptr, ret);
945     hiAppEventAgent_->TraceApiEvent(ret, "SetSeiMessageCbStatus", startTime, traceId_);
946     return ret;
947 }
948 
EnableReportMediaProgress(bool enable)949 int32_t PlayerImpl::EnableReportMediaProgress(bool enable)
950 {
951     int64_t startTime = SteadyClock::GetCurrentTimeMs();
952     ScopedTimer timer("EnableReportMediaProgress", OVERTIME_WARNING_MS);
953     MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " EnableReportMediaProgress in, enable is %{public}d",
954         FAKE_POINTER(this), enable);
955     CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist.");
956     int32_t ret = MSERR_OK;
957     LISTENER(ret = playerService_->EnableReportMediaProgress(enable),
958         "EnableReportMediaProgress", false, TIME_OUT_SECOND);
959     CHECK_AND_RETURN_RET_NOLOG(ret != MSERR_OK && hiAppEventAgent_ != nullptr, ret);
960     hiAppEventAgent_->TraceApiEvent(ret, "EnableReportMediaProgress", startTime, traceId_);
961     return ret;
962 }
963 
EnableReportAudioInterrupt(bool enable)964 int32_t PlayerImpl::EnableReportAudioInterrupt(bool enable)
965 {
966     int64_t startTime = SteadyClock::GetCurrentTimeMs();
967     ScopedTimer timer("EnableReportAudioInterrupt", OVERTIME_WARNING_MS);
968     MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " EnableReportAudioInterrupt in, enable is %{public}d",
969         FAKE_POINTER(this), enable);
970     CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist.");
971     int32_t ret = MSERR_OK;
972     LISTENER(ret = playerService_->EnableReportAudioInterrupt(enable),
973         "EnableReportAudioInterrupt", false, TIME_OUT_SECOND);
974     CHECK_AND_RETURN_RET_NOLOG(ret != MSERR_OK && hiAppEventAgent_ != nullptr, ret);
975     hiAppEventAgent_->TraceApiEvent(ret, "EnableReportAudioInterrupt", startTime, traceId_);
976     return ret;
977 }
978 
ReleaseClientListener()979 void PlayerImpl::ReleaseClientListener()
980 {
981     ScopedTimer timer("ReleaseClientListener", OVERTIME_WARNING_MS);
982     MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " ReleaseClientListener in", FAKE_POINTER(this));
983     MediaServiceFactory::GetInstance().ReleaseClientListener(); // not related to playerService_ thus no XCollie
984 }
985 
SetStartFrameRateOptEnabled(bool enabled)986 int32_t PlayerImpl::SetStartFrameRateOptEnabled(bool enabled)
987 {
988     MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " SetStartFrameRateOptEnabled in, enabled is %{public}d",
989         FAKE_POINTER(this), enabled);
990     CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist.");
991     return playerService_->SetStartFrameRateOptEnabled(enabled);
992 }
993 
TraceApiEvent(int errCode,const std::string & message,int64_t startTime)994 void PlayerImpl::TraceApiEvent(int errCode, const std::string& message, int64_t startTime)
995 {
996     MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " TraceApiEvent in", FAKE_POINTER(this));
997     CHECK_AND_RETURN_LOG(hiAppEventAgent_ != nullptr, "hiAppEventAgent nullptr");
998     hiAppEventAgent_->TraceApiEvent(errCode, message, startTime, traceId_);
999 }
1000 
ForceLoadVideo(bool status)1001 int32_t PlayerImpl::ForceLoadVideo(bool status)
1002 {
1003     MEDIA_LOGI("PlayerImpl:0x%{public}06" PRIXPTR " ForceLoadVideo %{public}d", FAKE_POINTER(this), status);
1004     CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist.");
1005     return playerService_->ForceLoadVideo(status);
1006 }
1007 
PlayerImplCallback(const std::shared_ptr<PlayerCallback> playerCb,std::shared_ptr<PlayerImpl> player)1008 PlayerImplCallback::PlayerImplCallback(const std::shared_ptr<PlayerCallback> playerCb,
1009     std::shared_ptr<PlayerImpl> player)
1010 {
1011     playerCb_ = playerCb;
1012     player_ = player;
1013 }
1014 
OnInfo(PlayerOnInfoType type,int32_t extra,const Format & infoBody)1015 void PlayerImplCallback::OnInfo(PlayerOnInfoType type, int32_t extra, const Format &infoBody)
1016 {
1017     auto player = player_.lock();
1018     CHECK_AND_RETURN_LOG(player != nullptr, "player does not exist..");
1019     player->OnInfo(type, extra, infoBody);
1020 }
1021 
OnError(int32_t errorCode,const std::string & errorMsg)1022 void PlayerImplCallback::OnError(int32_t errorCode, const std::string &errorMsg)
1023 {
1024     int64_t startTime = SteadyClock::GetCurrentTimeMs();
1025     std::shared_ptr<PlayerCallback> playerCb;
1026     {
1027         std::unique_lock<std::mutex> lock(playerImplCbMutex_);
1028         playerCb = playerCb_;
1029     }
1030 
1031     auto player = player_.lock();
1032     if (player != nullptr && getApiVersionFlag_) {
1033         player->GetApiVersion(apiVersion_);
1034         getApiVersionFlag_ = false;
1035     }
1036     MEDIA_LOGI("PlayerImplCallback apiVersion %{public}d", apiVersion_);
1037     if (apiVersion_ < API_VERSION_14) {
1038         if (IsAPI14IOError(static_cast<MediaServiceErrCode>(errorCode))) {
1039             errorCode = MSERR_DATA_SOURCE_IO_ERROR;
1040         }
1041     }
1042     if (player != nullptr) {
1043         player->TraceApiEvent(errorCode, errorMsg, startTime);
1044     }
1045     CHECK_AND_RETURN_LOG(playerCb != nullptr, "playerCb does not exist..");
1046     playerCb->OnError(errorCode, errorMsg);
1047 }
1048 
1049 } // namespace Media
1050 } // namespace OHOS
1051