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 Encode((int16_t *)payload, outLength, outBuffer_.data());
48
49 auto g711Frame = FrameImpl::Create();
50 RETURN_IF_NULL(g711Frame);
51 g711Frame->codecId_ = type_ == G711_ALAW ? CODEC_G711A : CODEC_G711U;
52 g711Frame->Assign((char *)outBuffer_.data(), outBuffer_.size());
53 DeliverFrame(g711Frame);
54 }
55
Encode(int16_t *decoded, int32_t nSamples, uint8_t *encoded)56 int32_t AudioG711Encoder::Encode(int16_t *decoded, int32_t nSamples, uint8_t *encoded)
57 {
58 RETURN_INVALID_IF_NULL(decoded);
59 RETURN_INVALID_IF_NULL(encoded);
60 if (nSamples < 0) {
61 return -1;
62 }
63
64 return nSamples;
65 }
66
AudioG711Decoder(G711_TYPE type)67 AudioG711Decoder::AudioG711Decoder(G711_TYPE type) : type_(type) {}
68
~AudioG711Decoder()69 AudioG711Decoder::~AudioG711Decoder() {}
70
Init()71 int32_t AudioG711Decoder::Init()
72 {
73 SHARING_LOGD("trace.");
74 inited_ = true;
75 return 0;
76 }
77
OnFrame(const Frame::Ptr &frame)78 void AudioG711Decoder::OnFrame(const Frame::Ptr &frame)
79 {
80 RETURN_IF_NULL(frame);
81 if (!inited_) {
82 SHARING_LOGE("donot init!");
83 return;
84 }
85
86 if ((type_ == G711_ALAW && frame->GetCodecId() != CODEC_G711A) ||
87 (type_ == G711_ULAW && frame->GetCodecId() != CODEC_G711U)) {
88 SHARING_LOGE("codecId is invalid!");
89 return;
90 }
91
92 auto payload = frame->Data();
93 int32_t length = frame->Size();
94 if ((int32_t)outBuffer_.size() != length * 2) { // 2: double size
95 outBuffer_.resize(length * 2); // 2: double size
96 }
97
98 Decode((uint8_t *)payload, length, (int16_t *)outBuffer_.data());
99 auto pcmFrame = FrameImpl::Create();
100 RETURN_IF_NULL(pcmFrame);
101 pcmFrame->codecId_ = CODEC_PCM;
102 pcmFrame->Assign((char *)outBuffer_.data(), outBuffer_.size());
103 DeliverFrame(pcmFrame);
104 };
105
Decode(uint8_t *encoded, int32_t nSamples, int16_t *decoded)106 int32_t AudioG711Decoder::Decode(uint8_t *encoded, int32_t nSamples, int16_t *decoded)
107 {
108 RETURN_INVALID_IF_NULL(decoded);
109 RETURN_INVALID_IF_NULL(encoded);
110 if (nSamples < 0) {
111 return -1;
112 }
113
114 return nSamples;
115 }
116 } // namespace Sharing
117 } // namespace OHOS