degraded_call.h 6.2 KB

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