Skip to content

Commit

Permalink
for ossrs#133, alloc and free rtp port.
Browse files Browse the repository at this point in the history
  • Loading branch information
winlinvip committed Feb 17, 2015
1 parent d4ceff6 commit f14af45
Show file tree
Hide file tree
Showing 9 changed files with 183 additions and 38 deletions.
10 changes: 6 additions & 4 deletions trunk/src/app/srs_app_listener.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,9 @@ SrsUdpListener::SrsUdpListener(ISrsUdpHandler* h, int p)

SrsUdpListener::~SrsUdpListener()
{
// close the stfd to trigger thread to interrupted.
srs_close_stfd(stfd);

pthread->stop();
srs_freep(pthread);

Expand Down Expand Up @@ -143,8 +144,8 @@ int SrsUdpListener::listen()
int SrsUdpListener::cycle()
{
int ret = ERROR_SUCCESS;

for (;;) {
while (pthread->can_loop()) {
// TODO: FIXME: support ipv6, @see man 7 ipv6
sockaddr_in from;
int nb_from = sizeof(sockaddr_in);
Expand Down Expand Up @@ -181,8 +182,9 @@ SrsTcpListener::SrsTcpListener(ISrsTcpHandler* h, int p)

SrsTcpListener::~SrsTcpListener()
{
// close the stfd to trigger thread to interrupted.
srs_close_stfd(stfd);

pthread->stop();
srs_freep(pthread);

Expand Down
101 changes: 89 additions & 12 deletions trunk/src/app/srs_app_rtsp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,41 @@ using namespace std;

#ifdef SRS_AUTO_STREAM_CASTER

ISrsRtspHandler::ISrsRtspHandler()
SrsRtpConn::SrsRtpConn(SrsRtspConn* r, int p)
{
rtsp = r;
_port = p;
listener = new SrsUdpListener(this, p);
}

ISrsRtspHandler::~ISrsRtspHandler()
SrsRtpConn::~SrsRtpConn()
{
srs_freep(listener);
}

SrsRtspConn::SrsRtspConn(SrsRtspCaster* c, st_netfd_t fd, std::string o, int lpmin, int lpmax)
int SrsRtpConn::port()
{
return _port;
}

int SrsRtpConn::listen()
{
return listener->listen();
}

int SrsRtpConn::on_udp_packet(sockaddr_in* from, char* buf, int nb_buf)
{
int ret = ERROR_SUCCESS;
return ret;
}

SrsRtspConn::SrsRtspConn(SrsRtspCaster* c, st_netfd_t fd, std::string o)
{
output = o;
local_port_min = lpmin;
local_port_max = lpmax;

session = "O9EaZ4bf"; // TODO: FIXME: generate session id.
video_rtp = NULL;
audio_rtp = NULL;

caster = c;
stfd = fd;
Expand All @@ -64,6 +84,9 @@ SrsRtspConn::~SrsRtspConn()
srs_close_stfd(stfd);
trd->stop();

srs_freep(video_rtp);
srs_freep(audio_rtp);

srs_freep(trd);
srs_freep(skt);
srs_freep(rtsp);
Expand Down Expand Up @@ -103,6 +126,8 @@ int SrsRtspConn::do_cycle()
}
} else if (req->is_announce()) {
srs_assert(req->sdp);
video_id = req->sdp->video_stream_id;
audio_id = req->sdp->audio_stream_id;
sps = req->sdp->video_sps;
pps = req->sdp->video_pps;
asc = req->sdp->audio_sh;
Expand All @@ -119,11 +144,31 @@ int SrsRtspConn::do_cycle()
}
} else if (req->is_setup()) {
srs_assert(req->transport);
int lpm = 0;
if ((ret = caster->alloc_port(&lpm)) != ERROR_SUCCESS) {
srs_error("rtsp: alloc port failed. ret=%d", ret);
return ret;
}

SrsRtpConn* rtp = NULL;
if (req->stream_id == video_id) {
srs_freep(video_rtp);
rtp = video_rtp = new SrsRtpConn(this, lpm);
} else {
srs_freep(audio_rtp);
rtp = audio_rtp = new SrsRtpConn(this, lpm);
}
if ((ret = rtp->listen()) != ERROR_SUCCESS) {
srs_error("rtsp: rtp listen at port=%d failed. ret=%d", lpm, ret);
return ret;
}
srs_trace("rtsp: rtp listen at port=%d ok.", lpm);

SrsRtspSetupResponse* res = new SrsRtspSetupResponse(req->seq);
res->client_port_min = req->transport->client_port_min;
res->client_port_max = req->transport->client_port_max;
res->local_port_min = local_port_min;
res->local_port_max = local_port_max;
res->local_port_min = lpm;
res->local_port_max = lpm + 1;
res->session = session;
if ((ret = rtsp->send_message(res)) != ERROR_SUCCESS) {
if (!srs_is_client_gracefully_close(ret)) {
Expand Down Expand Up @@ -165,6 +210,14 @@ int SrsRtspConn::cycle()

void SrsRtspConn::on_thread_stop()
{
if (video_rtp) {
caster->free_port(video_rtp->port(), video_rtp->port() + 1);
}

if (audio_rtp) {
caster->free_port(audio_rtp->port(), audio_rtp->port() + 1);
}

caster->remove(this);
}

Expand All @@ -184,16 +237,40 @@ SrsRtspCaster::~SrsRtspCaster()
srs_freep(conn);
}
clients.clear();
used_ports.clear();
}

int SrsRtspCaster::alloc_port(int* pport)
{
int ret = ERROR_SUCCESS;

// use a pair of port.
for (int i = local_port_min; i < local_port_max - 1; i += 2) {
if (!used_ports[i]) {
used_ports[i] = true;
used_ports[i + 1] = true;
*pport = i;
break;
}
}
srs_info("rtsp: alloc port=%d-%d", *pport, *pport + 1);

return ret;
}

void SrsRtspCaster::free_port(int lpmin, int lpmax)
{
for (int i = lpmin; i < lpmax; i++) {
used_ports[i] = false;
}
srs_trace("rtsp: free rtp port=%d-%d", lpmin, lpmax);
}

int SrsRtspCaster::serve_client(st_netfd_t stfd)
int SrsRtspCaster::on_tcp_client(st_netfd_t stfd)
{
int ret = ERROR_SUCCESS;

SrsRtspConn* conn = new SrsRtspConn(
this, stfd,
output, local_port_min, local_port_max
);
SrsRtspConn* conn = new SrsRtspConn(this, stfd, output);

if ((ret = conn->serve()) != ERROR_SUCCESS) {
srs_error("rtsp: serve client failed. ret=%d", ret);
Expand Down
53 changes: 39 additions & 14 deletions trunk/src/app/srs_app_rtsp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,30 +32,38 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

#include <string>
#include <vector>
#include <map>

#include <srs_app_st.hpp>
#include <srs_app_thread.hpp>
#include <srs_app_listener.hpp>

#ifdef SRS_AUTO_STREAM_CASTER

class SrsConfDirective;
class SrsStSocket;
class SrsRtspConn;
class SrsRtspStack;
class SrsRtspCaster;
class SrsConfDirective;

/**
* the handler for rtsp handler.
* a rtp connection which transport a stream.
*/
class ISrsRtspHandler
class SrsRtpConn: public ISrsUdpHandler
{
private:
SrsUdpListener* listener;
SrsRtspConn* rtsp;
int _port;
public:
ISrsRtspHandler();
virtual ~ISrsRtspHandler();
SrsRtpConn(SrsRtspConn* r, int p);
virtual ~SrsRtpConn();
public:
/**
* serve the rtsp connection.
*/
virtual int serve_client(st_netfd_t stfd) = 0;
virtual int port();
virtual int listen();
// interface ISrsUdpHandler
public:
virtual int on_udp_packet(sockaddr_in* from, char* buf, int nb_buf);
};

/**
Expand All @@ -65,10 +73,14 @@ class SrsRtspConn : public ISrsThreadHandler
{
private:
std::string output;
int local_port_min;
int local_port_max;
private:
std::string session;
// video stream.
std::string video_id;
SrsRtpConn* video_rtp;
// audio stream.
std::string audio_id;
SrsRtpConn* audio_rtp;
// video sequence header.
std::string sps;
std::string pps;
Expand All @@ -81,7 +93,7 @@ class SrsRtspConn : public ISrsThreadHandler
SrsRtspCaster* caster;
SrsThread* trd;
public:
SrsRtspConn(SrsRtspCaster* c, st_netfd_t fd, std::string o, int lpmin, int lpmax);
SrsRtspConn(SrsRtspCaster* c, st_netfd_t fd, std::string o);
virtual ~SrsRtspConn();
public:
virtual int serve();
Expand All @@ -96,19 +108,32 @@ class SrsRtspConn : public ISrsThreadHandler
/**
* the caster for rtsp.
*/
class SrsRtspCaster : public ISrsRtspHandler
class SrsRtspCaster : public ISrsTcpHandler
{
private:
std::string output;
int local_port_min;
int local_port_max;
// key: port, value: whether used.
std::map<int, bool> used_ports;
private:
std::vector<SrsRtspConn*> clients;
public:
SrsRtspCaster(SrsConfDirective* c);
virtual ~SrsRtspCaster();
public:
virtual int serve_client(st_netfd_t stfd);
/**
* alloc a rtp port from local ports pool.
* @param pport output the rtp port.
*/
virtual int alloc_port(int* pport);
/**
* free the alloced rtp port.
*/
virtual void free_port(int lpmin, int lpmax);
// interface ISrsTcpHandler
public:
virtual int on_tcp_client(st_netfd_t stfd);
// internal methods.
public:
virtual void remove(SrsRtspConn* conn);
Expand Down
2 changes: 1 addition & 1 deletion trunk/src/app/srs_app_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ int SrsRtspListener::on_tcp_client(st_netfd_t stfd)
{
int ret = ERROR_SUCCESS;

if ((ret = caster->serve_client(stfd)) != ERROR_SUCCESS) {
if ((ret = caster->on_tcp_client(stfd)) != ERROR_SUCCESS) {
srs_warn("accept client error. ret=%d", ret);
return ret;
}
Expand Down
5 changes: 2 additions & 3 deletions trunk/src/app/srs_app_server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ class SrsIngester;
class SrsHttpHeartbeat;
class SrsKbps;
class SrsConfDirective;
class ISrsTcpHandler;
class ISrsUdpHandler;
class ISrsRtspHandler;
class SrsUdpListener;
class SrsTcpListener;

Expand Down Expand Up @@ -111,8 +111,7 @@ class SrsRtspListener : virtual public SrsListener, virtual public ISrsTcpHandle
{
private:
SrsTcpListener* listener;
private:
ISrsRtspHandler* caster;
ISrsTcpHandler* caster;
public:
SrsRtspListener(SrsServer* server, SrsListenerType type, SrsConfDirective* c);
virtual ~SrsRtspListener();
Expand Down
32 changes: 28 additions & 4 deletions trunk/src/app/srs_app_thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ SrsThread::SrsThread(const char* name, ISrsThreadHandler* thread_handler, int64_

tid = NULL;
loop = false;
really_terminated = true;
_cid = -1;
_joinable = joinable;

Expand Down Expand Up @@ -120,10 +121,27 @@ void SrsThread::stop()
// which will terminate the cycle thread.
st_thread_interrupt(tid);

// wait the thread to exit.
int ret = st_thread_join(tid, NULL);
// TODO: FIXME: the join maybe failed, should use a variable to ensure thread terminated.
srs_assert(ret == 0);
// when joinable, wait util quit.
if (_joinable) {
// wait the thread to exit.
int ret = st_thread_join(tid, NULL);
if (ret) {
srs_warn("core: ignore join thread failed.");
}

// wait the thread actually terminated.
// sometimes the thread join return -1, for example,
// when thread use st_recvfrom, the thread join return -1.
// so here, we use a variable to ensure the thread stopped.
while (!really_terminated) {
st_usleep(10 * 1000);

if (really_terminated) {
break;
}
srs_warn("core: wait thread to actually terminated");
}
}

tid = NULL;
}
Expand All @@ -150,6 +168,9 @@ void SrsThread::thread_cycle()

srs_assert(handler);
handler->on_thread_start();

// thread is running now.
really_terminated = false;

// wait for cid to ready, for parent thread to get the cid.
while (!can_run && loop) {
Expand Down Expand Up @@ -191,6 +212,9 @@ void SrsThread::thread_cycle()

handler->on_thread_stop();
srs_info("thread %s cycle finished", _name);

// readly terminated now.
really_terminated = true;
}

void* SrsThread::thread_fun(void* arg)
Expand Down
1 change: 1 addition & 0 deletions trunk/src/app/srs_app_thread.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ class SrsThread
int _cid;
bool loop;
bool can_run;
bool really_terminated;
bool _joinable;
const char* _name;
private:
Expand Down
Loading

0 comments on commit f14af45

Please sign in to comment.