QobuzInputPlugin.cxx 5.45 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
 * Copyright 2003-2018 The Music Player Daemon Project
 * 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 "QobuzInputPlugin.hxx"
#include "QobuzClient.hxx"
#include "QobuzTrackRequest.hxx"
23
#include "QobuzTagScanner.hxx"
24 25 26 27 28 29
#include "CurlInputPlugin.hxx"
#include "PluginUnavailable.hxx"
#include "input/ProxyInputStream.hxx"
#include "input/FailingInputStream.hxx"
#include "input/InputPlugin.hxx"
#include "config/Block.hxx"
30
#include "lib/gcrypt/Init.hxx"
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
#include "thread/Mutex.hxx"
#include "util/StringCompare.hxx"

#include <stdexcept>
#include <memory>

#include <time.h>

static QobuzClient *qobuz_client;

class QobuzInputStream final
	: public ProxyInputStream, QobuzSessionHandler, QobuzTrackHandler {

	const std::string track_id;

	std::unique_ptr<QobuzTrackRequest> track_request;

	std::exception_ptr error;

public:
	QobuzInputStream(const char *_uri, const char *_track_id,
52 53
			 Mutex &_mutex) noexcept
		:ProxyInputStream(_uri, _mutex),
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
		 track_id(_track_id)
	{
		qobuz_client->AddLoginHandler(*this);
	}

	~QobuzInputStream() {
		qobuz_client->RemoveLoginHandler(*this);
	}

	/* virtual methods from InputStream */

	void Check() override {
		if (error)
			std::rethrow_exception(error);
	}

private:
	void Failed(std::exception_ptr e) {
		SetInput(std::make_unique<FailingInputStream>(GetURI(), e,
73
							      mutex));
74 75 76 77 78 79
	}

	/* virtual methods from QobuzSessionHandler */
	void OnQobuzSession() noexcept override;

	/* virtual methods from QobuzTrackHandler */
80
	void OnQobuzTrackSuccess(std::string url) noexcept override;
81 82 83 84 85 86 87 88 89 90 91
	void OnQobuzTrackError(std::exception_ptr error) noexcept override;
};

void
QobuzInputStream::OnQobuzSession() noexcept
{
	const std::lock_guard<Mutex> protect(mutex);

	try {
		const auto session = qobuz_client->GetSession();

92
		QobuzTrackHandler &h = *this;
93 94 95
		track_request = std::make_unique<QobuzTrackRequest>(*qobuz_client,
								    session,
								    track_id.c_str(),
96
								    h);
97 98 99 100 101 102 103
		track_request->Start();
	} catch (...) {
		Failed(std::current_exception());
	}
}

void
104
QobuzInputStream::OnQobuzTrackSuccess(std::string url) noexcept
105 106
{
	const std::lock_guard<Mutex> protect(mutex);
107
	track_request.reset();
108 109 110

	try {
		SetInput(OpenCurlInputStream(url.c_str(), {},
111
					     mutex));
112 113 114 115 116 117 118 119 120
	} catch (...) {
		Failed(std::current_exception());
	}
}

void
QobuzInputStream::OnQobuzTrackError(std::exception_ptr e) noexcept
{
	const std::lock_guard<Mutex> protect(mutex);
121
	track_request.reset();
122 123 124 125 126 127 128

	Failed(e);
}

static void
InitQobuzInput(EventLoop &event_loop, const ConfigBlock &block)
{
129 130
	Gcrypt::Init();

131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
	const char *base_url = block.GetBlockValue("base_url",
						   "http://www.qobuz.com/api.json/0.2/");

	const char *app_id = block.GetBlockValue("app_id");
	if (app_id == nullptr)
		throw PluginUnavailable("No Qobuz app_id configured");

	const char *app_secret = block.GetBlockValue("app_secret");
	if (app_secret == nullptr)
		throw PluginUnavailable("No Qobuz app_secret configured");

	const char *device_manufacturer_id = block.GetBlockValue("device_manufacturer_id",
								 "df691fdc-fa36-11e7-9718-635337d7df8f");

	const char *username = block.GetBlockValue("username");
	const char *email = block.GetBlockValue("email");
	if (username == nullptr && email == nullptr)
		throw PluginUnavailable("No Qobuz username configured");

	const char *password = block.GetBlockValue("password");
	if (password == nullptr)
		throw PluginUnavailable("No Qobuz password configured");

154 155
	const char *format_id = block.GetBlockValue("format_id", "5");

156 157 158
	qobuz_client = new QobuzClient(event_loop, base_url,
				       app_id, app_secret,
				       device_manufacturer_id,
159 160
				       username, email, password,
				       format_id);
161 162 163 164 165 166 167 168
}

static void
FinishQobuzInput()
{
	delete qobuz_client;
}

169 170 171
gcc_pure
static const char *
ExtractQobuzTrackId(const char *uri)
172
{
173 174 175 176
	// TODO: what's the standard "qobuz://" URI syntax?
	const char *track_id = StringAfterPrefix(uri, "qobuz://track/");
	if (track_id == nullptr)
		return nullptr;
177

178 179
	if (*track_id == 0)
		return nullptr;
180

181 182
	return track_id;
}
183

184
static InputStreamPtr
185
OpenQobuzInput(const char *uri, Mutex &mutex)
186 187
{
	assert(qobuz_client != nullptr);
188

189 190
	const char *track_id = ExtractQobuzTrackId(uri);
	if (track_id == nullptr)
191 192 193 194
		return nullptr;

	// TODO: validate track_id

195
	return std::make_unique<QobuzInputStream>(uri, track_id, mutex);
196 197
}

198 199 200 201 202 203 204 205 206 207 208 209 210
static std::unique_ptr<RemoteTagScanner>
ScanQobuzTags(const char *uri, RemoteTagHandler &handler)
{
	assert(qobuz_client != nullptr);

	const char *track_id = ExtractQobuzTrackId(uri);
	if (track_id == nullptr)
		return nullptr;

	return std::make_unique<QobuzTagScanner>(*qobuz_client, track_id,
						 handler);
}

211 212 213 214 215
static constexpr const char *qobuz_prefixes[] = {
	"qobuz://",
	nullptr
};

216 217
const InputPlugin qobuz_input_plugin = {
	"qobuz",
218
	qobuz_prefixes,
219 220 221
	InitQobuzInput,
	FinishQobuzInput,
	OpenQobuzInput,
222
	ScanQobuzTags,
223
};