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 "rtp_codec_aac.h"
17 #include <memory>
18 #include <securec.h>
19 #include "adts.h"
20 #include "common/common_macro.h"
21 #include "common/media_log.h"
22
23 namespace OHOS {
24 namespace Sharing {
RtpDecoderAAC(const RtpPlaylodParam & rpp)25 RtpDecoderAAC::RtpDecoderAAC(const RtpPlaylodParam &rpp) : rpp_(rpp)
26 {
27 MEDIA_LOGD("trace.");
28 frame_ = ObtainFrame();
29 auto aacExtra = std::static_pointer_cast<AACExtra>(rpp_.extra_);
30 if (aacExtra && !aacExtra->aacConfig_.empty()) {
31 for (size_t i = 0; i < aacExtra->aacConfig_.size() / 2; ++i) { // 2:unit
32 uint32_t cfg;
33 auto ret = sscanf_s(aacExtra->aacConfig_.substr(i * 2, 2).data(), "%02X", &cfg); // 2:unit
34 if (ret != EOK) {
35 MEDIA_LOGE("sscanf_s failed.");
36 }
37 cfg &= 0x00FF;
38 aacConfig_.push_back((char)cfg);
39 }
40 }
41 MEDIA_LOGD("aacConfig_: %{public}s.", aacConfig_.c_str());
42 }
43
~RtpDecoderAAC()44 RtpDecoderAAC::~RtpDecoderAAC() {}
45
InputRtp(const RtpPacket::Ptr & rtp)46 void RtpDecoderAAC::InputRtp(const RtpPacket::Ptr &rtp)
47 {
48 RETURN_IF_NULL(rtp);
49 MEDIA_LOGD("rtpDecoderAAC::InputRtp.");
50 auto stamp = rtp->GetStampMS();
51
52 auto ptr = rtp->GetPayload();
53
54 auto end = ptr + rtp->GetPayloadSize();
55
56 auto auHeaderCount = ((ptr[0] << 8) | ptr[1]) >> 4; // 8:byte offset, 4:byte offset
57
58 auto auHeaderPtr = ptr + 2; // 2:unit
59 ptr = auHeaderPtr + auHeaderCount * 2; // 2:unit
60
61 if (end < ptr) {
62 return;
63 }
64
65 if (!lastDts_) {
66 lastDts_ = stamp;
67 }
68
69 auto dtsInc_ = (stamp - lastDts_) / static_cast<uint32_t>(auHeaderCount);
70 MEDIA_LOGD(
71 "RtpDecoderAAC::InputRtp seq: %{public}d payloadLen: %{public}zu auHeaderCount: %{public}d stamp: %{public}d "
72 "dtsInc_: %{public}d\n.",
73 rtp->GetSeq(), rtp->GetPayloadSize(), auHeaderCount, stamp, dtsInc_);
74 if (dtsInc_ < 0 && dtsInc_ > 100) { // 100:unit
75 MEDIA_LOGD("abnormal timestamp.");
76 dtsInc_ = 0;
77 }
78
79 for (int32_t i = 0; i <= auHeaderCount; ++i) {
80 uint16_t size = ((auHeaderPtr[0] << 8) | auHeaderPtr[1]) >> 3; // 8:byte offset, 3:byte offset
81
82 if (ptr + size > end) {
83 break;
84 }
85
86 if (size) {
87 frame_->Assign((char *)ptr, size);
88
89 frame_->dts_ = lastDts_ + static_cast<uint32_t>(i) * dtsInc_;
90 ptr += size;
91 auHeaderPtr += 2; // 2:byte offset
92 FlushData();
93 }
94 }
95
96 lastDts_ = stamp;
97 }
98
SetOnFrame(const OnFrame & cb)99 void RtpDecoderAAC::SetOnFrame(const OnFrame &cb)
100 {
101 onFrame_ = cb;
102 }
103
ObtainFrame()104 FrameImpl::Ptr RtpDecoderAAC::ObtainFrame()
105 {
106 auto frame = FrameImpl::Create();
107 frame->codecId_ = CODEC_AAC;
108 return frame;
109 }
110
FlushData()111 void RtpDecoderAAC::FlushData()
112 {
113 if (frame_ == nullptr) {
114 return;
115 }
116
117 auto ptr = frame_->Data();
118 if ((ptr[0] == 0xFF && (ptr[1] & 0xF0) == 0xF0) && frame_->Size() > ADTS_HEADER_LEN) {
119 frame_->prefixSize_ = ADTS_HEADER_LEN;
120 } else {
121 char adtsHeader[128] = {0};
122 auto size = AdtsHeader::DumpAacConfig(aacConfig_, frame_->Size(), (uint8_t *)adtsHeader, sizeof(adtsHeader));
123 if (size > 0) {
124 auto buff = std::make_shared<DataBuffer>();
125 buff->Assign(adtsHeader, size);
126 buff->Append(frame_->Data(), frame_->Size());
127 frame_->Clear();
128 frame_->ReplaceData(reinterpret_cast<const char *>(buff->Data()), buff->Size());
129 frame_->prefixSize_ = static_cast<size_t>(size);
130 }
131 }
132
133 InputFrame(frame_);
134 frame_ = ObtainFrame();
135 }
136
InputFrame(const Frame::Ptr & frame)137 void RtpDecoderAAC::InputFrame(const Frame::Ptr &frame)
138 {
139 RETURN_IF_NULL(frame);
140 if (!frame->PrefixSize()) {
141 return;
142 }
143
144 auto ptr = frame->Data();
145 auto end = frame->Data() + frame->Size();
146 while (ptr < end) {
147 size_t frame_len = static_cast<size_t>(AdtsHeader::GetAacFrameLength((uint8_t *)ptr, end - ptr));
148 MEDIA_LOGD("aac frame len: %{public}zu frame size: %{public}d.", frame_len, frame->Size());
149 if (frame_len < ADTS_HEADER_LEN) {
150 break;
151 }
152 if (frame_len == static_cast<size_t>(frame->Size())) {
153 onFrame_(frame);
154 return;
155 }
156 ptr += frame_len;
157 if (ptr > end) {
158 break;
159 }
160 }
161 }
162
RtpEncoderAAC(uint32_t ssrc,uint32_t mtuSize,uint32_t sampleRate,uint8_t payloadType,uint16_t seq)163 RtpEncoderAAC::RtpEncoderAAC(uint32_t ssrc, uint32_t mtuSize, uint32_t sampleRate, uint8_t payloadType, uint16_t seq)
164 : RtpMaker(ssrc, mtuSize, payloadType, sampleRate, seq)
165 {
166 MEDIA_LOGD("trace.");
167 }
168
~RtpEncoderAAC()169 RtpEncoderAAC::~RtpEncoderAAC() {}
170
InputFrame(const Frame::Ptr & frame)171 void RtpEncoderAAC::InputFrame(const Frame::Ptr &frame)
172 {
173 RETURN_IF_NULL(frame);
174 MEDIA_LOGD("rtpEncoderAAC::InputFrame.");
175 auto stamp = frame->Dts();
176 auto data = frame->Data() + frame->PrefixSize();
177 auto len = (size_t)frame->Size() - frame->PrefixSize();
178 auto ptr = (char *)data;
179 auto remain_size = len;
180 auto max_size = GetMaxSize() - 4; // 4:avc start code size
181 while (remain_size > 0) {
182 if (remain_size <= max_size) {
183 sectionBuf_[0] = 0;
184 sectionBuf_[1] = 16; // 16:constants
185 sectionBuf_[2] = (len >> 5) & 0xFF; // 5:byte offset,2:byte offset
186 sectionBuf_[3] = ((len & 0x1F) << 3) & 0xFF; // 3:byte offset
187 auto ret = memcpy_s(sectionBuf_ + 4, remain_size, ptr, remain_size); // 4:byte offset
188 if (ret != EOK) {
189 MEDIA_LOGE("mem copy data failed.");
190 break;
191 }
192 MakeAACRtp(sectionBuf_, remain_size + 4, true, stamp); // 4:byte offset
193 break;
194 }
195 sectionBuf_[0] = 0;
196 sectionBuf_[1] = 16; // 16:byte offset
197 sectionBuf_[2] = ((len) >> 5) & 0xFF; // 2:byte offset, 5:byte offset
198 sectionBuf_[3] = ((len & 0x1F) << 3) & 0xFF; // 3:byte offset
199 auto ret = memcpy_s(sectionBuf_ + 4, max_size, ptr, max_size); // 4:byte offset
200 if (ret != EOK) {
201 MEDIA_LOGE("mem copy data failed.");
202 break;
203 }
204 MakeAACRtp(sectionBuf_, max_size + 4, false, stamp); // 4:byte offset
205 ptr += max_size;
206 remain_size -= max_size;
207 }
208 }
209
SetOnRtpPack(const OnRtpPack & cb)210 void RtpEncoderAAC::SetOnRtpPack(const OnRtpPack &cb)
211 {
212 onRtpPack_ = cb;
213 }
214
MakeAACRtp(const void * data,size_t len,bool mark,uint32_t stamp)215 void RtpEncoderAAC::MakeAACRtp(const void *data, size_t len, bool mark, uint32_t stamp)
216 {
217 MEDIA_LOGD("rtpEncoderAAC::MakeAACRtp len: %{public}zu, stamp: %{public}d.", len, stamp);
218 RETURN_IF_NULL(data);
219 auto rtp = MakeRtp(data, len, mark, stamp);
220 if (onRtpPack_) {
221 onRtpPack_(rtp);
222 }
223 }
224 } // namespace Sharing
225 } // namespace OHOS