channel_manager.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. * Copyright 2004 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 PC_CHANNEL_MANAGER_H_
  11. #define PC_CHANNEL_MANAGER_H_
  12. #include <stdint.h>
  13. #include <memory>
  14. #include <string>
  15. #include <vector>
  16. #include "api/audio_options.h"
  17. #include "api/crypto/crypto_options.h"
  18. #include "api/transport/media/media_transport_config.h"
  19. #include "call/call.h"
  20. #include "media/base/codec.h"
  21. #include "media/base/media_channel.h"
  22. #include "media/base/media_config.h"
  23. #include "media/base/media_engine.h"
  24. #include "pc/channel.h"
  25. #include "pc/rtp_transport_internal.h"
  26. #include "pc/session_description.h"
  27. #include "rtc_base/system/file_wrapper.h"
  28. #include "rtc_base/thread.h"
  29. namespace cricket {
  30. // ChannelManager allows the MediaEngine to run on a separate thread, and takes
  31. // care of marshalling calls between threads. It also creates and keeps track of
  32. // voice and video channels; by doing so, it can temporarily pause all the
  33. // channels when a new audio or video device is chosen. The voice and video
  34. // channels are stored in separate vectors, to easily allow operations on just
  35. // voice or just video channels.
  36. // ChannelManager also allows the application to discover what devices it has
  37. // using device manager.
  38. class ChannelManager final {
  39. public:
  40. // Construct a ChannelManager with the specified media engine and data engine.
  41. ChannelManager(std::unique_ptr<MediaEngineInterface> media_engine,
  42. std::unique_ptr<DataEngineInterface> data_engine,
  43. rtc::Thread* worker_thread,
  44. rtc::Thread* network_thread);
  45. ~ChannelManager();
  46. // Accessors for the worker thread, allowing it to be set after construction,
  47. // but before Init. set_worker_thread will return false if called after Init.
  48. rtc::Thread* worker_thread() const { return worker_thread_; }
  49. bool set_worker_thread(rtc::Thread* thread) {
  50. if (initialized_) {
  51. return false;
  52. }
  53. worker_thread_ = thread;
  54. return true;
  55. }
  56. rtc::Thread* network_thread() const { return network_thread_; }
  57. bool set_network_thread(rtc::Thread* thread) {
  58. if (initialized_) {
  59. return false;
  60. }
  61. network_thread_ = thread;
  62. return true;
  63. }
  64. MediaEngineInterface* media_engine() { return media_engine_.get(); }
  65. // Retrieves the list of supported audio & video codec types.
  66. // Can be called before starting the media engine.
  67. void GetSupportedAudioSendCodecs(std::vector<AudioCodec>* codecs) const;
  68. void GetSupportedAudioReceiveCodecs(std::vector<AudioCodec>* codecs) const;
  69. void GetSupportedVideoSendCodecs(std::vector<VideoCodec>* codecs) const;
  70. void GetSupportedVideoReceiveCodecs(std::vector<VideoCodec>* codecs) const;
  71. void GetSupportedDataCodecs(std::vector<DataCodec>* codecs) const;
  72. RtpHeaderExtensions GetDefaultEnabledAudioRtpHeaderExtensions() const;
  73. std::vector<webrtc::RtpHeaderExtensionCapability>
  74. GetSupportedAudioRtpHeaderExtensions() const;
  75. RtpHeaderExtensions GetDefaultEnabledVideoRtpHeaderExtensions() const;
  76. std::vector<webrtc::RtpHeaderExtensionCapability>
  77. GetSupportedVideoRtpHeaderExtensions() const;
  78. // Indicates whether the media engine is started.
  79. bool initialized() const { return initialized_; }
  80. // Starts up the media engine.
  81. bool Init();
  82. // Shuts down the media engine.
  83. void Terminate();
  84. // The operations below all occur on the worker thread.
  85. // ChannelManager retains ownership of the created channels, so clients should
  86. // call the appropriate Destroy*Channel method when done.
  87. // Creates a voice channel, to be associated with the specified session.
  88. VoiceChannel* CreateVoiceChannel(
  89. webrtc::Call* call,
  90. const cricket::MediaConfig& media_config,
  91. webrtc::RtpTransportInternal* rtp_transport,
  92. const webrtc::MediaTransportConfig& media_transport_config,
  93. rtc::Thread* signaling_thread,
  94. const std::string& content_name,
  95. bool srtp_required,
  96. const webrtc::CryptoOptions& crypto_options,
  97. rtc::UniqueRandomIdGenerator* ssrc_generator,
  98. const AudioOptions& options);
  99. // Destroys a voice channel created by CreateVoiceChannel.
  100. void DestroyVoiceChannel(VoiceChannel* voice_channel);
  101. // Creates a video channel, synced with the specified voice channel, and
  102. // associated with the specified session.
  103. // Version of the above that takes PacketTransportInternal.
  104. VideoChannel* CreateVideoChannel(
  105. webrtc::Call* call,
  106. const cricket::MediaConfig& media_config,
  107. webrtc::RtpTransportInternal* rtp_transport,
  108. const webrtc::MediaTransportConfig& media_transport_config,
  109. rtc::Thread* signaling_thread,
  110. const std::string& content_name,
  111. bool srtp_required,
  112. const webrtc::CryptoOptions& crypto_options,
  113. rtc::UniqueRandomIdGenerator* ssrc_generator,
  114. const VideoOptions& options,
  115. webrtc::VideoBitrateAllocatorFactory* video_bitrate_allocator_factory);
  116. // Destroys a video channel created by CreateVideoChannel.
  117. void DestroyVideoChannel(VideoChannel* video_channel);
  118. RtpDataChannel* CreateRtpDataChannel(
  119. const cricket::MediaConfig& media_config,
  120. webrtc::RtpTransportInternal* rtp_transport,
  121. rtc::Thread* signaling_thread,
  122. const std::string& content_name,
  123. bool srtp_required,
  124. const webrtc::CryptoOptions& crypto_options,
  125. rtc::UniqueRandomIdGenerator* ssrc_generator);
  126. // Destroys a data channel created by CreateRtpDataChannel.
  127. void DestroyRtpDataChannel(RtpDataChannel* data_channel);
  128. // Indicates whether any channels exist.
  129. bool has_channels() const {
  130. return (!voice_channels_.empty() || !video_channels_.empty() ||
  131. !data_channels_.empty());
  132. }
  133. // RTX will be enabled/disabled in engines that support it. The supporting
  134. // engines will start offering an RTX codec. Must be called before Init().
  135. bool SetVideoRtxEnabled(bool enable);
  136. // Starts/stops the local microphone and enables polling of the input level.
  137. bool capturing() const { return capturing_; }
  138. // The operations below occur on the main thread.
  139. // Starts AEC dump using existing file, with a specified maximum file size in
  140. // bytes. When the limit is reached, logging will stop and the file will be
  141. // closed. If max_size_bytes is set to <= 0, no limit will be used.
  142. bool StartAecDump(webrtc::FileWrapper file, int64_t max_size_bytes);
  143. // Stops recording AEC dump.
  144. void StopAecDump();
  145. private:
  146. std::unique_ptr<MediaEngineInterface> media_engine_; // Nullable.
  147. std::unique_ptr<DataEngineInterface> data_engine_; // Non-null.
  148. bool initialized_ = false;
  149. rtc::Thread* main_thread_;
  150. rtc::Thread* worker_thread_;
  151. rtc::Thread* network_thread_;
  152. // Vector contents are non-null.
  153. std::vector<std::unique_ptr<VoiceChannel>> voice_channels_;
  154. std::vector<std::unique_ptr<VideoChannel>> video_channels_;
  155. std::vector<std::unique_ptr<RtpDataChannel>> data_channels_;
  156. bool enable_rtx_ = false;
  157. bool capturing_ = false;
  158. };
  159. } // namespace cricket
  160. #endif // PC_CHANNEL_MANAGER_H_