From: Charles Keepax <ckeepax@opensource.wolfsonmicro.com>
To: vinod.koul@linux.intel.com
Cc: alsa-devel@alsa-project.org, patches@opensource.wolfsonmicro.com
Subject: [TINYCOMPRESS PATCH 2/2 v3] crec: Add primitive exception handling
Date: Thu, 23 Jan 2014 17:07:36 +0000 [thread overview]
Message-ID: <1390496856-16669-2-git-send-email-ckeepax@opensource.wolfsonmicro.com> (raw)
In-Reply-To: <1390496856-16669-1-git-send-email-ckeepax@opensource.wolfsonmicro.com>
Add very primitive signal handling, we will not attempt to drain any
remaining data etc. simply save out what we have to a file.
Signed-off-by: Charles Keepax <ckeepax@opensource.wolfsonmicro.com>
---
crec.c | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++++----------
1 files changed, 62 insertions(+), 12 deletions(-)
diff --git a/crec.c b/crec.c
index df53e4e..505073a 100644
--- a/crec.c
+++ b/crec.c
@@ -74,6 +74,7 @@
#include "tinycompress/tinycompress.h"
static int verbose;
+static int file;
static const unsigned int DEFAULT_CHANNELS = 1;
static const unsigned int DEFAULT_RATE = 44100;
@@ -183,6 +184,48 @@ static int print_time(struct compress *compress)
return 0;
}
+static int finish_record()
+{
+ struct wave_header header;
+ int ret;
+ size_t nread, written;
+
+ if (!file)
+ return -ENOENT;
+
+ /* Get amount of data written to file */
+ ret = lseek(file, 0, SEEK_END);
+ if (ret < 0)
+ return -errno;
+
+ written = ret;
+ if (written < sizeof(header))
+ return -ENOENT;
+ written -= sizeof(header);
+
+ /* Sync file header from file */
+ ret = lseek(file, 0, SEEK_SET);
+ if (ret < 0)
+ return -errno;
+
+ nread = read(file, &header, sizeof(header));
+ if (nread != sizeof(header))
+ return -errno;
+
+ /* Update file header */
+ ret = lseek(file, 0, SEEK_SET);
+ if (ret < 0)
+ return -errno;
+
+ size_wave_header(&header, written);
+
+ written = write(file, &header, sizeof(header));
+ if (written != sizeof(header))
+ return -errno;
+
+ return 0;
+}
+
void capture_samples(char *name, unsigned int card, unsigned int device,
unsigned long buffer_size, unsigned int frag,
unsigned int length, unsigned int rate,
@@ -192,7 +235,6 @@ void capture_samples(char *name, unsigned int card, unsigned int device,
struct snd_codec codec;
struct compress *compress;
struct wave_header header;
- int file;
char *buffer;
size_t written;
int read, ret;
@@ -214,7 +256,7 @@ void capture_samples(char *name, unsigned int card, unsigned int device,
if (verbose)
printf("%s: entry, reading %u bytes\n", __func__, length);
- file = open(name, O_WRONLY);
+ file = open(name, O_RDWR);
if (file == -1) {
fprintf(stderr, "Unable to open file '%s'\n", name);
exit(EXIT_FAILURE);
@@ -311,17 +353,9 @@ void capture_samples(char *name, unsigned int card, unsigned int device,
fprintf(stderr, "ERR: %s\n", compress_get_error(compress));
}
- /* Update file header now we know file size */
- size_wave_header(&header, total_read);
- ret = lseek(file, 0, SEEK_SET);
+ ret = finish_record();
if (ret < 0) {
- fprintf(stderr, "Error seeking: %s\n", strerror(errno));
- goto buf_exit;
- }
- written = write(file, &header, sizeof(header));
- if (written != sizeof(header)) {
- fprintf(stderr, "Error updating output file header: %s\n",
- strerror(errno));
+ fprintf(stderr, "Failed to finish header: %s\n", strerror(ret));
goto buf_exit;
}
@@ -330,6 +364,7 @@ void capture_samples(char *name, unsigned int card, unsigned int device,
free(buffer);
close(file);
+ file = 0;
compress_close(compress);
@@ -347,6 +382,16 @@ file_exit:
exit(EXIT_FAILURE);
}
+static void sig_handler(int signum __attribute__ ((unused)))
+{
+ finish_record();
+
+ if (file)
+ close(file);
+
+ _exit(EXIT_FAILURE);
+}
+
int main(int argc, char **argv)
{
char *file;
@@ -356,6 +401,11 @@ int main(int argc, char **argv)
unsigned int rate = DEFAULT_RATE, channels = DEFAULT_CHANNELS;
unsigned int format = DEFAULT_FORMAT;
+ if (signal(SIGINT, sig_handler) == SIG_ERR) {
+ fprintf(stderr, "Error registering signal handler\n");
+ exit(EXIT_FAILURE);
+ }
+
if (argc < 2)
usage();
--
1.7.2.5
prev parent reply other threads:[~2014-01-23 17:21 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-01-23 17:07 [TINYCOMPRESS PATCH 1/2 v3] crec: Initial version of a compressed capture utility Charles Keepax
2014-01-23 17:07 ` Charles Keepax [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1390496856-16669-2-git-send-email-ckeepax@opensource.wolfsonmicro.com \
--to=ckeepax@opensource.wolfsonmicro.com \
--cc=alsa-devel@alsa-project.org \
--cc=patches@opensource.wolfsonmicro.com \
--cc=vinod.koul@linux.intel.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox