summaryrefslogtreecommitdiffstats
path: root/mediaplayer/MediaplayerMpdBackend.cpp
blob: 5cc9dcea876a35dea2feb5afe891b339e5672a4d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/*
 * Copyright (C) 2022 Konsulko Group
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <QDebug>
#include "MediaplayerMpdBackend.h"
#include "MpdEventHandler.h"
#include "mediaplayer.h"

// Use a 60s timeout on our MPD connection
// NOTE: The connection is actively poked at a higher frequency than
//       this to ensure we don't hit the timeout.  The alternatives
//       are to either open and close a connection for every command,
//       or try to keep the connection in idle mode when not using it.
//       The latter is deemed too complicated for our purposes for now,
//       due to it likely requiring another thread.
#define MPD_CONNECTION_TIMEOUT	60000

MediaplayerMpdBackend::MediaplayerMpdBackend(Mediaplayer *player, QQmlContext *context, QObject *parent) :
	MediaplayerBackend(player, parent)
{
	struct mpd_connection *conn = mpd_connection_new(NULL, 0, MPD_CONNECTION_TIMEOUT);
	if (!conn) {
		qFatal("Could not create MPD connection");
	}
	if (mpd_connection_get_error(conn) != MPD_ERROR_SUCCESS) {
		qFatal("%s", mpd_connection_get_error_message(conn));
	}
	m_mpd_conn = conn;

	// Set up connection keepalive timer
	m_mpd_conn_timer = new QTimer(this);
	connect(m_mpd_conn_timer, &QTimer::timeout, this, &MediaplayerMpdBackend::connectionKeepaliveTimeout);
	m_mpd_conn_timer->start(MPD_CONNECTION_TIMEOUT / 2);

	m_song_pos_timer = new QTimer(this);
	connect(m_song_pos_timer, &QTimer::timeout, this, &MediaplayerMpdBackend::songPositionTimeout);

	// Connect signal to push the timer-driven position updates to the player
	connect(this,
		&MediaplayerMpdBackend::positionMetadataUpdate,
		player,
		&Mediaplayer::updateLocalMetadata);

	// Create thread to handle polling for MPD events
	MpdEventHandler *handler = new MpdEventHandler();
	handler->moveToThread(&m_handlerThread);
	connect(&m_handlerThread, &QThread::finished, handler, &QObject::deleteLater);
	connect(this,
		&MediaplayerMpdBackend::startHandler,
		handler,
		&MpdEventHandler::handleEvents);

	// Connect playback state updates from the backend handler thread
	// so our view of the state is kept in sync with MPD.
	connect(handler,
		&MpdEventHandler::playbackStateUpdate,
		this,
		&MediaplayerMpdBackend::updatePlaybackState,
		Qt::QueuedConnection);

	// Connect updates from backend handler thread.
	// For now, playlist updates go directly to the parent to keep its
	// model in sync.  This would have to change if there's ever another
	// backend with playlist support, or improving abstraction is desired.
	// Current song metadata is directed to a slot here for caching before
	// sending to the parent, as having the backends be where that is
	// done seems inherently more logical.
	connect(handler,
		&MpdEventHandler::playlistUpdate,
		player,
		&Mediaplayer::updateLocalPlaylist);
	connect(handler,
		&MpdEventHandler::metadataUpdate,
		this,
		&MediaplayerMpdBackend::updateMetadata);
	connect(this,
		&MediaplayerMpdBackend::metadataUpdate,
		player,
		&Mediaplayer::updateLocalMetadata);

	m_handlerThread.start();

	// Start event handler worker loop
	emit startHandler();
}

MediaplayerMpdBackend::~MediaplayerMpdBackend()
{
	m_handlerThread.quit();
	m_handlerThread.wait();

	m_mpd_conn_timer->stop();
	delete m_mpd_conn_timer;

	mpd_connection_free(m_mpd_conn);
}

void MediaplayerMpdBackend::start()
{
	// An explicit refresh of the playlist is likely necessary
	// here to handle starting in situations where MPD has been
	// running before we were (e.g. restarting an app after a
	// crash).  At present the timing seems to be such that we
	// do not have to worry about signals being sent before the
	// app is ready, but this could be an issue down the road.
}


void MediaplayerMpdBackend::refresh_metadata()
{
	if (m_cached_metadata.isEmpty()) {
		// This can happen if the app starts up with Bluetooth
		// connected and then the source is switched.  Provide
		// empty metadata to clear up the app's state.
		//
		// Getting the metadata for the first entry in the playlist
		// to provide it here intersects with how to handle hitting
		// the end of the playlist and MPD stopping, handling that
		// is complicated enough that it is being left as future
		// development.

		QVariantMap track;
		track.insert("title", "");
		track.insert("artist", "");
		track.insert("album", "");
		track.insert("duration", 0);
		m_cached_metadata["track"] = track;
		m_cached_metadata["position"] = 0;
		m_cached_metadata["status"] = "stopped";
	}

	// The demo app currently ignores other information in an update with
	// album art, which is a historical artifact of it arriving in a second
	// update.  Until that assumption is perhaps changed, to avoid having to
	// complicate things wrt caching, we recreate this behavior using the
	// metadata we have if it contains album art.
	if (m_cached_metadata.contains("track")) {
		QVariantMap tmp = m_cached_metadata;
		QVariantMap track = tmp.value("track").toMap();
		if (track.contains("image")) {
			track.remove("image");
			tmp["track"] = track;

			// Send this as the initial no art update
			emit metadataUpdate(tmp);
		}
	}

	emit metadataUpdate(m_cached_metadata);
}

// Slots

void MediaplayerMpdBackend::connectionKeepaliveTimeout(void)
{
	m_mpd_conn_mutex.lock();

	// Clear any lingering non-fatal errors
	if (!mpd_connection_clear_error(m_mpd_conn)) {
		// NOTE: There should likely be an attempt to reconnect here,
		//       but it definitely would complicate things for all the
		//       other users.
		qWarning() << "MPD connection in error state!";
		m_mpd_conn_mutex.unlock();
		return;
	}

	struct mpd_status *status = mpd_run_status(m_mpd_conn);
	if (!status) {
		qWarning() << "MPD connection status check failed";
	} else {
		mpd_status_free(status);
	}

	m_mpd_conn_mutex.unlock();
}

void MediaplayerMpdBackend::songPositionTimeout(void)
{
	m_state_mutex.lock();

	if (m_playing) {
		// Instead of the expense of repeatedly calling mpd_run_status,
		// provide our own elapsed time.  In practice this seems
		// sufficient for reasonable behavior in the application UI, and
		// it is what seems recommended for MPD client implementations.
		m_song_pos_ms += 250;
		QVariantMap metadata;
		metadata["position"] = m_song_pos_ms;
		m_cached_metadata["position"] = m_song_pos_ms;
		emit positionMetadataUpdate(metadata);
	}

	m_state_mutex.unlock();
}

void MediaplayerMpdBackend::updatePlaybackState(int queue_pos, int song_pos_ms, bool state)
{
	m_state_mutex.lock();

	m_queue_pos = queue_pos;
	m_song_pos_ms = song_pos_ms;
	if (m_playing != state) {
		if (state) {
			// Start position timer
			m_song_pos_timer->start(250);
		} else {
			// Stop position timer
			m_song_pos_timer->stop();
		}
	}
	m_playing = state;

	m_state_mutex.unlock();
}

void MediaplayerMpdBackend::updateMetadata(QVariantMap metadata)
{
	// Update our cached metadata to allow fast refreshes upon request
	// without having to make an out of band query to MPD.
	//
	// Updates from the event handler thread that contain track
	// information are assumed to be complete with the exception of the
	// album art, which may be included in a follow up update if it is
	// present.

	if (metadata.contains("status")) {
		QString status = metadata.value("status").toString();
		if (status == "stopped") {
			// There's likely no track information as chances are
			// this is from hitting the end of the playlist, so
			// clear things out.
			// If there actually is track metadata, it'll be
			// handled by the logic below.
			m_cached_metadata.clear();
		}
		m_cached_metadata["status"] = metadata["status"];
	}

	if (metadata.contains("track")) {
		QVariantMap track = metadata.value("track").toMap();
		m_cached_metadata["track"] = track;
	}

	// After playback starts, position updates will come from our local
	// timer, but take the initial value if present.
	if (metadata.contains("position"))
		m_cached_metadata["position"] = metadata["position"];

	// Send update up to front end
	emit metadataUpdate(metadata);
}

// Control methods

void MediaplayerMpdBackend::play()
{
	m_mpd_conn_mutex.lock();

	m_state_mutex.lock();
	if (!m_playing) {
		mpd_run_play(m_mpd_conn);
	}
	m_state_mutex.unlock();

	m_mpd_conn_mutex.unlock();
}

void MediaplayerMpdBackend::pause()
{
	m_mpd_conn_mutex.lock();

	m_state_mutex.lock();
	if (m_playing) {
		mpd_run_pause(m_mpd_conn, true);
	}
	m_state_mutex.unlock();

	m_mpd_conn_mutex.unlock();
}

void MediaplayerMpdBackend::previous()
{
	m_mpd_conn_mutex.lock();

	// MPD only allows next/previous if playing
	m_state_mutex.lock();
	if (m_playing) {
		mpd_run_previous(m_mpd_conn);
	}
	m_state_mutex.unlock();

	m_mpd_conn_mutex.unlock();
}

void MediaplayerMpdBackend::next()
{
	m_mpd_conn_mutex.lock();

	// MPD only allows next/previous if playing
	m_state_mutex.lock();
	if (m_playing) {
		mpd_run_next(m_mpd_conn);
	}
	m_state_mutex.unlock();

	m_mpd_conn_mutex.unlock();
}

void MediaplayerMpdBackend::seek(int milliseconds)
{
	m_mpd_conn_mutex.lock();

	float t = milliseconds;
	t /= 1000.0;
	mpd_run_seek_current(m_mpd_conn, t, false);

	m_mpd_conn_mutex.unlock();
}

// Relative to current position
void MediaplayerMpdBackend::fastforward(int milliseconds)
{
	m_mpd_conn_mutex.lock();

	float t = milliseconds;
	t /= 1000.0;
	mpd_run_seek_current(m_mpd_conn, t, true);

	m_mpd_conn_mutex.unlock();
}

// Relative to current position
void MediaplayerMpdBackend::rewind(int milliseconds)
{
	m_mpd_conn_mutex.lock();

	float t = -milliseconds;
	t /= 1000.0;
	mpd_run_seek_current(m_mpd_conn, t, true);

	m_mpd_conn_mutex.unlock();
}

void MediaplayerMpdBackend::picktrack(int track)
{
	m_mpd_conn_mutex.lock();

	if (track >= 0) {
		mpd_run_play_pos(m_mpd_conn, track);
	}

	m_mpd_conn_mutex.unlock();
}

void MediaplayerMpdBackend::volume(int volume)
{
	// Not implemented
}

void MediaplayerMpdBackend::loop(QString state)
{
	m_mpd_conn_mutex.lock();

	// Song:
	// mpd_run_single_state(m_mpd_conn, MPD_SINGLE_ON)
	// mpd_run_repeat(m_mpd_conn, true) to loop
	//
	// Playlist:
	// mpd_run_single_state(m_mpd_conn, MPD_SINGLE_OFF) (default)
	// mpd_run_repeat(m_mpd_conn, true) to loop

	if (state == "playlist") {
		mpd_run_repeat(m_mpd_conn, true);
	} else {
		mpd_run_repeat(m_mpd_conn, false);
	}

	m_mpd_conn_mutex.unlock();
}