aboutsummaryrefslogtreecommitdiff
path: root/src/input/DespotifyInputPlugin.cxx
blob: 1e5a8c6063fbe2fca9037748d69d8e9eda64ed2d (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
/*
 * Copyright (C) 2011-2013 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 "config.h"
#include "DespotifyInputPlugin.hxx"
#include "DespotifyUtils.hxx"
#include "InputInternal.hxx"
#include "InputStream.hxx"
#include "InputPlugin.hxx"
#include "tag.h"

extern "C" {
#include <despotify.h>
}

#include <glib.h>

#include <unistd.h>
#include <string.h>
#include <errno.h>

#include <stdio.h>

struct DespotifyInputStream {
	struct input_stream base;

	struct despotify_session *session;
	struct ds_track *track;
	struct tag *tag;
	struct ds_pcm_data pcm;
	size_t len_available;
	bool eof;

	DespotifyInputStream(const char *uri,
			     Mutex &mutex, Cond &cond,
			     despotify_session *_session,
			     ds_track *_track)
		:base(input_plugin_despotify, uri, mutex, cond),
		 session(_session), track(_track),
		 tag(mpd_despotify_tag_from_track(track)),
		 len_available(0), eof(false) {

		memset(&pcm, 0, sizeof(pcm));

		/* Despotify outputs pcm data */
		base.mime = g_strdup("audio/x-mpd-cdda-pcm");
		base.ready = true;
	}

	~DespotifyInputStream() {
		if (tag != NULL)
			tag_free(tag);

		despotify_free_track(track);
	}
};

static void
refill_buffer(DespotifyInputStream *ctx)
{
	/* Wait until there is data */
	while (1) {
		int rc = despotify_get_pcm(ctx->session, &ctx->pcm);

		if (rc == 0 && ctx->pcm.len) {
			ctx->len_available = ctx->pcm.len;
			break;
		}
		if (ctx->eof == true)
			break;

		if (rc < 0) {
			g_debug("despotify_get_pcm error\n");
			ctx->eof = true;
			break;
		}

		/* Wait a while until next iteration */
		usleep(50 * 1000);
	}
}

static void callback(G_GNUC_UNUSED struct despotify_session* ds,
		int sig, G_GNUC_UNUSED void* data, void* callback_data)
{
	DespotifyInputStream *ctx = (DespotifyInputStream *)callback_data;

	switch (sig) {
	case DESPOTIFY_NEW_TRACK:
		break;

	case DESPOTIFY_TIME_TELL:
		break;

	case DESPOTIFY_TRACK_PLAY_ERROR:
		g_debug("Track play error\n");
		ctx->eof = true;
		ctx->len_available = 0;
		break;

	case DESPOTIFY_END_OF_PLAYLIST:
		ctx->eof = true;
		g_debug("End of playlist: %d\n", ctx->eof);
		break;
	}
}


static struct input_stream *
input_despotify_open(const char *url,
		     Mutex &mutex, Cond &cond,
		     G_GNUC_UNUSED GError **error_r)
{
	struct despotify_session *session;
	struct ds_link *ds_link;
	struct ds_track *track;

	if (!g_str_has_prefix(url, "spt://"))
		return NULL;

	session = mpd_despotify_get_session();
	if (!session)
		return NULL;

	ds_link = despotify_link_from_uri(url + 6);
	if (!ds_link) {
		g_debug("Can't find %s\n", url);
		return NULL;
	}
	if (ds_link->type != LINK_TYPE_TRACK) {
		despotify_free_link(ds_link);
		return NULL;
	}

	track = despotify_link_get_track(session, ds_link);
	despotify_free_link(ds_link);
	if (!track)
		return NULL;

	DespotifyInputStream *ctx =
		new DespotifyInputStream(url, mutex, cond,
					 session, track);

	if (!mpd_despotify_register_callback(callback, ctx)) {
		delete ctx;
		return NULL;
	}

	if (despotify_play(ctx->session, ctx->track, false) == false) {
		mpd_despotify_unregister_callback(callback);
		delete ctx;
		return NULL;
	}

	return &ctx->base;
}

static size_t
input_despotify_read(struct input_stream *is, void *ptr, size_t size,
	       G_GNUC_UNUSED GError **error_r)
{
	DespotifyInputStream *ctx = (DespotifyInputStream *)is;
	size_t to_cpy = size;

	if (ctx->len_available == 0)
		refill_buffer(ctx);

	if (ctx->len_available < size)
		to_cpy = ctx->len_available;
	memcpy(ptr, ctx->pcm.buf, to_cpy);
	ctx->len_available -= to_cpy;

	is->offset += to_cpy;

	return to_cpy;
}

static void
input_despotify_close(struct input_stream *is)
{
	DespotifyInputStream *ctx = (DespotifyInputStream *)is;

	mpd_despotify_unregister_callback(callback);
	delete ctx;
}

static bool
input_despotify_eof(struct input_stream *is)
{
	DespotifyInputStream *ctx = (DespotifyInputStream *)is;

	return ctx->eof;
}

static bool
input_despotify_seek(G_GNUC_UNUSED struct input_stream *is,
	       G_GNUC_UNUSED goffset offset, G_GNUC_UNUSED int whence,
	       G_GNUC_UNUSED GError **error_r)
{
	return false;
}

static struct tag *
input_despotify_tag(struct input_stream *is)
{
	DespotifyInputStream *ctx = (DespotifyInputStream *)is;
	struct tag *tag = ctx->tag;

	ctx->tag = NULL;

	return tag;
}

const struct input_plugin input_plugin_despotify = {
	"spt",
	nullptr,
	nullptr,
	input_despotify_open,
	input_despotify_close,
	nullptr,
	nullptr,
	.tag = input_despotify_tag,
	nullptr,
	input_despotify_read,
	input_despotify_eof,
	input_despotify_seek,
};