brandtr | 76648da | 2016-10-20 04:54:48 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 16 | namespace webrtc { |
| 17 | |
| 18 | std::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 | |
| 25 | namespace { |
| 26 | |
| 27 | // TODO(brandtr): Update this function when we support multistream protection. |
brandtr | 43c31e7 | 2016-11-15 05:26:45 -0800 | [diff] [blame] | 28 | std::unique_ptr<FlexfecReceiver> MaybeCreateFlexfecReceiver( |
| 29 | const FlexfecReceiveStream::Config& config, |
brandtr | 76648da | 2016-10-20 04:54:48 -0700 | [diff] [blame] | 30 | RecoveredPacketReceiver* recovered_packet_callback) { |
brandtr | 43c31e7 | 2016-11-15 05:26:45 -0800 | [diff] [blame] | 31 | if (config.flexfec_payload_type < 0) { |
| 32 | LOG(LS_WARNING) << "Invalid FlexFEC payload type given. " |
| 33 | << "This FlexfecReceiveStream will therefore be useless."; |
brandtr | 76648da | 2016-10-20 04:54:48 -0700 | [diff] [blame] | 34 | return nullptr; |
brandtr | 43c31e7 | 2016-11-15 05:26:45 -0800 | [diff] [blame] | 35 | } |
| 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) { |
brandtr | 76648da | 2016-10-20 04:54:48 -0700 | [diff] [blame] | 50 | LOG(LS_WARNING) |
| 51 | << "The supplied FlexfecConfig contained multiple protected " |
| 52 | "media streams, but our implementation currently only " |
brandtr | 43c31e7 | 2016-11-15 05:26:45 -0800 | [diff] [blame] | 53 | "supports protecting a single media stream. " |
| 54 | "To avoid confusion, disabling FlexFEC completely."; |
| 55 | return nullptr; |
brandtr | 76648da | 2016-10-20 04:54:48 -0700 | [diff] [blame] | 56 | } |
brandtr | 43c31e7 | 2016-11-15 05:26:45 -0800 | [diff] [blame] | 57 | 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)); |
brandtr | 76648da | 2016-10-20 04:54:48 -0700 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | } // namespace |
| 64 | |
| 65 | namespace internal { |
| 66 | |
| 67 | FlexfecReceiveStream::FlexfecReceiveStream( |
brandtr | 446fcb6 | 2016-12-08 04:14:24 -0800 | [diff] [blame^] | 68 | const Config& config, |
brandtr | 76648da | 2016-10-20 04:54:48 -0700 | [diff] [blame] | 69 | RecoveredPacketReceiver* recovered_packet_callback) |
| 70 | : started_(false), |
brandtr | 446fcb6 | 2016-12-08 04:14:24 -0800 | [diff] [blame^] | 71 | config_(config), |
brandtr | 43c31e7 | 2016-11-15 05:26:45 -0800 | [diff] [blame] | 72 | receiver_( |
| 73 | MaybeCreateFlexfecReceiver(config_, recovered_packet_callback)) { |
brandtr | 76648da | 2016-10-20 04:54:48 -0700 | [diff] [blame] | 74 | LOG(LS_INFO) << "FlexfecReceiveStream: " << config_.ToString(); |
| 75 | } |
| 76 | |
| 77 | FlexfecReceiveStream::~FlexfecReceiveStream() { |
| 78 | LOG(LS_INFO) << "~FlexfecReceiveStream: " << config_.ToString(); |
| 79 | Stop(); |
| 80 | } |
| 81 | |
| 82 | bool 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 | |
| 94 | void FlexfecReceiveStream::Start() { |
| 95 | rtc::CritScope cs(&crit_); |
| 96 | started_ = true; |
| 97 | } |
| 98 | |
| 99 | void 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. |
| 106 | FlexfecReceiveStream::Stats FlexfecReceiveStream::GetStats() const { |
| 107 | return webrtc::FlexfecReceiveStream::Stats(); |
| 108 | } |
| 109 | |
| 110 | } // namespace internal |
| 111 | |
| 112 | } // namespace webrtc |