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_maker.h"
17 #include <arpa/inet.h>
18 #include <securec.h>
19 #include "common/common_macro.h"
20
21 namespace OHOS {
22 namespace Sharing {
RtpMaker(uint32_t ssrc,size_t mtuSize,uint8_t payloadType,uint32_t sampleRate,uint16_t seq)23 RtpMaker::RtpMaker(uint32_t ssrc, size_t mtuSize, uint8_t payloadType, uint32_t sampleRate, uint16_t seq)
24 : pt_(payloadType), seq_(seq), ssrc_(ssrc), sampleRate_(sampleRate), mtuSize_(mtuSize)
25 {
26 if (ssrc_ == 0) {
27 ssrc_ = ((uint64_t)this) & 0xFFFFFFFF;
28 }
29 }
30
~RtpMaker()31 RtpMaker::~RtpMaker() {}
32
GetMaxSize() const33 size_t RtpMaker::GetMaxSize() const
34 {
35 return mtuSize_ - RtpPacket::RTP_HEADER_SIZE;
36 }
37
GetSsrc() const38 uint32_t RtpMaker::GetSsrc() const
39 {
40 return ssrc_;
41 }
42
MakeRtp(const void * data,size_t len,bool mark,uint32_t stamp)43 RtpPacket::Ptr RtpMaker::MakeRtp(const void *data, size_t len, bool mark, uint32_t stamp)
44 {
45 if (data == nullptr) {
46 return nullptr;
47 }
48
49 uint16_t size = (uint16_t)(len + RtpPacket::RTP_HEADER_SIZE);
50 auto rtp = std::make_shared<RtpPacket>();
51 rtp->SetCapacity(size);
52 rtp->SetSize(size);
53
54 auto header = rtp->GetHeader();
55 header->version_ = RtpPacket::RTP_VERSION;
56 header->padding_ = 0;
57 header->ext_ = 0;
58 header->csrc_ = 0;
59 header->mark_ = mark;
60 header->pt_ = pt_;
61 header->seq_ = htons(seq_);
62 ++seq_;
63 header->stamp_ = htonl(uint64_t(stamp) * (sampleRate_ / 1000)); // 1000:unit
64 header->ssrc_ = htonl(ssrc_);
65
66 if (data) {
67 auto rtpData = rtp->Data();
68
69 auto ret = memcpy_s(rtpData + RtpPacket::RTP_HEADER_SIZE, len, data, len);
70 if (ret != EOK) {
71 return nullptr;
72 }
73 }
74
75 return rtp;
76 }
77 } // namespace Sharing
78 } // namespace OHOS