forked from Haivision/srt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
recvlive.cpp
192 lines (162 loc) · 5.15 KB
/
recvlive.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#ifndef WIN32
#include <cstdlib>
#include <netdb.h>
#else
#include <winsock2.h>
#include <ws2tcpip.h>
#endif
#include <fstream>
#include <iostream>
#include <string>
#include <cstring>
#include <srt.h>
#include <assert.h> // assert
using namespace std;
int main(int argc, char* argv[])
{
// usage: recvlive [server_port]
if ((2 < argc) || ((2 == argc) && (0 == atoi(argv[1]))))
{
cout << "usage: recvlive [server_port]" << endl;
return 0;
}
// use this function to initialize the UDT library
srt_startup();
srt_setloglevel(srt_logging::LogLevel::debug);
addrinfo hints;
addrinfo* res;
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_flags = AI_PASSIVE;
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_DGRAM;
string service("9000");
if (2 == argc)
service = argv[1];
if (0 != getaddrinfo(NULL, service.c_str(), &hints, &res))
{
cout << "illegal port number or port is busy.\n" << endl;
return 0;
}
SRTSOCKET sfd = srt_create_socket();
if (SRT_INVALID_SOCK == sfd)
{
cout << "srt_socket: " << srt_getlasterror_str() << endl;
return 0;
}
bool no = false;
if (SRT_ERROR == srt_setsockopt(sfd, 0, SRTO_RCVSYN, &no, sizeof no))
{
cout << "srt_setsockopt: " << srt_getlasterror_str() << endl;
return 0;
}
// Test the deprecated option feature here:
//srt_setsockopt(sfd, 0, SRTO_STRICTENC, &no, sizeof no);
// Windows UDP issue
// For better performance, modify HKLM\System\CurrentControlSet\Services\Afd\Parameters\FastSendDatagramThreshold
#ifdef WIN32
int mss = 1052;
srt_setsockopt(sfd, 0, SRTO_MSS, &mss, sizeof(int));
#endif
// int64_t maxbw = 5000000;
// srt_setsockopt(sfd, 0, SRTO_MAXBW, &maxbw, sizeof maxbw);
if (SRT_ERROR == srt_bind(sfd, res->ai_addr, res->ai_addrlen))
{
cout << "srt_bind: " << srt_getlasterror_str() << endl;
return 0;
}
freeaddrinfo(res);
cout << "server is ready at port: " << service << endl;
if (SRT_ERROR == srt_listen(sfd, 10))
{
cout << "srt_listen: " << srt_getlasterror_str() << endl;
return 0;
}
int epid = srt_epoll_create();
if (epid < 0)
{
cout << "srt_epoll_create: " << srt_getlasterror_str() << endl;
return 0;
}
int events = SRT_EPOLL_IN | SRT_EPOLL_ERR;
if (SRT_ERROR == srt_epoll_add_usock(epid, sfd, &events))
{
cout << "srt_epoll_add_usock: " << srt_getlasterror_str() << endl;
return 0;
}
const int srtrfdslenmax = 100;
SRTSOCKET srtrfds[srtrfdslenmax];
char data[1500];
// the event loop
while (true)
{
int srtrfdslen = srtrfdslenmax;
int n = srt_epoll_wait(epid, &srtrfds[0], &srtrfdslen, 0, 0, 100, 0, 0, 0, 0);
assert(n <= srtrfdslen);
for (int i = 0; i < n; i++)
{
SRTSOCKET s = srtrfds[i];
SRT_SOCKSTATUS status = srt_getsockstate(s);
if ((status == SRTS_BROKEN) ||
(status == SRTS_NONEXIST) ||
(status == SRTS_CLOSED))
{
cout << "source disconnected. status=" << status << endl;
srt_close(s);
continue;
}
else if (s == sfd)
{
assert(status == SRTS_LISTENING);
SRTSOCKET fhandle;
sockaddr_storage clientaddr;
int addrlen = sizeof(clientaddr);
fhandle = srt_accept(sfd, (sockaddr*)&clientaddr, &addrlen);
if (SRT_INVALID_SOCK == fhandle)
{
cout << "srt_accept: " << srt_getlasterror_str() << endl;
return 0;
}
char clienthost[NI_MAXHOST];
char clientservice[NI_MAXSERV];
getnameinfo((sockaddr *)&clientaddr, addrlen,
clienthost, sizeof(clienthost),
clientservice, sizeof(clientservice), NI_NUMERICHOST|NI_NUMERICSERV);
cout << "new connection: " << clienthost << ":" << clientservice << endl;
int events = SRT_EPOLL_IN | SRT_EPOLL_ERR;
if (SRT_ERROR == srt_epoll_add_usock(epid, fhandle, &events))
{
cout << "srt_epoll_add_usock: " << srt_getlasterror_str() << endl;
return 0;
}
}
else
{
while (true)
{
int ret = srt_recvmsg(s, data, sizeof(data));
if (SRT_ERROR == ret)
{
// EAGAIN for SRT READING
if (SRT_EASYNCRCV != srt_getlasterror(NULL))
{
cout << "srt_recvmsg: " << srt_getlasterror_str() << endl;
return 0;
}
break;
}
// cout << ret << " bytes received" << endl;
}
}
}
}
srt_close(sfd);
srt_epoll_release(epid);
// use this function to release the UDT library
srt_cleanup();
return 0;
}
// Local Variables:
// c-file-style: "ellemtel"
// c-basic-offset: 3
// compile-command: "g++ -Wall -O2 -std=c++11 -I.. -I../srtcore -o recvlive recvlive.cpp -L.. -lsrt -lpthread -L/usr/local/opt/openssl/lib -lssl -lcrypto"
// End: