channel_manager.h 7.2 KB

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