Skip to content

Commit

Permalink
for ossrs#133, rtsp extract tcp/udp listener.
Browse files Browse the repository at this point in the history
  • Loading branch information
winlinvip committed Feb 17, 2015
1 parent 4807f78 commit 40fbfd8
Show file tree
Hide file tree
Showing 17 changed files with 872 additions and 296 deletions.
5 changes: 5 additions & 0 deletions trunk/conf/full.conf
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,11 @@ stream_caster {
# for mpegts_over_udp caster, listen at udp port. for example, 8935.
# for rtsp caster, listen at tcp port. for example, 554.
listen 8935;
# for the rtsp caster, the rtp server local port over udp,
# which reply the rtsp setup request message, the port will be used:
# [rtp_port_min, rtp_port_max)
rtp_port_min 57200;
rtp_port_max 57300;
}
stream_caster {
enabled off;
Expand Down
2 changes: 1 addition & 1 deletion trunk/configure
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ if [ $SRS_EXPORT_LIBRTMP_PROJECT = NO ]; then
"srs_app_json" "srs_app_ingest" "srs_app_ffmpeg" "srs_app_utility" "srs_app_dvr" "srs_app_edge"
"srs_app_kbps" "srs_app_heartbeat" "srs_app_empty" "srs_app_http_client"
"srs_app_recv_thread" "srs_app_security" "srs_app_statistic"
"srs_app_mpegts_udp" "srs_app_rtsp")
"srs_app_mpegts_udp" "srs_app_rtsp" "srs_app_listener")
APP_INCS="src/app"; MODULE_DIR=${APP_INCS} . auto/modules.sh
APP_OBJS="${MODULE_OBJS[@]}"
fi
Expand Down
2 changes: 2 additions & 0 deletions trunk/ide/srs_upp/srs_upp.upp
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ file
../../src/app/srs_app_json.cpp,
../../src/app/srs_app_kbps.hpp,
../../src/app/srs_app_kbps.cpp,
../../src/app/srs_app_listener.hpp,
../../src/app/srs_app_listener.cpp,
../../src/app/srs_app_log.hpp,
../../src/app/srs_app_log.cpp,
../../src/app/srs_app_mpegts_udp.hpp,
Expand Down
2 changes: 2 additions & 0 deletions trunk/ide/srs_vs2010/srs.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
<ClInclude Include="..\..\src\app\srs_app_ingest.hpp" />
<ClInclude Include="..\..\src\app\srs_app_json.hpp" />
<ClInclude Include="..\..\src\app\srs_app_kbps.hpp" />
<ClInclude Include="..\..\src\app\srs_app_listener.hpp" />
<ClInclude Include="..\..\src\app\srs_app_log.hpp" />
<ClInclude Include="..\..\src\app\srs_app_mpegts_udp.hpp" />
<ClInclude Include="..\..\src\app\srs_app_pithy_print.hpp" />
Expand Down Expand Up @@ -161,6 +162,7 @@
<ClCompile Include="..\..\src\app\srs_app_ingest.cpp" />
<ClCompile Include="..\..\src\app\srs_app_json.cpp" />
<ClCompile Include="..\..\src\app\srs_app_kbps.cpp" />
<ClCompile Include="..\..\src\app\srs_app_listener.cpp" />
<ClCompile Include="..\..\src\app\srs_app_log.cpp" />
<ClCompile Include="..\..\src\app\srs_app_mpegts_udp.cpp" />
<ClCompile Include="..\..\src\app\srs_app_pithy_print.cpp" />
Expand Down
6 changes: 6 additions & 0 deletions trunk/ide/srs_vs2010/srs.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,9 @@
<ClCompile Include="..\..\src\protocol\srs_rtsp_stack.cpp">
<Filter>srs</Filter>
</ClCompile>
<ClCompile Include="..\..\src\app\srs_app_listener.cpp">
<Filter>srs</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\src\app\srs_app_bandwidth.hpp">
Expand Down Expand Up @@ -432,6 +435,9 @@
<ClInclude Include="..\..\src\protocol\srs_rtsp_stack.hpp">
<Filter>srs</Filter>
</ClInclude>
<ClInclude Include="..\..\src\app\srs_app_listener.hpp">
<Filter>srs</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<Filter Include="research">
Expand Down
26 changes: 25 additions & 1 deletion trunk/src/app/srs_app_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1395,7 +1395,7 @@ int SrsConfig::check_config()
SrsConfDirective* conf = stream_caster->at(i);
string n = conf->name;
if (n != "enabled" && n != "caster" && n != "output"
&& n != "listen"
&& n != "listen" && n != "rtp_port_min" && n != "rtp_port_max"
) {
ret = ERROR_SYSTEM_CONFIG_INVALID;
srs_error("unsupported stream_caster directive %s, ret=%d", n.c_str(), ret);
Expand Down Expand Up @@ -2065,6 +2065,30 @@ int SrsConfig::get_stream_caster_listen(SrsConfDirective* sc)
return ::atoi(conf->arg0().c_str());
}

int SrsConfig::get_stream_caster_rtp_port_min(SrsConfDirective* sc)
{
srs_assert(sc);

SrsConfDirective* conf = sc->get("rtp_port_min");
if (!conf) {
return 0;
}

return ::atoi(conf->arg0().c_str());
}

int SrsConfig::get_stream_caster_rtp_port_max(SrsConfDirective* sc)
{
srs_assert(sc);

SrsConfDirective* conf = sc->get("rtp_port_max");
if (!conf) {
return 0;
}

return ::atoi(conf->arg0().c_str());
}

SrsConfDirective* SrsConfig::get_vhost(string vhost)
{
srs_assert(root);
Expand Down
8 changes: 8 additions & 0 deletions trunk/src/app/srs_app_config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,14 @@ class SrsConfig
* get the listen port of stream caster.
*/
virtual int get_stream_caster_listen(SrsConfDirective* sc);
/**
* get the min udp port for rtp of stream caster rtsp.
*/
virtual int get_stream_caster_rtp_port_min(SrsConfDirective* sc);
/**
* get the max udp port for rtp of stream caster rtsp.
*/
virtual int get_stream_caster_rtp_port_max(SrsConfDirective* sc);
// vhost specified section
public:
/**
Expand Down
272 changes: 272 additions & 0 deletions trunk/src/app/srs_app_listener.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,272 @@
/*
The MIT License (MIT)
Copyright (c) 2013-2015 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.
*/

#include <srs_app_listener.hpp>

#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include <srs_kernel_log.hpp>
#include <srs_kernel_error.hpp>
#include <srs_app_server.hpp>
#include <srs_app_utility.hpp>

// set the max packet size.
#define SRS_UDP_MAX_PACKET_SIZE 65535

// sleep in ms for udp recv packet.
#define SRS_UDP_PACKET_RECV_CYCLE_INTERVAL_MS 0

// nginx also set to 512
#define SERVER_LISTEN_BACKLOG 512

ISrsUdpHandler::ISrsUdpHandler()
{
}

ISrsUdpHandler::~ISrsUdpHandler()
{
}

ISrsTcpHandler::ISrsTcpHandler()
{
}

ISrsTcpHandler::~ISrsTcpHandler()
{
}

SrsUdpListener::SrsUdpListener(ISrsUdpHandler* h, int p)
{
handler = h;
port = p;

_fd = -1;
stfd = NULL;

nb_buf = SRS_UDP_MAX_PACKET_SIZE;
buf = new char[nb_buf];

pthread = new SrsThread("udp", this, 0, true);
}

SrsUdpListener::~SrsUdpListener()
{
srs_close_stfd(stfd);

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

// st does not close it sometimes,
// close it manually.
close(_fd);

srs_freep(buf);
}

int SrsUdpListener::fd()
{
return _fd;
}

int SrsUdpListener::listen()
{
int ret = ERROR_SUCCESS;

if ((_fd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
ret = ERROR_SOCKET_CREATE;
srs_error("create linux socket error. port=%d, ret=%d", port, ret);
return ret;
}
srs_verbose("create linux socket success. port=%d, fd=%d", port, _fd);

int reuse_socket = 1;
if (setsockopt(_fd, SOL_SOCKET, SO_REUSEADDR, &reuse_socket, sizeof(int)) == -1) {
ret = ERROR_SOCKET_SETREUSE;
srs_error("setsockopt reuse-addr error. port=%d, ret=%d", port, ret);
return ret;
}
srs_verbose("setsockopt reuse-addr success. port=%d, fd=%d", port, _fd);

sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = INADDR_ANY;
if (bind(_fd, (const sockaddr*)&addr, sizeof(sockaddr_in)) == -1) {
ret = ERROR_SOCKET_BIND;
srs_error("bind socket error. port=%d, ret=%d", port, ret);
return ret;
}
srs_verbose("bind socket success. port=%d, fd=%d", port, _fd);

if ((stfd = st_netfd_open_socket(_fd)) == NULL){
ret = ERROR_ST_OPEN_SOCKET;
srs_error("st_netfd_open_socket open socket failed. port=%d, ret=%d", port, ret);
return ret;
}
srs_verbose("st open socket success. port=%d, fd=%d", port, _fd);

if ((ret = pthread->start()) != ERROR_SUCCESS) {
srs_error("st_thread_create listen thread error. port=%d, ret=%d", port, ret);
return ret;
}
srs_verbose("create st listen thread success, port=%d", port);

return ret;
}

int SrsUdpListener::cycle()
{
int ret = ERROR_SUCCESS;

for (;;) {
// TODO: FIXME: support ipv6, @see man 7 ipv6
sockaddr_in from;
int nb_from = sizeof(sockaddr_in);
int nread = 0;

if ((nread = st_recvfrom(stfd, buf, nb_buf, (sockaddr*)&from, &nb_from, ST_UTIME_NO_TIMEOUT)) <= 0) {
srs_warn("ignore recv udp packet failed, nread=%d", nread);
continue;
}

if ((ret = handler->on_udp_packet(&from, buf, nread)) != ERROR_SUCCESS) {
srs_warn("handle udp packet failed. ret=%d", ret);
continue;
}

if (SRS_UDP_PACKET_RECV_CYCLE_INTERVAL_MS > 0) {
st_usleep(SRS_UDP_PACKET_RECV_CYCLE_INTERVAL_MS * 1000);
}
}

return ret;
}

SrsTcpListener::SrsTcpListener(ISrsTcpHandler* h, int p)
{
handler = h;
port = p;

_fd = -1;
stfd = NULL;

pthread = new SrsThread("tcp", this, 0, true);
}

SrsTcpListener::~SrsTcpListener()
{
srs_close_stfd(stfd);

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

// st does not close it sometimes,
// close it manually.
close(_fd);
}

int SrsTcpListener::fd()
{
return _fd;
}

int SrsTcpListener::listen()
{
int ret = ERROR_SUCCESS;

if ((_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
ret = ERROR_SOCKET_CREATE;
srs_error("create linux socket error. port=%d, ret=%d", port, ret);
return ret;
}
srs_verbose("create linux socket success. port=%d, fd=%d", port, _fd);

int reuse_socket = 1;
if (setsockopt(_fd, SOL_SOCKET, SO_REUSEADDR, &reuse_socket, sizeof(int)) == -1) {
ret = ERROR_SOCKET_SETREUSE;
srs_error("setsockopt reuse-addr error. port=%d, ret=%d", port, ret);
return ret;
}
srs_verbose("setsockopt reuse-addr success. port=%d, fd=%d", port, _fd);

sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = INADDR_ANY;
if (bind(_fd, (const sockaddr*)&addr, sizeof(sockaddr_in)) == -1) {
ret = ERROR_SOCKET_BIND;
srs_error("bind socket error. port=%d, ret=%d", port, ret);
return ret;
}
srs_verbose("bind socket success. port=%d, fd=%d", port, _fd);

if (::listen(_fd, SERVER_LISTEN_BACKLOG) == -1) {
ret = ERROR_SOCKET_LISTEN;
srs_error("listen socket error. port=%d, ret=%d", port, ret);
return ret;
}
srs_verbose("listen socket success. port=%d, fd=%d", port, _fd);

if ((stfd = st_netfd_open_socket(_fd)) == NULL){
ret = ERROR_ST_OPEN_SOCKET;
srs_error("st_netfd_open_socket open socket failed. port=%d, ret=%d", port, ret);
return ret;
}
srs_verbose("st open socket success. port=%d, fd=%d", port, _fd);

if ((ret = pthread->start()) != ERROR_SUCCESS) {
srs_error("st_thread_create listen thread error. port=%d, ret=%d", port, ret);
return ret;
}
srs_verbose("create st listen thread success, port=%d", port);

return ret;
}

int SrsTcpListener::cycle()
{
int ret = ERROR_SUCCESS;

st_netfd_t client_stfd = st_accept(stfd, NULL, NULL, ST_UTIME_NO_TIMEOUT);

if(client_stfd == NULL){
// ignore error.
srs_error("ignore accept thread stoppped for accept client error");
return ret;
}
srs_verbose("get a client. fd=%d", st_netfd_fileno(client_stfd));

if ((ret = handler->on_tcp_client(client_stfd)) != ERROR_SUCCESS) {
srs_warn("accept client error. ret=%d", ret);
return ret;
}

return ret;
}

Loading

0 comments on commit 40fbfd8

Please sign in to comment.