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