Skip to contentSkip to conversation

crec: Initial version of a compressed capture utility

PATCH Superseded Charles Keepax 1 participant 2 messages Jan 23, 2014

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/2 crec: Initial version of a compressed capture utility

This version only supports capture of PCM streams over a compressed device and saves these as WAV files.

---

Changes since v2:
- Switched to using POSIX file IO to improve signal safety

Thanks,
Charles

 Android.mk     |   10 ++
 crec.c         |  418 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 makefile.linux |   20 ++-
 3 files changed, 441 insertions(+), 7 deletions(-)
 create mode 100644 crec.c
Android.mk +10 -0
index 104dfef..3cdd3d0 100644
@@ -20,3 +20,13 @@ LOCAL_MODULE_TAGS := optional
 
 include $(BUILD_EXECUTABLE)
 
+include $(CLEAR_VARS)
+
+LOCAL_C_INCLUDES:= $(LOCAL_PATH)/include
+LOCAL_SRC_FILES:= crec.c
+LOCAL_MODULE := crec
+LOCAL_SHARED_LIBRARIES:= libcutils libutils libtinycompress
+LOCAL_MODULE_TAGS := optional
+
+include $(BUILD_EXECUTABLE)
+
crec.c new file +418 -0

418 lines not on this page · raw message

makefile.linux +13 -7
index 677878d..81782d0 100644
@@ -1,10 +1,13 @@
 LIB = libtinycompress
-BIN = cplay
 VER = 0.1
 LIBSRC = compress.c
-SRC = cplay.c utils.c
+PLAYBIN = cplay
+RECBIN = crec
+PLAYSRC = cplay.c utils.c
+RECSRC = crec.c utils.c
 LIBOBJ = ${LIBSRC:.c=.o}
-OBJ = ${SRC:.c=.o}
+PLAYOBJ = ${PLAYSRC:.c=.o}
+RECOBJ = ${RECSRC:.c=.o}
 
 CC = gcc
 CROSS_COMPILE =
@@ -13,23 +16,26 @@ CFLAGS += -std=c99 -Wall -Wextra -Wunused -DVERSION=\"${VER}\" -I./include
 LDFLAGS += -L. -ltinycompress
 LIBLDFLAGS = -lasound
 
-all: libtinycompress cplay
+all: libtinycompress cplay crec
 
 $(LIB): ${LIBOBJ}
 	@echo "  LD	"$@
 	@${CROSS_COMPILE}${CC} ${CFLAGS} -shared -Wl,-soname,[email protected] -o [email protected] ${LIBOBJ} ${LIBLDFLAGS}
 
-$(BIN): ${OBJ} ${LIB}
+$(PLAYBIN): ${PLAYOBJ} ${LIB}
 	@echo "  LD	"$@
-	@${CROSS_COMPILE}${CC} ${CFLAGS} -o $@ ${OBJ} ${LDFLAGS}
+	@${CROSS_COMPILE}${CC} ${CFLAGS} -o $@ ${PLAYOBJ} ${LDFLAGS}
 
+$(RECBIN): ${RECOBJ} ${LIB}
+	@echo "  LD	"$@
+	@${CROSS_COMPILE}${CC} ${CFLAGS} -o $@ ${RECOBJ} ${LDFLAGS}
 
 %.o: %.c
 	@echo "  CC	"$<
 	@${CROSS_COMPILE}${CC} ${CFLAGS} -c -fPIC -o $@ $<
 
 clean:
-	@rm -rf ${BIN} ${OBJ} ${LIB}.so ${LIBOBJ}
+	@rm -rf ${BIN} ${PLAYOBJ} ${RECOBJ} ${LIB}.so ${LIBOBJ}
 
 
 .PHONY: all clean
Signature · 1 line
1.7.2.5
#1 lore raw

PATCH v3 2/2 crec: Add primitive exception handling

Add very primitive signal handling, we will not attempt to drain any remaining data etc. simply save out what we have to a file.

---
 crec.c |   74 +++++++++++++++++++++++++++++++++++++++++++++++++++++----------
 1 files changed, 62 insertions(+), 12 deletions(-)
crec.c +62 -12
index df53e4e..505073a 100644
@@ -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();
 
Signature · 1 line
1.7.2.5