| 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 | #include <string> |
| 20 | #include <vector> |
| 21 |
| 22 | #include "absl/strings/string_view.h" |
| 23 | #include "absl/types/optional.h" |
| 24 | #include "api/call/transport.h" |
| 25 | #include "api/fec_controller.h" |
| 26 | #include "api/media_types.h" |
| 27 | #include "api/rtp_headers.h" |
| 28 | #include "api/task_queue/pending_task_safety_flag.h" |
| 29 | #include "api/test/simulated_network.h" |
| 30 | #include "call/audio_receive_stream.h" |
| 31 | #include "call/audio_send_stream.h" |
| 32 | #include "call/call.h" |
| 33 | #include "call/fake_network_pipe.h" |
| 34 | #include "call/flexfec_receive_stream.h" |
| 35 | #include "call/packet_receiver.h" |
| 36 | #include "call/rtp_transport_controller_send_interface.h" |
| 37 | #include "call/simulated_network.h" |
| 38 | #include "call/video_receive_stream.h" |
| 39 | #include "call/video_send_stream.h" |
| 40 | #include "rtc_base/copy_on_write_buffer.h" |
| 41 | #include "rtc_base/network/sent_packet.h" |
| 42 | #include "rtc_base/task_queue.h" |
| 43 | #include "system_wrappers/include/clock.h" |
| 44 | #include "video/config/video_encoder_config.h" |
| 45 |
| 46 | namespace webrtc { |
| 47 | class DegradedCall : public Call, private PacketReceiver { |
| 48 | public: |
| 49 | struct TimeScopedNetworkConfig : public BuiltInNetworkBehaviorConfig { |
| 50 | TimeDelta duration = TimeDelta::PlusInfinity(); |
| 51 | }; |
| 52 |
| 53 | explicit DegradedCall( |
| 54 | std::unique_ptr<Call> call, |
| 55 | const std::vector<TimeScopedNetworkConfig>& send_configs, |
| 56 | const std::vector<TimeScopedNetworkConfig>& receive_configs); |
| 57 | ~DegradedCall() override; |
| 58 |
| 59 | // Implements Call. |
| 60 | AudioSendStream* CreateAudioSendStream( |
| 61 | const AudioSendStream::Config& config) override; |
| 62 | void DestroyAudioSendStream(AudioSendStream* send_stream) override; |
| 63 |
| 64 | AudioReceiveStreamInterface* CreateAudioReceiveStream( |
| 65 | const AudioReceiveStreamInterface::Config& config) override; |
| 66 | void DestroyAudioReceiveStream( |
| 67 | AudioReceiveStreamInterface* receive_stream) override; |
| 68 |
| 69 | VideoSendStream* CreateVideoSendStream( |
| 70 | VideoSendStream::Config config, |
| 71 | VideoEncoderConfig encoder_config) override; |
| 72 | VideoSendStream* CreateVideoSendStream( |
| 73 | VideoSendStream::Config config, |
| 74 | VideoEncoderConfig encoder_config, |
| 75 | std::unique_ptr<FecController> fec_controller) override; |
| 76 | void DestroyVideoSendStream(VideoSendStream* send_stream) override; |
| 77 |
| 78 | VideoReceiveStreamInterface* CreateVideoReceiveStream( |
| 79 | VideoReceiveStreamInterface::Config configuration) override; |
| 80 | void DestroyVideoReceiveStream( |
| 81 | VideoReceiveStreamInterface* receive_stream) override; |
| 82 |
| 83 | FlexfecReceiveStream* CreateFlexfecReceiveStream( |
| 84 | const FlexfecReceiveStream::Config config) override; |
| 85 | void DestroyFlexfecReceiveStream( |
| 86 | FlexfecReceiveStream* receive_stream) override; |
| 87 |
| 88 | void AddAdaptationResource(rtc::scoped_refptr<Resource> resource) override; |
| 89 |
| 90 | PacketReceiver* Receiver() override; |
| 91 |
| 92 | RtpTransportControllerSendInterface* GetTransportControllerSend() override; |
| 93 |
| 94 | Stats GetStats() const override; |
| 95 |
| 96 | const FieldTrialsView& trials() const override; |
| 97 |
| 98 | TaskQueueBase* network_thread() const override; |
| 99 | TaskQueueBase* worker_thread() const override; |
| 100 |
| 101 | void SignalChannelNetworkState(MediaType media, NetworkState state) override; |
| 102 | void OnAudioTransportOverheadChanged( |
| 103 | int transport_overhead_per_packet) override; |
| 104 | void OnLocalSsrcUpdated(AudioReceiveStreamInterface& stream, |
| 105 | uint32_t local_ssrc) override; |
| 106 | void OnLocalSsrcUpdated(VideoReceiveStreamInterface& stream, |
| 107 | uint32_t local_ssrc) override; |
| 108 | void OnLocalSsrcUpdated(FlexfecReceiveStream& stream, |
| 109 | uint32_t local_ssrc) override; |
| 110 | void OnUpdateSyncGroup(AudioReceiveStreamInterface& stream, |
| 111 | absl::string_view sync_group) override; |
| 112 | void OnSentPacket(const rtc::SentPacket& sent_packet) override; |
| 113 |
| 114 | protected: |
| 115 | // Implements PacketReceiver. |
| 116 | void DeliverRtpPacket( |
| 117 | MediaType media_type, |
| 118 | RtpPacketReceived packet, |
| 119 | OnUndemuxablePacketHandler undemuxable_packet_handler) override; |
| 120 | void DeliverRtcpPacket(rtc::CopyOnWriteBuffer packet) override; |
| 121 |
| 122 | private: |
| 123 | class FakeNetworkPipeOnTaskQueue { |
| 124 | public: |
| 125 | FakeNetworkPipeOnTaskQueue( |
| 126 | TaskQueueBase* task_queue, |
| 127 | rtc::scoped_refptr<PendingTaskSafetyFlag> call_alive, |
| 128 | Clock* clock, |
| 129 | std::unique_ptr<NetworkBehaviorInterface> network_behavior); |
| 130 |
| 131 | void SendRtp(rtc::ArrayView<const uint8_t> packet, |
| 132 | const PacketOptions& options, |
| 133 | Transport* transport); |
| 134 | void SendRtcp(rtc::ArrayView<const uint8_t> packet, Transport* transport); |
| 135 |
| 136 | void AddActiveTransport(Transport* transport); |
| 137 | void RemoveActiveTransport(Transport* transport); |
| 138 |
| 139 | private: |
| 140 | // Try to process packets on the fake network queue. |
| 141 | // Returns true if call resulted in a delayed process, false if queue empty. |
| 142 | bool Process(); |
| 143 |
| 144 | Clock* const clock_; |
| 145 | TaskQueueBase* const task_queue_; |
| 146 | rtc::scoped_refptr<PendingTaskSafetyFlag> call_alive_; |
| 147 | FakeNetworkPipe pipe_; |
| 148 | absl::optional<int64_t> next_process_ms_ RTC_GUARDED_BY(&task_queue_); |
| 149 | }; |
| 150 |
| 151 | // For audio/video send stream, a TransportAdapter instance is used to |
| 152 | // intercept packets to be sent, and put them into a common FakeNetworkPipe |
| 153 | // in such as way that they will eventually (unless dropped) be forwarded to |
| 154 | // the correct Transport for that stream. |
| 155 | class FakeNetworkPipeTransportAdapter : public Transport { |
| 156 | public: |
| 157 | FakeNetworkPipeTransportAdapter(FakeNetworkPipeOnTaskQueue* fake_network, |
| 158 | Call* call, |
| 159 | Clock* clock, |
| 160 | Transport* real_transport); |
| 161 | ~FakeNetworkPipeTransportAdapter(); |
| 162 |
| 163 | bool SendRtp(rtc::ArrayView<const uint8_t> packet, |
| 164 | const PacketOptions& options) override; |
| 165 | bool SendRtcp(rtc::ArrayView<const uint8_t> packet) override; |
| 166 |
| 167 | private: |
| 168 | FakeNetworkPipeOnTaskQueue* const network_pipe_; |
| 169 | Call* const call_; |
| 170 | Clock* const clock_; |
| 171 | Transport* const real_transport_; |
| 172 | }; |
| 173 |
| 174 | void SetClientBitratePreferences( |
| 175 | const webrtc::BitrateSettings& preferences) override; |
| 176 | void UpdateSendNetworkConfig(); |
| 177 | void UpdateReceiveNetworkConfig(); |
| 178 |
| 179 | Clock* const clock_; |
| 180 | const std::unique_ptr<Call> call_; |
| 181 | // For cancelling tasks on the network thread when DegradedCall is destroyed |
| 182 | rtc::scoped_refptr<PendingTaskSafetyFlag> call_alive_; |
| 183 | size_t send_config_index_; |
| 184 | const std::vector<TimeScopedNetworkConfig> send_configs_; |
| 185 | SimulatedNetwork* send_simulated_network_; |
| 186 | std::unique_ptr<FakeNetworkPipeOnTaskQueue> send_pipe_; |
| 187 | std::map<AudioSendStream*, std::unique_ptr<FakeNetworkPipeTransportAdapter>> |
| 188 | audio_send_transport_adapters_; |
| 189 | std::map<VideoSendStream*, std::unique_ptr<FakeNetworkPipeTransportAdapter>> |
| 190 | video_send_transport_adapters_; |
| 191 |
| 192 | size_t receive_config_index_; |
| 193 | const std::vector<TimeScopedNetworkConfig> receive_configs_; |
| 194 | SimulatedNetwork* receive_simulated_network_; |
| 195 | SequenceChecker received_packet_sequence_checker_; |
| 196 | std::unique_ptr<FakeNetworkPipe> receive_pipe_ |
| 197 | RTC_GUARDED_BY(received_packet_sequence_checker_); |
| 198 | }; |
| 199 |
| 200 | } // namespace webrtc |
| 201 |
| 202 | #endif // CALL_DEGRADED_CALL_H_ |
| 203 |