-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathincommingRemoteMediaStream.cxx
113 lines (94 loc) · 2.66 KB
/
incommingRemoteMediaStream.cxx
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
#include "srt/srtcore/srt.h"
#include "incommingRemoteMediaStream.hxx"
#include <cassert>
using namespace std;
IncommingRemoteMediaStream::IncommingRemoteMediaStream(const char* port, const char* addr)
{
mPort = port;
mAddr = addr;
}
IncommingRemoteMediaStream::~IncommingRemoteMediaStream()
{
srt_close(mServ);
srt_epoll_release(mEpid);
srt_cleanup();
}
int IncommingRemoteMediaStream::start()
{
const int no = 0;
struct sockaddr_in sa;
sa.sin_family = AF_INET;
sa.sin_port = htons(atoi(mPort));
if (inet_pton(AF_INET, mAddr, &sa.sin_addr) != 1)
return 0;
srt_startup();
mServ = srt_create_socket();
if (mServ == SRT_INVALID_SOCK) {
cout << "srt_socket: " << srt_getlasterror_str() << endl;
return 0;
}
mEpid = srt_epoll_create();
if (mEpid < 0) {
cout << "srt_epoll_create: " << srt_getlasterror_str() << endl;
return 0;
}
if (SRT_ERROR == srt_setsockflag(mServ, SRTO_RCVSYN, &no, sizeof no )
|| SRT_ERROR == srt_setsockflag(mServ, SRTO_SNDSYN, &no, sizeof no))
{
cout << "SRTO_RCVSYN or SRTO_SNDSYN: " << srt_getlasterror_str() << endl;
return 0;
}
int modes = SRT_EPOLL_OUT | SRT_EPOLL_ERR;
if (SRT_ERROR == srt_epoll_add_usock(mEpid, mServ, &modes))
{
cout << "srt_epoll_add_sock: " << srt_getlasterror_str() << endl;
return 0;
}
if (SRT_ERROR == srt_connect(mServ, (struct sockaddr*)&sa, sizeof sa))
{
cout << "srt_connect: " << srt_getlasterror_str() << endl;
return 0;
}
// wait for connection response or timeout after 1 second
int rlen = 1;
SRTSOCKET rready;
int wlen = 1;
SRTSOCKET wready;
if (srt_epoll_wait(mEpid, &rready, &rlen, &wready, &wlen, 1000, 0, 0, 0, 0) != -1)
{
SRT_SOCKSTATUS state = srt_getsockstate(mServ);
if (state != SRTS_CONNECTED || rlen > 0) // rlen > 0 - an error notification
{
cout << "srt_epoll_wait: reject reason " << srt_rejectreason_str(srt_getrejectreason(rready)) << endl;
return 0;
}
if (wlen != 1 || wready != mServ)
{
cout << "srt_epoll_wait: wlen " << wlen << ", wready " << wready << ", socket " << endl;
return 0;
}
}
else
{
cout << "srt_connect: " << srt_getlasterror_str() << endl;
return 0;
}
cout << "connected!" << endl;
char data[1500];
while (true)
{
int ret = srt_recvmsg(mServ, 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;
}
}
else
cout << ret << " bytes received" << endl;
}
return 0;
}