Phase 13: protect recordings and recover retained audio after write failures
This commit is contained in:
parent
05140c2e93
commit
01d9db90fd
15 changed files with 308 additions and 30 deletions
|
|
@ -4,6 +4,8 @@
|
|||
#include <float.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/stat.h>
|
||||
#include <errno.h>
|
||||
|
||||
struct xtc_recorder {
|
||||
float *ring;
|
||||
|
|
@ -12,7 +14,7 @@ struct xtc_recorder {
|
|||
unsigned int error, rate;
|
||||
FILE *file;
|
||||
char *path, *temporary;
|
||||
int finished;
|
||||
int finished, replace, committed, preserve;
|
||||
};
|
||||
|
||||
/**
|
||||
|
|
@ -24,7 +26,7 @@ struct xtc_recorder {
|
|||
* @param out Receives the newly allocated object; caller owns it on success.
|
||||
* @return XTC_OK on success, or an error code describing the failure.
|
||||
*/
|
||||
xtc_result xtc_recorder_create(const char *path, unsigned int rate, size_t capacity, xtc_recorder **out)
|
||||
xtc_result xtc_recorder_create_replace(const char *path, unsigned int rate, size_t capacity, int replace, xtc_recorder **out)
|
||||
{
|
||||
xtc_recorder *r;
|
||||
size_t length = 0;
|
||||
|
|
@ -32,7 +34,7 @@ xtc_result xtc_recorder_create(const char *path, unsigned int rate, size_t capac
|
|||
if (!out)
|
||||
return XTC_ERR_INVALID_ARGUMENT;
|
||||
*out = NULL;
|
||||
if (!path || !rate || rate > 768000 || !capacity || capacity > 2097152U)
|
||||
if ((replace != 0 && replace != 1) || !path || !rate || rate > 768000 || !capacity || capacity > 2097152U)
|
||||
return XTC_ERR_INVALID_ARGUMENT;
|
||||
while (length < 4096 && path[length])
|
||||
++length;
|
||||
|
|
@ -42,9 +44,14 @@ xtc_result xtc_recorder_create(const char *path, unsigned int rate, size_t capac
|
|||
return XTC_ERR_UNSUPPORTED;
|
||||
if (!__atomic_always_lock_free(sizeof(uint64_t), 0) || !__atomic_always_lock_free(sizeof(unsigned int), 0))
|
||||
return XTC_ERR_UNSUPPORTED;
|
||||
if (!replace) {
|
||||
struct stat info;
|
||||
if (lstat(path, &info) == 0 || errno != ENOENT) return XTC_ERR_IO;
|
||||
}
|
||||
r = calloc(1, sizeof(*r));
|
||||
if (!r)
|
||||
return XTC_ERR_OUT_OF_MEMORY;
|
||||
r->replace = replace;
|
||||
r->capacity = capacity;
|
||||
r->rate = rate;
|
||||
r->ring = malloc(capacity * sizeof(float));
|
||||
|
|
@ -166,8 +173,15 @@ xtc_result xtc_recorder_finish(xtc_recorder *r)
|
|||
if (fclose(r->file))
|
||||
failed = 1;
|
||||
r->file = NULL;
|
||||
if (!failed && rename(r->temporary, r->path))
|
||||
failed = 1;
|
||||
if (!failed) {
|
||||
if (r->replace ? rename(r->temporary, r->path) : link(r->temporary, r->path))
|
||||
failed = 1;
|
||||
else {
|
||||
r->committed = 1;
|
||||
if (!r->replace) (void)unlink(r->temporary);
|
||||
}
|
||||
}
|
||||
r->preserve = failed && (__atomic_load_n(&r->write_position, __ATOMIC_ACQUIRE) != 0 || r->written != 0);
|
||||
r->finished = 1;
|
||||
return failed ? XTC_ERR_IO : result;
|
||||
}
|
||||
|
|
@ -181,12 +195,36 @@ void xtc_recorder_destroy(xtc_recorder *r)
|
|||
{
|
||||
if (!r)
|
||||
return;
|
||||
if (r->file)
|
||||
fclose(r->file);
|
||||
if (r->temporary)
|
||||
if (r->file) {
|
||||
if (__atomic_load_n(&r->write_position, __ATOMIC_ACQUIRE))
|
||||
(void)xtc_recorder_finish(r); /* Producer must already be quiescent. */
|
||||
else { fclose(r->file); r->file = NULL; }
|
||||
}
|
||||
if (r->temporary && !r->preserve)
|
||||
unlink(r->temporary);
|
||||
free(r->temporary);
|
||||
free(r->path);
|
||||
free(r->ring);
|
||||
free(r);
|
||||
}
|
||||
|
||||
xtc_result xtc_recorder_create(const char *path, unsigned int rate, size_t capacity, xtc_recorder **out)
|
||||
{
|
||||
return xtc_recorder_create_replace(path, rate, capacity, 0, out);
|
||||
}
|
||||
const char *xtc_recorder_recovery_path(const xtc_recorder *r)
|
||||
{
|
||||
if (!r) return "";
|
||||
return r->committed ? r->path : r->temporary ? r->temporary : "";
|
||||
}
|
||||
|
||||
/* Internal cancellation for a failed recovery transaction; never removes the
|
||||
* destination, which may have been created by another process. */
|
||||
void xtc_recorder_abort(xtc_recorder *r)
|
||||
{
|
||||
if (!r) return;
|
||||
if (r->file) fclose(r->file);
|
||||
r->file = NULL;
|
||||
r->finished = 1;
|
||||
r->preserve = 0;
|
||||
}
|
||||
|
|
|
|||
50
src/audio/recovery.c
Normal file
50
src/audio/recovery.c
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
#define _POSIX_C_SOURCE 200809L
|
||||
#include "wave_internal.h"
|
||||
#include <math.h>
|
||||
#include <string.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
static uint32_t little(const unsigned char *p)
|
||||
{ return (uint32_t)p[0] | (uint32_t)p[1] << 8 | (uint32_t)p[2] << 16 | (uint32_t)p[3] << 24; }
|
||||
xtc_result xtc_recorder_recover(const char *source, const char *destination)
|
||||
{
|
||||
unsigned char header[44], bytes[1024];
|
||||
struct stat info;
|
||||
FILE *file;
|
||||
xtc_recorder *recorder = NULL;
|
||||
xtc_result result = XTC_ERR_FORMAT;
|
||||
uint64_t remaining;
|
||||
unsigned int rate;
|
||||
if (!source || !destination) return XTC_ERR_INVALID_ARGUMENT;
|
||||
file = fopen(source, "rb");
|
||||
if (!file) return XTC_ERR_IO;
|
||||
if (fstat(fileno(file), &info) || !S_ISREG(info.st_mode) || info.st_size < 48 ||
|
||||
(uint64_t)info.st_size > UINT64_C(4294967292) || fread(header, 1, 44, file) != 44) goto done;
|
||||
rate = little(header + 24);
|
||||
if (memcmp(header, "RIFF", 4) || memcmp(header + 8, "WAVEfmt ", 8) || little(header + 16) != 16 ||
|
||||
header[20] != 3 || header[21] || header[22] != 1 || header[23] || !rate || rate > 768000 ||
|
||||
little(header + 28) != rate * 4 || header[32] != 4 || header[33] || header[34] != 32 || header[35] ||
|
||||
memcmp(header + 36, "data", 4)) goto done;
|
||||
remaining = ((uint64_t)info.st_size - 44) / 4;
|
||||
result = xtc_recorder_create(destination, rate, 256, &recorder);
|
||||
while (result == XTC_OK && remaining) {
|
||||
float samples[256];
|
||||
size_t i, count = remaining > 256 ? 256 : (size_t)remaining;
|
||||
if (fread(bytes, 4, count, file) != count) { result = XTC_ERR_IO; break; }
|
||||
for (i = 0; i < count; ++i) {
|
||||
uint32_t bits = little(bytes + i * 4);
|
||||
memcpy(samples + i, &bits, sizeof(bits));
|
||||
if (!isfinite(samples[i])) { result = XTC_ERR_FORMAT; break; }
|
||||
}
|
||||
if (result != XTC_OK) break;
|
||||
xtc_recorder_push(recorder, samples, count);
|
||||
result = xtc_recorder_drain(recorder);
|
||||
remaining -= count;
|
||||
}
|
||||
if (result == XTC_OK) result = xtc_recorder_finish(recorder);
|
||||
if (result != XTC_OK) xtc_recorder_abort(recorder);
|
||||
xtc_recorder_destroy(recorder);
|
||||
done:
|
||||
if (fclose(file) && result == XTC_OK) result = XTC_ERR_IO;
|
||||
return result;
|
||||
}
|
||||
|
|
@ -36,4 +36,5 @@ int xtc_wave_header(FILE *file, unsigned int rate, uint32_t frames);
|
|||
* @return Nonzero if all samples were written, zero on a write failure.
|
||||
*/
|
||||
int xtc_wave_write(FILE *file, const float *input, size_t frames);
|
||||
void xtc_recorder_abort(xtc_recorder *recorder);
|
||||
#endif
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue