blob: e0e49c1a2ec24b9ae2651fa833360ca476f608c9 [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
13#include "webrtc/base/checks.h"
14#include "webrtc/base/logging.h"
15
16namespace webrtc {
17
18std::string FlexfecReceiveStream::Stats::ToString(int64_t time_ms) const {
19 std::stringstream ss;
20 ss << "FlexfecReceiveStream stats: " << time_ms
21 << ", {flexfec_bitrate_bps: " << flexfec_bitrate_bps << "}";
22 return ss.str();
23}
24
brandtr1cfbd602016-12-08 04:17:53 -080025std::string FlexfecReceiveStream::Config::ToString() const {
26 std::stringstream ss;
27 ss << "{payload_type: " << payload_type;
28 ss << ", remote_ssrc: " << remote_ssrc;
29 ss << ", local_ssrc: " << local_ssrc;
30 ss << ", protected_media_ssrcs: [";
31 size_t i = 0;
32 for (; i + 1 < protected_media_ssrcs.size(); ++i)
33 ss << protected_media_ssrcs[i] << ", ";
34 if (!protected_media_ssrcs.empty())
35 ss << protected_media_ssrcs[i];
36 ss << "], transport_cc: " << (transport_cc ? "on" : "off");
brandtr70e40532016-12-21 00:22:03 -080037 ss << ", extensions: [";
brandtr1cfbd602016-12-08 04:17:53 -080038 i = 0;
brandtr70e40532016-12-21 00:22:03 -080039 for (; i + 1 < extensions.size(); ++i)
40 ss << extensions[i].ToString() << ", ";
41 if (!extensions.empty())
42 ss << extensions[i].ToString();
brandtr1cfbd602016-12-08 04:17:53 -080043 ss << "]}";
44 return ss.str();
45}
46
brandtr76648da2016-10-20 04:54:48 -070047namespace {
48
49// TODO(brandtr): Update this function when we support multistream protection.
brandtr43c31e72016-11-15 05:26:45 -080050std::unique_ptr<FlexfecReceiver> MaybeCreateFlexfecReceiver(
51 const FlexfecReceiveStream::Config& config,
brandtr70e40532016-12-21 00:22:03 -080052 RecoveredPacketReceiver* recovered_packet_callback) {
brandtr1cfbd602016-12-08 04:17:53 -080053 if (config.payload_type < 0) {
brandtr43c31e72016-11-15 05:26:45 -080054 LOG(LS_WARNING) << "Invalid FlexFEC payload type given. "
55 << "This FlexfecReceiveStream will therefore be useless.";
brandtr76648da2016-10-20 04:54:48 -070056 return nullptr;
brandtr43c31e72016-11-15 05:26:45 -080057 }
brandtr1cfbd602016-12-08 04:17:53 -080058 RTC_DCHECK_GE(config.payload_type, 0);
59 RTC_DCHECK_LE(config.payload_type, 127);
60 if (config.remote_ssrc == 0) {
brandtr43c31e72016-11-15 05:26:45 -080061 LOG(LS_WARNING) << "Invalid FlexFEC SSRC given. "
62 << "This FlexfecReceiveStream will therefore be useless.";
63 return nullptr;
64 }
65 if (config.protected_media_ssrcs.empty()) {
66 LOG(LS_WARNING) << "No protected media SSRC supplied. "
67 << "This FlexfecReceiveStream will therefore be useless.";
68 return nullptr;
69 }
70
71 if (config.protected_media_ssrcs.size() > 1) {
brandtr76648da2016-10-20 04:54:48 -070072 LOG(LS_WARNING)
73 << "The supplied FlexfecConfig contained multiple protected "
74 "media streams, but our implementation currently only "
brandtr43c31e72016-11-15 05:26:45 -080075 "supports protecting a single media stream. "
76 "To avoid confusion, disabling FlexFEC completely.";
77 return nullptr;
brandtr76648da2016-10-20 04:54:48 -070078 }
brandtr43c31e72016-11-15 05:26:45 -080079 RTC_DCHECK_EQ(1U, config.protected_media_ssrcs.size());
80 return std::unique_ptr<FlexfecReceiver>(
brandtr1cfbd602016-12-08 04:17:53 -080081 new FlexfecReceiver(config.remote_ssrc, config.protected_media_ssrcs[0],
brandtr70e40532016-12-21 00:22:03 -080082 recovered_packet_callback));
brandtr76648da2016-10-20 04:54:48 -070083}
84
85} // namespace
86
brandtr7250b392016-12-19 01:13:46 -080087FlexfecReceiveStreamImpl::FlexfecReceiveStreamImpl(
brandtr446fcb62016-12-08 04:14:24 -080088 const Config& config,
brandtr70e40532016-12-21 00:22:03 -080089 RecoveredPacketReceiver* recovered_packet_callback)
brandtr76648da2016-10-20 04:54:48 -070090 : started_(false),
brandtr446fcb62016-12-08 04:14:24 -080091 config_(config),
brandtr43c31e72016-11-15 05:26:45 -080092 receiver_(
brandtr70e40532016-12-21 00:22:03 -080093 MaybeCreateFlexfecReceiver(config_, recovered_packet_callback)) {
brandtr7250b392016-12-19 01:13:46 -080094 LOG(LS_INFO) << "FlexfecReceiveStreamImpl: " << config_.ToString();
brandtr76648da2016-10-20 04:54:48 -070095}
96
brandtr7250b392016-12-19 01:13:46 -080097FlexfecReceiveStreamImpl::~FlexfecReceiveStreamImpl() {
98 LOG(LS_INFO) << "~FlexfecReceiveStreamImpl: " << config_.ToString();
brandtr76648da2016-10-20 04:54:48 -070099 Stop();
100}
101
brandtr7250b392016-12-19 01:13:46 -0800102bool FlexfecReceiveStreamImpl::AddAndProcessReceivedPacket(
brandtr70e40532016-12-21 00:22:03 -0800103 const uint8_t* packet,
104 size_t packet_length) {
brandtr76648da2016-10-20 04:54:48 -0700105 {
106 rtc::CritScope cs(&crit_);
107 if (!started_)
108 return false;
109 }
110 if (!receiver_)
111 return false;
brandtr70e40532016-12-21 00:22:03 -0800112 return receiver_->AddAndProcessReceivedPacket(packet, packet_length);
brandtr76648da2016-10-20 04:54:48 -0700113}
114
brandtr7250b392016-12-19 01:13:46 -0800115void FlexfecReceiveStreamImpl::Start() {
brandtr76648da2016-10-20 04:54:48 -0700116 rtc::CritScope cs(&crit_);
117 started_ = true;
118}
119
brandtr7250b392016-12-19 01:13:46 -0800120void FlexfecReceiveStreamImpl::Stop() {
brandtr76648da2016-10-20 04:54:48 -0700121 rtc::CritScope cs(&crit_);
122 started_ = false;
123}
124
125// TODO(brandtr): Implement this member function when we have designed the
126// stats for FlexFEC.
brandtr7250b392016-12-19 01:13:46 -0800127FlexfecReceiveStreamImpl::Stats FlexfecReceiveStreamImpl::GetStats() const {
128 return FlexfecReceiveStream::Stats();
brandtr76648da2016-10-20 04:54:48 -0700129}
130
brandtr76648da2016-10-20 04:54:48 -0700131} // namespace webrtc