aboutsummaryrefslogtreecommitdiffstats
path: root/src/playlist/flac_playlist_plugin.c
blob: 9d66fb3317e8252fc311a1a6436ad33a78e92092 (plain) (blame)
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
/*
 * Copyright (C) 2003-2010 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 "playlist/flac_playlist_plugin.h"
#include "playlist_plugin.h"
#include "tag.h"
#include "song.h"
#include "decoder/flac_metadata.h"

#include <FLAC/metadata.h>

#include <glib.h>

#undef G_LOG_DOMAIN
#define G_LOG_DOMAIN "flac"

#if defined(FLAC_API_VERSION_CURRENT) && FLAC_API_VERSION_CURRENT > 7

struct flac_playlist {
	struct playlist_provider base;

	char *uri;

	FLAC__StreamMetadata *cuesheet;
	FLAC__StreamMetadata streaminfo;

	unsigned next_track;
};

static struct playlist_provider *
flac_playlist_open_uri(const char *uri)
{
	if (!g_path_is_absolute(uri))
		/* only local files supported */
		return NULL;

	FLAC__StreamMetadata *cuesheet;
	if (!FLAC__metadata_get_cuesheet(uri, &cuesheet))
		return NULL;

	struct flac_playlist *playlist = g_new(struct flac_playlist, 1);
	playlist_provider_init(&playlist->base, &flac_playlist_plugin);

	if (!FLAC__metadata_get_streaminfo(uri, &playlist->streaminfo)) {
		FLAC__metadata_object_delete(playlist->cuesheet);
		g_free(playlist);
		return NULL;
	}

	playlist->uri = g_strdup(uri);
	playlist->cuesheet = cuesheet;
	playlist->next_track = 0;

	return &playlist->base;
}

static void
flac_playlist_close(struct playlist_provider *_playlist)
{
	struct flac_playlist *playlist = (struct flac_playlist *)_playlist;

	g_free(playlist->uri);
	FLAC__metadata_object_delete(playlist->cuesheet);
	g_free(playlist);
}

static struct song *
flac_playlist_read(struct playlist_provider *_playlist)
{
	struct flac_playlist *playlist = (struct flac_playlist *)_playlist;
	const FLAC__StreamMetadata_CueSheet *cs =
		&playlist->cuesheet->data.cue_sheet;

	/* find the next audio track */

	while (playlist->next_track < cs->num_tracks &&
	       (cs->tracks[playlist->next_track].number > cs->num_tracks ||
		cs->tracks[playlist->next_track].type != 0))
		++playlist->next_track;

	if (playlist->next_track >= cs->num_tracks)
		return NULL;

	FLAC__uint64 start = cs->tracks[playlist->next_track].offset;
	++playlist->next_track;
	FLAC__uint64 end = playlist->next_track < cs->num_tracks
		? cs->tracks[playlist->next_track].offset
		: playlist->streaminfo.data.stream_info.total_samples;

	struct song *song = song_file_new(playlist->uri, NULL);
	song->start_ms = start * 1000 /
		playlist->streaminfo.data.stream_info.sample_rate;
	song->end_ms = end * 1000 /
		playlist->streaminfo.data.stream_info.sample_rate;

	char track[16];
	g_snprintf(track, sizeof(track), "%u", playlist->next_track);
	song->tag = flac_tag_load(playlist->uri, track);
	if (song->tag == NULL)
		song->tag = tag_new();

	song->tag->time = end > start
		? ((end - start - 1 +
		    playlist->streaminfo.data.stream_info.sample_rate) /
		   playlist->streaminfo.data.stream_info.sample_rate)
		: 0;

	tag_clear_items_by_type(song->tag, TAG_TRACK);
	tag_add_item(song->tag, TAG_TRACK, track);

	return song;
}

static const char *const flac_playlist_suffixes[] = {
	"flac",
	NULL
};

static const char *const flac_playlist_mime_types[] = {
	"application/flac",
	"application/x-flac",
	"audio/flac",
	"audio/x-flac",
	NULL
};

const struct playlist_plugin flac_playlist_plugin = {
	.name = "flac",

	.open_uri = flac_playlist_open_uri,
	.close = flac_playlist_close,
	.read = flac_playlist_read,

	.suffixes = flac_playlist_suffixes,
	.mime_types = flac_playlist_mime_types,
};

#else /* FLAC_API_VERSION_CURRENT <= 7 */

static bool
flac_playlist_init(G_GNUC_UNUSED const struct config_param *param)
{
	/* this libFLAC version does not support embedded CUE sheets;
	   disable this plugin */
	return false;
}

const struct playlist_plugin flac_playlist_plugin = {
	.name = "flac",
	.init = flac_playlist_init,
};

#endif