OLD | NEW |
---|---|
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "remoting/protocol/rtp_utils.h" | 5 #include "remoting/protocol/rtp_utils.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "third_party/libjingle/source/talk/base/byteorder.h" | 8 #include "third_party/libjingle/source/talk/base/byteorder.h" |
9 | 9 |
10 using talk_base::GetBE16; | 10 using talk_base::GetBE16; |
11 using talk_base::GetBE32; | 11 using talk_base::GetBE32; |
12 using talk_base::SetBE16; | 12 using talk_base::SetBE16; |
13 using talk_base::SetBE32; | 13 using talk_base::SetBE32; |
14 | 14 |
15 namespace remoting { | 15 namespace remoting { |
16 namespace protocol { | 16 namespace protocol { |
17 | 17 |
18 namespace { | 18 namespace { |
19 const int kRtpBaseHeaderSize = 12; | 19 const int kRtpBaseHeaderSize = 12; |
20 const uint8 kRtpVersionNumber = 2; | 20 const uint8 kRtpVersionNumber = 2; |
21 const int kRtpMaxSources = 16; | 21 const int kRtpMaxSources = 16; |
22 } // namespace | 22 } // namespace |
23 | 23 |
24 int GetRtpHeaderSize(int sources) { | 24 static inline uint8 ExtractBits(uint8 byte, int bits_count, int shift) { |
25 DCHECK_GE(sources, 0); | 25 return (byte >> shift) & ((1 << bits_count) - 1); |
26 DCHECK_LT(sources, kRtpMaxSources); | |
27 return kRtpBaseHeaderSize + sources * 4; | |
28 } | 26 } |
29 | 27 |
30 void PackRtpHeader(uint8* buffer, int buffer_size, | 28 // RTP Header format: |
31 const RtpHeader& header) { | 29 // |
30 // 0 1 2 3 | |
31 // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 | |
32 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
33 // |V=2|P|X| CC |M| PT | sequence number | | |
34 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
35 // | timestamp | | |
36 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
37 // | synchronization source (SSRC) identifier | | |
38 // +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+ | |
39 // | contributing source (CSRC) identifiers | | |
40 // | .... | | |
41 // +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+ | |
42 | |
43 int GetRtpHeaderSize(const RtpHeader& header) { | |
44 DCHECK_GE(header.sources, 0); | |
45 DCHECK_LT(header.sources, kRtpMaxSources); | |
46 return kRtpBaseHeaderSize + header.sources * 4; | |
awong
2010/11/16 01:08:40
magic number....why *4?
Sergey Ulanov
2010/11/16 01:52:25
Size of each CSRC in the header. Added kBytesPerCS
| |
47 } | |
48 | |
49 void PackRtpHeader(const RtpHeader& header, uint8* buffer, int buffer_size) { | |
32 DCHECK_LT(header.sources, kRtpMaxSources); | 50 DCHECK_LT(header.sources, kRtpMaxSources); |
33 DCHECK_LT(header.payload_type, 1 << 7); | 51 DCHECK_LT(header.payload_type, 1 << 7); |
34 CHECK_GT(buffer_size, GetRtpHeaderSize(header.sources)); | 52 CHECK_GE(buffer_size, GetRtpHeaderSize(header)); |
35 | 53 |
36 buffer[0] = (kRtpVersionNumber << 6) + | 54 buffer[0] = (kRtpVersionNumber << 6) + |
37 ((uint8)header.padding << 5) + | 55 ((uint8)header.padding << 5) + |
38 ((uint8)header.extension << 4) + | 56 ((uint8)header.extension << 4) + |
39 header.sources; | 57 header.sources; |
40 buffer[1] = ((uint8)header.marker << 7) + | 58 buffer[1] = ((uint8)header.marker << 7) + |
41 header.payload_type; | 59 header.payload_type; |
42 SetBE16(buffer + 2, header.sequence_number); | 60 SetBE16(buffer + 2, header.sequence_number); |
43 SetBE32(buffer + 4, header.timestamp); | 61 SetBE32(buffer + 4, header.timestamp); |
44 SetBE32(buffer + 8, header.sync_source_id); | 62 SetBE32(buffer + 8, header.sync_source_id); |
45 | 63 |
46 for (int i = 0; i < header.sources; i++) { | 64 for (int i = 0; i < header.sources; i++) { |
47 SetBE32(buffer + i * 4 + 12, header.source_id[i]); | 65 SetBE32(buffer + i * 4 + 12, header.source_id[i]); |
48 } | 66 } |
49 } | 67 } |
50 | 68 |
51 static inline uint8 ExtractBits(uint8 byte, int bits_count, int shift) { | |
52 return (byte >> shift) & ((1 << bits_count) - 1); | |
53 } | |
54 | |
55 int UnpackRtpHeader(const uint8* buffer, int buffer_size, RtpHeader* header) { | 69 int UnpackRtpHeader(const uint8* buffer, int buffer_size, RtpHeader* header) { |
56 if (buffer_size < kRtpBaseHeaderSize) { | 70 if (buffer_size < kRtpBaseHeaderSize) { |
57 return -1; | 71 return -1; |
58 } | 72 } |
59 | 73 |
60 int version = ExtractBits(buffer[0], 2, 6); | 74 int version = ExtractBits(buffer[0], 2, 6); |
61 if (version != kRtpVersionNumber) { | 75 if (version != kRtpVersionNumber) { |
62 return -1; | 76 return -1; |
63 } | 77 } |
64 | 78 |
65 header->padding = ExtractBits(buffer[0], 1, 5) != 0; | 79 header->padding = ExtractBits(buffer[0], 1, 5) != 0; |
66 header->extension = ExtractBits(buffer[0], 1, 4) != 0; | 80 header->extension = ExtractBits(buffer[0], 1, 4) != 0; |
67 header->sources = ExtractBits(buffer[0], 4, 0); | 81 header->sources = ExtractBits(buffer[0], 4, 0); |
68 | 82 |
69 header->marker = ExtractBits(buffer[1], 1, 7) != 0; | 83 header->marker = ExtractBits(buffer[1], 1, 7) != 0; |
70 header->payload_type = ExtractBits(buffer[1], 7, 0); | 84 header->payload_type = ExtractBits(buffer[1], 7, 0); |
71 | 85 |
72 header->sequence_number = GetBE16(buffer + 2); | 86 header->sequence_number = GetBE16(buffer + 2); |
73 header->timestamp = GetBE32(buffer + 4); | 87 header->timestamp = GetBE32(buffer + 4); |
74 header->sync_source_id = GetBE32(buffer + 8); | 88 header->sync_source_id = GetBE32(buffer + 8); |
75 | 89 |
76 DCHECK_LT(header->sources, 16); | 90 DCHECK_LT(header->sources, 16); |
77 | 91 |
78 if (buffer_size < GetRtpHeaderSize(header->sources)) { | 92 if (buffer_size < GetRtpHeaderSize(*header)) { |
79 return -1; | 93 return -1; |
80 } | 94 } |
81 for (int i = 0; i < header->sources; i++) { | 95 for (int i = 0; i < header->sources; i++) { |
82 header->source_id[i] = GetBE32(buffer + i * 4 + 12); | 96 header->source_id[i] = GetBE32(buffer + i * 4 + 12); |
83 } | 97 } |
84 | 98 |
85 return GetRtpHeaderSize(header->sources); | 99 return GetRtpHeaderSize(*header); |
100 } | |
101 | |
102 // VP8 Payload Descriptor format: | |
103 // | |
104 // 0 1 2 3 | |
105 // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 | |
106 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
107 // | RSV |I|N|FI |B| PictureID (integer #bytes) | | |
108 // +-+-+-+-+-+-+-+-+ | | |
109 // : : | |
110 // | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
111 // | : (VP8 data or VP8 payload header; byte aligned)| | |
112 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
113 // | |
114 // RSV: 3 bits | |
115 // Bits reserved for future use. MUST be equal to zero and MUST be | |
116 // ignored by the receiver. | |
117 // | |
118 // I: 1 bit | |
119 // PictureID present. When set to one, a PictureID is provided after | |
120 // the first byte of the payload descriptor. When set to zero, the | |
121 // PictureID is omitted, and the one-byte payload descriptor is | |
122 // immediately followed by the VP8 payload. | |
123 // | |
124 // N: 1 bit | |
125 // Non-reference frame. When set to one, the frame can be discarded | |
126 // without affecting any other future or past frames. | |
127 // | |
128 // FI: 2 bits | |
129 // Fragmentation information field. This field contains information | |
130 // about the fragmentation of VP8 payloads carried in the RTP | |
131 // packet. The four different values are listed below. | |
132 // | |
133 // FI Fragmentation status | |
134 // 00 The RTP packet contains no fragmented VP8 partitions. The | |
135 // payload is one or several complete partitions. | |
136 // 01 The RTP packet contains the first part of a fragmented | |
137 // partition. The fragment must be placed in its own RTP packet. | |
138 // 10 The RTP packet contains a fragment that is neither the first nor | |
139 // the last part of a fragmented partition. The fragment must be | |
140 // placed in its own RTP packet. | |
141 // 11 The RTP packet contains the last part of a fragmented | |
142 // partition. The fragment must be placed in its own RTP packet. | |
143 // | |
144 // B: 1 bit | |
145 // Beginning VP8 frame. When set to 1 this signals that a new VP8 | |
146 // frame starts in this RTP packet. | |
147 // | |
148 // PictureID: Multiple of 8 bits | |
149 // This is a running index of the frames. The field is present only if | |
150 // the I bit is equal to one. The most significant bit of each byte is | |
151 // an extension flag. The 7 following bits carry (parts of) the | |
152 // PictureID. If the extension flag is one, the PictureID continues in | |
153 // the next byte. If the extension flag is zero, the 7 remaining bits | |
154 // are the last (and least significant) bits in the PictureID. The | |
155 // sender may choose any number of bytes for the PictureID. The | |
156 // PictureID SHALL start on a random number, and SHALL wrap after | |
157 // reaching the maximum ID as chosen by the application | |
158 | |
159 int GetVp8DescriptorSize(const Vp8Descriptor& descriptor) { | |
160 if (descriptor.picture_id == kuint32max) | |
161 return 1; | |
162 int result = 2; | |
163 // We need 1 byte per each 7 bits in picture_id. | |
164 uint32 picture_id = descriptor.picture_id >> 7; | |
165 while (picture_id > 0) { | |
166 picture_id >>= 7; | |
167 ++result; | |
168 } | |
169 return result; | |
170 } | |
171 | |
172 void PackVp8Descriptor(const Vp8Descriptor& descriptor, uint8* buffer, | |
173 int buffer_size) { | |
174 CHECK_GT(buffer_size, 0); | |
175 | |
176 buffer[0] = | |
177 ((uint8)(descriptor.picture_id != kuint32max) << 4) + | |
178 ((uint8)descriptor.non_reference_frame << 3) + | |
179 (descriptor.fragmentation_info << 1) + | |
180 ((uint8)descriptor.frame_beginning); | |
181 | |
182 uint32 picture_id = descriptor.picture_id; | |
183 if (picture_id == kuint32max) | |
184 return; | |
185 | |
186 int pos = 1; | |
187 while (picture_id > 0) { | |
188 CHECK_LT(pos, buffer_size); | |
189 buffer[pos] = picture_id & 0x7F; | |
190 picture_id >>= 7; | |
191 | |
192 // Set the extension bit if neccessary. | |
193 if (picture_id > 0) | |
194 buffer[pos] |= 1 << 7; | |
awong
2010/11/16 01:08:40
I personally find 0x80 to be easier to read -- esp
Sergey Ulanov
2010/11/16 01:52:25
Done.
| |
195 ++pos; | |
196 } | |
197 } | |
198 | |
199 int UnpackVp8Descriptor(const uint8* buffer, int buffer_size, | |
200 Vp8Descriptor* descriptor) { | |
201 if (buffer_size <= 0) | |
202 return -1; | |
203 | |
204 bool picture_id_present = ExtractBits(buffer[0], 1, 4) != 0; | |
205 descriptor->non_reference_frame = ExtractBits(buffer[0], 1, 3) != 0; | |
206 descriptor->fragmentation_info = ExtractBits(buffer[0], 2, 1); | |
207 descriptor->frame_beginning = ExtractBits(buffer[0], 1, 0) != 0; | |
208 | |
209 // Decode PictureID if it is present. | |
210 if (picture_id_present) { | |
awong
2010/11/16 01:08:40
Can we invert this conditional, and early return o
Sergey Ulanov
2010/11/16 01:52:25
Done.
| |
211 bool extension = true; | |
212 int pos = 1; | |
213 descriptor->picture_id = 0; | |
214 while (extension) { | |
215 if (pos >= buffer_size) | |
216 return -1; | |
217 | |
218 descriptor->picture_id |= buffer[pos] & 0x7F; | |
219 extension = (buffer[pos] & 0x80) != 0; | |
220 pos += 1; | |
221 } | |
222 return pos; | |
223 } else { | |
224 descriptor->picture_id = kuint32max; | |
225 return 1; | |
226 } | |
86 } | 227 } |
87 | 228 |
88 } // namespace protocol | 229 } // namespace protocol |
89 } // namespace remoting | 230 } // namespace remoting |
OLD | NEW |