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 #include "audio_g711_codec.h"
17 #include "common/common_macro.h"
18 #include "frame.h"
19 #include "sharing_log.h"
20
21 namespace OHOS {
22 namespace Sharing {
AudioG711Encoder(G711_TYPE type)23 AudioG711Encoder::AudioG711Encoder(G711_TYPE type) : type_(type) {}
24
~AudioG711Encoder()25 AudioG711Encoder::~AudioG711Encoder() {}
26
Init(uint32_t channels,uint32_t sampleBit,uint32_t sampleRate)27 int32_t AudioG711Encoder::Init(uint32_t channels, uint32_t sampleBit, uint32_t sampleRate)
28 {
29 SHARING_LOGD("trace.");
30 inited_ = true;
31 return 0;
32 }
33
OnFrame(const Frame::Ptr & frame)34 void AudioG711Encoder::OnFrame(const Frame::Ptr &frame)
35 {
36 RETURN_IF_NULL(frame);
37 if (!inited_ && frame->GetCodecId() != CODEC_PCM) {
38 return;
39 }
40
41 auto payload = frame->Data();
42 int32_t outLength = frame->Size() / 2; // 2: double size
43 if ((int32_t)outBuffer_.size() != outLength) {
44 outBuffer_.resize(outLength);
45 }
46
47 if (Encode((int16_t *)payload, outLength, outBuffer_.data()) == -1) {
48 return;
49 }
50
51 auto g711Frame = FrameImpl::Create();
52 RETURN_IF_NULL(g711Frame);
53 g711Frame->codecId_ = type_ == G711_ALAW ? CODEC_G711A : CODEC_G711U;
54 g711Frame->Assign((char *)outBuffer_.data(), outBuffer_.size());
55 DeliverFrame(g711Frame);
56 }
57
Encode(int16_t * decoded,int32_t nSamples,uint8_t * encoded)58 int32_t AudioG711Encoder::Encode(int16_t *decoded, int32_t nSamples, uint8_t *encoded)
59 {
60 RETURN_INVALID_IF_NULL(decoded);
61 RETURN_INVALID_IF_NULL(encoded);
62 if (nSamples < 0) {
63 return -1;
64 }
65
66 return nSamples;
67 }
68
AudioG711Decoder(G711_TYPE type)69 AudioG711Decoder::AudioG711Decoder(G711_TYPE type) : type_(type) {}
70
~AudioG711Decoder()71 AudioG711Decoder::~AudioG711Decoder() {}
72
Init(const AudioTrack & audioTrack)73 int32_t AudioG711Decoder::Init(const AudioTrack &audioTrack)
74 {
75 SHARING_LOGD("trace.");
76 inited_ = true;
77 return 0;
78 }
79
OnFrame(const Frame::Ptr & frame)80 void AudioG711Decoder::OnFrame(const Frame::Ptr &frame)
81 {
82 RETURN_IF_NULL(frame);
83 if (!inited_) {
84 SHARING_LOGE("donot init!");
85 return;
86 }
87
88 if ((type_ == G711_ALAW && frame->GetCodecId() != CODEC_G711A) ||
89 (type_ == G711_ULAW && frame->GetCodecId() != CODEC_G711U)) {
90 SHARING_LOGE("codecId is invalid!");
91 return;
92 }
93
94 auto payload = frame->Data();
95 int32_t length = frame->Size();
96 if ((int32_t)outBuffer_.size() != length * 2) { // 2: double size
97 outBuffer_.resize(length * 2); // 2: double size
98 }
99
100 Decode((uint8_t *)payload, length, (int16_t *)outBuffer_.data());
101 auto pcmFrame = FrameImpl::Create();
102 RETURN_IF_NULL(pcmFrame);
103 pcmFrame->codecId_ = CODEC_PCM;
104 pcmFrame->Assign((char *)outBuffer_.data(), outBuffer_.size());
105 DeliverFrame(pcmFrame);
106 };
107
Decode(uint8_t * encoded,int32_t nSamples,int16_t * decoded)108 int32_t AudioG711Decoder::Decode(uint8_t *encoded, int32_t nSamples, int16_t *decoded)
109 {
110 RETURN_INVALID_IF_NULL(decoded);
111 RETURN_INVALID_IF_NULL(encoded);
112 if (nSamples < 0) {
113 return -1;
114 }
115
116 return nSamples;
117 }
118 } // namespace Sharing
119 } // namespace OHOS