blob: d466e41bb01bb44f0621fb2844b99ac2751c83cf [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
Per83d09102016-04-15 14:59:13 +020019PayloadRouter::PayloadRouter(const std::vector<RtpRtcp*>& rtp_modules)
20 : active_(false), num_sending_modules_(1), rtp_modules_(rtp_modules) {
21 UpdateModuleSendingState();
22}
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +000023
24PayloadRouter::~PayloadRouter() {}
25
mflodman@webrtc.orga4ef2ce2015-02-12 09:54:18 +000026size_t PayloadRouter::DefaultMaxPayloadLength() {
27 const size_t kIpUdpSrtpLength = 44;
28 return IP_PACKET_SIZE - kIpUdpSrtpLength;
29}
30
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +000031void PayloadRouter::set_active(bool active) {
Tommi97888bd2016-01-21 23:24:59 +010032 rtc::CritScope lock(&crit_);
Peter Boström8b79b072016-02-26 16:31:37 +010033 if (active_ == active)
34 return;
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +000035 active_ = active;
Peter Boström8b79b072016-02-26 16:31:37 +010036 UpdateModuleSendingState();
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +000037}
38
39bool PayloadRouter::active() {
Tommi97888bd2016-01-21 23:24:59 +010040 rtc::CritScope lock(&crit_);
mflodman@webrtc.org47d657b2015-02-19 10:29:32 +000041 return active_ && !rtp_modules_.empty();
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +000042}
43
Peter Boström8b79b072016-02-26 16:31:37 +010044void PayloadRouter::SetSendingRtpModules(size_t num_sending_modules) {
45 RTC_DCHECK_LE(num_sending_modules, rtp_modules_.size());
46 rtc::CritScope lock(&crit_);
47 num_sending_modules_ = num_sending_modules;
48 UpdateModuleSendingState();
49}
50
51void PayloadRouter::UpdateModuleSendingState() {
52 for (size_t i = 0; i < num_sending_modules_; ++i) {
53 rtp_modules_[i]->SetSendingStatus(active_);
54 rtp_modules_[i]->SetSendingMediaStatus(active_);
55 }
56 // Disable inactive modules.
57 for (size_t i = num_sending_modules_; i < rtp_modules_.size(); ++i) {
58 rtp_modules_[i]->SetSendingStatus(false);
59 rtp_modules_[i]->SetSendingMediaStatus(false);
60 }
61}
62
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +000063bool PayloadRouter::RoutePayload(FrameType frame_type,
64 int8_t payload_type,
65 uint32_t time_stamp,
66 int64_t capture_time_ms,
67 const uint8_t* payload_data,
mflodman@webrtc.orga4ef2ce2015-02-12 09:54:18 +000068 size_t payload_length,
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +000069 const RTPFragmentationHeader* fragmentation,
70 const RTPVideoHeader* rtp_video_hdr) {
Tommi97888bd2016-01-21 23:24:59 +010071 rtc::CritScope lock(&crit_);
Peter Boström8b79b072016-02-26 16:31:37 +010072 RTC_DCHECK(!rtp_modules_.empty());
73 if (!active_ || num_sending_modules_ == 0)
mflodman@webrtc.org50e28162015-02-23 07:45:11 +000074 return false;
75
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +000076 int stream_idx = 0;
Peter Boström8b79b072016-02-26 16:31:37 +010077 if (rtp_video_hdr) {
78 RTC_DCHECK_LT(rtp_video_hdr->simulcastIdx, rtp_modules_.size());
79 // The simulcast index might actually be larger than the number of modules
80 // in case the encoder was processing a frame during a codec reconfig.
81 if (rtp_video_hdr->simulcastIdx >= num_sending_modules_)
82 return false;
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +000083 stream_idx = rtp_video_hdr->simulcastIdx;
Peter Boström8b79b072016-02-26 16:31:37 +010084 }
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +000085 return rtp_modules_[stream_idx]->SendOutgoingData(
86 frame_type, payload_type, time_stamp, capture_time_ms, payload_data,
mflodman@webrtc.orga4ef2ce2015-02-12 09:54:18 +000087 payload_length, fragmentation, rtp_video_hdr) == 0 ? true : false;
88}
89
mflodman@webrtc.org50e28162015-02-23 07:45:11 +000090void PayloadRouter::SetTargetSendBitrates(
91 const std::vector<uint32_t>& stream_bitrates) {
Tommi97888bd2016-01-21 23:24:59 +010092 rtc::CritScope lock(&crit_);
Peter Boström8b79b072016-02-26 16:31:37 +010093 RTC_DCHECK_LE(stream_bitrates.size(), rtp_modules_.size());
94 for (size_t i = 0; i < stream_bitrates.size(); ++i) {
95 rtp_modules_[i]->SetTargetSendBitrate(stream_bitrates[i]);
mflodman@webrtc.org50e28162015-02-23 07:45:11 +000096 }
97}
98
mflodman@webrtc.orga4ef2ce2015-02-12 09:54:18 +000099size_t PayloadRouter::MaxPayloadLength() const {
100 size_t min_payload_length = DefaultMaxPayloadLength();
Tommi97888bd2016-01-21 23:24:59 +0100101 rtc::CritScope lock(&crit_);
Peter Boström8b79b072016-02-26 16:31:37 +0100102 for (size_t i = 0; i < num_sending_modules_; ++i) {
103 size_t module_payload_length = rtp_modules_[i]->MaxDataPayloadLength();
mflodman@webrtc.orga4ef2ce2015-02-12 09:54:18 +0000104 if (module_payload_length < min_payload_length)
105 min_payload_length = module_payload_length;
106 }
107 return min_payload_length;
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +0000108}
109
110} // namespace webrtc