forked from ultravideo/uvgRTP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsrtp_user.cc
86 lines (68 loc) · 2.73 KB
/
srtp_user.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
#include <uvgrtp/lib.hh>
#include <climits>
#define PAYLOAD_MAXLEN 256
#define KEY_SIZE 16
#define SALT_SIZE 14
/* Key and salt for the SRTP session of sender and receiver
*
* NOTE: uvgRTP only supports 128 bit keys and 112 bit salts */
uint8_t key[KEY_SIZE] = { 0 };
uint8_t salt[SALT_SIZE] = { 0 };
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 let user manage keys */
unsigned flags = RCE_SRTP | RCE_SRTP_KMNGMNT_USER;
/* With user-managed keys, you have the option to use 192- and 256-bit keys.
*
* If 192- or 256-bit key size is specified in the flags, add_srtp_ctx() expects
* the key paramter to be 24 or 32 bytes long, respectively. */
if (0)
flags |= RCE_SRTP_KEYSIZE_192;
/* See sending.cc for more details about create_stream() */
uvgrtp::media_stream *recv = sess->create_stream(8889, 8888, RTP_FORMAT_GENERIC, flags);
/* Before anything else can be done,
* add_srtp_ctx() must be called with the SRTP key and salt.
*
* All calls to "recv" that try to modify and or/use the newly
* created media stream before calling add_srtp_ctx() will fail */
recv->add_srtp_ctx(key, salt);
for (;;) {
auto frame = recv->pull_frame();
fprintf(stderr, "Message: '%s'\n", frame->payload);
/* the frame must be destroyed manually */
(void)uvgrtp::frame::dealloc_frame(frame);
}
}
int main(void)
{
/* initialize SRTP key and salt */
for (int i = 0; i < KEY_SIZE; ++i)
key[i] = i;
for (int i = 0; i < SALT_SIZE; ++i)
salt[i] = i * 2;
/* Create separate thread for the receiver */
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 let user manage keys */
unsigned flags = RCE_SRTP | RCE_SRTP_KMNGMNT_USER;
/* See sending.cc for more details about create_stream() */
uvgrtp::media_stream *send = sess->create_stream(8888, 8889, RTP_FORMAT_GENERIC, flags);
/* Before anything else can be done,
* add_srtp_ctx() must be called with the SRTP key and salt.
*
* All calls to "send" that try to modify and or/use the newly
* created media stream before calling add_srtp_ctx() will fail */
send->add_srtp_ctx(key, salt);
/* All media is now encrypted/decrypted automatically */
char *message = (char *)"Hello, world!";
size_t msg_len = strlen(message);
for (;;) {
send->push_frame((uint8_t *)message, msg_len, RTP_NO_FLAGS);
std::this_thread::sleep_for(std::chrono::milliseconds(500));
}
}