• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <queue>
20 #include "audio_decoder.h"
21 #include "avcodec_audio_decoder.h"
22 
23 namespace OHOS {
24 namespace Sharing {
25 constexpr static uint32_t AUDIO_DECODE_WAIT_MILLISECONDS = 5000;
26 constexpr static uint32_t AUDIO_DECODE_DEFAULT_SAMPLERRATE = 48000;
27 constexpr static uint32_t AUDIO_DECODE_DEFAULT_CHANNEL_COUNT = 2;
28 constexpr static uint32_t ERROR_DECODER_INIT = -1;
29 
30 class AudioAvCodecDecoder : public AudioDecoder,
31                             public MediaAVCodec::AVCodecCallback {
32 public:
33     explicit AudioAvCodecDecoder();
34     virtual ~AudioAvCodecDecoder();
35 
36     bool Start() override;
37     void Stop() override;
38     void Release() override;
39     int32_t Init(const AudioTrack &audioTrack) override;
40     void OnFrame(const Frame::Ptr &frame) override;
41     bool SetDecoderFormat(const AudioTrack &audiotrack);
42     void OnOutputFormatChanged(const MediaAVCodec::Format &format) override;
43     void OnError(MediaAVCodec::AVCodecErrorType errorType, int32_t errorCode) override;
44     void OnInputBufferAvailable(uint32_t index, std::shared_ptr<MediaAVCodec::AVSharedMemory> buffer) override;
45     void OnOutputBufferAvailable(uint32_t index, MediaAVCodec::AVCodecBufferInfo info,
46                                  MediaAVCodec::AVCodecBufferFlag flag,
47                                  std::shared_ptr<MediaAVCodec::AVSharedMemory> buffer) override;
48     std::shared_ptr<MediaAVCodec::AVCodecAudioDecoder> GetDecoder();
49 
50 private:
51     bool InitDecoder();
52     bool StopDecoder();
53     bool StartDecoder();
54     bool SetAudioCallback();
55 
56 public:
57     std::queue<std::pair<int32_t, std::shared_ptr<MediaAVCodec::AVSharedMemory>>> inBufferQueue_;
58     std::mutex inputBufferMutex_;
59     std::mutex decoderMutex_;
60     std::condition_variable inCond_;
61     std::atomic_bool isRunning_ = false;
62     std::shared_ptr<MediaAVCodec::AVCodecAudioDecoder> audioDecoder_ = nullptr;
63     CodecId audioCodecId_ = CODEC_NONE;
64     bool isFirstFrame_ = true;
65 };
66 } // namespace Sharing
67 } // namespace OHOS
68 #endif