Open.cxx 1.74 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 42 43
	input_plugins_for_each_enabled(plugin) {
		InputStream *is;

44
		is = plugin->open(url, mutex, cond);
45 46 47
		if (is != nullptr) {
			is = input_rewind_open(is);

48
			return InputStreamPtr(is);
49
		}
50 51
	}

52
	throw std::runtime_error("Unrecognized URI");
53 54
}

55
InputStreamPtr
56
InputStream::OpenReady(const char *uri,
57
		       Mutex &mutex, Cond &cond)
58
{
59
	auto is = Open(uri, mutex, cond);
60

61
	{
62
		const std::lock_guard<Mutex> protect(mutex);
63
		is->WaitReady();
64
		is->Check();
65
	}
66 67 68

	return is;
}