From 6463d22a1404ec753172afd0465eb67dec017279 Mon Sep 17 00:00:00 2001 From: winlin Date: Sat, 24 Jan 2015 16:27:30 +0800 Subject: [PATCH] for #250, add mpegts over udp stream caster class. --- trunk/configure | 3 +- trunk/ide/srs_upp/srs_upp.upp | 2 + trunk/ide/srs_vs2010/srs.vcxproj | 2 + trunk/ide/srs_vs2010/srs.vcxproj.filters | 6 +++ trunk/src/app/srs_app_mpegts_udp.cpp | 57 ++++++++++++++++++++++ trunk/src/app/srs_app_mpegts_udp.hpp | 62 ++++++++++++++++++++++++ trunk/src/app/srs_app_server.cpp | 25 ++++------ trunk/src/app/srs_app_server.hpp | 20 +++----- 8 files changed, 148 insertions(+), 29 deletions(-) create mode 100644 trunk/src/app/srs_app_mpegts_udp.cpp create mode 100644 trunk/src/app/srs_app_mpegts_udp.hpp diff --git a/trunk/configure b/trunk/configure index 0ece47b2f..bea407f07 100755 --- a/trunk/configure +++ b/trunk/configure @@ -390,7 +390,8 @@ if [ $SRS_EXPORT_LIBRTMP_PROJECT = NO ]; then "srs_app_pithy_print" "srs_app_reload" "srs_app_http_api" "srs_app_http_conn" "srs_app_http_hooks" "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_recv_thread" "srs_app_security" "srs_app_statistic" + "srs_app_mpegts_udp") 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 9bfc97ce6..1694cb6f6 100755 --- a/trunk/ide/srs_upp/srs_upp.upp +++ b/trunk/ide/srs_upp/srs_upp.upp @@ -102,6 +102,8 @@ file ../../src/app/srs_app_kbps.cpp, ../../src/app/srs_app_log.hpp, ../../src/app/srs_app_log.cpp, + ../../src/app/srs_app_mpegts_udp.hpp, + ../../src/app/srs_app_mpegts_udp.cpp, ../../src/app/srs_app_recv_thread.hpp, ../../src/app/srs_app_recv_thread.cpp, ../../src/app/srs_app_refer.hpp, diff --git a/trunk/ide/srs_vs2010/srs.vcxproj b/trunk/ide/srs_vs2010/srs.vcxproj index 16ed50fea..fbebea036 100755 --- a/trunk/ide/srs_vs2010/srs.vcxproj +++ b/trunk/ide/srs_vs2010/srs.vcxproj @@ -83,6 +83,7 @@ + @@ -158,6 +159,7 @@ + diff --git a/trunk/ide/srs_vs2010/srs.vcxproj.filters b/trunk/ide/srs_vs2010/srs.vcxproj.filters index 1d78e4abc..6b03987fb 100755 --- a/trunk/ide/srs_vs2010/srs.vcxproj.filters +++ b/trunk/ide/srs_vs2010/srs.vcxproj.filters @@ -223,6 +223,9 @@ srs + + srs + @@ -408,6 +411,9 @@ srs + + srs + diff --git a/trunk/src/app/srs_app_mpegts_udp.cpp b/trunk/src/app/srs_app_mpegts_udp.cpp new file mode 100644 index 000000000..af05926c6 --- /dev/null +++ b/trunk/src/app/srs_app_mpegts_udp.cpp @@ -0,0 +1,57 @@ +/* +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 +#include +#include + +#include +#include +#include + +#ifdef SRS_AUTO_STREAM_CASTER + +SrsMpegtsOverUdp::SrsMpegtsOverUdp(SrsConfDirective* c) +{ +} + +SrsMpegtsOverUdp::~SrsMpegtsOverUdp() +{ +} + +int SrsMpegtsOverUdp::on_udp_packet(sockaddr_in* from, char* buf, int nb_buf) +{ + int ret = ERROR_SUCCESS; + + std::string peer_ip = inet_ntoa(from->sin_addr); + int peer_port = ntohs(from->sin_port); + + srs_info("udp: got %s:%d packet %d bytes", peer_ip.c_str(), peer_port, nb_buf); + // TODO: FIXME: implements it. + + return ret; +} + +#endif diff --git a/trunk/src/app/srs_app_mpegts_udp.hpp b/trunk/src/app/srs_app_mpegts_udp.hpp new file mode 100644 index 000000000..16d7faa0f --- /dev/null +++ b/trunk/src/app/srs_app_mpegts_udp.hpp @@ -0,0 +1,62 @@ +/* +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_MPEGTS_UDP_HPP +#define SRS_APP_MPEGTS_UDP_HPP + +/* +#include +*/ + +#include + +class sockaddr_in; + +class SrsConfDirective; + +#ifdef SRS_AUTO_STREAM_CASTER + +/** +* the mpegts over udp stream caster. +*/ +class SrsMpegtsOverUdp +{ +public: + SrsMpegtsOverUdp(SrsConfDirective* c); + virtual ~SrsMpegtsOverUdp(); +public: + /** + * when udp listener got a udp packet, notice server to process it. + * @param type, the client type, used to create concrete connection, + * for instance RTMP connection to serve client. + * @param from, the udp packet from address. + * @param buf, the udp packet bytes, user should copy if need to use. + * @param nb_buf, the size of udp packet bytes. + * @remark user should never use the buf, for it's a shared memory bytes. + */ + virtual int on_udp_packet(sockaddr_in* from, char* buf, int nb_buf); +}; + +#endif + +#endif diff --git a/trunk/src/app/srs_app_server.cpp b/trunk/src/app/srs_app_server.cpp index a45b78a04..dc5873696 100644 --- a/trunk/src/app/srs_app_server.cpp +++ b/trunk/src/app/srs_app_server.cpp @@ -44,6 +44,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #include #include #include +#include // signal defines. #define SIGNAL_RELOAD SIGHUP @@ -226,14 +227,18 @@ int SrsListener::cycle() return ret; } -SrsUdpListener::SrsUdpListener(SrsServer* server, SrsListenerType type) : SrsListener(server, type) +#ifdef SRS_AUTO_STREAM_CASTER +SrsUdpListener::SrsUdpListener(SrsServer* server, SrsListenerType type, SrsConfDirective* c) : SrsListener(server, type) { nb_buf = SRS_UDP_MAX_PACKET_SIZE; buf = new char[nb_buf]; + caster = new SrsMpegtsOverUdp(c); } SrsUdpListener::~SrsUdpListener() { + srs_freep(caster); + srs_freep(buf); } int SrsUdpListener::listen(int port) @@ -313,7 +318,7 @@ int SrsUdpListener::cycle() continue; } - if ((ret = _server->on_udp_packet(_type, &from, buf, nread)) != ERROR_SUCCESS) { + if ((ret = caster->on_udp_packet(&from, buf, nread)) != ERROR_SUCCESS) { srs_warn("handle udp packet failed. ret=%d", ret); continue; } @@ -328,6 +333,7 @@ int SrsUdpListener::cycle() return ret; } +#endif SrsSignalManager* SrsSignalManager::instance = NULL; @@ -1015,7 +1021,7 @@ int SrsServer::listen_stream_caster() 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); + listener = new SrsUdpListener(this, SrsListenerMpegTsOverUdp, stream_caster); } else { ret = ERROR_STREAM_CASTER_ENGINE; srs_error("unsupported stream caster %s. ret=%d", caster.c_str(), ret); @@ -1141,19 +1147,6 @@ int SrsServer::accept_client(SrsListenerType type, st_netfd_t client_stfd) return ret; } -int SrsServer::on_udp_packet(SrsListenerType type, sockaddr_in* from, char* buf, int nb_buf) -{ - int ret = ERROR_SUCCESS; - - std::string peer_ip = inet_ntoa(from->sin_addr); - int peer_port = ntohs(from->sin_port); - - // TODO: FIXME: implements it. - srs_warn("udp: drop %s:%d packet %d bytes", peer_ip.c_str(), peer_port, nb_buf); - - return ret; -} - int SrsServer::on_reload_listen() { return listen(); diff --git a/trunk/src/app/srs_app_server.hpp b/trunk/src/app/srs_app_server.hpp index 732d5093b..2c4b77ae0 100644 --- a/trunk/src/app/srs_app_server.hpp +++ b/trunk/src/app/srs_app_server.hpp @@ -44,7 +44,10 @@ class SrsHttpServer; class SrsIngester; class SrsHttpHeartbeat; class SrsKbps; -class sockaddr_in; +class SrsConfDirective; +#ifdef SRS_AUTO_STREAM_CASTER +class SrsMpegtsOverUdp; +#endif // listener type for server to identify the connection, // that is, use different type to process the connection. @@ -84,6 +87,7 @@ public: virtual int cycle(); }; +#ifdef SRS_AUTO_STREAM_CASTER /** * the udp listener, for udp server. */ @@ -92,8 +96,9 @@ class SrsUdpListener : public SrsListener private: char* buf; int nb_buf; + SrsMpegtsOverUdp* caster; public: - SrsUdpListener(SrsServer* server, SrsListenerType type); + SrsUdpListener(SrsServer* server, SrsListenerType type, SrsConfDirective* c); virtual ~SrsUdpListener(); public: virtual int listen(int port); @@ -101,6 +106,7 @@ public: public: virtual int cycle(); }; +#endif /** * convert signal to io, @@ -256,16 +262,6 @@ public: * @param client_stfd, the client fd in st boxed, the underlayer fd. */ virtual int accept_client(SrsListenerType type, st_netfd_t client_stfd); - /** - * when udp listener got a udp packet, notice server to process it. - * @param type, the client type, used to create concrete connection, - * for instance RTMP connection to serve client. - * @param from, the udp packet from address. - * @param buf, the udp packet bytes, user should copy if need to use. - * @param nb_buf, the size of udp packet bytes. - * @remark user should never use the buf, for it's a shared memory bytes. - */ - virtual int on_udp_packet(SrsListenerType type, sockaddr_in* from, char* buf, int nb_buf); // interface ISrsThreadHandler. public: virtual int on_reload_listen();