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