mirror of
https://github.com/ossrs/srs.git
synced 2025-03-09 15:49:59 +00:00
Refactor code to split implementation to cpp
This commit is contained in:
parent
6f2b78f16a
commit
b3a0284db5
14 changed files with 363 additions and 110 deletions
|
@ -46,14 +46,6 @@ SrsDtls::~SrsDtls()
|
|||
SSL_CTX_free(dtls_ctx);
|
||||
}
|
||||
|
||||
SrsDtls* SrsDtls::instance()
|
||||
{
|
||||
if (!_instance) {
|
||||
_instance = new SrsDtls();
|
||||
}
|
||||
return _instance;
|
||||
}
|
||||
|
||||
// The return value of verify_callback controls the strategy of the further verification process. If verify_callback
|
||||
// returns 0, the verification process is immediately stopped with "verification failed" state. If SSL_VERIFY_PEER is
|
||||
// set, a verification failure alert is sent to the peer and the TLS/SSL handshake is terminated. If verify_callback
|
||||
|
@ -256,3 +248,22 @@ srs_error_t SrsDtls::init(SrsRequest* r)
|
|||
|
||||
return err;
|
||||
}
|
||||
|
||||
SrsDtls* SrsDtls::instance()
|
||||
{
|
||||
if (!_instance) {
|
||||
_instance = new SrsDtls();
|
||||
}
|
||||
return _instance;
|
||||
}
|
||||
|
||||
SSL_CTX* SrsDtls::get_dtls_ctx()
|
||||
{
|
||||
return dtls_ctx;
|
||||
}
|
||||
|
||||
std::string SrsDtls::get_fingerprint() const
|
||||
{
|
||||
return fingerprint;
|
||||
}
|
||||
|
||||
|
|
|
@ -46,9 +46,9 @@ public:
|
|||
srs_error_t init(SrsRequest* r);
|
||||
public:
|
||||
static SrsDtls* instance();
|
||||
SSL_CTX* get_dtls_ctx() { return dtls_ctx; }
|
||||
SSL_CTX* get_dtls_ctx();
|
||||
public:
|
||||
std::string get_fingerprint() const { return fingerprint; }
|
||||
std::string get_fingerprint() const;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
|
@ -261,23 +261,6 @@ SrsUdpMuxSocket::~SrsUdpMuxSocket()
|
|||
srs_freepa(buf);
|
||||
}
|
||||
|
||||
SrsUdpMuxSocket* SrsUdpMuxSocket::copy_sendonly()
|
||||
{
|
||||
SrsUdpMuxSocket* sendonly = new SrsUdpMuxSocket(handler, lfd);
|
||||
|
||||
// Don't copy buffer
|
||||
srs_freepa(sendonly->buf);
|
||||
sendonly->nb_buf = 0;
|
||||
sendonly->nread = 0;
|
||||
sendonly->lfd = lfd;
|
||||
sendonly->from = from;
|
||||
sendonly->fromlen = fromlen;
|
||||
sendonly->peer_ip = peer_ip;
|
||||
sendonly->peer_port = peer_port;
|
||||
|
||||
return sendonly;
|
||||
}
|
||||
|
||||
int SrsUdpMuxSocket::recvfrom(srs_utime_t timeout)
|
||||
{
|
||||
fromlen = sizeof(from);
|
||||
|
@ -341,6 +324,26 @@ socklen_t SrsUdpMuxSocket::peer_addrlen()
|
|||
return (socklen_t)fromlen;
|
||||
}
|
||||
|
||||
char* SrsUdpMuxSocket::data()
|
||||
{
|
||||
return buf;
|
||||
}
|
||||
|
||||
int SrsUdpMuxSocket::size()
|
||||
{
|
||||
return nread;
|
||||
}
|
||||
|
||||
std::string SrsUdpMuxSocket::get_peer_ip() const
|
||||
{
|
||||
return peer_ip;
|
||||
}
|
||||
|
||||
int SrsUdpMuxSocket::get_peer_port() const
|
||||
{
|
||||
return peer_port;
|
||||
}
|
||||
|
||||
std::string SrsUdpMuxSocket::get_peer_id()
|
||||
{
|
||||
char id_buf[1024];
|
||||
|
@ -349,6 +352,28 @@ std::string SrsUdpMuxSocket::get_peer_id()
|
|||
return string(id_buf, len);
|
||||
}
|
||||
|
||||
SrsUdpMuxSocket* SrsUdpMuxSocket::copy_sendonly()
|
||||
{
|
||||
SrsUdpMuxSocket* sendonly = new SrsUdpMuxSocket(handler, lfd);
|
||||
|
||||
// Don't copy buffer
|
||||
srs_freepa(sendonly->buf);
|
||||
sendonly->nb_buf = 0;
|
||||
sendonly->nread = 0;
|
||||
sendonly->lfd = lfd;
|
||||
sendonly->from = from;
|
||||
sendonly->fromlen = fromlen;
|
||||
sendonly->peer_ip = peer_ip;
|
||||
sendonly->peer_port = peer_port;
|
||||
|
||||
return sendonly;
|
||||
}
|
||||
|
||||
ISrsUdpSender* SrsUdpMuxSocket::sender()
|
||||
{
|
||||
return handler;
|
||||
}
|
||||
|
||||
SrsUdpMuxListener::SrsUdpMuxListener(ISrsUdpMuxHandler* h, ISrsUdpSender* s, std::string i, int p)
|
||||
{
|
||||
handler = h;
|
||||
|
|
|
@ -163,26 +163,19 @@ private:
|
|||
public:
|
||||
SrsUdpMuxSocket(ISrsUdpSender* h, srs_netfd_t fd);
|
||||
virtual ~SrsUdpMuxSocket();
|
||||
|
||||
public:
|
||||
int recvfrom(srs_utime_t timeout);
|
||||
srs_error_t sendto(void* data, int size, srs_utime_t timeout);
|
||||
|
||||
srs_netfd_t stfd();
|
||||
sockaddr_in* peer_addr();
|
||||
socklen_t peer_addrlen();
|
||||
|
||||
char* data() { return buf; }
|
||||
int size() { return nread; }
|
||||
std::string get_peer_ip() const { return peer_ip; }
|
||||
int get_peer_port() const { return peer_port; }
|
||||
char* data();
|
||||
int size();
|
||||
std::string get_peer_ip() const;
|
||||
int get_peer_port() const;
|
||||
std::string get_peer_id();
|
||||
public:
|
||||
SrsUdpMuxSocket* copy_sendonly();
|
||||
ISrsUdpSender* sender() { return handler; };
|
||||
private:
|
||||
// Don't allow copy, user copy_sendonly instead
|
||||
SrsUdpMuxSocket(const SrsUdpMuxSocket& rhs);
|
||||
SrsUdpMuxSocket& operator=(const SrsUdpMuxSocket& rhs);
|
||||
ISrsUdpSender* sender();
|
||||
};
|
||||
|
||||
class SrsUdpMuxListener : public ISrsCoroutineHandler
|
||||
|
|
|
@ -3396,26 +3396,6 @@ srs_error_t SrsRtcServer::create_rtc_session(
|
|||
return err;
|
||||
}
|
||||
|
||||
SrsRtcSession* SrsRtcServer::find_rtc_session_by_peer_id(const string& peer_id)
|
||||
{
|
||||
map<string, SrsRtcSession*>::iterator iter = map_id_session.find(peer_id);
|
||||
if (iter == map_id_session.end()) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return iter->second;
|
||||
}
|
||||
|
||||
SrsRtcSession* SrsRtcServer::find_rtc_session_by_username(const std::string& username)
|
||||
{
|
||||
map<string, SrsRtcSession*>::iterator iter = map_username_session.find(username);
|
||||
if (iter == map_username_session.end()) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return iter->second;
|
||||
}
|
||||
|
||||
bool SrsRtcServer::insert_into_id_sessions(const string& peer_id, SrsRtcSession* rtc_session)
|
||||
{
|
||||
return map_id_session.insert(make_pair(peer_id, rtc_session)).second;
|
||||
|
@ -3447,6 +3427,31 @@ void SrsRtcServer::check_and_clean_timeout_session()
|
|||
}
|
||||
}
|
||||
|
||||
int SrsRtcServer::nn_sessions()
|
||||
{
|
||||
return (int)map_username_session.size();
|
||||
}
|
||||
|
||||
SrsRtcSession* SrsRtcServer::find_rtc_session_by_peer_id(const string& peer_id)
|
||||
{
|
||||
map<string, SrsRtcSession*>::iterator iter = map_id_session.find(peer_id);
|
||||
if (iter == map_id_session.end()) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return iter->second;
|
||||
}
|
||||
|
||||
SrsRtcSession* SrsRtcServer::find_rtc_session_by_username(const std::string& username)
|
||||
{
|
||||
map<string, SrsRtcSession*>::iterator iter = map_username_session.find(username);
|
||||
if (iter == map_username_session.end()) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return iter->second;
|
||||
}
|
||||
|
||||
srs_error_t SrsRtcServer::notify(int type, srs_utime_t interval, srs_utime_t tick)
|
||||
{
|
||||
check_and_clean_timeout_session();
|
||||
|
|
|
@ -444,7 +444,7 @@ public:
|
|||
);
|
||||
bool insert_into_id_sessions(const std::string& peer_id, SrsRtcSession* rtc_session);
|
||||
void check_and_clean_timeout_session();
|
||||
int nn_sessions() { return (int)map_username_session.size(); }
|
||||
int nn_sessions();
|
||||
private:
|
||||
SrsRtcSession* find_rtc_session_by_username(const std::string& ufrag);
|
||||
SrsRtcSession* find_rtc_session_by_peer_id(const std::string& peer_id);
|
||||
|
|
|
@ -150,6 +150,21 @@ SrsRtpRingBuffer::~SrsRtpRingBuffer()
|
|||
srs_freepa(queue_);
|
||||
}
|
||||
|
||||
uint16_t SrsRtpRingBuffer::low()
|
||||
{
|
||||
return low_;
|
||||
}
|
||||
|
||||
uint16_t SrsRtpRingBuffer::high()
|
||||
{
|
||||
return high_;
|
||||
}
|
||||
|
||||
void SrsRtpRingBuffer::advance_to(uint16_t seq)
|
||||
{
|
||||
low_ = seq;
|
||||
}
|
||||
|
||||
void SrsRtpRingBuffer::set(uint16_t at, SrsRtpPacket2* pkt)
|
||||
{
|
||||
SrsRtpPacket2* p = queue_[at % capacity_];
|
||||
|
@ -161,6 +176,11 @@ void SrsRtpRingBuffer::set(uint16_t at, SrsRtpPacket2* pkt)
|
|||
queue_[at % capacity_] = pkt;
|
||||
}
|
||||
|
||||
void SrsRtpRingBuffer::remove(uint16_t at)
|
||||
{
|
||||
set(at, NULL);
|
||||
}
|
||||
|
||||
void SrsRtpRingBuffer::reset(uint16_t low, uint16_t high)
|
||||
{
|
||||
for (uint16_t s = low; s != high; ++s) {
|
||||
|
@ -168,6 +188,16 @@ void SrsRtpRingBuffer::reset(uint16_t low, uint16_t high)
|
|||
}
|
||||
}
|
||||
|
||||
bool SrsRtpRingBuffer::overflow()
|
||||
{
|
||||
return high_ - low_ < capacity_;
|
||||
}
|
||||
|
||||
bool SrsRtpRingBuffer::is_heavy()
|
||||
{
|
||||
return high_ - low_ >= capacity_ / 2;
|
||||
}
|
||||
|
||||
uint16_t SrsRtpRingBuffer::next_start_of_frame()
|
||||
{
|
||||
if (low_ == high_) {
|
||||
|
@ -245,6 +275,11 @@ void SrsRtpRingBuffer::update(uint16_t seq, bool startup, uint16_t& nack_low, ui
|
|||
}
|
||||
}
|
||||
|
||||
SrsRtpPacket2* SrsRtpRingBuffer::at(uint16_t seq)
|
||||
{
|
||||
return queue_[seq % capacity_];
|
||||
}
|
||||
|
||||
SrsRtpQueue::SrsRtpQueue(size_t capacity, bool one_packet_per_frame)
|
||||
{
|
||||
nn_collected_frames = 0;
|
||||
|
@ -396,6 +431,11 @@ void SrsRtpQueue::notify_nack_list_full()
|
|||
queue_->advance_to(next + 1);
|
||||
}
|
||||
|
||||
void SrsRtpQueue::request_keyframe()
|
||||
{
|
||||
request_key_frame_ = true;
|
||||
}
|
||||
|
||||
uint32_t SrsRtpQueue::get_extended_highest_sequence()
|
||||
{
|
||||
return queue_->get_extended_highest_sequence();
|
||||
|
@ -450,7 +490,9 @@ void SrsRtpQueue::collect_packet()
|
|||
{
|
||||
while (queue_->low() != queue_->high()) {
|
||||
vector<SrsRtpPacket2*> frame;
|
||||
for (uint16_t s = queue_->low(); s != queue_->high(); ++s) {
|
||||
|
||||
uint16_t s = queue_->low();
|
||||
for (; s != queue_->high(); ++s) {
|
||||
SrsRtpPacket2* pkt = queue_->at(s);
|
||||
|
||||
// In NACK, never collect frame.
|
||||
|
@ -468,14 +510,17 @@ void SrsRtpQueue::collect_packet()
|
|||
frame.push_back(pkt);
|
||||
|
||||
// Not the last packet, continue to process next one.
|
||||
if (pkt->rtp_header.get_marker() || one_packet_per_frame_) {
|
||||
if (!pkt->rtp_header.get_marker() && !one_packet_per_frame_) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Done, we got the last packet of frame.
|
||||
nn_collected_frames++;
|
||||
frames_.push_back(frame);
|
||||
break;
|
||||
}
|
||||
|
||||
if (queue_->low() != s) {
|
||||
// Reset the range of packets to NULL in buffer.
|
||||
queue_->reset(queue_->low(), s);
|
||||
|
||||
|
|
|
@ -133,17 +133,17 @@ public:
|
|||
virtual ~SrsRtpRingBuffer();
|
||||
public:
|
||||
// Move the position of buffer.
|
||||
uint16_t low() { return low_; }
|
||||
uint16_t high() { return high_; }
|
||||
void advance_to(uint16_t seq) { low_ = seq; }
|
||||
uint16_t low();
|
||||
uint16_t high();
|
||||
void advance_to(uint16_t seq);
|
||||
// Free the packet at position.
|
||||
void set(uint16_t at, SrsRtpPacket2* pkt);
|
||||
void remove(uint16_t at) { set(at, NULL); }
|
||||
void remove(uint16_t at);
|
||||
// Directly reset range [low, high] to NULL.
|
||||
void reset(uint16_t low, uint16_t high);
|
||||
// Whether queue overflow or heavy(too many packets and need clear).
|
||||
bool overflow() { return high_ - low_ < capacity_; }
|
||||
bool is_heavy() { return high_ - low_ >= capacity_ / 2; }
|
||||
bool overflow();
|
||||
bool is_heavy();
|
||||
// Get the next start packet of frame.
|
||||
// @remark If not found, return the low_, which should never be the "next" one,
|
||||
// because it MAY or NOT current start packet of frame but never be the next.
|
||||
|
@ -156,7 +156,7 @@ public:
|
|||
// Update the sequence, got the nack range by [low, high].
|
||||
void update(uint16_t seq, bool startup, uint16_t& nack_low, uint16_t& nack_high);
|
||||
// Get the packet by seq.
|
||||
SrsRtpPacket2* at(uint16_t seq) { return queue_[seq % capacity_]; }
|
||||
SrsRtpPacket2* at(uint16_t seq);
|
||||
};
|
||||
|
||||
class SrsRtpQueue
|
||||
|
@ -186,7 +186,7 @@ public:
|
|||
bool should_request_key_frame();
|
||||
void notify_drop_seq(uint16_t seq);
|
||||
void notify_nack_list_full();
|
||||
void request_keyframe() { request_key_frame_ = true; }
|
||||
void request_keyframe();
|
||||
public:
|
||||
uint32_t get_extended_highest_sequence();
|
||||
uint8_t get_fraction_lost();
|
||||
|
|
|
@ -457,6 +457,11 @@ SrsConsumer::~SrsConsumer()
|
|||
#endif
|
||||
}
|
||||
|
||||
void SrsConsumer::enable_pass_timestamp()
|
||||
{
|
||||
pass_timestamp = true;
|
||||
}
|
||||
|
||||
void SrsConsumer::set_queue_size(srs_utime_t queue_size)
|
||||
{
|
||||
queue->set_queue_size(queue_size);
|
||||
|
@ -2764,6 +2769,11 @@ void SrsSource::request_keyframe()
|
|||
}
|
||||
}
|
||||
|
||||
void SrsSource::set_rtc_publisher(SrsRtcPublisher* v)
|
||||
{
|
||||
rtc_publisher = v;
|
||||
}
|
||||
|
||||
srs_error_t SrsSource::on_rtc_audio(SrsSharedPtrMessage* audio)
|
||||
{
|
||||
// TODO: FIXME: Merge with on_audio.
|
||||
|
|
|
@ -217,7 +217,7 @@ public:
|
|||
virtual ~SrsConsumer();
|
||||
public:
|
||||
// Use pass timestamp mode.
|
||||
void enable_pass_timestamp() { pass_timestamp = true; }
|
||||
void enable_pass_timestamp();
|
||||
// Set the size of queue.
|
||||
virtual void set_queue_size(srs_utime_t queue_size);
|
||||
// when source id changed, notice client to print.
|
||||
|
@ -658,7 +658,7 @@ public:
|
|||
// TODO: FIXME: Maybe we could cache the keyframe.
|
||||
// TODO: FIXME: Maybe we should only response for the new clients.
|
||||
void request_keyframe();
|
||||
void set_rtc_publisher(SrsRtcPublisher* v) { rtc_publisher = v; }
|
||||
void set_rtc_publisher(SrsRtcPublisher* v);
|
||||
// When got RTC audio message, which is encoded in opus.
|
||||
// TODO: FIXME: Merge with on_audio.
|
||||
srs_error_t on_rtc_audio(SrsSharedPtrMessage* audio);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue