diff options
author | iphydf <iphydf@users.noreply.github.com> | 2016-08-19 13:07:45 +0100 |
---|---|---|
committer | iphydf <iphydf@users.noreply.github.com> | 2016-08-27 01:16:14 +0100 |
commit | 13ae9e9a93a1c02fad9475002c0391b86b7ad7bb (patch) | |
tree | a9575d3582c4f40e051c93ae18dded03fdddc432 /toxav/audio.c | |
parent | 1f25fc0ae417bfc47dea4966cb5e43689aa88d5c (diff) |
Move logging to a callback.
This removes the global logger (which by the way was deleted when the first tox
was killed, so other toxes would then stop logging). Various bits of the code
now carry a logger or pass it around. It's a bit less transparent now, but now
there is no need to have a global logger, and clients can decide what to log and
where.
Diffstat (limited to 'toxav/audio.c')
-rw-r--r-- | toxav/audio.c | 80 |
1 files changed, 43 insertions, 37 deletions
diff --git a/toxav/audio.c b/toxav/audio.c index ad543502..9fd8f6f1 100644 --- a/toxav/audio.c +++ b/toxav/audio.c | |||
@@ -33,26 +33,26 @@ | |||
33 | static struct JitterBuffer *jbuf_new(uint32_t capacity); | 33 | static struct JitterBuffer *jbuf_new(uint32_t capacity); |
34 | static void jbuf_clear(struct JitterBuffer *q); | 34 | static void jbuf_clear(struct JitterBuffer *q); |
35 | static void jbuf_free(struct JitterBuffer *q); | 35 | static void jbuf_free(struct JitterBuffer *q); |
36 | static int jbuf_write(struct JitterBuffer *q, struct RTPMessage *m); | 36 | static int jbuf_write(Logger *log, struct JitterBuffer *q, struct RTPMessage *m); |
37 | static struct RTPMessage *jbuf_read(struct JitterBuffer *q, int32_t *success); | 37 | static struct RTPMessage *jbuf_read(struct JitterBuffer *q, int32_t *success); |
38 | OpusEncoder *create_audio_encoder (int32_t bit_rate, int32_t sampling_rate, int32_t channel_count); | 38 | OpusEncoder *create_audio_encoder (Logger *log, int32_t bit_rate, int32_t sampling_rate, int32_t channel_count); |
39 | bool reconfigure_audio_encoder(OpusEncoder **e, int32_t new_br, int32_t new_sr, uint8_t new_ch, | 39 | bool reconfigure_audio_encoder(Logger *log, OpusEncoder **e, int32_t new_br, int32_t new_sr, uint8_t new_ch, |
40 | int32_t *old_br, int32_t *old_sr, int32_t *old_ch); | 40 | int32_t *old_br, int32_t *old_sr, int32_t *old_ch); |
41 | bool reconfigure_audio_decoder(ACSession *ac, int32_t sampling_rate, int8_t channels); | 41 | bool reconfigure_audio_decoder(ACSession *ac, int32_t sampling_rate, int8_t channels); |
42 | 42 | ||
43 | 43 | ||
44 | 44 | ||
45 | ACSession *ac_new(ToxAV *av, uint32_t friend_number, toxav_audio_receive_frame_cb *cb, void *cb_data) | 45 | ACSession *ac_new(Logger *log, ToxAV *av, uint32_t friend_number, toxav_audio_receive_frame_cb *cb, void *cb_data) |
46 | { | 46 | { |
47 | ACSession *ac = calloc(sizeof(ACSession), 1); | 47 | ACSession *ac = calloc(sizeof(ACSession), 1); |
48 | 48 | ||
49 | if (!ac) { | 49 | if (!ac) { |
50 | LOGGER_WARNING("Allocation failed! Application might misbehave!"); | 50 | LOGGER_WARNING(log, "Allocation failed! Application might misbehave!"); |
51 | return NULL; | 51 | return NULL; |
52 | } | 52 | } |
53 | 53 | ||
54 | if (create_recursive_mutex(ac->queue_mutex) != 0) { | 54 | if (create_recursive_mutex(ac->queue_mutex) != 0) { |
55 | LOGGER_WARNING("Failed to create recursive mutex!"); | 55 | LOGGER_WARNING(log, "Failed to create recursive mutex!"); |
56 | free(ac); | 56 | free(ac); |
57 | return NULL; | 57 | return NULL; |
58 | } | 58 | } |
@@ -61,18 +61,20 @@ ACSession *ac_new(ToxAV *av, uint32_t friend_number, toxav_audio_receive_frame_c | |||
61 | ac->decoder = opus_decoder_create(48000, 2, &status); | 61 | ac->decoder = opus_decoder_create(48000, 2, &status); |
62 | 62 | ||
63 | if (status != OPUS_OK) { | 63 | if (status != OPUS_OK) { |
64 | LOGGER_ERROR("Error while starting audio decoder: %s", opus_strerror(status)); | 64 | LOGGER_ERROR(log, "Error while starting audio decoder: %s", opus_strerror(status)); |
65 | goto BASE_CLEANUP; | 65 | goto BASE_CLEANUP; |
66 | } | 66 | } |
67 | 67 | ||
68 | if (!(ac->j_buf = jbuf_new(3))) { | 68 | if (!(ac->j_buf = jbuf_new(3))) { |
69 | LOGGER_WARNING("Jitter buffer creaton failed!"); | 69 | LOGGER_WARNING(log, "Jitter buffer creaton failed!"); |
70 | opus_decoder_destroy(ac->decoder); | 70 | opus_decoder_destroy(ac->decoder); |
71 | goto BASE_CLEANUP; | 71 | goto BASE_CLEANUP; |
72 | } | 72 | } |
73 | 73 | ||
74 | ac->log = log; | ||
75 | |||
74 | /* Initialize encoders with default values */ | 76 | /* Initialize encoders with default values */ |
75 | ac->encoder = create_audio_encoder(48000, 48000, 2); | 77 | ac->encoder = create_audio_encoder(log, 48000, 48000, 2); |
76 | 78 | ||
77 | if (ac->encoder == NULL) | 79 | if (ac->encoder == NULL) |
78 | goto DECODER_CLEANUP; | 80 | goto DECODER_CLEANUP; |
@@ -117,7 +119,7 @@ void ac_kill(ACSession *ac) | |||
117 | 119 | ||
118 | pthread_mutex_destroy(ac->queue_mutex); | 120 | pthread_mutex_destroy(ac->queue_mutex); |
119 | 121 | ||
120 | LOGGER_DEBUG("Terminated audio handler: %p", ac); | 122 | LOGGER_DEBUG(ac->log, "Terminated audio handler: %p", ac); |
121 | free(ac); | 123 | free(ac); |
122 | } | 124 | } |
123 | void ac_iterate(ACSession *ac) | 125 | void ac_iterate(ACSession *ac) |
@@ -139,20 +141,24 @@ void ac_iterate(ACSession *ac) | |||
139 | pthread_mutex_unlock(ac->queue_mutex); | 141 | pthread_mutex_unlock(ac->queue_mutex); |
140 | 142 | ||
141 | if (rc == 2) { | 143 | if (rc == 2) { |
142 | LOGGER_DEBUG("OPUS correction"); | 144 | LOGGER_DEBUG(ac->log, "OPUS correction"); |
143 | int fs = (ac->lp_sampling_rate * ac->lp_frame_duration) / 1000; | 145 | int fs = (ac->lp_sampling_rate * ac->lp_frame_duration) / 1000; |
144 | rc = opus_decode(ac->decoder, NULL, 0, tmp, fs, 1); | 146 | rc = opus_decode(ac->decoder, NULL, 0, tmp, fs, 1); |
145 | } else { | 147 | } else { |
146 | /* Get values from packet and decode. */ | 148 | /* Get values from packet and decode. */ |
147 | /* NOTE: This didn't work very well | 149 | /* NOTE: This didn't work very well */ |
150 | #if 0 | ||
148 | rc = convert_bw_to_sampling_rate(opus_packet_get_bandwidth(msg->data)); | 151 | rc = convert_bw_to_sampling_rate(opus_packet_get_bandwidth(msg->data)); |
152 | |||
149 | if (rc != -1) { | 153 | if (rc != -1) { |
150 | cs->last_packet_sampling_rate = rc; | 154 | cs->last_packet_sampling_rate = rc; |
151 | } else { | 155 | } else { |
152 | LOGGER_WARNING("Failed to load packet values!"); | 156 | LOGGER_WARNING(ac->log, "Failed to load packet values!"); |
153 | rtp_free_msg(msg); | 157 | rtp_free_msg(msg); |
154 | continue; | 158 | continue; |
155 | }*/ | 159 | } |
160 | |||
161 | #endif | ||
156 | 162 | ||
157 | 163 | ||
158 | /* Pick up sampling rate from packet */ | 164 | /* Pick up sampling rate from packet */ |
@@ -165,7 +171,7 @@ void ac_iterate(ACSession *ac) | |||
165 | * it didn't work quite well. | 171 | * it didn't work quite well. |
166 | */ | 172 | */ |
167 | if (!reconfigure_audio_decoder(ac, ac->lp_sampling_rate, ac->lp_channel_count)) { | 173 | if (!reconfigure_audio_decoder(ac, ac->lp_sampling_rate, ac->lp_channel_count)) { |
168 | LOGGER_WARNING("Failed to reconfigure decoder!"); | 174 | LOGGER_WARNING(ac->log, "Failed to reconfigure decoder!"); |
169 | free(msg); | 175 | free(msg); |
170 | continue; | 176 | continue; |
171 | } | 177 | } |
@@ -175,7 +181,7 @@ void ac_iterate(ACSession *ac) | |||
175 | } | 181 | } |
176 | 182 | ||
177 | if (rc < 0) { | 183 | if (rc < 0) { |
178 | LOGGER_WARNING("Decoding error: %s", opus_strerror(rc)); | 184 | LOGGER_WARNING(ac->log, "Decoding error: %s", opus_strerror(rc)); |
179 | } else if (ac->acb.first) { | 185 | } else if (ac->acb.first) { |
180 | ac->lp_frame_duration = (rc * 1000) / ac->lp_sampling_rate; | 186 | ac->lp_frame_duration = (rc * 1000) / ac->lp_sampling_rate; |
181 | 187 | ||
@@ -193,26 +199,26 @@ int ac_queue_message(void *acp, struct RTPMessage *msg) | |||
193 | if (!acp || !msg) | 199 | if (!acp || !msg) |
194 | return -1; | 200 | return -1; |
195 | 201 | ||
202 | ACSession *ac = acp; | ||
203 | |||
196 | if ((msg->header.pt & 0x7f) == (rtp_TypeAudio + 2) % 128) { | 204 | if ((msg->header.pt & 0x7f) == (rtp_TypeAudio + 2) % 128) { |
197 | LOGGER_WARNING("Got dummy!"); | 205 | LOGGER_WARNING(ac->log, "Got dummy!"); |
198 | free(msg); | 206 | free(msg); |
199 | return 0; | 207 | return 0; |
200 | } | 208 | } |
201 | 209 | ||
202 | if ((msg->header.pt & 0x7f) != rtp_TypeAudio % 128) { | 210 | if ((msg->header.pt & 0x7f) != rtp_TypeAudio % 128) { |
203 | LOGGER_WARNING("Invalid payload type!"); | 211 | LOGGER_WARNING(ac->log, "Invalid payload type!"); |
204 | free(msg); | 212 | free(msg); |
205 | return -1; | 213 | return -1; |
206 | } | 214 | } |
207 | 215 | ||
208 | ACSession *ac = acp; | ||
209 | |||
210 | pthread_mutex_lock(ac->queue_mutex); | 216 | pthread_mutex_lock(ac->queue_mutex); |
211 | int rc = jbuf_write(ac->j_buf, msg); | 217 | int rc = jbuf_write(ac->log, ac->j_buf, msg); |
212 | pthread_mutex_unlock(ac->queue_mutex); | 218 | pthread_mutex_unlock(ac->queue_mutex); |
213 | 219 | ||
214 | if (rc == -1) { | 220 | if (rc == -1) { |
215 | LOGGER_WARNING("Could not queue the message!"); | 221 | LOGGER_WARNING(ac->log, "Could not queue the message!"); |
216 | free(msg); | 222 | free(msg); |
217 | return -1; | 223 | return -1; |
218 | } | 224 | } |
@@ -221,7 +227,7 @@ int ac_queue_message(void *acp, struct RTPMessage *msg) | |||
221 | } | 227 | } |
222 | int ac_reconfigure_encoder(ACSession *ac, int32_t bit_rate, int32_t sampling_rate, uint8_t channels) | 228 | int ac_reconfigure_encoder(ACSession *ac, int32_t bit_rate, int32_t sampling_rate, uint8_t channels) |
223 | { | 229 | { |
224 | if (!ac || !reconfigure_audio_encoder(&ac->encoder, bit_rate, | 230 | if (!ac || !reconfigure_audio_encoder(ac->log, &ac->encoder, bit_rate, |
225 | sampling_rate, channels, | 231 | sampling_rate, channels, |
226 | &ac->le_bit_rate, | 232 | &ac->le_bit_rate, |
227 | &ac->le_sample_rate, | 233 | &ac->le_sample_rate, |
@@ -279,14 +285,14 @@ static void jbuf_free(struct JitterBuffer *q) | |||
279 | free(q->queue); | 285 | free(q->queue); |
280 | free(q); | 286 | free(q); |
281 | } | 287 | } |
282 | static int jbuf_write(struct JitterBuffer *q, struct RTPMessage *m) | 288 | static int jbuf_write(Logger *log, struct JitterBuffer *q, struct RTPMessage *m) |
283 | { | 289 | { |
284 | uint16_t sequnum = m->header.sequnum; | 290 | uint16_t sequnum = m->header.sequnum; |
285 | 291 | ||
286 | unsigned int num = sequnum % q->size; | 292 | unsigned int num = sequnum % q->size; |
287 | 293 | ||
288 | if ((uint32_t)(sequnum - q->bottom) > q->size) { | 294 | if ((uint32_t)(sequnum - q->bottom) > q->size) { |
289 | LOGGER_DEBUG("Clearing filled jitter buffer: %p", q); | 295 | LOGGER_DEBUG(log, "Clearing filled jitter buffer: %p", q); |
290 | 296 | ||
291 | jbuf_clear(q); | 297 | jbuf_clear(q); |
292 | q->bottom = sequnum - q->capacity; | 298 | q->bottom = sequnum - q->capacity; |
@@ -331,20 +337,20 @@ static struct RTPMessage *jbuf_read(struct JitterBuffer *q, int32_t *success) | |||
331 | *success = 0; | 337 | *success = 0; |
332 | return NULL; | 338 | return NULL; |
333 | } | 339 | } |
334 | OpusEncoder *create_audio_encoder (int32_t bit_rate, int32_t sampling_rate, int32_t channel_count) | 340 | OpusEncoder *create_audio_encoder (Logger *log, int32_t bit_rate, int32_t sampling_rate, int32_t channel_count) |
335 | { | 341 | { |
336 | int status = OPUS_OK; | 342 | int status = OPUS_OK; |
337 | OpusEncoder *rc = opus_encoder_create(sampling_rate, channel_count, OPUS_APPLICATION_VOIP, &status); | 343 | OpusEncoder *rc = opus_encoder_create(sampling_rate, channel_count, OPUS_APPLICATION_VOIP, &status); |
338 | 344 | ||
339 | if (status != OPUS_OK) { | 345 | if (status != OPUS_OK) { |
340 | LOGGER_ERROR("Error while starting audio encoder: %s", opus_strerror(status)); | 346 | LOGGER_ERROR(log, "Error while starting audio encoder: %s", opus_strerror(status)); |
341 | return NULL; | 347 | return NULL; |
342 | } | 348 | } |
343 | 349 | ||
344 | status = opus_encoder_ctl(rc, OPUS_SET_BITRATE(bit_rate)); | 350 | status = opus_encoder_ctl(rc, OPUS_SET_BITRATE(bit_rate)); |
345 | 351 | ||
346 | if (status != OPUS_OK) { | 352 | if (status != OPUS_OK) { |
347 | LOGGER_ERROR("Error while setting encoder ctl: %s", opus_strerror(status)); | 353 | LOGGER_ERROR(log, "Error while setting encoder ctl: %s", opus_strerror(status)); |
348 | goto FAILURE; | 354 | goto FAILURE; |
349 | } | 355 | } |
350 | 356 | ||
@@ -352,7 +358,7 @@ OpusEncoder *create_audio_encoder (int32_t bit_rate, int32_t sampling_rate, int3 | |||
352 | status = opus_encoder_ctl(rc, OPUS_SET_INBAND_FEC(1)); | 358 | status = opus_encoder_ctl(rc, OPUS_SET_INBAND_FEC(1)); |
353 | 359 | ||
354 | if (status != OPUS_OK) { | 360 | if (status != OPUS_OK) { |
355 | LOGGER_ERROR("Error while setting encoder ctl: %s", opus_strerror(status)); | 361 | LOGGER_ERROR(log, "Error while setting encoder ctl: %s", opus_strerror(status)); |
356 | goto FAILURE; | 362 | goto FAILURE; |
357 | } | 363 | } |
358 | 364 | ||
@@ -363,7 +369,7 @@ OpusEncoder *create_audio_encoder (int32_t bit_rate, int32_t sampling_rate, int3 | |||
363 | status = opus_encoder_ctl(rc, OPUS_SET_PACKET_LOSS_PERC(10)); | 369 | status = opus_encoder_ctl(rc, OPUS_SET_PACKET_LOSS_PERC(10)); |
364 | 370 | ||
365 | if (status != OPUS_OK) { | 371 | if (status != OPUS_OK) { |
366 | LOGGER_ERROR("Error while setting encoder ctl: %s", opus_strerror(status)); | 372 | LOGGER_ERROR(log, "Error while setting encoder ctl: %s", opus_strerror(status)); |
367 | goto FAILURE; | 373 | goto FAILURE; |
368 | } | 374 | } |
369 | 375 | ||
@@ -371,7 +377,7 @@ OpusEncoder *create_audio_encoder (int32_t bit_rate, int32_t sampling_rate, int3 | |||
371 | status = opus_encoder_ctl(rc, OPUS_SET_COMPLEXITY(10)); | 377 | status = opus_encoder_ctl(rc, OPUS_SET_COMPLEXITY(10)); |
372 | 378 | ||
373 | if (status != OPUS_OK) { | 379 | if (status != OPUS_OK) { |
374 | LOGGER_ERROR("Error while setting encoder ctl: %s", opus_strerror(status)); | 380 | LOGGER_ERROR(log, "Error while setting encoder ctl: %s", opus_strerror(status)); |
375 | goto FAILURE; | 381 | goto FAILURE; |
376 | } | 382 | } |
377 | 383 | ||
@@ -381,12 +387,12 @@ FAILURE: | |||
381 | opus_encoder_destroy(rc); | 387 | opus_encoder_destroy(rc); |
382 | return NULL; | 388 | return NULL; |
383 | } | 389 | } |
384 | bool reconfigure_audio_encoder(OpusEncoder **e, int32_t new_br, int32_t new_sr, uint8_t new_ch, | 390 | bool reconfigure_audio_encoder(Logger *log, OpusEncoder **e, int32_t new_br, int32_t new_sr, uint8_t new_ch, |
385 | int32_t *old_br, int32_t *old_sr, int32_t *old_ch) | 391 | int32_t *old_br, int32_t *old_sr, int32_t *old_ch) |
386 | { | 392 | { |
387 | /* Values are checked in toxav.c */ | 393 | /* Values are checked in toxav.c */ |
388 | if (*old_sr != new_sr || *old_ch != new_ch) { | 394 | if (*old_sr != new_sr || *old_ch != new_ch) { |
389 | OpusEncoder *new_encoder = create_audio_encoder(new_br, new_sr, new_ch); | 395 | OpusEncoder *new_encoder = create_audio_encoder(log, new_br, new_sr, new_ch); |
390 | 396 | ||
391 | if (new_encoder == NULL) | 397 | if (new_encoder == NULL) |
392 | return false; | 398 | return false; |
@@ -399,7 +405,7 @@ bool reconfigure_audio_encoder(OpusEncoder **e, int32_t new_br, int32_t new_sr, | |||
399 | int status = opus_encoder_ctl(*e, OPUS_SET_BITRATE(new_br)); | 405 | int status = opus_encoder_ctl(*e, OPUS_SET_BITRATE(new_br)); |
400 | 406 | ||
401 | if (status != OPUS_OK) { | 407 | if (status != OPUS_OK) { |
402 | LOGGER_ERROR("Error while setting encoder ctl: %s", opus_strerror(status)); | 408 | LOGGER_ERROR(log, "Error while setting encoder ctl: %s", opus_strerror(status)); |
403 | return false; | 409 | return false; |
404 | } | 410 | } |
405 | } | 411 | } |
@@ -408,7 +414,7 @@ bool reconfigure_audio_encoder(OpusEncoder **e, int32_t new_br, int32_t new_sr, | |||
408 | *old_sr = new_sr; | 414 | *old_sr = new_sr; |
409 | *old_ch = new_ch; | 415 | *old_ch = new_ch; |
410 | 416 | ||
411 | LOGGER_DEBUG ("Reconfigured audio encoder br: %d sr: %d cc:%d", new_br, new_sr, new_ch); | 417 | LOGGER_DEBUG(log, "Reconfigured audio encoder br: %d sr: %d cc:%d", new_br, new_sr, new_ch); |
412 | return true; | 418 | return true; |
413 | } | 419 | } |
414 | bool reconfigure_audio_decoder(ACSession *ac, int32_t sampling_rate, int8_t channels) | 420 | bool reconfigure_audio_decoder(ACSession *ac, int32_t sampling_rate, int8_t channels) |
@@ -421,7 +427,7 @@ bool reconfigure_audio_decoder(ACSession *ac, int32_t sampling_rate, int8_t chan | |||
421 | OpusDecoder *new_dec = opus_decoder_create(sampling_rate, channels, &status); | 427 | OpusDecoder *new_dec = opus_decoder_create(sampling_rate, channels, &status); |
422 | 428 | ||
423 | if (status != OPUS_OK) { | 429 | if (status != OPUS_OK) { |
424 | LOGGER_ERROR("Error while starting audio decoder(%d %d): %s", sampling_rate, channels, opus_strerror(status)); | 430 | LOGGER_ERROR(ac->log, "Error while starting audio decoder(%d %d): %s", sampling_rate, channels, opus_strerror(status)); |
425 | return false; | 431 | return false; |
426 | } | 432 | } |
427 | 433 | ||
@@ -432,7 +438,7 @@ bool reconfigure_audio_decoder(ACSession *ac, int32_t sampling_rate, int8_t chan | |||
432 | opus_decoder_destroy(ac->decoder); | 438 | opus_decoder_destroy(ac->decoder); |
433 | ac->decoder = new_dec; | 439 | ac->decoder = new_dec; |
434 | 440 | ||
435 | LOGGER_DEBUG("Reconfigured audio decoder sr: %d cc: %d", sampling_rate, channels); | 441 | LOGGER_DEBUG(ac->log, "Reconfigured audio decoder sr: %d cc: %d", sampling_rate, channels); |
436 | } | 442 | } |
437 | 443 | ||
438 | return true; | 444 | return true; |