1 /*
2 * Copyright (c) 2023-2025 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 "string_wrapper.h"
19 #include "avsession_log.h"
20 #include "avcast_player_state.h"
21 #include "avqueue_item.h"
22 #include "avmedia_description.h"
23 #include "avsession_errors.h"
24 #include "avsession_sysevent.h"
25 #include "avsession_event_handler.h"
26 #include "avsession_trace.h"
27 #include "avsession_radar.h"
28
29 using namespace OHOS::CastEngine;
30
31 namespace OHOS::AVSession {
~HwCastStreamPlayer()32 HwCastStreamPlayer::~HwCastStreamPlayer()
33 {
34 SLOGI("destruct the HwCastStreamPlayer without release");
35 }
36
Init()37 int32_t HwCastStreamPlayer::Init()
38 {
39 SLOGI("Init the HwCastStreamPlayer");
40 std::lock_guard lockGuard(streamPlayerLock_);
41 if (streamPlayer_) {
42 SLOGI("register self in streamPlayer");
43 return streamPlayer_->RegisterListener(shared_from_this());
44 }
45 return AVSESSION_ERROR;
46 }
47
Release()48 void HwCastStreamPlayer::Release()
49 {
50 SLOGI("Release the HwCastStreamPlayer");
51
52 std::lock_guard lockGuard(streamPlayerLock_);
53 if (streamPlayer_) {
54 streamPlayer_->UnregisterListener();
55 streamPlayer_->Release();
56 streamPlayer_ = nullptr;
57 }
58
59 std::lock_guard playerListLockGuard(streamPlayerListenerListLock_);
60 streamPlayerListenerList_.clear();
61 SLOGI("Release the HwCastStreamPlayer done");
62 }
63
CheckCastTime(int32_t castTime)64 int32_t HwCastStreamPlayer::CheckCastTime(int32_t castTime)
65 {
66 if (castTime < castMinTime) {
67 return castMinTime * castTime;
68 } else {
69 return castTime;
70 }
71 }
72
SendControlCommand(const AVCastControlCommand castControlCommand)73 void HwCastStreamPlayer::SendControlCommand(const AVCastControlCommand castControlCommand)
74 {
75 int32_t commandNum = castControlCommand.GetCommand();
76 SLOGI("send command to streamPlayer %{public}d", static_cast<int32_t>(commandNum));
77 std::lock_guard lockGuard(streamPlayerLock_);
78 if (!streamPlayer_) {
79 SLOGE("streamPlayer is nullptr");
80 HISYSEVENT_FAULT("REMOTE_CONTROL_FAILED",
81 "SESSION_TYPE", "cast",
82 "ERROR_TYPE", "INNER_ERROR",
83 "ERROR_INFO", "streamPlayer is nullptr");
84 AVSessionRadarInfo info("HwCastStreamPlayer::SendControlCommand");
85 info.errorCode_ = AVSessionRadar::GetRadarErrorCode(ERR_REMOTE_CONNECTION_NOT_EXIST);
86 AVSessionRadar::GetInstance().FailToSendControlCommand(info);
87 return;
88 }
89 switch (castControlCommand.GetCommand()) {
90 case AVCastControlCommand::CAST_CONTROL_CMD_PLAY:
91 streamPlayer_->Play();
92 break;
93 case AVCastControlCommand::CAST_CONTROL_CMD_PAUSE:
94 streamPlayer_->Pause();
95 break;
96 case AVCastControlCommand::CAST_CONTROL_CMD_STOP:
97 streamPlayer_->Stop();
98 break;
99 case AVCastControlCommand::CAST_CONTROL_CMD_PLAY_NEXT:
100 streamPlayer_->Next();
101 break;
102 case AVCastControlCommand::CAST_CONTROL_CMD_PLAY_PREVIOUS:
103 streamPlayer_->Previous();
104 break;
105 default:
106 SendControlCommandWithParams(castControlCommand);
107 break;
108 }
109 }
110
SendCustomData(const std::string & data)111 void HwCastStreamPlayer::SendCustomData(const std::string& data)
112 {
113 std::lock_guard lockGuard(streamPlayerLock_);
114 if (streamPlayer_ == nullptr) {
115 SLOGE("streamPlayer is nullptr");
116 return;
117 }
118 streamPlayer_->SendData(DataType::CUSTOM_DATA, data);
119 }
120
SetSpid(uint32_t spid)121 void HwCastStreamPlayer::SetSpid(uint32_t spid)
122 {
123 SLOGI("SetSpid spid %{public}u", spid);
124 spid_ = spid;
125 }
126
SendControlCommandWithParams(const AVCastControlCommand castControlCommand)127 void HwCastStreamPlayer::SendControlCommandWithParams(const AVCastControlCommand castControlCommand)
128 {
129 std::lock_guard lockGuard(streamPlayerLock_);
130 int32_t param = 0;
131 switch (castControlCommand.GetCommand()) {
132 case AVCastControlCommand::CAST_CONTROL_CMD_FAST_FORWARD:
133 castControlCommand.GetForwardTime(param);
134 streamPlayer_->FastForward(CheckCastTime(param));
135 break;
136 case AVCastControlCommand::CAST_CONTROL_CMD_REWIND:
137 castControlCommand.GetRewindTime(param);
138 streamPlayer_->FastRewind(CheckCastTime(param));
139 break;
140 case AVCastControlCommand::CAST_CONTROL_CMD_SEEK:
141 castControlCommand.GetSeekTime(param);
142 streamPlayer_->Seek(param);
143 break;
144 case AVCastControlCommand::CAST_CONTROL_CMD_SET_VOLUME:
145 castControlCommand.GetVolume(param);
146 streamPlayer_->SetVolume(param);
147 break;
148 case AVCastControlCommand::CAST_CONTROL_CMD_SET_SPEED:
149 castControlCommand.GetSpeed(param);
150 streamPlayer_->SetSpeed(static_cast<CastEngine::PlaybackSpeed>(param));
151 break;
152 case AVCastControlCommand::CAST_CONTROL_CMD_SET_LOOP_MODE:
153 castControlCommand.GetLoopMode(param);
154 SLOGD("SetLoopMode int: %{public}d", param);
155 CHECK_AND_RETURN_LOG(intLoopModeToCastPlus_.count(param) != 0, "invalid LoopMode");
156 streamPlayer_->SetLoopMode(intLoopModeToCastPlus_[param]);
157 break;
158 case AVCastControlCommand::CAST_CONTROL_CMD_TOGGLE_FAVORITE:
159 break;
160 case AVCastControlCommand::CAST_CONTROL_CMD_TOGGLE_MUTE:
161 bool enableMute;
162 streamPlayer_->GetMute(enableMute);
163 streamPlayer_->SetMute(!enableMute);
164 break;
165 default:
166 SLOGE("invalid command");
167 HISYSEVENT_FAULT("REMOTE_CONTROL_FAILED", "ERROR_TYPE", "INNER_ERROR", "ERROR_INFO", "invalid command");
168 break;
169 }
170 }
171
GetCurrentItem()172 AVQueueItem HwCastStreamPlayer::GetCurrentItem()
173 {
174 SLOGI("Received GetCurrentItem request");
175 // do not place streamPlayerLock_ in side of curItemLock_
176 std::lock_guard lockGuard(curItemLock_);
177 return currentAVQueueItem_;
178 }
179
RefreshCurrentItemDuration()180 AVQueueItem HwCastStreamPlayer::RefreshCurrentItemDuration()
181 {
182 SLOGD("RefreshCurrentItemDuration in");
183 int32_t duration = 0;
184 GetDuration(duration);
185 // do not place streamPlayerLock_ in side of curItemLock_
186 std::lock_guard lockGuard(curItemLock_);
187 std::shared_ptr<AVMediaDescription> mediaDes = currentAVQueueItem_.GetDescription();
188 CHECK_AND_RETURN_RET_LOG(mediaDes != nullptr, currentAVQueueItem_, "AVMediaDescription get null!");
189 mediaDes->SetDuration(duration);
190 currentAVQueueItem_.SetDescription(mediaDes);
191 SLOGI("refresh duration:%{public}d", duration);
192 return currentAVQueueItem_;
193 }
194
RefreshCurrentAVQueueItem(const AVQueueItem & avQueueItem)195 int32_t HwCastStreamPlayer::RefreshCurrentAVQueueItem(const AVQueueItem& avQueueItem)
196 {
197 std::lock_guard lockGuard(curItemLock_);
198 currentAVQueueItem_ = avQueueItem;
199 return AVSESSION_SUCCESS;
200 }
201
Start(const AVQueueItem & avQueueItem)202 int32_t HwCastStreamPlayer::Start(const AVQueueItem& avQueueItem)
203 {
204 std::shared_ptr<AVMediaDescription> mediaDescription = avQueueItem.GetDescription();
205 CastEngine::MediaInfo mediaInfo;
206 buildCastInfo(mediaDescription, mediaInfo);
207
208 std::lock_guard lockGuard(streamPlayerLock_);
209 if (!streamPlayer_) {
210 SLOGE("Set media info and start failed");
211 return AVSESSION_ERROR;
212 }
213 std::shared_ptr<AVMediaDescription> originMediaDescription = nullptr;
214 {
215 std::lock_guard lockGuard(curItemLock_);
216 originMediaDescription = currentAVQueueItem_.GetDescription();
217 }
218 if (originMediaDescription && originMediaDescription->GetMediaUri() != "http:" &&
219 originMediaDescription->GetMediaId() == mediaInfo.mediaId) {
220 CHECK_AND_RETURN_RET_LOG(streamPlayer_->Play() == AVSESSION_SUCCESS, AVSESSION_ERROR, "Set play failed");
221 } else if (streamPlayer_->Play(mediaInfo) != AVSESSION_SUCCESS) {
222 SLOGE("Set media info and start failed");
223 return AVSESSION_ERROR;
224 }
225 RefreshCurrentAVQueueItem(avQueueItem);
226 SLOGI("Set media info and start successfully");
227 return AVSESSION_SUCCESS;
228 }
229
RepeatPrepare(std::shared_ptr<AVMediaDescription> & mediaDescription)230 bool HwCastStreamPlayer::RepeatPrepare(std::shared_ptr<AVMediaDescription>& mediaDescription)
231 {
232 bool hasIcon = false;
233 {
234 std::lock_guard lockGuard(curItemLock_);
235 if (mediaDescription != nullptr && currentAVQueueItem_.GetDescription() != nullptr &&
236 mediaDescription->GetIconUri() == "URI_CACHE" && mediaDescription->GetIcon() != nullptr) {
237 currentAVQueueItem_.GetDescription()->SetIcon(mediaDescription->GetIcon());
238 SLOGI("Repeat Prepare only setIcon");
239 hasIcon = true;
240 }
241 }
242 if (hasIcon && sessionCallbackForCastNtf_ && isPlayingState_) {
243 sessionCallbackForCastNtf_(true, true);
244 }
245 return hasIcon;
246 }
247
Prepare(const AVQueueItem & avQueueItem)248 int32_t HwCastStreamPlayer::Prepare(const AVQueueItem& avQueueItem)
249 {
250 std::shared_ptr<AVMediaDescription> mediaDescription = avQueueItem.GetDescription();
251 if (RepeatPrepare(mediaDescription)) {
252 return AVSESSION_SUCCESS;
253 }
254 SLOGI("do Prepare with mediaId %{public}s | title %{public}s",
255 mediaDescription->GetMediaId().c_str(), mediaDescription->GetTitle().c_str());
256
257 CastEngine::MediaInfo mediaInfo;
258 buildCastInfo(mediaDescription, mediaInfo);
259
260 std::lock_guard lockGuard(streamPlayerLock_);
261 SLOGI("pass playerlock, check item lock, mediaInfo mediaUrl and albumCoverUrl");
262 if (streamPlayer_ && streamPlayer_->Load(mediaInfo) == AVSESSION_SUCCESS) {
263 std::lock_guard lockGuard(curItemLock_);
264 SLOGI("Set media info and prepare with curItemLock successed");
265 currentAVQueueItem_ = avQueueItem;
266 return AVSESSION_SUCCESS;
267 }
268 SLOGE("Set media info and prepare failed");
269 return AVSESSION_ERROR;
270 }
271
buildCastInfo(std::shared_ptr<AVMediaDescription> & mediaDescription,CastEngine::MediaInfo & mediaInfo)272 void HwCastStreamPlayer::buildCastInfo(std::shared_ptr<AVMediaDescription>& mediaDescription,
273 CastEngine::MediaInfo& mediaInfo)
274 {
275 mediaInfo.mediaId = mediaDescription->GetMediaId();
276 mediaInfo.mediaName = mediaDescription->GetTitle();
277 mediaInfo.mediaUrl = mediaDescription->GetMediaUri();
278 if (mediaDescription->GetMediaUri() == "") {
279 if (mediaDescription->GetFdSrc().fd_ == 0) {
280 SLOGW("No media id and fd src");
281 mediaInfo.mediaUrl = "http:";
282 mediaDescription->SetMediaUri("http:");
283 } else {
284 mediaInfo.mediaUrl = std::to_string(mediaDescription->GetFdSrc().fd_);
285 }
286 }
287 mediaInfo.mediaType = mediaDescription->GetMediaType();
288 mediaInfo.mediaSize = static_cast<uint32_t>(mediaDescription->GetMediaSize());
289 mediaInfo.startPosition = static_cast<uint32_t>(mediaDescription->GetStartPosition());
290 mediaInfo.duration = static_cast<uint32_t>(mediaDescription->GetDuration());
291 SLOGI("mediaDescription duration is %{public}d", mediaDescription->GetDuration());
292 mediaInfo.closingCreditsPosition = static_cast<uint32_t>(mediaDescription->GetCreditsPosition());
293 mediaInfo.albumCoverUrl = mediaDescription->GetIconUri() == "" ?
294 mediaDescription->GetAlbumCoverUri() : mediaDescription->GetIconUri();
295 mediaInfo.albumTitle = mediaDescription->GetAlbumTitle();
296 mediaInfo.mediaArtist = mediaDescription->GetArtist();
297 mediaInfo.lrcUrl = mediaDescription->GetLyricUri();
298 mediaInfo.lrcContent = mediaDescription->GetLyricContent();
299 mediaInfo.appIconUrl = mediaDescription->GetIconUri();
300 mediaInfo.appName = mediaDescription->GetAppName();
301 mediaInfo.drmType = mediaDescription->GetDrmScheme();
302 if (spid_ > 0 && mediaDescription->GetLaunchClientData().length() > 0) {
303 mediaInfo.launchClientData = mediaDescription->GetLaunchClientData();
304 mediaInfo.spid = spid_;
305 }
306 AVDataSrcDescriptor dataSrc = mediaDescription->GetDataSrc();
307 SLOGI("has dataSrc hasCallback %{public}d", dataSrc.hasCallback);
308 if (dataSrc.hasCallback) {
309 if (castDataSrc_ == nullptr) {
310 castDataSrc_ = std::make_shared<AVSession::HwCastDataSourceDescriptor>();
311 }
312 castDataSrc_->SetCallback(dataSrc.callback_);
313 castDataSrc_->SetSize(dataSrc.fileSize);
314 mediaInfo.mediaUrl = "/file";
315 mediaInfo.dataSrc = castDataSrc_;
316 }
317 if (mediaDescription->GetPcmSrc() && mediaDescription->GetCastInfo() != nullptr) {
318 uid_t appUid = mediaDescription->GetCastInfo()->GetAppUid();
319 SLOGI("buildCastInfo AUDIO_PCM uid %{public}d", appUid);
320 mediaInfo.appUid = appUid;
321 mediaInfo.mediaType = "AUDIO_PCM";
322 }
323 }
324
GetDuration(int32_t & duration)325 int32_t HwCastStreamPlayer::GetDuration(int32_t& duration)
326 {
327 SLOGI("GetDuration begin");
328 std::lock_guard lockGuard(streamPlayerLock_);
329 if (!streamPlayer_) {
330 SLOGE("streamPlayer is nullptr");
331 return AVSESSION_ERROR;
332 }
333 streamPlayer_->GetDuration(duration);
334 SLOGI("GetDuration successed");
335 return AVSESSION_SUCCESS;
336 }
337
GetCastAVPlaybackState(AVPlaybackState & avPlaybackState)338 int32_t HwCastStreamPlayer::GetCastAVPlaybackState(AVPlaybackState& avPlaybackState)
339 {
340 SLOGI("GetCastAVPlaybackState begin");
341 std::lock_guard lockGuard(streamPlayerLock_);
342 if (!streamPlayer_) {
343 SLOGE("streamPlayer is nullptr");
344 return AVSESSION_ERROR;
345 }
346 CastEngine::PlayerStates castPlayerStates;
347 streamPlayer_->GetPlayerStatus(castPlayerStates);
348 if (castPlusStateToString_.count(castPlayerStates) != 0) {
349 avPlaybackState.SetState(castPlusStateToString_[castPlayerStates]);
350 }
351 CastEngine::PlaybackSpeed castPlaybackSpeed;
352 streamPlayer_->GetPlaySpeed(castPlaybackSpeed);
353 if (castPlusSpeedToDouble_.count(castPlaybackSpeed) != 0) {
354 avPlaybackState.SetSpeed(castPlusSpeedToDouble_[castPlaybackSpeed]);
355 }
356 int castPosition = 0;
357 streamPlayer_->GetPosition(castPosition);
358 AVPlaybackState::Position position;
359 position.elapsedTime_ = static_cast<int64_t>(castPosition);
360 avPlaybackState.SetPosition(position);
361 CastEngine::LoopMode castLoopMode;
362 streamPlayer_->GetLoopMode(castLoopMode);
363 if (castPlusLoopModeToInt_.count(castLoopMode) != 0) {
364 avPlaybackState.SetLoopMode(castPlusLoopModeToInt_[castLoopMode]);
365 }
366 int32_t castVolume = 0;
367 int32_t maxCastVolume = 0;
368 streamPlayer_->GetVolume(castVolume, maxCastVolume);
369 avPlaybackState.SetVolume(castVolume);
370
371 std::shared_ptr<AAFwk::WantParams> wantParams = std::make_shared<AAFwk::WantParams>();
372 sptr<AAFwk::IInterface> intIt = AAFwk::Integer::Box(maxCastVolume);
373 if (wantParams == nullptr || intIt == nullptr) {
374 return AVSESSION_ERROR;
375 }
376 wantParams->SetParam("maxCastVolume", intIt);
377 avPlaybackState.SetExtras(wantParams);
378
379 SLOGI("GetCastAVPlaybackState successed with state: %{public}d", avPlaybackState.GetState());
380 return AVSESSION_SUCCESS;
381 }
382
GetMediaDecodeOfVideoFromCJSON(cJSON * videoValue)383 void HwCastStreamPlayer::GetMediaDecodeOfVideoFromCJSON(cJSON* videoValue)
384 {
385 CHECK_AND_RETURN_LOG(videoValue != nullptr && !cJSON_IsInvalid(videoValue), "videoValue get invalid");
386 if (cJSON_HasObjectItem(videoValue, decodeSupportResolutionStr_.c_str())) {
387 cJSON* resolutionItem = cJSON_GetObjectItem(videoValue, decodeSupportResolutionStr_.c_str());
388 CHECK_AND_RETURN_LOG(resolutionItem != nullptr && !cJSON_IsInvalid(resolutionItem), "resolution get invalid");
389 std::vector<ResolutionLevel> resolutionLevels;
390 std::map<std::string, std::vector<ResolutionLevel>> decodeToResolution;
391 int num;
392 if (cJSON_HasObjectItem(resolutionItem, decodeOfVideoHevcStr_.c_str())) {
393 cJSON* decodeOfVideoHevcArray = cJSON_GetObjectItem(resolutionItem, decodeOfVideoHevcStr_.c_str());
394 CHECK_AND_RETURN_LOG(decodeOfVideoHevcArray != nullptr && !cJSON_IsInvalid(decodeOfVideoHevcArray) &&
395 cJSON_IsArray(decodeOfVideoHevcArray), "decodeOfVideoHevc get invalid");
396 cJSON* valueItem = nullptr;
397 cJSON_ArrayForEach(valueItem, decodeOfVideoHevcArray) {
398 CHECK_AND_CONTINUE(valueItem != nullptr && !cJSON_IsInvalid(valueItem) && cJSON_IsNumber(valueItem));
399 num = valueItem->valueint;
400 SLOGI("decodeSupportResolution's video/hevc is %{public}d", num);
401 resolutionLevels.emplace_back(static_cast<ResolutionLevel>(num));
402 }
403 decodeToResolution[decodeOfVideoHevcStr_] = resolutionLevels;
404 }
405 resolutionLevels.clear();
406 if (cJSON_HasObjectItem(resolutionItem, decodeOfVideoAvcStr_.c_str())) {
407 cJSON* decodeOfVideoAvcArray = cJSON_GetObjectItem(resolutionItem, decodeOfVideoAvcStr_.c_str());
408 CHECK_AND_RETURN_LOG(decodeOfVideoAvcArray != nullptr && !cJSON_IsInvalid(decodeOfVideoAvcArray) &&
409 cJSON_IsArray(decodeOfVideoAvcArray), "decodeOfVideoAvc get invalid");
410 cJSON* valueItem = nullptr;
411 cJSON_ArrayForEach(valueItem, decodeOfVideoAvcArray) {
412 CHECK_AND_CONTINUE(valueItem != nullptr && !cJSON_IsInvalid(valueItem) && cJSON_IsNumber(valueItem));
413 num = valueItem->valueint;
414 SLOGI("decodeSupportResolution's video/avc is %{public}d", num);
415 resolutionLevels.emplace_back(static_cast<ResolutionLevel>(num));
416 }
417 jsonCapabilitiesSptr_->decoderSupportResolutions_ = decodeToResolution;
418 }
419 } else {
420 SLOGI("%{public}s of %{public}s no contains", videoStr_.c_str(), decodeSupportResolutionStr_.c_str());
421 }
422 }
423
GetMediaCapabilitiesOfVideo(cJSON * videoValue)424 void HwCastStreamPlayer::GetMediaCapabilitiesOfVideo(cJSON* videoValue)
425 {
426 CHECK_AND_RETURN_LOG(videoValue != nullptr && !cJSON_IsInvalid(videoValue), "videoValue get invalid");
427 if (cJSON_HasObjectItem(videoValue, decodeTypeStr_.c_str())) {
428 cJSON* decodeTypeArray = cJSON_GetObjectItem(videoValue, decodeTypeStr_.c_str());
429 CHECK_AND_RETURN_LOG(decodeTypeArray != nullptr && !cJSON_IsInvalid(decodeTypeArray) &&
430 cJSON_IsArray(decodeTypeArray), "decodeType get invalid");
431 cJSON* decodeTypeItem = nullptr;
432 cJSON_ArrayForEach(decodeTypeItem, decodeTypeArray) {
433 CHECK_AND_CONTINUE(decodeTypeItem != nullptr && !cJSON_IsInvalid(decodeTypeItem) &&
434 cJSON_IsString(decodeTypeItem));
435 std::string str(decodeTypeItem->valuestring);
436 SLOGI("get %{public}s is %{public}s", decodeTypeStr_.c_str(), str.c_str());
437 jsonCapabilitiesSptr_->decoderTypes_.emplace_back(str);
438 }
439 }
440
441 GetMediaDecodeOfVideoFromCJSON(videoValue);
442
443 if (cJSON_HasObjectItem(videoValue, hdrFormatStr_.c_str())) {
444 cJSON* hdrFormatArray = cJSON_GetObjectItem(videoValue, hdrFormatStr_.c_str());
445 CHECK_AND_RETURN_LOG(hdrFormatArray != nullptr && !cJSON_IsInvalid(hdrFormatArray) &&
446 cJSON_IsArray(hdrFormatArray), "hdrFormat get invalid");
447 cJSON* hdrFormatItem = nullptr;
448 cJSON_ArrayForEach(hdrFormatItem, hdrFormatArray) {
449 CHECK_AND_CONTINUE(hdrFormatItem != nullptr && !cJSON_IsInvalid(hdrFormatItem) &&
450 cJSON_IsNumber(hdrFormatItem));
451 int num = hdrFormatItem->valueint;
452 SLOGI("get %{public}s is %{public}d", hdrFormatStr_.c_str(), num);
453 jsonCapabilitiesSptr_->hdrFormats_.emplace_back(static_cast<HDRFormat>(num));
454 }
455 } else {
456 SLOGI("%{public}s of %{public}s no contains", videoStr_.c_str(), hdrFormatStr_.c_str());
457 }
458 }
459
GetMediaCapabilitiesOfAudio(cJSON * audioValue)460 void HwCastStreamPlayer::GetMediaCapabilitiesOfAudio(cJSON* audioValue)
461 {
462 CHECK_AND_RETURN_LOG(audioValue != nullptr && !cJSON_IsInvalid(audioValue), "audioValue get invalid");
463 if (cJSON_HasObjectItem(audioValue, decodeTypeStr_.c_str())) {
464 cJSON* decodeTypeItem = cJSON_GetObjectItem(audioValue, decodeTypeStr_.c_str());
465 CHECK_AND_RETURN_LOG(decodeTypeItem != nullptr && !cJSON_IsInvalid(decodeTypeItem) &&
466 cJSON_IsString(decodeTypeItem), "decodeType str get invalid");
467 std::string str(decodeTypeItem->valuestring);
468 SLOGI("%{public}s of %{public}s", decodeTypeStr_.c_str(), str.c_str());
469 jsonCapabilitiesSptr_->decoderTypes_.emplace_back(str);
470 }
471 }
472
ClearJsonCapabilities()473 void HwCastStreamPlayer::ClearJsonCapabilities()
474 {
475 SLOGI("enter ClearJsonCapabilities");
476 jsonCapabilitiesSptr_->decoderTypes_.clear();
477 jsonCapabilitiesSptr_->hdrFormats_.clear();
478 jsonCapabilitiesSptr_->playSpeeds_.clear();
479 jsonCapabilitiesSptr_->decoderSupportResolutions_.clear();
480 }
481
GetMediaCapabilities()482 int32_t HwCastStreamPlayer::GetMediaCapabilities()
483 {
484 SLOGI("GetMediaCapabilities begin");
485 std::lock_guard lockGuard(streamPlayerLock_);
486 CHECK_AND_RETURN_RET_LOG(streamPlayer_, AVSESSION_ERROR, "streamPlayer_ is nullptr");
487 CHECK_AND_RETURN_RET_LOG(jsonCapabilitiesSptr_, AVSESSION_ERROR, "jsonCapabilitiesSptr_ is nullptr");
488 std::string supportCapabilities;
489 ClearJsonCapabilities();
490 streamPlayer_->GetMediaCapabilities(supportCapabilities);
491
492 cJSON* valueItem = cJSON_Parse(supportCapabilities.c_str());
493 CHECK_AND_RETURN_RET_LOG(valueItem != nullptr, AVSESSION_ERROR, "supportCapabilities is nullptr");
494 if (cJSON_IsInvalid(valueItem)) {
495 SLOGE("supportCapabilities is invalid");
496 cJSON_Delete(valueItem);
497 return AVSESSION_ERROR;
498 }
499 if (cJSON_HasObjectItem(valueItem, videoStr_.c_str())) {
500 cJSON* videoItem = cJSON_GetObjectItem(valueItem, videoStr_.c_str());
501 GetMediaCapabilitiesOfVideo(videoItem);
502 } else if (cJSON_HasObjectItem(valueItem, audioStr_.c_str())) {
503 cJSON* audioItem = cJSON_GetObjectItem(valueItem, audioStr_.c_str());
504 GetMediaCapabilitiesOfAudio(audioItem);
505 }
506
507 if (cJSON_HasObjectItem(valueItem, speedStr_.c_str())) {
508 cJSON* speedArray = cJSON_GetObjectItem(valueItem, speedStr_.c_str());
509 if (speedArray == nullptr || cJSON_IsInvalid(speedArray) || !cJSON_IsArray(speedArray)) {
510 SLOGE("speed get null or invalid");
511 cJSON_Delete(valueItem);
512 return AVSESSION_ERROR;
513 }
514 cJSON* speedItem = nullptr;
515 cJSON_ArrayForEach(speedItem, speedArray) {
516 CHECK_AND_CONTINUE(speedItem != nullptr && !cJSON_IsInvalid(speedItem) &&
517 cJSON_IsNumber(speedItem));
518 CastEngine::PlaybackSpeed num = static_cast<CastEngine::PlaybackSpeed>(speedItem->valueint);
519 SLOGI("support play speed is %{public}f", castPlusSpeedToDouble_[num]);
520 jsonCapabilitiesSptr_->playSpeeds_.emplace_back(castPlusSpeedToDouble_[num]);
521 }
522 }
523 cJSON_Delete(valueItem);
524 SLOGI("GetMediaCapabilities success");
525 return AVSESSION_SUCCESS;
526 }
527
GetSupportedDecoders(std::vector<std::string> & decoderTypes)528 int32_t HwCastStreamPlayer::GetSupportedDecoders(std::vector<std::string>& decoderTypes)
529 {
530 SLOGI("enter GetSupportedDecoders");
531 std::lock_guard lockGuard(streamPlayerLock_);
532 CHECK_AND_RETURN_RET_LOG(jsonCapabilitiesSptr_, AVSESSION_ERROR, "jsonCapabilitiesSptr_ is nullptr");
533 if (jsonCapabilitiesSptr_->decoderTypes_.empty()) {
534 GetMediaCapabilities();
535 }
536 decoderTypes = jsonCapabilitiesSptr_->decoderTypes_;
537 return AVSESSION_SUCCESS;
538 }
539
GetRecommendedResolutionLevel(std::string & decoderType,ResolutionLevel & resolutionLevel)540 int32_t HwCastStreamPlayer::GetRecommendedResolutionLevel(std::string& decoderType, ResolutionLevel& resolutionLevel)
541 {
542 SLOGI("enter GetRecommendedResolutionLevel and decoderType is %{public}s", decoderType.c_str());
543 std::lock_guard lockGuard(streamPlayerLock_);
544 CHECK_AND_RETURN_RET_LOG(jsonCapabilitiesSptr_, AVSESSION_ERROR, "jsonCapabilitiesSptr_ is nullptr");
545 if (jsonCapabilitiesSptr_->decoderSupportResolutions_.empty()) {
546 GetMediaCapabilities();
547 }
548 auto it = jsonCapabilitiesSptr_->decoderSupportResolutions_.find(decoderType);
549 if (it != jsonCapabilitiesSptr_->decoderSupportResolutions_.end()) {
550 std::vector<ResolutionLevel> resolutionLevels = jsonCapabilitiesSptr_->decoderSupportResolutions_[decoderType];
551 auto maxResolutionLevel = *std::max_element(resolutionLevels.begin(), resolutionLevels.end());
552 SLOGI("find %{public}s map to %{public}d", decoderType.c_str(), maxResolutionLevel);
553 resolutionLevel = static_cast<ResolutionLevel>(maxResolutionLevel);
554 } else {
555 SLOGI("no find %{public}s map to resolutionLevel", decoderType.c_str());
556 return AVSESSION_ERROR;
557 }
558 return AVSESSION_SUCCESS;
559 }
560
GetSupportedHdrCapabilities(std::vector<HDRFormat> & hdrFormats)561 int32_t HwCastStreamPlayer::GetSupportedHdrCapabilities(std::vector<HDRFormat>& hdrFormats)
562 {
563 SLOGI("enter GetSupportedHdrCapabilities");
564 std::lock_guard lockGuard(streamPlayerLock_);
565 CHECK_AND_RETURN_RET_LOG(jsonCapabilitiesSptr_, AVSESSION_ERROR, "jsonCapabilitiesSptr_ is nullptr");
566 if (jsonCapabilitiesSptr_->hdrFormats_.empty()) {
567 GetMediaCapabilities();
568 }
569 hdrFormats = jsonCapabilitiesSptr_->hdrFormats_;
570 return AVSESSION_SUCCESS;
571 }
572
GetSupportedPlaySpeeds(std::vector<float> & playSpeeds)573 int32_t HwCastStreamPlayer::GetSupportedPlaySpeeds(std::vector<float>& playSpeeds)
574 {
575 SLOGI("enter GetSupportedPlaySpeeds");
576 std::lock_guard lockGuard(streamPlayerLock_);
577 CHECK_AND_RETURN_RET_LOG(jsonCapabilitiesSptr_, AVSESSION_ERROR, "jsonCapabilitiesSptr_ is nullptr");
578 if (jsonCapabilitiesSptr_->playSpeeds_.empty()) {
579 GetMediaCapabilities();
580 }
581 playSpeeds = jsonCapabilitiesSptr_->playSpeeds_;
582 return AVSESSION_SUCCESS;
583 }
584
SetDisplaySurface(std::string & surfaceId)585 int32_t HwCastStreamPlayer::SetDisplaySurface(std::string &surfaceId)
586 {
587 SLOGI("SetDisplaySurface begin");
588 std::lock_guard lockGuard(streamPlayerLock_);
589 if (!streamPlayer_) {
590 SLOGE("streamPlayer is nullptr");
591 return AVSESSION_ERROR;
592 }
593 streamPlayer_->SetSurface(surfaceId);
594 SLOGI("SetDisplaySurface successed");
595 return AVSESSION_SUCCESS;
596 }
597
ProcessMediaKeyResponse(const std::string & assetId,const std::vector<uint8_t> & response)598 int32_t HwCastStreamPlayer::ProcessMediaKeyResponse(const std::string& assetId, const std::vector<uint8_t>& response)
599 {
600 SLOGI("ProcessMediaKeyResponse begin");
601 std::lock_guard lockGuard(streamPlayerLock_);
602 if (!streamPlayer_) {
603 SLOGE("streamPlayer is nullptr");
604 return AVSESSION_ERROR;
605 }
606 streamPlayer_->ProvideKeyResponse(assetId, response);
607 SLOGI("ProcessMediaKeyResponse successed");
608 return AVSESSION_SUCCESS;
609 }
610
RegisterControllerListener(std::shared_ptr<IAVCastControllerProxyListener> listener)611 int32_t HwCastStreamPlayer::RegisterControllerListener(std::shared_ptr<IAVCastControllerProxyListener> listener)
612 {
613 SLOGI("RegisterControllerListener begin");
614 if (listener == nullptr) {
615 SLOGE("RegisterControllerListener failed for the listener is nullptr");
616 return AVSESSION_ERROR;
617 }
618
619 std::lock_guard playerListLockGuard(streamPlayerListenerListLock_);
620 if (find(streamPlayerListenerList_.begin(), streamPlayerListenerList_.end(), listener)
621 != streamPlayerListenerList_.end()) {
622 SLOGE("listener is already in streamPlayerListenerList_");
623 return AVSESSION_ERROR;
624 }
625 SLOGI("RegisterControllerListener successed, and add it to streamPlayerListenerList_");
626 streamPlayerListenerList_.emplace_back(listener);
627 SLOGI("RegisterControllerListener done with size %{public}d", static_cast<int>(streamPlayerListenerList_.size()));
628 return AVSESSION_SUCCESS;
629 }
630
UnRegisterControllerListener(std::shared_ptr<IAVCastControllerProxyListener> listener)631 int32_t HwCastStreamPlayer::UnRegisterControllerListener(std::shared_ptr<IAVCastControllerProxyListener> listener)
632 {
633 if (listener == nullptr) {
634 SLOGE("UnRegisterCastSessionStateListener failed for the listener is nullptr");
635 return AVSESSION_ERROR;
636 }
637
638 std::lock_guard playerListLockGuard(streamPlayerListenerListLock_);
639 for (auto iter = streamPlayerListenerList_.begin(); iter != streamPlayerListenerList_.end();) {
640 if (*iter == listener) {
641 streamPlayerListenerList_.erase(iter);
642 SLOGI("UnRegisterControllerListener successed, and erase it from streamPlayerListenerList_");
643 return AVSESSION_SUCCESS;
644 } else {
645 ++iter;
646 }
647 }
648 SLOGE("listener is not found in streamPlayerListenerList_, so UnRegisterControllerListener failed");
649
650 return AVSESSION_ERROR;
651 }
652
GetValidAbility(std::vector<int32_t> & validCmdList)653 int32_t HwCastStreamPlayer::GetValidAbility(std::vector<int32_t>& validCmdList)
654 {
655 SLOGI("GetValidAbility in");
656 if (streamPlayer_ == nullptr) {
657 SLOGE("streamPlayer is nullptr");
658 return AVSESSION_ERROR;
659 }
660 CastEngine::StreamCapability streamCapability;
661 streamPlayer_->GetAvailableCapability(streamCapability);
662 checkCmdsFromAbility(streamCapability, validCmdList);
663 return AVSESSION_SUCCESS;
664 }
665
SetValidAbility(const std::vector<int32_t> & validCmdList)666 int32_t HwCastStreamPlayer::SetValidAbility(const std::vector<int32_t>& validCmdList)
667 {
668 SLOGI("SetValidAbility begin");
669 if (streamPlayer_ == nullptr) {
670 SLOGE("streamPlayer is nullptr");
671 return AVSESSION_ERROR;
672 }
673 CastEngine::StreamCapability streamCapability;
674 checkAbilityFromCmds(validCmdList, streamCapability);
675 streamPlayer_->SetAvailableCapability(streamCapability);
676 return AVSESSION_SUCCESS;
677 }
678
SetSessionCallbackForCastCap(const std::function<void (bool,bool)> & callback)679 void HwCastStreamPlayer::SetSessionCallbackForCastCap(const std::function<void(bool, bool)>& callback)
680 {
681 sessionCallbackForCastNtf_ = callback;
682 }
683
CheckIfCancelCastCapsule()684 void HwCastStreamPlayer::CheckIfCancelCastCapsule()
685 {
686 isPlayingState_ = false;
687 AVSessionEventHandler::GetInstance().AVSessionRemoveTask("CancelCastCapsule");
688 AVSessionEventHandler::GetInstance().AVSessionPostTask(
689 [this]() {
690 if (sessionCallbackForCastNtf_ && !isPlayingState_) {
691 SLOGI("MediaCapsule delCastCapsule isPlayingState_ %{public}d", isPlayingState_);
692 sessionCallbackForCastNtf_(false, false);
693 }
694 }, "CancelCastCapsule", cancelTimeout);
695 }
696
OnStateChanged(const CastEngine::PlayerStates playbackState,bool isPlayWhenReady)697 void HwCastStreamPlayer::OnStateChanged(const CastEngine::PlayerStates playbackState, bool isPlayWhenReady)
698 {
699 AVPlaybackState avCastPlaybackState;
700 if (castPlusStateToString_.count(playbackState) == 0) {
701 SLOGE("current playbackState status is not exist in castPlusStateToString_");
702 avCastPlaybackState.SetState(AVPlaybackState::PLAYBACK_STATE_ERROR);
703 } else {
704 SLOGD("On state changed, get state %{public}d", castPlusStateToString_[playbackState]);
705 avCastPlaybackState.SetState(castPlusStateToString_[playbackState]);
706 }
707 if (avCastPlaybackState.GetState() == AVPlaybackState::PLAYBACK_STATE_PLAY) {
708 // play state try notify cast notification
709 isPlayingState_ = true;
710 if (sessionCallbackForCastNtf_) {
711 SLOGI("MediaCapsule addCastCapsule");
712 sessionCallbackForCastNtf_(true, false);
713 }
714 } else {
715 CheckIfCancelCastCapsule();
716 }
717 RefreshCurrentItemDuration();
718 std::lock_guard playerListLockGuard(streamPlayerListenerListLock_);
719 for (auto listener : streamPlayerListenerList_) {
720 if (listener != nullptr) {
721 SLOGI("trigger the OnCastPlaybackStateChange for registered listeners");
722 listener->OnCastPlaybackStateChange(avCastPlaybackState);
723 }
724 }
725 SLOGI("onCastStateChange with duration");
726 }
727
OnPositionChanged(int position,int bufferPosition,int duration)728 void HwCastStreamPlayer::OnPositionChanged(int position, int bufferPosition, int duration)
729 {
730 if (position == -1 && bufferPosition == -1 && duration == -1) { // -1 is invalid(default) value
731 SLOGW("Invalid position change callback");
732 return;
733 }
734 AVPlaybackState avCastPlaybackState;
735 if (position != -1) { // -1 is invalid position
736 AVPlaybackState::Position castPosition;
737 castPosition.elapsedTime_ = position;
738 avCastPlaybackState.SetPosition(castPosition);
739 SLOGD("Received elapsedTime: %{public}d", position);
740 }
741 if (bufferPosition != -1) { // -1 is invalid buffer position
742 avCastPlaybackState.SetBufferedTime(bufferPosition);
743 SLOGD("Received bufferPosition: %{public}d", bufferPosition);
744 }
745 if (duration != -1) {
746 std::shared_ptr<AAFwk::WantParams> wantParams = std::make_shared<AAFwk::WantParams>();
747 sptr<AAFwk::IInterface> intIt = AAFwk::Integer::Box(duration);
748 wantParams->SetParam("duration", intIt);
749 avCastPlaybackState.SetExtras(wantParams);
750 SLOGD("Received duration: %{public}d", duration);
751 }
752 std::lock_guard playerListLockGuard(streamPlayerListenerListLock_);
753 for (auto listener : streamPlayerListenerList_) {
754 if (listener != nullptr) {
755 SLOGD("trigger the OnPositionChange");
756 listener->OnCastPlaybackStateChange(avCastPlaybackState);
757 }
758 }
759 SLOGI("on cast position change done");
760 }
761
OnMediaItemChanged(const CastEngine::MediaInfo & mediaInfo)762 void HwCastStreamPlayer::OnMediaItemChanged(const CastEngine::MediaInfo& mediaInfo)
763 {
764 SLOGD("Stream player received mediaItemChanged event");
765 std::shared_ptr<AVMediaDescription> mediaDescription = std::make_shared<AVMediaDescription>();
766 mediaDescription->SetMediaId(mediaInfo.mediaId);
767 mediaDescription->SetTitle(mediaInfo.mediaName);
768 mediaDescription->SetMediaUri(mediaInfo.mediaUrl);
769 mediaDescription->SetMediaType(mediaInfo.mediaType);
770 mediaDescription->SetMediaSize(mediaInfo.mediaSize);
771 mediaDescription->SetStartPosition(static_cast<uint32_t>(mediaInfo.startPosition));
772 mediaDescription->SetDuration(static_cast<uint32_t>(mediaInfo.duration));
773 mediaDescription->SetCreditsPosition(static_cast<int32_t>(mediaInfo.closingCreditsPosition));
774 mediaDescription->SetAlbumCoverUri(mediaInfo.albumCoverUrl);
775 mediaDescription->SetAlbumTitle(mediaInfo.albumTitle);
776 mediaDescription->SetArtist(mediaInfo.mediaArtist);
777 mediaDescription->SetLyricUri(mediaInfo.lrcUrl);
778 mediaDescription->SetLyricContent(mediaInfo.lrcContent);
779 mediaDescription->SetIconUri(mediaInfo.appIconUrl);
780 mediaDescription->SetAppName(mediaInfo.appName);
781 mediaDescription->SetDrmScheme(mediaInfo.drmType);
782 std::shared_ptr<AVMediaDescription> originMediaDescription = nullptr;
783 {
784 std::lock_guard lockGuard(curItemLock_);
785 originMediaDescription = currentAVQueueItem_.GetDescription();
786 }
787 if (originMediaDescription != nullptr && originMediaDescription->GetIcon() != nullptr
788 && mediaInfo.mediaId == originMediaDescription->GetMediaId()) {
789 SLOGI("cacheMedia:%{public}s", originMediaDescription->GetMediaId().c_str());
790 mediaDescription->SetIcon(originMediaDescription->GetIcon());
791 }
792 AVQueueItem queueItem;
793 queueItem.SetDescription(mediaDescription);
794 {
795 std::lock_guard playerListLockGuard(streamPlayerListenerListLock_);
796 for (auto listener : streamPlayerListenerList_) {
797 if (listener != nullptr) {
798 SLOGI("trigger the OnMediaItemChange for registered listeners");
799 listener->OnMediaItemChange(queueItem);
800 }
801 }
802 }
803 {
804 std::lock_guard lockGuard(curItemLock_);
805 currentAVQueueItem_ = queueItem;
806 }
807
808 SLOGI("StreamPlayer received mediaItemChanged event done");
809 }
810
OnNextRequest()811 void HwCastStreamPlayer::OnNextRequest()
812 {
813 SLOGD("StreamPlayer received next request");
814 std::lock_guard playerListLockGuard(streamPlayerListenerListLock_);
815 for (auto listener : streamPlayerListenerList_) {
816 if (listener != nullptr) {
817 SLOGI("trigger the OnPlayNext for registered listeners");
818 listener->OnPlayNext();
819 }
820 }
821 SLOGI("StreamPlayer received next request done");
822 }
823
OnPreviousRequest()824 void HwCastStreamPlayer::OnPreviousRequest()
825 {
826 SLOGD("StreamPlayer received previous request");
827 std::lock_guard playerListLockGuard(streamPlayerListenerListLock_);
828 for (auto listener : streamPlayerListenerList_) {
829 if (listener != nullptr) {
830 SLOGI("trigger the OnPlayPrevious for registered listeners");
831 listener->OnPlayPrevious();
832 }
833 }
834 SLOGI("StreamPlayer received previous request done");
835 }
836
OnVolumeChanged(int volume,int maxVolume)837 void HwCastStreamPlayer::OnVolumeChanged(int volume, int maxVolume)
838 {
839 SLOGD("StreamPlayer received volume changed event: %{public}d", volume);
840 AVPlaybackState avCastPlaybackState;
841 avCastPlaybackState.SetVolume(volume);
842
843 std::shared_ptr<AAFwk::WantParams> wantParams = std::make_shared<AAFwk::WantParams>();
844 sptr<AAFwk::IInterface> intIt = AAFwk::Integer::Box(maxVolume);
845 if (wantParams == nullptr || intIt == nullptr) {
846 return;
847 }
848 wantParams->SetParam("maxCastVolume", intIt);
849 avCastPlaybackState.SetExtras(wantParams);
850
851 std::lock_guard playerListLockGuard(streamPlayerListenerListLock_);
852 for (auto listener : streamPlayerListenerList_) {
853 if (listener != nullptr) {
854 SLOGI("trigger the OnVolumeChanged for registered listeners");
855 listener->OnCastPlaybackStateChange(avCastPlaybackState);
856 }
857 }
858 SLOGI("StreamPlayer received volume changed event done: %{public}d", volume);
859 }
860
OnLoopModeChanged(const CastEngine::LoopMode loopMode)861 void HwCastStreamPlayer::OnLoopModeChanged(const CastEngine::LoopMode loopMode)
862 {
863 AVPlaybackState avCastPlaybackState;
864 if (castPlusLoopModeToInt_.count(loopMode) == 0) {
865 SLOGE("current playbackState status is not exist in castPlusStateToString_");
866 } else {
867 SLOGD("StreamPlayer received loop mode changed event: %{public}d", castPlusLoopModeToInt_[loopMode]);
868 avCastPlaybackState.SetLoopMode(castPlusLoopModeToInt_[loopMode]);
869 }
870 std::lock_guard playerListLockGuard(streamPlayerListenerListLock_);
871 for (auto listener : streamPlayerListenerList_) {
872 if (listener != nullptr) {
873 SLOGI("trigger the OnLoopModeChanged for registered listeners");
874 listener->OnCastPlaybackStateChange(avCastPlaybackState);
875 }
876 }
877 SLOGI("loop mode changed event done: %{public}d", castPlusLoopModeToInt_[loopMode]);
878 }
879
OnPlaySpeedChanged(const CastEngine::PlaybackSpeed speed)880 void HwCastStreamPlayer::OnPlaySpeedChanged(const CastEngine::PlaybackSpeed speed)
881 {
882 AVPlaybackState avCastPlaybackState;
883 if (castPlusSpeedToDouble_.count(speed) == 0) {
884 SLOGE("current speed is not exist in castPlusSpeedToDouble_");
885 return;
886 }
887 SLOGD("StreamPlayer received play speed changed event: %{public}f", castPlusSpeedToDouble_[speed]);
888 avCastPlaybackState.SetSpeed(castPlusSpeedToDouble_[speed]);
889 std::lock_guard playerListLockGuard(streamPlayerListenerListLock_);
890 for (auto listener : streamPlayerListenerList_) {
891 if (listener != nullptr) {
892 SLOGI("trigger the OnPlaySpeedChanged for registered listeners");
893 listener->OnCastPlaybackStateChange(avCastPlaybackState);
894 }
895 }
896 SLOGI("play speed changed event done: %{public}f", castPlusSpeedToDouble_[speed]);
897 }
898
OnPlayerError(int errorCode,const std::string & errorMsg)899 void HwCastStreamPlayer::OnPlayerError(int errorCode, const std::string &errorMsg)
900 {
901 SLOGD("StreamPlayer received error event, code: %{public}d, message: %{public}s", errorCode, errorMsg.c_str());
902 std::lock_guard playerListLockGuard(streamPlayerListenerListLock_);
903 for (auto listener : streamPlayerListenerList_) {
904 if (listener != nullptr) {
905 SLOGI("trigger the OnPlayerError for registered listeners");
906 listener->OnPlayerError(errorCode, errorMsg);
907 }
908 }
909 SLOGI("error event done, code: %{public}d, message: %{public}s", errorCode, errorMsg.c_str());
910 }
911
OnSeekDone(int32_t seekNumber)912 void HwCastStreamPlayer::OnSeekDone(int32_t seekNumber)
913 {
914 SLOGD("StreamPlayer received seek done event: %{public}d", seekNumber);
915 std::lock_guard playerListLockGuard(streamPlayerListenerListLock_);
916 for (auto listener : streamPlayerListenerList_) {
917 if (listener != nullptr) {
918 SLOGI("trigger the OnSeekDone for registered listeners");
919 listener->OnSeekDone(seekNumber);
920 }
921 }
922 SLOGI("StreamPlayer received seek done event done with: %{public}d", seekNumber);
923 }
924
OnVideoSizeChanged(int width,int height)925 void HwCastStreamPlayer::OnVideoSizeChanged(int width, int height)
926 {
927 SLOGD("StreamPlayer received video size change event, width: %{public}d, height: %{public}d", width, height);
928 std::lock_guard playerListLockGuard(streamPlayerListenerListLock_);
929 for (auto listener : streamPlayerListenerList_) {
930 if (listener != nullptr) {
931 SLOGI("trigger the OnVideoSizeChange for registered listeners");
932 listener->OnVideoSizeChange(width, height);
933 }
934 }
935 SLOGI("video size change event done, width: %{public}d, height: %{public}d", width, height);
936 }
937
OnEndOfStream(int isLooping)938 void HwCastStreamPlayer::OnEndOfStream(int isLooping)
939 {
940 SLOGD("Received EndOfStream callback, value is %{public}d", isLooping);
941 std::lock_guard playerListLockGuard(streamPlayerListenerListLock_);
942 for (auto listener : streamPlayerListenerList_) {
943 if (listener != nullptr) {
944 SLOGI("trigger the OnEndOfStream for registered listeners");
945 listener->OnEndOfStream(isLooping);
946 }
947 }
948
949 AVPlaybackState avCastPlaybackState;
950 std::shared_ptr<AAFwk::WantParams> wantParams = std::make_shared<AAFwk::WantParams>();
951 sptr<AAFwk::IInterface> intIt = AAFwk::Integer::Box(isLooping);
952 if (wantParams == nullptr || intIt == nullptr) {
953 return;
954 }
955 wantParams->SetParam("endofstream", intIt);
956 avCastPlaybackState.SetExtras(wantParams);
957 SLOGD("Received end of stream event: %{public}d", isLooping);
958
959 for (auto listener : streamPlayerListenerList_) {
960 if (listener != nullptr) {
961 SLOGI("trigger the OnEndOfStream for registered listeners");
962 listener->OnCastPlaybackStateChange(avCastPlaybackState);
963 }
964 }
965 SLOGI("Received EndOfStream callback done, value is %{public}d", isLooping);
966 }
967
OnPlayRequest(const CastEngine::MediaInfo & mediaInfo)968 void HwCastStreamPlayer::OnPlayRequest(const CastEngine::MediaInfo& mediaInfo)
969 {
970 SLOGI("Stream player received PlayRequest event");
971 std::shared_ptr<AVMediaDescription> mediaDescription = std::make_shared<AVMediaDescription>();
972 mediaDescription->SetMediaId(mediaInfo.mediaId);
973 mediaDescription->SetTitle(mediaInfo.mediaName);
974 mediaDescription->SetMediaUri(mediaInfo.mediaUrl);
975 mediaDescription->SetMediaType(mediaInfo.mediaType);
976 mediaDescription->SetMediaSize(mediaInfo.mediaSize);
977 mediaDescription->SetStartPosition(static_cast<uint32_t>(mediaInfo.startPosition));
978 mediaDescription->SetDuration(static_cast<uint32_t>(mediaInfo.duration));
979 mediaDescription->SetCreditsPosition(static_cast<int32_t>(mediaInfo.closingCreditsPosition));
980 mediaDescription->SetAlbumCoverUri(mediaInfo.albumCoverUrl);
981 mediaDescription->SetAlbumTitle(mediaInfo.albumTitle);
982 mediaDescription->SetArtist(mediaInfo.mediaArtist);
983 mediaDescription->SetLyricUri(mediaInfo.lrcUrl);
984 mediaDescription->SetLyricContent(mediaInfo.lrcContent);
985 mediaDescription->SetIconUri(mediaInfo.appIconUrl);
986 mediaDescription->SetAppName(mediaInfo.appName);
987 mediaDescription->SetDrmScheme(mediaInfo.drmType);
988 AVQueueItem queueItem;
989 queueItem.SetDescription(mediaDescription);
990 std::lock_guard playerListLockGuard(streamPlayerListenerListLock_);
991 for (auto listener : streamPlayerListenerList_) {
992 if (listener != nullptr) {
993 SLOGI("trigger the OnPlayRequest for registered listeners");
994 listener->OnPlayRequest(queueItem);
995 }
996 }
997 SLOGI("Stream player received PlayRequest event done");
998 }
999
OnImageChanged(std::shared_ptr<Media::PixelMap> pixelMap)1000 void HwCastStreamPlayer::OnImageChanged(std::shared_ptr<Media::PixelMap> pixelMap)
1001 {
1002 SLOGD("Stream player received ImageChanged event");
1003 }
1004
OnData(const CastEngine::DataType dataType,const std::string & dataStr)1005 void HwCastStreamPlayer::OnData(const CastEngine::DataType dataType, const std::string& dataStr)
1006 {
1007 std::lock_guard playerListLockGuard(streamPlayerListenerListLock_);
1008 for (auto listener : streamPlayerListenerList_) {
1009 if (listener != nullptr) {
1010 AAFwk::WantParams data;
1011 data.SetParam("customData", AAFwk::String::Box(dataStr));
1012 listener->OnCustomData(data);
1013 }
1014 }
1015 }
1016
OnAlbumCoverChanged(std::shared_ptr<Media::PixelMap> pixelMap)1017 void HwCastStreamPlayer::OnAlbumCoverChanged(std::shared_ptr<Media::PixelMap> pixelMap)
1018 {
1019 SLOGI("Received AlbumCoverChanged callback");
1020 if (pixelMap == nullptr) {
1021 SLOGE("Invalid pixelMap null");
1022 return;
1023 }
1024 std::shared_ptr<AVSessionPixelMap> innerPixelMap =
1025 AVSessionPixelMapAdapter::ConvertToInnerWithLimitedSize(pixelMap);
1026 if (innerPixelMap == nullptr) {
1027 SLOGE("Invalid innerPixelMap null");
1028 return;
1029 }
1030
1031 std::shared_ptr<AVMediaDescription> mediaDescription = nullptr;
1032 {
1033 std::lock_guard lockGuard(curItemLock_);
1034 mediaDescription = currentAVQueueItem_.GetDescription();
1035 }
1036 if (mediaDescription == nullptr) {
1037 SLOGE("OnAlbumCoverChanged with nullptr mediaDescription, return with default");
1038 return;
1039 }
1040 mediaDescription->SetIcon(innerPixelMap);
1041 AVQueueItem queueItem;
1042 queueItem.SetDescription(mediaDescription);
1043 {
1044 std::lock_guard playerListLockGuard(streamPlayerListenerListLock_);
1045 for (auto listener : streamPlayerListenerList_) {
1046 if (listener != nullptr) {
1047 SLOGI("trigger the OnMediaItemChange for registered listeners on Album change");
1048 listener->OnMediaItemChange(queueItem);
1049 }
1050 }
1051 }
1052 {
1053 std::lock_guard lockGuard(curItemLock_);
1054 currentAVQueueItem_ = queueItem;
1055 }
1056 SLOGI("Received AlbumCoverChanged callback done");
1057 }
1058
OnAvailableCapabilityChanged(const CastEngine::StreamCapability & streamCapability)1059 void HwCastStreamPlayer::OnAvailableCapabilityChanged(const CastEngine::StreamCapability &streamCapability)
1060 {
1061 SLOGE("Received OnAvailableCapabilityChanged callback");
1062 std::vector<int32_t> supportedCastCmds;
1063 checkCmdsFromAbility(streamCapability, supportedCastCmds);
1064 for (auto listener : streamPlayerListenerList_) {
1065 if (listener != nullptr) {
1066 SLOGI("trigger the OnValidCommandChange for registered listeners");
1067 listener->OnValidCommandChange(supportedCastCmds);
1068 }
1069 }
1070 }
1071
OnKeyRequest(const std::string & assetId,const std::vector<uint8_t> & keyRequestData)1072 void HwCastStreamPlayer::OnKeyRequest(const std::string& assetId, const std::vector<uint8_t>& keyRequestData)
1073 {
1074 SLOGD("Stream player received keyRequest event");
1075 std::lock_guard playerListLockGuard(streamPlayerListenerListLock_);
1076 for (auto listener : streamPlayerListenerList_) {
1077 if (listener != nullptr) {
1078 SLOGI("trigger the OnKeyRequest for registered listeners");
1079 listener->OnKeyRequest(assetId, keyRequestData);
1080 }
1081 }
1082 SLOGI("Stream player received keyRequest event done");
1083 }
1084
checkCmdsFromAbility(const CastEngine::StreamCapability & streamCapability,std::vector<int32_t> & supportedCastCmds)1085 void HwCastStreamPlayer::checkCmdsFromAbility(
1086 const CastEngine::StreamCapability& streamCapability, std::vector<int32_t>& supportedCastCmds)
1087 {
1088 if (streamCapability.isPlaySupported) {
1089 supportedCastCmds.push_back(AVCastControlCommand::CAST_CONTROL_CMD_PLAY);
1090 }
1091 if (streamCapability.isPauseSupported) {
1092 supportedCastCmds.push_back(AVCastControlCommand::CAST_CONTROL_CMD_PAUSE);
1093 }
1094 if (streamCapability.isStopSupported) {
1095 supportedCastCmds.push_back(AVCastControlCommand::CAST_CONTROL_CMD_STOP);
1096 }
1097 if (streamCapability.isNextSupported) {
1098 supportedCastCmds.push_back(AVCastControlCommand::CAST_CONTROL_CMD_PLAY_NEXT);
1099 }
1100 if (streamCapability.isPreviousSupported) {
1101 supportedCastCmds.push_back(AVCastControlCommand::CAST_CONTROL_CMD_PLAY_PREVIOUS);
1102 }
1103 if (streamCapability.isSeekSupported) {
1104 supportedCastCmds.push_back(AVCastControlCommand::CAST_CONTROL_CMD_SEEK);
1105 }
1106 if (streamCapability.isFastForwardSupported) {
1107 supportedCastCmds.push_back(AVCastControlCommand::CAST_CONTROL_CMD_FAST_FORWARD);
1108 }
1109 if (streamCapability.isFastRewindSupported) {
1110 supportedCastCmds.push_back(AVCastControlCommand::CAST_CONTROL_CMD_REWIND);
1111 }
1112 if (streamCapability.isLoopModeSupported) {
1113 supportedCastCmds.push_back(AVCastControlCommand::CAST_CONTROL_CMD_SET_LOOP_MODE);
1114 }
1115 if (streamCapability.isSetVolumeSupported) {
1116 supportedCastCmds.push_back(AVCastControlCommand::CAST_CONTROL_CMD_SET_VOLUME);
1117 }
1118 }
1119
checkAbilityFromCmds(const std::vector<int32_t> & supportedCastCmds,CastEngine::StreamCapability & streamCapability)1120 void HwCastStreamPlayer::checkAbilityFromCmds(
1121 const std::vector<int32_t>& supportedCastCmds, CastEngine::StreamCapability& streamCapability)
1122 {
1123 for (const int32_t cmd : supportedCastCmds) {
1124 switch (cmd) {
1125 case AVCastControlCommand::CAST_CONTROL_CMD_PLAY:
1126 streamCapability.isPlaySupported = true;
1127 break;
1128 case AVCastControlCommand::CAST_CONTROL_CMD_PAUSE:
1129 streamCapability.isPauseSupported = true;
1130 break;
1131 case AVCastControlCommand::CAST_CONTROL_CMD_STOP:
1132 streamCapability.isStopSupported = true;
1133 break;
1134 case AVCastControlCommand::CAST_CONTROL_CMD_PLAY_NEXT:
1135 streamCapability.isNextSupported = true;
1136 break;
1137 case AVCastControlCommand::CAST_CONTROL_CMD_PLAY_PREVIOUS:
1138 streamCapability.isPreviousSupported = true;
1139 break;
1140 case AVCastControlCommand::CAST_CONTROL_CMD_SEEK:
1141 streamCapability.isSeekSupported = true;
1142 break;
1143 case AVCastControlCommand::CAST_CONTROL_CMD_FAST_FORWARD:
1144 streamCapability.isFastForwardSupported = true;
1145 break;
1146 case AVCastControlCommand::CAST_CONTROL_CMD_REWIND:
1147 streamCapability.isFastRewindSupported = true;
1148 break;
1149 case AVCastControlCommand::CAST_CONTROL_CMD_SET_LOOP_MODE:
1150 streamCapability.isLoopModeSupported = true;
1151 break;
1152 case AVCastControlCommand::CAST_CONTROL_CMD_SET_VOLUME:
1153 streamCapability.isSetVolumeSupported = true;
1154 break;
1155 default:
1156 break;
1157 }
1158 }
1159 streamCapability.isToggleFavoriteSupported = false;
1160 }
1161 } // namespace OHOS::AVSession
1162