static int init(int argc, char **argv) {
snd_output_stdio_attach(&output, stdout, 0);
-
// debug(2,"audio_alsa init called.");
int response = 0; // this will be what we return to the caller.
const char *str;
static void deinit(void) {
// debug(2,"audio_alsa deinit called.");
stop();
-
}
int open_alsa_device(void) {
snd_pcm_hw_params_alloca(&alsa_params);
snd_pcm_sw_params_alloca(&alsa_swparams);
-
+
ret = snd_pcm_hw_params_any(alsa_handle, alsa_params);
if (ret < 0) {
warn("audio_alsa: Broken configuration for device \"%s\": no configurations "
snd_strerror(ret));
return -9;
}
-
+
ret = snd_pcm_sw_params_current(alsa_handle, alsa_swparams);
if (ret < 0) {
warn("audio_alsa: Unable to get current sw parameters for device \"%s\": %s.", alsa_out_dev,
snd_strerror(ret));
return -11;
}
-
+
/* write the sw parameters */
ret = snd_pcm_sw_params(alsa_handle, alsa_swparams);
if (ret < 0) {
warn("audio_alsa: Unable to set software parameters of device: \"%s\": %s.", alsa_out_dev,
- snd_strerror(ret));
+ snd_strerror(ret));
return -12;
}
-
+
if (actual_buffer_length < config.audio_backend_buffer_desired_length + minimal_buffer_headroom) {
/*
// the dac buffer is too small, so let's try to set it
"length (%ld) you have chosen.",
actual_buffer_length, config.audio_backend_buffer_desired_length);
}
-
+
if (alsa_characteristics_already_listed == 0) {
alsa_characteristics_already_listed = 1;
int log_level = 2; // the level at which debug information should be output
measurement_data_is_valid = 0;
}
-int my_snd_pcm_delay(snd_pcm_t *pcm, snd_pcm_sframes_t *delayp) {
- int ret;
+int my_snd_pcm_delay(snd_pcm_t *pcm, snd_pcm_sframes_t *delayp) {
+ int ret;
snd_pcm_status_t *alsa_snd_pcm_status;
snd_pcm_status_alloca(&alsa_snd_pcm_status);
- struct timespec tn; // time now
- snd_htimestamp_t update_timestamp; //actually a struct timespec
+ struct timespec tn; // time now
+ snd_htimestamp_t update_timestamp; // actually a struct timespec
ret = snd_pcm_status(pcm, alsa_snd_pcm_status);
if (ret) {
*delayp = 0;
- return ret;
+ return ret;
}
-
+
snd_pcm_state_t state = snd_pcm_status_get_state(alsa_snd_pcm_status);
if (state != SND_PCM_STATE_RUNNING) {
*delayp = 0;
- return -EIO; // might be a better code than this...
+ return -EIO; // might be a better code than this...
}
-
+
clock_gettime(CLOCK_MONOTONIC, &tn);
snd_pcm_status_get_htstamp(alsa_snd_pcm_status, &update_timestamp);
-
- uint64_t t1 = tn.tv_sec * (uint64_t)1000000000 + tn.tv_nsec;
- uint64_t t2 = update_timestamp.tv_sec * (uint64_t)1000000000 + update_timestamp.tv_nsec;
+
+ uint64_t t1 = tn.tv_sec * (uint64_t)1000000000 + tn.tv_nsec;
+ uint64_t t2 = update_timestamp.tv_sec * (uint64_t)1000000000 + update_timestamp.tv_nsec;
uint64_t delta = t1 - t2;
-
- uint64_t frames_played_since_last_interrupt = ((uint64_t)desired_sample_rate * delta)/1000000000;
+
+ uint64_t frames_played_since_last_interrupt =
+ ((uint64_t)desired_sample_rate * delta) / 1000000000;
snd_pcm_sframes_t frames_played_since_last_interrupt_sized = frames_played_since_last_interrupt;
-
- *delayp = snd_pcm_status_get_delay(alsa_snd_pcm_status) - frames_played_since_last_interrupt_sized;
- return 0;
+
+ *delayp =
+ snd_pcm_status_get_delay(alsa_snd_pcm_status) - frames_played_since_last_interrupt_sized;
+ return 0;
}
int delay(long *the_delay) {
debug(1, "Error %d in delay(): \"%s\". Delay reported is %d frames.", reply,
snd_strerror(reply), *the_delay);
derr = snd_pcm_recover(alsa_handle, reply, 1);
- if (derr < 0)
- warn("Error %d -- could not clear an error after attempting delay(): \"%s\".", derr,
- snd_strerror(derr));
+ if (derr < 0)
+ warn("Error %d -- could not clear an error after attempting delay(): \"%s\".", derr,
+ snd_strerror(derr));
frame_index = 0;
measurement_data_is_valid = 0;
if ((derr = snd_pcm_prepare(alsa_handle))) {
debug(1, "Error preparing after delay error: \"%s\".", snd_strerror(derr));
derr = snd_pcm_recover(alsa_handle, derr, 1);
- if (derr < 0)
- warn("Error %d -- could not clear an error after attempting to recover following a delay(): \"%s\".", derr,
- snd_strerror(derr));
+ if (derr < 0)
+ warn("Error %d -- could not clear an error after attempting to recover following a "
+ "delay(): \"%s\".",
+ derr, snd_strerror(derr));
}
}
}
if (snd_pcm_state(alsa_handle) == SND_PCM_STATE_XRUN) {
if ((err = snd_pcm_prepare(alsa_handle))) {
debug(1, "Error preparing after underrun: \"%s\".", snd_strerror(err));
- err = snd_pcm_recover(alsa_handle, err, 1);
- if (err < 0)
- warn("Error %d -- could not clear an error after detecting underrun in play(): \"%s\".", err,
- snd_strerror(err));
+ err = snd_pcm_recover(alsa_handle, err, 1);
+ if (err < 0)
+ warn("Error %d -- could not clear an error after detecting underrun in play(): \"%s\".",
+ err, snd_strerror(err));
}
frame_index = 0; // we'll be starting over
measurement_data_is_valid = 0;
debug(1, "Error %d writing %d samples in play(): \"%s\".", err, samples,
snd_strerror(err));
err = snd_pcm_recover(alsa_handle, err, 1);
- if (err < 0)
- warn("Error %d -- could not clear an error after attempting to write %d samples in play(): \"%s\".", err, samples,
- snd_strerror(err));
+ if (err < 0)
+ warn("Error %d -- could not clear an error after attempting to write %d samples in "
+ "play(): \"%s\".",
+ err, samples, snd_strerror(err));
}
if (frame_index == 0) {
frames_sent_for_playing = samples;
debug(1, "Error %d in delay in play(): \"%s\". Delay reported is %d frames.", err2,
snd_strerror(err2), fl);
err2 = snd_pcm_recover(alsa_handle, err2, 1);
- if (err2 < 0)
- warn("Error %d -- could not clear an error after checking delay in play(): \"%s\".", err2,
- snd_strerror(err2));
+ if (err2 < 0)
+ warn("Error %d -- could not clear an error after checking delay in play(): \"%s\".",
+ err2, snd_strerror(err2));
frame_index = 0;
measurement_data_is_valid = 0;
} else {
if ((err = snd_pcm_prepare(alsa_handle))) {
debug(1, "Error preparing after play error: \"%s\".", snd_strerror(err));
err2 = snd_pcm_recover(alsa_handle, err, 1);
- if (err2 < 0)
- warn("Error %d -- could not clear an error after reporting ALSA device in incorrect state for play: \"%s\".", err2,
- snd_strerror(err2));
+ if (err2 < 0)
+ warn("Error %d -- could not clear an error after reporting ALSA device in incorrect "
+ "state for play: \"%s\".",
+ err2, snd_strerror(err2));
}
frame_index = 0;
measurement_data_is_valid = 0;
// now, if a flush_rtp_timestamp has been defined and the incoming timestamp is "before" it,
// drop it…
- if ((conn->flush_rtp_timestamp != 0) && (actual_timestamp != conn->flush_rtp_timestamp) &&
+ if ((conn->flush_rtp_timestamp != 0) && (actual_timestamp != conn->flush_rtp_timestamp) &&
(modulo_32_offset(actual_timestamp, conn->flush_rtp_timestamp) <
conn->input_rate * 10)) { // if it's less than 10 seconds
debug(2, "Dropping flushed packet in player_put_packet, seqno %u, timestamp %" PRIu32
conn->initial_reference_timestamp = 0;
} else {
if ((conn->flush_rtp_timestamp != 0) &&
- (modulo_32_offset(conn->flush_rtp_timestamp, actual_timestamp) > conn->input_rate/5) &&
+ (modulo_32_offset(conn->flush_rtp_timestamp, actual_timestamp) > conn->input_rate / 5) &&
(modulo_32_offset(conn->flush_rtp_timestamp, actual_timestamp) < conn->input_rate)) {
- // between 0.2 and 1 second
+ // between 0.2 and 1 second
debug(2, "Dropping flush request in player_put_packet");
conn->flush_rtp_timestamp = 0;
}
}
}
- if ((conn->flush_rtp_timestamp != 0) && (curframe->given_timestamp != conn->flush_rtp_timestamp) &&
+ if ((conn->flush_rtp_timestamp != 0) &&
+ (curframe->given_timestamp != conn->flush_rtp_timestamp) &&
(modulo_32_offset(curframe->given_timestamp, conn->flush_rtp_timestamp) <
conn->input_rate * 10)) { // if it's less than ten seconds
debug(2, "Dropping flushed packet in buffer_get_frame, seqno %u, timestamp %" PRIu32
conn->initial_reference_timestamp = 0;
}
if ((conn->flush_rtp_timestamp != 0) &&
- (modulo_32_offset(conn->flush_rtp_timestamp, curframe->given_timestamp) > conn->input_rate / 5) &&
- (modulo_32_offset(conn->flush_rtp_timestamp, curframe->given_timestamp) < conn->input_rate * 10 )) {
+ (modulo_32_offset(conn->flush_rtp_timestamp, curframe->given_timestamp) >
+ conn->input_rate / 5) &&
+ (modulo_32_offset(conn->flush_rtp_timestamp, curframe->given_timestamp) <
+ conn->input_rate * 10)) {
debug(2, "Dropping flush request in buffer_get_frame");
conn->flush_rtp_timestamp = 0;
}
int64_t rt, nt;
rt = reference_timestamp; // uint32_t to int64_t
nt = inframe->given_timestamp; // uint32_t to int64_t
- rt = rt*conn->output_sample_ratio;
- nt = nt*conn->output_sample_ratio;
+ rt = rt * conn->output_sample_ratio;
+ nt = nt * conn->output_sample_ratio;
uint64_t local_time_now = get_absolute_time_in_fp(); // types okay
// struct timespec tn;
// sync_error_out_of_bounds, sync_error);
sync_error_out_of_bounds = 0;
- int64_t filler_length = (int64_t)(config.resyncthreshold * config.output_rate); // number of samples
+ int64_t filler_length =
+ (int64_t)(config.resyncthreshold * config.output_rate); // number of samples
if ((sync_error > 0) && (sync_error > filler_length)) {
debug(2, "Large positive sync error: %" PRId64 ".", sync_error);
frames_to_drop = sync_error / conn->output_sample_ratio;
reset_input_flow_metrics(conn);
} else if ((sync_error < 0) && ((-sync_error) > filler_length)) {
- debug(2, "Large negative sync error: %" PRId64 " with should_be_frame_32 of %" PRIu32
- ", nt of %" PRId64 " and current_delay of %" PRId64 ".", sync_error, should_be_frame_32, nt, current_delay);
+ debug(2,
+ "Large negative sync error: %" PRId64 " with should_be_frame_32 of %" PRIu32
+ ", nt of %" PRId64 " and current_delay of %" PRId64 ".",
+ sync_error, should_be_frame_32, nt, current_delay);
int64_t silence_length = -sync_error;
if (silence_length > (filler_length * 5))
silence_length = filler_length * 5;
char *long_silence = malloc(conn->output_bytes_per_frame * silence_length_sized);
if (long_silence) {
memset(long_silence, 0, conn->output_bytes_per_frame * silence_length_sized);
- debug(2,"Play a silence of %d frames.",silence_length_sized);
+ debug(2, "Play a silence of %d frames.", silence_length_sized);
config.output->play(long_silence, silence_length_sized);
free(long_silence);
} else {