• 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 "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 != 1) {
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     if (auHeaderCount == 0) {
65         return;
66     }
67     if (!lastDts_) {
68         lastDts_ = stamp;
69     }
70 
71     auto dtsInc_ = (stamp - lastDts_) / static_cast<uint32_t>(auHeaderCount);
72     MEDIA_LOGD(
73         "RtpDecoderAAC::InputRtp seq: %{public}d payloadLen: %{public}zu  auHeaderCount: %{public}d stamp: %{public}d "
74         "dtsInc_: %{public}d\n.",
75         rtp->GetSeq(), rtp->GetPayloadSize(), auHeaderCount, stamp, dtsInc_);
76     if (dtsInc_ < 0 || dtsInc_ > 100) { // 100:unit
77         MEDIA_LOGD("abnormal timestamp.");
78         dtsInc_ = 0;
79     }
80 
81     for (int32_t i = 0; i <= auHeaderCount; ++i) {
82         uint16_t size = ((auHeaderPtr[0] << 8) | auHeaderPtr[1]) >> 3; // 8:byte offset, 3:byte offset
83 
84         if (ptr + size > end) {
85             break;
86         }
87 
88         if (size) {
89             frame_->Assign((char *)ptr, size);
90 
91             frame_->dts_ = lastDts_ + static_cast<uint32_t>(i) * dtsInc_;
92             ptr += size;
93             auHeaderPtr += 2; // 2:byte offset
94             FlushData();
95         }
96     }
97 
98     lastDts_ = stamp;
99 }
100 
SetOnFrame(const OnFrame & cb)101 void RtpDecoderAAC::SetOnFrame(const OnFrame &cb)
102 {
103     onFrame_ = cb;
104 }
105 
ObtainFrame()106 FrameImpl::Ptr RtpDecoderAAC::ObtainFrame()
107 {
108     auto frame = FrameImpl::Create();
109     frame->codecId_ = CODEC_AAC;
110     return frame;
111 }
112 
FlushData()113 void RtpDecoderAAC::FlushData()
114 {
115     if (frame_ == nullptr) {
116         return;
117     }
118 
119     auto ptr = frame_->Data();
120     if ((ptr[0] == 0xFF && (ptr[1] & 0xF0) == 0xF0) && frame_->Size() > ADTS_HEADER_LEN) {
121         frame_->prefixSize_ = ADTS_HEADER_LEN;
122     } else {
123         char adtsHeader[128] = {0};
124         auto size = AdtsHeader::DumpAacConfig(aacConfig_, frame_->Size(), (uint8_t *)adtsHeader, sizeof(adtsHeader));
125         if (size > 0) {
126             auto buff = std::make_shared<DataBuffer>();
127             buff->Assign(adtsHeader, size);
128             buff->Append(frame_->Data(), frame_->Size());
129             frame_->Clear();
130             frame_->ReplaceData(reinterpret_cast<const char *>(buff->Data()), buff->Size());
131             frame_->prefixSize_ = static_cast<size_t>(size);
132         }
133     }
134 
135     InputFrame(frame_);
136     frame_ = ObtainFrame();
137 }
138 
InputFrame(const Frame::Ptr & frame)139 void RtpDecoderAAC::InputFrame(const Frame::Ptr &frame)
140 {
141     RETURN_IF_NULL(frame);
142     if (!frame->PrefixSize()) {
143         return;
144     }
145 
146     auto ptr = frame->Data();
147     auto end = frame->Data() + frame->Size();
148     while (ptr < end) {
149         size_t frame_len = static_cast<size_t>(AdtsHeader::GetAacFrameLength((uint8_t *)ptr, end - ptr));
150         MEDIA_LOGD("aac frame len: %{public}zu   frame size: %{public}d.", frame_len, frame->Size());
151         if (frame_len < ADTS_HEADER_LEN) {
152             break;
153         }
154         if (frame_len == static_cast<size_t>(frame->Size())) {
155             onFrame_(frame);
156             return;
157         }
158         ptr += frame_len;
159         if (ptr > end) {
160             break;
161         }
162     }
163 }
164 
RtpEncoderAAC(uint32_t ssrc,uint32_t mtuSize,uint32_t sampleRate,uint8_t payloadType,uint16_t seq)165 RtpEncoderAAC::RtpEncoderAAC(uint32_t ssrc, uint32_t mtuSize, uint32_t sampleRate, uint8_t payloadType, uint16_t seq)
166     : RtpMaker(ssrc, mtuSize, payloadType, sampleRate, seq)
167 {
168     MEDIA_LOGD("trace.");
169 }
170 
~RtpEncoderAAC()171 RtpEncoderAAC::~RtpEncoderAAC() {}
172 
InputFrame(const Frame::Ptr & frame)173 void RtpEncoderAAC::InputFrame(const Frame::Ptr &frame)
174 {
175     RETURN_IF_NULL(frame);
176     MEDIA_LOGD("rtpEncoderAAC::InputFrame.");
177     auto stamp = frame->Dts();
178     auto data = frame->Data() + frame->PrefixSize();
179     auto len = (size_t)frame->Size() - frame->PrefixSize();
180     auto ptr = (char *)data;
181     auto remain_size = len;
182     auto max_size = GetMaxSize() - 4; // 4:avc start code size
183     while (remain_size > 0) {
184         if (remain_size <= max_size) {
185             sectionBuf_[0] = 0;
186             sectionBuf_[1] = 16;                                                 // 16:constants
187             sectionBuf_[2] = (len >> 5) & 0xFF;                                  // 5:byte offset,2:byte offset
188             sectionBuf_[3] = ((len & 0x1F) << 3) & 0xFF;                         // 3:byte offset
189             auto ret = memcpy_s(sectionBuf_ + 4, remain_size, ptr, remain_size); // 4:byte offset
190             if (ret != EOK) {
191                 MEDIA_LOGE("mem copy data failed.");
192                 break;
193             }
194             MakeAACRtp(sectionBuf_, remain_size + 4, true, stamp); // 4:byte offset
195             break;
196         }
197         sectionBuf_[0] = 0;
198         sectionBuf_[1] = 16;                                           // 16:byte offset
199         sectionBuf_[2] = ((len) >> 5) & 0xFF;                          // 2:byte offset, 5:byte offset
200         sectionBuf_[3] = ((len & 0x1F) << 3) & 0xFF;                   // 3:byte offset
201         auto ret = memcpy_s(sectionBuf_ + 4, max_size, ptr, max_size); // 4:byte offset
202         if (ret != EOK) {
203             MEDIA_LOGE("mem copy data failed.");
204             break;
205         }
206         MakeAACRtp(sectionBuf_, max_size + 4, false, stamp); // 4:byte offset
207         ptr += max_size;
208         remain_size -= max_size;
209     }
210 }
211 
SetOnRtpPack(const OnRtpPack & cb)212 void RtpEncoderAAC::SetOnRtpPack(const OnRtpPack &cb)
213 {
214     onRtpPack_ = cb;
215 }
216 
MakeAACRtp(const void * data,size_t len,bool mark,uint32_t stamp)217 void RtpEncoderAAC::MakeAACRtp(const void *data, size_t len, bool mark, uint32_t stamp)
218 {
219     MEDIA_LOGD("rtpEncoderAAC::MakeAACRtp len: %{public}zu, stamp: %{public}u.", len, stamp);
220     RETURN_IF_NULL(data);
221     auto rtp = MakeRtp(data, len, mark, stamp);
222     if (onRtpPack_) {
223         onRtpPack_(rtp);
224     }
225 }
226 } // namespace Sharing
227 } // namespace OHOS