Skip to content

Commit 7f02bfa

Browse files
committed
for ossrs#250, the mpegts over udp stream caster framework.
1 parent 52891b4 commit 7f02bfa

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

trunk/src/app/srs_app_server.cpp

+42
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
5151
// nginx also set to 512
5252
#define SERVER_LISTEN_BACKLOG 512
5353

54+
// sleep in ms for udp recv packet.
55+
#define SRS_UDP_PACKET_RECV_CYCLE_INTERVAL_MS 0
56+
57+
// set the max packet size.
58+
#define SRS_UDP_MAX_PACKET_SIZE 65535
59+
5460
// system interval in ms,
5561
// all resolution times should be times togother,
5662
// for example, system-interval is x=1s(1000ms),
@@ -222,6 +228,8 @@ int SrsListener::cycle()
222228

223229
SrsUdpListener::SrsUdpListener(SrsServer* server, SrsListenerType type) : SrsListener(server, type)
224230
{
231+
nb_buf = SRS_UDP_MAX_PACKET_SIZE;
232+
buf = new char[nb_buf];
225233
}
226234

227235
SrsUdpListener::~SrsUdpListener()
@@ -294,6 +302,27 @@ int SrsUdpListener::cycle()
294302
// we just assert here for unknown stream caster.
295303
srs_assert(_type == SrsListenerMpegTsOverUdp);
296304

305+
for (;;) {
306+
// TODO: FIXME: support ipv6, @see man 7 ipv6
307+
sockaddr_in from;
308+
int nb_from = sizeof(sockaddr_in);
309+
int nread = 0;
310+
311+
if ((nread = st_recvfrom(stfd, buf, nb_buf, (sockaddr*)&from, &nb_from, ST_UTIME_NO_TIMEOUT)) <= 0) {
312+
srs_warn("ignore recv udp packet failed, nread=%d", nread);
313+
continue;
314+
}
315+
316+
if ((ret = _server->on_udp_packet(_type, &from, buf, nread)) != ERROR_SUCCESS) {
317+
srs_warn("handle udp packet failed. ret=%d", ret);
318+
continue;
319+
}
320+
321+
if (SRS_UDP_PACKET_RECV_CYCLE_INTERVAL_MS > 0) {
322+
st_usleep(SRS_UDP_PACKET_RECV_CYCLE_INTERVAL_MS * 1000);
323+
}
324+
}
325+
297326
// TODO: FIXME: recv udp packet.
298327
st_sleep(1);
299328

@@ -1112,6 +1141,19 @@ int SrsServer::accept_client(SrsListenerType type, st_netfd_t client_stfd)
11121141
return ret;
11131142
}
11141143

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+
11151157
int SrsServer::on_reload_listen()
11161158
{
11171159
return listen();

trunk/src/app/srs_app_server.hpp

+14
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ class SrsHttpServer;
4444
class SrsIngester;
4545
class SrsHttpHeartbeat;
4646
class SrsKbps;
47+
class sockaddr_in;
4748

4849
// listener type for server to identify the connection,
4950
// that is, use different type to process the connection.
@@ -88,6 +89,9 @@ class SrsListener : public ISrsThreadHandler
8889
*/
8990
class SrsUdpListener : public SrsListener
9091
{
92+
private:
93+
char* buf;
94+
int nb_buf;
9195
public:
9296
SrsUdpListener(SrsServer* server, SrsListenerType type);
9397
virtual ~SrsUdpListener();
@@ -252,6 +256,16 @@ class SrsServer : virtual public ISrsReloadHandler
252256
* @param client_stfd, the client fd in st boxed, the underlayer fd.
253257
*/
254258
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);
255269
// interface ISrsThreadHandler.
256270
public:
257271
virtual int on_reload_listen();

0 commit comments

Comments
 (0)