summaryrefslogtreecommitdiff
path: root/toxav
diff options
context:
space:
mode:
Diffstat (limited to 'toxav')
-rw-r--r--toxav/av_test.c11
-rw-r--r--toxav/codec.c67
-rw-r--r--toxav/codec.h2
-rw-r--r--toxav/toxav.c1
4 files changed, 75 insertions, 6 deletions
diff --git a/toxav/av_test.c b/toxav/av_test.c
index 9be648f6..7948445d 100644
--- a/toxav/av_test.c
+++ b/toxav/av_test.c
@@ -26,6 +26,8 @@
26#define c_sleep(x) usleep(1000*x) 26#define c_sleep(x) usleep(1000*x)
27#endif 27#endif
28 28
29#define MIN(a,b) (((a)<(b))?(a):(b))
30
29/* Enable/disable tests */ 31/* Enable/disable tests */
30#define TEST_REGULAR_AV 0 32#define TEST_REGULAR_AV 0
31#define TEST_REGULAR_A 0 33#define TEST_REGULAR_A 0
@@ -156,12 +158,15 @@ void iterate(Tox* Bsn, ToxAV* AliceAV, ToxAV* BobAV)
156 toxav_iteration(AliceAV); 158 toxav_iteration(AliceAV);
157 toxav_iteration(BobAV); 159 toxav_iteration(BobAV);
158 160
159 c_sleep(toxav_iteration_interval(AliceAV)); 161 int mina = MIN(tox_do_interval(toxav_get_tox(AliceAV)), toxav_iteration_interval(AliceAV));
162 int minb = MIN(tox_do_interval(toxav_get_tox(BobAV)), toxav_iteration_interval(BobAV));
163
164 c_sleep(MIN(mina, minb));
160} 165}
161 166
162int device_read_frame(ALCdevice* device, int32_t frame_dur, int16_t* PCM, size_t max_size) 167int device_read_frame(ALCdevice* device, int32_t frame_dur, int16_t* PCM, size_t max_size)
163{ 168{
164 int f_size = (48000 * frame_dur / 1000); 169 int f_size = (8000 * frame_dur / 1000);
165 170
166 if (max_size < f_size) 171 if (max_size < f_size)
167 return -1; 172 return -1;
@@ -645,7 +650,7 @@ int main (int argc, char** argv)
645 int frame_size = device_read_frame(in_device, 20, PCM, sizeof(PCM)); 650 int frame_size = device_read_frame(in_device, 20, PCM, sizeof(PCM));
646 if (frame_size > 0) { 651 if (frame_size > 0) {
647 TOXAV_ERR_SEND_FRAME rc; 652 TOXAV_ERR_SEND_FRAME rc;
648 if (toxav_send_audio_frame(AliceAV, 0, PCM, frame_size, 2, 48000, &rc) == false) { 653 if (toxav_send_audio_frame(AliceAV, 0, PCM, frame_size, 2, 8000, &rc) == false) {
649 printf("Error sending frame of size %d: %d\n", frame_size, rc); 654 printf("Error sending frame of size %d: %d\n", frame_size, rc);
650 exit (1); 655 exit (1);
651 } 656 }
diff --git a/toxav/codec.c b/toxav/codec.c
index c3cb2622..a72b2764 100644
--- a/toxav/codec.c
+++ b/toxav/codec.c
@@ -237,6 +237,61 @@ static int convert_bw_to_sampling_rate(int bw)
237} 237}
238 238
239 239
240int cs_set_receiving_audio_bitrate(CSSession *cs, int32_t rate)
241{
242 if (cs->audio_decoder == NULL)
243 return -1;
244
245 int rc = opus_decoder_ctl(cs->audio_decoder, OPUS_SET_BITRATE(rate));
246
247 if ( rc != OPUS_OK ) {
248 LOGGER_ERROR("Error while setting decoder ctl: %s", opus_strerror(rc));
249 return -1;
250 }
251
252 LOGGER_DEBUG("Set new decoder bitrate to: %d", rate);
253 return 0;
254}
255
256int cs_set_receiving_audio_sampling_rate(CSSession* cs, int32_t rate)
257{
258 /* TODO Find a better way? */
259 if (cs->audio_decoder == NULL)
260 return -1;
261
262 if (cs->decoder_sample_rate == rate)
263 return 0;
264
265 int channels = cs->decoder_channels;
266
267 cs_disable_audio_receiving(cs);
268
269 cs->decoder_sample_rate = rate;
270 cs->decoder_channels = channels;
271
272 LOGGER_DEBUG("Set new encoder sampling rate: %d", rate);
273 return cs_enable_audio_receiving(cs);
274}
275
276int cs_set_receiving_audio_channels(CSSession* cs, int32_t count)
277{
278 /* TODO Find a better way? */
279 if (cs->audio_decoder == NULL)
280 return -1;
281
282 if (cs->decoder_channels == count)
283 return 0;
284
285 int srate = cs->decoder_sample_rate;
286 cs_disable_audio_receiving(cs);
287
288 cs->decoder_channels = count;
289 cs->decoder_sample_rate = srate;
290
291 LOGGER_DEBUG("Set new encoder channel count: %d", count);
292 return cs_enable_audio_receiving(cs);
293}
294
240 295
241/* PUBLIC */ 296/* PUBLIC */
242 297
@@ -284,6 +339,9 @@ void cs_do(CSSession *cs)
284 continue; 339 continue;
285 } 340 }
286 341
342 cs_set_receiving_audio_sampling_rate(cs, cs->last_packet_sampling_rate);
343 cs_set_receiving_audio_channels(cs, cs->last_pack_channels);
344
287 LOGGER_DEBUG("Decoding packet of length: %d", msg->length); 345 LOGGER_DEBUG("Decoding packet of length: %d", msg->length);
288 rc = opus_decode(cs->audio_decoder, msg->data, msg->length, tmp, fsize, 0); 346 rc = opus_decode(cs->audio_decoder, msg->data, msg->length, tmp, fsize, 0);
289 rtp_free_msg(NULL, msg); 347 rtp_free_msg(NULL, msg);
@@ -353,6 +411,8 @@ CSSession *cs_new(uint32_t peer_video_frame_piece_size)
353 411
354 cs->peer_video_frame_piece_size = peer_video_frame_piece_size; 412 cs->peer_video_frame_piece_size = peer_video_frame_piece_size;
355 413
414 cs->decoder_sample_rate = 48000;
415 cs->decoder_channels = 2;
356 return cs; 416 return cs;
357} 417}
358 418
@@ -676,6 +736,8 @@ void cs_disable_audio_receiving(CSSession* cs)
676 * To avoid unecessary checking we set this to 500 736 * To avoid unecessary checking we set this to 500
677 */ 737 */
678 cs->last_packet_frame_duration = 500; 738 cs->last_packet_frame_duration = 500;
739 cs->decoder_sample_rate = 48000;
740 cs->decoder_channels = 2;
679 } 741 }
680} 742}
681 743
@@ -721,9 +783,9 @@ int cs_enable_audio_receiving(CSSession* cs)
721{ 783{
722 if (cs->audio_decoder) 784 if (cs->audio_decoder)
723 return 0; 785 return 0;
724 786
725 int rc; 787 int rc;
726 cs->audio_decoder = opus_decoder_create(48000, 2, &rc ); 788 cs->audio_decoder = opus_decoder_create(cs->decoder_sample_rate, cs->decoder_channels, &rc );
727 789
728 if ( rc != OPUS_OK ) { 790 if ( rc != OPUS_OK ) {
729 LOGGER_ERROR("Error while starting audio decoder: %s", opus_strerror(rc)); 791 LOGGER_ERROR("Error while starting audio decoder: %s", opus_strerror(rc));
@@ -742,6 +804,7 @@ int cs_enable_audio_receiving(CSSession* cs)
742 * To avoid unecessary checking we set this to 500 804 * To avoid unecessary checking we set this to 500
743 */ 805 */
744 cs->last_packet_frame_duration = 500; 806 cs->last_packet_frame_duration = 500;
807
745 return 0; 808 return 0;
746} 809}
747 810
diff --git a/toxav/codec.h b/toxav/codec.h
index 971a9e05..5e015f39 100644
--- a/toxav/codec.h
+++ b/toxav/codec.h
@@ -112,6 +112,8 @@ typedef struct CSSession_s {
112 112
113 /* audio decoding */ 113 /* audio decoding */
114 OpusDecoder *audio_decoder; 114 OpusDecoder *audio_decoder;
115 int32_t decoder_channels;
116 int32_t decoder_sample_rate;
115 int32_t last_pack_channels; 117 int32_t last_pack_channels;
116 int32_t last_packet_sampling_rate; 118 int32_t last_packet_sampling_rate;
117 int32_t last_packet_frame_duration; 119 int32_t last_packet_frame_duration;
diff --git a/toxav/toxav.c b/toxav/toxav.c
index 850c2fc7..df6fa833 100644
--- a/toxav/toxav.c
+++ b/toxav/toxav.c
@@ -650,7 +650,6 @@ bool toxav_send_audio_frame(ToxAV* av, uint32_t friend_number, const int16_t* pc
650 } 650 }
651 651
652 { /* Encode and send */ 652 { /* Encode and send */
653 /* TODO redundant? */
654 cs_set_sending_audio_channels(call->cs, channels); 653 cs_set_sending_audio_channels(call->cs, channels);
655 cs_set_sending_audio_sampling_rate(call->cs, sampling_rate); 654 cs_set_sending_audio_sampling_rate(call->cs, sampling_rate);
656 655