Skip to content

Commit 16006b1

Browse files
committed
pts callback for incoming audio
1 parent 1be6d28 commit 16006b1

File tree

5 files changed

+48
-9
lines changed

5 files changed

+48
-9
lines changed

toxav/audio.c

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ static bool reconfigure_audio_decoder(ACSession *ac, int32_t sampling_rate, int8
4646

4747

4848
ACSession *ac_new(Mono_Time *mono_time, const Logger *log, ToxAV *av, Tox *tox, uint32_t friend_number,
49-
toxav_audio_receive_frame_cb *cb, void *cb_data)
49+
toxav_audio_receive_frame_cb *cb, void *cb_data,
50+
toxav_audio_receive_frame_pts_cb *cb_pts, void *cb_pts_data)
5051
{
5152
ACSession *ac = (ACSession *)calloc(sizeof(ACSession), 1);
5253

@@ -111,9 +112,15 @@ ACSession *ac_new(Mono_Time *mono_time, const Logger *log, ToxAV *av, Tox *tox,
111112
ac->av = av;
112113
ac->tox = tox;
113114
ac->friend_number = friend_number;
115+
116+
// set callback
114117
ac->acb = cb;
115118
ac->acb_user_data = cb_data;
116119

120+
// set pts callback
121+
ac->acb_pts = cb_pts;
122+
ac->acb_pts_user_data = cb_pts_data;
123+
117124
return ac;
118125

119126
DECODER_CLEANUP:
@@ -330,6 +337,7 @@ uint8_t ac_iterate(ACSession *ac, uint64_t *a_r_timestamp, uint64_t *a_l_timesta
330337
}
331338

332339
uint8_t ret_value = 1;
340+
uint64_t header_pts_saved = 0;
333341

334342
pthread_mutex_lock(ac->queue_mutex);
335343

@@ -432,13 +440,14 @@ uint8_t ac_iterate(ACSession *ac, uint64_t *a_r_timestamp, uint64_t *a_l_timesta
432440

433441
// LOGGER_ERROR(ac->log, "AUDIO:TTx: %llu %lld now=%llu", header_v3->frame_record_timestamp, (long long)*a_r_timestamp, current_time_monotonic(ac->mono_time));
434442
if (header_v3->frame_record_timestamp > 0) {
443+
header_pts_saved = header_v3->frame_record_timestamp;
435444
if (*a_r_timestamp < header_v3->frame_record_timestamp) {
436445
// LOGGER_ERROR(ac->log, "AUDIO:TTx:2: %llu", header_v3->frame_record_timestamp);
437446
*a_r_timestamp = header_v3->frame_record_timestamp;
438447
*a_l_timestamp = current_time_monotonic(ac->mono_time);
439448
} else {
440449
// TODO: this should not happen here!
441-
LOGGER_DEBUG(ac->log, "AUDIO: remote timestamp older");
450+
LOGGER_API_DEBUG(ac->tox, "AUDIO: remote timestamp older");
442451
}
443452
}
444453

@@ -455,11 +464,19 @@ uint8_t ac_iterate(ACSession *ac, uint64_t *a_r_timestamp, uint64_t *a_l_timesta
455464
}
456465

457466
if (rc < 0) {
458-
LOGGER_WARNING(ac->log, "Decoding error: %s", opus_strerror(rc));
459-
} else if (ac->acb) {
460-
ac->lp_frame_duration = (rc * 1000) / ac->lp_sampling_rate;
461-
ac->acb(ac->av, ac->friend_number, ac->temp_audio_buffer, rc, ac->lp_channel_count,
462-
ac->lp_sampling_rate, ac->acb_user_data);
467+
LOGGER_API_WARNING(ac->tox, "Decoding error: %s", opus_strerror(rc));
468+
} else {
469+
if (ac->acb_pts) {
470+
ac->lp_frame_duration = (rc * 1000) / ac->lp_sampling_rate;
471+
ac->acb_pts(ac->av, ac->friend_number, ac->temp_audio_buffer, rc, ac->lp_channel_count,
472+
ac->lp_sampling_rate, ac->acb_pts_user_data, header_pts_saved);
473+
return ret_value;
474+
} else if (ac->acb) {
475+
ac->lp_frame_duration = (rc * 1000) / ac->lp_sampling_rate;
476+
ac->acb(ac->av, ac->friend_number, ac->temp_audio_buffer, rc, ac->lp_channel_count,
477+
ac->lp_sampling_rate, ac->acb_user_data);
478+
return ret_value;
479+
}
463480
}
464481

465482
return ret_value;

toxav/audio.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,14 @@ typedef struct ACSession_s {
8787
/* Audio frame receive callback */
8888
toxav_audio_receive_frame_cb *acb;
8989
void *acb_user_data;
90+
91+
toxav_audio_receive_frame_pts_cb *acb_pts;
92+
void *acb_pts_user_data;
9093
} ACSession;
9194

9295
ACSession *ac_new(Mono_Time *mono_time, const Logger *log, ToxAV *av, Tox *tox, uint32_t friend_number,
93-
toxav_audio_receive_frame_cb *cb, void *cb_data);
96+
toxav_audio_receive_frame_cb *cb, void *cb_data,
97+
toxav_audio_receive_frame_pts_cb *cb_pts, void *cb_pts_data);
9498
void ac_kill(ACSession *ac);
9599
uint8_t ac_iterate(ACSession *ac, uint64_t *a_r_timestamp, uint64_t *a_l_timestamp, uint64_t *v_r_timestamp,
96100
uint64_t *v_l_timestamp,

toxav/tox_generic.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,8 @@ struct ToxAV {
157157
/* Audio frame receive callback */
158158
toxav_audio_receive_frame_cb *acb;
159159
void *acb_user_data;
160+
toxav_audio_receive_frame_pts_cb *acb_pts;
161+
void *acb_pts_user_data;
160162
/* Video frame receive callback */
161163
toxav_video_receive_frame_cb *vcb;
162164
void *vcb_user_data;

toxav/toxav.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1990,6 +1990,14 @@ void toxav_callback_audio_receive_frame(ToxAV *av, toxav_audio_receive_frame_cb
19901990
pthread_mutex_unlock(av->mutex);
19911991
}
19921992

1993+
void toxav_callback_audio_receive_frame_pts(ToxAV *av, toxav_audio_receive_frame_pts_cb *callback, void *user_data)
1994+
{
1995+
pthread_mutex_lock(av->mutex);
1996+
av->acb_pts = callback;
1997+
av->acb_pts_user_data = user_data;
1998+
pthread_mutex_unlock(av->mutex);
1999+
}
2000+
19932001
void toxav_callback_video_receive_frame(ToxAV *av, toxav_video_receive_frame_cb *callback, void *user_data)
19942002
{
19952003
pthread_mutex_lock(av->mutex);
@@ -2580,7 +2588,9 @@ static bool call_prepare_transmission(ToxAVCall *call)
25802588
call->bwc = bwc_new(av->tox, av->toxav_mono_time, call->friend_number, callback_bwc, call);
25812589

25822590
{ /* Prepare audio */
2583-
call->audio = ac_new(av->toxav_mono_time, nullptr, av, av->tox, call->friend_number, av->acb, av->acb_user_data);
2591+
call->audio = ac_new(av->toxav_mono_time, nullptr, av, av->tox, call->friend_number,
2592+
av->acb, av->acb_user_data,
2593+
av->acb_pts, av->acb_pts_user_data);
25842594

25852595
if (!call->audio) {
25862596
LOGGER_API_ERROR(av->tox, "Failed to create audio codec session");

toxav/toxav.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -791,6 +791,12 @@ typedef void toxav_audio_receive_frame_cb(ToxAV *av, uint32_t friend_number, con
791791
*/
792792
void toxav_callback_audio_receive_frame(ToxAV *av, toxav_audio_receive_frame_cb *callback, void *user_data);
793793

794+
795+
typedef void toxav_audio_receive_frame_pts_cb(ToxAV *av, uint32_t friend_number, const int16_t *pcm, size_t sample_count,
796+
uint8_t channels, uint32_t sampling_rate, void *user_data, uint64_t pts);
797+
798+
void toxav_callback_audio_receive_frame_pts(ToxAV *av, toxav_audio_receive_frame_pts_cb *callback, void *user_data);
799+
794800
/**
795801
* The function type for the video_receive_frame callback.
796802
*

0 commit comments

Comments
 (0)