1 /*
2  * Copyright (c) 2024-2024 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 #ifndef HISTREAMER_MEDIA_DOWNLOADER_H
17 #define HISTREAMER_MEDIA_DOWNLOADER_H
18 
19 #include <string>
20 #include "plugin/plugin_base.h"
21 #include "meta/media_types.h"
22 #include "plugin/source_plugin.h"
23 #include "download/downloader.h"
24 #include "common/media_source.h"
25 
26 namespace OHOS {
27 namespace Media {
28 namespace Plugins {
29 namespace HttpPlugin {
30 struct ReadDataInfo {
31     int32_t streamId_ = 0;
32     int32_t nextStreamId_ = 0; // streamId will change after switch in dash
33     uint64_t ffmpegOffset = 0;
34     unsigned int wantReadLength_ = 0;
35     unsigned int realReadLength_ = 0;
36     bool isEos_ = false;
37 };
38 
39 class MediaDownloader {
40 public:
41     static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, LOG_DOMAIN_STREAM_SOURCE, "HiStreamer" };
42     virtual ~MediaDownloader() = default;
43     virtual bool Open(const std::string& url, const std::map<std::string, std::string>& httpHeader) = 0;
44     virtual void Close(bool isAsync) = 0;
45     virtual void Pause() = 0;
46     virtual void Resume() = 0;
47     virtual Status Read(unsigned char* buff, ReadDataInfo& readDataInfo) = 0;
SeekToPos(int64_t offset)48     virtual bool SeekToPos(int64_t offset)
49     {
50         MEDIA_LOG_E("SeekToPos is unimplemented.");
51         return false;
52     }
53     virtual size_t GetBufferSize() const = 0;
54     virtual bool GetPlayable() = 0;
55     virtual bool GetBufferingTimeOut() = 0;
56     virtual size_t GetContentLength() const = 0;
57     virtual int64_t GetDuration() const = 0;
58     virtual Seekable GetSeekable() const = 0;
59     virtual void SetCallback(Callback* cb) = 0;
60     virtual void SetStatusCallback(StatusCallbackFunc cb) = 0;
61     virtual bool GetStartedStatus() = 0;
GetDownloadInfo(DownloadInfo & downloadInfo)62     virtual void GetDownloadInfo(DownloadInfo& downloadInfo)
63     {
64         MEDIA_LOG_E("GetDownloadInfo is unimplemented.");
65     }
GetDownloadInfo()66     virtual std::pair<int32_t, int32_t> GetDownloadInfo()
67     {
68         MEDIA_LOG_E("GetDownloadInfo is unimplemented.");
69         return std::make_pair(0, 0);
70     }
GetPlaybackInfo(PlaybackInfo & playbackInfo)71     virtual void GetPlaybackInfo(PlaybackInfo& playbackInfo)
72     {
73         MEDIA_LOG_E("GetPlaybackInfo is unimplemented.");
74     }
SeekToTime(int64_t seekTime,SeekMode mode)75     virtual bool SeekToTime(int64_t seekTime, SeekMode mode)
76     {
77         MEDIA_LOG_E("SeekToTime is unimplemented.");
78         return false;
79     }
GetBitRates()80     virtual std::vector<uint32_t> GetBitRates()
81     {
82         MEDIA_LOG_E("GetBitRates is unimplemented.");
83         return {};
84     }
SelectBitRate(uint32_t bitRate)85     virtual bool SelectBitRate(uint32_t bitRate)
86     {
87         MEDIA_LOG_E("SelectBitRate is unimplemented.");
88         return false;
89     }
SetIsTriggerAutoMode(bool isAuto)90     virtual void SetIsTriggerAutoMode(bool isAuto)
91     {
92         MEDIA_LOG_E("SetIsTriggerAutoMode is unimplemented.");
93     }
SetReadBlockingFlag(bool isReadBlockingAllowed)94     virtual void SetReadBlockingFlag(bool isReadBlockingAllowed)
95     {
96         MEDIA_LOG_W("SetReadBlockingFlag is unimplemented.");
97     }
SetDemuxerState(int32_t streamId)98     virtual void SetDemuxerState(int32_t streamId)
99     {
100         MEDIA_LOG_W("SetDemuxerState is unimplemented.");
101     }
SetDownloadErrorState()102     virtual void SetDownloadErrorState()
103     {
104         MEDIA_LOG_W("SetDownloadErrorState is unimplemented.");
105     }
SetCurrentBitRate(int32_t bitRate,int32_t streamID)106     virtual Status SetCurrentBitRate(int32_t bitRate, int32_t streamID)
107     {
108         MEDIA_LOG_W("SetCurrentBitRate is unimplemented.");
109         return Status::OK;
110     }
SetPlayStrategy(const std::shared_ptr<PlayStrategy> & playStrategy)111     virtual void SetPlayStrategy(const std::shared_ptr<PlayStrategy>& playStrategy)
112     {
113         MEDIA_LOG_W("SetPlayStrategy is unimplemented.");
114     }
115     virtual void SetInterruptState(bool isInterruptNeeded) = 0;
GetStreamInfo(std::vector<StreamInfo> & streams)116     virtual Status GetStreamInfo(std::vector<StreamInfo>& streams)
117     {
118         MEDIA_LOG_W("GetStreamInfo is unimplemented.");
119         return Status::OK;
120     }
SelectStream(int32_t streamId)121     virtual Status SelectStream(int32_t streamId)
122     {
123         MEDIA_LOG_W("SelectStream is unimplemented.");
124         return Status::OK;
125     }
126 
127     virtual void SetAppUid(int32_t appUid) = 0;
GetSegmentOffset()128     virtual size_t GetSegmentOffset()
129     {
130         return 0;
131     }
GetHLSDiscontinuity()132     virtual bool GetHLSDiscontinuity()
133     {
134         return false;
135     }
WaitForBufferingEnd()136     virtual void WaitForBufferingEnd() {}
SetIsReportedErrorCode()137     virtual void SetIsReportedErrorCode() {}
138 
GetReadTimeOut()139     virtual bool GetReadTimeOut()
140     {
141         return false;
142     }
143 
IsNotRetry(const std::shared_ptr<DownloadRequest> & request)144     virtual bool IsNotRetry(const std::shared_ptr<DownloadRequest>& request)
145     {
146         return false;
147     }
148 };
149 }
150 }
151 }
152 }
153 #endif