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