| 5 | #include <string> |
| 6 | #include <vector> |
| 7 | #include "api/audio_options.h" |
| 8 | #include "api/crypto/crypto_options.h" |
| 9 | #include "api/rtp_parameters.h" |
| 10 | #include "api/video/video_bitrate_allocator_factory.h" |
| 11 | #include "call/call.h" |
| 12 | #include "media/base/codec.h" |
| 13 | #include "media/base/media_channel.h" |
| 14 | #include "media/base/media_config.h" |
| 15 | #include "media/base/media_engine.h" |
| 16 | #include "pc/channel.h" |
| 17 | #include "pc/channel_interface.h" |
| 18 | #include "pc/rtp_transport_internal.h" |
| 19 | #include "pc/session_description.h" |
| 20 | #include "rtc_base/system/file_wrapper.h" |
| 21 | #include "rtc_base/thread.h" |
| 22 | #include "rtc_base/thread_annotations.h" |
| 23 | #include "rtc_base/unique_id_generator.h" |
| 24 | namespace tgcalls { |
| 25 | // ChannelManager allows the MediaEngine to run on a separate thread, and takes |
| 26 | // care of marshalling calls between threads. It also creates and keeps track of |
| 27 | // voice and video channels; by doing so, it can temporarily pause all the |
| 28 | // channels when a new audio or video device is chosen. The voice and video |
| 29 | // channels are stored in separate vectors, to easily allow operations on just |
| 30 | // voice or just video channels. |
| 31 | // ChannelManager also allows the application to discover what devices it has |
| 32 | // using device manager. |
| 33 | class ChannelManager { |
| 34 | public: |
| 35 | // Returns an initialized instance of ChannelManager. |
| 36 | // If media_engine is non-nullptr, then the returned ChannelManager instance |
| 37 | // will own that reference and media engine initialization |
| 38 | static std::unique_ptr<ChannelManager> Create( |
| 39 | std::unique_ptr<cricket::MediaEngineInterface> media_engine, |
| 40 | rtc::Thread* worker_thread, |
| 41 | rtc::Thread* network_thread); |
| 42 | ChannelManager() = delete; |
| 43 | ~ChannelManager(); |
| 44 | rtc::Thread* worker_thread() const { return worker_thread_; } |
| 45 | rtc::Thread* network_thread() const { return network_thread_; } |
| 46 | cricket::MediaEngineInterface* media_engine() { return media_engine_.get(); } |
| 47 | rtc::UniqueRandomIdGenerator& ssrc_generator() { return ssrc_generator_; } |
| 48 | // The operations below all occur on the worker thread. |
| 49 | // ChannelManager retains ownership of the created channels, so clients should |
| 50 | // call the appropriate Destroy*Channel method when done. |
| 51 | // Creates a voice channel, to be associated with the specified session. |
| 52 | cricket::VoiceChannel* CreateVoiceChannel(webrtc::Call* call, |
| 53 | const cricket::MediaConfig& media_config, |
| 54 | const std::string& mid, |
| 55 | bool srtp_required, |
| 56 | const webrtc::CryptoOptions& crypto_options, |
| 57 | const cricket::AudioOptions& options); |
| 58 | // Creates a video channel, synced with the specified voice channel, and |
| 59 | // associated with the specified session. |
| 60 | // Version of the above that takes PacketTransportInternal. |
| 61 | cricket::VideoChannel* CreateVideoChannel( |
| 62 | webrtc::Call* call, |
| 63 | const cricket::MediaConfig& media_config, |
| 64 | const std::string& mid, |
| 65 | bool srtp_required, |
| 66 | const webrtc::CryptoOptions& crypto_options, |
| 67 | const cricket::VideoOptions& options, |
| 68 | webrtc::VideoBitrateAllocatorFactory* video_bitrate_allocator_factory); |
| 69 | void DestroyChannel(cricket::ChannelInterface* channel); |
| 70 | protected: |
| 71 | ChannelManager(std::unique_ptr<cricket::MediaEngineInterface> media_engine, |
| 72 | rtc::Thread* worker_thread, |
| 73 | rtc::Thread* network_thread); |
| 74 | // Destroys a voice channel created by CreateVoiceChannel. |
| 75 | void DestroyVoiceChannel(cricket::VoiceChannel* voice_channel); |
| 76 | // Destroys a video channel created by CreateVideoChannel. |
| 77 | void DestroyVideoChannel(cricket::VideoChannel* video_channel); |
| 78 | private: |
| 79 | const std::unique_ptr<cricket::MediaEngineInterface> media_engine_; // Nullable. |
| 80 | rtc::Thread* const signaling_thread_; |
| 81 | rtc::Thread* const worker_thread_; |
| 82 | rtc::Thread* const network_thread_; |
| 83 | // This object should be used to generate any SSRC that is not explicitly |
| 84 | // specified by the user (or by the remote party). |
| 85 | // TODO(bugs.webrtc.org/12666): This variable is used from both the signaling |
| 86 | // and worker threads. See if we can't restrict usage to a single thread. |
| 87 | rtc::UniqueRandomIdGenerator ssrc_generator_; |
| 88 | // Vector contents are non-null. |
| 89 | std::vector<std::unique_ptr<cricket::VoiceChannel>> voice_channels_ |
| 90 | RTC_GUARDED_BY(worker_thread_); |
| 91 | std::vector<std::unique_ptr<cricket::VideoChannel>> video_channels_ |
| 92 | RTC_GUARDED_BY(worker_thread_); |
| 93 | }; |
| 94 | } // namespace tgcalls |
| 95 | #endif // TGCALLS_CHANNEL_MANAGER_H_ |