mflodman@webrtc.org | 02270cd | 2015-02-06 13:10:19 | [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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 11 | #include "video/payload_router.h" |
mflodman@webrtc.org | 02270cd | 2015-02-06 13:10:19 | [diff] [blame] | 12 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [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" |
mflodman@webrtc.org | 02270cd | 2015-02-06 13:10:19 | [diff] [blame] | 17 | |
| 18 | namespace webrtc { |
| 19 | |
kjellander | 02b3d27 | 2016-04-20 12:05:54 | [diff] [blame] | 20 | namespace { |
| 21 | // Map information from info into rtp. |
| 22 | void CopyCodecSpecific(const CodecSpecificInfo* info, RTPVideoHeader* rtp) { |
| 23 | RTC_DCHECK(info); |
| 24 | switch (info->codecType) { |
| 25 | case kVideoCodecVP8: { |
| 26 | rtp->codec = kRtpVideoVp8; |
| 27 | rtp->codecHeader.VP8.InitRTPVideoHeaderVP8(); |
| 28 | rtp->codecHeader.VP8.pictureId = info->codecSpecific.VP8.pictureId; |
| 29 | rtp->codecHeader.VP8.nonReference = info->codecSpecific.VP8.nonReference; |
| 30 | rtp->codecHeader.VP8.temporalIdx = info->codecSpecific.VP8.temporalIdx; |
| 31 | rtp->codecHeader.VP8.layerSync = info->codecSpecific.VP8.layerSync; |
| 32 | rtp->codecHeader.VP8.tl0PicIdx = info->codecSpecific.VP8.tl0PicIdx; |
| 33 | rtp->codecHeader.VP8.keyIdx = info->codecSpecific.VP8.keyIdx; |
| 34 | rtp->simulcastIdx = info->codecSpecific.VP8.simulcastIdx; |
| 35 | return; |
| 36 | } |
| 37 | case kVideoCodecVP9: { |
| 38 | rtp->codec = kRtpVideoVp9; |
| 39 | rtp->codecHeader.VP9.InitRTPVideoHeaderVP9(); |
| 40 | rtp->codecHeader.VP9.inter_pic_predicted = |
| 41 | info->codecSpecific.VP9.inter_pic_predicted; |
| 42 | rtp->codecHeader.VP9.flexible_mode = |
| 43 | info->codecSpecific.VP9.flexible_mode; |
| 44 | rtp->codecHeader.VP9.ss_data_available = |
| 45 | info->codecSpecific.VP9.ss_data_available; |
| 46 | rtp->codecHeader.VP9.picture_id = info->codecSpecific.VP9.picture_id; |
| 47 | rtp->codecHeader.VP9.tl0_pic_idx = info->codecSpecific.VP9.tl0_pic_idx; |
| 48 | rtp->codecHeader.VP9.temporal_idx = info->codecSpecific.VP9.temporal_idx; |
| 49 | rtp->codecHeader.VP9.spatial_idx = info->codecSpecific.VP9.spatial_idx; |
| 50 | rtp->codecHeader.VP9.temporal_up_switch = |
| 51 | info->codecSpecific.VP9.temporal_up_switch; |
| 52 | rtp->codecHeader.VP9.inter_layer_predicted = |
| 53 | info->codecSpecific.VP9.inter_layer_predicted; |
| 54 | rtp->codecHeader.VP9.gof_idx = info->codecSpecific.VP9.gof_idx; |
| 55 | rtp->codecHeader.VP9.num_spatial_layers = |
| 56 | info->codecSpecific.VP9.num_spatial_layers; |
| 57 | |
| 58 | if (info->codecSpecific.VP9.ss_data_available) { |
| 59 | rtp->codecHeader.VP9.spatial_layer_resolution_present = |
| 60 | 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) { |
| 64 | rtp->codecHeader.VP9.width[i] = info->codecSpecific.VP9.width[i]; |
| 65 | rtp->codecHeader.VP9.height[i] = info->codecSpecific.VP9.height[i]; |
| 66 | } |
| 67 | } |
| 68 | rtp->codecHeader.VP9.gof.CopyGofInfoVP9(info->codecSpecific.VP9.gof); |
| 69 | } |
| 70 | |
| 71 | rtp->codecHeader.VP9.num_ref_pics = info->codecSpecific.VP9.num_ref_pics; |
| 72 | for (int i = 0; i < info->codecSpecific.VP9.num_ref_pics; ++i) |
| 73 | rtp->codecHeader.VP9.pid_diff[i] = info->codecSpecific.VP9.p_diff[i]; |
| 74 | return; |
| 75 | } |
| 76 | case kVideoCodecH264: |
| 77 | rtp->codec = kRtpVideoH264; |
hta | 9aa9688 | 2016-12-06 13:36:03 | [diff] [blame] | 78 | rtp->codecHeader.H264.packetization_mode = |
| 79 | info->codecSpecific.H264.packetization_mode; |
kjellander | 02b3d27 | 2016-04-20 12:05:54 | [diff] [blame] | 80 | return; |
| 81 | case kVideoCodecGeneric: |
| 82 | rtp->codec = kRtpVideoGeneric; |
| 83 | rtp->simulcastIdx = info->codecSpecific.generic.simulcast_idx; |
| 84 | return; |
| 85 | default: |
| 86 | return; |
| 87 | } |
| 88 | } |
perkj | bc75d97 | 2016-05-02 13:31:25 | [diff] [blame] | 89 | |
kjellander | 02b3d27 | 2016-04-20 12:05:54 | [diff] [blame] | 90 | } // namespace |
| 91 | |
| 92 | PayloadRouter::PayloadRouter(const std::vector<RtpRtcp*>& rtp_modules, |
| 93 | int payload_type) |
| 94 | : active_(false), |
kjellander | 02b3d27 | 2016-04-20 12:05:54 | [diff] [blame] | 95 | rtp_modules_(rtp_modules), |
| 96 | payload_type_(payload_type) { |
Per | 83d0910 | 2016-04-15 12:59:13 | [diff] [blame] | 97 | } |
mflodman@webrtc.org | 02270cd | 2015-02-06 13:10:19 | [diff] [blame] | 98 | |
| 99 | PayloadRouter::~PayloadRouter() {} |
| 100 | |
sprang | 1a646ee | 2016-12-01 14:34:11 | [diff] [blame] | 101 | void PayloadRouter::SetActive(bool active) { |
Tommi | 97888bd | 2016-01-21 22:24:59 | [diff] [blame] | 102 | rtc::CritScope lock(&crit_); |
Peter Boström | 8b79b07 | 2016-02-26 15:31:37 | [diff] [blame] | 103 | if (active_ == active) |
| 104 | return; |
mflodman@webrtc.org | 02270cd | 2015-02-06 13:10:19 | [diff] [blame] | 105 | active_ = active; |
Per | 512ecb3 | 2016-09-23 13:52:06 | [diff] [blame] | 106 | |
| 107 | for (auto& module : rtp_modules_) { |
| 108 | module->SetSendingStatus(active_); |
| 109 | module->SetSendingMediaStatus(active_); |
| 110 | } |
mflodman@webrtc.org | 02270cd | 2015-02-06 13:10:19 | [diff] [blame] | 111 | } |
| 112 | |
sprang | 1a646ee | 2016-12-01 14:34:11 | [diff] [blame] | 113 | bool PayloadRouter::IsActive() { |
Tommi | 97888bd | 2016-01-21 22:24:59 | [diff] [blame] | 114 | rtc::CritScope lock(&crit_); |
mflodman@webrtc.org | 47d657b | 2015-02-19 10:29:32 | [diff] [blame] | 115 | return active_ && !rtp_modules_.empty(); |
mflodman@webrtc.org | 02270cd | 2015-02-06 13:10:19 | [diff] [blame] | 116 | } |
| 117 | |
Sergey Ulanov | 525df3f | 2016-08-03 00:46:41 | [diff] [blame] | 118 | EncodedImageCallback::Result PayloadRouter::OnEncodedImage( |
| 119 | const EncodedImage& encoded_image, |
| 120 | const CodecSpecificInfo* codec_specific_info, |
| 121 | const RTPFragmentationHeader* fragmentation) { |
Tommi | 97888bd | 2016-01-21 22:24:59 | [diff] [blame] | 122 | rtc::CritScope lock(&crit_); |
Peter Boström | 8b79b07 | 2016-02-26 15:31:37 | [diff] [blame] | 123 | RTC_DCHECK(!rtp_modules_.empty()); |
Per | 512ecb3 | 2016-09-23 13:52:06 | [diff] [blame] | 124 | if (!active_) |
Sergey Ulanov | 525df3f | 2016-08-03 00:46:41 | [diff] [blame] | 125 | return Result(Result::ERROR_SEND_FAILED); |
mflodman@webrtc.org | 50e2816 | 2015-02-23 07:45:11 | [diff] [blame] | 126 | |
kjellander | 02b3d27 | 2016-04-20 12:05:54 | [diff] [blame] | 127 | RTPVideoHeader rtp_video_header; |
| 128 | memset(&rtp_video_header, 0, sizeof(RTPVideoHeader)); |
| 129 | if (codec_specific_info) |
| 130 | CopyCodecSpecific(codec_specific_info, &rtp_video_header); |
| 131 | rtp_video_header.rotation = encoded_image.rotation_; |
ilnik | 00d802b | 2017-04-11 17:34:31 | [diff] [blame] | 132 | rtp_video_header.content_type = encoded_image.content_type_; |
sprang | ba050a6 | 2017-08-18 09:51:12 | [diff] [blame] | 133 | if (encoded_image.timing_.flags != TimingFrameFlags::kInvalid) { |
ilnik | 04f4d12 | 2017-06-19 14:18:55 | [diff] [blame] | 134 | rtp_video_header.video_timing.encode_start_delta_ms = |
ilnik | 2edc684 | 2017-07-06 10:06:50 | [diff] [blame] | 135 | VideoSendTiming::GetDeltaCappedMs( |
| 136 | encoded_image.capture_time_ms_, |
| 137 | encoded_image.timing_.encode_start_ms); |
ilnik | 04f4d12 | 2017-06-19 14:18:55 | [diff] [blame] | 138 | rtp_video_header.video_timing.encode_finish_delta_ms = |
ilnik | 2edc684 | 2017-07-06 10:06:50 | [diff] [blame] | 139 | VideoSendTiming::GetDeltaCappedMs( |
| 140 | encoded_image.capture_time_ms_, |
| 141 | encoded_image.timing_.encode_finish_ms); |
ilnik | 04f4d12 | 2017-06-19 14:18:55 | [diff] [blame] | 142 | rtp_video_header.video_timing.packetization_finish_delta_ms = 0; |
| 143 | rtp_video_header.video_timing.pacer_exit_delta_ms = 0; |
| 144 | rtp_video_header.video_timing.network_timstamp_delta_ms = 0; |
| 145 | rtp_video_header.video_timing.network2_timstamp_delta_ms = 0; |
ilnik | 04f4d12 | 2017-06-19 14:18:55 | [diff] [blame] | 146 | } |
sprang | ba050a6 | 2017-08-18 09:51:12 | [diff] [blame] | 147 | rtp_video_header.video_timing.flags = encoded_image.timing_.flags; |
isheriff | 6b4b5f3 | 2016-06-08 07:24:21 | [diff] [blame] | 148 | rtp_video_header.playout_delay = encoded_image.playout_delay_; |
kjellander | 02b3d27 | 2016-04-20 12:05:54 | [diff] [blame] | 149 | |
sergeyu | 7b9feee | 2016-11-18 00:16:14 | [diff] [blame] | 150 | int stream_index = rtp_video_header.simulcastIdx; |
| 151 | RTC_DCHECK_LT(stream_index, rtp_modules_.size()); |
Sergey Ulanov | 525df3f | 2016-08-03 00:46:41 | [diff] [blame] | 152 | uint32_t frame_id; |
sergeyu | 7b9feee | 2016-11-18 00:16:14 | [diff] [blame] | 153 | bool send_result = rtp_modules_[stream_index]->SendOutgoingData( |
kjellander | 02b3d27 | 2016-04-20 12:05:54 | [diff] [blame] | 154 | encoded_image._frameType, payload_type_, encoded_image._timeStamp, |
| 155 | encoded_image.capture_time_ms_, encoded_image._buffer, |
Sergey Ulanov | 525df3f | 2016-08-03 00:46:41 | [diff] [blame] | 156 | encoded_image._length, fragmentation, &rtp_video_header, &frame_id); |
sergeyu | 7b9feee | 2016-11-18 00:16:14 | [diff] [blame] | 157 | if (!send_result) |
Sergey Ulanov | 525df3f | 2016-08-03 00:46:41 | [diff] [blame] | 158 | return Result(Result::ERROR_SEND_FAILED); |
| 159 | |
| 160 | return Result(Result::OK, frame_id); |
mflodman@webrtc.org | a4ef2ce | 2015-02-12 09:54:18 | [diff] [blame] | 161 | } |
| 162 | |
sprang | 1a646ee | 2016-12-01 14:34:11 | [diff] [blame] | 163 | void PayloadRouter::OnBitrateAllocationUpdated( |
| 164 | const BitrateAllocation& bitrate) { |
| 165 | rtc::CritScope lock(&crit_); |
| 166 | if (IsActive()) { |
| 167 | if (rtp_modules_.size() == 1) { |
| 168 | // If spatial scalability is enabled, it is covered by a single stream. |
| 169 | rtp_modules_[0]->SetVideoBitrateAllocation(bitrate); |
| 170 | } else { |
| 171 | // Simulcast is in use, split the BitrateAllocation into one struct per |
| 172 | // rtp stream, moving over the temporal layer allocation. |
| 173 | for (size_t si = 0; si < rtp_modules_.size(); ++si) { |
sprang | d0fc37a | 2017-06-22 12:40:25 | [diff] [blame] | 174 | // Don't send empty TargetBitrate messages on streams not being relayed. |
| 175 | if (bitrate.GetSpatialLayerSum(si) == 0) |
| 176 | break; |
| 177 | |
sprang | 1a646ee | 2016-12-01 14:34:11 | [diff] [blame] | 178 | BitrateAllocation layer_bitrate; |
| 179 | for (int tl = 0; tl < kMaxTemporalStreams; ++tl) |
| 180 | layer_bitrate.SetBitrate(0, tl, bitrate.GetBitrate(si, tl)); |
| 181 | rtp_modules_[si]->SetVideoBitrateAllocation(layer_bitrate); |
| 182 | } |
| 183 | } |
| 184 | } |
| 185 | } |
| 186 | |
mflodman@webrtc.org | 02270cd | 2015-02-06 13:10:19 | [diff] [blame] | 187 | } // namespace webrtc |