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 #ifndef CALL_RTP_PAYLOAD_PARAMS_H_
12 #define CALL_RTP_PAYLOAD_PARAMS_H_
13 
14 #include <array>
15 
16 #include "absl/types/optional.h"
17 #include "api/transport/webrtc_key_value_config.h"
18 #include "api/video_codecs/video_encoder.h"
19 #include "call/rtp_config.h"
20 #include "modules/rtp_rtcp/source/rtp_generic_frame_descriptor.h"
21 #include "modules/rtp_rtcp/source/rtp_video_header.h"
22 #include "modules/video_coding/chain_diff_calculator.h"
23 #include "modules/video_coding/frame_dependencies_calculator.h"
24 #include "modules/video_coding/include/video_codec_interface.h"
25 
26 namespace webrtc {
27 
28 class RtpRtcp;
29 
30 // State for setting picture id and tl0 pic idx, for VP8 and VP9
31 // TODO(nisse): Make these properties not codec specific.
32 class RtpPayloadParams final {
33  public:
34   RtpPayloadParams(const uint32_t ssrc,
35                    const RtpPayloadState* state,
36                    const WebRtcKeyValueConfig& trials);
37   RtpPayloadParams(const RtpPayloadParams& other);
38   ~RtpPayloadParams();
39 
40   RTPVideoHeader GetRtpVideoHeader(const EncodedImage& image,
41                                    const CodecSpecificInfo* codec_specific_info,
42                                    int64_t shared_frame_id);
43 
44   uint32_t ssrc() const;
45 
46   RtpPayloadState state() const;
47 
48  private:
49   void SetCodecSpecific(RTPVideoHeader* rtp_video_header,
50                         bool first_frame_in_picture);
51   RTPVideoHeader::GenericDescriptorInfo GenericDescriptorFromFrameInfo(
52       const GenericFrameInfo& frame_info,
53       int64_t frame_id,
54       VideoFrameType frame_type);
55   void SetGeneric(const CodecSpecificInfo* codec_specific_info,
56                   int64_t frame_id,
57                   bool is_keyframe,
58                   RTPVideoHeader* rtp_video_header);
59 
60   void Vp8ToGeneric(const CodecSpecificInfoVP8& vp8_info,
61                     int64_t shared_frame_id,
62                     bool is_keyframe,
63                     RTPVideoHeader* rtp_video_header);
64 
65   void H264ToGeneric(const CodecSpecificInfoH264& h264_info,
66                      int64_t shared_frame_id,
67                      bool is_keyframe,
68                      RTPVideoHeader* rtp_video_header);
69 
70   void GenericToGeneric(int64_t shared_frame_id,
71                         bool is_keyframe,
72                         RTPVideoHeader* rtp_video_header);
73 
74   // TODO(bugs.webrtc.org/10242): Delete SetDependenciesVp8Deprecated() and move
75   // the logic in SetDependenciesVp8New() into Vp8ToGeneric() once all hardware
76   // wrappers have been updated.
77   void SetDependenciesVp8Deprecated(
78       const CodecSpecificInfoVP8& vp8_info,
79       int64_t shared_frame_id,
80       bool is_keyframe,
81       int spatial_index,
82       int temporal_index,
83       bool layer_sync,
84       RTPVideoHeader::GenericDescriptorInfo* generic);
85   void SetDependenciesVp8New(const CodecSpecificInfoVP8& vp8_info,
86                              int64_t shared_frame_id,
87                              bool is_keyframe,
88                              bool layer_sync,
89                              RTPVideoHeader::GenericDescriptorInfo* generic);
90 
91   FrameDependenciesCalculator dependencies_calculator_;
92   ChainDiffCalculator chains_calculator_;
93   // TODO(bugs.webrtc.org/10242): Remove once all encoder-wrappers are updated.
94   // Holds the last shared frame id for a given (spatial, temporal) layer.
95   std::array<std::array<int64_t, RtpGenericFrameDescriptor::kMaxTemporalLayers>,
96              RtpGenericFrameDescriptor::kMaxSpatialLayers>
97       last_shared_frame_id_;
98 
99   // TODO(eladalon): When additional codecs are supported,
100   // set kMaxCodecBuffersCount to the max() of these codecs' buffer count.
101   static constexpr size_t kMaxCodecBuffersCount =
102       CodecSpecificInfoVP8::kBuffersCount;
103 
104   // Maps buffer IDs to the frame-ID stored in them.
105   std::array<int64_t, kMaxCodecBuffersCount> buffer_id_to_frame_id_;
106 
107   // Until we remove SetDependenciesVp8Deprecated(), we should make sure
108   // that, for a given object, we either always use
109   // SetDependenciesVp8Deprecated(), or always use SetDependenciesVp8New().
110   // TODO(bugs.webrtc.org/10242): Remove.
111   absl::optional<bool> new_version_used_;
112 
113   const uint32_t ssrc_;
114   RtpPayloadState state_;
115 
116   const bool generic_picture_id_experiment_;
117 };
118 }  // namespace webrtc
119 #endif  // CALL_RTP_PAYLOAD_PARAMS_H_
120