2020-02-28 15:18:39 +00:00
|
|
|
/**
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2013-2020 Winlin
|
|
|
|
*
|
|
|
|
* 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-02-28 15:18:39 +00:00
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <map>
|
2020-03-02 14:47:40 +00:00
|
|
|
#include <vector>
|
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-07 16:30:31 +00:00
|
|
|
class SrsUdpRemuxSocket;
|
2020-02-28 15:18:39 +00:00
|
|
|
class SrsServer;
|
|
|
|
class SrsStunPacket;
|
|
|
|
|
2020-03-07 07:28:15 +00:00
|
|
|
class SrsCandidate
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
public:
|
|
|
|
SrsCandidate();
|
|
|
|
virtual ~SrsCandidate();
|
|
|
|
|
|
|
|
static std::vector<std::string> get_candidate_ips();
|
|
|
|
};
|
|
|
|
|
2020-03-02 14:47:40 +00:00
|
|
|
class SrsSdpMediaInfo
|
2020-02-28 15:18:39 +00:00
|
|
|
{
|
|
|
|
private:
|
|
|
|
public:
|
2020-03-02 14:47:40 +00:00
|
|
|
SrsSdpMediaInfo();
|
|
|
|
virtual ~SrsSdpMediaInfo();
|
|
|
|
};
|
|
|
|
|
|
|
|
class SrsSdp
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
std::string sdp;
|
|
|
|
int version;
|
|
|
|
std::string ice_ufrag;
|
|
|
|
std::string ice_pwd;
|
|
|
|
std::string fingerprint;
|
|
|
|
std::string setup;
|
|
|
|
std::vector<SrsSdpMediaInfo> media_infos;
|
|
|
|
public:
|
|
|
|
SrsSdp();
|
|
|
|
virtual ~SrsSdp();
|
|
|
|
|
2020-03-06 15:01:48 +00:00
|
|
|
srs_error_t decode(const std::string& sdp_str);
|
|
|
|
srs_error_t encode(std::string& sdp_str);
|
|
|
|
|
|
|
|
std::string get_ice_ufrag() const { return ice_ufrag; }
|
|
|
|
std::string get_ice_pwd() const { return ice_pwd; }
|
|
|
|
|
|
|
|
void set_ice_ufrag(const std::string& u) { ice_ufrag = u; }
|
|
|
|
void set_ice_pwd(const std::string& p) { ice_pwd = p; }
|
2020-03-02 14:47:40 +00:00
|
|
|
private:
|
|
|
|
srs_error_t parse_attr(const std::string& line);
|
2020-02-28 15:18:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
enum SrsRtcSessionStateType
|
|
|
|
{
|
|
|
|
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:
|
|
|
|
SSL* dtls;
|
|
|
|
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-07 16:30:31 +00:00
|
|
|
SrsDtlsSession();
|
2020-03-06 15:01:48 +00:00
|
|
|
virtual ~SrsDtlsSession();
|
|
|
|
|
2020-03-07 16:30:31 +00:00
|
|
|
srs_error_t on_dtls(SrsUdpRemuxSocket* udp_remux_socket);
|
|
|
|
srs_error_t on_dtls_handshake_done();
|
2020-03-06 15:01:48 +00:00
|
|
|
srs_error_t on_dtls_application_data(const char* data, const int len);
|
|
|
|
|
2020-03-07 16:30:31 +00:00
|
|
|
void send_client_hello(SrsUdpRemuxSocket* udp_remux_socket);
|
|
|
|
srs_error_t handshake(SrsUdpRemuxSocket* udp_remux_socket);
|
|
|
|
|
|
|
|
private:
|
2020-03-06 15:01:48 +00:00
|
|
|
srs_error_t srtp_init();
|
|
|
|
srs_error_t srtp_sender_side_init();
|
|
|
|
srs_error_t srtp_receiver_side_init();
|
|
|
|
};
|
|
|
|
|
2020-03-07 16:30:31 +00:00
|
|
|
class SrsRtcServer;
|
|
|
|
|
2020-03-06 15:01:48 +00:00
|
|
|
class SrsRtcSession
|
|
|
|
{
|
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-02-28 15:18:39 +00:00
|
|
|
public:
|
2020-03-07 16:30:31 +00:00
|
|
|
SrsRtcSession(SrsRtcServer* svr);
|
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; }
|
|
|
|
SrsSdp* get_remote_sdp() { return &remote_sdp; }
|
|
|
|
SrsRtcSessionStateType get_session_state() { return session_state; }
|
|
|
|
|
|
|
|
void set_local_sdp(const SrsSdp& sdp) { local_sdp = sdp; }
|
|
|
|
void set_remote_sdp(const SrsSdp& sdp) { remote_sdp = sdp; }
|
|
|
|
void set_session_state(SrsRtcSessionStateType state) { session_state = state; }
|
2020-03-07 16:30:31 +00:00
|
|
|
public:
|
|
|
|
srs_error_t on_stun(SrsUdpRemuxSocket* udp_remux_socket, SrsStunPacket* stun_req);
|
|
|
|
srs_error_t on_dtls(SrsUdpRemuxSocket* udp_remux_socket);
|
|
|
|
public:
|
|
|
|
srs_error_t send_client_hello(SrsUdpRemuxSocket* udp_remux_socket);
|
|
|
|
private:
|
|
|
|
srs_error_t on_binding_request(SrsUdpRemuxSocket* udp_remux_socket, SrsStunPacket* stun_req);
|
2020-02-28 15:18:39 +00:00
|
|
|
};
|
|
|
|
|
2020-03-07 16:30:31 +00:00
|
|
|
class SrsRtcServer : public ISrsUdpRemuxHandler
|
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-07 16:30:31 +00:00
|
|
|
SrsRtcServer();
|
2020-02-28 15:18:39 +00:00
|
|
|
virtual ~SrsRtcServer();
|
|
|
|
public:
|
|
|
|
virtual srs_error_t initialize();
|
2020-03-07 07:28:15 +00:00
|
|
|
|
2020-03-07 16:30:31 +00:00
|
|
|
virtual srs_error_t on_udp_packet(SrsUdpRemuxSocket* udp_remux_socket);
|
2020-03-06 15:01:48 +00:00
|
|
|
|
|
|
|
SrsRtcSession* create_rtc_session(const SrsSdp& remote_sdp, SrsSdp& local_sdp);
|
2020-03-07 16:30:31 +00:00
|
|
|
bool insert_into_id_sessions(const std::string& peer_id, SrsRtcSession* rtc_session);
|
2020-02-28 15:18:39 +00:00
|
|
|
private:
|
2020-03-07 16:30:31 +00:00
|
|
|
srs_error_t on_stun(SrsUdpRemuxSocket* udp_remux_socket);
|
|
|
|
srs_error_t on_dtls(SrsUdpRemuxSocket* udp_remux_socket);
|
|
|
|
srs_error_t on_rtp_or_rtcp(SrsUdpRemuxSocket* udp_remux_socket);
|
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-02-28 15:18:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|