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 HISTREAMER_MINIMP3_DECODER_PLUGIN_H
17 #define HISTREAMER_MINIMP3_DECODER_PLUGIN_H
18 
19 #include <functional>
20 #include <map>
21 #include "foundation/osal/thread/mutex.h"
22 #include "foundation/osal/thread/scoped_lock.h"
23 #include "minimp3_wrapper.h"
24 #include "plugin/common/plugin_types.h"
25 #include "plugin/interface/codec_plugin.h"
26 
27 using Mp3DecoderHandle = Minimp3WrapperMp3dec;
28 using Mp3DecoderSample = Minimp3WrapperMp3dSample;
29 
30 struct AudioDecoderRst {
31     uint32_t usedInputLength;
32     uint8_t *packetBuffer;
33     uint32_t packetLength;
34 };
35 
36 struct AudioDecoderMp3Attr {
37     Mp3DecoderHandle  mp3DecoderHandle;
38     Mp3DecoderSample *pcm;
39     AudioDecoderRst  *rst;
40 };
41 
42 namespace OHOS {
43 namespace Media {
44 namespace Plugin {
45 namespace Minimp3 {
46 class Minimp3DecoderPlugin : public CodecPlugin {
47 public:
48     explicit Minimp3DecoderPlugin(std::string name);
49 
50     ~Minimp3DecoderPlugin() override;
51 
52     Status Init() override;
53 
54     Status Deinit() override;
55 
56     Status Prepare() override;
57 
58     Status Reset() override;
59 
60     Status Start() override;
61 
62     Status Stop() override;
63 
64     Status GetParameter(Tag tag, ValueType& value) override;
65 
66     Status SetParameter(Tag tag, const ValueType& value) override;
67 
68     std::shared_ptr<Allocator> GetAllocator() override;
69 
70     Status SetCallback(Callback* cb) override;
71 
72     Status GetPcmDataProcess(const std::shared_ptr<Buffer>& inputBuffer, std::shared_ptr<Buffer>& outputBuffer);
73 
74     Status QueueInputBuffer(const std::shared_ptr<Buffer>& inputBuffer, int32_t timeoutMs) override;
75 
76     Status QueueOutputBuffer(const std::shared_ptr<Buffer>& outputBuffers, int32_t timeoutMs) override;
77 
78     Status Flush() override;
79 
80     Status SetDataCallback(DataCallback* dataCallback) override;
81 
82 private:
83 
84     void AudioDecoderMp3Open();
85 
86     int  AudioDecoderMp3Close();
87 
88     Status AudioDecoderMp3Process(std::shared_ptr<Buffer> inBuffer, std::shared_ptr<Buffer> outBuffer);
89 
90     int  AudioDecoderMp3FreePacket(uint8_t *packet);
91 
92     Status SendOutputBuffer();
93 
94     mutable OSAL::Mutex         ioMutex_{};
95     uint32_t                    samplesPerFrame_;
96     uint32_t                    channels_;
97     State                       state_{State::CREATED};
98     Minimp3DemuxerOp            minimp3DecoderImpl_{};
99     AudioDecoderMp3Attr         mp3DecoderAttr_{};
100     std::map<Tag, ValueType>    mp3Parameter_ {};
101     std::shared_ptr<Buffer>     inputBuffer_ {nullptr};
102     std::shared_ptr<Buffer>     outputBuffer_ {nullptr};
103     DataCallback* dataCb_ {};
104 };
105 } // namespace Minimp3
106 } // namespace Plugin
107 } // namespace Media
108 } // namespace OHOS
109 
110 #endif // HISTREAMER_MINIMP3_DECODER_PLUGIN_H