• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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 AVCODEC_AUDIO_CODEC_IMPL_H
16 #define AVCODEC_AUDIO_CODEC_IMPL_H
17 
18 #include "avcodec_audio_codec.h"
19 #include "nocopyable.h"
20 #include "task_thread.h"
21 #include "i_avcodec_service.h"
22 #include "meta/meta.h"
23 #include "buffer/avbuffer.h"
24 #include "buffer/avbuffer_queue.h"
25 #include "buffer/avbuffer_queue_consumer.h"
26 #include "buffer/avbuffer_queue_define.h"
27 #include "buffer/avbuffer_queue_producer.h"
28 
29 namespace OHOS {
30 namespace MediaAVCodec {
31 class AVCodecAudioCodecImpl {
32 public:
33     AVCodecAudioCodecImpl();
34     ~AVCodecAudioCodecImpl();
35 
36     int32_t Configure(const Format &format);
37     int32_t Prepare();
38     int32_t Start();
39     int32_t Stop();
40     int32_t Flush();
41     int32_t Reset();
42     int32_t Release();
43     int32_t QueueInputBuffer(uint32_t index);
44     int32_t GetOutputFormat(Format &format);
45     int32_t ReleaseOutputBuffer(uint32_t index);
46     int32_t SetParameter(const Format &format);
47     int32_t SetCallback(const std::shared_ptr<MediaCodecCallback> &callback);
48     int32_t Init(AVCodecType type, bool isMimeType, const std::string &name);
49     void Notify();
50 
51 private:
52     void ProduceInputBuffer();
53     void ConsumerOutputBuffer();
54     int32_t GetInputBufferSize();
55     void ClearCache();
56     void StopTask();
57     void PauseTask();
58 
59 private:
60     std::atomic<bool> isRunning_;
61     std::shared_ptr<ICodecService> codecService_ = nullptr;
62     std::shared_ptr<Media::AVBufferQueue> implBufferQueue_;
63     std::unique_ptr<TaskThread> inputTask_;
64     std::unique_ptr<TaskThread> outputTask_;
65     std::shared_ptr<MediaCodecCallback> callback_;
66     std::condition_variable inputCondition_;
67     std::condition_variable outputCondition_;
68     std::mutex inputMutex_;
69     std::mutex inputMutex2_;
70     std::mutex outputMutex_;
71     std::mutex outputMutex_2;
72     std::atomic<int32_t> bufferConsumerAvailableCount_ = 0;
73     std::atomic<int32_t> indexInput_ = 0;
74     std::atomic<int32_t> indexOutput_ = 0;
75     std::unordered_map<uint32_t, std::shared_ptr<AVBuffer>> inputBufferObjMap_;
76     std::unordered_map<uint32_t, std::shared_ptr<AVBuffer>> outputBufferObjMap_;
77     sptr<Media::AVBufferQueueProducer> mediaCodecProducer_;
78     sptr<Media::AVBufferQueueProducer> implProducer_;
79     sptr<Media::AVBufferQueueConsumer> implConsumer_;
80 };
81 
82 class AudioCodecConsumerListener : public Media::IConsumerListener {
83 public:
84     explicit AudioCodecConsumerListener(AVCodecAudioCodecImpl *impl);
85     void OnBufferAvailable() override;
86 private:
87     AVCodecAudioCodecImpl *impl_;
88 };
89 
90 class AudioCodecProducerListener : public IRemoteStub<Media::IProducerListener> {
91 public:
92     explicit AudioCodecProducerListener(AVCodecAudioCodecImpl *impl);
93     void OnBufferAvailable() override;
94 private:
95     AVCodecAudioCodecImpl *impl_;
96     bool isCousumer_;
97 };
98 
99 } // namespace MediaAVCodec
100 } // namespace OHOS
101 #endif // AVCODEC_AUDIO_CODEC_IMPL_H