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_AUDIO_FFMPEG_DECODER_PLUGIN_H
17 #define HISTREAMER_AUDIO_FFMPEG_DECODER_PLUGIN_H
18 
19 #include <functional>
20 #include <map>
21 #include "foundation/osal/thread/task.h"
22 #include "foundation/utils/blocking_queue.h"
23 #include "plugin/interface/codec_plugin.h"
24 
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28 #include "libavcodec/avcodec.h"
29 #ifdef __cplusplus
30 };
31 #endif
32 
33 namespace OHOS {
34 namespace Media {
35 namespace Plugin {
36 namespace Ffmpeg {
37 class AudioFfmpegDecoderPlugin : public CodecPlugin {
38 public:
39     explicit AudioFfmpegDecoderPlugin(std::string name);
40 
41     ~AudioFfmpegDecoderPlugin() override;
42 
43     Status Init() override;
44 
45     Status Deinit() override;
46 
47     Status Prepare() override;
48 
49     Status Reset() override;
50 
51     Status Start() override;
52 
53     Status Stop() override;
54 
55     Status GetParameter(Tag tag, ValueType& value) override;
56 
57     Status SetParameter(Tag tag, const ValueType& value) override;
58 
59     std::shared_ptr<Allocator> GetAllocator() override;
60 
SetCallback(Callback * cb)61     Status SetCallback(Callback* cb) override
62     {
63         return Status::OK;
64     }
65 
66     Status QueueInputBuffer(const std::shared_ptr<Buffer>& inputBuffer, int32_t timeoutMs) override;
67 
68     Status QueueOutputBuffer(const std::shared_ptr<Buffer>& outputBuffer, int32_t timeoutMs) override;
69 
70     Status Flush() override;
71 
SetDataCallback(DataCallback * dataCallback)72     Status SetDataCallback(DataCallback* dataCallback) override
73     {
74         dataCallback_ = dataCallback;
75         return Status::OK;
76     }
77 
78 private:
79     Status AssignExtraDataIfExistsLocked(const std::shared_ptr<AVCodecContext>& ctx);
80 
81     Status OpenCtxLocked();
82 
83     Status CloseCtxLocked();
84 
85     Status StopLocked();
86 
87     Status ResetLocked();
88 
89     Status DeInitLocked();
90 
91     template <typename T>
92     Status FindInParameterMapThenAssignLocked(Tag tag, T& assign);
93 
94     Status SendBufferLocked(const std::shared_ptr<Buffer>& inputBuffer);
95 
96     Status ReceiveFrameSucc(const std::shared_ptr<Buffer>& ioInfo);
97 
98     Status ReceiveBuffer();
99 
100     Status ReceiveBufferLocked(const std::shared_ptr<Buffer>& ioInfo);
101 
102     Status SendOutputBuffer();
103 
104     mutable OSAL::Mutex parameterMutex_ {};
105     std::map<Tag, ValueType> audioParameter_ {};
106 
107     mutable OSAL::Mutex avMutex_ {};
108     std::shared_ptr<const AVCodec> avCodec_ {};
109     std::shared_ptr<AVCodecContext> avCodecContext_ {};
110     std::shared_ptr<AVFrame> cachedFrame_ {};
111     std::shared_ptr<AVPacket> avPacket_ {};
112 
113     std::vector<uint8_t> paddedBuffer_ {};
114     size_t paddedBufferSize_ {0};
115     std::shared_ptr<Buffer> outBuffer_ {nullptr};
116     DataCallback* dataCallback_ {nullptr};
117     int64_t preBufferGroupPts_ {0};
118     int64_t curBufferGroupPts_ {0};
119     int32_t bufferNum_ {1};
120     int32_t bufferIndex_ {1};
121     int64_t bufferGroupPtsDistance {0};
122     mutable OSAL::Mutex bufferMetaMutex_ {};
123     std::shared_ptr<BufferMeta> bufferMeta_ {nullptr};
124 };
125 } // namespace Ffmpeg
126 } // namespace Plugin
127 } // namespace Media
128 } // namespace OHOS
129 
130 #endif // HISTREAMER_AUDIO_FFMPEG_DECODER_PLUGIN_H
131