blob: 6ab1c3ab66dd4d6bbf6b0e7580d62ca78eb86126 [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
11#include "webrtc/call/flexfec_receive_stream.h"
12
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
25namespace {
26
27// TODO(brandtr): Update this function when we support multistream protection.
brandtr43c31e72016-11-15 05:26:45 -080028std::unique_ptr<FlexfecReceiver> MaybeCreateFlexfecReceiver(
29 const FlexfecReceiveStream::Config& config,
brandtr76648da2016-10-20 04:54:48 -070030 RecoveredPacketReceiver* recovered_packet_callback) {
brandtr43c31e72016-11-15 05:26:45 -080031 if (config.flexfec_payload_type < 0) {
32 LOG(LS_WARNING) << "Invalid FlexFEC payload type given. "
33 << "This FlexfecReceiveStream will therefore be useless.";
brandtr76648da2016-10-20 04:54:48 -070034 return nullptr;
brandtr43c31e72016-11-15 05:26:45 -080035 }
36 RTC_DCHECK_GE(config.flexfec_payload_type, 0);
37 RTC_DCHECK_LE(config.flexfec_payload_type, 127);
38 if (config.flexfec_ssrc == 0) {
39 LOG(LS_WARNING) << "Invalid FlexFEC SSRC given. "
40 << "This FlexfecReceiveStream will therefore be useless.";
41 return nullptr;
42 }
43 if (config.protected_media_ssrcs.empty()) {
44 LOG(LS_WARNING) << "No protected media SSRC supplied. "
45 << "This FlexfecReceiveStream will therefore be useless.";
46 return nullptr;
47 }
48
49 if (config.protected_media_ssrcs.size() > 1) {
brandtr76648da2016-10-20 04:54:48 -070050 LOG(LS_WARNING)
51 << "The supplied FlexfecConfig contained multiple protected "
52 "media streams, but our implementation currently only "
brandtr43c31e72016-11-15 05:26:45 -080053 "supports protecting a single media stream. "
54 "To avoid confusion, disabling FlexFEC completely.";
55 return nullptr;
brandtr76648da2016-10-20 04:54:48 -070056 }
brandtr43c31e72016-11-15 05:26:45 -080057 RTC_DCHECK_EQ(1U, config.protected_media_ssrcs.size());
58 return std::unique_ptr<FlexfecReceiver>(
59 new FlexfecReceiver(config.flexfec_ssrc, config.protected_media_ssrcs[0],
60 recovered_packet_callback));
brandtr76648da2016-10-20 04:54:48 -070061}
62
63} // namespace
64
65namespace internal {
66
67FlexfecReceiveStream::FlexfecReceiveStream(
brandtr446fcb62016-12-08 04:14:24 -080068 const Config& config,
brandtr76648da2016-10-20 04:54:48 -070069 RecoveredPacketReceiver* recovered_packet_callback)
70 : started_(false),
brandtr446fcb62016-12-08 04:14:24 -080071 config_(config),
brandtr43c31e72016-11-15 05:26:45 -080072 receiver_(
73 MaybeCreateFlexfecReceiver(config_, recovered_packet_callback)) {
brandtr76648da2016-10-20 04:54:48 -070074 LOG(LS_INFO) << "FlexfecReceiveStream: " << config_.ToString();
75}
76
77FlexfecReceiveStream::~FlexfecReceiveStream() {
78 LOG(LS_INFO) << "~FlexfecReceiveStream: " << config_.ToString();
79 Stop();
80}
81
82bool FlexfecReceiveStream::AddAndProcessReceivedPacket(const uint8_t* packet,
83 size_t packet_length) {
84 {
85 rtc::CritScope cs(&crit_);
86 if (!started_)
87 return false;
88 }
89 if (!receiver_)
90 return false;
91 return receiver_->AddAndProcessReceivedPacket(packet, packet_length);
92}
93
94void FlexfecReceiveStream::Start() {
95 rtc::CritScope cs(&crit_);
96 started_ = true;
97}
98
99void FlexfecReceiveStream::Stop() {
100 rtc::CritScope cs(&crit_);
101 started_ = false;
102}
103
104// TODO(brandtr): Implement this member function when we have designed the
105// stats for FlexFEC.
106FlexfecReceiveStream::Stats FlexfecReceiveStream::GetStats() const {
107 return webrtc::FlexfecReceiveStream::Stats();
108}
109
110} // namespace internal
111
112} // namespace webrtc