blob: 49230ca1edc0730552949543753c02d1c1aa7e47 [file] [log] [blame]
Erik Språng09708512018-03-14 15:16:50 +01001/*
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
Yves Gerey3e707812018-11-28 16:47:49 +010014#include <stddef.h>
15#include <stdint.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020016
Erik Språngeea605d2019-08-12 15:56:51 +020017#include <map>
Erik Språng09708512018-03-14 15:16:50 +010018#include <memory>
19
Danil Chapovalovb9b146c2018-06-15 12:28:07 +020020#include "absl/types/optional.h"
Erik Språng09708512018-03-14 15:16:50 +010021#include "api/call/transport.h"
Yves Gerey3e707812018-11-28 16:47:49 +010022#include "api/fec_controller.h"
Steve Anton10542f22019-01-11 09:11:00 -080023#include "api/media_types.h"
Yves Gerey3e707812018-11-28 16:47:49 +010024#include "api/rtp_headers.h"
Artem Titov3229d652018-08-17 13:00:54 +020025#include "api/test/simulated_network.h"
Yves Gerey3e707812018-11-28 16:47:49 +010026#include "api/video_codecs/video_encoder_config.h"
27#include "call/audio_receive_stream.h"
28#include "call/audio_send_stream.h"
Erik Språng09708512018-03-14 15:16:50 +010029#include "call/call.h"
30#include "call/fake_network_pipe.h"
Yves Gerey3e707812018-11-28 16:47:49 +010031#include "call/flexfec_receive_stream.h"
32#include "call/packet_receiver.h"
33#include "call/rtp_transport_controller_send_interface.h"
Artem Titov3229d652018-08-17 13:00:54 +020034#include "call/simulated_network.h"
Yves Gerey3e707812018-11-28 16:47:49 +010035#include "call/video_receive_stream.h"
36#include "call/video_send_stream.h"
Erik Språng09708512018-03-14 15:16:50 +010037#include "modules/utility/include/process_thread.h"
Steve Anton10542f22019-01-11 09:11:00 -080038#include "rtc_base/copy_on_write_buffer.h"
Yves Gerey3e707812018-11-28 16:47:49 +010039#include "rtc_base/network/sent_packet.h"
Erik Språngc6488192019-08-06 15:54:23 +020040#include "rtc_base/task_queue.h"
Erik Språng09708512018-03-14 15:16:50 +010041#include "system_wrappers/include/clock.h"
42
43namespace webrtc {
Erik Språngeea605d2019-08-12 15:56:51 +020044class DegradedCall : public Call, private PacketReceiver {
Erik Språng09708512018-03-14 15:16:50 +010045 public:
Artem Titov3229d652018-08-17 13:00:54 +020046 explicit DegradedCall(
47 std::unique_ptr<Call> call,
Artem Titov75e36472018-10-08 12:28:56 +020048 absl::optional<BuiltInNetworkBehaviorConfig> send_config,
Erik Språngc6488192019-08-06 15:54:23 +020049 absl::optional<BuiltInNetworkBehaviorConfig> receive_config,
50 TaskQueueFactory* task_queue_factory);
Erik Språng09708512018-03-14 15:16:50 +010051 ~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 PacketReceiver* Receiver() override;
81
82 RtpTransportControllerSendInterface* GetTransportControllerSend() override;
83
84 Stats GetStats() const override;
85
Erik Språng09708512018-03-14 15:16:50 +010086 void SignalChannelNetworkState(MediaType media, NetworkState state) override;
Stefan Holmer64be7fa2018-10-04 15:21:55 +020087 void OnAudioTransportOverheadChanged(
88 int transport_overhead_per_packet) override;
Erik Språng09708512018-03-14 15:16:50 +010089 void OnSentPacket(const rtc::SentPacket& sent_packet) override;
90
91 protected:
Erik Språng09708512018-03-14 15:16:50 +010092 // Implements PacketReceiver.
93 DeliveryStatus DeliverPacket(MediaType media_type,
94 rtc::CopyOnWriteBuffer packet,
Niels Möller70082872018-08-07 11:03:12 +020095 int64_t packet_time_us) override;
Erik Språng09708512018-03-14 15:16:50 +010096
97 private:
Erik Språngc6488192019-08-06 15:54:23 +020098 class FakeNetworkPipeOnTaskQueue {
99 public:
100 FakeNetworkPipeOnTaskQueue(
101 TaskQueueFactory* task_queue_factory,
102 Clock* clock,
Erik Språngeea605d2019-08-12 15:56:51 +0200103 std::unique_ptr<NetworkBehaviorInterface> network_behavior);
Erik Språngc6488192019-08-06 15:54:23 +0200104
105 void SendRtp(const uint8_t* packet,
106 size_t length,
Erik Språngeea605d2019-08-12 15:56:51 +0200107 const PacketOptions& options,
108 Transport* transport);
109 void SendRtcp(const uint8_t* packet, size_t length, Transport* transport);
110
111 void AddActiveTransport(Transport* transport);
112 void RemoveActiveTransport(Transport* transport);
Erik Språngc6488192019-08-06 15:54:23 +0200113
114 private:
115 // Try to process packets on the fake network queue.
116 // Returns true if call resulted in a delayed process, false if queue empty.
117 bool Process();
118
119 Clock* const clock_;
120 rtc::TaskQueue task_queue_;
121 FakeNetworkPipe pipe_;
122 absl::optional<int64_t> next_process_ms_ RTC_GUARDED_BY(&task_queue_);
123 };
124
Erik Språngeea605d2019-08-12 15:56:51 +0200125 // For audio/video send stream, a TransportAdapter instance is used to
126 // intercept packets to be sent, and put them into a common FakeNetworkPipe
127 // in such as way that they will eventually (unless dropped) be forwarded to
128 // the correct Transport for that stream.
129 class FakeNetworkPipeTransportAdapter : public Transport {
130 public:
131 FakeNetworkPipeTransportAdapter(FakeNetworkPipeOnTaskQueue* fake_network,
132 Call* call,
133 Clock* clock,
134 Transport* real_transport);
135 ~FakeNetworkPipeTransportAdapter();
136
137 bool SendRtp(const uint8_t* packet,
138 size_t length,
139 const PacketOptions& options) override;
140 bool SendRtcp(const uint8_t* packet, size_t length) override;
141
142 private:
143 FakeNetworkPipeOnTaskQueue* const network_pipe_;
144 Call* const call_;
145 Clock* const clock_;
146 Transport* const real_transport_;
147 };
148
Erik Språng09708512018-03-14 15:16:50 +0100149 Clock* const clock_;
150 const std::unique_ptr<Call> call_;
Erik Språngc6488192019-08-06 15:54:23 +0200151 TaskQueueFactory* const task_queue_factory_;
Erik Språng09708512018-03-14 15:16:50 +0100152
Piotr (Peter) Slatala7fbfaa42019-03-18 10:31:54 -0700153 void SetClientBitratePreferences(
154 const webrtc::BitrateSettings& preferences) override {}
Erik Språngeea605d2019-08-12 15:56:51 +0200155
Artem Titov75e36472018-10-08 12:28:56 +0200156 const absl::optional<BuiltInNetworkBehaviorConfig> send_config_;
Artem Titov3229d652018-08-17 13:00:54 +0200157 SimulatedNetwork* send_simulated_network_;
Erik Språngc6488192019-08-06 15:54:23 +0200158 std::unique_ptr<FakeNetworkPipeOnTaskQueue> send_pipe_;
Erik Språngeea605d2019-08-12 15:56:51 +0200159 std::map<AudioSendStream*, std::unique_ptr<FakeNetworkPipeTransportAdapter>>
160 audio_send_transport_adapters_;
161 std::map<VideoSendStream*, std::unique_ptr<FakeNetworkPipeTransportAdapter>>
162 video_send_transport_adapters_;
Erik Språng09708512018-03-14 15:16:50 +0100163
Artem Titov75e36472018-10-08 12:28:56 +0200164 const absl::optional<BuiltInNetworkBehaviorConfig> receive_config_;
Artem Titov3229d652018-08-17 13:00:54 +0200165 SimulatedNetwork* receive_simulated_network_;
Erik Språng09708512018-03-14 15:16:50 +0100166 std::unique_ptr<FakeNetworkPipe> receive_pipe_;
167};
168
169} // namespace webrtc
170
171#endif // CALL_DEGRADED_CALL_H_