blob: 0dbe1837c6e74b5bbc432edd4ebddad575b09151 [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: {
30 rtp->vp8().InitRTPVideoHeaderVP8();
31 rtp->vp8().nonReference = info.codecSpecific.VP8.nonReference;
32 rtp->vp8().temporalIdx = info.codecSpecific.VP8.temporalIdx;
33 rtp->vp8().layerSync = info.codecSpecific.VP8.layerSync;
34 rtp->vp8().keyIdx = info.codecSpecific.VP8.keyIdx;
Niels Möllerd3b8c632018-08-27 15:33:42 +020035 rtp->simulcastIdx = spatial_index.value_or(0);
Stefan Holmerf7044682018-07-17 10:16:41 +020036 return;
37 }
38 case kVideoCodecVP9: {
philipel29d88462018-08-08 14:26:00 +020039 auto& vp9_header = rtp->video_type_header.emplace<RTPVideoHeaderVP9>();
40 vp9_header.InitRTPVideoHeaderVP9();
41 vp9_header.inter_pic_predicted =
Stefan Holmerf7044682018-07-17 10:16:41 +020042 info.codecSpecific.VP9.inter_pic_predicted;
philipel29d88462018-08-08 14:26:00 +020043 vp9_header.flexible_mode = info.codecSpecific.VP9.flexible_mode;
44 vp9_header.ss_data_available = info.codecSpecific.VP9.ss_data_available;
45 vp9_header.non_ref_for_inter_layer_pred =
Stefan Holmerf7044682018-07-17 10:16:41 +020046 info.codecSpecific.VP9.non_ref_for_inter_layer_pred;
philipel29d88462018-08-08 14:26:00 +020047 vp9_header.temporal_idx = info.codecSpecific.VP9.temporal_idx;
philipel29d88462018-08-08 14:26:00 +020048 vp9_header.temporal_up_switch = info.codecSpecific.VP9.temporal_up_switch;
49 vp9_header.inter_layer_predicted =
Stefan Holmerf7044682018-07-17 10:16:41 +020050 info.codecSpecific.VP9.inter_layer_predicted;
philipel29d88462018-08-08 14:26:00 +020051 vp9_header.gof_idx = info.codecSpecific.VP9.gof_idx;
52 vp9_header.num_spatial_layers = info.codecSpecific.VP9.num_spatial_layers;
Niels Möllerd3b8c632018-08-27 15:33:42 +020053 if (vp9_header.num_spatial_layers > 1) {
54 vp9_header.spatial_idx = spatial_index.value_or(kNoSpatialIdx);
55 } else {
56 vp9_header.spatial_idx = kNoSpatialIdx;
57 }
Stefan Holmerf7044682018-07-17 10:16:41 +020058 if (info.codecSpecific.VP9.ss_data_available) {
philipel29d88462018-08-08 14:26:00 +020059 vp9_header.spatial_layer_resolution_present =
Stefan Holmerf7044682018-07-17 10:16:41 +020060 info.codecSpecific.VP9.spatial_layer_resolution_present;
61 if (info.codecSpecific.VP9.spatial_layer_resolution_present) {
62 for (size_t i = 0; i < info.codecSpecific.VP9.num_spatial_layers;
63 ++i) {
philipel29d88462018-08-08 14:26:00 +020064 vp9_header.width[i] = info.codecSpecific.VP9.width[i];
65 vp9_header.height[i] = info.codecSpecific.VP9.height[i];
Stefan Holmerf7044682018-07-17 10:16:41 +020066 }
67 }
philipel29d88462018-08-08 14:26:00 +020068 vp9_header.gof.CopyGofInfoVP9(info.codecSpecific.VP9.gof);
Stefan Holmerf7044682018-07-17 10:16:41 +020069 }
70
philipel29d88462018-08-08 14:26:00 +020071 vp9_header.num_ref_pics = info.codecSpecific.VP9.num_ref_pics;
Stefan Holmerf7044682018-07-17 10:16:41 +020072 for (int i = 0; i < info.codecSpecific.VP9.num_ref_pics; ++i) {
philipel29d88462018-08-08 14:26:00 +020073 vp9_header.pid_diff[i] = info.codecSpecific.VP9.p_diff[i];
Stefan Holmerf7044682018-07-17 10:16:41 +020074 }
philipel29d88462018-08-08 14:26:00 +020075 vp9_header.end_of_picture = info.codecSpecific.VP9.end_of_picture;
Stefan Holmerf7044682018-07-17 10:16:41 +020076 return;
77 }
78 case kVideoCodecH264: {
philipel7d745e52018-08-02 14:03:53 +020079 auto& h264_header = rtp->video_type_header.emplace<RTPVideoHeaderH264>();
80 h264_header.packetization_mode =
Stefan Holmerf7044682018-07-17 10:16:41 +020081 info.codecSpecific.H264.packetization_mode;
Niels Möllerd3b8c632018-08-27 15:33:42 +020082 rtp->simulcastIdx = spatial_index.value_or(0);
Stefan Holmerf7044682018-07-17 10:16:41 +020083 return;
84 }
85 case kVideoCodecMultiplex:
86 case kVideoCodecGeneric:
87 rtp->codec = kVideoCodecGeneric;
Niels Möllerd3b8c632018-08-27 15:33:42 +020088 rtp->simulcastIdx = spatial_index.value_or(0);
Stefan Holmerf7044682018-07-17 10:16:41 +020089 return;
90 default:
91 return;
92 }
93}
94
95void SetVideoTiming(const EncodedImage& image, VideoSendTiming* timing) {
96 if (image.timing_.flags == VideoSendTiming::TimingFrameFlags::kInvalid ||
97 image.timing_.flags == VideoSendTiming::TimingFrameFlags::kNotTriggered) {
98 timing->flags = VideoSendTiming::TimingFrameFlags::kInvalid;
99 return;
100 }
101
102 timing->encode_start_delta_ms = VideoSendTiming::GetDeltaCappedMs(
103 image.capture_time_ms_, image.timing_.encode_start_ms);
104 timing->encode_finish_delta_ms = VideoSendTiming::GetDeltaCappedMs(
105 image.capture_time_ms_, image.timing_.encode_finish_ms);
106 timing->packetization_finish_delta_ms = 0;
107 timing->pacer_exit_delta_ms = 0;
108 timing->network_timestamp_delta_ms = 0;
109 timing->network2_timestamp_delta_ms = 0;
110 timing->flags = image.timing_.flags;
111}
112} // namespace
113
114RtpPayloadParams::RtpPayloadParams(const uint32_t ssrc,
115 const RtpPayloadState* state)
philipelbf2b6202018-08-27 14:33:18 +0200116 : ssrc_(ssrc),
117 generic_picture_id_experiment_(
118 field_trial::IsEnabled("WebRTC-GenericPictureId")) {
119 for (auto& spatial_layer : last_shared_frame_id_)
120 spatial_layer.fill(-1);
121
Stefan Holmerf7044682018-07-17 10:16:41 +0200122 Random random(rtc::TimeMicros());
123 state_.picture_id =
124 state ? state->picture_id : (random.Rand<int16_t>() & 0x7FFF);
125 state_.tl0_pic_idx = state ? state->tl0_pic_idx : (random.Rand<uint8_t>());
126}
philipelbf2b6202018-08-27 14:33:18 +0200127
128RtpPayloadParams::RtpPayloadParams(const RtpPayloadParams& other) = default;
129
Stefan Holmerf7044682018-07-17 10:16:41 +0200130RtpPayloadParams::~RtpPayloadParams() {}
131
132RTPVideoHeader RtpPayloadParams::GetRtpVideoHeader(
133 const EncodedImage& image,
philipelbf2b6202018-08-27 14:33:18 +0200134 const CodecSpecificInfo* codec_specific_info,
135 int64_t shared_frame_id) {
Stefan Holmerf7044682018-07-17 10:16:41 +0200136 RTPVideoHeader rtp_video_header;
137 if (codec_specific_info) {
Niels Möllerd3b8c632018-08-27 15:33:42 +0200138 PopulateRtpWithCodecSpecifics(*codec_specific_info, image.SpatialIndex(),
139 &rtp_video_header);
Stefan Holmerf7044682018-07-17 10:16:41 +0200140 }
141 rtp_video_header.rotation = image.rotation_;
142 rtp_video_header.content_type = image.content_type_;
143 rtp_video_header.playout_delay = image.playout_delay_;
144
145 SetVideoTiming(image, &rtp_video_header.video_timing);
146
philipelbf2b6202018-08-27 14:33:18 +0200147 const bool is_keyframe = image._frameType == kVideoFrameKey;
Stefan Holmerf7044682018-07-17 10:16:41 +0200148 const bool first_frame_in_picture =
149 (codec_specific_info && codec_specific_info->codecType == kVideoCodecVP9)
150 ? codec_specific_info->codecSpecific.VP9.first_frame_in_picture
151 : true;
philipelbf2b6202018-08-27 14:33:18 +0200152
153 SetCodecSpecific(&rtp_video_header, first_frame_in_picture);
154 SetGeneric(shared_frame_id, is_keyframe, &rtp_video_header);
155
Stefan Holmerf7044682018-07-17 10:16:41 +0200156 return rtp_video_header;
157}
158
159uint32_t RtpPayloadParams::ssrc() const {
160 return ssrc_;
161}
162
163RtpPayloadState RtpPayloadParams::state() const {
164 return state_;
165}
166
philipelbf2b6202018-08-27 14:33:18 +0200167void RtpPayloadParams::SetCodecSpecific(RTPVideoHeader* rtp_video_header,
168 bool first_frame_in_picture) {
Stefan Holmerf7044682018-07-17 10:16:41 +0200169 // Always set picture id. Set tl0_pic_idx iff temporal index is set.
170 if (first_frame_in_picture) {
171 state_.picture_id = (static_cast<uint16_t>(state_.picture_id) + 1) & 0x7FFF;
172 }
173 if (rtp_video_header->codec == kVideoCodecVP8) {
174 rtp_video_header->vp8().pictureId = state_.picture_id;
175
176 if (rtp_video_header->vp8().temporalIdx != kNoTemporalIdx) {
177 if (rtp_video_header->vp8().temporalIdx == 0) {
178 ++state_.tl0_pic_idx;
179 }
180 rtp_video_header->vp8().tl0PicIdx = state_.tl0_pic_idx;
181 }
182 }
183 if (rtp_video_header->codec == kVideoCodecVP9) {
philipel29d88462018-08-08 14:26:00 +0200184 auto& vp9_header =
185 absl::get<RTPVideoHeaderVP9>(rtp_video_header->video_type_header);
186 vp9_header.picture_id = state_.picture_id;
Stefan Holmerf7044682018-07-17 10:16:41 +0200187
188 // Note that in the case that we have no temporal layers but we do have
189 // spatial layers, packets will carry layering info with a temporal_idx of
190 // zero, and we then have to set and increment tl0_pic_idx.
philipel29d88462018-08-08 14:26:00 +0200191 if (vp9_header.temporal_idx != kNoTemporalIdx ||
192 vp9_header.spatial_idx != kNoSpatialIdx) {
Stefan Holmerf7044682018-07-17 10:16:41 +0200193 if (first_frame_in_picture &&
philipel29d88462018-08-08 14:26:00 +0200194 (vp9_header.temporal_idx == 0 ||
195 vp9_header.temporal_idx == kNoTemporalIdx)) {
Stefan Holmerf7044682018-07-17 10:16:41 +0200196 ++state_.tl0_pic_idx;
197 }
philipel29d88462018-08-08 14:26:00 +0200198 vp9_header.tl0_pic_idx = state_.tl0_pic_idx;
Stefan Holmerf7044682018-07-17 10:16:41 +0200199 }
200 }
philipelbf2b6202018-08-27 14:33:18 +0200201 // There are currently two generic descriptors in WebRTC. The old descriptor
202 // can not share a picture id space between simulcast streams, so we use the
203 // |picture_id| in this case. We let the |picture_id| tag along in |frame_id|
204 // until the old generic format can be removed.
205 // TODO(philipel): Remove this when the new generic format has been fully
206 // implemented.
207 if (generic_picture_id_experiment_ &&
208 rtp_video_header->codec == kVideoCodecGeneric) {
209 rtp_video_header->generic.emplace().frame_id = state_.picture_id;
210 }
Stefan Holmerf7044682018-07-17 10:16:41 +0200211}
philipelbf2b6202018-08-27 14:33:18 +0200212
213void RtpPayloadParams::SetGeneric(int64_t frame_id,
214 bool is_keyframe,
215 RTPVideoHeader* rtp_video_header) {
216 if (rtp_video_header->codec == kVideoCodecVP8) {
217 Vp8ToGeneric(frame_id, is_keyframe, rtp_video_header);
218 }
219
220 // TODO(philipel): Implement VP9 to new generic descriptor.
221 // TODO(philipel): Implement H264 to new generic descriptor.
222 // TODO(philipel): Implement generic codec to new generic descriptor.
223}
224
225void RtpPayloadParams::Vp8ToGeneric(int64_t shared_frame_id,
226 bool is_keyframe,
227 RTPVideoHeader* rtp_video_header) {
228 const auto& vp8_header =
229 absl::get<RTPVideoHeaderVP8>(rtp_video_header->video_type_header);
230 const int spatial_index = 0;
231 const int temporal_index =
232 vp8_header.temporalIdx != kNoTemporalIdx ? vp8_header.temporalIdx : 0;
233
234 if (temporal_index >= RtpGenericFrameDescriptor::kMaxTemporalLayers ||
235 spatial_index >= RtpGenericFrameDescriptor::kMaxSpatialLayers) {
236 RTC_LOG(LS_WARNING) << "Temporal and/or spatial index is too high to be "
237 "used with generic frame descriptor.";
238 return;
239 }
240
241 RTPVideoHeader::GenericDescriptorInfo& generic =
242 rtp_video_header->generic.emplace();
243
244 generic.frame_id = shared_frame_id;
245 generic.spatial_index = spatial_index;
246 generic.temporal_index = temporal_index;
247
248 if (is_keyframe) {
249 RTC_DCHECK_EQ(temporal_index, 0);
250 last_shared_frame_id_[spatial_index].fill(-1);
251 last_shared_frame_id_[spatial_index][temporal_index] = shared_frame_id;
252 return;
253 }
254
255 if (vp8_header.layerSync) {
256 int64_t tl0_frame_id = last_shared_frame_id_[spatial_index][0];
257
258 for (int i = 1; i < RtpGenericFrameDescriptor::kMaxTemporalLayers; ++i) {
259 if (last_shared_frame_id_[spatial_index][i] < tl0_frame_id) {
260 last_shared_frame_id_[spatial_index][i] = -1;
261 }
262 }
263
264 RTC_DCHECK_GE(tl0_frame_id, 0);
265 RTC_DCHECK_LT(tl0_frame_id, shared_frame_id);
266 generic.dependencies.push_back(tl0_frame_id);
267 } else {
268 for (int i = 0; i <= temporal_index; ++i) {
269 int64_t frame_id = last_shared_frame_id_[spatial_index][i];
270
271 if (frame_id != -1) {
272 RTC_DCHECK_LT(frame_id, shared_frame_id);
273 generic.dependencies.push_back(frame_id);
274 }
275 }
276 }
277
278 last_shared_frame_id_[spatial_index][temporal_index] = shared_frame_id;
279}
280
Stefan Holmerf7044682018-07-17 10:16:41 +0200281} // namespace webrtc