blob: 13390a12c06ce891c9f85248fb474c00aea39fea [file] [log] [blame]
brandtr76648da2016-10-20 04:54:48 -07001/*
2 * Copyright (c) 2016 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
brandtr7250b392016-12-19 01:13:46 -080011#include "webrtc/call/flexfec_receive_stream_impl.h"
brandtr76648da2016-10-20 04:54:48 -070012
brandtrab2ffa32016-12-20 03:33:58 -080013#include <utility>
14
brandtr76648da2016-10-20 04:54:48 -070015#include "webrtc/base/checks.h"
16#include "webrtc/base/logging.h"
17
18namespace webrtc {
19
20std::string FlexfecReceiveStream::Stats::ToString(int64_t time_ms) const {
21 std::stringstream ss;
22 ss << "FlexfecReceiveStream stats: " << time_ms
23 << ", {flexfec_bitrate_bps: " << flexfec_bitrate_bps << "}";
24 return ss.str();
25}
26
brandtr1cfbd602016-12-08 04:17:53 -080027std::string FlexfecReceiveStream::Config::ToString() const {
28 std::stringstream ss;
29 ss << "{payload_type: " << payload_type;
30 ss << ", remote_ssrc: " << remote_ssrc;
31 ss << ", local_ssrc: " << local_ssrc;
32 ss << ", protected_media_ssrcs: [";
33 size_t i = 0;
34 for (; i + 1 < protected_media_ssrcs.size(); ++i)
35 ss << protected_media_ssrcs[i] << ", ";
36 if (!protected_media_ssrcs.empty())
37 ss << protected_media_ssrcs[i];
38 ss << "], transport_cc: " << (transport_cc ? "on" : "off");
brandtrab2ffa32016-12-20 03:33:58 -080039 ss << ", rtp_header_extensions: [";
brandtr1cfbd602016-12-08 04:17:53 -080040 i = 0;
brandtrab2ffa32016-12-20 03:33:58 -080041 for (; i + 1 < rtp_header_extensions.size(); ++i)
42 ss << rtp_header_extensions[i].ToString() << ", ";
43 if (!rtp_header_extensions.empty())
44 ss << rtp_header_extensions[i].ToString();
brandtr1cfbd602016-12-08 04:17:53 -080045 ss << "]}";
46 return ss.str();
47}
48
brandtr76648da2016-10-20 04:54:48 -070049namespace {
50
51// TODO(brandtr): Update this function when we support multistream protection.
brandtr43c31e72016-11-15 05:26:45 -080052std::unique_ptr<FlexfecReceiver> MaybeCreateFlexfecReceiver(
53 const FlexfecReceiveStream::Config& config,
brandtrab2ffa32016-12-20 03:33:58 -080054 RecoveredPacketReceiver* recovered_packet_receiver) {
brandtr1cfbd602016-12-08 04:17:53 -080055 if (config.payload_type < 0) {
brandtr43c31e72016-11-15 05:26:45 -080056 LOG(LS_WARNING) << "Invalid FlexFEC payload type given. "
57 << "This FlexfecReceiveStream will therefore be useless.";
brandtr76648da2016-10-20 04:54:48 -070058 return nullptr;
brandtr43c31e72016-11-15 05:26:45 -080059 }
brandtr1cfbd602016-12-08 04:17:53 -080060 RTC_DCHECK_GE(config.payload_type, 0);
61 RTC_DCHECK_LE(config.payload_type, 127);
62 if (config.remote_ssrc == 0) {
brandtr43c31e72016-11-15 05:26:45 -080063 LOG(LS_WARNING) << "Invalid FlexFEC SSRC given. "
64 << "This FlexfecReceiveStream will therefore be useless.";
65 return nullptr;
66 }
67 if (config.protected_media_ssrcs.empty()) {
68 LOG(LS_WARNING) << "No protected media SSRC supplied. "
69 << "This FlexfecReceiveStream will therefore be useless.";
70 return nullptr;
71 }
72
73 if (config.protected_media_ssrcs.size() > 1) {
brandtr76648da2016-10-20 04:54:48 -070074 LOG(LS_WARNING)
75 << "The supplied FlexfecConfig contained multiple protected "
76 "media streams, but our implementation currently only "
brandtr43c31e72016-11-15 05:26:45 -080077 "supports protecting a single media stream. "
78 "To avoid confusion, disabling FlexFEC completely.";
79 return nullptr;
brandtr76648da2016-10-20 04:54:48 -070080 }
brandtr43c31e72016-11-15 05:26:45 -080081 RTC_DCHECK_EQ(1U, config.protected_media_ssrcs.size());
82 return std::unique_ptr<FlexfecReceiver>(
brandtr1cfbd602016-12-08 04:17:53 -080083 new FlexfecReceiver(config.remote_ssrc, config.protected_media_ssrcs[0],
brandtrab2ffa32016-12-20 03:33:58 -080084 recovered_packet_receiver));
brandtr76648da2016-10-20 04:54:48 -070085}
86
87} // namespace
88
brandtr7250b392016-12-19 01:13:46 -080089FlexfecReceiveStreamImpl::FlexfecReceiveStreamImpl(
brandtr446fcb62016-12-08 04:14:24 -080090 const Config& config,
brandtrab2ffa32016-12-20 03:33:58 -080091 RecoveredPacketReceiver* recovered_packet_receiver)
brandtr76648da2016-10-20 04:54:48 -070092 : started_(false),
brandtr446fcb62016-12-08 04:14:24 -080093 config_(config),
brandtr43c31e72016-11-15 05:26:45 -080094 receiver_(
brandtrab2ffa32016-12-20 03:33:58 -080095 MaybeCreateFlexfecReceiver(config_, recovered_packet_receiver)) {
brandtr7250b392016-12-19 01:13:46 -080096 LOG(LS_INFO) << "FlexfecReceiveStreamImpl: " << config_.ToString();
brandtr76648da2016-10-20 04:54:48 -070097}
98
brandtr7250b392016-12-19 01:13:46 -080099FlexfecReceiveStreamImpl::~FlexfecReceiveStreamImpl() {
100 LOG(LS_INFO) << "~FlexfecReceiveStreamImpl: " << config_.ToString();
brandtr76648da2016-10-20 04:54:48 -0700101 Stop();
102}
103
brandtr7250b392016-12-19 01:13:46 -0800104bool FlexfecReceiveStreamImpl::AddAndProcessReceivedPacket(
brandtrab2ffa32016-12-20 03:33:58 -0800105 RtpPacketReceived packet) {
brandtr76648da2016-10-20 04:54:48 -0700106 {
107 rtc::CritScope cs(&crit_);
108 if (!started_)
109 return false;
110 }
111 if (!receiver_)
112 return false;
brandtrab2ffa32016-12-20 03:33:58 -0800113 return receiver_->AddAndProcessReceivedPacket(std::move(packet));
brandtr76648da2016-10-20 04:54:48 -0700114}
115
brandtr7250b392016-12-19 01:13:46 -0800116void FlexfecReceiveStreamImpl::Start() {
brandtr76648da2016-10-20 04:54:48 -0700117 rtc::CritScope cs(&crit_);
118 started_ = true;
119}
120
brandtr7250b392016-12-19 01:13:46 -0800121void FlexfecReceiveStreamImpl::Stop() {
brandtr76648da2016-10-20 04:54:48 -0700122 rtc::CritScope cs(&crit_);
123 started_ = false;
124}
125
126// TODO(brandtr): Implement this member function when we have designed the
127// stats for FlexFEC.
brandtr7250b392016-12-19 01:13:46 -0800128FlexfecReceiveStreamImpl::Stats FlexfecReceiveStreamImpl::GetStats() const {
129 return FlexfecReceiveStream::Stats();
brandtr76648da2016-10-20 04:54:48 -0700130}
131
brandtr76648da2016-10-20 04:54:48 -0700132} // namespace webrtc