summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthias P. Braendli <matthias.braendli@mpb.li>2015-05-22 22:09:42 +0200
committerMatthias P. Braendli <matthias.braendli@mpb.li>2015-05-22 22:09:42 +0200
commit7832f3bf3a9356112cab6ccb7faafb8256ea5271 (patch)
tree5f36e7b6aafb07132385ee4aaa8dae5149edd2d0
parentb5d910704541550fa23f14a610848402d51ba542 (diff)
downloadODR-AudioEnc-7832f3bf3a9356112cab6ccb7faafb8256ea5271.tar.gz
ODR-AudioEnc-7832f3bf3a9356112cab6ccb7faafb8256ea5271.tar.bz2
ODR-AudioEnc-7832f3bf3a9356112cab6ccb7faafb8256ea5271.zip
Move writing ICY Text to a separate async task
-rw-r--r--src/VLCInput.cpp35
-rw-r--r--src/VLCInput.h5
2 files changed, 36 insertions, 4 deletions
diff --git a/src/VLCInput.cpp b/src/VLCInput.cpp
index 70381fb..3a2e9ec 100644
--- a/src/VLCInput.cpp
+++ b/src/VLCInput.cpp
@@ -20,6 +20,7 @@
#include <string>
#include <cstring>
#include <chrono>
+#include <functional>
#include "VLCInput.h"
@@ -294,11 +295,39 @@ ssize_t VLCInput::read(uint8_t* buf, size_t length)
return read;
}
-void VLCInput::write_icy_text(const std::string& filename) const
+bool write_icy_to_file(const std::string& text, const std::string& filename)
{
FILE* fd = fopen(filename.c_str(), "wb");
- fputs_unlocked(m_nowplaying.c_str(), fd);
- fclose(fd);
+ if (fd) {
+ int ret = fputs(text.c_str(), fd);
+ fclose(fd);
+
+ return ret >= 0;
+ }
+
+ return false;
+}
+
+void VLCInput::write_icy_text(const std::string& filename)
+{
+ if (icy_text_written.valid()) {
+ auto status = icy_text_written.wait_for(std::chrono::microseconds(1));
+ if (status == std::future_status::ready) {
+ if (not icy_text_written.get()) {
+ fprintf(stderr, "Failed to write ICY Text to file!\n");
+ }
+ }
+ }
+
+ else {
+ if (m_nowplaying_previous != m_nowplaying) {
+ icy_text_written = std::async(std::launch::async,
+ std::bind(write_icy_to_file, m_nowplaying, filename));
+
+ }
+
+ m_nowplaying_previous = m_nowplaying;
+ }
}
/* VLC up to version 2.1.0 used a different callback function signature.
diff --git a/src/VLCInput.h b/src/VLCInput.h
index 048c931..6b0a0f4 100644
--- a/src/VLCInput.h
+++ b/src/VLCInput.h
@@ -29,6 +29,7 @@
#include <deque>
#include <thread>
#include <mutex>
+#include <future>
#include <vlc/vlc.h>
@@ -67,7 +68,7 @@ class VLCInput
/* Write the last received ICY-Text to the
* file.
*/
- void write_icy_text(const std::string& filename) const;
+ void write_icy_text(const std::string& filename);
// Callbacks for VLC
@@ -102,7 +103,9 @@ class VLCInput
unsigned m_channels;
int m_rate;
+ std::future<bool> icy_text_written;
std::string m_nowplaying;
+ std::string m_nowplaying_previous;
// VLC pointers
libvlc_instance_t *m_vlc;