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 | |
brandtr | 7250b39 | 2016-12-19 01:13:46 -0800 | [diff] [blame] | 11 | #include "webrtc/call/flexfec_receive_stream_impl.h" |
brandtr | 76648da | 2016-10-20 04:54:48 -0700 | [diff] [blame] | 12 | |
brandtr | b29e652 | 2016-12-21 06:37:18 -0800 | [diff] [blame^] | 13 | #include <utility> |
| 14 | |
brandtr | 76648da | 2016-10-20 04:54:48 -0700 | [diff] [blame] | 15 | #include "webrtc/base/checks.h" |
| 16 | #include "webrtc/base/logging.h" |
| 17 | |
| 18 | namespace webrtc { |
| 19 | |
| 20 | std::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 | |
brandtr | 1cfbd60 | 2016-12-08 04:17:53 -0800 | [diff] [blame] | 27 | std::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"); |
brandtr | b29e652 | 2016-12-21 06:37:18 -0800 | [diff] [blame^] | 39 | ss << ", rtp_header_extensions: ["; |
brandtr | 1cfbd60 | 2016-12-08 04:17:53 -0800 | [diff] [blame] | 40 | i = 0; |
brandtr | b29e652 | 2016-12-21 06:37:18 -0800 | [diff] [blame^] | 41 | 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(); |
brandtr | 1cfbd60 | 2016-12-08 04:17:53 -0800 | [diff] [blame] | 45 | ss << "]}"; |
| 46 | return ss.str(); |
| 47 | } |
| 48 | |
brandtr | 76648da | 2016-10-20 04:54:48 -0700 | [diff] [blame] | 49 | namespace { |
| 50 | |
| 51 | // TODO(brandtr): Update this function when we support multistream protection. |
brandtr | 43c31e7 | 2016-11-15 05:26:45 -0800 | [diff] [blame] | 52 | std::unique_ptr<FlexfecReceiver> MaybeCreateFlexfecReceiver( |
| 53 | const FlexfecReceiveStream::Config& config, |
brandtr | b29e652 | 2016-12-21 06:37:18 -0800 | [diff] [blame^] | 54 | RecoveredPacketReceiver* recovered_packet_receiver) { |
brandtr | 1cfbd60 | 2016-12-08 04:17:53 -0800 | [diff] [blame] | 55 | if (config.payload_type < 0) { |
brandtr | 43c31e7 | 2016-11-15 05:26:45 -0800 | [diff] [blame] | 56 | LOG(LS_WARNING) << "Invalid FlexFEC payload type given. " |
| 57 | << "This FlexfecReceiveStream will therefore be useless."; |
brandtr | 76648da | 2016-10-20 04:54:48 -0700 | [diff] [blame] | 58 | return nullptr; |
brandtr | 43c31e7 | 2016-11-15 05:26:45 -0800 | [diff] [blame] | 59 | } |
brandtr | 1cfbd60 | 2016-12-08 04:17:53 -0800 | [diff] [blame] | 60 | RTC_DCHECK_GE(config.payload_type, 0); |
| 61 | RTC_DCHECK_LE(config.payload_type, 127); |
| 62 | if (config.remote_ssrc == 0) { |
brandtr | 43c31e7 | 2016-11-15 05:26:45 -0800 | [diff] [blame] | 63 | 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) { |
brandtr | 76648da | 2016-10-20 04:54:48 -0700 | [diff] [blame] | 74 | LOG(LS_WARNING) |
| 75 | << "The supplied FlexfecConfig contained multiple protected " |
| 76 | "media streams, but our implementation currently only " |
brandtr | 43c31e7 | 2016-11-15 05:26:45 -0800 | [diff] [blame] | 77 | "supports protecting a single media stream. " |
| 78 | "To avoid confusion, disabling FlexFEC completely."; |
| 79 | return nullptr; |
brandtr | 76648da | 2016-10-20 04:54:48 -0700 | [diff] [blame] | 80 | } |
brandtr | 43c31e7 | 2016-11-15 05:26:45 -0800 | [diff] [blame] | 81 | RTC_DCHECK_EQ(1U, config.protected_media_ssrcs.size()); |
| 82 | return std::unique_ptr<FlexfecReceiver>( |
brandtr | 1cfbd60 | 2016-12-08 04:17:53 -0800 | [diff] [blame] | 83 | new FlexfecReceiver(config.remote_ssrc, config.protected_media_ssrcs[0], |
brandtr | b29e652 | 2016-12-21 06:37:18 -0800 | [diff] [blame^] | 84 | recovered_packet_receiver)); |
brandtr | 76648da | 2016-10-20 04:54:48 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | } // namespace |
| 88 | |
brandtr | 7250b39 | 2016-12-19 01:13:46 -0800 | [diff] [blame] | 89 | FlexfecReceiveStreamImpl::FlexfecReceiveStreamImpl( |
brandtr | 446fcb6 | 2016-12-08 04:14:24 -0800 | [diff] [blame] | 90 | const Config& config, |
brandtr | b29e652 | 2016-12-21 06:37:18 -0800 | [diff] [blame^] | 91 | RecoveredPacketReceiver* recovered_packet_receiver) |
brandtr | 76648da | 2016-10-20 04:54:48 -0700 | [diff] [blame] | 92 | : started_(false), |
brandtr | 446fcb6 | 2016-12-08 04:14:24 -0800 | [diff] [blame] | 93 | config_(config), |
brandtr | 43c31e7 | 2016-11-15 05:26:45 -0800 | [diff] [blame] | 94 | receiver_( |
brandtr | b29e652 | 2016-12-21 06:37:18 -0800 | [diff] [blame^] | 95 | MaybeCreateFlexfecReceiver(config_, recovered_packet_receiver)) { |
brandtr | 7250b39 | 2016-12-19 01:13:46 -0800 | [diff] [blame] | 96 | LOG(LS_INFO) << "FlexfecReceiveStreamImpl: " << config_.ToString(); |
brandtr | 76648da | 2016-10-20 04:54:48 -0700 | [diff] [blame] | 97 | } |
| 98 | |
brandtr | 7250b39 | 2016-12-19 01:13:46 -0800 | [diff] [blame] | 99 | FlexfecReceiveStreamImpl::~FlexfecReceiveStreamImpl() { |
| 100 | LOG(LS_INFO) << "~FlexfecReceiveStreamImpl: " << config_.ToString(); |
brandtr | 76648da | 2016-10-20 04:54:48 -0700 | [diff] [blame] | 101 | Stop(); |
| 102 | } |
| 103 | |
brandtr | 7250b39 | 2016-12-19 01:13:46 -0800 | [diff] [blame] | 104 | bool FlexfecReceiveStreamImpl::AddAndProcessReceivedPacket( |
brandtr | b29e652 | 2016-12-21 06:37:18 -0800 | [diff] [blame^] | 105 | RtpPacketReceived packet) { |
brandtr | 76648da | 2016-10-20 04:54:48 -0700 | [diff] [blame] | 106 | { |
| 107 | rtc::CritScope cs(&crit_); |
| 108 | if (!started_) |
| 109 | return false; |
| 110 | } |
| 111 | if (!receiver_) |
| 112 | return false; |
brandtr | b29e652 | 2016-12-21 06:37:18 -0800 | [diff] [blame^] | 113 | return receiver_->AddAndProcessReceivedPacket(std::move(packet)); |
brandtr | 76648da | 2016-10-20 04:54:48 -0700 | [diff] [blame] | 114 | } |
| 115 | |
brandtr | 7250b39 | 2016-12-19 01:13:46 -0800 | [diff] [blame] | 116 | void FlexfecReceiveStreamImpl::Start() { |
brandtr | 76648da | 2016-10-20 04:54:48 -0700 | [diff] [blame] | 117 | rtc::CritScope cs(&crit_); |
| 118 | started_ = true; |
| 119 | } |
| 120 | |
brandtr | 7250b39 | 2016-12-19 01:13:46 -0800 | [diff] [blame] | 121 | void FlexfecReceiveStreamImpl::Stop() { |
brandtr | 76648da | 2016-10-20 04:54:48 -0700 | [diff] [blame] | 122 | 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. |
brandtr | 7250b39 | 2016-12-19 01:13:46 -0800 | [diff] [blame] | 128 | FlexfecReceiveStreamImpl::Stats FlexfecReceiveStreamImpl::GetStats() const { |
| 129 | return FlexfecReceiveStream::Stats(); |
brandtr | 76648da | 2016-10-20 04:54:48 -0700 | [diff] [blame] | 130 | } |
| 131 | |
brandtr | 76648da | 2016-10-20 04:54:48 -0700 | [diff] [blame] | 132 | } // namespace webrtc |