Open.cxx 1.84 KB
Newer Older
1
/*
2
 * Copyright 2003-2018 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
 * 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 "InputStream.hxx"
#include "Registry.hxx"
#include "InputPlugin.hxx"
23
#include "LocalOpen.hxx"
24
#include "CondHandler.hxx"
25
#include "RewindInputStream.hxx"
26
#include "fs/Traits.hxx"
27
#include "fs/AllocatedPath.hxx"
28

29 30
#include <stdexcept>

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

39
	input_plugins_for_each_enabled(plugin) {
40 41 42
		if (!plugin->SupportsUri(url))
			continue;

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

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

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

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

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

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

			handler.cond.wait(mutex);
		}

70
		is->Check();
71
	}
72

73
	is->SetHandler(nullptr);
74 75
	return is;
}