From e81e090239c62b8b1ac10c495f7640ab557e4776 Mon Sep 17 00:00:00 2001 From: winlin Date: Sun, 15 Feb 2015 21:28:31 +0800 Subject: [PATCH] for #133, add rtsp listener and handler. --- trunk/conf/full.conf | 30 +++++++++++--- trunk/configure | 2 +- trunk/ide/srs_upp/srs_upp.upp | 2 + trunk/src/app/srs_app_config.hpp | 1 + trunk/src/app/srs_app_rtsp.cpp | 48 +++++++++++++++++++++++ trunk/src/app/srs_app_rtsp.hpp | 63 ++++++++++++++++++++++++++++++ trunk/src/app/srs_app_server.cpp | 45 ++++++++++++++++++++- trunk/src/app/srs_app_server.hpp | 18 +++++++++ trunk/src/core/srs_core.hpp | 2 +- trunk/src/main/srs_main_server.cpp | 6 +++ 10 files changed, 209 insertions(+), 8 deletions(-) create mode 100644 trunk/src/app/srs_app_rtsp.cpp create mode 100644 trunk/src/app/srs_app_rtsp.hpp diff --git a/trunk/conf/full.conf b/trunk/conf/full.conf index a00f939580..d20284aa6f 100644 --- a/trunk/conf/full.conf +++ b/trunk/conf/full.conf @@ -141,17 +141,37 @@ http_server { stream_caster { # whether stream caster is enabled. # default: off - enabled on; + enabled off; # the caster type of stream, the casters: # mpegts_over_udp, MPEG-TS over UDP caster. + # rtsp, Real Time Streaming Protocol (RTSP). caster mpegts_over_udp; # the output rtmp url. - # for example, rtmp://127.0.0.1/live/livestream. + # for mpegts_over_udp caster, the typically output url: + # rtmp://127.0.0.1/live/livestream + # for rtsp caster, the typically output url: + # rtmp://127.0.0.1/[app]/[stream] + # for example, the rtsp url: + # rtsp://192.168.1.173:8544/live/livestream.sdp + # where the [app] is "live" and [stream] is "livestream", output is: + # rtmp://127.0.0.1/live/livestream output rtmp://127.0.0.1/live/livestream; # the listen port for stream caster. - # for caster: - # mpegts_over_udp, listen at udp port. - listen 1935; + # for mpegts_over_udp caster, listen at udp port. + # for rtsp caster, listen at tcp port. + listen 8935; +} +stream_caster { + enabled off; + caster mpegts_over_udp; + output rtmp://127.0.0.1/live/livestream; + listen 8935; +} +stream_caster { + enabled on; + caster rtsp; + output rtmp://127.0.0.1/[app]/[stream]; + listen 554; } ############################################################################################# diff --git a/trunk/configure b/trunk/configure index 1a1ecf6777..41630abb68 100755 --- a/trunk/configure +++ b/trunk/configure @@ -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_mpegts_udp" "srs_app_rtsp") APP_INCS="src/app"; MODULE_DIR=${APP_INCS} . auto/modules.sh APP_OBJS="${MODULE_OBJS[@]}" fi diff --git a/trunk/ide/srs_upp/srs_upp.upp b/trunk/ide/srs_upp/srs_upp.upp index 7749a6949e..d617bc52dc 100755 --- a/trunk/ide/srs_upp/srs_upp.upp +++ b/trunk/ide/srs_upp/srs_upp.upp @@ -112,6 +112,8 @@ file ../../src/app/srs_app_reload.cpp, ../../src/app/srs_app_rtmp_conn.hpp, ../../src/app/srs_app_rtmp_conn.cpp, + ../../src/app/srs_app_rtsp.hpp, + ../../src/app/srs_app_rtsp.cpp, ../../src/app/srs_app_pithy_print.hpp, ../../src/app/srs_app_pithy_print.cpp, ../../src/app/srs_app_security.hpp, diff --git a/trunk/src/app/srs_app_config.hpp b/trunk/src/app/srs_app_config.hpp index bb2cf8d582..36807ea5e3 100644 --- a/trunk/src/app/srs_app_config.hpp +++ b/trunk/src/app/srs_app_config.hpp @@ -87,6 +87,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #define SRS_CONF_DEFAULT_STREAM_CASTER_ENABLED false #define SRS_CONF_DEFAULT_STREAM_CASTER_MPEGTS_OVER_UDP "mpegts_over_udp" +#define SRS_CONF_DEFAULT_STREAM_CASTER_RTSP "rtsp" #define SRS_CONF_DEFAULT_STATS_NETWORK_DEVICE_INDEX 0 diff --git a/trunk/src/app/srs_app_rtsp.cpp b/trunk/src/app/srs_app_rtsp.cpp new file mode 100644 index 0000000000..56c091bf39 --- /dev/null +++ b/trunk/src/app/srs_app_rtsp.cpp @@ -0,0 +1,48 @@ +/* +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 + +#include + +#ifdef SRS_AUTO_STREAM_CASTER + +ISrsRtspHandler::ISrsRtspHandler() +{ +} + +ISrsRtspHandler::~ISrsRtspHandler() +{ +} + +SrsRtspConn::SrsRtspConn(SrsConfDirective* c) +{ + output = _srs_config->get_stream_caster_output(c); +} + +SrsRtspConn::~SrsRtspConn() +{ +} + +#endif + diff --git a/trunk/src/app/srs_app_rtsp.hpp b/trunk/src/app/srs_app_rtsp.hpp new file mode 100644 index 0000000000..acf7995c8f --- /dev/null +++ b/trunk/src/app/srs_app_rtsp.hpp @@ -0,0 +1,63 @@ +/* +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. +*/ + +#ifndef SRS_APP_RTSP_HPP +#define SRS_APP_RTSP_HPP + +/* +#include +*/ + +#include + +#include + +#ifdef SRS_AUTO_STREAM_CASTER + +class SrsConfDirective; + +/** +* the handler for rtsp handler. +*/ +class ISrsRtspHandler +{ +public: + ISrsRtspHandler(); + virtual ~ISrsRtspHandler(); +}; + +/** +* the connection for rtsp. +*/ +class SrsRtspConn : public ISrsRtspHandler +{ +private: + std::string output; +public: + SrsRtspConn(SrsConfDirective* c); + virtual ~SrsRtspConn(); +}; + +#endif + +#endif diff --git a/trunk/src/app/srs_app_server.cpp b/trunk/src/app/srs_app_server.cpp index d7a89a0803..626f20640c 100644 --- a/trunk/src/app/srs_app_server.cpp +++ b/trunk/src/app/srs_app_server.cpp @@ -46,6 +46,7 @@ using namespace std; #include #include #include +#include // signal defines. #define SIGNAL_RELOAD SIGHUP @@ -112,6 +113,8 @@ std::string __srs_listener_type2string(SrsListenerType type) return "HTTP-Server"; case SrsListenerMpegTsOverUdp: return "MPEG-TS over UDP"; + case SrsListenerRtsp: + return "RTSP"; default: return "UNKONWN"; } @@ -229,6 +232,44 @@ int SrsListener::cycle() } #ifdef SRS_AUTO_STREAM_CASTER +SrsRtspListener::SrsRtspListener(SrsServer* server, SrsListenerType type, SrsConfDirective* c) : SrsListener(server, type) +{ + _type = type; + + // the caller already ensure the type is ok, + // we just assert here for unknown stream caster. + srs_assert(_type == SrsListenerRtsp); + if (_type == SrsListenerRtsp) { + caster = new SrsRtspConn(c); + } +} + +SrsRtspListener::~SrsRtspListener() +{ + srs_freep(caster); +} + +int SrsRtspListener::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 = _server->accept_client(_type, client_stfd)) != ERROR_SUCCESS) { + srs_warn("accept client error. ret=%d", ret); + return ret; + } + + return ret; +} + SrsUdpListener::SrsUdpListener(SrsServer* server, SrsListenerType type, SrsConfDirective* c) : SrsListener(server, type) { _type = type; @@ -1022,11 +1063,13 @@ int SrsServer::listen_stream_caster() continue; } - SrsUdpListener* listener = NULL; + SrsListener* listener = NULL; std::string caster = _srs_config->get_stream_caster_engine(stream_caster); if (caster == SRS_CONF_DEFAULT_STREAM_CASTER_MPEGTS_OVER_UDP) { listener = new SrsUdpListener(this, SrsListenerMpegTsOverUdp, stream_caster); + } else if (caster == SRS_CONF_DEFAULT_STREAM_CASTER_RTSP) { + listener = new SrsRtspListener(this, SrsListenerRtsp, stream_caster); } else { ret = ERROR_STREAM_CASTER_ENGINE; srs_error("unsupported stream caster %s. ret=%d", caster.c_str(), ret); diff --git a/trunk/src/app/srs_app_server.hpp b/trunk/src/app/srs_app_server.hpp index 79112840ff..496f6c509d 100644 --- a/trunk/src/app/srs_app_server.hpp +++ b/trunk/src/app/srs_app_server.hpp @@ -48,6 +48,7 @@ class SrsHttpHeartbeat; class SrsKbps; class SrsConfDirective; class ISrsUdpHandler; +class ISrsRtspHandler; // listener type for server to identify the connection, // that is, use different type to process the connection. @@ -61,6 +62,8 @@ enum SrsListenerType SrsListenerHttpStream = 2, // UDP stream, MPEG-TS over udp. SrsListenerMpegTsOverUdp = 3, + // TCP stream, RTSP stream. + SrsListenerRtsp = 4, }; /** @@ -88,6 +91,21 @@ class SrsListener : public ISrsThreadHandler }; #ifdef SRS_AUTO_STREAM_CASTER +/** +* the tcp listener, for rtsp server. +*/ +class SrsRtspListener : public SrsListener +{ +private: + ISrsRtspHandler* caster; +public: + SrsRtspListener(SrsServer* server, SrsListenerType type, SrsConfDirective* c); + virtual ~SrsRtspListener(); +// interface ISrsThreadHandler. +public: + virtual int cycle(); +}; + /** * the udp listener, for udp server. */ diff --git a/trunk/src/core/srs_core.hpp b/trunk/src/core/srs_core.hpp index 2ecc3cc4c6..817dc22da1 100644 --- a/trunk/src/core/srs_core.hpp +++ b/trunk/src/core/srs_core.hpp @@ -31,7 +31,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // current release version #define VERSION_MAJOR 2 #define VERSION_MINOR 0 -#define VERSION_REVISION 118 +#define VERSION_REVISION 119 // server info. #define RTMP_SIG_SRS_KEY "SRS" diff --git a/trunk/src/main/srs_main_server.cpp b/trunk/src/main/srs_main_server.cpp index 3b010bd555..d0ead523c0 100644 --- a/trunk/src/main/srs_main_server.cpp +++ b/trunk/src/main/srs_main_server.cpp @@ -132,6 +132,12 @@ void show_macro_features() srs_warn("check feature compile ffmpeg: off"); #endif +#ifdef SRS_AUTO_STREAM_CASTER + srs_trace("stream caster: on"); +#else + srs_warn("stream caster: off"); +#endif + #ifdef SRS_PERF_MERGED_READ srs_trace("MR(merged-read): on, @see %s", RTMP_SIG_SRS_ISSUES(241)); #else