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
|
||||
|
|
|
|||
|
|
@ -282,7 +282,9 @@ MainWindow::~MainWindow()
|
|||
std::fputs("Failed to close JACK during GUI destruction; backend and recording retained until process exit.\n",
|
||||
stderr);
|
||||
else if (recorder_) {
|
||||
(void)xtc_recorder_finish(recorder_);
|
||||
xtc_result result = xtc_recorder_finish(recorder_);
|
||||
std::fprintf(stderr, "Recording finalized during shutdown (%s): %s\n", xtc_result_string(result),
|
||||
xtc_recorder_recovery_path(recorder_));
|
||||
xtc_recorder_destroy(recorder_);
|
||||
}
|
||||
}
|
||||
|
|
@ -1091,13 +1093,14 @@ void MainWindow::finish_recording()
|
|||
uint64_t frames = 0;
|
||||
(void)xtc_recorder_status(recorder_, &frames);
|
||||
xtc_result r = xtc_recorder_finish(recorder_);
|
||||
std::string recovery_path = xtc_recorder_recovery_path(recorder_);
|
||||
xtc_recorder_destroy(recorder_);
|
||||
recorder_ = nullptr;
|
||||
if (r != XTC_OK) {
|
||||
char text[768];
|
||||
std::snprintf(text, sizeof(text),
|
||||
"Recording failed (%s). Partial take, if finalized: %s. It was not added to the project.",
|
||||
xtc_result_string(r), recording_path_.c_str());
|
||||
"Recording failed (%s). Retained take/temporary: %s. Recover with xtc-check --recover-recording. Not added.",
|
||||
xtc_result_string(r), recovery_path.c_str());
|
||||
set_label(error_, text);
|
||||
return;
|
||||
}
|
||||
|
|
@ -1155,8 +1158,7 @@ void MainWindow::audio_file_callback(Fl_Widget *widget, void *context)
|
|||
chooser.title(recording ? "Record JACK input to WAV" : relink ? "Relink selected clip" : "Import WAV");
|
||||
chooser.type(recording ? Fl_Native_File_Chooser::BROWSE_SAVE_FILE : Fl_Native_File_Chooser::BROWSE_FILE);
|
||||
chooser.filter("WAV audio\t*.wav");
|
||||
if (recording)
|
||||
chooser.options(Fl_Native_File_Chooser::SAVEAS_CONFIRM);
|
||||
// Recordings require a new destination; existing files are never replaced.
|
||||
int chosen = chooser.show();
|
||||
if (chosen == 1)
|
||||
return;
|
||||
|
|
@ -1173,7 +1175,8 @@ void MainWindow::audio_file_callback(Fl_Widget *widget, void *context)
|
|||
} else if (r == XTC_OK)
|
||||
r = recording ? window->record_audio(chooser.filename()) : window->import_audio(chooser.filename());
|
||||
if (r != XTC_OK)
|
||||
window->report_error("Project audio operation failed", r);
|
||||
window->report_error(recording ? "Project recording failed; choose a new WAV path in a writable directory"
|
||||
: "Project audio operation failed", r);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
15
src/main.c
15
src/main.c
|
|
@ -5,6 +5,7 @@
|
|||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "xtc/xtc_project.h"
|
||||
#include "xtc/xtc_audio.h"
|
||||
|
||||
/**
|
||||
* @brief Runs the xtc-check command-line entry point.
|
||||
|
|
@ -25,10 +26,20 @@ int main(int argc, char **argv)
|
|||
return EXIT_SUCCESS;
|
||||
}
|
||||
if (argc == 2 && !strcmp(argv[1], "--help")) {
|
||||
puts("Usage: xtc-check [--version | --help | --validate PROJECT]\nNo arguments checks core startup/shutdown. "
|
||||
puts("Usage: xtc-check [--version | --help | --validate PROJECT | --recover-recording TEMP NEW.wav]\nNo arguments checks core startup/shutdown. "
|
||||
"Validation checks metadata only.");
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
if (argc == 4 && !strcmp(argv[1], "--recover-recording")) {
|
||||
xtc_result result = xtc_recorder_recover(argv[2], argv[3]);
|
||||
if (result != XTC_OK) {
|
||||
fprintf(stderr, "Recording recovery failed: %s. Source retained; choose a new writable destination.\n",
|
||||
xtc_result_string(result));
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
puts("Complete recorded frames recovered; source retained.");
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
if (argc == 3 && !strcmp(argv[1], "--validate")) {
|
||||
xtc_project *project = NULL;
|
||||
xtc_result result = xtc_project_create(&project);
|
||||
|
|
@ -43,7 +54,7 @@ int main(int argc, char **argv)
|
|||
return EXIT_SUCCESS;
|
||||
}
|
||||
if (argc != 1) {
|
||||
fputs("Usage: xtc-check [--version | --help | --validate PROJECT]\n", stderr);
|
||||
fputs("Usage: xtc-check [--version | --help | --validate PROJECT | --recover-recording TEMP NEW.wav]\n", stderr);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue