1 /* 2 * Copyright (c) 2025-2025 Shenzhen Kaihong Digital Industry Development 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 OHOS_SHARING_AUDIO_AVCODEC_DECODER_H 17 #define OHOS_SHARING_AUDIO_AVCODEC_DECODER_H 18 19 #include <chrono> 20 #include <queue> 21 #include "audio_decoder.h" 22 #include "avcodec_audio_decoder.h" 23 24 namespace OHOS { 25 namespace Sharing { 26 constexpr static uint32_t AUDIO_DECODE_WAIT_MILLISECONDS = 5000; 27 constexpr static uint32_t AUDIO_DECODE_DEFAULT_SAMPLERATE = 48000; 28 constexpr static uint32_t AUDIO_DECODE_DEFAULT_CHANNEL_COUNT = 2; 29 constexpr static uint32_t ERROR_DECODER_INIT = -1; 30 constexpr static uint32_t MAX_BUFFER_SIZE = 30; 31 constexpr static uint32_t NEXT_FRAME_WAIT_TIME = 20; 32 constexpr static int32_t AUDIO_DECODE_DROP_INTERVAL = 2000 * 1000; 33 constexpr static int32_t NO_AUDIO_FRAME_INTERVAL = 300 * 1000 * 1000; 34 35 class AudioAvCodecDecoder : public AudioDecoder, 36 public MediaAVCodec::AVCodecCallback { 37 public: 38 explicit AudioAvCodecDecoder(); 39 virtual ~AudioAvCodecDecoder(); 40 41 bool Start() override; 42 void Stop() override; 43 void Release() override; 44 int32_t Init(const AudioTrack &audioTrack) override; 45 void OnFrame(const Frame::Ptr &frame) override; 46 bool SetDecoderFormat(const AudioTrack &audiotrack); 47 void OnOutputFormatChanged(const MediaAVCodec::Format &format) override; 48 void OnError(MediaAVCodec::AVCodecErrorType errorType, int32_t errorCode) override; 49 void OnInputBufferAvailable(uint32_t index, std::shared_ptr<MediaAVCodec::AVSharedMemory> buffer) override; 50 void OnOutputBufferAvailable(uint32_t index, MediaAVCodec::AVCodecBufferInfo info, 51 MediaAVCodec::AVCodecBufferFlag flag, 52 std::shared_ptr<MediaAVCodec::AVSharedMemory> buffer) override; 53 int64_t GetDecoderTimestamp() override; 54 void DropOneFrame() override; 55 std::shared_ptr<MediaAVCodec::AVCodecAudioDecoder> GetDecoder(); 56 57 private: 58 bool InitDecoder(); 59 bool IsNeedDropFrame(int64_t nowTimeUs); 60 bool ReleaseOutputBuffer(uint32_t index); 61 bool StopDecoder(); 62 bool StartDecoder(); 63 bool SetAudioCallback(); 64 bool StartRender(); 65 bool StopRender(); 66 void ClearRenderBufferQueue(); 67 void RenderOutBuffer(); 68 void Render(std::shared_ptr<FrameImpl> frameBuffer); 69 70 public: 71 std::queue<std::pair<int32_t, std::shared_ptr<MediaAVCodec::AVSharedMemory>>> inBufferQueue_; 72 std::mutex inputBufferMutex_; 73 std::mutex decoderMutex_; 74 std::mutex renderBufferMutex_; 75 std::condition_variable inCond_; 76 std::atomic_bool isRunning_ = false; 77 std::atomic_bool isForceDrop_ = false; 78 std::shared_ptr<MediaAVCodec::AVCodecAudioDecoder> audioDecoder_ = nullptr; 79 CodecId audioCodecId_ = CODEC_NONE; 80 81 private: 82 int64_t firstTimestampUs_{0}; 83 int64_t lastPlayPts_{0}; 84 85 std::atomic<bool> isRenderReady_{false}; 86 std::atomic<int64_t> audioLatency_{0 * 1000}; 87 std::atomic<int64_t> lastDropTimeUs_{0}; 88 std::condition_variable renderCond_; 89 std::thread renderThread_; 90 std::queue<std::shared_ptr<FrameImpl>> renderBuffer_; 91 }; 92 } // namespace Sharing 93 } // namespace OHOS 94 #endif