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