diff options
Diffstat (limited to 'toxav')
-rw-r--r-- | toxav/msi.c | 16 | ||||
-rw-r--r-- | toxav/rtp.c | 39 | ||||
-rw-r--r-- | toxav/toxav.c | 24 | ||||
-rw-r--r-- | toxav/toxav.h | 25 |
4 files changed, 55 insertions, 49 deletions
diff --git a/toxav/msi.c b/toxav/msi.c index 8f69d942..6cc9cf12 100644 --- a/toxav/msi.c +++ b/toxav/msi.c | |||
@@ -893,8 +893,8 @@ int handle_recv_start ( MSISession *session, MSIMessage *msg ) | |||
893 | session->call->key_peer = calloc ( sizeof ( uint8_t ), crypto_secretbox_KEYBYTES ); | 893 | session->call->key_peer = calloc ( sizeof ( uint8_t ), crypto_secretbox_KEYBYTES ); |
894 | memcpy ( session->call->key_peer, msg->cryptokey.header_value, crypto_secretbox_KEYBYTES ); | 894 | memcpy ( session->call->key_peer, msg->cryptokey.header_value, crypto_secretbox_KEYBYTES ); |
895 | 895 | ||
896 | session->call->nonce_peer = calloc ( sizeof ( uint8_t ), crypto_box_NONCEBYTES ); | 896 | session->call->nonce_peer = calloc ( sizeof ( uint8_t ), crypto_secretbox_NONCEBYTES ); |
897 | memcpy ( session->call->nonce_peer, msg->nonce.header_value, crypto_box_NONCEBYTES ); | 897 | memcpy ( session->call->nonce_peer, msg->nonce.header_value, crypto_secretbox_NONCEBYTES ); |
898 | 898 | ||
899 | flush_peer_type ( session, msg, 0 ); | 899 | flush_peer_type ( session, msg, 0 ); |
900 | 900 | ||
@@ -981,21 +981,21 @@ int handle_recv_starting ( MSISession *session, MSIMessage *msg ) | |||
981 | session->call->key_local = calloc ( sizeof ( uint8_t ), crypto_secretbox_KEYBYTES ); | 981 | session->call->key_local = calloc ( sizeof ( uint8_t ), crypto_secretbox_KEYBYTES ); |
982 | new_symmetric_key ( session->call->key_local ); | 982 | new_symmetric_key ( session->call->key_local ); |
983 | 983 | ||
984 | session->call->nonce_local = calloc ( sizeof ( uint8_t ), crypto_box_NONCEBYTES ); | 984 | session->call->nonce_local = calloc ( sizeof ( uint8_t ), crypto_secretbox_NONCEBYTES ); |
985 | new_nonce ( session->call->nonce_local ); | 985 | new_nonce ( session->call->nonce_local ); |
986 | 986 | ||
987 | /* Save peer key/nonce */ | 987 | /* Save peer key/nonce */ |
988 | session->call->key_peer = calloc ( sizeof ( uint8_t ), crypto_secretbox_KEYBYTES ); | 988 | session->call->key_peer = calloc ( sizeof ( uint8_t ), crypto_secretbox_KEYBYTES ); |
989 | memcpy ( session->call->key_peer, msg->cryptokey.header_value, crypto_secretbox_KEYBYTES ); | 989 | memcpy ( session->call->key_peer, msg->cryptokey.header_value, crypto_secretbox_KEYBYTES ); |
990 | 990 | ||
991 | session->call->nonce_peer = calloc ( sizeof ( uint8_t ), crypto_box_NONCEBYTES ); | 991 | session->call->nonce_peer = calloc ( sizeof ( uint8_t ), crypto_secretbox_NONCEBYTES ); |
992 | memcpy ( session->call->nonce_peer, msg->nonce.header_value, crypto_box_NONCEBYTES ); | 992 | memcpy ( session->call->nonce_peer, msg->nonce.header_value, crypto_secretbox_NONCEBYTES ); |
993 | 993 | ||
994 | session->call->state = call_active; | 994 | session->call->state = call_active; |
995 | 995 | ||
996 | MSIMessage *_msg_start = msi_new_message ( TYPE_REQUEST, stringify_request ( start ) ); | 996 | MSIMessage *_msg_start = msi_new_message ( TYPE_REQUEST, stringify_request ( start ) ); |
997 | msi_msg_set_cryptokey ( _msg_start, session->call->key_local, crypto_secretbox_KEYBYTES ); | 997 | msi_msg_set_cryptokey ( _msg_start, session->call->key_local, crypto_secretbox_KEYBYTES ); |
998 | msi_msg_set_nonce ( _msg_start, session->call->nonce_local, crypto_box_NONCEBYTES ); | 998 | msi_msg_set_nonce ( _msg_start, session->call->nonce_local, crypto_secretbox_NONCEBYTES ); |
999 | send_message ( session, _msg_start, msg->friend_id ); | 999 | send_message ( session, _msg_start, msg->friend_id ); |
1000 | free_message ( _msg_start ); | 1000 | free_message ( _msg_start ); |
1001 | 1001 | ||
@@ -1341,11 +1341,11 @@ int msi_answer ( MSISession *session, MSICallType call_type ) | |||
1341 | session->call->key_local = calloc ( sizeof ( uint8_t ), crypto_secretbox_KEYBYTES ); | 1341 | session->call->key_local = calloc ( sizeof ( uint8_t ), crypto_secretbox_KEYBYTES ); |
1342 | new_symmetric_key ( session->call->key_local ); | 1342 | new_symmetric_key ( session->call->key_local ); |
1343 | 1343 | ||
1344 | session->call->nonce_local = calloc ( sizeof ( uint8_t ), crypto_box_NONCEBYTES ); | 1344 | session->call->nonce_local = calloc ( sizeof ( uint8_t ), crypto_secretbox_NONCEBYTES ); |
1345 | new_nonce ( session->call->nonce_local ); | 1345 | new_nonce ( session->call->nonce_local ); |
1346 | 1346 | ||
1347 | msi_msg_set_cryptokey ( _msg_starting, session->call->key_local, crypto_secretbox_KEYBYTES ); | 1347 | msi_msg_set_cryptokey ( _msg_starting, session->call->key_local, crypto_secretbox_KEYBYTES ); |
1348 | msi_msg_set_nonce ( _msg_starting, session->call->nonce_local, crypto_box_NONCEBYTES ); | 1348 | msi_msg_set_nonce ( _msg_starting, session->call->nonce_local, crypto_secretbox_NONCEBYTES ); |
1349 | 1349 | ||
1350 | send_message ( session, _msg_starting, session->call->peers[session->call->peer_count - 1] ); | 1350 | send_message ( session, _msg_starting, session->call->peers[session->call->peer_count - 1] ); |
1351 | free_message ( _msg_starting ); | 1351 | free_message ( _msg_starting ); |
diff --git a/toxav/rtp.c b/toxav/rtp.c index dbaecbb6..8eca46d4 100644 --- a/toxav/rtp.c +++ b/toxav/rtp.c | |||
@@ -168,8 +168,8 @@ inline__ void increase_nonce(uint8_t *nonce, uint16_t target) | |||
168 | uint16_t _nonce_counter; | 168 | uint16_t _nonce_counter; |
169 | 169 | ||
170 | uint8_t _reverse_bytes[2]; | 170 | uint8_t _reverse_bytes[2]; |
171 | _reverse_bytes[0] = nonce[crypto_box_NONCEBYTES - 1]; | 171 | _reverse_bytes[0] = nonce[crypto_secretbox_NONCEBYTES - 1]; |
172 | _reverse_bytes[1] = nonce[crypto_box_NONCEBYTES - 2]; | 172 | _reverse_bytes[1] = nonce[crypto_secretbox_NONCEBYTES - 2]; |
173 | 173 | ||
174 | bytes_to_U16(&_nonce_counter, _reverse_bytes ); | 174 | bytes_to_U16(&_nonce_counter, _reverse_bytes ); |
175 | 175 | ||
@@ -177,7 +177,8 @@ inline__ void increase_nonce(uint8_t *nonce, uint16_t target) | |||
177 | if (_nonce_counter > UINT16_MAX - target ) { /* 2 bytes are not long enough */ | 177 | if (_nonce_counter > UINT16_MAX - target ) { /* 2 bytes are not long enough */ |
178 | uint8_t _it = 3; | 178 | uint8_t _it = 3; |
179 | 179 | ||
180 | while ( _it <= crypto_box_NONCEBYTES ) _it += ++nonce[crypto_box_NONCEBYTES - _it] ? crypto_box_NONCEBYTES : 1; | 180 | while ( _it <= crypto_secretbox_NONCEBYTES ) _it += ++nonce[crypto_secretbox_NONCEBYTES - _it] ? |
181 | crypto_secretbox_NONCEBYTES : 1; | ||
181 | 182 | ||
182 | _nonce_counter = _nonce_counter - (UINT16_MAX - target ); /* Assign the rest of it */ | 183 | _nonce_counter = _nonce_counter - (UINT16_MAX - target ); /* Assign the rest of it */ |
183 | } else { /* Increase nonce */ | 184 | } else { /* Increase nonce */ |
@@ -188,8 +189,8 @@ inline__ void increase_nonce(uint8_t *nonce, uint16_t target) | |||
188 | /* Assign the last bytes */ | 189 | /* Assign the last bytes */ |
189 | 190 | ||
190 | U16_to_bytes( _reverse_bytes, _nonce_counter); | 191 | U16_to_bytes( _reverse_bytes, _nonce_counter); |
191 | nonce [crypto_box_NONCEBYTES - 1] = _reverse_bytes[0]; | 192 | nonce [crypto_secretbox_NONCEBYTES - 1] = _reverse_bytes[0]; |
192 | nonce [crypto_box_NONCEBYTES - 2] = _reverse_bytes[1]; | 193 | nonce [crypto_secretbox_NONCEBYTES - 2] = _reverse_bytes[1]; |
193 | 194 | ||
194 | } | 195 | } |
195 | 196 | ||
@@ -516,7 +517,7 @@ int rtp_handle_packet ( void *object, IP_Port ip_port, uint8_t *data, uint32_t l | |||
516 | RTPSession *_session = object; | 517 | RTPSession *_session = object; |
517 | RTPMessage *_msg; | 518 | RTPMessage *_msg; |
518 | 519 | ||
519 | if ( !_session || length < 13 ) /* 12 is the minimum length for rtp + desc. byte */ | 520 | if ( !_session || length < 13 + crypto_secretbox_MACBYTES) /* 12 is the minimum length for rtp + desc. byte */ |
520 | return -1; | 521 | return -1; |
521 | 522 | ||
522 | uint8_t _plain[MAX_UDP_PACKET_SIZE]; | 523 | uint8_t _plain[MAX_UDP_PACKET_SIZE]; |
@@ -525,8 +526,8 @@ int rtp_handle_packet ( void *object, IP_Port ip_port, uint8_t *data, uint32_t l | |||
525 | bytes_to_U16(&_sequnum, data + 1); | 526 | bytes_to_U16(&_sequnum, data + 1); |
526 | 527 | ||
527 | /* Clculate the right nonce */ | 528 | /* Clculate the right nonce */ |
528 | uint8_t _calculated[crypto_box_NONCEBYTES]; | 529 | uint8_t _calculated[crypto_secretbox_NONCEBYTES]; |
529 | memcpy(_calculated, _session->decrypt_nonce, crypto_box_NONCEBYTES); | 530 | memcpy(_calculated, _session->decrypt_nonce, crypto_secretbox_NONCEBYTES); |
530 | increase_nonce ( _calculated, _sequnum ); | 531 | increase_nonce ( _calculated, _sequnum ); |
531 | 532 | ||
532 | /* Decrypt message */ | 533 | /* Decrypt message */ |
@@ -556,8 +557,8 @@ int rtp_handle_packet ( void *object, IP_Port ip_port, uint8_t *data, uint32_t l | |||
556 | if ( !_decrypted_length ) return -1; /* This is just an error */ | 557 | if ( !_decrypted_length ) return -1; /* This is just an error */ |
557 | 558 | ||
558 | /* A new cycle setting. */ | 559 | /* A new cycle setting. */ |
559 | memcpy(_session->nonce_cycle, _session->decrypt_nonce, crypto_box_NONCEBYTES); | 560 | memcpy(_session->nonce_cycle, _session->decrypt_nonce, crypto_secretbox_NONCEBYTES); |
560 | memcpy(_session->decrypt_nonce, _calculated, crypto_box_NONCEBYTES); | 561 | memcpy(_session->decrypt_nonce, _calculated, crypto_secretbox_NONCEBYTES); |
561 | } | 562 | } |
562 | } | 563 | } |
563 | 564 | ||
@@ -755,8 +756,8 @@ int rtp_send_msg ( RTPSession *session, Messenger *messenger, const uint8_t *dat | |||
755 | _send_data[0] = session->prefix; | 756 | _send_data[0] = session->prefix; |
756 | 757 | ||
757 | /* Generate the right nonce */ | 758 | /* Generate the right nonce */ |
758 | uint8_t _calculated[crypto_box_NONCEBYTES]; | 759 | uint8_t _calculated[crypto_secretbox_NONCEBYTES]; |
759 | memcpy(_calculated, session->encrypt_nonce, crypto_box_NONCEBYTES); | 760 | memcpy(_calculated, session->encrypt_nonce, crypto_secretbox_NONCEBYTES); |
760 | increase_nonce ( _calculated, msg->header->sequnum ); | 761 | increase_nonce ( _calculated, msg->header->sequnum ); |
761 | 762 | ||
762 | /* Need to skip 2 bytes that are for sequnum */ | 763 | /* Need to skip 2 bytes that are for sequnum */ |
@@ -779,7 +780,7 @@ int rtp_send_msg ( RTPSession *session, Messenger *messenger, const uint8_t *dat | |||
779 | /* Set sequ number */ | 780 | /* Set sequ number */ |
780 | if ( session->sequnum >= MAX_SEQU_NUM ) { | 781 | if ( session->sequnum >= MAX_SEQU_NUM ) { |
781 | session->sequnum = 0; | 782 | session->sequnum = 0; |
782 | memcpy(session->encrypt_nonce, _calculated, crypto_box_NONCEBYTES); | 783 | memcpy(session->encrypt_nonce, _calculated, crypto_secretbox_NONCEBYTES); |
783 | } else { | 784 | } else { |
784 | session->sequnum++; | 785 | session->sequnum++; |
785 | } | 786 | } |
@@ -874,16 +875,16 @@ RTPSession *rtp_init_session ( int payload_type, | |||
874 | _retu->decrypt_key = decrypt_key; | 875 | _retu->decrypt_key = decrypt_key; |
875 | 876 | ||
876 | /* Need to allocate new memory */ | 877 | /* Need to allocate new memory */ |
877 | _retu->encrypt_nonce = calloc ( crypto_box_NONCEBYTES, sizeof (uint8_t) ); | 878 | _retu->encrypt_nonce = calloc ( crypto_secretbox_NONCEBYTES, sizeof (uint8_t) ); |
878 | assert(_retu->encrypt_nonce); | 879 | assert(_retu->encrypt_nonce); |
879 | _retu->decrypt_nonce = calloc ( crypto_box_NONCEBYTES, sizeof (uint8_t) ); | 880 | _retu->decrypt_nonce = calloc ( crypto_secretbox_NONCEBYTES, sizeof (uint8_t) ); |
880 | assert(_retu->decrypt_nonce); | 881 | assert(_retu->decrypt_nonce); |
881 | _retu->nonce_cycle = calloc ( crypto_box_NONCEBYTES, sizeof (uint8_t) ); | 882 | _retu->nonce_cycle = calloc ( crypto_secretbox_NONCEBYTES, sizeof (uint8_t) ); |
882 | assert(_retu->nonce_cycle); | 883 | assert(_retu->nonce_cycle); |
883 | 884 | ||
884 | memcpy(_retu->encrypt_nonce, encrypt_nonce, crypto_box_NONCEBYTES); | 885 | memcpy(_retu->encrypt_nonce, encrypt_nonce, crypto_secretbox_NONCEBYTES); |
885 | memcpy(_retu->decrypt_nonce, decrypt_nonce, crypto_box_NONCEBYTES); | 886 | memcpy(_retu->decrypt_nonce, decrypt_nonce, crypto_secretbox_NONCEBYTES); |
886 | memcpy(_retu->nonce_cycle , decrypt_nonce, crypto_box_NONCEBYTES); | 887 | memcpy(_retu->nonce_cycle , decrypt_nonce, crypto_secretbox_NONCEBYTES); |
887 | 888 | ||
888 | _retu->csrc = calloc(1, sizeof (uint32_t)); | 889 | _retu->csrc = calloc(1, sizeof (uint32_t)); |
889 | assert(_retu->csrc); | 890 | assert(_retu->csrc); |
diff --git a/toxav/toxav.c b/toxav/toxav.c index f704bc6b..e0ddc731 100644 --- a/toxav/toxav.c +++ b/toxav/toxav.c | |||
@@ -35,6 +35,16 @@ | |||
35 | 35 | ||
36 | #include "toxav.h" | 36 | #include "toxav.h" |
37 | 37 | ||
38 | /* Default video bitrate in bytes/s */ | ||
39 | #define VIDEO_BITRATE (10*1000*100) | ||
40 | |||
41 | /* Default audio bitrate in bits/s */ | ||
42 | #define AUDIO_BITRATE 64000 | ||
43 | |||
44 | /* Assume 60 fps*/ | ||
45 | #define MAX_ENCODE_TIME_US ((1000 / 60) * 1000) | ||
46 | |||
47 | |||
38 | #define inline__ inline __attribute__((always_inline)) | 48 | #define inline__ inline __attribute__((always_inline)) |
39 | 49 | ||
40 | static const uint8_t audio_index = 0, video_index = 1; | 50 | static const uint8_t audio_index = 0, video_index = 1; |
@@ -474,11 +484,12 @@ inline__ int toxav_send_video ( ToxAv *av, vpx_image_t *input) | |||
474 | * @brief Receive decoded audio frame. | 484 | * @brief Receive decoded audio frame. |
475 | * | 485 | * |
476 | * @param av Handler. | 486 | * @param av Handler. |
477 | * @param frame_size ... | 487 | * @param frame_size The size of dest in frames/samples (one frame/sample is 16 bits or 2 bytes |
478 | * @param dest Destination of the packet. Make sure it has enough space for | 488 | * and corresponds to one sample of audio.) |
479 | * RTP_PAYLOAD_SIZE bytes. | 489 | * @param dest Destination of the raw audio (16 bit signed pcm with AUDIO_CHANNELS channels). |
490 | * Make sure it has enough space for frame_size frames/samples. | ||
480 | * @return int | 491 | * @return int |
481 | * @retval >=0 Size of received packet. | 492 | * @retval >=0 Size of received data in frames/samples. |
482 | * @retval ToxAvError On error. | 493 | * @retval ToxAvError On error. |
483 | */ | 494 | */ |
484 | inline__ int toxav_recv_audio ( ToxAv *av, int frame_size, int16_t *dest ) | 495 | inline__ int toxav_recv_audio ( ToxAv *av, int frame_size, int16_t *dest ) |
@@ -503,8 +514,9 @@ inline__ int toxav_recv_audio ( ToxAv *av, int frame_size, int16_t *dest ) | |||
503 | * @brief Encode and send audio frame. | 514 | * @brief Encode and send audio frame. |
504 | * | 515 | * |
505 | * @param av Handler. | 516 | * @param av Handler. |
506 | * @param frame The frame. | 517 | * @param frame The frame (raw 16 bit signed pcm with AUDIO_CHANNELS channels audio.) |
507 | * @param frame_size It's size. | 518 | * @param frame_size Its size in number of frames/samples (one frame/sample is 16 bits or 2 bytes) |
519 | * frame size should be AUDIO_FRAME_SIZE. | ||
508 | * @return int | 520 | * @return int |
509 | * @retval 0 Success. | 521 | * @retval 0 Success. |
510 | * @retval ToxAvError On error. | 522 | * @retval ToxAvError On error. |
diff --git a/toxav/toxav.h b/toxav/toxav.h index be7a2950..40cd90db 100644 --- a/toxav/toxav.h +++ b/toxav/toxav.h | |||
@@ -39,12 +39,6 @@ typedef struct Tox Tox; | |||
39 | 39 | ||
40 | #define RTP_PAYLOAD_SIZE 65535 | 40 | #define RTP_PAYLOAD_SIZE 65535 |
41 | 41 | ||
42 | /* Default video bitrate in bytes/s */ | ||
43 | #define VIDEO_BITRATE 10*1000*100 | ||
44 | |||
45 | /* Default audio bitrate in bits/s */ | ||
46 | #define AUDIO_BITRATE 64000 | ||
47 | |||
48 | /* Number of audio channels. */ | 42 | /* Number of audio channels. */ |
49 | #define AUDIO_CHANNELS 1 | 43 | #define AUDIO_CHANNELS 1 |
50 | 44 | ||
@@ -55,10 +49,7 @@ typedef struct Tox Tox; | |||
55 | #define AUDIO_SAMPLE_RATE 48000 | 49 | #define AUDIO_SAMPLE_RATE 48000 |
56 | 50 | ||
57 | /* The amount of samples in one audio frame */ | 51 | /* The amount of samples in one audio frame */ |
58 | #define AUDIO_FRAME_SIZE AUDIO_SAMPLE_RATE*AUDIO_FRAME_DURATION/1000 | 52 | #define AUDIO_FRAME_SIZE (AUDIO_SAMPLE_RATE*AUDIO_FRAME_DURATION/1000) |
59 | |||
60 | /* Assume 60 fps*/ | ||
61 | #define MAX_ENCODE_TIME_US ((1000 / 60) * 1000) | ||
62 | 53 | ||
63 | 54 | ||
64 | /** | 55 | /** |
@@ -246,11 +237,12 @@ int toxav_recv_video ( ToxAv *av, vpx_image_t **output); | |||
246 | * @brief Receive decoded audio frame. | 237 | * @brief Receive decoded audio frame. |
247 | * | 238 | * |
248 | * @param av Handler. | 239 | * @param av Handler. |
249 | * @param frame_size ... | 240 | * @param frame_size The size of dest in frames/samples (one frame/sample is 16 bits or 2 bytes |
250 | * @param dest Destination of the packet. Make sure it has enough space for | 241 | * and corresponds to one sample of audio.) |
251 | * RTP_PAYLOAD_SIZE bytes. | 242 | * @param dest Destination of the raw audio (16 bit signed pcm with AUDIO_CHANNELS channels). |
243 | * Make sure it has enough space for frame_size frames/samples. | ||
252 | * @return int | 244 | * @return int |
253 | * @retval >=0 Size of received packet. | 245 | * @retval >=0 Size of received data in frames/samples. |
254 | * @retval ToxAvError On error. | 246 | * @retval ToxAvError On error. |
255 | */ | 247 | */ |
256 | int toxav_recv_audio( ToxAv *av, int frame_size, int16_t *dest ); | 248 | int toxav_recv_audio( ToxAv *av, int frame_size, int16_t *dest ); |
@@ -270,8 +262,9 @@ int toxav_send_video ( ToxAv *av, vpx_image_t *input); | |||
270 | * @brief Encode and send audio frame. | 262 | * @brief Encode and send audio frame. |
271 | * | 263 | * |
272 | * @param av Handler. | 264 | * @param av Handler. |
273 | * @param frame The frame. | 265 | * @param frame The frame (raw 16 bit signed pcm with AUDIO_CHANNELS channels audio.) |
274 | * @param frame_size It's size. | 266 | * @param frame_size Its size in number of frames/samples (one frame/sample is 16 bits or 2 bytes) |
267 | * frame size should be AUDIO_FRAME_SIZE. | ||
275 | * @return int | 268 | * @return int |
276 | * @retval 0 Success. | 269 | * @retval 0 Success. |
277 | * @retval ToxAvError On error. | 270 | * @retval ToxAvError On error. |