Open.cxx 1.81 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2017 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 * http://www.musicpd.org
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include "config.h"
#include "InputStream.hxx"
#include "Registry.hxx"
#include "InputPlugin.hxx"
24
#include "LocalOpen.hxx"
25
#include "CondHandler.hxx"
26
#include "RewindInputStream.hxx"
27
#include "fs/Traits.hxx"
28
#include "fs/AllocatedPath.hxx"
29

30 31
#include <stdexcept>

32
InputStreamPtr
33
InputStream::Open(const char *url, Mutex &mutex)
34
{
35
	if (PathTraitsUTF8::IsAbsolute(url)) {
36
		const auto path = AllocatedPath::FromUTF8Throw(url);
37
		return OpenLocalInputStream(path, mutex);
38
	}
39

40
	input_plugins_for_each_enabled(plugin) {
41
		auto is = plugin->open(url, mutex);
42
		if (is != nullptr)
43
			return input_rewind_open(std::move(is));
44 45
	}

46
	throw std::runtime_error("Unrecognized URI");
47 48
}

49
InputStreamPtr
50
InputStream::OpenReady(const char *uri, Mutex &mutex)
51
{
52 53 54 55
	CondInputStreamHandler handler;

	auto is = Open(uri, mutex);
	is->SetHandler(&handler);
56

57
	{
58
		const std::lock_guard<Mutex> protect(mutex);
59 60 61 62 63 64 65 66 67

		while (true) {
			is->Update();
			if (is->IsReady())
				break;

			handler.cond.wait(mutex);
		}

68
		is->Check();
69
	}
70

71
	is->SetHandler(nullptr);
72 73
	return is;
}