forked from ultravideo/uvgRTP
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsending_generic.cc
95 lines (71 loc) · 3.31 KB
/
sending_generic.cc
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
#include <uvgrtp/lib.hh>
#include <climits>
#define PAYLOAD_MAXLEN (0xffff - 0x1000)
int main(void)
{
/* See sending.cc for more details */
uvgrtp::context ctx;
/* See sending.cc for more details */
uvgrtp::session *sess = ctx.create_session("127.0.0.1");
/* To enable interoperability between RTP libraries, uvgRTP won't fragment generic frames by default.
*
* If fragmentation for sender and defragmentation for receiver should be enabled,
* RCE_FRAGMENT_GENERIC flag must be passed to create_stream()
*
* This flag will split the input frame into packets of 1500 bytes and set the marker bit
* for first and last fragment. When the receiver notices a generic frame with a marker bit
* set, it knows that the RTP frame is in fact a fragment and when all fragments have been
* received, uvgRTP constructs one full RTP frame from the fragments and returns the frame to user.
*
* See sending.cc for more details about create_stream() */
uvgrtp::media_stream *send = sess->create_stream(8888, 8889, RTP_FORMAT_GENERIC, RCE_FRAGMENT_GENERIC);
uvgrtp::media_stream *recv = sess->create_stream(8889, 8888, RTP_FORMAT_GENERIC, RCE_FRAGMENT_GENERIC);
/* Notice that PAYLOAD_MAXLEN > MTU (4096 > 1500).
*
* uvgRTP fragments all generic input frames that are larger than 1500 and in the receiving end,
* it will reconstruct the full sent frame from fragments when all fragments have been received */
auto media = std::unique_ptr<uint8_t[]>(new uint8_t[PAYLOAD_MAXLEN]);
srand(time(NULL));
while (true) {
int size = (rand() % PAYLOAD_MAXLEN) + 1;
for (int i = 0; i < size; ++i)
media[i] = (i + size) % CHAR_MAX;
if (send->push_frame(media.get(), size, RTP_NO_FLAGS) != RTP_OK) {
fprintf(stderr, "Failed to send frame!\n");
return -1;
}
auto frame = recv->pull_frame();
if (memcmp(frame->payload, media.get(), size))
LOG_ERROR("frame is corrupted!");
fprintf(stderr, "received frame of size %u, rand %d\n", frame->payload_len, size);
uvgrtp::frame::dealloc_frame(frame);
}
#if 0
for (int i = 0; i < PAYLOAD_MAXLEN; ++i)
custom_media[i] = i % CHAR_MAX;
if (send->push_frame(std::move(custom_media), PAYLOAD_MAXLEN, RTP_NO_FLAGS) != RTP_OK) {
fprintf(stderr, "Failed to send frame!\n");
return -1;
}
auto frame = recv->pull_frame();
/* Verify that all packets were received without corruption */
for (int i = 0; i < PAYLOAD_MAXLEN; ++i) {
if (frame->payload[i] != (i % CHAR_MAX))
fprintf(stderr, "frame was corrupted during transfer!\n");
}
/* the frame must be destroyed manually */
(void)uvgrtp::frame::dealloc_frame(frame);
/* The input data size doesn't always have to be larger than MTU,
* i.e., you can still send small packets */
uint8_t data[5] = { 0x1, 0x5, 0xa, 0x77, 0xff };
send->push_frame(data, sizeof(data), 0);
frame = recv->pull_frame();
if (memcmp(data, frame->payload, 5))
fprintf(stderr, "frame was corrupted during transfer!\n");
/* the frame must be destroyed manually */
(void)uvgrtp::frame::dealloc_frame(frame);
#endif
/* Session must be destroyed manually */
ctx.destroy_session(sess);
return 0;
}