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 | |
Peter Boström | 7623ce4 | 2015-12-09 12:13:30 +0100 | [diff] [blame] | 11 | #include "webrtc/video/payload_router.h" |
mflodman@webrtc.org | 02270cd | 2015-02-06 13:10:19 +0000 | [diff] [blame] | 12 | |
| 13 | #include "webrtc/base/checks.h" |
Henrik Kjellander | ff761fb | 2015-11-04 08:31:52 +0100 | [diff] [blame] | 14 | #include "webrtc/modules/rtp_rtcp/include/rtp_rtcp.h" |
| 15 | #include "webrtc/modules/rtp_rtcp/include/rtp_rtcp_defines.h" |
kjellander | 02b3d27 | 2016-04-20 05:05:54 -0700 | [diff] [blame] | 16 | #include "webrtc/modules/video_coding/include/video_codec_interface.h" |
mflodman@webrtc.org | 02270cd | 2015-02-06 13:10:19 +0000 | [diff] [blame] | 17 | |
| 18 | namespace webrtc { |
| 19 | |
kjellander | 02b3d27 | 2016-04-20 05:05:54 -0700 | [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; |
| 78 | return; |
| 79 | case kVideoCodecGeneric: |
| 80 | rtp->codec = kRtpVideoGeneric; |
| 81 | rtp->simulcastIdx = info->codecSpecific.generic.simulcast_idx; |
| 82 | return; |
| 83 | default: |
| 84 | return; |
| 85 | } |
| 86 | } |
perkj | bc75d97 | 2016-05-02 06:31:25 -0700 | [diff] [blame] | 87 | |
kjellander | 02b3d27 | 2016-04-20 05:05:54 -0700 | [diff] [blame] | 88 | } // namespace |
| 89 | |
| 90 | PayloadRouter::PayloadRouter(const std::vector<RtpRtcp*>& rtp_modules, |
| 91 | int payload_type) |
| 92 | : active_(false), |
kjellander | 02b3d27 | 2016-04-20 05:05:54 -0700 | [diff] [blame] | 93 | rtp_modules_(rtp_modules), |
| 94 | payload_type_(payload_type) { |
Per | 83d0910 | 2016-04-15 14:59:13 +0200 | [diff] [blame] | 95 | } |
mflodman@webrtc.org | 02270cd | 2015-02-06 13:10:19 +0000 | [diff] [blame] | 96 | |
| 97 | PayloadRouter::~PayloadRouter() {} |
| 98 | |
mflodman@webrtc.org | a4ef2ce | 2015-02-12 09:54:18 +0000 | [diff] [blame] | 99 | size_t PayloadRouter::DefaultMaxPayloadLength() { |
| 100 | const size_t kIpUdpSrtpLength = 44; |
| 101 | return IP_PACKET_SIZE - kIpUdpSrtpLength; |
| 102 | } |
| 103 | |
mflodman@webrtc.org | 02270cd | 2015-02-06 13:10:19 +0000 | [diff] [blame] | 104 | void PayloadRouter::set_active(bool active) { |
Tommi | 97888bd | 2016-01-21 23:24:59 +0100 | [diff] [blame] | 105 | rtc::CritScope lock(&crit_); |
Peter Boström | 8b79b07 | 2016-02-26 16:31:37 +0100 | [diff] [blame] | 106 | if (active_ == active) |
| 107 | return; |
mflodman@webrtc.org | 02270cd | 2015-02-06 13:10:19 +0000 | [diff] [blame] | 108 | active_ = active; |
Per | 512ecb3 | 2016-09-23 15:52:06 +0200 | [diff] [blame] | 109 | |
| 110 | for (auto& module : rtp_modules_) { |
| 111 | module->SetSendingStatus(active_); |
| 112 | module->SetSendingMediaStatus(active_); |
| 113 | } |
mflodman@webrtc.org | 02270cd | 2015-02-06 13:10:19 +0000 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | bool PayloadRouter::active() { |
Tommi | 97888bd | 2016-01-21 23:24:59 +0100 | [diff] [blame] | 117 | rtc::CritScope lock(&crit_); |
mflodman@webrtc.org | 47d657b | 2015-02-19 10:29:32 +0000 | [diff] [blame] | 118 | return active_ && !rtp_modules_.empty(); |
mflodman@webrtc.org | 02270cd | 2015-02-06 13:10:19 +0000 | [diff] [blame] | 119 | } |
| 120 | |
Sergey Ulanov | 525df3f | 2016-08-02 17:46:41 -0700 | [diff] [blame] | 121 | EncodedImageCallback::Result PayloadRouter::OnEncodedImage( |
| 122 | const EncodedImage& encoded_image, |
| 123 | const CodecSpecificInfo* codec_specific_info, |
| 124 | const RTPFragmentationHeader* fragmentation) { |
Tommi | 97888bd | 2016-01-21 23:24:59 +0100 | [diff] [blame] | 125 | rtc::CritScope lock(&crit_); |
Peter Boström | 8b79b07 | 2016-02-26 16:31:37 +0100 | [diff] [blame] | 126 | RTC_DCHECK(!rtp_modules_.empty()); |
Per | 512ecb3 | 2016-09-23 15:52:06 +0200 | [diff] [blame] | 127 | if (!active_) |
Sergey Ulanov | 525df3f | 2016-08-02 17:46:41 -0700 | [diff] [blame] | 128 | return Result(Result::ERROR_SEND_FAILED); |
mflodman@webrtc.org | 50e2816 | 2015-02-23 07:45:11 +0000 | [diff] [blame] | 129 | |
kjellander | 02b3d27 | 2016-04-20 05:05:54 -0700 | [diff] [blame] | 130 | RTPVideoHeader rtp_video_header; |
| 131 | memset(&rtp_video_header, 0, sizeof(RTPVideoHeader)); |
| 132 | if (codec_specific_info) |
| 133 | CopyCodecSpecific(codec_specific_info, &rtp_video_header); |
| 134 | rtp_video_header.rotation = encoded_image.rotation_; |
isheriff | 6b4b5f3 | 2016-06-08 00:24:21 -0700 | [diff] [blame] | 135 | rtp_video_header.playout_delay = encoded_image.playout_delay_; |
kjellander | 02b3d27 | 2016-04-20 05:05:54 -0700 | [diff] [blame] | 136 | |
sergeyu | 7b9feee | 2016-11-17 16:16:14 -0800 | [diff] [blame] | 137 | int stream_index = rtp_video_header.simulcastIdx; |
| 138 | RTC_DCHECK_LT(stream_index, rtp_modules_.size()); |
Sergey Ulanov | 525df3f | 2016-08-02 17:46:41 -0700 | [diff] [blame] | 139 | uint32_t frame_id; |
sergeyu | 7b9feee | 2016-11-17 16:16:14 -0800 | [diff] [blame] | 140 | bool send_result = rtp_modules_[stream_index]->SendOutgoingData( |
kjellander | 02b3d27 | 2016-04-20 05:05:54 -0700 | [diff] [blame] | 141 | encoded_image._frameType, payload_type_, encoded_image._timeStamp, |
| 142 | encoded_image.capture_time_ms_, encoded_image._buffer, |
Sergey Ulanov | 525df3f | 2016-08-02 17:46:41 -0700 | [diff] [blame] | 143 | encoded_image._length, fragmentation, &rtp_video_header, &frame_id); |
sergeyu | 7b9feee | 2016-11-17 16:16:14 -0800 | [diff] [blame] | 144 | if (!send_result) |
Sergey Ulanov | 525df3f | 2016-08-02 17:46:41 -0700 | [diff] [blame] | 145 | return Result(Result::ERROR_SEND_FAILED); |
| 146 | |
| 147 | return Result(Result::OK, frame_id); |
mflodman@webrtc.org | a4ef2ce | 2015-02-12 09:54:18 +0000 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | size_t PayloadRouter::MaxPayloadLength() const { |
| 151 | size_t min_payload_length = DefaultMaxPayloadLength(); |
Tommi | 97888bd | 2016-01-21 23:24:59 +0100 | [diff] [blame] | 152 | rtc::CritScope lock(&crit_); |
Per | 512ecb3 | 2016-09-23 15:52:06 +0200 | [diff] [blame] | 153 | for (size_t i = 0; i < rtp_modules_.size(); ++i) { |
Peter Boström | 8b79b07 | 2016-02-26 16:31:37 +0100 | [diff] [blame] | 154 | size_t module_payload_length = rtp_modules_[i]->MaxDataPayloadLength(); |
mflodman@webrtc.org | a4ef2ce | 2015-02-12 09:54:18 +0000 | [diff] [blame] | 155 | if (module_payload_length < min_payload_length) |
| 156 | min_payload_length = module_payload_length; |
| 157 | } |
| 158 | return min_payload_length; |
mflodman@webrtc.org | 02270cd | 2015-02-06 13:10:19 +0000 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | } // namespace webrtc |