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