• 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 
16 #ifndef AV_CODEC_ADIO_CODEC_WORKER_H
17 #define AV_CODEC_ADIO_CODEC_WORKER_H
18 
19 #include "audio_buffers_manager.h"
20 #include "audio_base_codec.h"
21 #include "avcodec_common.h"
22 #include "avcodec_errors.h"
23 #include "nocopyable.h"
24 #include "task_thread.h"
25 
26 #include <condition_variable>
27 #include <mutex>
28 #include <queue>
29 
30 namespace OHOS {
31 namespace MediaAVCodec {
32 class AudioCodecWorker : public NoCopyable {
33 public:
34     AudioCodecWorker(const std::shared_ptr<AudioBaseCodec> &codec, const std::shared_ptr<AVCodecCallback> &callback);
35 
36     ~AudioCodecWorker();
37 
38     bool PushInputData(const uint32_t &index);
39 
40     bool Start();
41 
42     bool Stop();
43 
44     bool Pause();
45 
46     bool Resume();
47 
48     bool Release();
49 
50     std::shared_ptr<AudioBuffersManager> GetInputBuffer() const noexcept;
51 
52     std::shared_ptr<AudioBuffersManager> GetOutputBuffer() const noexcept;
53 
54     std::shared_ptr<AudioBufferInfo> GetOutputBufferInfo(const uint32_t &index) const noexcept;
55 
56     std::shared_ptr<AudioBufferInfo> GetInputBufferInfo(const uint32_t &index) const noexcept;
57 
58 private:
59     void ProduceInputBuffer();
60     void ConsumerOutputBuffer();
61     void Dispose();
62     bool Begin();
63     bool HandInputBuffer(int32_t &ret);
64     void ReleaseOutputBuffer(const uint32_t &index, const int32_t &ret);
65     void SetFirstAndEosStatus(std::shared_ptr<AudioBufferInfo> &outBuffer, bool isEos, uint32_t index);
66     void ReleaseAllInBufferQueue();
67     void ReleaseAllInBufferAvaQueue();
68     void ResetTask();
69 
70 private:
71     bool isFirFrame_;
72     std::atomic<bool> isRunning;
73     std::shared_ptr<AudioBaseCodec> codec_;
74     int32_t inputBufferSize;
75     int32_t outputBufferSize;
76     const int16_t bufferCount;
77     const std::string_view name_;
78     std::mutex stateMutex_;
79     std::mutex inAvaMutex_;
80     std::mutex inputMutex_;
81     std::mutex outputMutex_;
82     std::condition_variable inputCondition_;
83     std::condition_variable outputCondition_;
84 
85     std::unique_ptr<TaskThread> inputTask_;
86     std::unique_ptr<TaskThread> outputTask_;
87     std::shared_ptr<AVCodecCallback> callback_;
88     std::shared_ptr<AudioBuffersManager> inputBuffer_;
89     std::shared_ptr<AudioBuffersManager> outputBuffer_;
90     std::queue<uint32_t> inBufIndexQue_;
91     std::queue<uint32_t> inBufAvaIndexQue_;
92 };
93 } // namespace MediaAVCodec
94 } // namespace OHOS
95 
96 #endif