blob: 968d82df62a0fa2c7153f49ca70202ea6c05090a [file] [log] [blame]
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +00001/*
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öm7623ce42015-12-09 12:13:30 +010011#include "webrtc/video/payload_router.h"
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +000012
13#include "webrtc/base/checks.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010014#include "webrtc/modules/rtp_rtcp/include/rtp_rtcp.h"
15#include "webrtc/modules/rtp_rtcp/include/rtp_rtcp_defines.h"
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +000016
17namespace webrtc {
18
19PayloadRouter::PayloadRouter()
Peter Boström8b79b072016-02-26 16:31:37 +010020 : active_(false), num_sending_modules_(0) {}
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +000021
22PayloadRouter::~PayloadRouter() {}
23
mflodman@webrtc.orga4ef2ce2015-02-12 09:54:18 +000024size_t PayloadRouter::DefaultMaxPayloadLength() {
25 const size_t kIpUdpSrtpLength = 44;
26 return IP_PACKET_SIZE - kIpUdpSrtpLength;
27}
28
Peter Boström8b79b072016-02-26 16:31:37 +010029void PayloadRouter::Init(
Peter Boström404686a2016-02-11 23:37:26 +010030 const std::vector<RtpRtcp*>& rtp_modules) {
Peter Boström8b79b072016-02-26 16:31:37 +010031 RTC_DCHECK(rtp_modules_.empty());
Peter Boström404686a2016-02-11 23:37:26 +010032 rtp_modules_ = rtp_modules;
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +000033}
34
35void PayloadRouter::set_active(bool active) {
Tommi97888bd2016-01-21 23:24:59 +010036 rtc::CritScope lock(&crit_);
Peter Boström8b79b072016-02-26 16:31:37 +010037 if (active_ == active)
38 return;
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +000039 active_ = active;
Peter Boström8b79b072016-02-26 16:31:37 +010040 UpdateModuleSendingState();
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +000041}
42
43bool PayloadRouter::active() {
Tommi97888bd2016-01-21 23:24:59 +010044 rtc::CritScope lock(&crit_);
mflodman@webrtc.org47d657b2015-02-19 10:29:32 +000045 return active_ && !rtp_modules_.empty();
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +000046}
47
Peter Boström8b79b072016-02-26 16:31:37 +010048void PayloadRouter::SetSendingRtpModules(size_t num_sending_modules) {
49 RTC_DCHECK_LE(num_sending_modules, rtp_modules_.size());
50 rtc::CritScope lock(&crit_);
51 num_sending_modules_ = num_sending_modules;
52 UpdateModuleSendingState();
53}
54
55void PayloadRouter::UpdateModuleSendingState() {
56 for (size_t i = 0; i < num_sending_modules_; ++i) {
57 rtp_modules_[i]->SetSendingStatus(active_);
58 rtp_modules_[i]->SetSendingMediaStatus(active_);
59 }
60 // Disable inactive modules.
61 for (size_t i = num_sending_modules_; i < rtp_modules_.size(); ++i) {
62 rtp_modules_[i]->SetSendingStatus(false);
63 rtp_modules_[i]->SetSendingMediaStatus(false);
64 }
65}
66
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +000067bool PayloadRouter::RoutePayload(FrameType frame_type,
68 int8_t payload_type,
69 uint32_t time_stamp,
70 int64_t capture_time_ms,
71 const uint8_t* payload_data,
mflodman@webrtc.orga4ef2ce2015-02-12 09:54:18 +000072 size_t payload_length,
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +000073 const RTPFragmentationHeader* fragmentation,
74 const RTPVideoHeader* rtp_video_hdr) {
Tommi97888bd2016-01-21 23:24:59 +010075 rtc::CritScope lock(&crit_);
Peter Boström8b79b072016-02-26 16:31:37 +010076 RTC_DCHECK(!rtp_modules_.empty());
77 if (!active_ || num_sending_modules_ == 0)
mflodman@webrtc.org50e28162015-02-23 07:45:11 +000078 return false;
79
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +000080 int stream_idx = 0;
Peter Boström8b79b072016-02-26 16:31:37 +010081 if (rtp_video_hdr) {
82 RTC_DCHECK_LT(rtp_video_hdr->simulcastIdx, rtp_modules_.size());
83 // The simulcast index might actually be larger than the number of modules
84 // in case the encoder was processing a frame during a codec reconfig.
85 if (rtp_video_hdr->simulcastIdx >= num_sending_modules_)
86 return false;
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +000087 stream_idx = rtp_video_hdr->simulcastIdx;
Peter Boström8b79b072016-02-26 16:31:37 +010088 }
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +000089 return rtp_modules_[stream_idx]->SendOutgoingData(
90 frame_type, payload_type, time_stamp, capture_time_ms, payload_data,
mflodman@webrtc.orga4ef2ce2015-02-12 09:54:18 +000091 payload_length, fragmentation, rtp_video_hdr) == 0 ? true : false;
92}
93
mflodman@webrtc.org50e28162015-02-23 07:45:11 +000094void PayloadRouter::SetTargetSendBitrates(
95 const std::vector<uint32_t>& stream_bitrates) {
Tommi97888bd2016-01-21 23:24:59 +010096 rtc::CritScope lock(&crit_);
Peter Boström8b79b072016-02-26 16:31:37 +010097 RTC_DCHECK_LE(stream_bitrates.size(), rtp_modules_.size());
98 for (size_t i = 0; i < stream_bitrates.size(); ++i) {
99 rtp_modules_[i]->SetTargetSendBitrate(stream_bitrates[i]);
mflodman@webrtc.org50e28162015-02-23 07:45:11 +0000100 }
101}
102
mflodman@webrtc.orga4ef2ce2015-02-12 09:54:18 +0000103size_t PayloadRouter::MaxPayloadLength() const {
104 size_t min_payload_length = DefaultMaxPayloadLength();
Tommi97888bd2016-01-21 23:24:59 +0100105 rtc::CritScope lock(&crit_);
Peter Boström8b79b072016-02-26 16:31:37 +0100106 for (size_t i = 0; i < num_sending_modules_; ++i) {
107 size_t module_payload_length = rtp_modules_[i]->MaxDataPayloadLength();
mflodman@webrtc.orga4ef2ce2015-02-12 09:54:18 +0000108 if (module_payload_length < min_payload_length)
109 min_payload_length = module_payload_length;
110 }
111 return min_payload_length;
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +0000112}
113
114} // namespace webrtc