• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 "hw_cast_stream_player.h"
17 #include "int_wrapper.h"
18 #include "avsession_log.h"
19 #include "avcast_player_state.h"
20 #include "avqueue_item.h"
21 #include "avmedia_description.h"
22 #include "avsession_errors.h"
23 #include "avsession_sysevent.h"
24 #include "avsession_trace.h"
25 
26 using namespace OHOS::CastEngine;
27 
28 namespace OHOS::AVSession {
~HwCastStreamPlayer()29 HwCastStreamPlayer::~HwCastStreamPlayer()
30 {
31     SLOGI("destruct the HwCastStreamPlayer");
32     Release();
33 }
34 
Init()35 void HwCastStreamPlayer::Init()
36 {
37     SLOGI("Init the HwCastStreamPlayer");
38     std::lock_guard lockGuard(streamPlayerLock_);
39     if (streamPlayer_) {
40         SLOGI("register self in streamPlayer");
41         streamPlayer_->RegisterListener(shared_from_this());
42     }
43 }
44 
Release()45 void HwCastStreamPlayer::Release()
46 {
47     SLOGI("Release the HwCastStreamPlayer");
48     std::lock_guard lockGuard(streamPlayerLock_);
49     if (streamPlayer_) {
50         streamPlayer_->UnregisterListener();
51         streamPlayer_->Release();
52         streamPlayer_ = nullptr;
53     }
54     std::lock_guard playerListLockGuard(streamPlayerListenerListLock_);
55     streamPlayerListenerList_.clear();
56     SLOGI("Release the HwCastStreamPlayer done");
57 }
58 
CheckCastTime(int32_t castTime)59 int32_t HwCastStreamPlayer::CheckCastTime(int32_t castTime)
60 {
61     if (castTime < castMinTime) {
62         return castMinTime * castTime;
63     } else {
64         return castTime;
65     }
66 }
67 
SendControlCommand(const AVCastControlCommand castControlCommand)68 void HwCastStreamPlayer::SendControlCommand(const AVCastControlCommand castControlCommand)
69 {
70     int32_t commandNum = castControlCommand.GetCommand();
71     SLOGI("send command to streamPlayer %{public}d", static_cast<int32_t>(commandNum));
72     std::lock_guard lockGuard(streamPlayerLock_);
73     if (!streamPlayer_) {
74         SLOGE("streamPlayer is nullptr");
75         HISYSEVENT_FAULT("REMOTE_CONTROL_FAILED",
76             "SESSION_TYPE", "cast",
77             "ERROR_TYPE", "INNER_ERROR",
78             "ERROR_INFO", "streamPlayer is nullptr");
79         return;
80     }
81     switch (castControlCommand.GetCommand()) {
82         case AVCastControlCommand::CAST_CONTROL_CMD_PLAY:
83             streamPlayer_->Play();
84             break;
85         case AVCastControlCommand::CAST_CONTROL_CMD_PAUSE:
86             streamPlayer_->Pause();
87             break;
88         case AVCastControlCommand::CAST_CONTROL_CMD_STOP:
89             streamPlayer_->Stop();
90             break;
91         case AVCastControlCommand::CAST_CONTROL_CMD_PLAY_NEXT:
92             streamPlayer_->Next();
93             break;
94         case AVCastControlCommand::CAST_CONTROL_CMD_PLAY_PREVIOUS:
95             streamPlayer_->Previous();
96             break;
97         default:
98             SendControlCommandWithParams(castControlCommand);
99             break;
100     }
101 }
102 
SendControlCommandWithParams(const AVCastControlCommand castControlCommand)103 void HwCastStreamPlayer::SendControlCommandWithParams(const AVCastControlCommand castControlCommand)
104 {
105     std::lock_guard lockGuard(streamPlayerLock_);
106     int32_t timeParam;
107     switch (castControlCommand.GetCommand()) {
108         case AVCastControlCommand::CAST_CONTROL_CMD_FAST_FORWARD:
109             castControlCommand.GetForwardTime(timeParam);
110             streamPlayer_->FastForward(CheckCastTime(timeParam));
111             break;
112         case AVCastControlCommand::CAST_CONTROL_CMD_REWIND:
113             castControlCommand.GetRewindTime(timeParam);
114             streamPlayer_->FastRewind(CheckCastTime(timeParam));
115             break;
116         case AVCastControlCommand::CAST_CONTROL_CMD_SEEK:
117             castControlCommand.GetSeekTime(timeParam);
118             streamPlayer_->Seek(timeParam);
119             break;
120         case AVCastControlCommand::CAST_CONTROL_CMD_SET_VOLUME:
121             int32_t volume;
122             castControlCommand.GetVolume(volume);
123             streamPlayer_->SetVolume(volume);
124             break;
125         case AVCastControlCommand::CAST_CONTROL_CMD_SET_SPEED:
126             int32_t speed;
127             castControlCommand.GetSpeed(speed);
128             streamPlayer_->SetSpeed(static_cast<CastEngine::PlaybackSpeed>(speed));
129             break;
130         case AVCastControlCommand::CAST_CONTROL_CMD_SET_LOOP_MODE:
131             int32_t loopMode;
132             castControlCommand.GetLoopMode(loopMode);
133             if (intLoopModeToCastPlus_.count(loopMode) != 0) {
134                 streamPlayer_->SetLoopMode(intLoopModeToCastPlus_[loopMode]);
135             } else {
136                 SLOGE("invalid LoopMode: %{public}d", loopMode);
137             }
138             break;
139         case AVCastControlCommand::CAST_CONTROL_CMD_TOGGLE_FAVORITE:
140             break;
141         case AVCastControlCommand::CAST_CONTROL_CMD_TOGGLE_MUTE:
142             bool enableMute;
143             streamPlayer_->GetMute(enableMute);
144             streamPlayer_->SetMute(!enableMute);
145             break;
146         default:
147             SLOGE("invalid command");
148             HISYSEVENT_FAULT("REMOTE_CONTROL_FAILED", "ERROR_TYPE", "INNER_ERROR", "ERROR_INFO", "invalid command");
149             break;
150     }
151 }
152 
GetCurrentItem()153 AVQueueItem HwCastStreamPlayer::GetCurrentItem()
154 {
155     std::lock_guard lockGuard(streamPlayerLock_);
156     SLOGI("Received GetCurrentItem request");
157     int32_t duration;
158     GetDuration(duration);
159     std::shared_ptr<AVMediaDescription> mediaDescription = currentAVQueueItem_.GetDescription();
160     if (mediaDescription == nullptr) {
161         SLOGE("GetCurrentItem with nullptr, return with default");
162         return currentAVQueueItem_;
163     }
164     mediaDescription->SetDuration(duration);
165     AVQueueItem queueItem;
166     queueItem.SetDescription(mediaDescription);
167     currentAVQueueItem_ = queueItem;
168     return currentAVQueueItem_;
169 }
170 
Start(const AVQueueItem & avQueueItem)171 int32_t HwCastStreamPlayer::Start(const AVQueueItem& avQueueItem)
172 {
173     CastEngine::MediaInfo mediaInfo;
174     std::shared_ptr<AVMediaDescription> mediaDescription = avQueueItem.GetDescription();
175     mediaInfo.mediaId = mediaDescription->GetMediaId();
176     mediaInfo.mediaName = mediaDescription->GetTitle();
177     if (mediaDescription->GetMediaUri() == "") {
178         if (mediaDescription->GetFdSrc().fd_ == 0) {
179             SLOGW("No media id and fd src");
180             mediaInfo.mediaUrl = "http:";
181         } else {
182             mediaInfo.mediaUrl = std::to_string(mediaDescription->GetFdSrc().fd_);
183         }
184     } else {
185         mediaInfo.mediaUrl = mediaDescription->GetMediaUri();
186     }
187     mediaInfo.mediaType = mediaDescription->GetMediaType();
188     mediaInfo.mediaSize = mediaDescription->GetMediaSize();
189     mediaInfo.startPosition = static_cast<uint32_t>(mediaDescription->GetStartPosition());
190     mediaInfo.duration = static_cast<uint32_t>(mediaDescription->GetDuration());
191     mediaInfo.closingCreditsPosition = static_cast<uint32_t>(mediaDescription->GetCreditsPosition());
192     if (mediaDescription->GetIconUri() == "") {
193         mediaInfo.albumCoverUrl = mediaDescription->GetAlbumCoverUri();
194     } else {
195         mediaInfo.albumCoverUrl = mediaDescription->GetIconUri();
196     }
197     mediaInfo.albumTitle = mediaDescription->GetAlbumTitle();
198     mediaInfo.mediaArtist = mediaDescription->GetArtist();
199     mediaInfo.lrcUrl = mediaDescription->GetLyricUri();
200     mediaInfo.appIconUrl = mediaDescription->GetIconUri();
201     mediaInfo.appName = mediaDescription->GetAppName();
202     std::lock_guard lockGuard(streamPlayerLock_);
203     SLOGI("mediaInfo media is %{public}s %{public}s", mediaInfo.albumCoverUrl.c_str(), mediaInfo.mediaUrl.c_str());
204     if (!streamPlayer_) {
205         SLOGE("Set media info and start failed");
206         return AVSESSION_ERROR;
207     }
208     if (currentAVQueueItem_.GetDescription() && currentAVQueueItem_.GetDescription()->GetMediaUri() != "http:" &&
209         currentAVQueueItem_.GetDescription()->GetMediaId() == mediaInfo.mediaId) {
210         if (streamPlayer_->Play() != AVSESSION_SUCCESS) {
211             SLOGE("Set media info and start failed");
212             return AVSESSION_ERROR;
213         }
214     } else if (streamPlayer_->Play(mediaInfo) != AVSESSION_SUCCESS) {
215         SLOGE("Set media info and start failed");
216         return AVSESSION_ERROR;
217     }
218     currentAVQueueItem_ = avQueueItem;
219     SLOGI("Set media info and start successfully");
220     return AVSESSION_SUCCESS;
221 }
222 
Prepare(const AVQueueItem & avQueueItem)223 int32_t HwCastStreamPlayer::Prepare(const AVQueueItem& avQueueItem)
224 {
225     CastEngine::MediaInfo mediaInfo;
226     std::shared_ptr<AVMediaDescription> mediaDescription = avQueueItem.GetDescription();
227     mediaInfo.mediaId = mediaDescription->GetMediaId();
228     mediaInfo.mediaName = mediaDescription->GetTitle();
229     if (mediaDescription->GetMediaUri() == "") {
230         if (mediaDescription->GetFdSrc().fd_ == 0) {
231             SLOGW("No media id and fd src");
232             mediaInfo.mediaUrl = "http:";
233             avQueueItem.GetDescription()->SetMediaUri("http:");
234         } else {
235             mediaInfo.mediaUrl = std::to_string(mediaDescription->GetFdSrc().fd_);
236         }
237     } else {
238         mediaInfo.mediaUrl = mediaDescription->GetMediaUri();
239     }
240     mediaInfo.mediaType = mediaDescription->GetMediaType();
241     mediaInfo.mediaSize = mediaDescription->GetMediaSize();
242     mediaInfo.startPosition = static_cast<uint32_t>(mediaDescription->GetStartPosition());
243     mediaInfo.duration = static_cast<uint32_t>(mediaDescription->GetDuration());
244     mediaInfo.closingCreditsPosition = static_cast<uint32_t>(mediaDescription->GetCreditsPosition());
245     if (mediaDescription->GetIconUri() == "") {
246         mediaInfo.albumCoverUrl = mediaDescription->GetAlbumCoverUri();
247     } else {
248         mediaInfo.albumCoverUrl = mediaDescription->GetIconUri();
249     }
250     mediaInfo.albumTitle = mediaDescription->GetAlbumTitle();
251     mediaInfo.mediaArtist = mediaDescription->GetArtist();
252     mediaInfo.lrcUrl = mediaDescription->GetLyricUri();
253     mediaInfo.appIconUrl = mediaDescription->GetIconUri();
254     mediaInfo.appName = mediaDescription->GetAppName();
255     SLOGD("mediaInfo albumCoverUrl is %{public}s", mediaInfo.albumCoverUrl.c_str());
256     std::lock_guard lockGuard(streamPlayerLock_);
257     SLOGI("mediaInfo mediaUrl is %{public}s", mediaInfo.mediaUrl.c_str());
258     if (streamPlayer_ && streamPlayer_->Load(mediaInfo) == AVSESSION_SUCCESS) {
259         SLOGI("Set media info and prepare successed");
260         currentAVQueueItem_ = avQueueItem;
261         return AVSESSION_SUCCESS;
262     }
263     SLOGE("Set media info and prepare failed");
264     return AVSESSION_ERROR;
265 }
266 
GetDuration(int32_t & duration)267 int32_t HwCastStreamPlayer::GetDuration(int32_t& duration)
268 {
269     SLOGI("GetDuration begin");
270     std::lock_guard lockGuard(streamPlayerLock_);
271     if (!streamPlayer_) {
272         SLOGE("streamPlayer is nullptr");
273         return AVSESSION_ERROR;
274     }
275     streamPlayer_->GetDuration(duration);
276     SLOGI("GetDuration successed");
277     return AVSESSION_SUCCESS;
278 }
279 
GetCastAVPlaybackState(AVPlaybackState & avPlaybackState)280 int32_t HwCastStreamPlayer::GetCastAVPlaybackState(AVPlaybackState& avPlaybackState)
281 {
282     SLOGI("GetCastAVPlaybackState begin");
283     std::lock_guard lockGuard(streamPlayerLock_);
284     if (!streamPlayer_) {
285         SLOGE("streamPlayer is nullptr");
286         return AVSESSION_ERROR;
287     }
288     CastEngine::PlayerStates castPlayerStates;
289     streamPlayer_->GetPlayerStatus(castPlayerStates);
290     if (castPlusStateToString_.count(castPlayerStates) != 0) {
291         avPlaybackState.SetState(castPlusStateToString_[castPlayerStates]);
292     }
293     CastEngine::PlaybackSpeed castPlaybackSpeed;
294     streamPlayer_->GetPlaySpeed(castPlaybackSpeed);
295     if (castPlusSpeedToDouble_.count(castPlaybackSpeed) != 0) {
296         avPlaybackState.SetSpeed(castPlusSpeedToDouble_[castPlaybackSpeed]);
297     }
298     int castPosition;
299     streamPlayer_->GetPosition(castPosition);
300     AVPlaybackState::Position position;
301     position.elapsedTime_ = static_cast<int64_t>(castPosition);
302     avPlaybackState.SetPosition(position);
303     CastEngine::LoopMode castLoopMode;
304     streamPlayer_->GetLoopMode(castLoopMode);
305     if (castPlusLoopModeToInt_.count(castLoopMode) != 0) {
306         avPlaybackState.SetLoopMode(castPlusLoopModeToInt_[castLoopMode]);
307     }
308     int32_t castVolume;
309     int32_t maxCastVolume;
310     streamPlayer_->GetVolume(castVolume, maxCastVolume);
311     avPlaybackState.SetVolume(castVolume);
312 
313     std::shared_ptr<AAFwk::WantParams> wantParams = std::make_shared<AAFwk::WantParams>();
314     sptr<AAFwk::IInterface> intIt = AAFwk::Integer::Box(maxCastVolume);
315     if (wantParams == nullptr || intIt == nullptr) {
316         return AVSESSION_ERROR;
317     }
318     wantParams->SetParam("maxCastVolume", intIt);
319     avPlaybackState.SetExtras(wantParams);
320 
321     SLOGI("GetCastAVPlaybackState successed with state: %{public}d", avPlaybackState.GetState());
322     return AVSESSION_SUCCESS;
323 }
324 
SetDisplaySurface(std::string & surfaceId)325 int32_t HwCastStreamPlayer::SetDisplaySurface(std::string &surfaceId)
326 {
327     SLOGI("SetDisplaySurface begin");
328     std::lock_guard lockGuard(streamPlayerLock_);
329     if (!streamPlayer_) {
330         SLOGE("streamPlayer is nullptr");
331         return AVSESSION_ERROR;
332     }
333     streamPlayer_->SetSurface(surfaceId);
334     SLOGI("SetDisplaySurface successed");
335     return AVSESSION_SUCCESS;
336 }
337 
RegisterControllerListener(std::shared_ptr<IAVCastControllerProxyListener> listener)338 int32_t HwCastStreamPlayer::RegisterControllerListener(std::shared_ptr<IAVCastControllerProxyListener> listener)
339 {
340     SLOGI("RegisterControllerListener begin");
341     if (listener == nullptr) {
342         SLOGE("RegisterControllerListener failed for the listener is nullptr");
343         return AVSESSION_ERROR;
344     }
345     std::lock_guard playerListLockGuard(streamPlayerListenerListLock_);
346     if (find(streamPlayerListenerList_.begin(), streamPlayerListenerList_.end(), listener)
347         != streamPlayerListenerList_.end()) {
348         SLOGE("listener is already in streamPlayerListenerList_");
349         return AVSESSION_ERROR;
350     }
351     SLOGI("RegisterControllerListener successed, and add it to streamPlayerListenerList_");
352     streamPlayerListenerList_.emplace_back(listener);
353     SLOGI("RegisterControllerListener done");
354     return AVSESSION_SUCCESS;
355 }
356 
UnRegisterControllerListener(std::shared_ptr<IAVCastControllerProxyListener> listener)357 int32_t HwCastStreamPlayer::UnRegisterControllerListener(std::shared_ptr<IAVCastControllerProxyListener> listener)
358 {
359     if (listener == nullptr) {
360         SLOGE("UnRegisterCastSessionStateListener failed for the listener is nullptr");
361         return AVSESSION_ERROR;
362     }
363     std::lock_guard playerListLockGuard(streamPlayerListenerListLock_);
364     for (auto iter = streamPlayerListenerList_.begin(); iter != streamPlayerListenerList_.end();) {
365         if (*iter == listener) {
366             streamPlayerListenerList_.erase(iter);
367             SLOGI("UnRegisterControllerListener successed, and erase it from streamPlayerListenerList_");
368             return AVSESSION_SUCCESS;
369         } else {
370             ++iter;
371         }
372     }
373     SLOGE("listener is not found in streamPlayerListenerList_, so UnRegisterControllerListener failed");
374 
375     return AVSESSION_ERROR;
376 }
377 
GetValidAbility(const std::vector<int32_t> & validAbilityList)378 int32_t HwCastStreamPlayer::GetValidAbility(const std::vector<int32_t>& validAbilityList)
379 {
380     SLOGI("GetValidAbility in");
381     CastEngine::StreamCapability &streamCapability;
382     if (!streamPlayer_) {
383         SLOGE("streamPlayer is nullptr");
384         return AVSESSION_ERROR;
385     }
386     streamPlayer_->GetAvailableCapability(streamCapability);
387     checkCmdsFromAbility(streamCapability, validAbilityList);
388     return AVSESSION_SUCCESS;
389 }
390 
SetValidAbility(const std::vector<int32_t> & validAbilityList)391 int32_t HwCastStreamPlayer::SetValidAbility(const std::vector<int32_t>& validAbilityList)
392 {
393     SLOGI("SetValidAbility begin");
394     CastEngine::StreamCapability &streamCapability;
395     if (!streamPlayer_) {
396         SLOGE("streamPlayer is nullptr");
397         return AVSESSION_ERROR;
398     }
399     checkAbilityFromCmds(validAbilityList, streamCapability);
400     streamPlayer_->SetAvailableCapability(streamCapability);
401     return AVSESSION_SUCCESS;
402 }
403 
OnStateChanged(const CastEngine::PlayerStates playbackState,bool isPlayWhenReady)404 void HwCastStreamPlayer::OnStateChanged(const CastEngine::PlayerStates playbackState, bool isPlayWhenReady)
405 {
406     AVPlaybackState avCastPlaybackState;
407     if (castPlusStateToString_.count(playbackState) == 0) {
408         SLOGE("current playbackState status is not exist in castPlusStateToString_");
409         avCastPlaybackState.SetState(AVPlaybackState::PLAYBACK_STATE_ERROR);
410     } else {
411         SLOGD("On state changed, get state %{public}d", castPlusStateToString_[playbackState]);
412         avCastPlaybackState.SetState(castPlusStateToString_[playbackState]);
413     }
414     std::lock_guard playerListLockGuard(streamPlayerListenerListLock_);
415     for (auto listener : streamPlayerListenerList_) {
416         if (listener != nullptr) {
417             SLOGI("trigger the OnCastPlaybackStateChange for registered listeners");
418             listener->OnCastPlaybackStateChange(avCastPlaybackState);
419         }
420     }
421 }
422 
OnPositionChanged(int position,int bufferPosition,int duration)423 void HwCastStreamPlayer::OnPositionChanged(int position, int bufferPosition, int duration)
424 {
425     if (position == -1 && bufferPosition == -1 && duration == -1) { // -1 is invalid(default) value
426         SLOGW("Invalid position change callback");
427         return;
428     }
429     AVPlaybackState avCastPlaybackState;
430     if (position != -1) { // -1 is invalid position
431         AVPlaybackState::Position castPosition;
432         castPosition.elapsedTime_ = position;
433         avCastPlaybackState.SetPosition(castPosition);
434         SLOGD("Received elapsedTime: %{public}d", position);
435     }
436     if (bufferPosition != -1) { // -1 is invalid buffer position
437         avCastPlaybackState.SetBufferedTime(bufferPosition);
438         SLOGD("Received bufferPosition: %{public}d", bufferPosition);
439     }
440     if (duration != -1) {
441         std::shared_ptr<AAFwk::WantParams> wantParams = std::make_shared<AAFwk::WantParams>();
442         sptr<AAFwk::IInterface> intIt = AAFwk::Integer::Box(duration);
443         wantParams->SetParam("duration", intIt);
444         avCastPlaybackState.SetExtras(wantParams);
445         SLOGD("Received duration: %{public}d", duration);
446     }
447     std::lock_guard playerListLockGuard(streamPlayerListenerListLock_);
448     for (auto listener : streamPlayerListenerList_) {
449         if (listener != nullptr) {
450             SLOGI("trigger the OnPositionChange for registered listeners");
451             listener->OnCastPlaybackStateChange(avCastPlaybackState);
452         }
453     }
454     SLOGI("on cast position change done");
455 }
456 
OnMediaItemChanged(const CastEngine::MediaInfo & mediaInfo)457 void HwCastStreamPlayer::OnMediaItemChanged(const CastEngine::MediaInfo& mediaInfo)
458 {
459     SLOGD("Stream player received mediaItemChanged event");
460     std::shared_ptr<AVMediaDescription> mediaDescription = std::make_shared<AVMediaDescription>();
461     mediaDescription->SetMediaId(mediaInfo.mediaId);
462     mediaDescription->SetTitle(mediaInfo.mediaName);
463     mediaDescription->SetMediaUri(mediaInfo.mediaUrl);
464     mediaDescription->SetMediaType(mediaInfo.mediaType);
465     mediaDescription->SetMediaSize(mediaInfo.mediaSize);
466     mediaDescription->SetStartPosition(static_cast<uint32_t>(mediaInfo.startPosition));
467     mediaDescription->SetDuration(static_cast<uint32_t>(mediaInfo.duration));
468     mediaDescription->SetCreditsPosition(static_cast<int32_t>(mediaInfo.closingCreditsPosition));
469     mediaDescription->SetAlbumCoverUri(mediaInfo.albumCoverUrl);
470     mediaDescription->SetAlbumTitle(mediaInfo.albumTitle);
471     mediaDescription->SetArtist(mediaInfo.mediaArtist);
472     mediaDescription->SetLyricUri(mediaInfo.lrcUrl);
473     mediaDescription->SetIconUri(mediaInfo.appIconUrl);
474     mediaDescription->SetAppName(mediaInfo.appName);
475     AVQueueItem queueItem;
476     queueItem.SetDescription(mediaDescription);
477     std::lock_guard playerListLockGuard(streamPlayerListenerListLock_);
478     for (auto listener : streamPlayerListenerList_) {
479         if (listener != nullptr) {
480             SLOGI("trigger the OnMediaItemChange for registered listeners");
481             listener->OnMediaItemChange(queueItem);
482         }
483     }
484     std::lock_guard lockGuard(streamPlayerLock_);
485     currentAVQueueItem_ = queueItem;
486     SLOGI("Stream player received mediaItemChanged event done");
487 }
488 
OnNextRequest()489 void HwCastStreamPlayer::OnNextRequest()
490 {
491     SLOGD("StreamPlayer received next request");
492     std::lock_guard playerListLockGuard(streamPlayerListenerListLock_);
493     for (auto listener : streamPlayerListenerList_) {
494         if (listener != nullptr) {
495             SLOGI("trigger the OnPlayNext for registered listeners");
496             listener->OnPlayNext();
497         }
498     }
499     SLOGI("StreamPlayer received next request done");
500 }
501 
OnPreviousRequest()502 void HwCastStreamPlayer::OnPreviousRequest()
503 {
504     SLOGD("StreamPlayer received previous request");
505     std::lock_guard playerListLockGuard(streamPlayerListenerListLock_);
506     for (auto listener : streamPlayerListenerList_) {
507         if (listener != nullptr) {
508             SLOGI("trigger the OnPlayPrevious for registered listeners");
509             listener->OnPlayPrevious();
510         }
511     }
512     SLOGI("StreamPlayer received previous request done");
513 }
514 
OnVolumeChanged(int volume,int maxVolume)515 void HwCastStreamPlayer::OnVolumeChanged(int volume, int maxVolume)
516 {
517     SLOGD("StreamPlayer received volume changed event: %{public}d", volume);
518     AVPlaybackState avCastPlaybackState;
519     avCastPlaybackState.SetVolume(volume);
520 
521     std::shared_ptr<AAFwk::WantParams> wantParams = std::make_shared<AAFwk::WantParams>();
522     sptr<AAFwk::IInterface> intIt = AAFwk::Integer::Box(maxVolume);
523     if (wantParams == nullptr || intIt == nullptr) {
524         return;
525     }
526     wantParams->SetParam("maxCastVolume", intIt);
527     avCastPlaybackState.SetExtras(wantParams);
528 
529     std::lock_guard playerListLockGuard(streamPlayerListenerListLock_);
530     for (auto listener : streamPlayerListenerList_) {
531         if (listener != nullptr) {
532             SLOGI("trigger the OnVolumeChanged for registered listeners");
533             listener->OnCastPlaybackStateChange(avCastPlaybackState);
534         }
535     }
536     SLOGI("StreamPlayer received volume changed event done: %{public}d", volume);
537 }
538 
OnLoopModeChanged(const CastEngine::LoopMode loopMode)539 void HwCastStreamPlayer::OnLoopModeChanged(const CastEngine::LoopMode loopMode)
540 {
541     AVPlaybackState avCastPlaybackState;
542     if (castPlusLoopModeToInt_.count(loopMode) == 0) {
543         SLOGE("current playbackState status is not exist in castPlusStateToString_");
544     } else {
545         SLOGD("StreamPlayer received loop mode changed event: %{public}d", castPlusLoopModeToInt_[loopMode]);
546         avCastPlaybackState.SetLoopMode(castPlusLoopModeToInt_[loopMode]);
547     }
548     std::lock_guard playerListLockGuard(streamPlayerListenerListLock_);
549     for (auto listener : streamPlayerListenerList_) {
550         if (listener != nullptr) {
551             SLOGI("trigger the OnLoopModeChanged for registered listeners");
552             listener->OnCastPlaybackStateChange(avCastPlaybackState);
553         }
554     }
555     SLOGD("loop mode changed event done: %{public}d", castPlusLoopModeToInt_[loopMode]);
556 }
557 
OnPlaySpeedChanged(const CastEngine::PlaybackSpeed speed)558 void HwCastStreamPlayer::OnPlaySpeedChanged(const CastEngine::PlaybackSpeed speed)
559 {
560     AVPlaybackState avCastPlaybackState;
561     if (castPlusSpeedToDouble_.count(speed) == 0) {
562         SLOGE("current speed is not exist in castPlusSpeedToDouble_");
563         return;
564     }
565     SLOGD("StreamPlayer received play speed changed event: %{public}f", castPlusSpeedToDouble_[speed]);
566     avCastPlaybackState.SetSpeed(castPlusSpeedToDouble_[speed]);
567     std::lock_guard playerListLockGuard(streamPlayerListenerListLock_);
568     for (auto listener : streamPlayerListenerList_) {
569         if (listener != nullptr) {
570             SLOGI("trigger the OnPositionChange for registered listeners");
571             listener->OnCastPlaybackStateChange(avCastPlaybackState);
572         }
573     }
574     SLOGD("play speed changed event done: %{public}f", castPlusSpeedToDouble_[speed]);
575 }
576 
OnPlayerError(int errorCode,const std::string & errorMsg)577 void HwCastStreamPlayer::OnPlayerError(int errorCode, const std::string &errorMsg)
578 {
579     SLOGD("StreamPlayer received error event, code: %{public}d, message: %{public}s", errorCode, errorMsg.c_str());
580     std::lock_guard playerListLockGuard(streamPlayerListenerListLock_);
581     for (auto listener : streamPlayerListenerList_) {
582         if (listener != nullptr) {
583             SLOGI("trigger the OnPlayerError for registered listeners");
584             listener->OnPlayerError(errorCode, errorMsg);
585         }
586     }
587     SLOGI("error event done, code: %{public}d, message: %{public}s", errorCode, errorMsg.c_str());
588 }
589 
OnSeekDone(int32_t seekNumber)590 void HwCastStreamPlayer::OnSeekDone(int32_t seekNumber)
591 {
592     SLOGD("StreamPlayer received seek done event: %{public}d", seekNumber);
593     std::lock_guard playerListLockGuard(streamPlayerListenerListLock_);
594     for (auto listener : streamPlayerListenerList_) {
595         if (listener != nullptr) {
596             SLOGI("trigger the OnSeekDone for registered listeners");
597             listener->OnSeekDone(seekNumber);
598         }
599     }
600     SLOGI("StreamPlayer received seek done event done with: %{public}d", seekNumber);
601 }
602 
OnVideoSizeChanged(int width,int height)603 void HwCastStreamPlayer::OnVideoSizeChanged(int width, int height)
604 {
605     SLOGD("StreamPlayer received video size change event, width: %{public}d, height: %{public}d", width, height);
606     std::lock_guard playerListLockGuard(streamPlayerListenerListLock_);
607     for (auto listener : streamPlayerListenerList_) {
608         if (listener != nullptr) {
609             SLOGI("trigger the OnVideoSizeChange for registered listeners");
610             listener->OnVideoSizeChange(width, height);
611         }
612     }
613     SLOGI("video size change event done, width: %{public}d, height: %{public}d", width, height);
614 }
615 
OnEndOfStream(int isLooping)616 void HwCastStreamPlayer::OnEndOfStream(int isLooping)
617 {
618     SLOGD("Received EndOfStream callback, value is %{public}d", isLooping);
619     std::lock_guard playerListLockGuard(streamPlayerListenerListLock_);
620     for (auto listener : streamPlayerListenerList_) {
621         if (listener != nullptr) {
622             SLOGI("trigger the OnEndOfStream for registered listeners");
623             listener->OnEndOfStream(isLooping);
624         }
625     }
626 
627     AVPlaybackState avCastPlaybackState;
628     std::shared_ptr<AAFwk::WantParams> wantParams = std::make_shared<AAFwk::WantParams>();
629     sptr<AAFwk::IInterface> intIt = AAFwk::Integer::Box(isLooping);
630     if (wantParams == nullptr || intIt == nullptr) {
631         return;
632     }
633     wantParams->SetParam("endofstream", intIt);
634     avCastPlaybackState.SetExtras(wantParams);
635     SLOGD("Received end of stream event: %{public}d", isLooping);
636 
637     for (auto listener : streamPlayerListenerList_) {
638         if (listener != nullptr) {
639             SLOGI("trigger the OnEndOfStream for registered listeners");
640             listener->OnCastPlaybackStateChange(avCastPlaybackState);
641         }
642     }
643     SLOGI("Received EndOfStream callback done, value is %{public}d", isLooping);
644 }
645 
OnPlayRequest(const CastEngine::MediaInfo & mediaInfo)646 void HwCastStreamPlayer::OnPlayRequest(const CastEngine::MediaInfo& mediaInfo)
647 {
648     SLOGD("Stream player received PlayRequest event");
649     std::shared_ptr<AVMediaDescription> mediaDescription = std::make_shared<AVMediaDescription>();
650     mediaDescription->SetMediaId(mediaInfo.mediaId);
651     mediaDescription->SetTitle(mediaInfo.mediaName);
652     mediaDescription->SetMediaUri(mediaInfo.mediaUrl);
653     mediaDescription->SetMediaType(mediaInfo.mediaType);
654     mediaDescription->SetMediaSize(mediaInfo.mediaSize);
655     mediaDescription->SetStartPosition(static_cast<uint32_t>(mediaInfo.startPosition));
656     mediaDescription->SetDuration(static_cast<uint32_t>(mediaInfo.duration));
657     mediaDescription->SetCreditsPosition(static_cast<int32_t>(mediaInfo.closingCreditsPosition));
658     mediaDescription->SetAlbumCoverUri(mediaInfo.albumCoverUrl);
659     mediaDescription->SetAlbumTitle(mediaInfo.albumTitle);
660     mediaDescription->SetArtist(mediaInfo.mediaArtist);
661     mediaDescription->SetLyricUri(mediaInfo.lrcUrl);
662     mediaDescription->SetIconUri(mediaInfo.appIconUrl);
663     mediaDescription->SetAppName(mediaInfo.appName);
664     AVQueueItem queueItem;
665     queueItem.SetDescription(mediaDescription);
666     std::lock_guard playerListLockGuard(streamPlayerListenerListLock_);
667     for (auto listener : streamPlayerListenerList_) {
668         if (listener != nullptr) {
669             SLOGI("trigger the OnPlayRequest for registered listeners");
670             listener->OnPlayRequest(queueItem);
671         }
672     }
673     std::lock_guard lockGuard(streamPlayerLock_);
674     SLOGI("Stream player received PlayRequest event done");
675 }
676 
OnImageChanged(std::shared_ptr<Media::PixelMap> pixelMap)677 void HwCastStreamPlayer::OnImageChanged(std::shared_ptr<Media::PixelMap> pixelMap)
678 {
679     SLOGD("Stream player received ImageChanged event");
680 }
681 
OnAlbumCoverChanged(std::shared_ptr<Media::PixelMap> pixelMap)682 void HwCastStreamPlayer::OnAlbumCoverChanged(std::shared_ptr<Media::PixelMap> pixelMap)
683 {
684     SLOGI("Received AlbumCoverChanged callback");
685     if (pixelMap == nullptr) {
686         SLOGE("Invalid pixelMap null");
687         return;
688     }
689     std::shared_ptr<AVSessionPixelMap> innerPixelMap =
690         AVSessionPixelMapAdapter::ConvertToInnerWithLimitedSize(pixelMap);
691     if (innerPixelMap == nullptr) {
692         SLOGE("Invalid innerPixelMap null");
693         return;
694     }
695 
696     std::shared_ptr<AVMediaDescription> mediaDescription = currentAVQueueItem_.GetDescription();
697     if (mediaDescription == nullptr) {
698         SLOGE("OnAlbumCoverChanged with nullptr mediaDescription, return with default");
699         return;
700     }
701     mediaDescription->SetIcon(innerPixelMap);
702     AVQueueItem queueItem;
703     queueItem.SetDescription(mediaDescription);
704     std::lock_guard playerListLockGuard(streamPlayerListenerListLock_);
705     for (auto listener : streamPlayerListenerList_) {
706         if (listener != nullptr) {
707             SLOGI("trigger the OnMediaItemChange for registered listeners on Album change");
708             listener->OnMediaItemChange(queueItem);
709         }
710     }
711 
712     std::lock_guard lockGuard(streamPlayerLock_);
713     currentAVQueueItem_ = queueItem;
714     SLOGI("Received AlbumCoverChanged callback done");
715 }
716 
OnAvailableCapabilityChanged(const CastEngine::StreamCapability & streamCapability)717 void HwCastStreamPlayer::OnAvailableCapabilityChanged(const CastEngine::StreamCapability &streamCapability)
718 {
719     SLOGE("Received OnAvailableCapabilityChanged callback");
720     std::vector<int32_t>& supportedCastCmds;
721     checkControlScopeChange(streamCapability, supportedCastCmds);
722     for (auto listener : streamPlayerListenerList_) {
723         if (listener != nullptr) {
724             SLOGI("trigger the OnValidCommandChange for registered listeners");
725             listener->OnValidCommandChange(supportedCastCmds);
726         }
727     }
728 }
729 
checkCmdsFromAbility(const CastEngine::StreamCapability & streamCapability,std::vector<int32_t> supportedCastCmds)730 checkCmdsFromAbility(const CastEngine::StreamCapability &streamCapability,
731     std::vector<int32_t> supportedCastCmds)
732 {
733     if (controlScope.isPlaySupported) {
734         supportedCastCmds.push_back(AVCastControlCommand::CAST_CONTROL_CMD_PLAY);
735     }
736     if (controlScope.isPauseSupported) {
737         supportedCastCmds.push_back(AVCastControlCommand::CAST_CONTROL_CMD_PAUSE);
738     }
739     if (controlScope.isStopSupported) {
740         supportedCastCmds.push_back(AVCastControlCommand::CAST_CONTROL_CMD_STOP);
741     }
742     if (controlScope.isNextSupported) {
743         supportedCastCmds.push_back(AVCastControlCommand::CAST_CONTROL_CMD_PLAY_NEXT);
744     }
745     if (controlScope.isPreviousSupported) {
746         supportedCastCmds.push_back(AVCastControlCommand::CAST_CONTROL_CMD_PLAY_PREVIOUS);
747     }
748     if (controlScope.isSeekSupported) {
749         supportedCastCmds.push_back(AVCastControlCommand::CAST_CONTROL_CMD_SEEK);
750     }
751     if (controlScope.isFastForwardSupported) {
752         supportedCastCmds.push_back(AVCastControlCommand::CAST_CONTROL_CMD_FAST_FORWARD);
753     }
754     if (controlScope.isFastRewindSupported) {
755         supportedCastCmds.push_back(AVCastControlCommand::CAST_CONTROL_CMD_REWIND);
756     }
757     if (controlScope.isLoopModeSupported) {
758         supportedCastCmds.push_back(AVCastControlCommand::CAST_CONTROL_CMD_SET_LOOP_MODE);
759     }
760 }
761 
checkAbilityFromCmds(std::vector<int32_t> supportedCastCmds,const CastEngine::StreamCapability & streamCapability)762 checkAbilityFromCmds(std::vector<int32_t> supportedCastCmds,
763     const CastEngine::StreamCapability &streamCapability)
764 {
765     for (const int32_t cmd : supportedCastCmds) {
766         switch (cmd) {
767             case AVCastControlCommand::CAST_CONTROL_CMD_PLAY:
768                 streamCapability.isPlaySupported = true;
769                 break;
770             case AVCastControlCommand::CAST_CONTROL_CMD_PAUSE:
771                 streamCapability.isPauseSupported = true;
772                 break;
773             case AVCastControlCommand::CAST_CONTROL_CMD_STOP:
774                 streamCapability.isStopSupported = true;
775                 break;
776             case AVCastControlCommand::CAST_CONTROL_CMD_PLAY_NEXT:
777                 streamCapability.isNextSupported = true;
778                 break;
779             case AVCastControlCommand::CAST_CONTROL_CMD_PLAY_PREVIOUS:
780                 streamCapability.isPreviousSupported = true;
781                 break;
782             case AVCastControlCommand::CAST_CONTROL_CMD_SEEK:
783                 streamCapability.isSeekSupported = true;
784                 break;
785             case AVCastControlCommand::CAST_CONTROL_CMD_FAST_FORWARD:
786                 streamCapability.isFastForwardSupported = true;
787                 break;
788             case AVCastControlCommand::CAST_CONTROL_CMD_REWIND:
789                 streamCapability.isFastRewindSupported = true;
790                 break;
791             case AVCastControlCommand::CAST_CONTROL_CMD_SET_LOOP_MODE:
792                 streamCapability.isLoopModeSupported = true;
793                 break;
794             default:
795                 break;
796         }
797     }
798 }
799 } // namespace OHOS::AVSession
800