blob: 0c8b30755b7bcda16b965fa99a69ab41f338b44f [file] [log] [blame]
Stefan Holmerf7044682018-07-17 10:16:41 +02001/*
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"
philipelbf2b6202018-08-27 14:33:18 +020016#include "rtc_base/logging.h"
Stefan Holmerf7044682018-07-17 10:16:41 +020017#include "rtc_base/random.h"
18#include "rtc_base/timeutils.h"
philipelbf2b6202018-08-27 14:33:18 +020019#include "system_wrappers/include/field_trial.h"
Stefan Holmerf7044682018-07-17 10:16:41 +020020
21namespace webrtc {
22
23namespace {
24void PopulateRtpWithCodecSpecifics(const CodecSpecificInfo& info,
Niels Möllerd3b8c632018-08-27 15:33:42 +020025 absl::optional<int> spatial_index,
Stefan Holmerf7044682018-07-17 10:16:41 +020026 RTPVideoHeader* rtp) {
27 rtp->codec = info.codecType;
28 switch (info.codecType) {
29 case kVideoCodecVP8: {
Philip Eliassond52a1a62018-09-07 13:03:55 +000030 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öllerd3b8c632018-08-27 15:33:42 +020036 rtp->simulcastIdx = spatial_index.value_or(0);
Stefan Holmerf7044682018-07-17 10:16:41 +020037 return;
38 }
39 case kVideoCodecVP9: {
philipel29d88462018-08-08 14:26:00 +020040 auto& vp9_header = rtp->video_type_header.emplace<RTPVideoHeaderVP9>();
41 vp9_header.InitRTPVideoHeaderVP9();
42 vp9_header.inter_pic_predicted =
Stefan Holmerf7044682018-07-17 10:16:41 +020043 info.codecSpecific.VP9.inter_pic_predicted;
philipel29d88462018-08-08 14:26:00 +020044 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 Holmerf7044682018-07-17 10:16:41 +020047 info.codecSpecific.VP9.non_ref_for_inter_layer_pred;
philipel29d88462018-08-08 14:26:00 +020048 vp9_header.temporal_idx = info.codecSpecific.VP9.temporal_idx;
philipel29d88462018-08-08 14:26:00 +020049 vp9_header.temporal_up_switch = info.codecSpecific.VP9.temporal_up_switch;
50 vp9_header.inter_layer_predicted =
Stefan Holmerf7044682018-07-17 10:16:41 +020051 info.codecSpecific.VP9.inter_layer_predicted;
philipel29d88462018-08-08 14:26:00 +020052 vp9_header.gof_idx = info.codecSpecific.VP9.gof_idx;
53 vp9_header.num_spatial_layers = info.codecSpecific.VP9.num_spatial_layers;
Niels Möllerd3b8c632018-08-27 15:33:42 +020054 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 Holmerf7044682018-07-17 10:16:41 +020059 if (info.codecSpecific.VP9.ss_data_available) {
philipel29d88462018-08-08 14:26:00 +020060 vp9_header.spatial_layer_resolution_present =
Stefan Holmerf7044682018-07-17 10:16:41 +020061 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) {
philipel29d88462018-08-08 14:26:00 +020065 vp9_header.width[i] = info.codecSpecific.VP9.width[i];
66 vp9_header.height[i] = info.codecSpecific.VP9.height[i];
Stefan Holmerf7044682018-07-17 10:16:41 +020067 }
68 }
philipel29d88462018-08-08 14:26:00 +020069 vp9_header.gof.CopyGofInfoVP9(info.codecSpecific.VP9.gof);
Stefan Holmerf7044682018-07-17 10:16:41 +020070 }
71
philipel29d88462018-08-08 14:26:00 +020072 vp9_header.num_ref_pics = info.codecSpecific.VP9.num_ref_pics;
Stefan Holmerf7044682018-07-17 10:16:41 +020073 for (int i = 0; i < info.codecSpecific.VP9.num_ref_pics; ++i) {
philipel29d88462018-08-08 14:26:00 +020074 vp9_header.pid_diff[i] = info.codecSpecific.VP9.p_diff[i];
Stefan Holmerf7044682018-07-17 10:16:41 +020075 }
philipel29d88462018-08-08 14:26:00 +020076 vp9_header.end_of_picture = info.codecSpecific.VP9.end_of_picture;
Stefan Holmerf7044682018-07-17 10:16:41 +020077 return;
78 }
79 case kVideoCodecH264: {
philipel7d745e52018-08-02 14:03:53 +020080 auto& h264_header = rtp->video_type_header.emplace<RTPVideoHeaderH264>();
81 h264_header.packetization_mode =
Stefan Holmerf7044682018-07-17 10:16:41 +020082 info.codecSpecific.H264.packetization_mode;
Niels Möllerd3b8c632018-08-27 15:33:42 +020083 rtp->simulcastIdx = spatial_index.value_or(0);
Stefan Holmerf7044682018-07-17 10:16:41 +020084 return;
85 }
86 case kVideoCodecMultiplex:
87 case kVideoCodecGeneric:
88 rtp->codec = kVideoCodecGeneric;
Niels Möllerd3b8c632018-08-27 15:33:42 +020089 rtp->simulcastIdx = spatial_index.value_or(0);
Stefan Holmerf7044682018-07-17 10:16:41 +020090 return;
91 default:
92 return;
93 }
94}
95
96void 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
115RtpPayloadParams::RtpPayloadParams(const uint32_t ssrc,
116 const RtpPayloadState* state)
philipelbf2b6202018-08-27 14:33:18 +0200117 : ssrc_(ssrc),
118 generic_picture_id_experiment_(
Lu Liu6f683242018-09-25 18:48:48 +0000119 field_trial::IsEnabled("WebRTC-GenericPictureId")) {
philipelbf2b6202018-08-27 14:33:18 +0200120 for (auto& spatial_layer : last_shared_frame_id_)
121 spatial_layer.fill(-1);
122
Stefan Holmerf7044682018-07-17 10:16:41 +0200123 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}
philipelbf2b6202018-08-27 14:33:18 +0200128
129RtpPayloadParams::RtpPayloadParams(const RtpPayloadParams& other) = default;
130
Stefan Holmerf7044682018-07-17 10:16:41 +0200131RtpPayloadParams::~RtpPayloadParams() {}
132
133RTPVideoHeader RtpPayloadParams::GetRtpVideoHeader(
134 const EncodedImage& image,
philipelbf2b6202018-08-27 14:33:18 +0200135 const CodecSpecificInfo* codec_specific_info,
136 int64_t shared_frame_id) {
Stefan Holmerf7044682018-07-17 10:16:41 +0200137 RTPVideoHeader rtp_video_header;
138 if (codec_specific_info) {
Niels Möllerd3b8c632018-08-27 15:33:42 +0200139 PopulateRtpWithCodecSpecifics(*codec_specific_info, image.SpatialIndex(),
140 &rtp_video_header);
Stefan Holmerf7044682018-07-17 10:16:41 +0200141 }
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
philipelbf2b6202018-08-27 14:33:18 +0200148 const bool is_keyframe = image._frameType == kVideoFrameKey;
Stefan Holmerf7044682018-07-17 10:16:41 +0200149 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;
philipelbf2b6202018-08-27 14:33:18 +0200153
154 SetCodecSpecific(&rtp_video_header, first_frame_in_picture);
Lu Liu6f683242018-09-25 18:48:48 +0000155 SetGeneric(shared_frame_id, is_keyframe, &rtp_video_header);
philipelbf2b6202018-08-27 14:33:18 +0200156
Stefan Holmerf7044682018-07-17 10:16:41 +0200157 return rtp_video_header;
158}
159
160uint32_t RtpPayloadParams::ssrc() const {
161 return ssrc_;
162}
163
164RtpPayloadState RtpPayloadParams::state() const {
165 return state_;
166}
167
philipelbf2b6202018-08-27 14:33:18 +0200168void RtpPayloadParams::SetCodecSpecific(RTPVideoHeader* rtp_video_header,
169 bool first_frame_in_picture) {
Stefan Holmerf7044682018-07-17 10:16:41 +0200170 // 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 Eliassond52a1a62018-09-07 13:03:55 +0000175 auto& vp8_header =
176 absl::get<RTPVideoHeaderVP8>(rtp_video_header->video_type_header);
177 vp8_header.pictureId = state_.picture_id;
Stefan Holmerf7044682018-07-17 10:16:41 +0200178
Philip Eliassond52a1a62018-09-07 13:03:55 +0000179 if (vp8_header.temporalIdx != kNoTemporalIdx) {
180 if (vp8_header.temporalIdx == 0) {
Stefan Holmerf7044682018-07-17 10:16:41 +0200181 ++state_.tl0_pic_idx;
182 }
Philip Eliassond52a1a62018-09-07 13:03:55 +0000183 vp8_header.tl0PicIdx = state_.tl0_pic_idx;
Stefan Holmerf7044682018-07-17 10:16:41 +0200184 }
185 }
186 if (rtp_video_header->codec == kVideoCodecVP9) {
philipel29d88462018-08-08 14:26:00 +0200187 auto& vp9_header =
188 absl::get<RTPVideoHeaderVP9>(rtp_video_header->video_type_header);
189 vp9_header.picture_id = state_.picture_id;
Stefan Holmerf7044682018-07-17 10:16:41 +0200190
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.
philipel29d88462018-08-08 14:26:00 +0200194 if (vp9_header.temporal_idx != kNoTemporalIdx ||
195 vp9_header.spatial_idx != kNoSpatialIdx) {
Stefan Holmerf7044682018-07-17 10:16:41 +0200196 if (first_frame_in_picture &&
philipel29d88462018-08-08 14:26:00 +0200197 (vp9_header.temporal_idx == 0 ||
198 vp9_header.temporal_idx == kNoTemporalIdx)) {
Stefan Holmerf7044682018-07-17 10:16:41 +0200199 ++state_.tl0_pic_idx;
200 }
philipel29d88462018-08-08 14:26:00 +0200201 vp9_header.tl0_pic_idx = state_.tl0_pic_idx;
Stefan Holmerf7044682018-07-17 10:16:41 +0200202 }
203 }
philipelbf2b6202018-08-27 14:33:18 +0200204 // 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 Holmerf7044682018-07-17 10:16:41 +0200214}
philipelbf2b6202018-08-27 14:33:18 +0200215
216void 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
228void 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 Holmerf7044682018-07-17 10:16:41 +0200284} // namespace webrtc