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