aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorMax Kellermann <max@duempel.org>2013-12-29 18:08:49 +0100
committerMax Kellermann <max@duempel.org>2013-12-29 18:08:49 +0100
commitaeb2baa495776d9fc06e63f91167c40c334303c8 (patch)
tree4bbd926c1f19a64a1f3fc5b580d0d68bd3aabca3 /src
parentea9aff1d3f9f68eb5ae9e5f2bd67d283b8c0f47e (diff)
downloadmpd-aeb2baa495776d9fc06e63f91167c40c334303c8.tar.gz
mpd-aeb2baa495776d9fc06e63f91167c40c334303c8.tar.xz
mpd-aeb2baa495776d9fc06e63f91167c40c334303c8.zip
InputStream: add static method OpenReady()
Merge some duplicate code.
Diffstat (limited to 'src')
-rw-r--r--src/InputStream.cxx22
-rw-r--r--src/InputStream.hxx9
-rw-r--r--src/PlaylistAny.cxx2
-rw-r--r--src/PlaylistRegistry.cxx10
-rw-r--r--src/TagFile.cxx6
-rw-r--r--src/archive/Bzip2ArchivePlugin.cxx2
-rw-r--r--src/playlist/SoundCloudPlaylistPlugin.cxx5
7 files changed, 41 insertions, 15 deletions
diff --git a/src/InputStream.cxx b/src/InputStream.cxx
index 28a0aad1a..7d3ebe615 100644
--- a/src/InputStream.cxx
+++ b/src/InputStream.cxx
@@ -57,6 +57,28 @@ InputStream::Open(const char *url,
return nullptr;
}
+InputStream *
+InputStream::OpenReady(const char *uri,
+ Mutex &mutex, Cond &cond,
+ Error &error)
+{
+ InputStream *is = Open(uri, mutex, cond, error);
+ if (is == nullptr)
+ return nullptr;
+
+ mutex.lock();
+ is->WaitReady();
+ bool success = is->Check(error);
+ mutex.unlock();
+
+ if (!success) {
+ is->Close();
+ is = nullptr;
+ }
+
+ return is;
+}
+
bool
InputStream::Check(Error &error)
{
diff --git a/src/InputStream.hxx b/src/InputStream.hxx
index b1bc9c4ab..5ac3bdcae 100644
--- a/src/InputStream.hxx
+++ b/src/InputStream.hxx
@@ -119,6 +119,15 @@ struct InputStream {
Error &error);
/**
+ * Just like Open(), but waits for the stream to become ready.
+ * It is a wrapper for Open(), WaitReady() and Check().
+ */
+ gcc_malloc gcc_nonnull_all
+ static InputStream *OpenReady(const char *uri,
+ Mutex &mutex, Cond &cond,
+ Error &error);
+
+ /**
* Close the input stream and free resources.
*
* The caller must not lock the mutex.
diff --git a/src/PlaylistAny.cxx b/src/PlaylistAny.cxx
index dfe3bfdff..083b9a230 100644
--- a/src/PlaylistAny.cxx
+++ b/src/PlaylistAny.cxx
@@ -41,7 +41,7 @@ playlist_open_remote(const char *uri, Mutex &mutex, Cond &cond,
}
Error error;
- InputStream *is = InputStream::Open(uri, mutex, cond, error);
+ InputStream *is = InputStream::OpenReady(uri, mutex, cond, error);
if (is == nullptr) {
if (error.IsDefined())
FormatError(error, "Failed to open %s", uri);
diff --git a/src/PlaylistRegistry.cxx b/src/PlaylistRegistry.cxx
index 65f55e6ff..17cad3db5 100644
--- a/src/PlaylistRegistry.cxx
+++ b/src/PlaylistRegistry.cxx
@@ -269,9 +269,7 @@ playlist_list_open_stream_suffix(InputStream &is, const char *suffix)
SongEnumerator *
playlist_list_open_stream(InputStream &is, const char *uri)
{
- const char *suffix;
-
- is.LockWaitReady();
+ assert(is.ready);
const char *const mime = is.GetMimeType();
if (mime != nullptr) {
@@ -280,7 +278,7 @@ playlist_list_open_stream(InputStream &is, const char *uri)
return playlist;
}
- suffix = uri != nullptr ? uri_get_suffix(uri) : nullptr;
+ const char *suffix = uri != nullptr ? uri_get_suffix(uri) : nullptr;
if (suffix != nullptr) {
auto playlist = playlist_list_open_stream_suffix(is, suffix);
if (playlist != nullptr)
@@ -317,7 +315,7 @@ playlist_list_open_path(const char *path_fs, Mutex &mutex, Cond &cond,
return nullptr;
Error error;
- InputStream *is = InputStream::Open(path_fs, mutex, cond, error);
+ InputStream *is = InputStream::OpenReady(path_fs, mutex, cond, error);
if (is == nullptr) {
if (error.IsDefined())
LogError(error);
@@ -325,8 +323,6 @@ playlist_list_open_path(const char *path_fs, Mutex &mutex, Cond &cond,
return nullptr;
}
- is->LockWaitReady();
-
auto playlist = playlist_list_open_stream_suffix(*is, suffix);
if (playlist != nullptr)
*is_r = is;
diff --git a/src/TagFile.cxx b/src/TagFile.cxx
index 8d29d5ebe..3a6756008 100644
--- a/src/TagFile.cxx
+++ b/src/TagFile.cxx
@@ -62,9 +62,9 @@ public:
/* open the InputStream (if not already open) */
if (is == nullptr) {
- is = InputStream::Open(path_fs.c_str(),
- mutex, cond,
- IgnoreError());
+ is = InputStream::OpenReady(path_fs.c_str(),
+ mutex, cond,
+ IgnoreError());
if (is == nullptr)
return false;
} else
diff --git a/src/archive/Bzip2ArchivePlugin.cxx b/src/archive/Bzip2ArchivePlugin.cxx
index c2d312cd1..7734d3f4b 100644
--- a/src/archive/Bzip2ArchivePlugin.cxx
+++ b/src/archive/Bzip2ArchivePlugin.cxx
@@ -146,7 +146,7 @@ bz2_open(const char *pathname, Error &error)
{
static Mutex mutex;
static Cond cond;
- InputStream *is = InputStream::Open(pathname, mutex, cond, error);
+ InputStream *is = InputStream::OpenReady(pathname, mutex, cond, error);
if (is == nullptr)
return nullptr;
diff --git a/src/playlist/SoundCloudPlaylistPlugin.cxx b/src/playlist/SoundCloudPlaylistPlugin.cxx
index 0ab250b22..afcbaef23 100644
--- a/src/playlist/SoundCloudPlaylistPlugin.cxx
+++ b/src/playlist/SoundCloudPlaylistPlugin.cxx
@@ -254,8 +254,8 @@ soundcloud_parse_json(const char *url, yajl_handle hand,
Mutex &mutex, Cond &cond)
{
Error error;
- InputStream *input_stream = InputStream::Open(url, mutex, cond,
- error);
+ InputStream *input_stream = InputStream::OpenReady(url, mutex, cond,
+ error);
if (input_stream == nullptr) {
if (error.IsDefined())
LogError(error);
@@ -263,7 +263,6 @@ soundcloud_parse_json(const char *url, yajl_handle hand,
}
mutex.lock();
- input_stream->WaitReady();
yajl_status stat;
int done = 0;