diff options
-rw-r--r-- | src/VLCInput.cpp | 35 | ||||
-rw-r--r-- | src/VLCInput.h | 5 |
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; |