1 /*
2  *  Copyright (c) 2020 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_descriptor_authentication.h"
12 
13 #include <cstdint>
14 #include <vector>
15 
16 #include "modules/rtp_rtcp/source/rtp_generic_frame_descriptor.h"
17 #include "modules/rtp_rtcp/source/rtp_generic_frame_descriptor_extension.h"
18 #include "modules/rtp_rtcp/source/rtp_video_header.h"
19 
20 namespace webrtc {
21 
RtpDescriptorAuthentication(const RTPVideoHeader & rtp_video_header)22 std::vector<uint8_t> RtpDescriptorAuthentication(
23     const RTPVideoHeader& rtp_video_header) {
24   if (!rtp_video_header.generic) {
25     return {};
26   }
27   const RTPVideoHeader::GenericDescriptorInfo& descriptor =
28       *rtp_video_header.generic;
29   // Default way of creating additional data for an encrypted frame.
30   if (descriptor.spatial_index < 0 || descriptor.temporal_index < 0 ||
31       descriptor.spatial_index >=
32           RtpGenericFrameDescriptor::kMaxSpatialLayers ||
33       descriptor.temporal_index >=
34           RtpGenericFrameDescriptor::kMaxTemporalLayers ||
35       descriptor.dependencies.size() >
36           RtpGenericFrameDescriptor::kMaxNumFrameDependencies) {
37     return {};
38   }
39   RtpGenericFrameDescriptor frame_descriptor;
40   frame_descriptor.SetFirstPacketInSubFrame(true);
41   frame_descriptor.SetLastPacketInSubFrame(false);
42   frame_descriptor.SetTemporalLayer(descriptor.temporal_index);
43   frame_descriptor.SetSpatialLayersBitmask(1 << descriptor.spatial_index);
44   frame_descriptor.SetFrameId(descriptor.frame_id & 0xFFFF);
45   for (int64_t dependency : descriptor.dependencies) {
46     frame_descriptor.AddFrameDependencyDiff(descriptor.frame_id - dependency);
47   }
48   if (descriptor.dependencies.empty()) {
49     frame_descriptor.SetResolution(rtp_video_header.width,
50                                    rtp_video_header.height);
51   }
52   std::vector<uint8_t> result(
53       RtpGenericFrameDescriptorExtension00::ValueSize(frame_descriptor));
54   RtpGenericFrameDescriptorExtension00::Write(result, frame_descriptor);
55   return result;
56 }
57 
58 }  // namespace webrtc
59