Skip to content

Commit 6463d22

Browse files
committed
for ossrs#250, add mpegts over udp stream caster class.
1 parent 7f02bfa commit 6463d22

File tree

8 files changed

+148
-29
lines changed

8 files changed

+148
-29
lines changed

trunk/configure

+2-1
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,8 @@ if [ $SRS_EXPORT_LIBRTMP_PROJECT = NO ]; then
390390
"srs_app_pithy_print" "srs_app_reload" "srs_app_http_api" "srs_app_http_conn" "srs_app_http_hooks"
391391
"srs_app_json" "srs_app_ingest" "srs_app_ffmpeg" "srs_app_utility" "srs_app_dvr" "srs_app_edge"
392392
"srs_app_kbps" "srs_app_heartbeat" "srs_app_empty" "srs_app_http_client"
393-
"srs_app_recv_thread" "srs_app_security" "srs_app_statistic")
393+
"srs_app_recv_thread" "srs_app_security" "srs_app_statistic"
394+
"srs_app_mpegts_udp")
394395
APP_INCS="src/app"; MODULE_DIR=${APP_INCS} . auto/modules.sh
395396
APP_OBJS="${MODULE_OBJS[@]}"
396397
fi

trunk/ide/srs_upp/srs_upp.upp

+2
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ file
102102
../../src/app/srs_app_kbps.cpp,
103103
../../src/app/srs_app_log.hpp,
104104
../../src/app/srs_app_log.cpp,
105+
../../src/app/srs_app_mpegts_udp.hpp,
106+
../../src/app/srs_app_mpegts_udp.cpp,
105107
../../src/app/srs_app_recv_thread.hpp,
106108
../../src/app/srs_app_recv_thread.cpp,
107109
../../src/app/srs_app_refer.hpp,

trunk/ide/srs_vs2010/srs.vcxproj

+2
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
<ClInclude Include="..\..\src\app\srs_app_json.hpp" />
8484
<ClInclude Include="..\..\src\app\srs_app_kbps.hpp" />
8585
<ClInclude Include="..\..\src\app\srs_app_log.hpp" />
86+
<ClInclude Include="..\..\src\app\srs_app_mpegts_udp.hpp" />
8687
<ClInclude Include="..\..\src\app\srs_app_pithy_print.hpp" />
8788
<ClInclude Include="..\..\src\app\srs_app_recv_thread.hpp" />
8889
<ClInclude Include="..\..\src\app\srs_app_refer.hpp" />
@@ -158,6 +159,7 @@
158159
<ClCompile Include="..\..\src\app\srs_app_json.cpp" />
159160
<ClCompile Include="..\..\src\app\srs_app_kbps.cpp" />
160161
<ClCompile Include="..\..\src\app\srs_app_log.cpp" />
162+
<ClCompile Include="..\..\src\app\srs_app_mpegts_udp.cpp" />
161163
<ClCompile Include="..\..\src\app\srs_app_pithy_print.cpp" />
162164
<ClCompile Include="..\..\src\app\srs_app_recv_thread.cpp" />
163165
<ClCompile Include="..\..\src\app\srs_app_refer.cpp" />

trunk/ide/srs_vs2010/srs.vcxproj.filters

+6
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,9 @@
223223
<ClCompile Include="..\..\src\protocol\srs_rtmp_utility.cpp">
224224
<Filter>srs</Filter>
225225
</ClCompile>
226+
<ClCompile Include="..\..\src\app\srs_app_mpegts_udp.cpp">
227+
<Filter>srs</Filter>
228+
</ClCompile>
226229
</ItemGroup>
227230
<ItemGroup>
228231
<ClInclude Include="..\..\src\app\srs_app_bandwidth.hpp">
@@ -408,6 +411,9 @@
408411
<ClInclude Include="..\..\src\protocol\srs_rtmp_utility.hpp">
409412
<Filter>srs</Filter>
410413
</ClInclude>
414+
<ClInclude Include="..\..\src\app\srs_app_mpegts_udp.hpp">
415+
<Filter>srs</Filter>
416+
</ClInclude>
411417
</ItemGroup>
412418
<ItemGroup>
413419
<Filter Include="research">

trunk/src/app/srs_app_mpegts_udp.cpp

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
The MIT License (MIT)
3+
4+
Copyright (c) 2013-2015 winlin
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy of
7+
this software and associated documentation files (the "Software"), to deal in
8+
the Software without restriction, including without limitation the rights to
9+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
10+
the Software, and to permit persons to whom the Software is furnished to do so,
11+
subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in all
14+
copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
18+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
19+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
20+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22+
*/
23+
24+
#include <srs_app_mpegts_udp.hpp>
25+
26+
#include <sys/socket.h>
27+
#include <netinet/in.h>
28+
#include <arpa/inet.h>
29+
30+
#include <srs_app_config.hpp>
31+
#include <srs_kernel_error.hpp>
32+
#include <srs_kernel_log.hpp>
33+
34+
#ifdef SRS_AUTO_STREAM_CASTER
35+
36+
SrsMpegtsOverUdp::SrsMpegtsOverUdp(SrsConfDirective* c)
37+
{
38+
}
39+
40+
SrsMpegtsOverUdp::~SrsMpegtsOverUdp()
41+
{
42+
}
43+
44+
int SrsMpegtsOverUdp::on_udp_packet(sockaddr_in* from, char* buf, int nb_buf)
45+
{
46+
int ret = ERROR_SUCCESS;
47+
48+
std::string peer_ip = inet_ntoa(from->sin_addr);
49+
int peer_port = ntohs(from->sin_port);
50+
51+
srs_info("udp: got %s:%d packet %d bytes", peer_ip.c_str(), peer_port, nb_buf);
52+
// TODO: FIXME: implements it.
53+
54+
return ret;
55+
}
56+
57+
#endif

trunk/src/app/srs_app_mpegts_udp.hpp

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
The MIT License (MIT)
3+
4+
Copyright (c) 2013-2015 winlin
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy of
7+
this software and associated documentation files (the "Software"), to deal in
8+
the Software without restriction, including without limitation the rights to
9+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
10+
the Software, and to permit persons to whom the Software is furnished to do so,
11+
subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in all
14+
copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
18+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
19+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
20+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22+
*/
23+
24+
#ifndef SRS_APP_MPEGTS_UDP_HPP
25+
#define SRS_APP_MPEGTS_UDP_HPP
26+
27+
/*
28+
#include <srs_app_mpegts_udp.hpp>
29+
*/
30+
31+
#include <srs_core.hpp>
32+
33+
class sockaddr_in;
34+
35+
class SrsConfDirective;
36+
37+
#ifdef SRS_AUTO_STREAM_CASTER
38+
39+
/**
40+
* the mpegts over udp stream caster.
41+
*/
42+
class SrsMpegtsOverUdp
43+
{
44+
public:
45+
SrsMpegtsOverUdp(SrsConfDirective* c);
46+
virtual ~SrsMpegtsOverUdp();
47+
public:
48+
/**
49+
* when udp listener got a udp packet, notice server to process it.
50+
* @param type, the client type, used to create concrete connection,
51+
* for instance RTMP connection to serve client.
52+
* @param from, the udp packet from address.
53+
* @param buf, the udp packet bytes, user should copy if need to use.
54+
* @param nb_buf, the size of udp packet bytes.
55+
* @remark user should never use the buf, for it's a shared memory bytes.
56+
*/
57+
virtual int on_udp_packet(sockaddr_in* from, char* buf, int nb_buf);
58+
};
59+
60+
#endif
61+
62+
#endif

trunk/src/app/srs_app_server.cpp

+9-16
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
4444
#include <srs_app_source.hpp>
4545
#include <srs_app_utility.hpp>
4646
#include <srs_app_heartbeat.hpp>
47+
#include <srs_app_mpegts_udp.hpp>
4748

4849
// signal defines.
4950
#define SIGNAL_RELOAD SIGHUP
@@ -226,14 +227,18 @@ int SrsListener::cycle()
226227
return ret;
227228
}
228229

229-
SrsUdpListener::SrsUdpListener(SrsServer* server, SrsListenerType type) : SrsListener(server, type)
230+
#ifdef SRS_AUTO_STREAM_CASTER
231+
SrsUdpListener::SrsUdpListener(SrsServer* server, SrsListenerType type, SrsConfDirective* c) : SrsListener(server, type)
230232
{
231233
nb_buf = SRS_UDP_MAX_PACKET_SIZE;
232234
buf = new char[nb_buf];
235+
caster = new SrsMpegtsOverUdp(c);
233236
}
234237

235238
SrsUdpListener::~SrsUdpListener()
236239
{
240+
srs_freep(caster);
241+
srs_freep(buf);
237242
}
238243

239244
int SrsUdpListener::listen(int port)
@@ -313,7 +318,7 @@ int SrsUdpListener::cycle()
313318
continue;
314319
}
315320

316-
if ((ret = _server->on_udp_packet(_type, &from, buf, nread)) != ERROR_SUCCESS) {
321+
if ((ret = caster->on_udp_packet(&from, buf, nread)) != ERROR_SUCCESS) {
317322
srs_warn("handle udp packet failed. ret=%d", ret);
318323
continue;
319324
}
@@ -328,6 +333,7 @@ int SrsUdpListener::cycle()
328333

329334
return ret;
330335
}
336+
#endif
331337

332338
SrsSignalManager* SrsSignalManager::instance = NULL;
333339

@@ -1015,7 +1021,7 @@ int SrsServer::listen_stream_caster()
10151021

10161022
std::string caster = _srs_config->get_stream_caster_engine(stream_caster);
10171023
if (caster == SRS_CONF_DEFAULT_STREAM_CASTER_MPEGTS_OVER_UDP) {
1018-
listener = new SrsUdpListener(this, SrsListenerMpegTsOverUdp);
1024+
listener = new SrsUdpListener(this, SrsListenerMpegTsOverUdp, stream_caster);
10191025
} else {
10201026
ret = ERROR_STREAM_CASTER_ENGINE;
10211027
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)
11411147
return ret;
11421148
}
11431149

1144-
int SrsServer::on_udp_packet(SrsListenerType type, sockaddr_in* from, char* buf, int nb_buf)
1145-
{
1146-
int ret = ERROR_SUCCESS;
1147-
1148-
std::string peer_ip = inet_ntoa(from->sin_addr);
1149-
int peer_port = ntohs(from->sin_port);
1150-
1151-
// TODO: FIXME: implements it.
1152-
srs_warn("udp: drop %s:%d packet %d bytes", peer_ip.c_str(), peer_port, nb_buf);
1153-
1154-
return ret;
1155-
}
1156-
11571150
int SrsServer::on_reload_listen()
11581151
{
11591152
return listen();

trunk/src/app/srs_app_server.hpp

+8-12
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,10 @@ class SrsHttpServer;
4444
class SrsIngester;
4545
class SrsHttpHeartbeat;
4646
class SrsKbps;
47-
class sockaddr_in;
47+
class SrsConfDirective;
48+
#ifdef SRS_AUTO_STREAM_CASTER
49+
class SrsMpegtsOverUdp;
50+
#endif
4851

4952
// listener type for server to identify the connection,
5053
// that is, use different type to process the connection.
@@ -84,6 +87,7 @@ class SrsListener : public ISrsThreadHandler
8487
virtual int cycle();
8588
};
8689

90+
#ifdef SRS_AUTO_STREAM_CASTER
8791
/**
8892
* the udp listener, for udp server.
8993
*/
@@ -92,15 +96,17 @@ class SrsUdpListener : public SrsListener
9296
private:
9397
char* buf;
9498
int nb_buf;
99+
SrsMpegtsOverUdp* caster;
95100
public:
96-
SrsUdpListener(SrsServer* server, SrsListenerType type);
101+
SrsUdpListener(SrsServer* server, SrsListenerType type, SrsConfDirective* c);
97102
virtual ~SrsUdpListener();
98103
public:
99104
virtual int listen(int port);
100105
// interface ISrsThreadHandler.
101106
public:
102107
virtual int cycle();
103108
};
109+
#endif
104110

105111
/**
106112
* convert signal to io,
@@ -256,16 +262,6 @@ class SrsServer : virtual public ISrsReloadHandler
256262
* @param client_stfd, the client fd in st boxed, the underlayer fd.
257263
*/
258264
virtual int accept_client(SrsListenerType type, st_netfd_t client_stfd);
259-
/**
260-
* when udp listener got a udp packet, notice server to process it.
261-
* @param type, the client type, used to create concrete connection,
262-
* for instance RTMP connection to serve client.
263-
* @param from, the udp packet from address.
264-
* @param buf, the udp packet bytes, user should copy if need to use.
265-
* @param nb_buf, the size of udp packet bytes.
266-
* @remark user should never use the buf, for it's a shared memory bytes.
267-
*/
268-
virtual int on_udp_packet(SrsListenerType type, sockaddr_in* from, char* buf, int nb_buf);
269265
// interface ISrsThreadHandler.
270266
public:
271267
virtual int on_reload_listen();

0 commit comments

Comments
 (0)