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 "foundation/utils/blocking_queue.h" 21 #include "plugin/convert/ffmpeg_convert.h" 22 #include "plugin/interface/codec_plugin.h" 23 #include "plugins/ffmpeg_adapter/utils/ffmpeg_utils.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 AudioFfmpegEncoderPlugin : public CodecPlugin { 38 public: 39 explicit AudioFfmpegEncoderPlugin(std::string name); 40 41 ~AudioFfmpegEncoderPlugin() 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 ResetLocked(); 80 81 Status DeInitLocked(); 82 83 Status SendBufferLocked(const std::shared_ptr<Buffer>& inputBuffer); 84 85 Status ReceiveFrameSucc(const std::shared_ptr<Buffer>& ioInfo, const std::shared_ptr<AVPacket>& packet); 86 87 Status ReceiveBuffer(); 88 89 Status ReceiveBufferLocked(const std::shared_ptr<Buffer>& ioInfo); 90 91 bool CheckReformat(); 92 93 void FillInFrameCache(const std::shared_ptr<Memory>& mem); 94 95 Status SendOutputBuffer(); 96 97 mutable OSAL::Mutex parameterMutex_ {}; 98 std::map<Tag, ValueType> audioParameter_ {}; 99 100 mutable OSAL::Mutex avMutex_ {}; 101 std::shared_ptr<const AVCodec> avCodec_ {nullptr}; 102 std::shared_ptr<AVCodecContext> avCodecContext_ {nullptr}; 103 std::shared_ptr<AVFrame> cachedFrame_ {nullptr}; 104 uint32_t fullInputFrameSize_ {0}; 105 std::shared_ptr<Buffer> outBuffer_ {nullptr}; 106 uint64_t prevPts_; 107 bool needReformat_ {false}; 108 AVSampleFormat srcFmt_ {AVSampleFormat::AV_SAMPLE_FMT_NONE}; 109 uint32_t srcBytesPerSample_ {0}; 110 DataCallback* dataCallback_ {nullptr}; 111 std::shared_ptr<Ffmpeg::Resample> resample_ {nullptr}; 112 mutable OSAL::Mutex bufferMetaMutex_ {}; 113 std::shared_ptr<BufferMeta> bufferMeta_ {nullptr}; 114 }; 115 } // Ffmpeg 116 } // namespace Plugin 117 } // namespace Media 118 } // namespace OHOS 119 120 #endif // HISTREAMER_AUDIO_FFMPEG_ENCODER_PLUGIN_H 121