From 7832f3bf3a9356112cab6ccb7faafb8256ea5271 Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Fri, 22 May 2015 22:09:42 +0200 Subject: Move writing ICY Text to a separate async task --- src/VLCInput.cpp | 35 ++++++++++++++++++++++++++++++++--- src/VLCInput.h | 5 ++++- 2 files changed, 36 insertions(+), 4 deletions(-) (limited to 'src') 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 #include #include +#include #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 #include #include +#include #include @@ -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 icy_text_written; std::string m_nowplaying; + std::string m_nowplaying_previous; // VLC pointers libvlc_instance_t *m_vlc; -- cgit v1.2.3