aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStanimir Varbanov <stanimir.varbanov@linaro.org>2016-02-23 11:24:51 +0200
committerStanimir Varbanov <stanimir.varbanov@linaro.org>2016-06-13 18:34:40 +0300
commit3cf64ff8bb753e4495e1802db05c89943dcbea8b (patch)
tree6a3b994c80cef8d6f3931cce6cd90a8112934429
parentb6074c3f8d9ff3e45390de7fda5718f64c8d2808 (diff)
downloadv4l2-encode-3cf64ff8bb753e4495e1802db05c89943dcbea8b.tar.gz
add pthread condition on output buffers
add verbose and disable_gentest cmd line args Signed-off-by: Stanimir Varbanov <stanimir.varbanov@linaro.org>
-rw-r--r--args.c12
-rw-r--r--common.h5
-rw-r--r--main.c29
3 files changed, 35 insertions, 11 deletions
diff --git a/args.c b/args.c
index 40c94cd..f6bb18c 100644
--- a/args.c
+++ b/args.c
@@ -21,6 +21,7 @@
*
*/
+#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
@@ -40,7 +41,8 @@ void print_usage(char *name)
printf("\t-w video width\n");
printf("\t-h video height\n");
printf("\t-f save encoded frames on disk\n");
-
+ printf("\t-g disable test pattern generator\n");
+ printf("\t-v verbose mode\n");
printf("\n");
}
@@ -75,7 +77,7 @@ int parse_args(struct instance *i, int argc, char **argv)
init_to_defaults(i);
- while ((c = getopt(argc, argv, "w:h:c:d:m:fn:")) != -1) {
+ while ((c = getopt(argc, argv, "w:h:c:d:m:f:n:gv")) != -1) {
switch (c) {
case 'c':
i->codec = get_codec(optarg);
@@ -100,6 +102,12 @@ int parse_args(struct instance *i, int argc, char **argv)
case 'n':
i->num_frames_to_save = atoi(optarg);
break;
+ case 'g':
+ i->disable_gentest = true;
+ break;
+ case 'v':
+ i->verbose = true;
+ break;
default:
err("Bad argument");
return -1;
diff --git a/common.h b/common.h
index 11067af..6752a33 100644
--- a/common.h
+++ b/common.h
@@ -24,6 +24,7 @@
#ifndef INCLUDE_COMMON_H
#define INCLUDE_COMMON_H
+#include <stdbool.h>
#include <stdio.h>
#include <stdint.h>
#include <semaphore.h>
@@ -142,6 +143,8 @@ struct instance {
unsigned int save_encoded;
char *save_path;
unsigned long num_frames_to_save;
+ bool disable_gentest;
+ bool verbose;
int use_dmabuf;
@@ -158,6 +161,8 @@ struct instance {
char *codec_name;
pthread_mutex_t lock;
+ pthread_condattr_t attr;
+ pthread_cond_t cond;
/* Control */
int error; /* The error flag */
diff --git a/main.c b/main.c
index 87bb900..44a0f9b 100644
--- a/main.c
+++ b/main.c
@@ -126,10 +126,7 @@ int save_encoded(struct instance *i, const void *buf, unsigned int size)
if (!i->save_encoded)
return 0;
- if (!i->save_path)
- ret = sprintf(filename, "/mnt/sdcard/encoded.%s", ext);
- else
- ret = sprintf(filename, "%s/encoded.%s", i->save_path, ext);
+ ret = sprintf(filename, "%s/encoded.%s", i->save_path, ext);
if (ret < 0) {
err("sprintf fail (%s)", strerror(errno));
return -1;
@@ -138,14 +135,14 @@ int save_encoded(struct instance *i, const void *buf, unsigned int size)
if (out->fd)
goto write;
- dbg("create file %s", filename);
-
out->fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_SYNC, mode);
if (out->fd < 0) {
err("cannot open file (%s)", strerror(errno));
return -1;
}
+ info("created file %s", filename);
+
write:
written = pwrite(out->fd, buf, size, out->offs);
if (written < 0) {
@@ -155,13 +152,13 @@ write:
out->offs += written;
- dbg("written %zd bytes at offset: %zu", written, out->offs);
+ dbg("written %zd bytes at offset %zu", written, out->offs);
return 0;
}
static int input_read(struct instance *inst, unsigned int index,
- unsigned int *used, unsigned int *fs)
+ unsigned int *used, unsigned int *fs)
{
struct video *vid = &inst->video;
unsigned char *to = vid->out_buf_addr[index];
@@ -169,6 +166,9 @@ static int input_read(struct instance *inst, unsigned int index,
*used = vid->out_buf_size;
*fs = vid->out_buf_size;
+ if (inst->disable_gentest)
+ return 0;
+
gentest_fill(inst->width, inst->height, to, vid->out_buf_size);
return 0;
@@ -210,6 +210,10 @@ void *input_thread_func(void *args)
pthread_mutex_unlock(&i->lock);
dbg("queued output buffer %d", n);
+ } else {
+ pthread_mutex_lock(&i->lock);
+ pthread_cond_wait(&i->cond, &i->lock);
+ pthread_mutex_unlock(&i->lock);
}
}
@@ -264,7 +268,8 @@ void *main_thread_func(void *args)
vid->cap_buf_flag[n] = 0;
pthread_mutex_unlock(&i->lock);
- info("encoded frame %ld", vid->total_encoded);
+ if (i->verbose)
+ info("encoded frame %ld", vid->total_encoded);
if (finished)
break;
@@ -297,6 +302,7 @@ next_event:
pthread_mutex_lock(&i->lock);
vid->out_buf_flag[n] = 0;
pthread_mutex_unlock(&i->lock);
+ pthread_cond_signal(&i->cond);
}
dbg("dequeued output buffer %d", n);
@@ -323,6 +329,9 @@ int main(int argc, char **argv)
pthread_mutex_init(&inst.lock, 0);
+ pthread_condattr_init(&inst.attr);
+ pthread_cond_init(&inst.cond, &inst.attr);
+
vid->total_encoded = 0;
ret = video_open(&inst, inst.video.name);
@@ -406,6 +415,8 @@ int main(int argc, char **argv)
return 0;
err:
pthread_mutex_destroy(&inst.lock);
+ pthread_cond_destroy(&inst.cond);
+ pthread_condattr_destroy(&inst.attr);
cleanup(&inst);
gentest_deinit();
return 1;