Commit 58e600f4 authored by Max Kellermann's avatar Max Kellermann

output/recorder: move code to _write_to_file()

parent d34e55c3
...@@ -120,32 +120,22 @@ recorder_output_finish(struct audio_output *ao) ...@@ -120,32 +120,22 @@ recorder_output_finish(struct audio_output *ao)
g_free(recorder); g_free(recorder);
} }
/**
* Writes pending data from the encoder to the output file.
*/
static bool static bool
recorder_output_encoder_to_file(struct recorder_output *recorder, recorder_write_to_file(struct recorder_output *recorder,
GError **error_r) const void *_data, size_t length,
GError **error_r)
{ {
assert(recorder->fd >= 0); assert(length > 0);
/* read from the encoder */ const int fd = recorder->fd;
size_t size = encoder_read(recorder->encoder, recorder->buffer, const uint8_t *data = (const uint8_t *)_data, *end = data + length;
sizeof(recorder->buffer));
if (size == 0)
return true;
/* write everything into the file */
size_t position = 0;
while (true) { while (true) {
ssize_t nbytes = write(recorder->fd, ssize_t nbytes = write(fd, data, end - data);
recorder->buffer + position,
size - position);
if (nbytes > 0) { if (nbytes > 0) {
position += (size_t)nbytes; data += nbytes;
if (position >= size) if (data == end)
return true; return true;
} else if (nbytes == 0) { } else if (nbytes == 0) {
/* shouldn't happen for files */ /* shouldn't happen for files */
...@@ -161,6 +151,28 @@ recorder_output_encoder_to_file(struct recorder_output *recorder, ...@@ -161,6 +151,28 @@ recorder_output_encoder_to_file(struct recorder_output *recorder,
} }
} }
/**
* Writes pending data from the encoder to the output file.
*/
static bool
recorder_output_encoder_to_file(struct recorder_output *recorder,
GError **error_r)
{
assert(recorder->fd >= 0);
/* read from the encoder */
size_t size = encoder_read(recorder->encoder, recorder->buffer,
sizeof(recorder->buffer));
if (size == 0)
return true;
/* write everything into the file */
return recorder_write_to_file(recorder, recorder->buffer, size,
error_r);
}
static bool static bool
recorder_output_open(struct audio_output *ao, recorder_output_open(struct audio_output *ao,
struct audio_format *audio_format, struct audio_format *audio_format,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment