2020-02-28 15:18:39 +00:00
|
|
|
/**
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
2020-03-31 10:03:04 +00:00
|
|
|
* Copyright (c) 2013-2020 John
|
2020-02-28 15:18:39 +00:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
|
|
* this software and associated documentation files (the "Software"), to deal in
|
|
|
|
* the Software without restriction, including without limitation the rights to
|
|
|
|
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
|
|
* the Software, and to permit persons to whom the Software is furnished to do so,
|
|
|
|
* subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
|
|
* copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
|
|
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
|
|
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef SRS_APP_RTC_CONN_HPP
|
|
|
|
#define SRS_APP_RTC_CONN_HPP
|
|
|
|
|
|
|
|
#include <srs_core.hpp>
|
2020-03-07 16:30:31 +00:00
|
|
|
#include <srs_app_listener.hpp>
|
2020-03-06 15:01:48 +00:00
|
|
|
#include <srs_service_st.hpp>
|
2020-03-12 16:24:56 +00:00
|
|
|
#include <srs_kernel_utility.hpp>
|
2020-03-14 14:11:01 +00:00
|
|
|
#include <srs_rtmp_stack.hpp>
|
2020-03-17 09:56:37 +00:00
|
|
|
#include <srs_app_hybrid.hpp>
|
2020-03-19 04:58:04 +00:00
|
|
|
#include <srs_app_hourglass.hpp>
|
2020-03-30 07:16:29 +00:00
|
|
|
#include <srs_app_sdp.hpp>
|
2020-04-10 10:14:33 +00:00
|
|
|
#include <srs_app_reload.hpp>
|
2020-02-28 15:18:39 +00:00
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <map>
|
2020-03-02 14:47:40 +00:00
|
|
|
#include <vector>
|
2020-04-05 08:53:08 +00:00
|
|
|
#include <sys/socket.h>
|
2020-02-28 15:18:39 +00:00
|
|
|
|
2020-03-06 15:01:48 +00:00
|
|
|
#include <openssl/ssl.h>
|
|
|
|
#include <srtp2/srtp.h>
|
|
|
|
|
2020-03-12 16:24:56 +00:00
|
|
|
class SrsUdpMuxSocket;
|
2020-03-09 11:46:27 +00:00
|
|
|
class SrsConsumer;
|
2020-02-28 15:18:39 +00:00
|
|
|
class SrsStunPacket;
|
2020-03-09 11:46:27 +00:00
|
|
|
class SrsRtcServer;
|
|
|
|
class SrsRtcSession;
|
2020-03-12 16:24:56 +00:00
|
|
|
class SrsSharedPtrMessage;
|
2020-03-14 14:11:01 +00:00
|
|
|
class SrsSource;
|
2020-04-11 14:54:44 +00:00
|
|
|
class SrsRtpPacket2;
|
2020-04-16 11:33:10 +00:00
|
|
|
class ISrsUdpSender;
|
2020-04-23 09:08:21 +00:00
|
|
|
class SrsRtpQueue;
|
|
|
|
class SrsRtpH264Demuxer;
|
2020-04-24 08:19:08 +00:00
|
|
|
class SrsRtpOpusDemuxer;
|
2020-03-12 16:24:56 +00:00
|
|
|
|
2020-03-14 14:11:01 +00:00
|
|
|
const uint8_t kSR = 200;
|
|
|
|
const uint8_t kRR = 201;
|
2020-03-12 16:24:56 +00:00
|
|
|
const uint8_t kSDES = 202;
|
2020-03-14 14:11:01 +00:00
|
|
|
const uint8_t kBye = 203;
|
|
|
|
const uint8_t kApp = 204;
|
|
|
|
|
|
|
|
// @see: https://tools.ietf.org/html/rfc4585#section-6.1
|
|
|
|
const uint8_t kRtpFb = 205;
|
|
|
|
const uint8_t kPsFb = 206;
|
2020-04-23 09:08:21 +00:00
|
|
|
const uint8_t kXR = 207;
|
2020-03-14 14:11:01 +00:00
|
|
|
|
|
|
|
// @see: https://tools.ietf.org/html/rfc4585#section-6.3
|
|
|
|
const uint8_t kPLI = 1;
|
|
|
|
const uint8_t kSLI = 2;
|
|
|
|
const uint8_t kRPSI = 3;
|
|
|
|
const uint8_t kAFB = 15;
|
2020-03-12 16:24:56 +00:00
|
|
|
|
2020-04-23 09:08:21 +00:00
|
|
|
class SrsNtp
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
uint64_t system_ms_;
|
|
|
|
uint64_t ntp_;
|
|
|
|
uint32_t ntp_second_;
|
|
|
|
uint32_t ntp_fractions_;
|
|
|
|
public:
|
|
|
|
SrsNtp();
|
|
|
|
virtual ~SrsNtp();
|
|
|
|
public:
|
|
|
|
static SrsNtp from_time_ms(uint64_t ms);
|
|
|
|
static SrsNtp to_time_ms(uint64_t ntp);
|
|
|
|
public:
|
|
|
|
static uint64_t kMagicNtpFractionalUnit;
|
|
|
|
};
|
|
|
|
|
2020-02-28 15:18:39 +00:00
|
|
|
enum SrsRtcSessionStateType
|
|
|
|
{
|
2020-03-16 14:01:09 +00:00
|
|
|
// TODO: FIXME: Should prefixed by enum name.
|
2020-02-28 15:18:39 +00:00
|
|
|
INIT = -1,
|
|
|
|
WAITING_STUN = 1,
|
|
|
|
DOING_DTLS_HANDSHAKE = 2,
|
|
|
|
ESTABLISHED = 3,
|
|
|
|
CLOSED = 4,
|
|
|
|
};
|
|
|
|
|
2020-03-06 15:01:48 +00:00
|
|
|
class SrsDtlsSession
|
2020-02-28 15:18:39 +00:00
|
|
|
{
|
2020-03-06 15:01:48 +00:00
|
|
|
private:
|
2020-03-09 11:46:27 +00:00
|
|
|
SrsRtcSession* rtc_session;
|
|
|
|
|
2020-04-09 00:20:55 +00:00
|
|
|
SSL* dtls;
|
2020-03-06 15:01:48 +00:00
|
|
|
BIO* bio_in;
|
|
|
|
BIO* bio_out;
|
|
|
|
|
|
|
|
std::string client_key;
|
|
|
|
std::string server_key;
|
|
|
|
|
|
|
|
srtp_t srtp_send;
|
|
|
|
srtp_t srtp_recv;
|
|
|
|
|
|
|
|
bool handshake_done;
|
|
|
|
|
2020-02-28 15:18:39 +00:00
|
|
|
public:
|
2020-03-09 11:46:27 +00:00
|
|
|
SrsDtlsSession(SrsRtcSession* s);
|
2020-03-06 15:01:48 +00:00
|
|
|
virtual ~SrsDtlsSession();
|
|
|
|
|
2020-04-26 08:12:23 +00:00
|
|
|
srs_error_t initialize(SrsRequest* r);
|
2020-03-30 07:16:29 +00:00
|
|
|
|
2020-04-10 11:21:47 +00:00
|
|
|
srs_error_t on_dtls(SrsUdpMuxSocket* skt);
|
|
|
|
srs_error_t on_dtls_handshake_done(SrsUdpMuxSocket* skt);
|
2020-03-06 15:01:48 +00:00
|
|
|
srs_error_t on_dtls_application_data(const char* data, const int len);
|
2020-03-12 16:24:56 +00:00
|
|
|
public:
|
|
|
|
srs_error_t protect_rtp(char* protected_buf, const char* ori_buf, int& nb_protected_buf);
|
2020-04-13 07:24:41 +00:00
|
|
|
srs_error_t protect_rtp2(void* rtp_hdr, int* len_ptr);
|
2020-03-12 16:24:56 +00:00
|
|
|
srs_error_t unprotect_rtp(char* unprotected_buf, const char* ori_buf, int& nb_unprotected_buf);
|
|
|
|
srs_error_t protect_rtcp(char* protected_buf, const char* ori_buf, int& nb_protected_buf);
|
|
|
|
srs_error_t unprotect_rtcp(char* unprotected_buf, const char* ori_buf, int& nb_unprotected_buf);
|
|
|
|
private:
|
2020-04-10 11:21:47 +00:00
|
|
|
srs_error_t handshake(SrsUdpMuxSocket* skt);
|
2020-03-07 16:30:31 +00:00
|
|
|
private:
|
2020-03-09 11:46:27 +00:00
|
|
|
srs_error_t srtp_initialize();
|
2020-03-12 16:24:56 +00:00
|
|
|
srs_error_t srtp_send_init();
|
|
|
|
srs_error_t srtp_recv_init();
|
2020-03-06 15:01:48 +00:00
|
|
|
};
|
|
|
|
|
2020-04-14 12:12:14 +00:00
|
|
|
// A group of RTP packets.
|
2020-04-13 08:50:24 +00:00
|
|
|
class SrsRtcPackets
|
|
|
|
{
|
|
|
|
public:
|
2020-04-13 15:40:30 +00:00
|
|
|
bool use_gso;
|
2020-04-13 08:50:24 +00:00
|
|
|
bool should_merge_nalus;
|
|
|
|
public:
|
2020-04-16 01:25:18 +00:00
|
|
|
#if defined(SRS_DEBUG)
|
|
|
|
// Debug id.
|
|
|
|
uint32_t debug_id;
|
|
|
|
#endif
|
2020-04-16 02:05:17 +00:00
|
|
|
public:
|
2020-04-17 08:36:56 +00:00
|
|
|
// The total bytes of AVFrame packets.
|
2020-04-13 08:50:24 +00:00
|
|
|
int nn_bytes;
|
2020-04-17 08:36:56 +00:00
|
|
|
// The total bytes of RTP packets.
|
|
|
|
int nn_rtp_bytes;
|
2020-04-16 02:05:17 +00:00
|
|
|
// The total padded bytes.
|
|
|
|
int nn_padding_bytes;
|
|
|
|
public:
|
2020-04-14 12:12:14 +00:00
|
|
|
// The RTP packets send out by sendmmsg or sendmsg. Note that if many packets group to
|
|
|
|
// one msghdr by GSO, it's only one RTP packet, because we only send once.
|
2020-04-13 08:50:24 +00:00
|
|
|
int nn_rtp_pkts;
|
2020-04-14 12:12:14 +00:00
|
|
|
// For video, the samples or NALUs.
|
2020-04-13 08:50:24 +00:00
|
|
|
int nn_samples;
|
2020-04-14 12:12:14 +00:00
|
|
|
// For audio, the generated extra audio packets.
|
|
|
|
// For example, when transcoding AAC to opus, may many extra payloads for a audio.
|
2020-04-13 09:11:46 +00:00
|
|
|
int nn_extras;
|
2020-04-14 12:12:14 +00:00
|
|
|
// The original audio messages.
|
2020-04-13 08:50:24 +00:00
|
|
|
int nn_audios;
|
2020-04-14 12:12:14 +00:00
|
|
|
// The original video messages.
|
2020-04-13 08:50:24 +00:00
|
|
|
int nn_videos;
|
2020-04-15 07:58:17 +00:00
|
|
|
// The number of padded packet.
|
|
|
|
int nn_paddings;
|
2020-04-17 04:30:53 +00:00
|
|
|
// The number of dropped messages.
|
|
|
|
int nn_dropped;
|
2020-04-15 14:46:06 +00:00
|
|
|
private:
|
|
|
|
int cursor;
|
2020-04-18 12:37:08 +00:00
|
|
|
int nn_cache;
|
|
|
|
SrsRtpPacket2* cache;
|
2020-04-13 08:50:24 +00:00
|
|
|
public:
|
2020-04-18 12:37:08 +00:00
|
|
|
SrsRtcPackets(int nn_cache_max);
|
2020-04-13 08:50:24 +00:00
|
|
|
virtual ~SrsRtcPackets();
|
2020-04-15 13:59:27 +00:00
|
|
|
public:
|
|
|
|
void reset(bool gso, bool merge_nalus);
|
2020-04-15 14:46:06 +00:00
|
|
|
SrsRtpPacket2* fetch();
|
|
|
|
SrsRtpPacket2* back();
|
|
|
|
int size();
|
2020-04-16 04:35:36 +00:00
|
|
|
int capacity();
|
2020-04-15 14:46:06 +00:00
|
|
|
SrsRtpPacket2* at(int index);
|
2020-04-13 08:50:24 +00:00
|
|
|
};
|
|
|
|
|
2020-04-26 08:12:23 +00:00
|
|
|
// TODO: FIXME: Rename to RTC player or subscriber.
|
2020-04-13 08:50:24 +00:00
|
|
|
class SrsRtcSenderThread : virtual public ISrsCoroutineHandler, virtual public ISrsReloadHandler
|
2020-03-09 11:46:27 +00:00
|
|
|
{
|
|
|
|
protected:
|
|
|
|
SrsCoroutine* trd;
|
|
|
|
int _parent_cid;
|
|
|
|
private:
|
|
|
|
SrsRtcSession* rtc_session;
|
2020-03-30 07:16:29 +00:00
|
|
|
uint32_t video_ssrc;
|
|
|
|
uint32_t audio_ssrc;
|
|
|
|
uint16_t video_payload_type;
|
|
|
|
uint16_t audio_payload_type;
|
2020-04-11 09:52:14 +00:00
|
|
|
private:
|
|
|
|
// TODO: FIXME: How to handle timestamp overflow?
|
2020-04-11 10:39:46 +00:00
|
|
|
uint32_t audio_timestamp;
|
|
|
|
uint16_t audio_sequence;
|
2020-04-11 13:03:37 +00:00
|
|
|
private:
|
|
|
|
uint16_t video_sequence;
|
2020-03-22 08:54:31 +00:00
|
|
|
public:
|
|
|
|
SrsUdpMuxSocket* sendonly_ukt;
|
2020-04-17 06:24:24 +00:00
|
|
|
private:
|
2020-04-16 11:33:10 +00:00
|
|
|
ISrsUdpSender* sender;
|
2020-04-17 06:24:24 +00:00
|
|
|
private:
|
2020-04-13 08:50:24 +00:00
|
|
|
bool merge_nalus;
|
|
|
|
bool gso;
|
2020-04-15 07:58:17 +00:00
|
|
|
int max_padding;
|
2020-04-18 02:04:45 +00:00
|
|
|
private:
|
|
|
|
srs_utime_t mw_sleep;
|
|
|
|
int mw_msgs;
|
|
|
|
bool realtime;
|
2020-03-09 11:46:27 +00:00
|
|
|
public:
|
2020-03-12 16:24:56 +00:00
|
|
|
SrsRtcSenderThread(SrsRtcSession* s, SrsUdpMuxSocket* u, int parent_cid);
|
2020-03-09 11:46:27 +00:00
|
|
|
virtual ~SrsRtcSenderThread();
|
2020-03-30 07:16:29 +00:00
|
|
|
public:
|
|
|
|
srs_error_t initialize(const uint32_t& vssrc, const uint32_t& assrc, const uint16_t& v_pt, const uint16_t& a_pt);
|
2020-04-13 08:50:24 +00:00
|
|
|
// interface ISrsReloadHandler
|
|
|
|
public:
|
|
|
|
virtual srs_error_t on_reload_rtc_server();
|
2020-04-18 02:04:45 +00:00
|
|
|
virtual srs_error_t on_reload_vhost_play(std::string vhost);
|
|
|
|
virtual srs_error_t on_reload_vhost_realtime(std::string vhost);
|
2020-03-09 11:46:27 +00:00
|
|
|
public:
|
|
|
|
virtual int cid();
|
|
|
|
public:
|
|
|
|
virtual srs_error_t start();
|
|
|
|
virtual void stop();
|
|
|
|
virtual void stop_loop();
|
2020-03-22 08:54:31 +00:00
|
|
|
public:
|
2020-04-10 11:21:47 +00:00
|
|
|
void update_sendonly_socket(SrsUdpMuxSocket* skt);
|
2020-04-13 05:58:34 +00:00
|
|
|
public:
|
|
|
|
virtual srs_error_t cycle();
|
2020-03-12 16:24:56 +00:00
|
|
|
private:
|
2020-04-16 11:33:10 +00:00
|
|
|
srs_error_t send_messages(SrsSource* source, SrsSharedPtrMessage** msgs, int nb_msgs, SrsRtcPackets& packets);
|
2020-04-13 08:50:24 +00:00
|
|
|
srs_error_t messages_to_packets(SrsSource* source, SrsSharedPtrMessage** msgs, int nb_msgs, SrsRtcPackets& packets);
|
2020-04-16 11:33:10 +00:00
|
|
|
srs_error_t send_packets(SrsRtcPackets& packets);
|
|
|
|
srs_error_t send_packets_gso(SrsRtcPackets& packets);
|
2020-04-11 09:52:14 +00:00
|
|
|
private:
|
2020-04-17 10:04:52 +00:00
|
|
|
srs_error_t packet_opus(SrsSample* sample, SrsRtcPackets& packets, int nn_max_payload);
|
2020-04-11 13:03:37 +00:00
|
|
|
private:
|
2020-04-13 08:50:24 +00:00
|
|
|
srs_error_t packet_fu_a(SrsSharedPtrMessage* msg, SrsSample* sample, int fu_payload_size, SrsRtcPackets& packets);
|
2020-04-13 09:11:46 +00:00
|
|
|
srs_error_t packet_nalus(SrsSharedPtrMessage* msg, SrsRtcPackets& packets);
|
2020-04-15 14:46:06 +00:00
|
|
|
srs_error_t packet_single_nalu(SrsSharedPtrMessage* msg, SrsSample* sample, SrsRtcPackets& packets);
|
|
|
|
srs_error_t packet_stap_a(SrsSource* source, SrsSharedPtrMessage* msg, SrsRtcPackets& packets);
|
2020-03-09 11:46:27 +00:00
|
|
|
};
|
2020-03-07 16:30:31 +00:00
|
|
|
|
2020-04-23 15:14:30 +00:00
|
|
|
class SrsRtcPublisher : virtual public ISrsHourGlass
|
2020-04-23 09:08:21 +00:00
|
|
|
{
|
2020-04-23 15:14:30 +00:00
|
|
|
private:
|
|
|
|
SrsHourGlass* report_timer;
|
|
|
|
public:
|
|
|
|
SrsUdpMuxSocket* sendonly_ukt;
|
2020-04-23 09:08:21 +00:00
|
|
|
private:
|
|
|
|
SrsRtcSession* rtc_session;
|
|
|
|
uint32_t video_ssrc;
|
|
|
|
uint32_t audio_ssrc;
|
|
|
|
private:
|
|
|
|
SrsRtpH264Demuxer* rtp_h264_demuxer;
|
2020-04-24 08:19:08 +00:00
|
|
|
SrsRtpOpusDemuxer* rtp_opus_demuxer;
|
2020-04-23 09:08:21 +00:00
|
|
|
SrsRtpQueue* rtp_video_queue;
|
|
|
|
SrsRtpQueue* rtp_audio_queue;
|
|
|
|
private:
|
2020-04-26 08:12:23 +00:00
|
|
|
SrsRequest* req;
|
2020-04-23 09:08:21 +00:00
|
|
|
SrsSource* source;
|
|
|
|
std::string sps;
|
|
|
|
std::string pps;
|
2020-04-23 15:14:30 +00:00
|
|
|
private:
|
|
|
|
std::map<uint32_t, uint64_t> last_sender_report_sys_time;
|
|
|
|
std::map<uint32_t, SrsNtp> last_sender_report_ntp;
|
2020-04-23 09:08:21 +00:00
|
|
|
public:
|
|
|
|
SrsRtcPublisher(SrsRtcSession* session);
|
|
|
|
virtual ~SrsRtcPublisher();
|
|
|
|
public:
|
2020-04-26 08:12:23 +00:00
|
|
|
srs_error_t initialize(SrsUdpMuxSocket* skt, uint32_t vssrc, uint32_t assrc, SrsRequest* req);
|
2020-04-23 09:08:21 +00:00
|
|
|
srs_error_t on_rtp(SrsUdpMuxSocket* skt, char* buf, int nb_buf);
|
2020-04-23 15:14:30 +00:00
|
|
|
srs_error_t on_rtcp_sender_report(char* buf, int nb_buf, SrsUdpMuxSocket* skt);
|
|
|
|
srs_error_t on_rtcp_xr(char* buf, int nb_buf, SrsUdpMuxSocket* skt);
|
|
|
|
private:
|
|
|
|
void check_send_nacks(SrsRtpQueue* rtp_queue, uint32_t ssrc, SrsUdpMuxSocket* skt);
|
|
|
|
srs_error_t send_rtcp_rr(SrsUdpMuxSocket* skt, uint32_t ssrc, SrsRtpQueue* rtp_queue);
|
|
|
|
srs_error_t send_rtcp_xr_rrtr(SrsUdpMuxSocket* skt, uint32_t ssrc);
|
2020-04-24 08:19:08 +00:00
|
|
|
srs_error_t send_rtcp_fb_pli(SrsUdpMuxSocket* skt, uint32_t ssrc);
|
2020-04-23 09:08:21 +00:00
|
|
|
private:
|
2020-04-30 01:33:21 +00:00
|
|
|
srs_error_t on_audio(SrsUdpMuxSocket* skt, SrsRtpSharedPacket* pkt);
|
2020-04-27 05:45:50 +00:00
|
|
|
srs_error_t collect_audio_frame();
|
|
|
|
private:
|
2020-04-30 01:33:21 +00:00
|
|
|
srs_error_t on_video(SrsUdpMuxSocket* skt, SrsRtpSharedPacket* pkt);
|
2020-04-23 15:14:30 +00:00
|
|
|
private:
|
|
|
|
srs_error_t collect_video_frame();
|
|
|
|
public:
|
|
|
|
void update_sendonly_socket(SrsUdpMuxSocket* skt);
|
2020-04-30 00:24:15 +00:00
|
|
|
void request_keyframe();
|
2020-04-23 15:14:30 +00:00
|
|
|
// interface ISrsHourGlass
|
|
|
|
public:
|
|
|
|
virtual srs_error_t notify(int type, srs_utime_t interval, srs_utime_t tick);
|
2020-04-23 09:08:21 +00:00
|
|
|
};
|
|
|
|
|
2020-03-06 15:01:48 +00:00
|
|
|
class SrsRtcSession
|
|
|
|
{
|
2020-04-27 01:35:50 +00:00
|
|
|
friend class SrsDtlsSession;
|
2020-03-09 11:46:27 +00:00
|
|
|
friend class SrsRtcSenderThread;
|
2020-04-23 09:08:21 +00:00
|
|
|
friend class SrsRtcPublisher;
|
2020-02-28 15:18:39 +00:00
|
|
|
private:
|
2020-03-07 16:30:31 +00:00
|
|
|
SrsRtcServer* rtc_server;
|
2020-03-06 15:01:48 +00:00
|
|
|
SrsSdp remote_sdp;
|
|
|
|
SrsSdp local_sdp;
|
2020-02-28 15:18:39 +00:00
|
|
|
SrsRtcSessionStateType session_state;
|
2020-03-06 15:01:48 +00:00
|
|
|
SrsDtlsSession* dtls_session;
|
2020-04-25 14:30:55 +00:00
|
|
|
SrsRtcSenderThread* sender;
|
2020-03-12 16:24:56 +00:00
|
|
|
std::string username;
|
|
|
|
std::string peer_id;
|
|
|
|
srs_utime_t last_stun_time;
|
2020-03-17 04:33:08 +00:00
|
|
|
private:
|
|
|
|
// For each RTC session, we use a specified cid for debugging logs.
|
|
|
|
int cid;
|
2020-04-07 08:05:31 +00:00
|
|
|
// For each RTC session, whether requires encrypt.
|
|
|
|
// Read config value, rtc_server.encrypt, default to on.
|
|
|
|
// Sepcifies by HTTP API, query encrypt, optional.
|
|
|
|
// TODO: FIXME: Support reload.
|
|
|
|
bool encrypt;
|
2020-04-08 05:30:28 +00:00
|
|
|
// The timeout of session, keep alive by STUN ping pong.
|
|
|
|
srs_utime_t sessionStunTimeout;
|
2020-04-27 01:35:50 +00:00
|
|
|
private:
|
|
|
|
bool blackhole;
|
|
|
|
sockaddr_in* blackhole_addr;
|
|
|
|
srs_netfd_t blackhole_stfd;
|
2020-02-28 15:18:39 +00:00
|
|
|
public:
|
2020-04-26 08:12:23 +00:00
|
|
|
SrsRequest* req;
|
2020-03-14 14:11:01 +00:00
|
|
|
SrsSource* source;
|
2020-04-23 09:08:21 +00:00
|
|
|
private:
|
2020-04-25 14:30:55 +00:00
|
|
|
SrsRtcPublisher* publisher;
|
2020-03-09 11:46:27 +00:00
|
|
|
public:
|
2020-04-26 08:12:23 +00:00
|
|
|
SrsRtcSession(SrsRtcServer* s, SrsRequest* r, const std::string& un, int context_id);
|
2020-02-28 15:18:39 +00:00
|
|
|
virtual ~SrsRtcSession();
|
2020-03-07 16:30:31 +00:00
|
|
|
public:
|
2020-03-06 15:01:48 +00:00
|
|
|
SrsSdp* get_local_sdp() { return &local_sdp; }
|
2020-03-30 07:16:29 +00:00
|
|
|
void set_local_sdp(const SrsSdp& sdp);
|
2020-03-12 16:24:56 +00:00
|
|
|
|
|
|
|
SrsSdp* get_remote_sdp() { return &remote_sdp; }
|
2020-03-06 15:01:48 +00:00
|
|
|
void set_remote_sdp(const SrsSdp& sdp) { remote_sdp = sdp; }
|
2020-03-12 16:24:56 +00:00
|
|
|
|
|
|
|
SrsRtcSessionStateType get_session_state() { return session_state; }
|
2020-03-06 15:01:48 +00:00
|
|
|
void set_session_state(SrsRtcSessionStateType state) { session_state = state; }
|
2020-03-12 16:24:56 +00:00
|
|
|
|
|
|
|
std::string id() const { return peer_id + "_" + username; }
|
|
|
|
|
|
|
|
std::string get_peer_id() const { return peer_id; }
|
|
|
|
void set_peer_id(const std::string& id) { peer_id = id; }
|
2020-03-17 04:33:08 +00:00
|
|
|
|
2020-04-07 08:05:31 +00:00
|
|
|
void set_encrypt(bool v) { encrypt = v; }
|
|
|
|
|
2020-03-17 04:33:08 +00:00
|
|
|
void switch_to_context();
|
2020-04-30 00:24:15 +00:00
|
|
|
int context_id() { return cid; }
|
2020-03-12 16:24:56 +00:00
|
|
|
public:
|
2020-04-26 11:00:36 +00:00
|
|
|
srs_error_t initialize();
|
2020-04-10 11:21:47 +00:00
|
|
|
srs_error_t on_stun(SrsUdpMuxSocket* skt, SrsStunPacket* stun_req);
|
|
|
|
srs_error_t on_dtls(SrsUdpMuxSocket* skt);
|
|
|
|
srs_error_t on_rtcp(SrsUdpMuxSocket* skt);
|
2020-04-23 09:08:21 +00:00
|
|
|
srs_error_t on_rtp(SrsUdpMuxSocket* skt);
|
2020-03-07 16:30:31 +00:00
|
|
|
public:
|
2020-04-10 11:21:47 +00:00
|
|
|
srs_error_t send_client_hello(SrsUdpMuxSocket* skt);
|
|
|
|
srs_error_t on_connection_established(SrsUdpMuxSocket* skt);
|
|
|
|
srs_error_t start_play(SrsUdpMuxSocket* skt);
|
2020-04-23 16:06:59 +00:00
|
|
|
srs_error_t start_publish(SrsUdpMuxSocket* skt);
|
2020-03-07 16:30:31 +00:00
|
|
|
public:
|
2020-04-08 05:30:28 +00:00
|
|
|
bool is_stun_timeout();
|
2020-03-07 16:30:31 +00:00
|
|
|
private:
|
2020-04-10 11:21:47 +00:00
|
|
|
srs_error_t on_binding_request(SrsUdpMuxSocket* skt, SrsStunPacket* stun_req);
|
2020-03-09 11:46:27 +00:00
|
|
|
private:
|
2020-04-10 11:21:47 +00:00
|
|
|
srs_error_t on_rtcp_feedback(char* buf, int nb_buf, SrsUdpMuxSocket* skt);
|
|
|
|
srs_error_t on_rtcp_ps_feedback(char* buf, int nb_buf, SrsUdpMuxSocket* skt);
|
2020-04-23 09:08:21 +00:00
|
|
|
srs_error_t on_rtcp_xr(char* buf, int nb_buf, SrsUdpMuxSocket* skt);
|
|
|
|
srs_error_t on_rtcp_sender_report(char* buf, int nb_buf, SrsUdpMuxSocket* skt);
|
2020-04-10 11:21:47 +00:00
|
|
|
srs_error_t on_rtcp_receiver_report(char* buf, int nb_buf, SrsUdpMuxSocket* skt);
|
2020-03-12 16:24:56 +00:00
|
|
|
};
|
|
|
|
|
2020-04-13 01:13:12 +00:00
|
|
|
class SrsUdpMuxSender : virtual public ISrsUdpSender, virtual public ISrsCoroutineHandler, virtual public ISrsReloadHandler
|
2020-02-28 15:18:39 +00:00
|
|
|
{
|
2020-03-09 11:46:27 +00:00
|
|
|
private:
|
2020-04-13 01:13:12 +00:00
|
|
|
srs_netfd_t lfd;
|
|
|
|
SrsRtcServer* server;
|
2020-04-05 08:53:08 +00:00
|
|
|
SrsCoroutine* trd;
|
2020-04-13 01:13:12 +00:00
|
|
|
private:
|
2020-04-05 08:53:08 +00:00
|
|
|
srs_cond_t cond;
|
|
|
|
bool waiting_msgs;
|
2020-04-18 02:35:30 +00:00
|
|
|
bool gso;
|
|
|
|
int nn_senders;
|
|
|
|
private:
|
2020-04-09 11:38:50 +00:00
|
|
|
// Hotspot msgs, we are working on it.
|
|
|
|
// @remark We will wait util all messages are ready.
|
|
|
|
std::vector<mmsghdr> hotspot;
|
|
|
|
// Cache msgs, for other coroutines to fill it.
|
|
|
|
std::vector<mmsghdr> cache;
|
|
|
|
int cache_pos;
|
2020-04-10 10:14:33 +00:00
|
|
|
// The max number of messages for sendmmsg. If 1, we use sendmsg to send.
|
|
|
|
int max_sendmmsg;
|
2020-04-17 04:48:05 +00:00
|
|
|
// The total queue length, for each sender.
|
2020-04-17 04:30:53 +00:00
|
|
|
int queue_length;
|
2020-04-18 02:35:30 +00:00
|
|
|
// The extra queue ratio.
|
|
|
|
int extra_ratio;
|
|
|
|
int extra_queue;
|
2020-04-13 01:13:12 +00:00
|
|
|
public:
|
|
|
|
SrsUdpMuxSender(SrsRtcServer* s);
|
|
|
|
virtual ~SrsUdpMuxSender();
|
|
|
|
public:
|
2020-04-17 04:30:53 +00:00
|
|
|
virtual srs_error_t initialize(srs_netfd_t fd, int senders);
|
2020-04-13 01:13:12 +00:00
|
|
|
private:
|
|
|
|
void free_mhdrs(std::vector<mmsghdr>& mhdrs);
|
|
|
|
public:
|
|
|
|
virtual srs_error_t fetch(mmsghdr** pphdr);
|
|
|
|
virtual srs_error_t sendmmsg(mmsghdr* hdr);
|
2020-04-17 04:30:53 +00:00
|
|
|
virtual bool overflow();
|
2020-04-18 02:35:30 +00:00
|
|
|
virtual void set_extra_ratio(int r);
|
|
|
|
public:
|
2020-04-13 01:13:12 +00:00
|
|
|
virtual srs_error_t cycle();
|
|
|
|
// interface ISrsReloadHandler
|
|
|
|
public:
|
|
|
|
virtual srs_error_t on_reload_rtc_server();
|
|
|
|
};
|
|
|
|
|
|
|
|
class SrsRtcServer : virtual public ISrsUdpMuxHandler, virtual public ISrsHourGlass
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
SrsHourGlass* timer;
|
|
|
|
std::vector<SrsUdpMuxListener*> listeners;
|
|
|
|
std::vector<SrsUdpMuxSender*> senders;
|
2020-02-28 15:18:39 +00:00
|
|
|
private:
|
2020-03-07 16:30:31 +00:00
|
|
|
std::map<std::string, SrsRtcSession*> map_username_session; // key: username(local_ufrag + ":" + remote_ufrag)
|
|
|
|
std::map<std::string, SrsRtcSession*> map_id_session; // key: peerip(ip + ":" + port)
|
2020-02-28 15:18:39 +00:00
|
|
|
public:
|
2020-03-17 09:56:37 +00:00
|
|
|
SrsRtcServer();
|
2020-02-28 15:18:39 +00:00
|
|
|
virtual ~SrsRtcServer();
|
|
|
|
public:
|
|
|
|
virtual srs_error_t initialize();
|
2020-03-17 10:11:03 +00:00
|
|
|
public:
|
2020-03-17 09:56:37 +00:00
|
|
|
// TODO: FIXME: Support gracefully quit.
|
|
|
|
// TODO: FIXME: Support reload.
|
2020-03-17 10:11:03 +00:00
|
|
|
virtual srs_error_t listen_udp();
|
2020-04-10 11:21:47 +00:00
|
|
|
virtual srs_error_t on_udp_packet(SrsUdpMuxSocket* skt);
|
2020-03-17 10:11:03 +00:00
|
|
|
public:
|
|
|
|
virtual srs_error_t listen_api();
|
2020-04-26 08:12:23 +00:00
|
|
|
srs_error_t create_rtc_session(
|
|
|
|
SrsRequest* req, const SrsSdp& remote_sdp, SrsSdp& local_sdp, const std::string& mock_eip, bool publish,
|
|
|
|
SrsRtcSession** psession
|
|
|
|
);
|
2020-03-07 16:30:31 +00:00
|
|
|
bool insert_into_id_sessions(const std::string& peer_id, SrsRtcSession* rtc_session);
|
2020-03-12 16:24:56 +00:00
|
|
|
void check_and_clean_timeout_session();
|
2020-04-13 01:13:12 +00:00
|
|
|
int nn_sessions() { return (int)map_username_session.size(); }
|
2020-02-28 15:18:39 +00:00
|
|
|
private:
|
2020-04-10 11:21:47 +00:00
|
|
|
srs_error_t on_stun(SrsUdpMuxSocket* skt);
|
|
|
|
srs_error_t on_dtls(SrsUdpMuxSocket* skt);
|
|
|
|
srs_error_t on_rtp_or_rtcp(SrsUdpMuxSocket* skt);
|
2020-02-28 15:18:39 +00:00
|
|
|
private:
|
2020-03-07 16:30:31 +00:00
|
|
|
SrsRtcSession* find_rtc_session_by_username(const std::string& ufrag);
|
|
|
|
SrsRtcSession* find_rtc_session_by_peer_id(const std::string& peer_id);
|
2020-03-19 04:58:04 +00:00
|
|
|
// interface ISrsHourGlass
|
|
|
|
public:
|
|
|
|
virtual srs_error_t notify(int type, srs_utime_t interval, srs_utime_t tick);
|
2020-02-28 15:18:39 +00:00
|
|
|
};
|
|
|
|
|
2020-03-17 09:56:37 +00:00
|
|
|
// The RTC server adapter.
|
|
|
|
class RtcServerAdapter : public ISrsHybridServer
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
SrsRtcServer* rtc;
|
|
|
|
public:
|
|
|
|
RtcServerAdapter();
|
|
|
|
virtual ~RtcServerAdapter();
|
|
|
|
public:
|
|
|
|
virtual srs_error_t initialize();
|
|
|
|
virtual srs_error_t run();
|
|
|
|
virtual void stop();
|
|
|
|
};
|
|
|
|
|
2020-02-28 15:18:39 +00:00
|
|
|
#endif
|
|
|
|
|