1 /* 2 * Copyright (c) 2018 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 #ifndef CALL_DEGRADED_CALL_H_ 12 #define CALL_DEGRADED_CALL_H_ 13 14 #include <stddef.h> 15 #include <stdint.h> 16 17 #include <map> 18 #include <memory> 19 20 #include "absl/types/optional.h" 21 #include "api/call/transport.h" 22 #include "api/fec_controller.h" 23 #include "api/media_types.h" 24 #include "api/rtp_headers.h" 25 #include "api/test/simulated_network.h" 26 #include "api/video_codecs/video_encoder_config.h" 27 #include "call/audio_receive_stream.h" 28 #include "call/audio_send_stream.h" 29 #include "call/call.h" 30 #include "call/fake_network_pipe.h" 31 #include "call/flexfec_receive_stream.h" 32 #include "call/packet_receiver.h" 33 #include "call/rtp_transport_controller_send_interface.h" 34 #include "call/simulated_network.h" 35 #include "call/video_receive_stream.h" 36 #include "call/video_send_stream.h" 37 #include "modules/utility/include/process_thread.h" 38 #include "rtc_base/copy_on_write_buffer.h" 39 #include "rtc_base/network/sent_packet.h" 40 #include "rtc_base/task_queue.h" 41 #include "system_wrappers/include/clock.h" 42 43 namespace webrtc { 44 class DegradedCall : public Call, private PacketReceiver { 45 public: 46 explicit DegradedCall( 47 std::unique_ptr<Call> call, 48 absl::optional<BuiltInNetworkBehaviorConfig> send_config, 49 absl::optional<BuiltInNetworkBehaviorConfig> receive_config, 50 TaskQueueFactory* task_queue_factory); 51 ~DegradedCall() override; 52 53 // Implements Call. 54 AudioSendStream* CreateAudioSendStream( 55 const AudioSendStream::Config& config) override; 56 void DestroyAudioSendStream(AudioSendStream* send_stream) override; 57 58 AudioReceiveStream* CreateAudioReceiveStream( 59 const AudioReceiveStream::Config& config) override; 60 void DestroyAudioReceiveStream(AudioReceiveStream* receive_stream) override; 61 62 VideoSendStream* CreateVideoSendStream( 63 VideoSendStream::Config config, 64 VideoEncoderConfig encoder_config) override; 65 VideoSendStream* CreateVideoSendStream( 66 VideoSendStream::Config config, 67 VideoEncoderConfig encoder_config, 68 std::unique_ptr<FecController> fec_controller) override; 69 void DestroyVideoSendStream(VideoSendStream* send_stream) override; 70 71 VideoReceiveStream* CreateVideoReceiveStream( 72 VideoReceiveStream::Config configuration) override; 73 void DestroyVideoReceiveStream(VideoReceiveStream* receive_stream) override; 74 75 FlexfecReceiveStream* CreateFlexfecReceiveStream( 76 const FlexfecReceiveStream::Config& config) override; 77 void DestroyFlexfecReceiveStream( 78 FlexfecReceiveStream* receive_stream) override; 79 80 void AddAdaptationResource(rtc::scoped_refptr<Resource> resource) override; 81 82 PacketReceiver* Receiver() override; 83 84 RtpTransportControllerSendInterface* GetTransportControllerSend() override; 85 86 Stats GetStats() const override; 87 88 void SignalChannelNetworkState(MediaType media, NetworkState state) override; 89 void OnAudioTransportOverheadChanged( 90 int transport_overhead_per_packet) override; 91 void OnSentPacket(const rtc::SentPacket& sent_packet) override; 92 93 protected: 94 // Implements PacketReceiver. 95 DeliveryStatus DeliverPacket(MediaType media_type, 96 rtc::CopyOnWriteBuffer packet, 97 int64_t packet_time_us) override; 98 99 private: 100 class FakeNetworkPipeOnTaskQueue { 101 public: 102 FakeNetworkPipeOnTaskQueue( 103 TaskQueueFactory* task_queue_factory, 104 Clock* clock, 105 std::unique_ptr<NetworkBehaviorInterface> network_behavior); 106 107 void SendRtp(const uint8_t* packet, 108 size_t length, 109 const PacketOptions& options, 110 Transport* transport); 111 void SendRtcp(const uint8_t* packet, size_t length, Transport* transport); 112 113 void AddActiveTransport(Transport* transport); 114 void RemoveActiveTransport(Transport* transport); 115 116 private: 117 // Try to process packets on the fake network queue. 118 // Returns true if call resulted in a delayed process, false if queue empty. 119 bool Process(); 120 121 Clock* const clock_; 122 rtc::TaskQueue task_queue_; 123 FakeNetworkPipe pipe_; 124 absl::optional<int64_t> next_process_ms_ RTC_GUARDED_BY(&task_queue_); 125 }; 126 127 // For audio/video send stream, a TransportAdapter instance is used to 128 // intercept packets to be sent, and put them into a common FakeNetworkPipe 129 // in such as way that they will eventually (unless dropped) be forwarded to 130 // the correct Transport for that stream. 131 class FakeNetworkPipeTransportAdapter : public Transport { 132 public: 133 FakeNetworkPipeTransportAdapter(FakeNetworkPipeOnTaskQueue* fake_network, 134 Call* call, 135 Clock* clock, 136 Transport* real_transport); 137 ~FakeNetworkPipeTransportAdapter(); 138 139 bool SendRtp(const uint8_t* packet, 140 size_t length, 141 const PacketOptions& options) override; 142 bool SendRtcp(const uint8_t* packet, size_t length) override; 143 144 private: 145 FakeNetworkPipeOnTaskQueue* const network_pipe_; 146 Call* const call_; 147 Clock* const clock_; 148 Transport* const real_transport_; 149 }; 150 151 Clock* const clock_; 152 const std::unique_ptr<Call> call_; 153 TaskQueueFactory* const task_queue_factory_; 154 SetClientBitratePreferences(const webrtc::BitrateSettings & preferences)155 void SetClientBitratePreferences( 156 const webrtc::BitrateSettings& preferences) override {} 157 158 const absl::optional<BuiltInNetworkBehaviorConfig> send_config_; 159 SimulatedNetwork* send_simulated_network_; 160 std::unique_ptr<FakeNetworkPipeOnTaskQueue> send_pipe_; 161 std::map<AudioSendStream*, std::unique_ptr<FakeNetworkPipeTransportAdapter>> 162 audio_send_transport_adapters_; 163 std::map<VideoSendStream*, std::unique_ptr<FakeNetworkPipeTransportAdapter>> 164 video_send_transport_adapters_; 165 166 const absl::optional<BuiltInNetworkBehaviorConfig> receive_config_; 167 SimulatedNetwork* receive_simulated_network_; 168 std::unique_ptr<FakeNetworkPipe> receive_pipe_; 169 }; 170 171 } // namespace webrtc 172 173 #endif // CALL_DEGRADED_CALL_H_ 174