1 /* 2 * Copyright (C) 2021 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 AVSPLITER_H 17 #define AVSPLITER_H 18 19 #include <string> 20 #include "avcontainer_common.h" 21 #include "media_data_source.h" 22 #include "media_description.h" 23 24 namespace OHOS { 25 namespace Media { 26 /** 27 * @brief Enumerates the track select mode. 28 */ 29 enum TrackSelectMode : uint8_t { 30 /** 31 * @brief this mode indicates that all track will be processed synchronized. When 32 * selecting a track, this track's starting position to read will be forced to keep 33 * sync with the other already selected tracks. If it is the first track selected, 34 * this track will be read from zero timestamp. 35 */ 36 TRACK_TIME_SYNC, 37 /** 38 * @brief this mode indicates that all track will be processed independent. When 39 * selecting a track, this track's starting position to read will be restored to 40 * last unselected position. If it it selected for the first time, this track will 41 * be read from zero timestamp. 42 */ 43 TRACK_TIME_INDEPENDENT, 44 }; 45 46 /** 47 * @brief Provides the track spliter for media files to get sample data of each track. 48 */ 49 class AVSpliter { 50 public: 51 virtual ~AVSpliter() = default; 52 53 /** 54 * @brief Set the uri source for avspliter. Calling this method before the reset 55 * of the methods in this class. This method maybe time consuming. 56 * 57 * @param uri the URI of input media source. 58 * @param mode the mode indicates how to set the track's sample read position 59 * when select a new track, see {@link TrackSelectMode}. 60 * @return Returns {@link MSERR_OK} if the setting is successful; returns 61 * an error code otherwise. 62 */ 63 virtual int32_t SetSource(const std::string &uri, TrackSelectMode mode) = 0; 64 65 /** 66 * @brief Set the data source for avspliter. Calling this method before the reset 67 * of the methods in this class. This method maybe time consuming. 68 * 69 * @param dataSource the media data source to be split 70 * @param mode the mode indicates how to set the track's sample read position 71 * when select a new track, see {@link TrackSelectMode}. 72 * @return Returns {@link MSERR_OK} if the setting is successful; returns 73 * an error code otherwise. 74 */ 75 virtual int32_t SetSource(std::shared_ptr<IMediaDataSource> dataSource, TrackSelectMode mode) = 0; 76 77 /** 78 * @brief Get the container description. 79 * 80 * @param desc the output result will be filled into this parameter, and 81 * all container informations will be represented by key-value pairs. For 82 * keys, see {@link media_description.h} 83 * @return Returns {@link MSERR_OK} if resolving the container informations 84 * is successful; returns an error code otherwise. 85 */ 86 virtual int32_t GetContainerDescription(MediaDescription &desc) = 0; 87 88 /** 89 * @brief Get the track description for specified track index. 90 * 91 * @param trackIdx the specified track index, for all track count in the 92 * container, refer to the {@link GetContainerDescription}'s result. 93 * @param desc the output result will be filled into this parameter, and 94 * the specified track's all informations will be represented by key-value pairs. 95 * For keys, see {@link media_description.h} 96 * @return Returns {@link MSERR_OK} if resolving the track informations 97 * is successful; returns an error code otherwise. 98 */ 99 virtual int32_t GetTrackDescription(uint32_t trackIdx, MediaDescription &desc) = 0; 100 101 /** 102 * @brief Selecting a specified track to read track sample. Selecting the same track 103 * multiple times has no effect. This function has different behavior when set the 104 * input source with different mode, see {@link TrackSelectMode}. 105 * 106 * @param trackIdx the specified track index. 107 * @return Returns {@link MSERR_OK} if the selecting is success, returns an error code 108 * otherwise. 109 */ 110 virtual int32_t SelectTrack(uint32_t trackIdx) = 0; 111 112 /** 113 * @brief Unselecting a specified track. After this function called, the {@link ReadTrackSample} 114 * will not output sample of the specified track. Unselecting the same track multiple 115 * times has no effect. 116 * 117 * @param trackIdx the specified track index. 118 * @return Returns {@link MSERR_OK} if the unselecting is success, returns an error code 119 * otherwise. 120 */ 121 virtual int32_t UnSelectTrack(uint32_t trackIdx) = 0; 122 123 /** 124 * @brief Read a encoded sample from all selected track sample, and store it in the 125 * buffer starting at the given offset. All sample will be read in sequence based on 126 * timestamps. If no track selected, the default track for supported media type will 127 * be read. If the Codec Specific Data exists, it will be output before any frame data. 128 * Such data would be marked using the flag {@link AVCodecBufferFlag::AVCODEC_BUFFER_FLAG_CODEC_DATA}. 129 * 130 * @param buffer the destination output buffer, see{@link AVContainerMemory}. 131 * @param info the sample's description information, see {@link TrackSampleInfo}. 132 * @return Returns {@link MSERR_OK} if the reading is success, returns an error code 133 * otherwise. 134 */ 135 virtual int32_t ReadTrackSample(std::shared_ptr<AVContainerMemory> buffer, TrackSampleInfo &info) = 0; 136 137 /** 138 * @brief Seek all track to specified time position according the given seek mode. 139 * 140 * @param timeUs the time position in microseconds where the sample will be read. 141 * @param mode the hint about how to seek to the specified time position. 142 * @return Returns {@link MSERR_OK} if the seek is success, returns an error code 143 * otherwise. 144 */ 145 virtual int32_t Seek(int64_t timeUs, AVSeekMode mode) = 0; 146 147 /** 148 * @brief Get the an current estimate of how much data is cached in memory, and 149 * the information about whether the cached data has reached the end of stream. 150 * This API is only valid for network streams. 151 * 152 * @param durationUs cached duration in microseconds. 153 * @param endOfStream true if the cached data has reached the end of stream. 154 * @return Returns {@link MSERR_OK} if the query is success, returns an error code 155 * otherwise. 156 */ 157 virtual int32_t GetCacheState(int64_t &durationUs, bool &endOfStream) = 0; 158 159 /** 160 * @brief Release the internel resource. After this method called, the avspliter 161 * instance can not be used again. 162 */ 163 virtual void Release() = 0; 164 }; 165 166 class __attribute__((visibility("default"))) AVSpliterFactory { 167 public: 168 #ifdef UNSUPPORT_MUXER CreateAVSpliter()169 static std::shared_ptr<AVSpliter> CreateAVSpliter() 170 { 171 return nullptr; 172 } 173 #else 174 static std::shared_ptr<AVSpliter> CreateAVSpliter(); 175 #endif 176 private: 177 AVSpliterFactory() = default; 178 ~AVSpliterFactory() = default; 179 }; 180 } // namespace Media 181 } // namespace OHOS 182 #endif // AVSPLITER_H 183