Skip to content

Commit

Permalink
fix #149, RTMP/HTTP support bind to <[ip:]port>. 2.0.148
Browse files Browse the repository at this point in the history
  • Loading branch information
winlinvip committed Mar 23, 2015
2 parents d16ae19 + b6feb07 commit 713cedf
Show file tree
Hide file tree
Showing 11 changed files with 147 additions and 82 deletions.
13 changes: 10 additions & 3 deletions trunk/conf/full.conf
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
#############################################################################################
# RTMP sections
#############################################################################################
# the rtmp listen ports, split by space.
# the rtmp listen ports, split by space, each listen entry is <[ip:]port>
# for example, 192.168.1.100:1935 10.10.10.100:1935
# where the ip is optional, default to 0.0.0.0, that is 1935 equals to 0.0.0.0:1935
listen 1935;
# the pid file
# to ensure only one process can use a pid file
Expand Down Expand Up @@ -106,7 +108,9 @@ http_api {
# whether http api is enabled.
# default: off
enabled on;
# the http api port
# the http api listen entry is <[ip:]port>
# for example, 192.168.1.100:1985
# where the ip is optional, default to 0.0.0.0, that is 1985 equals to 0.0.0.0:1985
# default: 1985
listen 1985;
# whether enable crossdomain request.
Expand All @@ -127,7 +131,9 @@ http_server {
# whether http streaming service is enabled.
# default: off
enabled on;
# the http streaming port
# the http streaming listen entry is <[ip:]port>
# for example, 192.168.1.100:8080
# where the ip is optional, default to 0.0.0.0, that is 8080 equals to 0.0.0.0:8080
# @remark, if use lower port, for instance 80, user must start srs by root.
# default: 8080
listen 8080;
Expand Down Expand Up @@ -162,6 +168,7 @@ stream_caster {
# the listen port for stream caster.
# for mpegts_over_udp caster, listen at udp port. for example, 8935.
# for rtsp caster, listen at tcp port. for example, 554.
# TODO: support listen at <[ip:]port>
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:
Expand Down
30 changes: 15 additions & 15 deletions trunk/src/app/srs_app_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1586,7 +1586,7 @@ int SrsConfig::check_config()
// check listen for rtmp.
////////////////////////////////////////////////////////////////////////
if (true) {
vector<string> listens = get_listen();
vector<string> listens = get_listens();
if (listens.size() <= 0) {
ret = ERROR_SYSTEM_CONFIG_INVALID;
srs_error("directive \"listen\" is empty, ret=%d", ret);
Expand All @@ -1613,11 +1613,11 @@ int SrsConfig::check_config()

// check max connections of system limits
if (true) {
int nb_consumed_fds = (int)get_listen().size();
if (get_http_api_listen() > 0) {
int nb_consumed_fds = (int)get_listens().size();
if (!get_http_api_listen().empty()) {
nb_consumed_fds++;
}
if (get_http_stream_listen() > 0) {
if (!get_http_stream_listen().empty()) {
nb_consumed_fds++;
}
if (get_log_tank_file()) {
Expand Down Expand Up @@ -1694,20 +1694,20 @@ int SrsConfig::check_config()
////////////////////////////////////////////////////////////////////////
// check http api
////////////////////////////////////////////////////////////////////////
if (get_http_api_listen() <= 0) {
if (get_http_api_listen().empty()) {
ret = ERROR_SYSTEM_CONFIG_INVALID;
srs_error("directive http_api listen invalid, listen=%d, ret=%d",
get_http_api_listen(), ret);
srs_error("directive http_api listen invalid, listen=%s, ret=%d",
get_http_api_listen().c_str(), ret);
return ret;
}

////////////////////////////////////////////////////////////////////////
// check http stream
////////////////////////////////////////////////////////////////////////
if (get_http_stream_listen() <= 0) {
if (get_http_stream_listen().empty()) {
ret = ERROR_SYSTEM_CONFIG_INVALID;
srs_error("directive http_stream listen invalid, listen=%d, ret=%d",
get_http_stream_listen(), ret);
srs_error("directive http_stream listen invalid, listen=%s, ret=%d",
get_http_stream_listen().c_str(), ret);
return ret;
}

Expand Down Expand Up @@ -1858,7 +1858,7 @@ int SrsConfig::get_max_connections()
return ::atoi(conf->arg0().c_str());
}

vector<string> SrsConfig::get_listen()
vector<string> SrsConfig::get_listens()
{
std::vector<string> ports;

Expand Down Expand Up @@ -3546,7 +3546,7 @@ bool SrsConfig::get_http_api_enabled(SrsConfDirective* conf)
return false;
}

int SrsConfig::get_http_api_listen()
string SrsConfig::get_http_api_listen()
{
SrsConfDirective* conf = get_http_api();

Expand All @@ -3559,7 +3559,7 @@ int SrsConfig::get_http_api_listen()
return SRS_CONF_DEFAULT_HTTP_API_PORT;
}

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

bool SrsConfig::get_http_api_crossdomain()
Expand Down Expand Up @@ -3609,7 +3609,7 @@ bool SrsConfig::get_http_stream_enabled(SrsConfDirective* conf)
return false;
}

int SrsConfig::get_http_stream_listen()
string SrsConfig::get_http_stream_listen()
{
SrsConfDirective* conf = get_http_stream();

Expand All @@ -3622,7 +3622,7 @@ int SrsConfig::get_http_stream_listen()
return SRS_CONF_DEFAULT_HTTP_STREAM_PORT;
}

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

string SrsConfig::get_http_stream_dir()
Expand Down
10 changes: 5 additions & 5 deletions trunk/src/app/srs_app_config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#define SRS_CONF_DEFAULT_HTTP_DIR SRS_CONF_DEFAULT_HLS_PATH
#define SRS_CONF_DEFAULT_HTTP_AUDIO_FAST_CACHE 0

#define SRS_CONF_DEFAULT_HTTP_STREAM_PORT 8080
#define SRS_CONF_DEFAULT_HTTP_API_PORT 1985
#define SRS_CONF_DEFAULT_HTTP_STREAM_PORT "8080"
#define SRS_CONF_DEFAULT_HTTP_API_PORT "1985"
#define SRS_CONF_DEFAULT_HTTP_API_CROSSDOMAIN true

#define SRS_CONF_DEFAULT_HTTP_HEAETBEAT_ENABLED false
Expand Down Expand Up @@ -411,7 +411,7 @@ class SrsConfig
* user can specifies multiple listen ports,
* each args of directive is a listen port.
*/
virtual std::vector<std::string> get_listen();
virtual std::vector<std::string> get_listens();
/**
* get the pid file path.
* the pid file is used to save the pid of SRS,
Expand Down Expand Up @@ -990,7 +990,7 @@ class SrsConfig
/**
* get the http api listen port.
*/
virtual int get_http_api_listen();
virtual std::string get_http_api_listen();
/**
* whether enable crossdomain for http api.
*/
Expand All @@ -1013,7 +1013,7 @@ class SrsConfig
/**
* get the http stream listen port.
*/
virtual int get_http_stream_listen();
virtual std::string get_http_stream_listen();
/**
* get the http stream root dir.
*/
Expand Down
13 changes: 10 additions & 3 deletions trunk/src/app/srs_app_ingest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ using namespace std;
#include <srs_app_ffmpeg.hpp>
#include <srs_app_pithy_print.hpp>
#include <srs_kernel_utility.hpp>
#include <srs_app_utility.hpp>

// when error, ingester sleep for a while and retry.
// ingest never sleep a long time, for we must start the stream ASAP.
Expand Down Expand Up @@ -228,9 +229,15 @@ int SrsIngester::initialize_ffmpeg(SrsFFMPEG* ffmpeg, SrsConfDirective* vhost, S
{
int ret = ERROR_SUCCESS;

std::vector<std::string> ports = _srs_config->get_listen();
srs_assert(ports.size() > 0);
std::string port = ports[0];
std::string port;
if (true) {
std::vector<std::string> ip_ports = _srs_config->get_listens();
srs_assert(ip_ports.size() > 0);

std::string ep = ip_ports[0];
std::string ip;
srs_parse_endpoint(ep, ip, port);
}

std::string output = _srs_config->get_engine_output(engine);
// output stream, to other/self server
Expand Down
47 changes: 25 additions & 22 deletions trunk/src/app/srs_app_listener.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
using namespace std;

#include <srs_kernel_log.hpp>
#include <srs_kernel_error.hpp>
Expand Down Expand Up @@ -61,9 +62,10 @@ ISrsTcpHandler::~ISrsTcpHandler()
{
}

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

_fd = -1;
Expand Down Expand Up @@ -101,42 +103,42 @@ int SrsUdpListener::listen()

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);
srs_error("create linux socket error. port=%d, ret=%d", ip.c_str(), port, ret);
return ret;
}
srs_verbose("create linux socket success. port=%d, fd=%d", port, _fd);
srs_verbose("create linux socket success. port=%d, fd=%d", ip.c_str(), 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);
srs_error("setsockopt reuse-addr error. port=%d, ret=%d", ip.c_str(), port, ret);
return ret;
}
srs_verbose("setsockopt reuse-addr success. port=%d, fd=%d", port, _fd);
srs_verbose("setsockopt reuse-addr success. port=%d, fd=%d", ip.c_str(), port, _fd);

sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = INADDR_ANY;
addr.sin_addr.s_addr = inet_addr(ip.c_str());
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);
srs_error("bind socket error. ep=%s:%d, ret=%d", ip.c_str(), port, ret);
return ret;
}
srs_verbose("bind socket success. port=%d, fd=%d", port, _fd);
srs_verbose("bind socket success. ep=%s:%d, fd=%d", ip.c_str(), 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);
srs_error("st_netfd_open_socket open socket failed. ep=%s:%d, ret=%d", ip.c_str(), port, ret);
return ret;
}
srs_verbose("st open socket success. port=%d, fd=%d", port, _fd);
srs_verbose("st open socket success. ep=%s:%d, fd=%d", ip.c_str(), port, _fd);

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

return ret;
}
Expand Down Expand Up @@ -169,9 +171,10 @@ int SrsUdpListener::cycle()
return ret;
}

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

_fd = -1;
Expand Down Expand Up @@ -220,33 +223,33 @@ int SrsTcpListener::listen()
sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = INADDR_ANY;
addr.sin_addr.s_addr = inet_addr(ip.c_str());
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);
srs_error("bind socket error. ep=%s:%d, ret=%d", ip.c_str(), port, ret);
return ret;
}
srs_verbose("bind socket success. port=%d, fd=%d", port, _fd);
srs_verbose("bind socket success. ep=%s:%d, fd=%d", ip.c_str(), port, _fd);

if (::listen(_fd, SERVER_LISTEN_BACKLOG) == -1) {
ret = ERROR_SOCKET_LISTEN;
srs_error("listen socket error. port=%d, ret=%d", port, ret);
srs_error("listen socket error. ep=%s:%d, ret=%d", ip.c_str(), port, ret);
return ret;
}
srs_verbose("listen socket success. port=%d, fd=%d", port, _fd);
srs_verbose("listen socket success. ep=%s:%d, fd=%d", ip.c_str(), 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);
srs_error("st_netfd_open_socket open socket failed. ep=%s:%d, ret=%d", ip.c_str(), port, ret);
return ret;
}
srs_verbose("st open socket success. port=%d, fd=%d", port, _fd);
srs_verbose("st open socket success. ep=%s:%d, fd=%d", ip.c_str(), port, _fd);

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

return ret;
}
Expand Down
8 changes: 6 additions & 2 deletions trunk/src/app/srs_app_listener.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

#include <srs_core.hpp>

#include <string>

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

Expand Down Expand Up @@ -85,9 +87,10 @@ class SrsUdpListener : public ISrsThreadHandler
int nb_buf;
private:
ISrsUdpHandler* handler;
std::string ip;
int port;
public:
SrsUdpListener(ISrsUdpHandler* h, int p);
SrsUdpListener(ISrsUdpHandler* h, std::string i, int p);
virtual ~SrsUdpListener();
public:
virtual int fd();
Expand All @@ -109,9 +112,10 @@ class SrsTcpListener : public ISrsThreadHandler
SrsThread* pthread;
private:
ISrsTcpHandler* handler;
std::string ip;
int port;
public:
SrsTcpListener(ISrsTcpHandler* h, int p);
SrsTcpListener(ISrsTcpHandler* h, std::string i, int p);
virtual ~SrsTcpListener();
public:
virtual int fd();
Expand Down
3 changes: 2 additions & 1 deletion trunk/src/app/srs_app_rtsp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ SrsRtpConn::SrsRtpConn(SrsRtspConn* r, int p, int sid)
rtsp = r;
_port = p;
stream_id = sid;
listener = new SrsUdpListener(this, p);
// TODO: support listen at <[ip:]port>
listener = new SrsUdpListener(this, "0.0.0.0", p);
cache = new SrsRtpPacket();
pprint = SrsPithyPrint::create_caster();
}
Expand Down
Loading

0 comments on commit 713cedf

Please sign in to comment.