2020-04-27 11:07:24 +00:00
|
|
|
#include <uvgrtp/lib.hh>
|
|
|
|
#include <climits>
|
|
|
|
|
|
|
|
#define PAYLOAD_MAXLEN 256
|
|
|
|
#define KEY_SIZE 16
|
2020-04-29 22:46:55 +00:00
|
|
|
#define SALT_SIZE 14
|
2020-04-27 11:07:24 +00:00
|
|
|
|
2020-04-29 22:46:55 +00:00
|
|
|
/* 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 };
|
2020-04-27 11:07:24 +00:00
|
|
|
|
|
|
|
void thread_func(void)
|
|
|
|
{
|
|
|
|
/* See sending.cc for more details */
|
|
|
|
uvg_rtp::context ctx;
|
|
|
|
uvg_rtp::session *sess = ctx.create_session("127.0.0.1");
|
|
|
|
|
2020-04-29 22:46:55 +00:00
|
|
|
/* Enable SRTP and let user manage keys */
|
2020-04-27 11:07:24 +00:00
|
|
|
unsigned flags = RCE_SRTP | RCE_SRTP_KMNGMNT_USER;
|
|
|
|
|
|
|
|
/* See sending.cc for more details about create_stream() */
|
|
|
|
uvg_rtp::media_stream *recv = sess->create_stream(8889, 8888, RTP_FORMAT_GENERIC, flags);
|
|
|
|
|
2020-04-29 22:46:55 +00:00
|
|
|
/* 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);
|
2020-04-27 11:07:24 +00:00
|
|
|
|
2020-04-29 22:46:55 +00:00
|
|
|
for (;;) {
|
|
|
|
auto frame = recv->pull_frame();
|
|
|
|
fprintf(stderr, "Message: '%s'\n", frame->payload);
|
|
|
|
}
|
2020-04-27 11:07:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(void)
|
|
|
|
{
|
2020-04-29 22:46:55 +00:00
|
|
|
/* initialize SRTP key and salt */
|
2020-04-27 11:07:24 +00:00
|
|
|
for (int i = 0; i < KEY_SIZE; ++i)
|
|
|
|
key[i] = i;
|
|
|
|
|
2020-04-29 22:46:55 +00:00
|
|
|
for (int i = 0; i < SALT_SIZE; ++i)
|
|
|
|
salt[i] = i * 2;
|
|
|
|
|
2020-04-27 11:07:24 +00:00
|
|
|
/* Create separate thread for the receiver */
|
|
|
|
new std::thread(thread_func);
|
|
|
|
|
|
|
|
/* See sending.cc for more details */
|
|
|
|
uvg_rtp::context ctx;
|
|
|
|
uvg_rtp::session *sess = ctx.create_session("127.0.0.1");
|
|
|
|
|
2020-04-29 22:46:55 +00:00
|
|
|
/* Enable SRTP and let user manage keys */
|
2020-04-27 11:07:24 +00:00
|
|
|
unsigned flags = RCE_SRTP | RCE_SRTP_KMNGMNT_USER;
|
|
|
|
|
|
|
|
/* See sending.cc for more details about create_stream() */
|
|
|
|
uvg_rtp::media_stream *send = sess->create_stream(8888, 8889, RTP_FORMAT_GENERIC, flags);
|
|
|
|
|
2020-04-29 22:46:55 +00:00
|
|
|
/* 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);
|
2020-04-27 11:07:24 +00:00
|
|
|
|
|
|
|
/* All media is now encrypted/decrypted automatically */
|
2020-04-29 22:46:55 +00:00
|
|
|
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));
|
|
|
|
}
|
2020-04-27 11:07:24 +00:00
|
|
|
}
|