1 /*
2  * Copyright (c) 2024-2024 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 AVCODEC_SAMPLE_AUDIO_ENCODER_H
17 #define AVCODEC_SAMPLE_AUDIO_ENCODER_H
18 
19 #include "native_avcodec_audiocodec.h"
20 #include "sample_info.h"
21 #include "audio_record.h"
22 
23 namespace OHOS {
24 namespace CameraStandard {
25 class AudioEncoder {
26 public:
27     explicit AudioEncoder();
28     ~AudioEncoder();
29 
30     int32_t Create(const std::string &codecMime);
31     int32_t Config();
32     int32_t Start();
33     bool EncodeAudioBuffer(vector<sptr<AudioRecord>> audioRecords);
34     int32_t PushInputData(sptr<CodecAVBufferInfo> info);
35     int32_t FreeOutputData(uint32_t bufferIndex);
36     int32_t Stop();
37     int32_t Release();
38 
39 private:
40     int32_t SetCallback(sptr<CodecUserData> codecUserData);
41     void RestartAudioCodec();
42     int32_t Configure();
43     bool EnqueueBuffer(sptr<AudioRecord> audioRecord);
44     bool EncodeAudioBuffer(sptr<AudioRecord> audioRecord);
45     std::atomic<bool> isStarted_ { false };
46     std::atomic<bool> isEncoding_ { false };
47     std::mutex encoderMutex_;
48     OH_AVCodec *encoder_ = nullptr;
49     sptr<CodecUserData> context_ = nullptr;
50     std::mutex serialMutex_;
51 };
52 } // CameraStandard
53 } // OHOS
54 #endif // AVCODEC_SAMPLE_AUDIO_ENCODER_H