summaryrefslogtreecommitdiff
path: root/toxav/codec.c
diff options
context:
space:
mode:
authormannol <eniz_vukovic@hotmail.com>2015-03-23 23:38:04 +0100
committermannol <eniz_vukovic@hotmail.com>2015-03-23 23:38:04 +0100
commit96ca88a0d63b9fd3ee1f986ab99627590fdbaa56 (patch)
tree8c6e46bbba1742598180121ff0bb73e2efb0a633 /toxav/codec.c
parent995bddbc26be5106cb33400311fbb669e314312a (diff)
Make it possible to change channels/sample rate of the decoder
Diffstat (limited to 'toxav/codec.c')
-rw-r--r--toxav/codec.c67
1 files changed, 65 insertions, 2 deletions
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