-
Notifications
You must be signed in to change notification settings - Fork 94
/
srtp_zrtp.cc
73 lines (56 loc) · 2.29 KB
/
srtp_zrtp.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
#include <uvgrtp/lib.hh>
#include <climits>
#include <cstring>
#include <iostream>
constexpr int AMOUNT_OF_TEST_PACKETS = 30;
constexpr auto END_WAIT = std::chrono::seconds(5);
void thread_func(void)
{
/* See sending.cc for more details */
uvgrtp::context ctx;
uvgrtp::session *sess = ctx.create_session("127.0.0.1");
/* Enable SRTP and use ZRTP to manage keys */
unsigned flags = RCE_SRTP | RCE_SRTP_KMNGMNT_ZRTP;
/* See sending.cc for more details about create_stream() */
uvgrtp::media_stream *recv = sess->create_stream(8889, 8888, RTP_FORMAT_GENERIC, flags);
for (;;) {
auto frame = recv->pull_frame();
std::string payload = std::string((char*)frame->payload, frame->payload_len);
std::cout << "Received SRTP frame. Payload: " << payload << std::endl;
/* the frame must be destroyed manually */
(void)uvgrtp::frame::dealloc_frame(frame);
}
}
int main(void)
{
/* Create separate thread for the receiver
*
* Because we're using ZRTP for SRTP key management,
* the receiver and sender must communicate with each other
* before the actual media communication starts */
new std::thread(thread_func);
/* See sending.cc for more details */
uvgrtp::context ctx;
uvgrtp::session *sess = ctx.create_session("127.0.0.1");
/* Enable SRTP and use ZRTP to manage keys */
unsigned flags = RCE_SRTP | RCE_SRTP_KMNGMNT_ZRTP;
/* See sending.cc for more details about create_stream() */
uvgrtp::media_stream *send = sess->create_stream(8888, 8889, RTP_FORMAT_GENERIC, flags);
char *message = (char *)"Hello, world!";
size_t msg_len = strlen(message);
for (int i = 0; i < AMOUNT_OF_TEST_PACKETS; ++i) {
uint8_t* message_data = new uint8_t[msg_len];
memcpy(message_data, message, msg_len);
send->push_frame((uint8_t *)message_data, msg_len, RTP_NO_FLAGS);
std::this_thread::sleep_for(std::chrono::milliseconds(500));
}
std::cout << "All packets sent, waiting " << END_WAIT.count()
<< " seconds before exiting." << std::endl;
// destroy the session after waiting for END_WAIT seconds
std::this_thread::sleep_for(END_WAIT);
sess->destroy_stream(send);
if (sess) {
ctx.destroy_session(sess);
}
return EXIT_SUCCESS;
}