123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519 |
- #ifndef PC_CHANNEL_H_
- #define PC_CHANNEL_H_
- #include <map>
- #include <memory>
- #include <set>
- #include <string>
- #include <utility>
- #include <vector>
- #include "api/call/audio_sink.h"
- #include "api/function_view.h"
- #include "api/jsep.h"
- #include "api/rtp_receiver_interface.h"
- #include "api/video/video_sink_interface.h"
- #include "api/video/video_source_interface.h"
- #include "call/rtp_packet_sink_interface.h"
- #include "media/base/media_channel.h"
- #include "media/base/media_engine.h"
- #include "media/base/stream_params.h"
- #include "p2p/base/dtls_transport_internal.h"
- #include "p2p/base/packet_transport_internal.h"
- #include "pc/channel_interface.h"
- #include "pc/dtls_srtp_transport.h"
- #include "pc/media_session.h"
- #include "pc/rtp_transport.h"
- #include "pc/srtp_filter.h"
- #include "pc/srtp_transport.h"
- #include "rtc_base/async_invoker.h"
- #include "rtc_base/async_udp_socket.h"
- #include "rtc_base/network.h"
- #include "rtc_base/synchronization/sequence_checker.h"
- #include "rtc_base/third_party/sigslot/sigslot.h"
- #include "rtc_base/thread_annotations.h"
- #include "rtc_base/unique_id_generator.h"
- namespace webrtc {
- class AudioSinkInterface;
- }
- namespace cricket {
- struct CryptoParams;
- class BaseChannel : public ChannelInterface,
- public rtc::MessageHandlerAutoCleanup,
- public sigslot::has_slots<>,
- public MediaChannel::NetworkInterface,
- public webrtc::RtpPacketSinkInterface {
- public:
-
-
-
-
-
-
- BaseChannel(rtc::Thread* worker_thread,
- rtc::Thread* network_thread,
- rtc::Thread* signaling_thread,
- std::unique_ptr<MediaChannel> media_channel,
- const std::string& content_name,
- bool srtp_required,
- webrtc::CryptoOptions crypto_options,
- rtc::UniqueRandomIdGenerator* ssrc_generator);
- virtual ~BaseChannel();
- virtual void Init_w(webrtc::RtpTransportInternal* rtp_transport);
-
-
- void Deinit();
- rtc::Thread* worker_thread() const { return worker_thread_; }
- rtc::Thread* network_thread() const { return network_thread_; }
- const std::string& content_name() const override { return content_name_; }
-
- const std::string& transport_name() const override { return transport_name_; }
- bool enabled() const override { return enabled_; }
-
- bool srtp_active() const {
- return rtp_transport_ && rtp_transport_->IsSrtpActive();
- }
- bool writable() const { return writable_; }
-
-
-
-
- bool SetRtpTransport(webrtc::RtpTransportInternal* rtp_transport) override;
- webrtc::RtpTransportInternal* rtp_transport() const { return rtp_transport_; }
-
- bool SetLocalContent(const MediaContentDescription* content,
- webrtc::SdpType type,
- std::string* error_desc) override;
- bool SetRemoteContent(const MediaContentDescription* content,
- webrtc::SdpType type,
- std::string* error_desc) override;
-
-
-
-
-
-
-
-
- bool SetPayloadTypeDemuxingEnabled(bool enabled) override;
- bool Enable(bool enable) override;
- const std::vector<StreamParams>& local_streams() const override {
- return local_streams_;
- }
- const std::vector<StreamParams>& remote_streams() const override {
- return remote_streams_;
- }
-
- sigslot::signal1<ChannelInterface*>& SignalFirstPacketReceived() override;
-
- sigslot::signal1<const rtc::SentPacket&>& SignalSentPacket();
-
- void OnTransportReadyToSend(bool ready);
-
- int SetOption(SocketType type, rtc::Socket::Option o, int val) override;
- int SetOption_n(SocketType type, rtc::Socket::Option o, int val);
-
- void OnRtpPacket(const webrtc::RtpPacketReceived& packet) override;
-
-
- void set_transport_name_for_testing(const std::string& transport_name) {
- transport_name_ = transport_name;
- }
- MediaChannel* media_channel() const override { return media_channel_.get(); }
- protected:
- bool was_ever_writable() const { return was_ever_writable_; }
- void set_local_content_direction(webrtc::RtpTransceiverDirection direction) {
- local_content_direction_ = direction;
- }
- void set_remote_content_direction(webrtc::RtpTransceiverDirection direction) {
- remote_content_direction_ = direction;
- }
-
-
-
-
-
-
-
-
-
-
- bool IsReadyToReceiveMedia_w() const;
- bool IsReadyToSendMedia_w() const;
- rtc::Thread* signaling_thread() { return signaling_thread_; }
- void FlushRtcpMessages_n();
-
- bool SendPacket(rtc::CopyOnWriteBuffer* packet,
- const rtc::PacketOptions& options) override;
- bool SendRtcp(rtc::CopyOnWriteBuffer* packet,
- const rtc::PacketOptions& options) override;
-
- void OnWritableState(bool writable);
- void OnNetworkRouteChanged(absl::optional<rtc::NetworkRoute> network_route);
- bool PacketIsRtcp(const rtc::PacketTransportInternal* transport,
- const char* data,
- size_t len);
- bool SendPacket(bool rtcp,
- rtc::CopyOnWriteBuffer* packet,
- const rtc::PacketOptions& options);
- void EnableMedia_w();
- void DisableMedia_w();
-
-
-
- void UpdateWritableState_n();
- void ChannelWritable_n();
- void ChannelNotWritable_n();
- bool AddRecvStream_w(const StreamParams& sp);
- bool RemoveRecvStream_w(uint32_t ssrc);
- void ResetUnsignaledRecvStream_w();
- bool SetPayloadTypeDemuxingEnabled_w(bool enabled);
- bool AddSendStream_w(const StreamParams& sp);
- bool RemoveSendStream_w(uint32_t ssrc);
-
-
-
- void UpdateMediaSendRecvState();
- virtual void UpdateMediaSendRecvState_w() = 0;
- bool UpdateLocalStreams_w(const std::vector<StreamParams>& streams,
- webrtc::SdpType type,
- std::string* error_desc);
- bool UpdateRemoteStreams_w(const std::vector<StreamParams>& streams,
- webrtc::SdpType type,
- std::string* error_desc);
- virtual bool SetLocalContent_w(const MediaContentDescription* content,
- webrtc::SdpType type,
- std::string* error_desc) = 0;
- virtual bool SetRemoteContent_w(const MediaContentDescription* content,
- webrtc::SdpType type,
- std::string* error_desc) = 0;
-
-
-
- RtpHeaderExtensions GetFilteredRtpHeaderExtensions(
- const RtpHeaderExtensions& extensions);
-
- void OnMessage(rtc::Message* pmsg) override;
-
- template <class T>
- T InvokeOnWorker(const rtc::Location& posted_from,
- rtc::FunctionView<T()> functor) {
- return worker_thread_->Invoke<T>(posted_from, functor);
- }
-
-
- void MaybeAddHandledPayloadType(int payload_type) RTC_RUN_ON(worker_thread());
- void ClearHandledPayloadTypes() RTC_RUN_ON(worker_thread());
- void UpdateRtpHeaderExtensionMap(
- const RtpHeaderExtensions& header_extensions);
- bool RegisterRtpDemuxerSink();
-
- std::string ToString() const;
- bool has_received_packet_ = false;
- private:
- bool ConnectToRtpTransport();
- void DisconnectFromRtpTransport();
- void SignalSentPacket_n(const rtc::SentPacket& sent_packet);
- bool IsReadyToSendMedia_n() const;
- rtc::Thread* const worker_thread_;
- rtc::Thread* const network_thread_;
- rtc::Thread* const signaling_thread_;
- rtc::AsyncInvoker invoker_;
- sigslot::signal1<ChannelInterface*> SignalFirstPacketReceived_
- RTC_GUARDED_BY(signaling_thread_);
- sigslot::signal1<const rtc::SentPacket&> SignalSentPacket_
- RTC_GUARDED_BY(worker_thread_);
- const std::string content_name_;
-
- std::string transport_name_;
- webrtc::RtpTransportInternal* rtp_transport_ = nullptr;
- std::vector<std::pair<rtc::Socket::Option, int> > socket_options_;
- std::vector<std::pair<rtc::Socket::Option, int> > rtcp_socket_options_;
- bool writable_ = false;
- bool was_ever_writable_ = false;
- const bool srtp_required_ = true;
- webrtc::CryptoOptions crypto_options_;
-
-
- std::unique_ptr<MediaChannel> media_channel_;
-
-
-
- bool enabled_ = false;
- bool payload_type_demuxing_enabled_ RTC_GUARDED_BY(worker_thread()) = true;
- std::vector<StreamParams> local_streams_;
- std::vector<StreamParams> remote_streams_;
- webrtc::RtpTransceiverDirection local_content_direction_ =
- webrtc::RtpTransceiverDirection::kInactive;
- webrtc::RtpTransceiverDirection remote_content_direction_ =
- webrtc::RtpTransceiverDirection::kInactive;
-
- std::set<uint8_t> payload_types_ RTC_GUARDED_BY(worker_thread());
- webrtc::RtpDemuxerCriteria demuxer_criteria_;
-
-
-
-
- rtc::UniqueRandomIdGenerator* const ssrc_generator_;
- };
- class VoiceChannel : public BaseChannel {
- public:
- VoiceChannel(rtc::Thread* worker_thread,
- rtc::Thread* network_thread,
- rtc::Thread* signaling_thread,
- std::unique_ptr<VoiceMediaChannel> channel,
- const std::string& content_name,
- bool srtp_required,
- webrtc::CryptoOptions crypto_options,
- rtc::UniqueRandomIdGenerator* ssrc_generator);
- ~VoiceChannel();
-
- VoiceMediaChannel* media_channel() const override {
- return static_cast<VoiceMediaChannel*>(BaseChannel::media_channel());
- }
- cricket::MediaType media_type() const override {
- return cricket::MEDIA_TYPE_AUDIO;
- }
- void Init_w(webrtc::RtpTransportInternal* rtp_transport) override;
- private:
-
- void UpdateMediaSendRecvState_w() override;
- bool SetLocalContent_w(const MediaContentDescription* content,
- webrtc::SdpType type,
- std::string* error_desc) override;
- bool SetRemoteContent_w(const MediaContentDescription* content,
- webrtc::SdpType type,
- std::string* error_desc) override;
-
-
- AudioSendParameters last_send_params_;
-
-
- AudioRecvParameters last_recv_params_;
- };
- class VideoChannel : public BaseChannel {
- public:
- VideoChannel(rtc::Thread* worker_thread,
- rtc::Thread* network_thread,
- rtc::Thread* signaling_thread,
- std::unique_ptr<VideoMediaChannel> media_channel,
- const std::string& content_name,
- bool srtp_required,
- webrtc::CryptoOptions crypto_options,
- rtc::UniqueRandomIdGenerator* ssrc_generator);
- ~VideoChannel();
-
- VideoMediaChannel* media_channel() const override {
- return static_cast<VideoMediaChannel*>(BaseChannel::media_channel());
- }
- void FillBitrateInfo(BandwidthEstimationInfo* bwe_info);
- cricket::MediaType media_type() const override {
- return cricket::MEDIA_TYPE_VIDEO;
- }
- private:
-
- void UpdateMediaSendRecvState_w() override;
- bool SetLocalContent_w(const MediaContentDescription* content,
- webrtc::SdpType type,
- std::string* error_desc) override;
- bool SetRemoteContent_w(const MediaContentDescription* content,
- webrtc::SdpType type,
- std::string* error_desc) override;
-
-
- VideoSendParameters last_send_params_;
-
-
- VideoRecvParameters last_recv_params_;
- };
- class RtpDataChannel : public BaseChannel {
- public:
- RtpDataChannel(rtc::Thread* worker_thread,
- rtc::Thread* network_thread,
- rtc::Thread* signaling_thread,
- std::unique_ptr<DataMediaChannel> channel,
- const std::string& content_name,
- bool srtp_required,
- webrtc::CryptoOptions crypto_options,
- rtc::UniqueRandomIdGenerator* ssrc_generator);
- ~RtpDataChannel();
-
-
- void Init_w(DtlsTransportInternal* rtp_dtls_transport,
- DtlsTransportInternal* rtcp_dtls_transport,
- rtc::PacketTransportInternal* rtp_packet_transport,
- rtc::PacketTransportInternal* rtcp_packet_transport);
- void Init_w(webrtc::RtpTransportInternal* rtp_transport) override;
- virtual bool SendData(const SendDataParams& params,
- const rtc::CopyOnWriteBuffer& payload,
- SendDataResult* result);
-
- bool ready_to_send_data() const { return ready_to_send_data_; }
- sigslot::signal2<const ReceiveDataParams&, const rtc::CopyOnWriteBuffer&>
- SignalDataReceived;
-
-
-
- sigslot::signal1<bool> SignalReadyToSendData;
- cricket::MediaType media_type() const override {
- return cricket::MEDIA_TYPE_DATA;
- }
- protected:
-
- DataMediaChannel* media_channel() const override {
- return static_cast<DataMediaChannel*>(BaseChannel::media_channel());
- }
- private:
- struct SendDataMessageData : public rtc::MessageData {
- SendDataMessageData(const SendDataParams& params,
- const rtc::CopyOnWriteBuffer* payload,
- SendDataResult* result)
- : params(params), payload(payload), result(result), succeeded(false) {}
- const SendDataParams& params;
- const rtc::CopyOnWriteBuffer* payload;
- SendDataResult* result;
- bool succeeded;
- };
- struct DataReceivedMessageData : public rtc::MessageData {
-
-
-
- DataReceivedMessageData(const ReceiveDataParams& params,
- const char* data,
- size_t len)
- : params(params), payload(data, len) {}
- const ReceiveDataParams params;
- const rtc::CopyOnWriteBuffer payload;
- };
- typedef rtc::TypedMessageData<bool> DataChannelReadyToSendMessageData;
-
-
- bool CheckDataChannelTypeFromContent(const MediaContentDescription* content,
- std::string* error_desc);
- bool SetLocalContent_w(const MediaContentDescription* content,
- webrtc::SdpType type,
- std::string* error_desc) override;
- bool SetRemoteContent_w(const MediaContentDescription* content,
- webrtc::SdpType type,
- std::string* error_desc) override;
- void UpdateMediaSendRecvState_w() override;
- void OnMessage(rtc::Message* pmsg) override;
- void OnDataReceived(const ReceiveDataParams& params,
- const char* data,
- size_t len);
- void OnDataChannelReadyToSend(bool writable);
- bool ready_to_send_data_ = false;
-
-
- DataSendParameters last_send_params_;
-
-
- DataRecvParameters last_recv_params_;
- };
- }
- #endif
|