1
0
Fork 0
mirror of https://github.com/ossrs/srs.git synced 2025-02-24 06:54:22 +00:00
srs/trunk/src/app/srs_app_rtc_conn.hpp

225 lines
6.6 KiB
C++
Raw Normal View History

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 SrsConsumer;
2020-02-28 15:18:39 +00:00
class SrsStunPacket;
class SrsRtcServer;
class SrsRtcSession;
2020-02-28 15:18:39 +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:
SrsRtcSession* rtc_session;
2020-03-06 15:01:48 +00:00
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:
SrsDtlsSession(SrsRtcSession* s);
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(SrsUdpRemuxSocket* udp_remux_socket);
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);
srs_error_t srtp_sender_protect(char* protected_buf, const char* ori_buf, int& nb_protected_buf);
2020-03-10 11:47:49 +00:00
srs_error_t srtp_receiver_unprotect(char* unprotected_buf, const char* ori_buf, int& nb_unprotected_buf);
2020-03-07 16:30:31 +00:00
private:
srs_error_t srtp_initialize();
2020-03-06 15:01:48 +00:00
srs_error_t srtp_sender_side_init();
srs_error_t srtp_receiver_side_init();
};
class SrsRtcSenderThread : public ISrsCoroutineHandler
{
protected:
SrsCoroutine* trd;
int _parent_cid;
private:
SrsRtcSession* rtc_session;
SrsUdpRemuxSocket ukt;
public:
// Constructor.
// @param tm The receive timeout in srs_utime_t.
SrsRtcSenderThread(SrsRtcSession* s, SrsUdpRemuxSocket* u, int parent_cid);
virtual ~SrsRtcSenderThread();
public:
virtual int cid();
public:
virtual srs_error_t start();
virtual void stop();
virtual void stop_loop();
public:
virtual srs_error_t cycle();
};
2020-03-07 16:30:31 +00:00
2020-03-06 15:01:48 +00:00
class SrsRtcSession
{
friend class SrsRtcSenderThread;
2020-02-28 15:18:39 +00:00
private:
SrsServer* server;
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;
SrsRtcSenderThread* strd;
2020-02-28 15:18:39 +00:00
public:
std::string app;
std::string stream;
public:
SrsRtcSession(SrsServer* svr, SrsRtcServer* rtc_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; }
void set_app_stream(const std::string& a, const std::string& s) { app = a; stream = s; }
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);
2020-03-10 11:47:49 +00:00
srs_error_t on_rtp_or_rtcp(SrsUdpRemuxSocket* udp_remux_socket);
2020-03-07 16:30:31 +00:00
public:
srs_error_t send_client_hello(SrsUdpRemuxSocket* udp_remux_socket);
void on_connection_established(SrsUdpRemuxSocket* udp_remux_socket);
srs_error_t start_play(SrsUdpRemuxSocket* udp_remux_socket);
2020-03-07 16:30:31 +00:00
private:
srs_error_t on_binding_request(SrsUdpRemuxSocket* udp_remux_socket, SrsStunPacket* stun_req);
private:
srs_error_t do_playing(SrsConsumer* consumer, SrsUdpRemuxSocket* udp_remux_socket);
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:
SrsServer* server;
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:
SrsRtcServer(SrsServer* svr);
2020-02-28 15:18:39 +00:00
virtual ~SrsRtcServer();
public:
virtual srs_error_t initialize();
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