Open.cxx 1.98 KB
Newer Older
1
/*
2
 * Copyright 2003-2016 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
#include "util/Error.hxx"

31
InputStreamPtr
32 33 34 35
InputStream::Open(const char *url,
		  Mutex &mutex, Cond &cond,
		  Error &error)
{
36 37 38 39 40 41
	if (PathTraitsUTF8::IsAbsolute(url)) {
		const auto path = AllocatedPath::FromUTF8(url, error);
		if (path.IsNull())
			return nullptr;

		return OpenLocalInputStream(path,
42
					    mutex, cond, error);
43
	}
44

45 46 47 48 49 50 51
	input_plugins_for_each_enabled(plugin) {
		InputStream *is;

		is = plugin->open(url, mutex, cond, error);
		if (is != nullptr) {
			is = input_rewind_open(is);

52
			return InputStreamPtr(is);
53 54 55 56 57 58 59 60
		} else if (error.IsDefined())
			return nullptr;
	}

	error.Set(input_domain, "Unrecognized URI");
	return nullptr;
}

61
InputStreamPtr
62 63 64 65
InputStream::OpenReady(const char *uri,
		       Mutex &mutex, Cond &cond,
		       Error &error)
{
66
	auto is = Open(uri, mutex, cond, error);
67 68 69 70 71 72 73 74
	if (is == nullptr)
		return nullptr;

	mutex.lock();
	is->WaitReady();
	bool success = is->Check(error);
	mutex.unlock();

75 76
	if (!success)
		is.reset();
77 78 79

	return is;
}