1 /* 2 * Copyright (c) 2022-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 OHOS_AUDIO_ENCODER_H 17 #define OHOS_AUDIO_ENCODER_H 18 19 #include <atomic> 20 #include <mutex> 21 #include <queue> 22 #include <thread> 23 24 #include "avcodec_common.h" 25 #include "avcodec_audio_encoder.h" 26 #include "format.h" 27 28 #include "audio_data.h" 29 #include "audio_event.h" 30 #include "audio_param.h" 31 #include "iaudio_codec.h" 32 #include "iaudio_codec_callback.h" 33 34 namespace OHOS { 35 namespace DistributedHardware { 36 class AudioEncoder : public IAudioCodec, public std::enable_shared_from_this<AudioEncoder> { 37 public: 38 AudioEncoder() = default; 39 ~AudioEncoder() override; 40 41 int32_t ConfigureAudioCodec(const AudioCommonParam &codecParam, 42 const std::shared_ptr<IAudioCodecCallback> &codecCallback) override; 43 int32_t ReleaseAudioCodec() override; 44 int32_t StartAudioCodec() override; 45 int32_t StopAudioCodec() override; 46 int32_t FeedAudioData(const std::shared_ptr<AudioData> &inputData) override; 47 48 void OnInputBufferAvailable(uint32_t index); 49 void OnOutputBufferAvailable(uint32_t index, Media::AVCodecBufferInfo info, Media::AVCodecBufferFlag flag); 50 void OnOutputFormatChanged(const Media::Format &format); 51 void OnError(const AudioEvent &event); 52 53 private: 54 int32_t InitAudioEncoder(const AudioCommonParam &codecParam); 55 int32_t SetEncoderFormat(const AudioCommonParam &codecParam); 56 bool IsInEncodeRange(const AudioCommonParam &codecParam); 57 void StartInputThread(); 58 void StopInputThread(); 59 void IncreaseWaitEncodeCnt(); 60 void ReduceWaitEncodeCnt(); 61 void InputEncodeAudioData(); 62 int32_t ProcessData(const std::shared_ptr<AudioData> &audioData, const int32_t bufferIndex); 63 int64_t GetEncoderTimeStamp(); 64 int32_t EncodeDone(const std::shared_ptr<AudioData> &outputData); 65 66 private: 67 constexpr static int32_t AUDIO_ENCODER_QUEUE_MAX = 100; 68 constexpr static uint32_t ENCODE_WAIT_MILLISECONDS = 50; 69 constexpr static int32_t INVALID_MEMORY_SIZE = -1; 70 constexpr static int32_t CHANNEL_MASK_MIN = 1; 71 constexpr static int32_t CHANNEL_MASK_MAX = 2; 72 constexpr static int32_t SAMPLE_RATE_MIN = 8000; 73 constexpr static int32_t SAMPLE_RATE_MAX = 96000; 74 const static std::string ENCODE_MIME_AAC; 75 static constexpr const char* ENCODE_THREAD = "encodeThread"; 76 77 std::mutex mtxData_; 78 std::mutex mtxCnt_; 79 std::thread encodeThread_; 80 std::condition_variable encodeCond_; 81 82 std::atomic<bool> isEncoderRunning_ = false; 83 int64_t firstInputTimeUs_ = 0; 84 int64_t inputTimeStampUs_ = 0; 85 int64_t outputTimeStampUs_ = 0; 86 int32_t waitOutputCount_ = 0; 87 88 Media::Format cfgFormat_; 89 Media::Format outputFormat_; 90 AudioCommonParam codecParam_; 91 std::weak_ptr<IAudioCodecCallback> codecCallback_; 92 std::shared_ptr<Media::AVCodecAudioEncoder> audioEncoder_ = nullptr; 93 std::shared_ptr<Media::AVCodecCallback> encoderCallback_ = nullptr; 94 std::queue<std::shared_ptr<AudioData>> inputBufQueue_; 95 std::queue<uint32_t> bufIndexQueue_; 96 }; 97 } // namespace DistributedHardware 98 } // namespace OHOS 99 #endif // OHOS_AUDIO_ENCODER_H 100