• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #define HST_LOG_TAG "Source"
17 #define MEDIA_ATOMIC_ABILITY
18 
19 #include "avcodec_trace.h"
20 #include "cpp_ext/type_traits_ext.h"
21 #include "common/log.h"
22 #include "osal/utils/util.h"
23 #include "common/media_source.h"
24 #include "plugin/plugin_manager_v2.h"
25 #include "source.h"
26 #include "scoped_timer.h"
27 
28 namespace {
29 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, LOG_DOMAIN_SYSTEM_PLAYER, "Source" };
30 constexpr int64_t SOURCE_INIT_WARNING_MS = 20;
31 }
32 
33 namespace OHOS {
34 namespace Media {
35 using namespace Plugins;
36 
37 static std::map<std::string, ProtocolType> g_protocolStringToType = {
38     {"http", ProtocolType::HTTP},
39     {"https", ProtocolType::HTTPS},
40     {"file", ProtocolType::FILE},
41     {"stream", ProtocolType::STREAM},
42     {"fd", ProtocolType::FD}
43 };
44 
45 const int32_t MAX_RETRY = 20;
46 const int32_t WAIT_TIME = 10;
47 
Source()48 Source::Source()
49     : protocol_(),
50       uri_(),
51       seekable_(Seekable::INVALID),
52       plugin_(nullptr),
53       isPluginReady_(false),
54       isAboveWaterline_(false),
55       mediaDemuxerCallback_(std::make_shared<CallbackImpl>())
56 {
57     MEDIA_LOG_D("Source called");
58 }
59 
~Source()60 Source::~Source()
61 {
62     MEDIA_LOG_D("~Source called");
63     if (plugin_) {
64         plugin_->Deinit();
65     }
66 }
67 
SetCallback(Callback * callback)68 void Source::SetCallback(Callback* callback)
69 {
70     MEDIA_LOG_D("SetCallback entered.");
71     FALSE_RETURN_MSG(callback != nullptr, "callback is nullptr");
72     FALSE_RETURN_MSG(mediaDemuxerCallback_ != nullptr, "mediaDemuxerCallback is nullptr");
73     mediaDemuxerCallback_->SetCallbackWrap(callback);
74 }
75 
ClearData()76 void Source::ClearData()
77 {
78     protocol_.clear();
79     uri_.clear();
80     seekable_ = Seekable::INVALID;
81     isPluginReady_ = false;
82     isAboveWaterline_ = false;
83     seekToTimeFlag_ = false;
84 }
85 
IsFlvLiveStream() const86 bool Source::IsFlvLiveStream() const
87 {
88     return isFlvLiveStream_;
89 }
SetSource(const std::shared_ptr<MediaSource> & source)90 Status Source::SetSource(const std::shared_ptr<MediaSource>& source)
91 {
92     MediaAVCodec::AVCodecTrace trace("Source::SetSource");
93     MEDIA_LOG_D("SetSource enter.");
94     FALSE_RETURN_V_MSG_E(source != nullptr, Status::ERROR_INVALID_PARAMETER, "SetSource Invalid source");
95 
96     ClearData();
97     Status ret = FindPlugin(source);
98     FALSE_RETURN_V_MSG_E(ret == Status::OK, ret, "SetSource FindPlugin failed");
99 
100     {
101         ScopedTimer timer("Source InitPlugin", SOURCE_INIT_WARNING_MS);
102         ret = InitPlugin(source);
103     }
104     FALSE_RETURN_V_MSG_E(ret == Status::OK, ret, "SetSource InitPlugin failed");
105 
106     if (plugin_ != nullptr) {
107         seekToTimeFlag_ = plugin_->IsSeekToTimeSupported();
108     }
109     MEDIA_LOG_D("SetSource seekToTimeFlag_: " PUBLIC_LOG_D32, seekToTimeFlag_);
110     MEDIA_LOG_D("SetSource exit.");
111     return Status::OK;
112 }
113 
SetStartPts(int64_t startPts)114 Status Source::SetStartPts(int64_t startPts)
115 {
116     MEDIA_LOG_D("startPts=" PUBLIC_LOG_D64, startPts);
117     if (plugin_ == nullptr) {
118         MEDIA_LOG_E("SetStartPts failed, plugin_ is nullptr");
119         return Status::ERROR_INVALID_OPERATION;
120     }
121     return plugin_->SetStartPts(startPts);
122 }
123 
SetExtraCache(uint64_t cacheDuration)124 Status Source::SetExtraCache(uint64_t cacheDuration)
125 {
126     if (plugin_ == nullptr) {
127         MEDIA_LOG_E("SetExtraCache failed, plugin_ is nullptr");
128         return Status::ERROR_INVALID_OPERATION;
129     }
130     return plugin_->SetExtraCache(cacheDuration);
131 }
132 
SetBundleName(const std::string & bundleName)133 void Source::SetBundleName(const std::string& bundleName)
134 {
135     if (plugin_ != nullptr) {
136         MEDIA_LOG_I("SetBundleName bundleName: " PUBLIC_LOG_S, bundleName.c_str());
137         plugin_->SetBundleName(bundleName);
138     }
139 }
140 
SetDemuxerState(int32_t streamId)141 void Source::SetDemuxerState(int32_t streamId)
142 {
143     plugin_->SetDemuxerState(streamId);
144 }
145 
GetContentType()146 std::string Source::GetContentType()
147 {
148     FALSE_RETURN_V(plugin_ != nullptr, "");
149     MEDIA_LOG_D("In");
150     return plugin_->GetContentType();
151 }
152 
InitPlugin(const std::shared_ptr<MediaSource> & source)153 Status Source::InitPlugin(const std::shared_ptr<MediaSource>& source)
154 {
155     MediaAVCodec::AVCodecTrace trace("Source::InitPlugin");
156     MEDIA_LOG_D("InitPlugin enter");
157     FALSE_RETURN_V_MSG_E(plugin_ != nullptr, Status::ERROR_INVALID_OPERATION, "InitPlugin, Source plugin is nullptr");
158 
159     Status ret = plugin_->Init();
160     FALSE_RETURN_V_MSG_E(ret == Status::OK, ret, "InitPlugin failed");
161 
162     plugin_->SetCallback(this);
163     plugin_->SetEnableOnlineFdCache(isEnableFdCache_);
164     ret = plugin_->SetSource(source);
165 
166     MEDIA_LOG_D("InitPlugin exit");
167     return ret;
168 }
169 
Prepare()170 Status Source::Prepare()
171 {
172     MEDIA_LOG_I("Prepare entered.");
173     if (plugin_ == nullptr) {
174         return Status::OK;
175     }
176     Status ret = plugin_->Prepare();
177     if (ret == Status::OK) {
178         MEDIA_LOG_D("media source send EVENT_READY");
179     } else if (ret == Status::ERROR_DELAY_READY) {
180         if (isAboveWaterline_) {
181             MEDIA_LOG_D("media source send EVENT_READY");
182             isPluginReady_ = false;
183             isAboveWaterline_ = false;
184         }
185     }
186     return ret;
187 }
188 
IsSeekToTimeSupported()189 bool Source::IsSeekToTimeSupported()
190 {
191     return seekToTimeFlag_;
192 }
193 
Start()194 Status Source::Start()
195 {
196     MEDIA_LOG_I("Start entered.");
197     return plugin_ ? plugin_->Start() : Status::ERROR_INVALID_OPERATION;
198 }
199 
GetBitRates(std::vector<uint32_t> & bitRates)200 Status Source::GetBitRates(std::vector<uint32_t>& bitRates)
201 {
202     MEDIA_LOG_I("GetBitRates");
203     if (plugin_ == nullptr) {
204         MEDIA_LOG_E("GetBitRates failed, plugin_ is nullptr");
205         return Status::ERROR_INVALID_OPERATION;
206     }
207     return plugin_->GetBitRates(bitRates);
208 }
209 
SelectBitRate(uint32_t bitRate)210 Status Source::SelectBitRate(uint32_t bitRate)
211 {
212     MEDIA_LOG_I("SelectBitRate" PUBLIC_LOG_U32, bitRate);
213     if (plugin_ == nullptr) {
214         MEDIA_LOG_E("SelectBitRate failed, plugin_ is nullptr");
215         return Status::ERROR_INVALID_OPERATION;
216     }
217     return plugin_->SelectBitRate(bitRate);
218 }
219 
AutoSelectBitRate(uint32_t bitRate)220 Status Source::AutoSelectBitRate(uint32_t bitRate)
221 {
222     MEDIA_LOG_I("AutoSelectBitRate" PUBLIC_LOG_U32, bitRate);
223     if (plugin_ == nullptr) {
224         MEDIA_LOG_E("AutoSelectBitRate failed, plugin_ is nullptr");
225         return Status::ERROR_INVALID_OPERATION;
226     }
227     return plugin_->AutoSelectBitRate(bitRate);
228 }
229 
SetCurrentBitRate(int32_t bitRate,int32_t streamID)230 Status Source::SetCurrentBitRate(int32_t bitRate, int32_t streamID)
231 {
232     MEDIA_LOG_D("SetCurrentBitRate");
233     if (plugin_ == nullptr) {
234         MEDIA_LOG_E("SetCurrentBitRate failed, plugin_ is nullptr");
235         return Status::ERROR_INVALID_OPERATION;
236     }
237     return plugin_->SetCurrentBitRate(bitRate, streamID);
238 }
239 
SeekToTime(int64_t seekTime,SeekMode mode)240 Status Source::SeekToTime(int64_t seekTime, SeekMode mode)
241 {
242     if (seekable_ != Seekable::SEEKABLE) {
243         GetSeekable();
244     }
245     int64_t timeNs;
246     if (Plugins::Ms2HstTime(seekTime, timeNs)) {
247         return plugin_->SeekToTime(timeNs, mode);
248     } else {
249         return Status::ERROR_INVALID_PARAMETER;
250     }
251 }
252 
GetDownloadInfo(DownloadInfo & downloadInfo)253 Status Source::GetDownloadInfo(DownloadInfo& downloadInfo)
254 {
255     MEDIA_LOG_I("GetDownloadInfo");
256     if (plugin_ == nullptr) {
257         MEDIA_LOG_E("GetDownloadInfo failed, plugin_ is nullptr");
258         return Status::ERROR_INVALID_OPERATION;
259     }
260     return plugin_->GetDownloadInfo(downloadInfo);
261 }
262 
GetPlaybackInfo(PlaybackInfo & playbackInfo)263 Status Source::GetPlaybackInfo(PlaybackInfo& playbackInfo)
264 {
265     MEDIA_LOG_I("GetPlaybackInfo");
266     if (plugin_ == nullptr) {
267         MEDIA_LOG_E("GetPlaybackInfo  failed, plugin_ is nullptr");
268         return Status::ERROR_INVALID_OPERATION;
269     }
270     return plugin_->GetPlaybackInfo(playbackInfo);
271 }
272 
IsNeedPreDownload()273 bool Source::IsNeedPreDownload()
274 {
275     if (plugin_ == nullptr) {
276         MEDIA_LOG_E("IsNeedPreDownload failed, plugin_ is nullptr");
277         return false;
278     }
279     return plugin_->IsNeedPreDownload();
280 }
281 
Stop()282 Status Source::Stop()
283 {
284     MEDIA_LOG_I("Stop entered.");
285     seekable_ = Seekable::INVALID;
286     protocol_.clear();
287     uri_.clear();
288     return plugin_->Stop();
289 }
290 
Pause()291 Status Source::Pause()
292 {
293     MEDIA_LOG_I("Pause entered.");
294     if (plugin_ != nullptr) {
295         plugin_->Pause();
296     }
297     return Status::OK;
298 }
299 
Resume()300 Status Source::Resume()
301 {
302     MEDIA_LOG_I("Resume entered.");
303     if (plugin_ != nullptr) {
304         plugin_->Resume();
305     }
306     return Status::OK;
307 }
308 
SetReadBlockingFlag(bool isReadBlockingAllowed)309 Status Source::SetReadBlockingFlag(bool isReadBlockingAllowed)
310 {
311     MEDIA_LOG_D("SetReadBlockingFlag entered, IsReadBlockingAllowed %{public}d", isReadBlockingAllowed);
312     FALSE_RETURN_V(plugin_ != nullptr, Status::OK);
313     return plugin_->SetReadBlockingFlag(isReadBlockingAllowed);
314 }
315 
OnEvent(const Plugins::PluginEvent & event)316 void Source::OnEvent(const Plugins::PluginEvent& event)
317 {
318     MEDIA_LOG_D("OnEvent");
319     if (protocol_ == "http" && isInterruptNeeded_) {
320         MEDIA_LOG_I("http OnEvent isInterruptNeeded, return");
321         return;
322     }
323     if (event.type == PluginEventType::ABOVE_LOW_WATERLINE) {
324         if (isPluginReady_ && isAboveWaterline_) {
325             isPluginReady_ = false;
326             isAboveWaterline_ = false;
327         }
328         return;
329     }
330     FALSE_RETURN_MSG(mediaDemuxerCallback_ != nullptr, "mediaDemuxerCallback is nullptr");
331     if (event.type == PluginEventType::CLIENT_ERROR || event.type == PluginEventType::SERVER_ERROR) {
332         MEDIA_LOG_I("Error happened, need notify client by OnEvent");
333         mediaDemuxerCallback_->OnEvent(event);
334     } else if (event.type == PluginEventType::SOURCE_DRM_INFO_UPDATE) {
335         MEDIA_LOG_I("Drminfo updates from source");
336         mediaDemuxerCallback_->OnEvent(event);
337     } else if (event.type == PluginEventType::BUFFERING_END || event.type == PluginEventType::BUFFERING_START) {
338         MEDIA_LOG_I("Buffering start or end.");
339         mediaDemuxerCallback_->OnEvent(event);
340     } else if (event.type == PluginEventType::SOURCE_BITRATE_START) {
341         MEDIA_LOG_I("source bitrate start from source.");
342         mediaDemuxerCallback_->OnEvent(event);
343     } else if (event.type == PluginEventType::CACHED_DURATION) {
344         MEDIA_LOG_D("Onevent cached duration.");
345         mediaDemuxerCallback_->OnEvent(event);
346     } else if (event.type == PluginEventType::EVENT_BUFFER_PROGRESS) {
347         MEDIA_LOG_D("buffer percent update.");
348         mediaDemuxerCallback_->OnEvent(event);
349     } else if (event.type == PluginEventType::INITIAL_BUFFER_SUCCESS) {
350         MEDIA_LOG_I("initial buffer success.");
351         mediaDemuxerCallback_->OnEvent(event);
352     } else if (event.type == PluginEventType::DASH_SEEK_READY) {
353         MEDIA_LOG_D("Onevent dash seek ready.");
354         mediaDemuxerCallback_->OnEvent(event);
355     } else if (event.type == PluginEventType::FLV_AUTO_SELECT_BITRATE) {
356         MEDIA_LOG_D("Onevent flv select bitrate.");
357         mediaDemuxerCallback_->OnEvent(event);
358     } else if (event.type == PluginEventType::HLS_SEEK_READY) {
359         MEDIA_LOG_D("Onevent hls seek ready.");
360         mediaDemuxerCallback_->OnEvent(event);
361     } else {
362         MEDIA_LOG_I("on event type undefined.");
363     }
364 }
365 
SetSelectBitRateFlag(bool flag,uint32_t desBitRate)366 void Source::SetSelectBitRateFlag(bool flag, uint32_t desBitRate)
367 {
368     if (mediaDemuxerCallback_) {
369         mediaDemuxerCallback_->SetSelectBitRateFlag(flag, desBitRate);
370     }
371 }
372 
CanAutoSelectBitRate()373 bool Source::CanAutoSelectBitRate()
374 {
375     FALSE_RETURN_V_MSG_E(mediaDemuxerCallback_ != nullptr, false, "mediaDemuxerCallback_ is nullptr.");
376     return mediaDemuxerCallback_->CanAutoSelectBitRate();
377 }
378 
SetInterruptState(bool isInterruptNeeded)379 void Source::SetInterruptState(bool isInterruptNeeded)
380 {
381     MEDIA_LOG_I("Source OnInterrupted %{public}d", isInterruptNeeded);
382     std::unique_lock<std::mutex> lock(mutex_);
383     isInterruptNeeded_ = isInterruptNeeded;
384     if (plugin_) {
385         plugin_->SetInterruptState(isInterruptNeeded_);
386     }
387     seekCond_.notify_all();
388 }
389 
GetSeekable()390 Plugins::Seekable Source::GetSeekable()
391 {
392     FALSE_RETURN_V_MSG_E(plugin_ != nullptr, Plugins::Seekable::INVALID, "GetSeekable, Source plugin is nullptr");
393     int32_t retry {0};
394     seekable_ = Seekable::INVALID;
395     do {
396         seekable_ = plugin_->GetSeekable();
397         retry++;
398         if (seekable_ == Seekable::INVALID) {
399             if (retry >= MAX_RETRY) {
400                 break;
401             }
402             std::unique_lock<std::mutex> lock(mutex_);
403             seekCond_.wait_for(lock, std::chrono::milliseconds(WAIT_TIME), [&] { return isInterruptNeeded_.load(); });
404         }
405     } while (seekable_ == Seekable::INVALID && !isInterruptNeeded_.load());
406     return seekable_;
407 }
408 
GetUriSuffix(const std::string & uri)409 std::string Source::GetUriSuffix(const std::string& uri)
410 {
411     MEDIA_LOG_D("IN");
412     std::string suffix;
413     auto const pos = uri.find_last_of('.');
414     if (pos != std::string::npos) {
415         suffix = uri.substr(pos + 1);
416     }
417     return suffix;
418 }
419 
Read(int32_t streamID,std::shared_ptr<Buffer> & buffer,uint64_t offset,size_t expectedLen)420 Status Source::Read(int32_t streamID, std::shared_ptr<Buffer>& buffer, uint64_t offset, size_t expectedLen)
421 {
422     FALSE_RETURN_V_MSG_E(plugin_ != nullptr, Status::ERROR_INVALID_OPERATION, "ReadData, Source plugin is nullptr");
423     FALSE_RETURN_V_NOLOG(!perfRecEnabled_, ReadWithPerfRecord(streamID, buffer, offset, expectedLen));
424     if (seekToTimeFlag_) {
425         return plugin_->Read(streamID, buffer, offset, expectedLen);
426     }
427     return plugin_->Read(buffer, offset, expectedLen);
428 }
429 
SetPerfRecEnabled(bool perfRecEnabled)430 Status Source::SetPerfRecEnabled(bool perfRecEnabled)
431 {
432     perfRecEnabled_ = perfRecEnabled;
433     return Status::OK;
434 }
435 
ReadWithPerfRecord(int32_t streamID,std::shared_ptr<Buffer> & buffer,uint64_t offset,size_t expectedLen)436 Status Source::ReadWithPerfRecord(
437     int32_t streamID, std::shared_ptr<Buffer>& buffer, uint64_t offset, size_t expectedLen)
438 {
439     int64_t readDuration = 0;
440     Status readRes = Status::OK;
441     if (seekToTimeFlag_) {
442         readDuration = CALC_EXPR_TIME_MS(readRes = plugin_->Read(streamID, buffer, offset, expectedLen));
443     } else {
444         readDuration = CALC_EXPR_TIME_MS(readRes = plugin_->Read(buffer, offset, expectedLen));
445     }
446     int64_t readDurationUs = 0;
447     FALSE_RETURN_V_MSG(
448         Plugins::Ms2Us(readDuration, readDurationUs), readRes, "Invalid readDuration %{public}" PRId64, readDuration);
449     int64_t readSpeed = static_cast<int64_t>(expectedLen) / readDurationUs;
450     FALSE_RETURN_V_NOLOG(perfRecorder_.Record(readSpeed) == PerfRecorder::FULL, readRes);
451     FALSE_RETURN_V_MSG(mediaDemuxerCallback_ != nullptr, readRes, "Report perf failed, callback is nullptr");
452     mediaDemuxerCallback_->OnDfxEvent(
453         { .type = Plugins::PluginDfxEventType::PERF_SOURCE, .param = perfRecorder_.GetMainPerfData() });
454     perfRecorder_.Reset();
455     return readRes;
456 }
457 
SeekTo(uint64_t offset)458 Status Source::SeekTo(uint64_t offset)
459 {
460     FALSE_RETURN_V_MSG_E(plugin_ != nullptr, Status::ERROR_INVALID_OPERATION, "SeekTo, Source plugin is nullptr");
461     return plugin_->SeekTo(offset);
462 }
463 
GetStreamInfo(std::vector<StreamInfo> & streams)464 Status Source::GetStreamInfo(std::vector<StreamInfo>& streams)
465 {
466     FALSE_RETURN_V_MSG_E(plugin_ != nullptr, Status::ERROR_INVALID_OPERATION,
467         "GetStreamInfo, Source plugin is nullptr");
468     Status ret = plugin_->GetStreamInfo(streams);
469     if (ret == Status::OK && streams.size() == 0) {
470         MEDIA_LOG_D("GetStreamInfo empty, MIX Stream");
471         Plugins::StreamInfo info;
472         info.streamId = 0;
473         info.bitRate = 0;
474         info.type = Plugins::MIXED;
475         streams.push_back(info);
476     }
477     for (auto& iter : streams) {
478         MEDIA_LOG_D("Source GetStreamInfo id = " PUBLIC_LOG_D32 " type = " PUBLIC_LOG_D32,
479             iter.streamId, iter.type);
480     }
481     return ret;
482 }
483 
GetProtocolByUri()484 bool Source::GetProtocolByUri()
485 {
486     auto ret = true;
487     auto const pos = uri_.find("://");
488     if (pos != std::string::npos) {
489         auto prefix = uri_.substr(0, pos);
490         protocol_.append(prefix);
491     } else {
492         protocol_.append("file");
493         std::string fullPath;
494         ret = OSAL::ConvertFullPath(uri_, fullPath); // convert path to full path
495         if (ret && !fullPath.empty()) {
496             uri_ = fullPath;
497         }
498     }
499     return ret;
500 }
501 
ParseProtocol(const std::shared_ptr<MediaSource> & source)502 bool Source::ParseProtocol(const std::shared_ptr<MediaSource>& source)
503 {
504     bool ret = true;
505     SourceType srcType = source->GetSourceType();
506     MEDIA_LOG_D("sourceType = " PUBLIC_LOG_D32, CppExt::to_underlying(srcType));
507     if (srcType == SourceType::SOURCE_TYPE_URI) {
508         uri_ = source->GetSourceUri();
509         isFlvLiveStream_ = source->GetMediaStreamList().size() > 0;
510         std::string mimeType = source->GetMimeType();
511         if (mimeType == AVMimeTypes::APPLICATION_M3U8) {
512             protocol_ = "http";
513         } else {
514             ret = GetProtocolByUri();
515         }
516     } else if (srcType == SourceType::SOURCE_TYPE_FD) {
517         protocol_.append("fd");
518         uri_ = source->GetSourceUri();
519     } else if (srcType == SourceType::SOURCE_TYPE_STREAM) {
520         protocol_.append("stream");
521         uri_.append("stream://");
522     }
523     return ret;
524 }
525 
FindPlugin(const std::shared_ptr<MediaSource> & source)526 Status Source::FindPlugin(const std::shared_ptr<MediaSource>& source)
527 {
528     MediaAVCodec::AVCodecTrace trace("Source::FindPlugin");
529     if (!ParseProtocol(source)) {
530         MEDIA_LOG_E("Invalid source!");
531         return Status::ERROR_INVALID_PARAMETER;
532     }
533     if (protocol_.empty()) {
534         MEDIA_LOG_E("protocol_ is empty");
535         return Status::ERROR_INVALID_PARAMETER;
536     }
537     auto plugin = Plugins::PluginManagerV2::Instance().CreatePluginByMime(Plugins::PluginType::SOURCE, protocol_);
538     if (plugin != nullptr) {
539         plugin_ = std::static_pointer_cast<SourcePlugin>(plugin);
540         plugin_->SetInterruptState(isInterruptNeeded_);
541         return Status::OK;
542     }
543     MEDIA_LOG_E("Cannot find any plugin");
544     return Status::ERROR_UNSUPPORTED_FORMAT;
545 }
546 
IsLocalFd()547 bool Source::IsLocalFd()
548 {
549     FALSE_RETURN_V_MSG_W(plugin_ != nullptr, false, "IsLocalFd source plugin is nullptr");
550     return plugin_->IsLocalFd();
551 }
552 
GetDuration()553 int64_t Source::GetDuration()
554 {
555     FALSE_RETURN_V_MSG_W(seekToTimeFlag_, Plugins::HST_TIME_NONE, "Source GetDuration return -1 for isHls false");
556     FALSE_RETURN_V_MSG_W(plugin_ != nullptr, Plugins::HST_TIME_NONE, "Source GetDuration error, plugin_ is nullptr");
557     int64_t duration;
558     Status ret = plugin_->GetDuration(duration);
559     FALSE_RETURN_V_MSG_W(ret == Status::OK, Plugins::HST_TIME_NONE, "Source GetDuration from source plugin failed");
560     return duration;
561 }
562 
GetSize(uint64_t & fileSize)563 Status Source::GetSize(uint64_t &fileSize)
564 {
565     FALSE_RETURN_V_MSG_W(plugin_ != nullptr, Status::ERROR_INVALID_OPERATION, "GetSize Source plugin is nullptr!");
566     return plugin_->GetSize(fileSize);
567 }
568 
SelectStream(int32_t streamID)569 Status Source::SelectStream(int32_t streamID)
570 {
571     FALSE_RETURN_V_MSG_W(plugin_ != nullptr, Status::ERROR_INVALID_OPERATION, "SelectStream Source plugin is nullptr!");
572     return plugin_->SelectStream(streamID);
573 }
574 
SetEnableOnlineFdCache(bool isEnableFdCache)575 void Source::SetEnableOnlineFdCache(bool isEnableFdCache)
576 {
577     isEnableFdCache_ = isEnableFdCache;
578 }
579 
GetSegmentOffset()580 size_t Source::GetSegmentOffset()
581 {
582     FALSE_RETURN_V_MSG_W(plugin_ != nullptr, 0, "GetSegmentOffset source plugin is nullptr!");
583     return plugin_->GetSegmentOffset();
584 }
585 
GetHLSDiscontinuity()586 bool Source::GetHLSDiscontinuity()
587 {
588     FALSE_RETURN_V_MSG_W(plugin_ != nullptr, false, "GetHLSDiscontinuity source plugin is nullptr!");
589     return plugin_->GetHLSDiscontinuity();
590 }
591 
SetInitialBufferSize(int32_t offset,int32_t size)592 bool Source::SetInitialBufferSize(int32_t offset, int32_t size)
593 {
594     FALSE_RETURN_V_MSG_W(plugin_ != nullptr, false, "SetInitialBufferSize source plugin is nullptr");
595     return plugin_->SetSourceInitialBufferSize(offset, size);
596 }
597 
WaitForBufferingEnd()598 void Source::WaitForBufferingEnd()
599 {
600     FALSE_RETURN_MSG(plugin_ != nullptr, "WaitForBufferingEnd source plugin is nullptr");
601     return plugin_->WaitForBufferingEnd();
602 }
603 
NotifyInitSuccess()604 void Source::NotifyInitSuccess()
605 {
606     FALSE_RETURN_MSG(plugin_ != nullptr, "NotifyInitSuccess source plugin is nullptr");
607     plugin_->NotifyInitSuccess();
608 }
609 
GetCachedDuration()610 uint64_t Source::GetCachedDuration()
611 {
612     FALSE_RETURN_V_MSG_E(plugin_ != nullptr, 0, "plugin_ is nullptr");
613     return plugin_->GetCachedDuration();
614 }
615 
RestartAndClearBuffer()616 void Source::RestartAndClearBuffer()
617 {
618     FALSE_RETURN_MSG(plugin_ != nullptr, "plugin_ is nullptr");
619     return plugin_->RestartAndClearBuffer();
620 }
621 
IsFlvLive()622 bool Source::IsFlvLive()
623 {
624     FALSE_RETURN_V_MSG_E(plugin_ != nullptr, false, "plugin_ is nullptr");
625     return plugin_->IsFlvLive();
626 }
627 
IsHlsFmp4()628 bool Source::IsHlsFmp4()
629 {
630     FALSE_RETURN_V_MSG_E(plugin_ != nullptr, false, "plugin_ is nullptr");
631     return plugin_->IsHlsFmp4();
632 }
633 
GetMemorySize()634 uint64_t Source::GetMemorySize()
635 {
636     FALSE_RETURN_V_MSG_E(plugin_ != nullptr, 0, "plugin_ is nullptr");
637     return plugin_->GetMemorySize();
638 }
639 
StopBufferring(bool isAppBackground)640 Status Source::StopBufferring(bool isAppBackground)
641 {
642     FALSE_RETURN_V_MSG_E(plugin_ != nullptr, Status::ERROR_NULL_POINTER, "plugin_ is nullptr");
643     return plugin_->StopBufferring(isAppBackground);
644 }
645 } // namespace Media
646 } // namespace OHOS