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