• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-2024 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_AAC_CODEC_H
17 #define OHOS_SHARING_AUDIO_AAC_CODEC_H
18 
19 #include <memory>
20 #include <stdint.h>
21 #include <string.h>
22 #include "audio_decoder.h"
23 #include "audio_encoder.h"
24 #include "media_frame_pipeline.h"
25 #include "protocol/frame/h264_frame.h"
26 #include "utils/data_buffer.h"
27 
28 extern "C" {
29 #include <libavcodec/avcodec.h>
30 #include <libavformat/avformat.h>
31 #include <libavutil/audio_fifo.h>
32 #include <libavutil/channel_layout.h>
33 #include <libswresample/swresample.h>
34 };
35 
36 namespace OHOS {
37 namespace Sharing {
38 class AudioAACDecoder : public AudioDecoder {
39 public:
40     AudioAACDecoder();
41     ~AudioAACDecoder();
42 
43     int32_t Init() override;
44     void OnFrame(const Frame::Ptr &frame) override;
45 
46 private:
47     uint8_t *swrOutBuffer_ = nullptr;
48     int32_t swrOutBufferSize_ = 0;
49 
50     std::unique_ptr<DataBuffer> buffer_;
51 
52     AVFrame *avFrame_ = nullptr;
53     AVPacket *avPacket_ = nullptr;
54     AVCodecContext *codecCtx_ = nullptr;
55     SwrContext *swrContext_ = nullptr;
56 };
57 
58 class AudioAACEncoder : public AudioEncoder {
59 public:
60     AudioAACEncoder();
61     ~AudioAACEncoder();
62 
63     int32_t Init(uint32_t channels = 2, uint32_t sampleBit = 16, uint32_t sampleRate = 44100) override;
64     void OnFrame(const Frame::Ptr &frame) override ;
65 private:
66     int InitSwr();
67     void DoSwr(const Frame::Ptr &frame);
68     int AddSamplesToFifo(uint8_t **samples, int frame_size);
69     void InitEncoderCtx(uint32_t channels, uint32_t sampleBit, uint32_t sampleRate);
70 private:
71     uint32_t inChannels_ = 2;
72     uint32_t inSampleBit_ = 16;
73     uint32_t inSampleRate_ = 44100;
74 
75     std::unique_ptr<DataBuffer> buffer_;
76 
77     AVCodecContext *enc_ = nullptr;
78     AVFrame *encFrame_ = nullptr;
79     AVPacket *encPacket_ = nullptr;
80 
81     SwrContext *swr_ = nullptr;
82     //buffer for swr out put
83     uint8_t **swrData_;
84     AVAudioFifo *fifo_ = nullptr;
85 
86     int64_t nextOutPts_;
87     uint8_t* outBuffer_ = nullptr;
88 };
89 
90 } // namespace Sharing
91 } // namespace OHOS
92 #endif
93