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 | |
| 11 | #include "webrtc/video_engine/payload_router.h" |
| 12 | |
| 13 | #include "webrtc/base/checks.h" |
| 14 | #include "webrtc/modules/rtp_rtcp/interface/rtp_rtcp.h" |
mflodman@webrtc.org | a4ef2ce | 2015-02-12 09:54:18 +0000 | [diff] [blame] | 15 | #include "webrtc/modules/rtp_rtcp/interface/rtp_rtcp_defines.h" |
mflodman@webrtc.org | 02270cd | 2015-02-06 13:10:19 +0000 | [diff] [blame] | 16 | #include "webrtc/system_wrappers/interface/critical_section_wrapper.h" |
| 17 | |
| 18 | namespace webrtc { |
| 19 | |
| 20 | PayloadRouter::PayloadRouter() |
| 21 | : crit_(CriticalSectionWrapper::CreateCriticalSection()), |
| 22 | active_(false) {} |
| 23 | |
| 24 | PayloadRouter::~PayloadRouter() {} |
| 25 | |
mflodman@webrtc.org | a4ef2ce | 2015-02-12 09:54:18 +0000 | [diff] [blame] | 26 | size_t PayloadRouter::DefaultMaxPayloadLength() { |
| 27 | const size_t kIpUdpSrtpLength = 44; |
| 28 | return IP_PACKET_SIZE - kIpUdpSrtpLength; |
| 29 | } |
| 30 | |
mflodman@webrtc.org | 02270cd | 2015-02-06 13:10:19 +0000 | [diff] [blame] | 31 | void PayloadRouter::SetSendingRtpModules( |
| 32 | const std::list<RtpRtcp*>& rtp_modules) { |
| 33 | CriticalSectionScoped cs(crit_.get()); |
| 34 | rtp_modules_.clear(); |
| 35 | rtp_modules_.reserve(rtp_modules.size()); |
| 36 | for (auto* rtp_module : rtp_modules) { |
| 37 | rtp_modules_.push_back(rtp_module); |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | void PayloadRouter::set_active(bool active) { |
| 42 | CriticalSectionScoped cs(crit_.get()); |
| 43 | active_ = active; |
| 44 | } |
| 45 | |
| 46 | bool PayloadRouter::active() { |
| 47 | CriticalSectionScoped cs(crit_.get()); |
mflodman@webrtc.org | 47d657b | 2015-02-19 10:29:32 +0000 | [diff] [blame] | 48 | return active_ && !rtp_modules_.empty(); |
mflodman@webrtc.org | 02270cd | 2015-02-06 13:10:19 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | bool PayloadRouter::RoutePayload(FrameType frame_type, |
| 52 | int8_t payload_type, |
| 53 | uint32_t time_stamp, |
| 54 | int64_t capture_time_ms, |
| 55 | const uint8_t* payload_data, |
mflodman@webrtc.org | a4ef2ce | 2015-02-12 09:54:18 +0000 | [diff] [blame] | 56 | size_t payload_length, |
mflodman@webrtc.org | 02270cd | 2015-02-06 13:10:19 +0000 | [diff] [blame] | 57 | const RTPFragmentationHeader* fragmentation, |
| 58 | const RTPVideoHeader* rtp_video_hdr) { |
| 59 | CriticalSectionScoped cs(crit_.get()); |
mflodman@webrtc.org | 02270cd | 2015-02-06 13:10:19 +0000 | [diff] [blame] | 60 | if (!active_ || rtp_modules_.empty()) |
| 61 | return false; |
| 62 | |
mflodman@webrtc.org | 50e2816 | 2015-02-23 07:45:11 +0000 | [diff] [blame^] | 63 | // The simulcast index might actually be larger than the number of modules in |
| 64 | // case the encoder was processing a frame during a codec reconfig. |
| 65 | if (rtp_video_hdr != NULL && |
| 66 | rtp_video_hdr->simulcastIdx >= rtp_modules_.size()) |
| 67 | return false; |
| 68 | |
mflodman@webrtc.org | 02270cd | 2015-02-06 13:10:19 +0000 | [diff] [blame] | 69 | int stream_idx = 0; |
| 70 | if (rtp_video_hdr != NULL) |
| 71 | stream_idx = rtp_video_hdr->simulcastIdx; |
| 72 | return rtp_modules_[stream_idx]->SendOutgoingData( |
| 73 | frame_type, payload_type, time_stamp, capture_time_ms, payload_data, |
mflodman@webrtc.org | a4ef2ce | 2015-02-12 09:54:18 +0000 | [diff] [blame] | 74 | payload_length, fragmentation, rtp_video_hdr) == 0 ? true : false; |
| 75 | } |
| 76 | |
mflodman@webrtc.org | 290cb56 | 2015-02-17 10:15:06 +0000 | [diff] [blame] | 77 | bool PayloadRouter::TimeToSendPacket(uint32_t ssrc, |
| 78 | uint16_t sequence_number, |
| 79 | int64_t capture_timestamp, |
| 80 | bool retransmission) { |
| 81 | CriticalSectionScoped cs(crit_.get()); |
| 82 | for (auto* rtp_module : rtp_modules_) { |
| 83 | if (rtp_module->SendingMedia() && ssrc == rtp_module->SSRC()) { |
| 84 | return rtp_module->TimeToSendPacket(ssrc, sequence_number, |
| 85 | capture_timestamp, retransmission); |
| 86 | } |
| 87 | } |
| 88 | return true; |
| 89 | } |
| 90 | |
mflodman@webrtc.org | 50e2816 | 2015-02-23 07:45:11 +0000 | [diff] [blame^] | 91 | void PayloadRouter::SetTargetSendBitrates( |
| 92 | const std::vector<uint32_t>& stream_bitrates) { |
| 93 | CriticalSectionScoped cs(crit_.get()); |
| 94 | if (stream_bitrates.size() < rtp_modules_.size()) { |
| 95 | // There can be a size mis-match during codec reconfiguration. |
| 96 | return; |
| 97 | } |
| 98 | int idx = 0; |
| 99 | for (auto* rtp_module : rtp_modules_) { |
| 100 | rtp_module->SetTargetSendBitrate(stream_bitrates[idx++]); |
| 101 | } |
| 102 | } |
| 103 | |
mflodman@webrtc.org | 290cb56 | 2015-02-17 10:15:06 +0000 | [diff] [blame] | 104 | size_t PayloadRouter::TimeToSendPadding(size_t bytes) { |
| 105 | CriticalSectionScoped cs(crit_.get()); |
| 106 | for(auto* rtp_module : rtp_modules_) { |
| 107 | if (rtp_module->SendingMedia()) |
| 108 | return rtp_module->TimeToSendPadding(bytes); |
| 109 | } |
| 110 | return 0; |
| 111 | } |
| 112 | |
mflodman@webrtc.org | a4ef2ce | 2015-02-12 09:54:18 +0000 | [diff] [blame] | 113 | size_t PayloadRouter::MaxPayloadLength() const { |
| 114 | size_t min_payload_length = DefaultMaxPayloadLength(); |
| 115 | CriticalSectionScoped cs(crit_.get()); |
| 116 | for (auto* rtp_module : rtp_modules_) { |
| 117 | size_t module_payload_length = rtp_module->MaxDataPayloadLength(); |
| 118 | if (module_payload_length < min_payload_length) |
| 119 | min_payload_length = module_payload_length; |
| 120 | } |
| 121 | return min_payload_length; |
mflodman@webrtc.org | 02270cd | 2015-02-06 13:10:19 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | } // namespace webrtc |