aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStefan Pöschel <github@basicmaster.de>2017-08-24 11:34:00 +0200
committerStefan Pöschel <github@basicmaster.de>2017-08-24 12:07:29 +0200
commit060aad1d873414d4acec2fdcdcdd56529abe4d7c (patch)
treebb26ce373a8c6d3eead9707829c6d730fd608505
parent88b9fa7ad5c0e251b854bb0145a85cc6806c9f5c (diff)
downloadODR-PadEnc-060aad1d873414d4acec2fdcdcdd56529abe4d7c.tar.gz
ODR-PadEnc-060aad1d873414d4acec2fdcdcdd56529abe4d7c.tar.bz2
ODR-PadEnc-060aad1d873414d4acec2fdcdcdd56529abe4d7c.zip
Refactor actual encoder invokation
- move two vars to base class - handle thread sleep in base class - add missing lock guard for exit var
-rw-r--r--src/odr-padenc.cpp85
-rw-r--r--src/odr-padenc.h4
2 files changed, 52 insertions, 37 deletions
diff --git a/src/odr-padenc.cpp b/src/odr-padenc.cpp
index 2c53d4b..d9ceaf0 100644
--- a/src/odr-padenc.cpp
+++ b/src/odr-padenc.cpp
@@ -275,7 +275,24 @@ int PadEncoder::Main() {
}
// invoke actual encoder
- int result = Encode();
+ int result = 0;
+ for(;;) {
+ {
+ std::lock_guard<std::mutex> lock(status_mutex);
+
+ if(do_exit)
+ break;
+ }
+
+ result = Encode();
+
+ // abort on error
+ if(result)
+ break;
+
+ // sleep until next run
+ std::this_thread::sleep_until(run_timeline);
+ }
// cleanup
if(close(output_fd)) {
@@ -295,52 +312,46 @@ int PadEncoder::Main() {
const int BurstPadEncoder::DLS_REPETITION_WHILE_SLS = 50; // PADs
int BurstPadEncoder::Encode() {
- steady_clock::time_point next_run = steady_clock::now();
- int curr_dls_file = 0;
-
- while(!do_exit) {
- // try to read slides dir (if present)
- if (options.SLSEnabled() && slides.Empty()) {
- if (!slides.InitFromDir(options.sls_dir))
- return 1;
- }
-
- // if slides available, encode the first one
- if (!slides.Empty()) {
- slide_metadata_t slide = slides.GetSlide();
-
- if (!sls_encoder.encodeSlide(slide.filepath, slide.fidx, options.raw_slides))
- fprintf(stderr, "ODR-PadEnc Error: cannot encode file '%s'\n", slide.filepath.c_str());
+ // try to read slides dir (if present)
+ if (options.SLSEnabled() && slides.Empty()) {
+ if (!slides.InitFromDir(options.sls_dir))
+ return 1;
+ }
- if (options.erase_after_tx) {
- if (unlink(slide.filepath.c_str()) == -1)
- perror(("ODR-PadEnc Error: erasing file '" + slide.filepath +"' failed").c_str());
- }
+ // if slides available, encode the first one
+ if (!slides.Empty()) {
+ slide_metadata_t slide = slides.GetSlide();
- // while flushing, insert DLS (if present) after a certain PAD amout
- while (pad_packetizer.QueueFilled()) {
- if (options.DLSEnabled())
- dls_encoder.encodeLabel(options.dls_files[curr_dls_file], options.dl_params);
+ if (!sls_encoder.encodeSlide(slide.filepath, slide.fidx, options.raw_slides))
+ fprintf(stderr, "ODR-PadEnc Error: cannot encode file '%s'\n", slide.filepath.c_str());
- pad_packetizer.WriteAllPADs(output_fd, DLS_REPETITION_WHILE_SLS);
- }
+ if (options.erase_after_tx) {
+ if (unlink(slide.filepath.c_str()) == -1)
+ perror(("ODR-PadEnc Error: erasing file '" + slide.filepath +"' failed").c_str());
}
- // encode (a last) DLS (if present)
- if (options.DLSEnabled()) {
- dls_encoder.encodeLabel(options.dls_files[curr_dls_file], options.dl_params);
+ // while flushing, insert DLS (if present) after a certain PAD amout
+ while (pad_packetizer.QueueFilled()) {
+ if (options.DLSEnabled())
+ dls_encoder.encodeLabel(options.dls_files[curr_dls_file], options.dl_params);
- // switch to next DLS file
- curr_dls_file = (curr_dls_file + 1) % options.dls_files.size();
+ pad_packetizer.WriteAllPADs(output_fd, DLS_REPETITION_WHILE_SLS);
}
+ }
- // flush all remaining PADs
- pad_packetizer.WriteAllPADs(output_fd);
+ // encode (a last) DLS (if present)
+ if (options.DLSEnabled()) {
+ dls_encoder.encodeLabel(options.dls_files[curr_dls_file], options.dl_params);
- // sleep until next run
- next_run += std::chrono::seconds(options.slide_interval);
- std::this_thread::sleep_until(next_run);
+ // switch to next DLS file
+ curr_dls_file = (curr_dls_file + 1) % options.dls_files.size();
}
+ // flush all remaining PADs
+ pad_packetizer.WriteAllPADs(output_fd);
+
+ // schedule next run at next slide interval
+ run_timeline += std::chrono::seconds(options.slide_interval);
+
return 0;
}
diff --git a/src/odr-padenc.h b/src/odr-padenc.h
index 0158f55..a2c84a4 100644
--- a/src/odr-padenc.h
+++ b/src/odr-padenc.h
@@ -80,7 +80,9 @@ protected:
DLSEncoder dls_encoder;
SLSEncoder sls_encoder;
SlideStore slides;
+ int curr_dls_file;
int output_fd;
+ steady_clock::time_point run_timeline;
std::mutex status_mutex;
bool do_exit;
@@ -90,7 +92,9 @@ protected:
pad_packetizer(PADPacketizer(options.padlen)),
dls_encoder(DLSEncoder(&pad_packetizer)),
sls_encoder(SLSEncoder(&pad_packetizer)),
+ curr_dls_file(0),
output_fd(-1),
+ run_timeline(steady_clock::now()),
do_exit(false)
{}