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