1 // Copyright 2023 Samsung Electronics Inc. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "media/filters/media_player_bridge_capi_tv.h"
7 #include "base/strings/string_split.h"
8 #include "media/base/efl/media_player_util_efl.h"
9 #include "tizen_src/chromium_impl/media/filters/media_player_tizen_client.h"
12 const int kSeekableTimeUpdateInterval = 500;
15 static void PlayerPreLoadingCb(void* data) {
17 LOG(ERROR) << "input data is null";
20 media::MediaPlayerBridgeCapiTV* player =
21 static_cast<media::MediaPlayerBridgeCapiTV*>(data);
22 player->OnPlayerPreloading();
25 static void DrmErrorCb(int err_code, char* err_str, void* data) {
27 media::MediaPlayerBridgeCapiTV* player =
28 static_cast<media::MediaPlayerBridgeCapiTV*>(data);
30 player->OnDrmError(err_code, err_str);
35 MediaPlayerBridgeCapiTV::MediaPlayerBridgeCapiTV(const GURL& url,
36 const std::string& user_agent,
38 : MediaPlayerBridgeCapi(url, user_agent, volume), weak_factory_(this) {
39 LOG(INFO) << "(" << static_cast<void*>(this) << ") " << __func__;
42 MediaPlayerBridgeCapiTV::~MediaPlayerBridgeCapiTV() {
43 LOG(INFO) << "(" << static_cast<void*>(this) << ") " << __func__;
44 weak_factory_.InvalidateWeakPtrs();
47 void MediaPlayerBridgeCapiTV::SetContentMimeType(const std::string& mime_type) {
48 LOG(INFO) << "(" << static_cast<void*>(this) << ") " << __func__
49 << " mime_type : " << mime_type;
50 mime_type_ = mime_type;
53 void MediaPlayerBridgeCapiTV::Prepare() {
54 bool media_resource_acquired = false;
55 std::string translated_url;
58 if (GetMediaPlayerClient())
59 GetMediaPlayerClient()->NotifyPlaybackState(
60 kPlaybackLoad, player_id_, url_.spec(), mime_type_,
61 &media_resource_acquired, &translated_url, &drm_info);
63 LOG(INFO) << "media_resource_acquired: " << media_resource_acquired
64 << ",translated_url:" << translated_url
65 << ",drm_info: " << drm_info;
67 if (mime_type_.find("application/vnd.oipf.contentaccessstreaming+xml") !=
69 LOG(INFO) << "CASD url,waiting for hbbtv parse the real url and set "
74 if (GetMediaPlayerClient() &&
75 GetMediaPlayerClient()->PlaybackNotificationEnabled() &&
77 if (!SetDrmInfo(drm_info)) {
78 LOG(ERROR) << "SetDrmInfo failed";
81 if (!translated_url.empty())
82 url_ = media::GetCleanURL(translated_url);
85 if (url_.spec().find(".mpd") != std::string::npos)
86 stream_type_ = DASH_STREAM;
87 else if (url_.spec().find(".m3u") != std::string::npos)
88 stream_type_ = HLS_STREAM;
89 else if (mime_type_.find("application/dash+xml") != std::string::npos) {
90 char steaming_type_dash[] = "DASH";
91 char steaming_type_dash_ex[] = "DASHEX";
92 player_set_streaming_type(
93 player_, blink::IsHbbTV() ? steaming_type_dash_ex : steaming_type_dash);
94 stream_type_ = DASH_STREAM;
97 if (blink::IsHbbTV() && CheckHighBitRate() && stream_type_ == DASH_STREAM)
98 AppendUrlHighBitRate(url_.spec());
99 player_prepared_ = false;
100 MediaPlayerBridgeCapi::Prepare();
101 if (GetMediaPlayerClient())
102 GetMediaPlayerClient()->NotifyPlaybackState(kPlaybackReady, player_id_);
105 void MediaPlayerBridgeCapiTV::Release() {
106 is_preloaded_ = false;
107 player_prepared_ = false;
108 StopSeekableTimeUpdateTimer();
109 MediaPlayerBridgeCapi::Release();
110 if (GetMediaPlayerClient())
111 GetMediaPlayerClient()->NotifyPlaybackState(kPlaybackStop, player_id_);
114 bool MediaPlayerBridgeCapiTV::Play() {
115 LOG(INFO) << "(" << static_cast<void*>(this) << ") " << __func__
116 << ",current_time:" << GetCurrentTime().InSecondsF();
118 // HBBTV run in preloading and preplay mode. If no prepread do prepare
119 // in PlayerPrePlay(), otherwise do normal Play.
120 if (blink::IsHbbTV() && !player_prepared_) {
121 if (!PlayerPrePlay())
122 LOG(ERROR) << "HBBTV prePlay fail.";
125 if (!MediaPlayerBridgeCapi::Play())
127 if (GetMediaPlayerClient())
128 GetMediaPlayerClient()->NotifyPlaybackState(kPlaybackStart, player_id_);
132 void MediaPlayerBridgeCapiTV::Pause(bool is_media_related_action) {
133 if (!player_prepared_) {
134 LOG(INFO) << "(" << static_cast<void*>(this)
135 << "), pause while player is not prepared, pause delay";
136 delayed_player_state_ = PLAYER_STATE_DELAYED_PAUSE;
139 MediaPlayerBridgeCapi::Pause(is_media_related_action);
142 void MediaPlayerBridgeCapiTV::PlaybackCompleteUpdate() {
143 MediaPlayerBridgeCapi::PlaybackCompleteUpdate();
144 if (GetMediaPlayerClient())
145 GetMediaPlayerClient()->NotifyPlaybackState(kPlaybackFinish, player_id_);
148 void MediaPlayerBridgeCapiTV::PlayerPrepared() {
149 player_prepared_ = true;
150 MediaPlayerBridgeCapi::PlayerPrepared();
153 int retVal = player_seek_available(player_, &canSeek);
154 if (retVal == PLAYER_ERROR_NONE && !canSeek)
155 is_player_seek_available_ = false;
157 GetAdaptiveStreamingInfo();
158 if (!blink::IsHbbTV() && is_live_stream_) {
159 UpdateSeekableTime();
160 StartSeekableTimeUpdateTimer();
164 void MediaPlayerBridgeCapiTV::StartSeekableTimeUpdateTimer() {
165 if (!seekable_time_update_timer_.IsRunning()) {
166 seekable_time_update_timer_.Start(
167 FROM_HERE, base::Milliseconds(kSeekableTimeUpdateInterval), this,
168 &MediaPlayerBridgeCapiTV::OnSeekableTimeUpdateTimerFired);
172 void MediaPlayerBridgeCapiTV::StopSeekableTimeUpdateTimer() {
173 if (seekable_time_update_timer_.IsRunning())
174 seekable_time_update_timer_.Stop();
177 void MediaPlayerBridgeCapiTV::OnSeekableTimeUpdateTimerFired() {
178 UpdateSeekableTime();
181 void MediaPlayerBridgeCapiTV::GetAdaptiveStreamingInfo() {
182 int ret = player_get_adaptive_streaming_info(player_, &is_live_stream_,
183 PLAYER_ADAPTIVE_INFO_IS_LIVE);
184 if (ret != PLAYER_ERROR_NONE || is_live_stream_) {
185 LOG(INFO) << "(" << static_cast<void*>(this) << ") " << __func__
186 << " A live stream.";
190 void MediaPlayerBridgeCapiTV::UpdateSeekableTime() {
191 if (GetPlayerState() == PLAYER_STATE_NONE || !is_live_stream_)
194 int64_t min = 0, max = 0;
195 if (GetLiveStreamingDuration(&min, &max)) {
196 if (min_seekable_time_.InMilliseconds() != min ||
197 max_seekable_time_.InMilliseconds() != max) {
198 min_seekable_time_ = base::Milliseconds(min);
199 max_seekable_time_ = base::Milliseconds(max);
200 if (GetMediaPlayerClient())
201 GetMediaPlayerClient()->OnSeekableTimeChange(
202 min_seekable_time_, max_seekable_time_, is_live_stream_);
208 GetAdaptiveStreamingInfo();
209 if (!is_live_stream_) {
210 LOG(INFO) << "(" << static_cast<void*>(this) << ") " << __func__
211 << " changed to static stream";
212 if (GetMediaPlayerClient())
213 GetMediaPlayerClient()->OnSeekableTimeChange({}, {}, is_live_stream_);
214 MediaPlayerBridgeCapi::UpdateDuration();
218 bool MediaPlayerBridgeCapiTV::GetLiveStreamingDuration(int64_t* min,
222 constexpr const size_t kDurationBufferSize = 64;
223 char live_duration[kDurationBufferSize] = {
226 char* live_ptr = live_duration;
227 const auto err = player_get_adaptive_streaming_info(
228 player_, live_duration, PLAYER_ADAPTIVE_INFO_LIVE_DURATION);
229 if (err != PLAYER_ERROR_NONE) {
230 LOG(ERROR) << "(" << static_cast<void*>(this) << ") " << __func__
231 << " |player_get_adaptive_streaming_info| failed " << err;
235 const std::string duration(live_duration);
236 const std::string::size_type delimiter = duration.find('|');
237 if (delimiter == std::string::npos) {
238 LOG(ERROR) << "(" << static_cast<void*>(this) << ") " << __func__
239 << " Failed to find delimiter | in duration: " << duration;
243 const std::string duration_min = duration.substr(0, delimiter);
244 if (duration_min.empty()) {
245 LOG(ERROR) << "(" << static_cast<void*>(this) << ") " << __func__
246 << " Failed empty min sub str: " << duration << ", "
252 if (!base::StringToInt64(duration_min, &out_min)) {
253 LOG(ERROR) << "(" << static_cast<void*>(this) << ") " << __func__
254 << " Failed to get min from duration: " << duration;
258 const std::string duration_max = duration.substr(delimiter + 1);
259 if (duration_max.empty()) {
260 LOG(ERROR) << "(" << static_cast<void*>(this) << ") " << __func__
261 << " empty max sub str: " << duration << ", " << delimiter;
266 if (!base::StringToInt64(duration_max, &out_max)) {
267 LOG(ERROR) << "(" << static_cast<void*>(this) << ") " << __func__
268 << " Failed to get max from duration: " << duration;
277 void MediaPlayerBridgeCapiTV::AppendUrlHighBitRate(const std::string& url) {
278 // don't use url.append("|STARTBITRATE=HIGHEST") to get hbbtv url
279 // "|" will be replaced with "%7C"
280 const char high_bitrate[] = "|STARTBITRATE=HIGHEST";
281 int len = url.length() + strlen(high_bitrate) + 1;
282 char* str_url = new char[len];
287 memset(str_url, 0, len);
288 strncat(str_url, url.c_str(), url.length());
289 strncat(str_url, high_bitrate, strlen(high_bitrate));
290 hbbtv_url_ = str_url;
292 LOG(INFO) << "hbbtv url:" << hbbtv_url_.c_str();
295 bool MediaPlayerBridgeCapiTV::CheckHighBitRate() {
296 if (!GetMediaPlayerClient()) {
297 LOG(ERROR) << "MediaPlayerClient is null";
300 content::WebContentsDelegate* web_contents_delegate =
301 GetMediaPlayerClient()->GetWebContentsDelegate();
302 if (!web_contents_delegate) {
303 LOG(ERROR) << "get web_contents_delegate fail";
306 bool ret = web_contents_delegate->IsHighBitRate();
307 LOG(INFO) << "get high bit rate: " << std::boolalpha << ret;
311 void MediaPlayerBridgeCapiTV::HandleDownloadableFontInfo(
312 const std::string& scheme_id_uri,
313 const std::string& value,
314 const std::string& data,
316 content::WebContentsDelegate* web_contents_delegate =
317 GetMediaPlayerClient()->GetWebContentsDelegate();
318 if (!web_contents_delegate)
320 web_contents_delegate->NotifyDownloadableFontInfo(scheme_id_uri, value, data,
324 bool MediaPlayerBridgeCapiTV::PreloadIfNeeded(int& ret) {
325 LOG(INFO) << "PreloadIfNeeded, is_preloaded_ " << is_preloaded_;
326 if (blink::IsHbbTV() && !is_preloaded_) {
327 ret = player_preloading_async(player_, -1, PlayerPreLoadingCb, this);
331 // fixup : kPlaybackReady status need checked by HBBTVResourceAcquired() patch
332 if (GetMediaPlayerClient())
333 GetMediaPlayerClient()->NotifyPlaybackState(kPlaybackReady, player_id_);
335 LOG(ERROR) << "(" << static_cast<void*>(this)
336 << "), GetMediaPlayerClient return null";
337 SetDisplayAtPausedState();
341 void MediaPlayerBridgeCapiTV::SetDisplayAtPausedState() {
342 int ret = PLAYER_ERROR_NONE;
343 if (!pending_seek_duration_.is_zero())
344 ret = player_display_video_at_paused_state(player_, false);
346 ret = player_display_video_at_paused_state(player_, true);
348 if (ret != PLAYER_ERROR_NONE)
349 LOG(ERROR) << "player_display_video_at_paused_state() failed";
352 void MediaPlayerBridgeCapiTV::UpdateDuration() {
353 LOG(INFO) << "(" << static_cast<void*>(this) << ") " << __func__;
354 if (blink::IsHbbTV() && !is_preloaded_) {
355 LOG(INFO) << "HBBTV preload not finished, no need update duration. ";
358 MediaPlayerBridgeCapi::UpdateDuration();
361 void MediaPlayerBridgeCapiTV::UpdateMediaType() {
362 LOG(INFO) << "(" << static_cast<void*>(this) << ") " << __func__;
364 if (blink::IsHbbTV() && !is_preloaded_) {
365 LOG(INFO) << "HBBTV preload not finished, no need update media type. ";
368 MediaPlayerBridgeCapi::UpdateMediaType();
371 bool MediaPlayerBridgeCapiTV::CheckLiveStreaming() const {
373 int retVal = player_get_adaptive_streaming_info(player_, &isLive,
374 PLAYER_ADAPTIVE_INFO_IS_LIVE);
375 if (retVal != PLAYER_ERROR_NONE) {
376 LOG(ERROR) << "player_get_adaptive_streaming_info failed, error: "
384 LOG(INFO) << "This is a live streaming.";
388 void MediaPlayerBridgeCapiTV::OnPlayerPreloading() {
389 task_runner_->PostTask(
390 FROM_HERE, base::BindOnce(&MediaPlayerBridgeCapiTV::PlayerPreloaded,
391 weak_factory_.GetWeakPtr()));
394 bool MediaPlayerBridgeCapiTV::HBBTVResourceAcquired() {
395 bool media_resource_acquired = false;
396 if (!GetMediaPlayerClient()) {
397 LOG(ERROR) << "MediaPlayerTizenClient is nullptr";
401 GetMediaPlayerClient()->NotifyPlaybackState(
402 kPlaybackReady, player_id_, url_.spec(), mime_type_,
403 &media_resource_acquired, NULL, NULL);
404 if (!media_resource_acquired) {
405 GetMediaPlayerClient()->NotifyPlaybackState(kPlaybackStop, player_id_);
406 LOG(ERROR) << "HBBTV media resource acquired failed";
408 return media_resource_acquired;
411 void MediaPlayerBridgeCapiTV::PlayerPreloaded() {
412 LOG(INFO) << "PlayerPreloaded,this: " << this
413 << ",player_prepared_:" << player_prepared_;
415 is_preloaded_ = true;
416 is_live_stream_ = CheckLiveStreaming();
417 // if (stream_type_ == DASH_STREAM)
419 if (is_live_stream_ && stream_type_ != OTHER_STREAM) {
420 // UpdateSeekableTime();
421 // StartSeekableTimeUpdateTimer();
423 // UpdateAudioTrackInfo();
424 // UpdateVideoTrackInfo();
425 // UpdateTextTrackInfo();
428 if (GetMediaType() == MediaType::Invalid) {
429 LOG(ERROR) << "Media type is not valid!";
433 OnReadyStateChange(player_id_,
434 blink::WebMediaPlayer::kReadyStateHaveFutureData);
435 OnNetworkStateChange(player_id_, blink::WebMediaPlayer::kNetworkStateLoading);
437 if (!player_prepared_ && HBBTVResourceAcquired()) {
438 int ret = SetPlayerPrepareAsync();
439 if (ret != PLAYER_ERROR_NONE) {
440 OnHandlePlayerError(ret, FROM_HERE);
445 bool MediaPlayerBridgeCapiTV::PlayerPrePlay() {
446 LOG(INFO) << "PlayerPrePlay, this: " << this
447 << ",is_player_prepared : " << player_prepared_;
448 if (IsPlayerSuspended()) {
449 LOG(INFO) << "PlayerPrePlay while player is suspended";
453 if (!HBBTVResourceAcquired()) {
455 << "PlayerPrePlay while it's not in case of HBBTV Resource Acquired";
459 delayed_player_state_ = PLAYER_STATE_DELAYED_PLAY;
460 LOG(INFO) << "begin to |player_prepare_async|";
462 SetDisplayAtPausedState();
463 int ret = SetPlayerPrepareAsync();
464 if (ret != PLAYER_ERROR_NONE) {
465 OnHandlePlayerError(ret, FROM_HERE);
471 void MediaPlayerBridgeCapiTV::Initialize(VideoRendererSink* sink) {
472 player_set_drm_error_cb(player_, DrmErrorCb, this);
473 MediaPlayerBridgeCapi::Initialize(sink);
476 bool MediaPlayerBridgeCapiTV::SetDrmInfo(std::string& drm_info) {
477 // The DRM info from HbbTV comes as a JSON-like string in the following
479 // pair = Property ':' Value
480 // string = pair {',' pair}
481 std::vector<std::string> drm_info_pairs = base::SplitStringUsingSubstr(
482 drm_info, ", ", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
483 for (auto it = drm_info_pairs.begin(); it != drm_info_pairs.end(); ++it) {
484 std::vector<std::string> drm_info_pair = base::SplitStringUsingSubstr(
485 *it, ": ", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
487 if (drm_info_pair.size() != 2)
490 std::string trim_key = drm_info_pair.at(0);
491 std::string trim_value = drm_info_pair.at(1);
492 LOG(INFO) << "trim_key: " << trim_key.c_str()
493 << ",trim_value: " << trim_value.c_str();
495 if (!SetMediaDRMInfo(trim_key, trim_value)) {
497 << "MediaPlayerBridgeCapiTV::SetDrmInfo SetMediaDRMInfo failed";
505 bool MediaPlayerBridgeCapiTV::SetMediaDRMInfo(const std::string& type_data,
506 const std::string& value_data) {
507 LOG(INFO) << "player_set_drm_info(type_length(" << type_data.length() << ") "
508 << " value_length(" << value_data.length() << ")) ";
509 int ret = PLAYER_ERROR_INVALID_OPERATION;
510 const void* type_data_ptr = type_data.c_str();
511 const void* value_data_ptr = value_data.c_str();
512 player_drm_type_e drm_type = PLAYER_DRM_TYPE_PLAYREADY;
513 ret = player_set_drm_info(player_, type_data_ptr, type_data.length(),
514 value_data_ptr, value_data.length(), drm_type);
515 if (ret != PLAYER_ERROR_NONE) {
516 MediaPlayerBridgeCapi::OnHandlePlayerError(ret, FROM_HERE);
523 void MediaPlayerBridgeCapiTV::OnDrmError(int err_code, char* err_str) {
524 LOG(ERROR) << "OnDrmError err_str[" << err_str << "]";
525 if (!task_runner_->BelongsToCurrentThread()) {
526 task_runner_->PostTask(
528 base::BindOnce(&MediaPlayerBridgeCapiTV::OnDrmError,
529 weak_factory_.GetWeakPtr(), err_code, err_str));
531 MediaPlayerBridgeCapi::OnHandlePlayerError(err_code, FROM_HERE);