• 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_packet.h"
17 #include <arpa/inet.h>
18 #include <cstdlib>
19 
20 namespace OHOS {
21 namespace Sharing {
22 #define AV_RB16(x) ((((const uint8_t *)(x))[0] << 8) | ((const uint8_t *)(x))[1])
23 
GetCsrcSize() const24 size_t RtpHeader::GetCsrcSize() const
25 {
26     return csrc_ << 2; // 2:byte offset
27 }
28 
GetCsrcData()29 uint8_t *RtpHeader::GetCsrcData()
30 {
31     if (!csrc_) {
32         return nullptr;
33     }
34 
35     return &payload_;
36 }
37 
GetExtSize() const38 size_t RtpHeader::GetExtSize() const
39 {
40     if (!ext_) {
41         return 0;
42     }
43     auto ext_ptr = &payload_ + GetCsrcSize();
44 
45     return AV_RB16(ext_ptr + 2) << 2; // 2:byte offset
46 }
47 
GetExtReserved() const48 uint16_t RtpHeader::GetExtReserved() const
49 {
50     if (!ext_) {
51         return 0;
52     }
53     auto ext_ptr = &payload_ + GetCsrcSize();
54     return AV_RB16(ext_ptr);
55 }
56 
GetExtData()57 uint8_t *RtpHeader::GetExtData()
58 {
59     if (!ext_) {
60         return nullptr;
61     }
62     auto ext_ptr = &payload_ + GetCsrcSize();
63 
64     return ext_ptr + 4; // 4:byte offset
65 }
66 
GetPayloadOffset() const67 size_t RtpHeader::GetPayloadOffset() const
68 {
69     return GetCsrcSize() + (ext_ ? (4 + GetExtSize()) : 0); // 4:byte offset
70 }
71 
GetPayloadData()72 uint8_t *RtpHeader::GetPayloadData()
73 {
74     return &payload_ + GetPayloadOffset();
75 }
76 
GetPaddingSize(size_t rtp_size) const77 size_t RtpHeader::GetPaddingSize(size_t rtp_size) const
78 {
79     if (!padding_) {
80         return 0;
81     }
82     auto end = (uint8_t *)this + rtp_size - 1;
83     return *end;
84 }
85 
GetPayloadSize(size_t rtp_size) const86 size_t RtpHeader::GetPayloadSize(size_t rtp_size) const
87 {
88     auto invalid_size = GetPayloadOffset() + GetPaddingSize(rtp_size);
89     if (invalid_size + RtpPacket::RTP_HEADER_SIZE >= rtp_size) {
90         return 0;
91     }
92 
93     return rtp_size - invalid_size - RtpPacket::RTP_HEADER_SIZE;
94 }
95 
GetHeader()96 RtpHeader *RtpPacket::GetHeader()
97 {
98     return (RtpHeader *)Data();
99 }
100 
GetSeq()101 uint16_t RtpPacket::GetSeq()
102 {
103     return ntohs(GetHeader()->seq_);
104 }
105 
GetStamp()106 uint32_t RtpPacket::GetStamp()
107 {
108     return ntohl(GetHeader()->stamp_);
109 }
110 
GetStampMS()111 uint32_t RtpPacket::GetStampMS()
112 {
113     return GetStamp() * uint64_t(1000) / sampleRate_; // 1000:unit
114 }
115 
GetSSRC()116 uint32_t RtpPacket::GetSSRC()
117 {
118     return ntohl(GetHeader()->ssrc_);
119 }
120 
GetPayload()121 uint8_t *RtpPacket::GetPayload()
122 {
123     return GetHeader()->GetPayloadData();
124 }
125 
GetPayloadSize()126 size_t RtpPacket::GetPayloadSize()
127 {
128     return GetHeader()->GetPayloadSize(Size());
129 }
130 } // namespace Sharing
131 } // namespace OHOS