• 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 #ifndef HISTREAMER_AUDIO_FFMPEG_ENCODER_PLUGIN_H
16 #define HISTREAMER_AUDIO_FFMPEG_ENCODER_PLUGIN_H
17 
18 #include <functional>
19 #include <map>
20 #include "utils/blocking_queue.h"
21 #include "plugins/ffmpeg_adapter/utils/ffmpeg_utils.h"
22 #include "plugin/interface/codec_plugin.h"
23 
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27 #include "libavcodec/avcodec.h"
28 #ifdef __cplusplus
29 };
30 #endif
31 
32 namespace OHOS {
33 namespace Media {
34 namespace Plugin {
35 namespace Ffmpeg {
36 class AudioFfmpegEncoderPlugin : public CodecPlugin {
37 public:
38     explicit AudioFfmpegEncoderPlugin(std::string name);
39 
40     ~AudioFfmpegEncoderPlugin() override;
41 
42     Status Init() override;
43 
44     Status Deinit() override;
45 
46     Status Prepare() override;
47 
48     Status Reset() override;
49 
50     Status Start() override;
51 
52     Status Stop() override;
53 
54     Status GetParameter(Tag tag, ValueType& value) override;
55 
56     Status SetParameter(Tag tag, const ValueType& value) override;
57 
58     std::shared_ptr<Allocator> GetAllocator() override;
59 
SetCallback(Callback * cb)60     Status SetCallback(Callback* cb) override
61     {
62         return Status::OK;
63     }
64 
65     Status QueueInputBuffer(const std::shared_ptr<Buffer>& inputBuffer, int32_t timeoutMs) override;
66 
67     Status QueueOutputBuffer(const std::shared_ptr<Buffer>& outputBuffer, int32_t timeoutMs) override;
68 
69     Status Flush() override;
70 
SetDataCallback(DataCallback * dataCallback)71     Status SetDataCallback(DataCallback* dataCallback) override
72     {
73         dataCallback_ = dataCallback;
74         return Status::OK;
75     }
76 
77 private:
78     Status ResetLocked();
79 
80     Status DeInitLocked();
81 
82     Status SendBufferLocked(const std::shared_ptr<Buffer>& inputBuffer);
83 
84     Status ReceiveFrameSucc(const std::shared_ptr<Buffer>& ioInfo, const std::shared_ptr<AVPacket>& packet);
85 
86     Status ReceiveBuffer();
87 
88     Status ReceiveBufferLocked(const std::shared_ptr<Buffer>& ioInfo);
89 
90     bool CheckReformat();
91 
92     void FillInFrameCache(const std::shared_ptr<Memory>& mem);
93 
94     Status SendOutputBuffer();
95 
96     mutable OSAL::Mutex parameterMutex_ {};
97     std::map<Tag, ValueType> audioParameter_ {};
98 
99     mutable OSAL::Mutex avMutex_ {};
100     std::shared_ptr<const AVCodec> avCodec_ {nullptr};
101     std::shared_ptr<AVCodecContext> avCodecContext_ {nullptr};
102     std::shared_ptr<AVFrame> cachedFrame_ {nullptr};
103     uint32_t fullInputFrameSize_ {0};
104     std::shared_ptr<Buffer> outBuffer_ {nullptr};
105     uint64_t prev_pts_;
106     bool needReformat_ {false};
107     AVSampleFormat srcFmt_ {AVSampleFormat::AV_SAMPLE_FMT_NONE};
108     uint32_t srcBytesPerSample_ {0};
109     DataCallback* dataCallback_ {nullptr};
110     std::shared_ptr<Ffmpeg::Resample> resample_ {nullptr};
111 };
112 } // Ffmpeg
113 } // namespace Plugin
114 } // namespace Media
115 } // namespace OHOS
116 
117 #endif // HISTREAMER_AUDIO_FFMPEG_ENCODER_PLUGIN_H
118