xref: /aosp_15_r20/external/webrtc/modules/rtp_rtcp/source/rtp_header_extension_size.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright (c) 2018 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #include "modules/rtp_rtcp/source/rtp_header_extension_size.h"
12 
13 #include "api/rtp_parameters.h"
14 
15 namespace webrtc {
16 
RtpHeaderExtensionSize(rtc::ArrayView<const RtpExtensionSize> extensions,const RtpHeaderExtensionMap & registered_extensions)17 int RtpHeaderExtensionSize(rtc::ArrayView<const RtpExtensionSize> extensions,
18                            const RtpHeaderExtensionMap& registered_extensions) {
19   // RFC3550 Section 5.3.1
20   static constexpr int kExtensionBlockHeaderSize = 4;
21 
22   int values_size = 0;
23   int num_extensions = 0;
24   int each_extension_header_size = 1;
25   for (const RtpExtensionSize& extension : extensions) {
26     int id = registered_extensions.GetId(extension.type);
27     if (id == RtpHeaderExtensionMap::kInvalidId)
28       continue;
29     // All extensions should use same size header. Check if the `extension`
30     // forces to switch to two byte header that allows larger id and value size.
31     if (id > RtpExtension::kOneByteHeaderExtensionMaxId ||
32         extension.value_size >
33             RtpExtension::kOneByteHeaderExtensionMaxValueSize) {
34       each_extension_header_size = 2;
35     }
36     values_size += extension.value_size;
37     num_extensions++;
38   }
39   if (values_size == 0)
40     return 0;
41   int size = kExtensionBlockHeaderSize +
42              each_extension_header_size * num_extensions + values_size;
43   // Extension size specified in 32bit words,
44   // so result must be multiple of 4 bytes. Round up.
45   return size + 3 - (size + 3) % 4;
46 }
47 
48 }  // namespace webrtc
49