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