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