Stefan Holmer | f704468 | 2018-07-17 10:16:41 +0200 | [diff] [blame] | 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 "call/rtp_payload_params.h" |
| 12 | |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 13 | #include <stddef.h> |
| 14 | |
| 15 | #include "absl/container/inlined_vector.h" |
| 16 | #include "absl/types/optional.h" |
| 17 | #include "absl/types/variant.h" |
| 18 | #include "api/video/video_timing.h" |
| 19 | #include "common_types.h" // NOLINT(build/include) |
| 20 | #include "modules/video_coding/codecs/h264/include/h264_globals.h" |
| 21 | #include "modules/video_coding/codecs/interface/common_constants.h" |
| 22 | #include "modules/video_coding/codecs/vp8/include/vp8_globals.h" |
| 23 | #include "modules/video_coding/codecs/vp9/include/vp9_globals.h" |
Stefan Holmer | f704468 | 2018-07-17 10:16:41 +0200 | [diff] [blame] | 24 | #include "modules/video_coding/include/video_codec_interface.h" |
| 25 | #include "rtc_base/checks.h" |
philipel | bf2b620 | 2018-08-27 14:33:18 +0200 | [diff] [blame] | 26 | #include "rtc_base/logging.h" |
Stefan Holmer | f704468 | 2018-07-17 10:16:41 +0200 | [diff] [blame] | 27 | #include "rtc_base/random.h" |
| 28 | #include "rtc_base/timeutils.h" |
philipel | bf2b620 | 2018-08-27 14:33:18 +0200 | [diff] [blame] | 29 | #include "system_wrappers/include/field_trial.h" |
Stefan Holmer | f704468 | 2018-07-17 10:16:41 +0200 | [diff] [blame] | 30 | |
| 31 | namespace webrtc { |
| 32 | |
| 33 | namespace { |
| 34 | void PopulateRtpWithCodecSpecifics(const CodecSpecificInfo& info, |
Niels Möller | d3b8c63 | 2018-08-27 15:33:42 +0200 | [diff] [blame] | 35 | absl::optional<int> spatial_index, |
Stefan Holmer | f704468 | 2018-07-17 10:16:41 +0200 | [diff] [blame] | 36 | RTPVideoHeader* rtp) { |
| 37 | rtp->codec = info.codecType; |
| 38 | switch (info.codecType) { |
| 39 | case kVideoCodecVP8: { |
Philip Eliasson | d52a1a6 | 2018-09-07 13:03:55 +0000 | [diff] [blame] | 40 | auto& vp8_header = rtp->video_type_header.emplace<RTPVideoHeaderVP8>(); |
| 41 | vp8_header.InitRTPVideoHeaderVP8(); |
| 42 | vp8_header.nonReference = info.codecSpecific.VP8.nonReference; |
| 43 | vp8_header.temporalIdx = info.codecSpecific.VP8.temporalIdx; |
| 44 | vp8_header.layerSync = info.codecSpecific.VP8.layerSync; |
| 45 | vp8_header.keyIdx = info.codecSpecific.VP8.keyIdx; |
Niels Möller | d3b8c63 | 2018-08-27 15:33:42 +0200 | [diff] [blame] | 46 | rtp->simulcastIdx = spatial_index.value_or(0); |
Stefan Holmer | f704468 | 2018-07-17 10:16:41 +0200 | [diff] [blame] | 47 | return; |
| 48 | } |
| 49 | case kVideoCodecVP9: { |
philipel | 29d8846 | 2018-08-08 14:26:00 +0200 | [diff] [blame] | 50 | auto& vp9_header = rtp->video_type_header.emplace<RTPVideoHeaderVP9>(); |
| 51 | vp9_header.InitRTPVideoHeaderVP9(); |
| 52 | vp9_header.inter_pic_predicted = |
Stefan Holmer | f704468 | 2018-07-17 10:16:41 +0200 | [diff] [blame] | 53 | info.codecSpecific.VP9.inter_pic_predicted; |
philipel | 29d8846 | 2018-08-08 14:26:00 +0200 | [diff] [blame] | 54 | vp9_header.flexible_mode = info.codecSpecific.VP9.flexible_mode; |
| 55 | vp9_header.ss_data_available = info.codecSpecific.VP9.ss_data_available; |
| 56 | vp9_header.non_ref_for_inter_layer_pred = |
Stefan Holmer | f704468 | 2018-07-17 10:16:41 +0200 | [diff] [blame] | 57 | info.codecSpecific.VP9.non_ref_for_inter_layer_pred; |
philipel | 29d8846 | 2018-08-08 14:26:00 +0200 | [diff] [blame] | 58 | vp9_header.temporal_idx = info.codecSpecific.VP9.temporal_idx; |
philipel | 29d8846 | 2018-08-08 14:26:00 +0200 | [diff] [blame] | 59 | vp9_header.temporal_up_switch = info.codecSpecific.VP9.temporal_up_switch; |
| 60 | vp9_header.inter_layer_predicted = |
Stefan Holmer | f704468 | 2018-07-17 10:16:41 +0200 | [diff] [blame] | 61 | info.codecSpecific.VP9.inter_layer_predicted; |
philipel | 29d8846 | 2018-08-08 14:26:00 +0200 | [diff] [blame] | 62 | vp9_header.gof_idx = info.codecSpecific.VP9.gof_idx; |
| 63 | vp9_header.num_spatial_layers = info.codecSpecific.VP9.num_spatial_layers; |
Niels Möller | d3b8c63 | 2018-08-27 15:33:42 +0200 | [diff] [blame] | 64 | if (vp9_header.num_spatial_layers > 1) { |
| 65 | vp9_header.spatial_idx = spatial_index.value_or(kNoSpatialIdx); |
| 66 | } else { |
| 67 | vp9_header.spatial_idx = kNoSpatialIdx; |
| 68 | } |
Stefan Holmer | f704468 | 2018-07-17 10:16:41 +0200 | [diff] [blame] | 69 | if (info.codecSpecific.VP9.ss_data_available) { |
philipel | 29d8846 | 2018-08-08 14:26:00 +0200 | [diff] [blame] | 70 | vp9_header.spatial_layer_resolution_present = |
Stefan Holmer | f704468 | 2018-07-17 10:16:41 +0200 | [diff] [blame] | 71 | info.codecSpecific.VP9.spatial_layer_resolution_present; |
| 72 | if (info.codecSpecific.VP9.spatial_layer_resolution_present) { |
| 73 | for (size_t i = 0; i < info.codecSpecific.VP9.num_spatial_layers; |
| 74 | ++i) { |
philipel | 29d8846 | 2018-08-08 14:26:00 +0200 | [diff] [blame] | 75 | vp9_header.width[i] = info.codecSpecific.VP9.width[i]; |
| 76 | vp9_header.height[i] = info.codecSpecific.VP9.height[i]; |
Stefan Holmer | f704468 | 2018-07-17 10:16:41 +0200 | [diff] [blame] | 77 | } |
| 78 | } |
philipel | 29d8846 | 2018-08-08 14:26:00 +0200 | [diff] [blame] | 79 | vp9_header.gof.CopyGofInfoVP9(info.codecSpecific.VP9.gof); |
Stefan Holmer | f704468 | 2018-07-17 10:16:41 +0200 | [diff] [blame] | 80 | } |
| 81 | |
philipel | 29d8846 | 2018-08-08 14:26:00 +0200 | [diff] [blame] | 82 | vp9_header.num_ref_pics = info.codecSpecific.VP9.num_ref_pics; |
Stefan Holmer | f704468 | 2018-07-17 10:16:41 +0200 | [diff] [blame] | 83 | for (int i = 0; i < info.codecSpecific.VP9.num_ref_pics; ++i) { |
philipel | 29d8846 | 2018-08-08 14:26:00 +0200 | [diff] [blame] | 84 | vp9_header.pid_diff[i] = info.codecSpecific.VP9.p_diff[i]; |
Stefan Holmer | f704468 | 2018-07-17 10:16:41 +0200 | [diff] [blame] | 85 | } |
philipel | 29d8846 | 2018-08-08 14:26:00 +0200 | [diff] [blame] | 86 | vp9_header.end_of_picture = info.codecSpecific.VP9.end_of_picture; |
Stefan Holmer | f704468 | 2018-07-17 10:16:41 +0200 | [diff] [blame] | 87 | return; |
| 88 | } |
| 89 | case kVideoCodecH264: { |
philipel | 7d745e5 | 2018-08-02 14:03:53 +0200 | [diff] [blame] | 90 | auto& h264_header = rtp->video_type_header.emplace<RTPVideoHeaderH264>(); |
| 91 | h264_header.packetization_mode = |
Stefan Holmer | f704468 | 2018-07-17 10:16:41 +0200 | [diff] [blame] | 92 | info.codecSpecific.H264.packetization_mode; |
Niels Möller | d3b8c63 | 2018-08-27 15:33:42 +0200 | [diff] [blame] | 93 | rtp->simulcastIdx = spatial_index.value_or(0); |
Stefan Holmer | f704468 | 2018-07-17 10:16:41 +0200 | [diff] [blame] | 94 | return; |
| 95 | } |
| 96 | case kVideoCodecMultiplex: |
| 97 | case kVideoCodecGeneric: |
| 98 | rtp->codec = kVideoCodecGeneric; |
Niels Möller | d3b8c63 | 2018-08-27 15:33:42 +0200 | [diff] [blame] | 99 | rtp->simulcastIdx = spatial_index.value_or(0); |
Stefan Holmer | f704468 | 2018-07-17 10:16:41 +0200 | [diff] [blame] | 100 | return; |
| 101 | default: |
| 102 | return; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | void SetVideoTiming(const EncodedImage& image, VideoSendTiming* timing) { |
| 107 | if (image.timing_.flags == VideoSendTiming::TimingFrameFlags::kInvalid || |
| 108 | image.timing_.flags == VideoSendTiming::TimingFrameFlags::kNotTriggered) { |
| 109 | timing->flags = VideoSendTiming::TimingFrameFlags::kInvalid; |
| 110 | return; |
| 111 | } |
| 112 | |
| 113 | timing->encode_start_delta_ms = VideoSendTiming::GetDeltaCappedMs( |
| 114 | image.capture_time_ms_, image.timing_.encode_start_ms); |
| 115 | timing->encode_finish_delta_ms = VideoSendTiming::GetDeltaCappedMs( |
| 116 | image.capture_time_ms_, image.timing_.encode_finish_ms); |
| 117 | timing->packetization_finish_delta_ms = 0; |
| 118 | timing->pacer_exit_delta_ms = 0; |
| 119 | timing->network_timestamp_delta_ms = 0; |
| 120 | timing->network2_timestamp_delta_ms = 0; |
| 121 | timing->flags = image.timing_.flags; |
| 122 | } |
| 123 | } // namespace |
| 124 | |
| 125 | RtpPayloadParams::RtpPayloadParams(const uint32_t ssrc, |
| 126 | const RtpPayloadState* state) |
philipel | bf2b620 | 2018-08-27 14:33:18 +0200 | [diff] [blame] | 127 | : ssrc_(ssrc), |
| 128 | generic_picture_id_experiment_( |
philipel | 569397f | 2018-09-26 12:25:31 +0200 | [diff] [blame] | 129 | field_trial::IsEnabled("WebRTC-GenericPictureId")), |
| 130 | generic_descriptor_experiment_( |
| 131 | field_trial::IsEnabled("WebRTC-GenericDescriptor")) { |
philipel | bf2b620 | 2018-08-27 14:33:18 +0200 | [diff] [blame] | 132 | for (auto& spatial_layer : last_shared_frame_id_) |
| 133 | spatial_layer.fill(-1); |
| 134 | |
Stefan Holmer | f704468 | 2018-07-17 10:16:41 +0200 | [diff] [blame] | 135 | Random random(rtc::TimeMicros()); |
| 136 | state_.picture_id = |
| 137 | state ? state->picture_id : (random.Rand<int16_t>() & 0x7FFF); |
| 138 | state_.tl0_pic_idx = state ? state->tl0_pic_idx : (random.Rand<uint8_t>()); |
| 139 | } |
philipel | bf2b620 | 2018-08-27 14:33:18 +0200 | [diff] [blame] | 140 | |
| 141 | RtpPayloadParams::RtpPayloadParams(const RtpPayloadParams& other) = default; |
| 142 | |
Stefan Holmer | f704468 | 2018-07-17 10:16:41 +0200 | [diff] [blame] | 143 | RtpPayloadParams::~RtpPayloadParams() {} |
| 144 | |
| 145 | RTPVideoHeader RtpPayloadParams::GetRtpVideoHeader( |
| 146 | const EncodedImage& image, |
philipel | bf2b620 | 2018-08-27 14:33:18 +0200 | [diff] [blame] | 147 | const CodecSpecificInfo* codec_specific_info, |
| 148 | int64_t shared_frame_id) { |
Stefan Holmer | f704468 | 2018-07-17 10:16:41 +0200 | [diff] [blame] | 149 | RTPVideoHeader rtp_video_header; |
| 150 | if (codec_specific_info) { |
Niels Möller | d3b8c63 | 2018-08-27 15:33:42 +0200 | [diff] [blame] | 151 | PopulateRtpWithCodecSpecifics(*codec_specific_info, image.SpatialIndex(), |
| 152 | &rtp_video_header); |
Stefan Holmer | f704468 | 2018-07-17 10:16:41 +0200 | [diff] [blame] | 153 | } |
| 154 | rtp_video_header.rotation = image.rotation_; |
| 155 | rtp_video_header.content_type = image.content_type_; |
| 156 | rtp_video_header.playout_delay = image.playout_delay_; |
philipel | fab9129 | 2018-10-17 14:36:08 +0200 | [diff] [blame] | 157 | rtp_video_header.width = image._encodedWidth; |
| 158 | rtp_video_header.height = image._encodedHeight; |
Johannes Kron | d0b69a8 | 2018-12-03 14:18:53 +0100 | [diff] [blame] | 159 | rtp_video_header.color_space = image.ColorSpace() |
| 160 | ? absl::make_optional(*image.ColorSpace()) |
| 161 | : absl::nullopt; |
Stefan Holmer | f704468 | 2018-07-17 10:16:41 +0200 | [diff] [blame] | 162 | SetVideoTiming(image, &rtp_video_header.video_timing); |
| 163 | |
philipel | bf2b620 | 2018-08-27 14:33:18 +0200 | [diff] [blame] | 164 | const bool is_keyframe = image._frameType == kVideoFrameKey; |
Stefan Holmer | f704468 | 2018-07-17 10:16:41 +0200 | [diff] [blame] | 165 | const bool first_frame_in_picture = |
| 166 | (codec_specific_info && codec_specific_info->codecType == kVideoCodecVP9) |
| 167 | ? codec_specific_info->codecSpecific.VP9.first_frame_in_picture |
| 168 | : true; |
philipel | bf2b620 | 2018-08-27 14:33:18 +0200 | [diff] [blame] | 169 | |
| 170 | SetCodecSpecific(&rtp_video_header, first_frame_in_picture); |
philipel | 569397f | 2018-09-26 12:25:31 +0200 | [diff] [blame] | 171 | |
| 172 | if (generic_descriptor_experiment_) |
| 173 | SetGeneric(shared_frame_id, is_keyframe, &rtp_video_header); |
philipel | bf2b620 | 2018-08-27 14:33:18 +0200 | [diff] [blame] | 174 | |
Stefan Holmer | f704468 | 2018-07-17 10:16:41 +0200 | [diff] [blame] | 175 | return rtp_video_header; |
| 176 | } |
| 177 | |
| 178 | uint32_t RtpPayloadParams::ssrc() const { |
| 179 | return ssrc_; |
| 180 | } |
| 181 | |
| 182 | RtpPayloadState RtpPayloadParams::state() const { |
| 183 | return state_; |
| 184 | } |
| 185 | |
philipel | bf2b620 | 2018-08-27 14:33:18 +0200 | [diff] [blame] | 186 | void RtpPayloadParams::SetCodecSpecific(RTPVideoHeader* rtp_video_header, |
| 187 | bool first_frame_in_picture) { |
Stefan Holmer | f704468 | 2018-07-17 10:16:41 +0200 | [diff] [blame] | 188 | // Always set picture id. Set tl0_pic_idx iff temporal index is set. |
| 189 | if (first_frame_in_picture) { |
| 190 | state_.picture_id = (static_cast<uint16_t>(state_.picture_id) + 1) & 0x7FFF; |
| 191 | } |
| 192 | if (rtp_video_header->codec == kVideoCodecVP8) { |
Philip Eliasson | d52a1a6 | 2018-09-07 13:03:55 +0000 | [diff] [blame] | 193 | auto& vp8_header = |
| 194 | absl::get<RTPVideoHeaderVP8>(rtp_video_header->video_type_header); |
| 195 | vp8_header.pictureId = state_.picture_id; |
Stefan Holmer | f704468 | 2018-07-17 10:16:41 +0200 | [diff] [blame] | 196 | |
Philip Eliasson | d52a1a6 | 2018-09-07 13:03:55 +0000 | [diff] [blame] | 197 | if (vp8_header.temporalIdx != kNoTemporalIdx) { |
| 198 | if (vp8_header.temporalIdx == 0) { |
Stefan Holmer | f704468 | 2018-07-17 10:16:41 +0200 | [diff] [blame] | 199 | ++state_.tl0_pic_idx; |
| 200 | } |
Philip Eliasson | d52a1a6 | 2018-09-07 13:03:55 +0000 | [diff] [blame] | 201 | vp8_header.tl0PicIdx = state_.tl0_pic_idx; |
Stefan Holmer | f704468 | 2018-07-17 10:16:41 +0200 | [diff] [blame] | 202 | } |
| 203 | } |
| 204 | if (rtp_video_header->codec == kVideoCodecVP9) { |
philipel | 29d8846 | 2018-08-08 14:26:00 +0200 | [diff] [blame] | 205 | auto& vp9_header = |
| 206 | absl::get<RTPVideoHeaderVP9>(rtp_video_header->video_type_header); |
| 207 | vp9_header.picture_id = state_.picture_id; |
Stefan Holmer | f704468 | 2018-07-17 10:16:41 +0200 | [diff] [blame] | 208 | |
| 209 | // Note that in the case that we have no temporal layers but we do have |
| 210 | // spatial layers, packets will carry layering info with a temporal_idx of |
| 211 | // zero, and we then have to set and increment tl0_pic_idx. |
philipel | 29d8846 | 2018-08-08 14:26:00 +0200 | [diff] [blame] | 212 | if (vp9_header.temporal_idx != kNoTemporalIdx || |
| 213 | vp9_header.spatial_idx != kNoSpatialIdx) { |
Stefan Holmer | f704468 | 2018-07-17 10:16:41 +0200 | [diff] [blame] | 214 | if (first_frame_in_picture && |
philipel | 29d8846 | 2018-08-08 14:26:00 +0200 | [diff] [blame] | 215 | (vp9_header.temporal_idx == 0 || |
| 216 | vp9_header.temporal_idx == kNoTemporalIdx)) { |
Stefan Holmer | f704468 | 2018-07-17 10:16:41 +0200 | [diff] [blame] | 217 | ++state_.tl0_pic_idx; |
| 218 | } |
philipel | 29d8846 | 2018-08-08 14:26:00 +0200 | [diff] [blame] | 219 | vp9_header.tl0_pic_idx = state_.tl0_pic_idx; |
Stefan Holmer | f704468 | 2018-07-17 10:16:41 +0200 | [diff] [blame] | 220 | } |
| 221 | } |
philipel | bf2b620 | 2018-08-27 14:33:18 +0200 | [diff] [blame] | 222 | // There are currently two generic descriptors in WebRTC. The old descriptor |
| 223 | // can not share a picture id space between simulcast streams, so we use the |
| 224 | // |picture_id| in this case. We let the |picture_id| tag along in |frame_id| |
| 225 | // until the old generic format can be removed. |
| 226 | // TODO(philipel): Remove this when the new generic format has been fully |
| 227 | // implemented. |
| 228 | if (generic_picture_id_experiment_ && |
| 229 | rtp_video_header->codec == kVideoCodecGeneric) { |
| 230 | rtp_video_header->generic.emplace().frame_id = state_.picture_id; |
| 231 | } |
Stefan Holmer | f704468 | 2018-07-17 10:16:41 +0200 | [diff] [blame] | 232 | } |
philipel | bf2b620 | 2018-08-27 14:33:18 +0200 | [diff] [blame] | 233 | |
| 234 | void RtpPayloadParams::SetGeneric(int64_t frame_id, |
| 235 | bool is_keyframe, |
| 236 | RTPVideoHeader* rtp_video_header) { |
| 237 | if (rtp_video_header->codec == kVideoCodecVP8) { |
| 238 | Vp8ToGeneric(frame_id, is_keyframe, rtp_video_header); |
| 239 | } |
| 240 | |
| 241 | // TODO(philipel): Implement VP9 to new generic descriptor. |
| 242 | // TODO(philipel): Implement H264 to new generic descriptor. |
| 243 | // TODO(philipel): Implement generic codec to new generic descriptor. |
| 244 | } |
| 245 | |
| 246 | void RtpPayloadParams::Vp8ToGeneric(int64_t shared_frame_id, |
| 247 | bool is_keyframe, |
| 248 | RTPVideoHeader* rtp_video_header) { |
| 249 | const auto& vp8_header = |
| 250 | absl::get<RTPVideoHeaderVP8>(rtp_video_header->video_type_header); |
| 251 | const int spatial_index = 0; |
| 252 | const int temporal_index = |
| 253 | vp8_header.temporalIdx != kNoTemporalIdx ? vp8_header.temporalIdx : 0; |
| 254 | |
| 255 | if (temporal_index >= RtpGenericFrameDescriptor::kMaxTemporalLayers || |
| 256 | spatial_index >= RtpGenericFrameDescriptor::kMaxSpatialLayers) { |
| 257 | RTC_LOG(LS_WARNING) << "Temporal and/or spatial index is too high to be " |
| 258 | "used with generic frame descriptor."; |
| 259 | return; |
| 260 | } |
| 261 | |
| 262 | RTPVideoHeader::GenericDescriptorInfo& generic = |
| 263 | rtp_video_header->generic.emplace(); |
| 264 | |
| 265 | generic.frame_id = shared_frame_id; |
| 266 | generic.spatial_index = spatial_index; |
| 267 | generic.temporal_index = temporal_index; |
| 268 | |
| 269 | if (is_keyframe) { |
| 270 | RTC_DCHECK_EQ(temporal_index, 0); |
| 271 | last_shared_frame_id_[spatial_index].fill(-1); |
| 272 | last_shared_frame_id_[spatial_index][temporal_index] = shared_frame_id; |
| 273 | return; |
| 274 | } |
| 275 | |
| 276 | if (vp8_header.layerSync) { |
| 277 | int64_t tl0_frame_id = last_shared_frame_id_[spatial_index][0]; |
| 278 | |
| 279 | for (int i = 1; i < RtpGenericFrameDescriptor::kMaxTemporalLayers; ++i) { |
| 280 | if (last_shared_frame_id_[spatial_index][i] < tl0_frame_id) { |
| 281 | last_shared_frame_id_[spatial_index][i] = -1; |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | RTC_DCHECK_GE(tl0_frame_id, 0); |
| 286 | RTC_DCHECK_LT(tl0_frame_id, shared_frame_id); |
| 287 | generic.dependencies.push_back(tl0_frame_id); |
| 288 | } else { |
| 289 | for (int i = 0; i <= temporal_index; ++i) { |
| 290 | int64_t frame_id = last_shared_frame_id_[spatial_index][i]; |
| 291 | |
| 292 | if (frame_id != -1) { |
| 293 | RTC_DCHECK_LT(frame_id, shared_frame_id); |
| 294 | generic.dependencies.push_back(frame_id); |
| 295 | } |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | last_shared_frame_id_[spatial_index][temporal_index] = shared_frame_id; |
| 300 | } |
| 301 | |
Stefan Holmer | f704468 | 2018-07-17 10:16:41 +0200 | [diff] [blame] | 302 | } // namespace webrtc |