Open.cxx 1.7 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 "Domain.hxx"
26
#include "plugins/RewindInputPlugin.hxx"
27
#include "fs/Traits.hxx"
28
#include "fs/AllocatedPath.hxx"
29

30 31
#include <stdexcept>

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

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

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

50
InputStreamPtr
51
InputStream::OpenReady(const char *uri,
52
		       Mutex &mutex, Cond &cond)
53
{
54
	auto is = Open(uri, mutex, cond);
55

56
	{
57
		const std::lock_guard<Mutex> protect(mutex);
58
		is->WaitReady();
59
		is->Check();
60
	}
61 62 63

	return is;
}