Skip to content

Commit

Permalink
for ossrs#250, add mpegts over udp stream caster class.
Browse files Browse the repository at this point in the history
  • Loading branch information
winlinvip committed Jan 24, 2015
1 parent 7f02bfa commit 6463d22
Show file tree
Hide file tree
Showing 8 changed files with 148 additions and 29 deletions.
3 changes: 2 additions & 1 deletion trunk/configure
Original file line number Diff line number Diff line change
Expand Up @@ -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
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_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,
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 @@ -83,6 +83,7 @@
<ClInclude Include="..\..\src\app\srs_app_json.hpp" />
<ClInclude Include="..\..\src\app\srs_app_kbps.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" />
<ClInclude Include="..\..\src\app\srs_app_recv_thread.hpp" />
<ClInclude Include="..\..\src\app\srs_app_refer.hpp" />
Expand Down Expand Up @@ -158,6 +159,7 @@
<ClCompile Include="..\..\src\app\srs_app_json.cpp" />
<ClCompile Include="..\..\src\app\srs_app_kbps.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" />
<ClCompile Include="..\..\src\app\srs_app_recv_thread.cpp" />
<ClCompile Include="..\..\src\app\srs_app_refer.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 @@ -223,6 +223,9 @@
<ClCompile Include="..\..\src\protocol\srs_rtmp_utility.cpp">
<Filter>srs</Filter>
</ClCompile>
<ClCompile Include="..\..\src\app\srs_app_mpegts_udp.cpp">
<Filter>srs</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\src\app\srs_app_bandwidth.hpp">
Expand Down Expand Up @@ -408,6 +411,9 @@
<ClInclude Include="..\..\src\protocol\srs_rtmp_utility.hpp">
<Filter>srs</Filter>
</ClInclude>
<ClInclude Include="..\..\src\app\srs_app_mpegts_udp.hpp">
<Filter>srs</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<Filter Include="research">
Expand Down
57 changes: 57 additions & 0 deletions trunk/src/app/srs_app_mpegts_udp.cpp
Original file line number Diff line number Diff line change
@@ -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 <srs_app_mpegts_udp.hpp>

#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include <srs_app_config.hpp>
#include <srs_kernel_error.hpp>
#include <srs_kernel_log.hpp>

#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
62 changes: 62 additions & 0 deletions trunk/src/app/srs_app_mpegts_udp.hpp
Original file line number Diff line number Diff line change
@@ -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 <srs_app_mpegts_udp.hpp>
*/

#include <srs_core.hpp>

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
25 changes: 9 additions & 16 deletions trunk/src/app/srs_app_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#include <srs_app_source.hpp>
#include <srs_app_utility.hpp>
#include <srs_app_heartbeat.hpp>
#include <srs_app_mpegts_udp.hpp>

// signal defines.
#define SIGNAL_RELOAD SIGHUP
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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;
}
Expand All @@ -328,6 +333,7 @@ int SrsUdpListener::cycle()

return ret;
}
#endif

SrsSignalManager* SrsSignalManager::instance = NULL;

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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();
Expand Down
20 changes: 8 additions & 12 deletions trunk/src/app/srs_app_server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -84,6 +87,7 @@ class SrsListener : public ISrsThreadHandler
virtual int cycle();
};

#ifdef SRS_AUTO_STREAM_CASTER
/**
* the udp listener, for udp server.
*/
Expand All @@ -92,15 +96,17 @@ 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);
// interface ISrsThreadHandler.
public:
virtual int cycle();
};
#endif

/**
* convert signal to io,
Expand Down Expand Up @@ -256,16 +262,6 @@ class SrsServer : virtual public ISrsReloadHandler
* @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();
Expand Down

0 comments on commit 6463d22

Please sign in to comment.