mflodman@webrtc.org | 02270cd | 2015-02-06 13:10:19 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015 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 | |
Stefan Holmer | a2f1533 | 2018-07-11 17:11:31 +0200 | [diff] [blame] | 11 | #include "call/payload_router.h" |
mflodman@webrtc.org | 02270cd | 2015-02-06 13:10:19 +0000 | [diff] [blame] | 12 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 13 | #include "modules/rtp_rtcp/include/rtp_rtcp.h" |
| 14 | #include "modules/rtp_rtcp/include/rtp_rtcp_defines.h" |
| 15 | #include "modules/video_coding/include/video_codec_interface.h" |
| 16 | #include "rtc_base/checks.h" |
JT Teh | c2406e4 | 2018-07-16 20:37:52 +0000 | [diff] [blame] | 17 | #include "rtc_base/random.h" |
| 18 | #include "rtc_base/timeutils.h" |
mflodman@webrtc.org | 02270cd | 2015-02-06 13:10:19 +0000 | [diff] [blame] | 19 | |
| 20 | namespace webrtc { |
| 21 | |
kjellander | 02b3d27 | 2016-04-20 05:05:54 -0700 | [diff] [blame] | 22 | namespace { |
JT Teh | c2406e4 | 2018-07-16 20:37:52 +0000 | [diff] [blame] | 23 | // Map information from info into rtp. |
| 24 | void CopyCodecSpecific(const CodecSpecificInfo* info, RTPVideoHeader* rtp) { |
| 25 | RTC_DCHECK(info); |
| 26 | rtp->codec = info->codecType; |
kjellander | 02b3d27 | 2016-04-20 05:05:54 -0700 | [diff] [blame] | 27 | switch (info->codecType) { |
JT Teh | c2406e4 | 2018-07-16 20:37:52 +0000 | [diff] [blame] | 28 | case kVideoCodecVP8: { |
| 29 | rtp->vp8().InitRTPVideoHeaderVP8(); |
| 30 | rtp->vp8().nonReference = info->codecSpecific.VP8.nonReference; |
| 31 | rtp->vp8().temporalIdx = info->codecSpecific.VP8.temporalIdx; |
| 32 | rtp->vp8().layerSync = info->codecSpecific.VP8.layerSync; |
| 33 | rtp->vp8().keyIdx = info->codecSpecific.VP8.keyIdx; |
| 34 | rtp->simulcastIdx = info->codecSpecific.VP8.simulcastIdx; |
| 35 | return; |
| 36 | } |
| 37 | case kVideoCodecVP9: { |
| 38 | rtp->vp9().InitRTPVideoHeaderVP9(); |
| 39 | rtp->vp9().inter_pic_predicted = |
| 40 | info->codecSpecific.VP9.inter_pic_predicted; |
| 41 | rtp->vp9().flexible_mode = info->codecSpecific.VP9.flexible_mode; |
| 42 | rtp->vp9().ss_data_available = info->codecSpecific.VP9.ss_data_available; |
| 43 | rtp->vp9().non_ref_for_inter_layer_pred = |
| 44 | info->codecSpecific.VP9.non_ref_for_inter_layer_pred; |
| 45 | rtp->vp9().temporal_idx = info->codecSpecific.VP9.temporal_idx; |
| 46 | rtp->vp9().spatial_idx = info->codecSpecific.VP9.spatial_idx; |
| 47 | rtp->vp9().temporal_up_switch = |
| 48 | info->codecSpecific.VP9.temporal_up_switch; |
| 49 | rtp->vp9().inter_layer_predicted = |
| 50 | info->codecSpecific.VP9.inter_layer_predicted; |
| 51 | rtp->vp9().gof_idx = info->codecSpecific.VP9.gof_idx; |
| 52 | rtp->vp9().num_spatial_layers = |
| 53 | info->codecSpecific.VP9.num_spatial_layers; |
| 54 | |
| 55 | if (info->codecSpecific.VP9.ss_data_available) { |
| 56 | rtp->vp9().spatial_layer_resolution_present = |
| 57 | info->codecSpecific.VP9.spatial_layer_resolution_present; |
| 58 | if (info->codecSpecific.VP9.spatial_layer_resolution_present) { |
| 59 | for (size_t i = 0; i < info->codecSpecific.VP9.num_spatial_layers; |
| 60 | ++i) { |
| 61 | rtp->vp9().width[i] = info->codecSpecific.VP9.width[i]; |
| 62 | rtp->vp9().height[i] = info->codecSpecific.VP9.height[i]; |
| 63 | } |
| 64 | } |
| 65 | rtp->vp9().gof.CopyGofInfoVP9(info->codecSpecific.VP9.gof); |
| 66 | } |
| 67 | |
| 68 | rtp->vp9().num_ref_pics = info->codecSpecific.VP9.num_ref_pics; |
| 69 | for (int i = 0; i < info->codecSpecific.VP9.num_ref_pics; ++i) { |
| 70 | rtp->vp9().pid_diff[i] = info->codecSpecific.VP9.p_diff[i]; |
| 71 | } |
| 72 | rtp->vp9().end_of_picture = info->codecSpecific.VP9.end_of_picture; |
| 73 | return; |
| 74 | } |
JT Teh | 5daeff9 | 2018-07-16 17:17:17 +0000 | [diff] [blame] | 75 | case kVideoCodecH264: |
| 76 | rtp->h264().packetization_mode = |
JT Teh | c2406e4 | 2018-07-16 20:37:52 +0000 | [diff] [blame] | 77 | info->codecSpecific.H264.packetization_mode; |
| 78 | rtp->simulcastIdx = info->codecSpecific.H264.simulcast_idx; |
| 79 | return; |
Emircan Uysaler | d7ae3c3 | 2018-01-25 13:01:09 -0800 | [diff] [blame] | 80 | case kVideoCodecMultiplex: |
kjellander | 02b3d27 | 2016-04-20 05:05:54 -0700 | [diff] [blame] | 81 | case kVideoCodecGeneric: |
JT Teh | c2406e4 | 2018-07-16 20:37:52 +0000 | [diff] [blame] | 82 | rtp->codec = kVideoCodecGeneric; |
| 83 | rtp->simulcastIdx = info->codecSpecific.generic.simulcast_idx; |
| 84 | return; |
kjellander | 02b3d27 | 2016-04-20 05:05:54 -0700 | [diff] [blame] | 85 | default: |
JT Teh | c2406e4 | 2018-07-16 20:37:52 +0000 | [diff] [blame] | 86 | return; |
kjellander | 02b3d27 | 2016-04-20 05:05:54 -0700 | [diff] [blame] | 87 | } |
| 88 | } |
JT Teh | c2406e4 | 2018-07-16 20:37:52 +0000 | [diff] [blame] | 89 | |
| 90 | void SetVideoTiming(VideoSendTiming* timing, const EncodedImage& image) { |
| 91 | if (image.timing_.flags == VideoSendTiming::TimingFrameFlags::kInvalid || |
| 92 | image.timing_.flags == VideoSendTiming::TimingFrameFlags::kNotTriggered) { |
| 93 | timing->flags = VideoSendTiming::TimingFrameFlags::kInvalid; |
| 94 | return; |
| 95 | } |
| 96 | |
| 97 | timing->encode_start_delta_ms = VideoSendTiming::GetDeltaCappedMs( |
| 98 | image.capture_time_ms_, image.timing_.encode_start_ms); |
| 99 | timing->encode_finish_delta_ms = VideoSendTiming::GetDeltaCappedMs( |
| 100 | image.capture_time_ms_, image.timing_.encode_finish_ms); |
| 101 | timing->packetization_finish_delta_ms = 0; |
| 102 | timing->pacer_exit_delta_ms = 0; |
| 103 | timing->network_timestamp_delta_ms = 0; |
| 104 | timing->network2_timestamp_delta_ms = 0; |
| 105 | timing->flags = image.timing_.flags; |
| 106 | } |
| 107 | |
kjellander | 02b3d27 | 2016-04-20 05:05:54 -0700 | [diff] [blame] | 108 | } // namespace |
| 109 | |
JT Teh | c2406e4 | 2018-07-16 20:37:52 +0000 | [diff] [blame] | 110 | // State for setting picture id and tl0 pic idx, for VP8 and VP9 |
| 111 | // TODO(nisse): Make these properties not codec specific. |
| 112 | class PayloadRouter::RtpPayloadParams final { |
| 113 | public: |
| 114 | RtpPayloadParams(const uint32_t ssrc, const RtpPayloadState* state) |
| 115 | : ssrc_(ssrc) { |
| 116 | Random random(rtc::TimeMicros()); |
| 117 | state_.picture_id = |
| 118 | state ? state->picture_id : (random.Rand<int16_t>() & 0x7FFF); |
| 119 | state_.tl0_pic_idx = state ? state->tl0_pic_idx : (random.Rand<uint8_t>()); |
| 120 | } |
| 121 | ~RtpPayloadParams() {} |
| 122 | |
| 123 | void Set(RTPVideoHeader* rtp_video_header, bool first_frame_in_picture) { |
| 124 | // Always set picture id. Set tl0_pic_idx iff temporal index is set. |
| 125 | if (first_frame_in_picture) { |
| 126 | state_.picture_id = |
| 127 | (static_cast<uint16_t>(state_.picture_id) + 1) & 0x7FFF; |
| 128 | } |
| 129 | if (rtp_video_header->codec == kVideoCodecVP8) { |
| 130 | rtp_video_header->vp8().pictureId = state_.picture_id; |
| 131 | |
| 132 | if (rtp_video_header->vp8().temporalIdx != kNoTemporalIdx) { |
| 133 | if (rtp_video_header->vp8().temporalIdx == 0) { |
| 134 | ++state_.tl0_pic_idx; |
| 135 | } |
| 136 | rtp_video_header->vp8().tl0PicIdx = state_.tl0_pic_idx; |
| 137 | } |
| 138 | } |
| 139 | if (rtp_video_header->codec == kVideoCodecVP9) { |
| 140 | rtp_video_header->vp9().picture_id = state_.picture_id; |
| 141 | |
| 142 | // Note that in the case that we have no temporal layers but we do have |
| 143 | // spatial layers, packets will carry layering info with a temporal_idx of |
| 144 | // zero, and we then have to set and increment tl0_pic_idx. |
| 145 | if (rtp_video_header->vp9().temporal_idx != kNoTemporalIdx || |
| 146 | rtp_video_header->vp9().spatial_idx != kNoSpatialIdx) { |
| 147 | if (first_frame_in_picture && |
| 148 | (rtp_video_header->vp9().temporal_idx == 0 || |
| 149 | rtp_video_header->vp9().temporal_idx == kNoTemporalIdx)) { |
| 150 | ++state_.tl0_pic_idx; |
| 151 | } |
| 152 | rtp_video_header->vp9().tl0_pic_idx = state_.tl0_pic_idx; |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | uint32_t ssrc() const { return ssrc_; } |
| 158 | |
| 159 | RtpPayloadState state() const { return state_; } |
| 160 | |
| 161 | private: |
| 162 | const uint32_t ssrc_; |
| 163 | RtpPayloadState state_; |
| 164 | }; |
| 165 | |
kjellander | 02b3d27 | 2016-04-20 05:05:54 -0700 | [diff] [blame] | 166 | PayloadRouter::PayloadRouter(const std::vector<RtpRtcp*>& rtp_modules, |
Åsa Persson | 4bece9a | 2017-10-06 10:04:04 +0200 | [diff] [blame] | 167 | const std::vector<uint32_t>& ssrcs, |
| 168 | int payload_type, |
| 169 | const std::map<uint32_t, RtpPayloadState>& states) |
Niels Möller | bb894ff | 2018-03-15 12:28:53 +0100 | [diff] [blame] | 170 | : active_(false), rtp_modules_(rtp_modules), payload_type_(payload_type) { |
Åsa Persson | 4bece9a | 2017-10-06 10:04:04 +0200 | [diff] [blame] | 171 | RTC_DCHECK_EQ(ssrcs.size(), rtp_modules.size()); |
| 172 | // SSRCs are assumed to be sorted in the same order as |rtp_modules|. |
| 173 | for (uint32_t ssrc : ssrcs) { |
| 174 | // Restore state if it previously existed. |
| 175 | const RtpPayloadState* state = nullptr; |
| 176 | auto it = states.find(ssrc); |
| 177 | if (it != states.end()) { |
| 178 | state = &it->second; |
| 179 | } |
| 180 | params_.push_back(RtpPayloadParams(ssrc, state)); |
| 181 | } |
Per | 83d0910 | 2016-04-15 14:59:13 +0200 | [diff] [blame] | 182 | } |
mflodman@webrtc.org | 02270cd | 2015-02-06 13:10:19 +0000 | [diff] [blame] | 183 | |
| 184 | PayloadRouter::~PayloadRouter() {} |
| 185 | |
sprang | 1a646ee | 2016-12-01 06:34:11 -0800 | [diff] [blame] | 186 | void PayloadRouter::SetActive(bool active) { |
Tommi | 97888bd | 2016-01-21 23:24:59 +0100 | [diff] [blame] | 187 | rtc::CritScope lock(&crit_); |
Peter Boström | 8b79b07 | 2016-02-26 16:31:37 +0100 | [diff] [blame] | 188 | if (active_ == active) |
| 189 | return; |
Seth Hampson | cc7125f | 2018-02-02 08:46:16 -0800 | [diff] [blame] | 190 | const std::vector<bool> active_modules(rtp_modules_.size(), active); |
| 191 | SetActiveModules(active_modules); |
| 192 | } |
Per | 512ecb3 | 2016-09-23 15:52:06 +0200 | [diff] [blame] | 193 | |
Seth Hampson | cc7125f | 2018-02-02 08:46:16 -0800 | [diff] [blame] | 194 | void PayloadRouter::SetActiveModules(const std::vector<bool> active_modules) { |
| 195 | rtc::CritScope lock(&crit_); |
| 196 | RTC_DCHECK_EQ(rtp_modules_.size(), active_modules.size()); |
| 197 | active_ = false; |
| 198 | for (size_t i = 0; i < active_modules.size(); ++i) { |
| 199 | if (active_modules[i]) { |
| 200 | active_ = true; |
| 201 | } |
| 202 | // Sends a kRtcpByeCode when going from true to false. |
| 203 | rtp_modules_[i]->SetSendingStatus(active_modules[i]); |
| 204 | // If set to false this module won't send media. |
| 205 | rtp_modules_[i]->SetSendingMediaStatus(active_modules[i]); |
Per | 512ecb3 | 2016-09-23 15:52:06 +0200 | [diff] [blame] | 206 | } |
mflodman@webrtc.org | 02270cd | 2015-02-06 13:10:19 +0000 | [diff] [blame] | 207 | } |
| 208 | |
sprang | 1a646ee | 2016-12-01 06:34:11 -0800 | [diff] [blame] | 209 | bool PayloadRouter::IsActive() { |
Tommi | 97888bd | 2016-01-21 23:24:59 +0100 | [diff] [blame] | 210 | rtc::CritScope lock(&crit_); |
mflodman@webrtc.org | 47d657b | 2015-02-19 10:29:32 +0000 | [diff] [blame] | 211 | return active_ && !rtp_modules_.empty(); |
mflodman@webrtc.org | 02270cd | 2015-02-06 13:10:19 +0000 | [diff] [blame] | 212 | } |
| 213 | |
Åsa Persson | 4bece9a | 2017-10-06 10:04:04 +0200 | [diff] [blame] | 214 | std::map<uint32_t, RtpPayloadState> PayloadRouter::GetRtpPayloadStates() const { |
| 215 | rtc::CritScope lock(&crit_); |
| 216 | std::map<uint32_t, RtpPayloadState> payload_states; |
| 217 | for (const auto& param : params_) { |
| 218 | payload_states[param.ssrc()] = param.state(); |
| 219 | } |
| 220 | return payload_states; |
| 221 | } |
| 222 | |
Sergey Ulanov | 525df3f | 2016-08-02 17:46:41 -0700 | [diff] [blame] | 223 | EncodedImageCallback::Result PayloadRouter::OnEncodedImage( |
| 224 | const EncodedImage& encoded_image, |
| 225 | const CodecSpecificInfo* codec_specific_info, |
| 226 | const RTPFragmentationHeader* fragmentation) { |
Tommi | 97888bd | 2016-01-21 23:24:59 +0100 | [diff] [blame] | 227 | rtc::CritScope lock(&crit_); |
Peter Boström | 8b79b07 | 2016-02-26 16:31:37 +0100 | [diff] [blame] | 228 | RTC_DCHECK(!rtp_modules_.empty()); |
Per | 512ecb3 | 2016-09-23 15:52:06 +0200 | [diff] [blame] | 229 | if (!active_) |
Sergey Ulanov | 525df3f | 2016-08-02 17:46:41 -0700 | [diff] [blame] | 230 | return Result(Result::ERROR_SEND_FAILED); |
mflodman@webrtc.org | 50e2816 | 2015-02-23 07:45:11 +0000 | [diff] [blame] | 231 | |
JT Teh | c2406e4 | 2018-07-16 20:37:52 +0000 | [diff] [blame] | 232 | RTPVideoHeader rtp_video_header; |
| 233 | if (codec_specific_info) |
| 234 | CopyCodecSpecific(codec_specific_info, &rtp_video_header); |
| 235 | |
| 236 | rtp_video_header.rotation = encoded_image.rotation_; |
| 237 | rtp_video_header.content_type = encoded_image.content_type_; |
| 238 | rtp_video_header.playout_delay = encoded_image.playout_delay_; |
| 239 | |
| 240 | SetVideoTiming(&rtp_video_header.video_timing, encoded_image); |
| 241 | |
| 242 | int stream_index = rtp_video_header.simulcastIdx; |
sergeyu | 7b9feee | 2016-11-17 16:16:14 -0800 | [diff] [blame] | 243 | RTC_DCHECK_LT(stream_index, rtp_modules_.size()); |
JT Teh | c2406e4 | 2018-07-16 20:37:52 +0000 | [diff] [blame] | 244 | |
| 245 | // Sets picture id and tl0 pic idx. |
| 246 | const bool first_frame_in_picture = |
| 247 | (codec_specific_info && codec_specific_info->codecType == kVideoCodecVP9) |
| 248 | ? codec_specific_info->codecSpecific.VP9.first_frame_in_picture |
| 249 | : true; |
| 250 | params_[stream_index].Set(&rtp_video_header, first_frame_in_picture); |
Niels Möller | bb894ff | 2018-03-15 12:28:53 +0100 | [diff] [blame] | 251 | |
Sergey Ulanov | 525df3f | 2016-08-02 17:46:41 -0700 | [diff] [blame] | 252 | uint32_t frame_id; |
Seth Hampson | cc7125f | 2018-02-02 08:46:16 -0800 | [diff] [blame] | 253 | if (!rtp_modules_[stream_index]->Sending()) { |
| 254 | // The payload router could be active but this module isn't sending. |
| 255 | return Result(Result::ERROR_SEND_FAILED); |
| 256 | } |
sergeyu | 7b9feee | 2016-11-17 16:16:14 -0800 | [diff] [blame] | 257 | bool send_result = rtp_modules_[stream_index]->SendOutgoingData( |
kjellander | 02b3d27 | 2016-04-20 05:05:54 -0700 | [diff] [blame] | 258 | encoded_image._frameType, payload_type_, encoded_image._timeStamp, |
| 259 | encoded_image.capture_time_ms_, encoded_image._buffer, |
Sergey Ulanov | 525df3f | 2016-08-02 17:46:41 -0700 | [diff] [blame] | 260 | encoded_image._length, fragmentation, &rtp_video_header, &frame_id); |
sergeyu | 7b9feee | 2016-11-17 16:16:14 -0800 | [diff] [blame] | 261 | if (!send_result) |
Sergey Ulanov | 525df3f | 2016-08-02 17:46:41 -0700 | [diff] [blame] | 262 | return Result(Result::ERROR_SEND_FAILED); |
| 263 | |
| 264 | return Result(Result::OK, frame_id); |
mflodman@webrtc.org | a4ef2ce | 2015-02-12 09:54:18 +0000 | [diff] [blame] | 265 | } |
| 266 | |
sprang | 1a646ee | 2016-12-01 06:34:11 -0800 | [diff] [blame] | 267 | void PayloadRouter::OnBitrateAllocationUpdated( |
Erik Språng | 566124a | 2018-04-23 12:32:22 +0200 | [diff] [blame] | 268 | const VideoBitrateAllocation& bitrate) { |
sprang | 1a646ee | 2016-12-01 06:34:11 -0800 | [diff] [blame] | 269 | rtc::CritScope lock(&crit_); |
| 270 | if (IsActive()) { |
| 271 | if (rtp_modules_.size() == 1) { |
| 272 | // If spatial scalability is enabled, it is covered by a single stream. |
| 273 | rtp_modules_[0]->SetVideoBitrateAllocation(bitrate); |
| 274 | } else { |
Erik Språng | 566124a | 2018-04-23 12:32:22 +0200 | [diff] [blame] | 275 | // Simulcast is in use, split the VideoBitrateAllocation into one struct |
| 276 | // per rtp stream, moving over the temporal layer allocation. |
JT Teh | c2406e4 | 2018-07-16 20:37:52 +0000 | [diff] [blame] | 277 | for (size_t si = 0; si < rtp_modules_.size(); ++si) { |
| 278 | // Don't send empty TargetBitrate messages on streams not being relayed. |
| 279 | if (!bitrate.IsSpatialLayerUsed(si)) { |
| 280 | // The next spatial layer could be used if the current one is |
| 281 | // inactive. |
| 282 | continue; |
Seth Hampson | 46e31ba | 2018-01-18 10:39:54 -0800 | [diff] [blame] | 283 | } |
JT Teh | c2406e4 | 2018-07-16 20:37:52 +0000 | [diff] [blame] | 284 | |
| 285 | VideoBitrateAllocation layer_bitrate; |
| 286 | for (int tl = 0; tl < kMaxTemporalStreams; ++tl) { |
| 287 | if (bitrate.HasBitrate(si, tl)) |
| 288 | layer_bitrate.SetBitrate(0, tl, bitrate.GetBitrate(si, tl)); |
| 289 | } |
| 290 | rtp_modules_[si]->SetVideoBitrateAllocation(layer_bitrate); |
sprang | 1a646ee | 2016-12-01 06:34:11 -0800 | [diff] [blame] | 291 | } |
| 292 | } |
| 293 | } |
| 294 | } |
| 295 | |
mflodman@webrtc.org | 02270cd | 2015-02-06 13:10:19 +0000 | [diff] [blame] | 296 | } // namespace webrtc |