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_( |
Lu Liu | 6f68324 | 2018-09-25 18:48:48 +0000 | [diff] [blame] | 119 | field_trial::IsEnabled("WebRTC-GenericPictureId")) { |
philipel | bf2b620 | 2018-08-27 14:33:18 +0200 | [diff] [blame] | 120 | for (auto& spatial_layer : last_shared_frame_id_) |
| 121 | spatial_layer.fill(-1); |
| 122 | |
Stefan Holmer | f704468 | 2018-07-17 10:16:41 +0200 | [diff] [blame] | 123 | Random random(rtc::TimeMicros()); |
| 124 | state_.picture_id = |
| 125 | state ? state->picture_id : (random.Rand<int16_t>() & 0x7FFF); |
| 126 | state_.tl0_pic_idx = state ? state->tl0_pic_idx : (random.Rand<uint8_t>()); |
| 127 | } |
philipel | bf2b620 | 2018-08-27 14:33:18 +0200 | [diff] [blame] | 128 | |
| 129 | RtpPayloadParams::RtpPayloadParams(const RtpPayloadParams& other) = default; |
| 130 | |
Stefan Holmer | f704468 | 2018-07-17 10:16:41 +0200 | [diff] [blame] | 131 | RtpPayloadParams::~RtpPayloadParams() {} |
| 132 | |
| 133 | RTPVideoHeader RtpPayloadParams::GetRtpVideoHeader( |
| 134 | const EncodedImage& image, |
philipel | bf2b620 | 2018-08-27 14:33:18 +0200 | [diff] [blame] | 135 | const CodecSpecificInfo* codec_specific_info, |
| 136 | int64_t shared_frame_id) { |
Stefan Holmer | f704468 | 2018-07-17 10:16:41 +0200 | [diff] [blame] | 137 | RTPVideoHeader rtp_video_header; |
| 138 | if (codec_specific_info) { |
Niels Möller | d3b8c63 | 2018-08-27 15:33:42 +0200 | [diff] [blame] | 139 | PopulateRtpWithCodecSpecifics(*codec_specific_info, image.SpatialIndex(), |
| 140 | &rtp_video_header); |
Stefan Holmer | f704468 | 2018-07-17 10:16:41 +0200 | [diff] [blame] | 141 | } |
| 142 | rtp_video_header.rotation = image.rotation_; |
| 143 | rtp_video_header.content_type = image.content_type_; |
| 144 | rtp_video_header.playout_delay = image.playout_delay_; |
| 145 | |
| 146 | SetVideoTiming(image, &rtp_video_header.video_timing); |
| 147 | |
philipel | bf2b620 | 2018-08-27 14:33:18 +0200 | [diff] [blame] | 148 | const bool is_keyframe = image._frameType == kVideoFrameKey; |
Stefan Holmer | f704468 | 2018-07-17 10:16:41 +0200 | [diff] [blame] | 149 | const bool first_frame_in_picture = |
| 150 | (codec_specific_info && codec_specific_info->codecType == kVideoCodecVP9) |
| 151 | ? codec_specific_info->codecSpecific.VP9.first_frame_in_picture |
| 152 | : true; |
philipel | bf2b620 | 2018-08-27 14:33:18 +0200 | [diff] [blame] | 153 | |
| 154 | SetCodecSpecific(&rtp_video_header, first_frame_in_picture); |
Lu Liu | 6f68324 | 2018-09-25 18:48:48 +0000 | [diff] [blame] | 155 | SetGeneric(shared_frame_id, is_keyframe, &rtp_video_header); |
philipel | bf2b620 | 2018-08-27 14:33:18 +0200 | [diff] [blame] | 156 | |
Stefan Holmer | f704468 | 2018-07-17 10:16:41 +0200 | [diff] [blame] | 157 | return rtp_video_header; |
| 158 | } |
| 159 | |
| 160 | uint32_t RtpPayloadParams::ssrc() const { |
| 161 | return ssrc_; |
| 162 | } |
| 163 | |
| 164 | RtpPayloadState RtpPayloadParams::state() const { |
| 165 | return state_; |
| 166 | } |
| 167 | |
philipel | bf2b620 | 2018-08-27 14:33:18 +0200 | [diff] [blame] | 168 | void RtpPayloadParams::SetCodecSpecific(RTPVideoHeader* rtp_video_header, |
| 169 | bool first_frame_in_picture) { |
Stefan Holmer | f704468 | 2018-07-17 10:16:41 +0200 | [diff] [blame] | 170 | // Always set picture id. Set tl0_pic_idx iff temporal index is set. |
| 171 | if (first_frame_in_picture) { |
| 172 | state_.picture_id = (static_cast<uint16_t>(state_.picture_id) + 1) & 0x7FFF; |
| 173 | } |
| 174 | if (rtp_video_header->codec == kVideoCodecVP8) { |
Philip Eliasson | d52a1a6 | 2018-09-07 13:03:55 +0000 | [diff] [blame] | 175 | auto& vp8_header = |
| 176 | absl::get<RTPVideoHeaderVP8>(rtp_video_header->video_type_header); |
| 177 | vp8_header.pictureId = state_.picture_id; |
Stefan Holmer | f704468 | 2018-07-17 10:16:41 +0200 | [diff] [blame] | 178 | |
Philip Eliasson | d52a1a6 | 2018-09-07 13:03:55 +0000 | [diff] [blame] | 179 | if (vp8_header.temporalIdx != kNoTemporalIdx) { |
| 180 | if (vp8_header.temporalIdx == 0) { |
Stefan Holmer | f704468 | 2018-07-17 10:16:41 +0200 | [diff] [blame] | 181 | ++state_.tl0_pic_idx; |
| 182 | } |
Philip Eliasson | d52a1a6 | 2018-09-07 13:03:55 +0000 | [diff] [blame] | 183 | vp8_header.tl0PicIdx = state_.tl0_pic_idx; |
Stefan Holmer | f704468 | 2018-07-17 10:16:41 +0200 | [diff] [blame] | 184 | } |
| 185 | } |
| 186 | if (rtp_video_header->codec == kVideoCodecVP9) { |
philipel | 29d8846 | 2018-08-08 14:26:00 +0200 | [diff] [blame] | 187 | auto& vp9_header = |
| 188 | absl::get<RTPVideoHeaderVP9>(rtp_video_header->video_type_header); |
| 189 | vp9_header.picture_id = state_.picture_id; |
Stefan Holmer | f704468 | 2018-07-17 10:16:41 +0200 | [diff] [blame] | 190 | |
| 191 | // Note that in the case that we have no temporal layers but we do have |
| 192 | // spatial layers, packets will carry layering info with a temporal_idx of |
| 193 | // zero, and we then have to set and increment tl0_pic_idx. |
philipel | 29d8846 | 2018-08-08 14:26:00 +0200 | [diff] [blame] | 194 | if (vp9_header.temporal_idx != kNoTemporalIdx || |
| 195 | vp9_header.spatial_idx != kNoSpatialIdx) { |
Stefan Holmer | f704468 | 2018-07-17 10:16:41 +0200 | [diff] [blame] | 196 | if (first_frame_in_picture && |
philipel | 29d8846 | 2018-08-08 14:26:00 +0200 | [diff] [blame] | 197 | (vp9_header.temporal_idx == 0 || |
| 198 | vp9_header.temporal_idx == kNoTemporalIdx)) { |
Stefan Holmer | f704468 | 2018-07-17 10:16:41 +0200 | [diff] [blame] | 199 | ++state_.tl0_pic_idx; |
| 200 | } |
philipel | 29d8846 | 2018-08-08 14:26:00 +0200 | [diff] [blame] | 201 | vp9_header.tl0_pic_idx = state_.tl0_pic_idx; |
Stefan Holmer | f704468 | 2018-07-17 10:16:41 +0200 | [diff] [blame] | 202 | } |
| 203 | } |
philipel | bf2b620 | 2018-08-27 14:33:18 +0200 | [diff] [blame] | 204 | // There are currently two generic descriptors in WebRTC. The old descriptor |
| 205 | // can not share a picture id space between simulcast streams, so we use the |
| 206 | // |picture_id| in this case. We let the |picture_id| tag along in |frame_id| |
| 207 | // until the old generic format can be removed. |
| 208 | // TODO(philipel): Remove this when the new generic format has been fully |
| 209 | // implemented. |
| 210 | if (generic_picture_id_experiment_ && |
| 211 | rtp_video_header->codec == kVideoCodecGeneric) { |
| 212 | rtp_video_header->generic.emplace().frame_id = state_.picture_id; |
| 213 | } |
Stefan Holmer | f704468 | 2018-07-17 10:16:41 +0200 | [diff] [blame] | 214 | } |
philipel | bf2b620 | 2018-08-27 14:33:18 +0200 | [diff] [blame] | 215 | |
| 216 | void RtpPayloadParams::SetGeneric(int64_t frame_id, |
| 217 | bool is_keyframe, |
| 218 | RTPVideoHeader* rtp_video_header) { |
| 219 | if (rtp_video_header->codec == kVideoCodecVP8) { |
| 220 | Vp8ToGeneric(frame_id, is_keyframe, rtp_video_header); |
| 221 | } |
| 222 | |
| 223 | // TODO(philipel): Implement VP9 to new generic descriptor. |
| 224 | // TODO(philipel): Implement H264 to new generic descriptor. |
| 225 | // TODO(philipel): Implement generic codec to new generic descriptor. |
| 226 | } |
| 227 | |
| 228 | void RtpPayloadParams::Vp8ToGeneric(int64_t shared_frame_id, |
| 229 | bool is_keyframe, |
| 230 | RTPVideoHeader* rtp_video_header) { |
| 231 | const auto& vp8_header = |
| 232 | absl::get<RTPVideoHeaderVP8>(rtp_video_header->video_type_header); |
| 233 | const int spatial_index = 0; |
| 234 | const int temporal_index = |
| 235 | vp8_header.temporalIdx != kNoTemporalIdx ? vp8_header.temporalIdx : 0; |
| 236 | |
| 237 | if (temporal_index >= RtpGenericFrameDescriptor::kMaxTemporalLayers || |
| 238 | spatial_index >= RtpGenericFrameDescriptor::kMaxSpatialLayers) { |
| 239 | RTC_LOG(LS_WARNING) << "Temporal and/or spatial index is too high to be " |
| 240 | "used with generic frame descriptor."; |
| 241 | return; |
| 242 | } |
| 243 | |
| 244 | RTPVideoHeader::GenericDescriptorInfo& generic = |
| 245 | rtp_video_header->generic.emplace(); |
| 246 | |
| 247 | generic.frame_id = shared_frame_id; |
| 248 | generic.spatial_index = spatial_index; |
| 249 | generic.temporal_index = temporal_index; |
| 250 | |
| 251 | if (is_keyframe) { |
| 252 | RTC_DCHECK_EQ(temporal_index, 0); |
| 253 | last_shared_frame_id_[spatial_index].fill(-1); |
| 254 | last_shared_frame_id_[spatial_index][temporal_index] = shared_frame_id; |
| 255 | return; |
| 256 | } |
| 257 | |
| 258 | if (vp8_header.layerSync) { |
| 259 | int64_t tl0_frame_id = last_shared_frame_id_[spatial_index][0]; |
| 260 | |
| 261 | for (int i = 1; i < RtpGenericFrameDescriptor::kMaxTemporalLayers; ++i) { |
| 262 | if (last_shared_frame_id_[spatial_index][i] < tl0_frame_id) { |
| 263 | last_shared_frame_id_[spatial_index][i] = -1; |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | RTC_DCHECK_GE(tl0_frame_id, 0); |
| 268 | RTC_DCHECK_LT(tl0_frame_id, shared_frame_id); |
| 269 | generic.dependencies.push_back(tl0_frame_id); |
| 270 | } else { |
| 271 | for (int i = 0; i <= temporal_index; ++i) { |
| 272 | int64_t frame_id = last_shared_frame_id_[spatial_index][i]; |
| 273 | |
| 274 | if (frame_id != -1) { |
| 275 | RTC_DCHECK_LT(frame_id, shared_frame_id); |
| 276 | generic.dependencies.push_back(frame_id); |
| 277 | } |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | last_shared_frame_id_[spatial_index][temporal_index] = shared_frame_id; |
| 282 | } |
| 283 | |
Stefan Holmer | f704468 | 2018-07-17 10:16:41 +0200 | [diff] [blame] | 284 | } // namespace webrtc |