1 /* 2 * Copyright (c) 2021-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 MINIMP3_DEMUXER_PLUGIN_H 17 #define MINIMP3_DEMUXER_PLUGIN_H 18 19 #include <memory> 20 #include <string> 21 #include <vector> 22 23 #include "core/plugin_register.h" 24 #include "minimp3_wrapper.h" 25 #include "plugin/interface/demuxer_plugin.h" 26 #include "thread/mutex.h" 27 #include "utils/type_define.h" 28 29 using Mp3DemuxerHandle = Minimp3WrapperMp3dec; 30 using Mp3DemuxerSampleAttr = Minimp3WrapperMp3dSample; 31 using Mp3DemuxerFrameInfo = Minimp3WrapperMp3decFrameInfo; 32 33 enum AudioDemuxerStatus { 34 AUDIO_DEMUXER_PREPARE_UNMATCHED_FORMAT = -2, 35 AUDIO_DEMUXER_ERROR = -1, 36 AUDIO_DEMUXER_SUCCESS = 0, 37 AUDIO_DEMUXER_PROCESS_NEED_MORE_DATA, 38 AUDIO_DEMUXER_PREPARE_NEED_MORE_DATA, 39 AUDIO_DEMUXER_PREPARE_NEED_SEEK, 40 AUDIO_DEMUXER_SEEK_NEED_MORE_DATA, 41 }; 42 43 struct AudioDemuxerUserArg { 44 uint32_t fileSize; 45 void *priv; 46 }; 47 48 struct AudioDemuxerRst { 49 uint64_t usedInputLength; 50 uint8_t *frameBuffer; 51 uint32_t frameLength; 52 uint64_t inputNeedOffsetSize; 53 uint32_t frameBitrateKbps; 54 uint32_t frameSampleRate; 55 uint8_t frameChannels; 56 uint8_t audioLayer; 57 uint32_t samplesPerFrame; 58 }; 59 60 struct AudioDemuxerMp3Attr { 61 Mp3DemuxerHandle mp3DemuxerHandle; 62 AudioDemuxerRst *rst; 63 void *userArg; 64 uint32_t internalRemainLen; 65 uint32_t fileSize; 66 uint32_t bitRate; 67 uint32_t sampleRate; 68 uint8_t channelNum; 69 70 uint8_t id3v2SkipFlag; 71 uint32_t id3v2Size; 72 uint32_t id3v2Offset; 73 74 uint32_t seekOffset; 75 76 uint8_t mp3SeekFlag; 77 uint8_t discardItemCount; 78 79 Mp3DemuxerSampleAttr *probePcmBuf; 80 }; 81 82 namespace OHOS { 83 namespace Media { 84 namespace Plugin { 85 namespace Minimp3 { 86 class Minimp3DemuxerPlugin : public DemuxerPlugin { 87 public: 88 explicit Minimp3DemuxerPlugin(std::string name); 89 ~Minimp3DemuxerPlugin() override; 90 Status Init() override; 91 Status Deinit() override; 92 Status Prepare() override; 93 Status Reset() override; 94 Status Start() override; 95 Status Stop() override; 96 bool IsParameterSupported(Tag tag) override; 97 Status GetParameter(Tag tag, ValueType& value) override; 98 Status SetParameter(Tag tag, const ValueType& value) override; 99 std::shared_ptr<Allocator> GetAllocator() override; 100 Status SetCallback(Callback* cb) override; 101 102 Status SetDataSource(const std::shared_ptr<DataSource>& source) override; 103 Status GetMediaInfo(MediaInfo& mediaInfo) override; 104 Status ReadFrame(Buffer& outBuffer, int32_t timeOutMs) override; 105 Status SeekTo(int32_t trackId, int64_t hstTime, SeekMode mode) override; 106 107 size_t GetTrackCount() override; 108 Status SelectTrack(int32_t trackId) override; 109 Status UnselectTrack(int32_t trackId) override; 110 Status GetSelectedTracks(std::vector<int32_t>& trackIds) override; 111 Status GetDataFromSource(); 112 uint64_t GetCurrentPositionTimeS(); 113 private: 114 struct IOContext { 115 std::shared_ptr<DataSource> dataSource {nullptr}; 116 int64_t offset {0}; 117 bool eos {false}; 118 }; 119 void AudioDemuxerMp3IgnoreTailZero(uint8_t *data, uint32_t *dataLen); 120 static int AudioDemuxerMp3IterateCallback(void *userData, const uint8_t *frame, int frameSize, 121 int freeFormatBytes, size_t bufSize, uint64_t offset, 122 Mp3DemuxerFrameInfo *info); 123 static int AudioDemuxerMp3IterateCallbackForPrepare(void *userData, const uint8_t *frame, 124 int frameSize, int freeFormatBytes, size_t bufSize, 125 uint64_t offset, Mp3DemuxerFrameInfo *info); 126 void AudioDemuxerMp3Open(); 127 int AudioDemuxerMp3Close(); 128 Status AudioDemuxerMp3Prepare(AudioDemuxerMp3Attr *mp3DemuxerAttr, uint8_t *inputBuffer, 129 uint32_t inputLength, AudioDemuxerRst *mp3DemuxerRst); 130 int AudioDemuxerMp3Process(uint8_t *buf, uint32_t len); 131 int AudioDemuxerMp3FreeFrame(uint8_t *frame); 132 int AudioDemuxerMp3Seek(uint32_t pos, uint8_t *buf, uint32_t len, AudioDemuxerRst *rst); 133 int AudioDemuxerMp3GetSeekPosition(uint32_t targetTimeMs, uint64_t *pos); 134 135 Status DoReadFromSource(uint32_t readSize); 136 137 void FillInMediaInfo(MediaInfo& mediaInfo) const; 138 139 int inIoBufferSize_; 140 size_t fileSize_; 141 uint8_t *inIoBuffer_; 142 uint32_t ioDataRemainSize_; 143 uint64_t currentDemuxerPos_; 144 uint64_t durationMs_; 145 IOContext ioContext_; 146 AudioDemuxerRst mp3DemuxerRst_ {}; 147 Minimp3DemuxerOp minimp3DemuxerImpl_ {}; 148 AudioDemuxerMp3Attr mp3DemuxerAttr_ {}; 149 }; 150 } // namespace Minimp3 151 } // namespace Plugin 152 } // namespace Media 153 } // namespace OHOS 154 155 #endif // MINIMP3_DEMUXER_PLUGIN_H 156