1 /* 2 * Copyright (c) 2023-2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef AVCODEC_SOURCE_PLUGIN_H 17 #define AVCODEC_SOURCE_PLUGIN_H 18 19 #include <map> 20 #include <string> 21 22 #include "common/media_source.h" 23 #include "plugin/plugin_base.h" 24 #include "plugin/plugin_buffer.h" 25 #include "plugin/plugin_caps.h" 26 #include "plugin/plugin_definition.h" 27 #include "meta/media_types.h" 28 #include "plugin/plugin_time.h" 29 30 namespace OHOS { 31 namespace Media { 32 namespace Plugins { 33 34 enum StreamType { 35 MIXED = 0, 36 VIDEO, 37 AUDIO, 38 SUBTITLE 39 }; 40 41 enum VideoType { 42 VIDEO_TYPE_SDR = 0, 43 VIDEO_TYPE_HDR_VIVID = 1, 44 VIDEO_TYPE_HDR_10 45 }; 46 47 class StreamInfo { 48 public: 49 int32_t streamId; 50 StreamType type; 51 uint32_t bitRate; 52 53 int32_t videoHeight = 0; 54 int32_t videoWidth = 0; 55 std::string lang = ""; 56 VideoType videoType = VideoType::VIDEO_TYPE_SDR; 57 std::string trackName = ""; 58 }; 59 60 /** 61 * @brief Source Plugin Interface. 62 * 63 * The data source may be network push or active read. 64 * 65 * @since 1.0 66 * @version 1.0 67 */ 68 class SourcePlugin : public PluginBase { 69 /// constructor 70 public: SourcePlugin(std::string name)71 explicit SourcePlugin(std::string name): PluginBase(std::move(name)) {} 72 /** 73 * @brief Set the data source to source plugin. 74 * 75 * The function is valid only in the CREATED state. 76 * 77 * @param source data source, uri or stream source 78 * @return Execution status return 79 * @retval OK: Plugin SetSource succeeded. 80 * @retval ERROR_WRONG_STATE: Call this function in non wrong state 81 * @retval ERROR_NOT_EXISTED: Uri is not existed. 82 * @retval ERROR_UNSUPPORTED_FORMAT: Uri is not supported. 83 * @retval ERROR_INVALID_PARAMETER: Uri is invalid. 84 */ 85 virtual Status SetSource(std::shared_ptr<MediaSource> source) = 0; 86 87 /** 88 * @brief Read data from data source. 89 * 90 * The function is valid only after RUNNING state. 91 * 92 * @param buffer Buffer to store the data, it can be nullptr or empty to get the buffer from plugin. 93 * @param expectedLen Expected data size to be read 94 * @return Execution status return 95 * @retval OK: Plugin Read succeeded. 96 * @retval ERROR_NOT_ENOUGH_DATA: Data not enough 97 * @retval END_OF_STREAM: End of stream 98 */ 99 virtual Status Read(std::shared_ptr<Buffer>& buffer, uint64_t offset, size_t expectedLen) = 0; 100 101 /** 102 * @brief Read data from data source. 103 * 104 * The function is valid only after RUNNING state. 105 * 106 * @param streamId stream index. 107 * @param buffer Buffer to store the data, it can be nullptr or empty to get the buffer from plugin. 108 * @param expectedLen Expected data size to be read 109 * @return Execution status return 110 * @retval OK: Plugin Read succeeded. 111 * @retval ERROR_NOT_ENOUGH_DATA: Data not enough 112 * @retval END_OF_STREAM: End of stream 113 */ Read(int32_t streamId,std::shared_ptr<Buffer> & buffer,uint64_t offset,size_t expectedLen)114 virtual Status Read(int32_t streamId, std::shared_ptr<Buffer>& buffer, uint64_t offset, size_t expectedLen) 115 { 116 return Status::OK; 117 } 118 119 /** 120 * @brief Get data source size. 121 * 122 * The function is valid only after INITIALIZED state. 123 * 124 * @param size data source size. 125 * @return Execution status return. 126 * @retval OK: Plugin GetSize succeeded. 127 */ 128 virtual Status GetSize(uint64_t& size) = 0; 129 130 /** 131 * @brief Indicates that the current source can be seek. 132 * 133 * The function is valid only after INITIALIZED state. 134 * 135 * @return Execution status return 136 * @retval OK: Plugin GetSeekable succeeded. 137 */ 138 virtual Seekable GetSeekable() = 0; 139 140 /** 141 * @brief Seeks for a specified position for the source. 142 * 143 * After being started, the source seeks for a specified position to read data frames. 144 * 145 * The function is valid only after RUNNING state. 146 * 147 * @param offset position to read data frames 148 * @return Execution status return 149 * @retval OK: Plugin SeekTo succeeded. 150 * @retval ERROR_INVALID_DATA: The offset is invalid. 151 */ 152 virtual Status SeekTo(uint64_t offset) = 0; 153 154 virtual Status Reset() = 0; 155 SetDemuxerState(int32_t streamId)156 virtual void SetDemuxerState(int32_t streamId) {} 157 SetDownloadErrorState()158 virtual void SetDownloadErrorState() {} 159 SetBundleName(const std::string & bundleName)160 virtual void SetBundleName(const std::string& bundleName) {} 161 GetDownloadInfo(DownloadInfo & downloadInfo)162 virtual Status GetDownloadInfo(DownloadInfo& downloadInfo) 163 { 164 return Status::OK; 165 } 166 GetPlaybackInfo(PlaybackInfo & playbackInfo)167 virtual Status GetPlaybackInfo(PlaybackInfo& playbackInfo) 168 { 169 return Status::OK; 170 } 171 GetBitRates(std::vector<uint32_t> & bitRates)172 virtual Status GetBitRates(std::vector<uint32_t>& bitRates) 173 { 174 return Status::OK; 175 } 176 SetStartPts(int64_t startPts)177 virtual Status SetStartPts(int64_t startPts) 178 { 179 return Status::OK; 180 } 181 SetExtraCache(uint64_t cacheDuration)182 virtual Status SetExtraCache(uint64_t cacheDuration) 183 { 184 return Status::OK; 185 } 186 SelectBitRate(uint32_t bitRate)187 virtual Status SelectBitRate(uint32_t bitRate) 188 { 189 return Status::OK; 190 } 191 AutoSelectBitRate(uint32_t bitRate)192 virtual Status AutoSelectBitRate(uint32_t bitRate) 193 { 194 return Status::OK; 195 } 196 IsSeekToTimeSupported()197 virtual bool IsSeekToTimeSupported() 198 { 199 return false; 200 } 201 SeekToTime(int64_t seekTime,SeekMode mode)202 virtual Status SeekToTime(int64_t seekTime, SeekMode mode) 203 { 204 return Status::OK; 205 } 206 GetDuration(int64_t & duration)207 virtual Status GetDuration(int64_t& duration) 208 { 209 duration = Plugins::HST_TIME_NONE; 210 return Status::OK; 211 } 212 IsNeedPreDownload()213 virtual bool IsNeedPreDownload() 214 { 215 return false; 216 } 217 SetReadBlockingFlag(bool isReadBlockingAllowed)218 virtual Status SetReadBlockingFlag(bool isReadBlockingAllowed) 219 { 220 return Status::OK; 221 } 222 SetInterruptState(bool isInterruptNeeded)223 virtual void SetInterruptState(bool isInterruptNeeded) {} 224 SetCurrentBitRate(int32_t bitRate,int32_t streamID)225 virtual Status SetCurrentBitRate(int32_t bitRate, int32_t streamID) 226 { 227 return Status::OK; 228 } 229 GetStreamInfo(std::vector<StreamInfo> & streams)230 virtual Status GetStreamInfo(std::vector<StreamInfo>& streams) 231 { 232 return Status::OK; 233 } 234 IsLocalFd()235 virtual bool IsLocalFd() 236 { 237 return false; 238 } 239 Pause()240 virtual Status Pause() 241 { 242 return Status::OK; 243 } 244 Resume()245 virtual Status Resume() 246 { 247 return Status::OK; 248 } SelectStream(int32_t streamID)249 virtual Status SelectStream(int32_t streamID) 250 { 251 return Status::OK; 252 } SetEnableOnlineFdCache(bool isEnableFdCache)253 virtual void SetEnableOnlineFdCache(bool isEnableFdCache) 254 { 255 (void)isEnableFdCache; 256 } 257 GetSegmentOffset()258 virtual size_t GetSegmentOffset() 259 { 260 return 0; 261 } 262 GetHLSDiscontinuity()263 virtual bool GetHLSDiscontinuity() 264 { 265 return false; 266 } 267 StopBufferring(bool isAppBackground)268 virtual Status StopBufferring(bool isAppBackground) 269 { 270 return Status::OK; 271 } 272 WaitForBufferingEnd()273 virtual void WaitForBufferingEnd() {} SetSourceInitialBufferSize(int32_t offset,int32_t size)274 virtual bool SetSourceInitialBufferSize(int32_t offset, int32_t size) 275 { 276 return false; 277 } 278 NotifyInitSuccess()279 virtual void NotifyInitSuccess() {} 280 GetCachedDuration()281 virtual uint64_t GetCachedDuration() 282 { 283 return 0; 284 } RestartAndClearBuffer()285 virtual void RestartAndClearBuffer() {} IsFlvLive()286 virtual bool IsFlvLive() 287 { 288 return false; 289 } GetContentType()290 virtual std::string GetContentType() 291 { 292 return ""; 293 } IsHlsFmp4()294 virtual bool IsHlsFmp4() 295 { 296 return false; 297 } GetMemorySize()298 virtual uint64_t GetMemorySize() 299 { 300 return 0; 301 } 302 }; 303 304 /// Source plugin api major number. 305 #define SOURCE_API_VERSION_MAJOR (1) 306 307 /// Source plugin api minor number 308 #define SOURCE_API_VERSION_MINOR (0) 309 310 /// Source plugin version 311 #define SOURCE_API_VERSION MAKE_VERSION(SOURCE_API_VERSION_MAJOR, SOURCE_API_VERSION_MINOR) 312 313 /** 314 * @brief Describes the source plugin information. 315 * 316 * @since 1.0 317 * @version 1.0 318 */ 319 struct SourcePluginDef : public PluginDefBase { SourcePluginDefSourcePluginDef320 SourcePluginDef() 321 : PluginDefBase() 322 { 323 apiVersion = SOURCE_API_VERSION; ///< Source plugin version. 324 pluginType = PluginType::SOURCE; ///< Plugin type, MUST be SOURCE. 325 } 326 }; 327 } // namespace Plugins 328 } // namespace Media 329 } // namespace OHOS 330 #endif // AVCODEC_SOURCE_PLUGIN_H 331