aboutsummaryrefslogtreecommitdiffstats
path: root/src/metadata_pipe.c
blob: 83c36ce1ba9a6596c1df1c5f00c773ac5ebb513b (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
/* the Music Player Daemon (MPD)
 * Copyright (C) 2003-2007 by Warren Dukes (warren.dukes@gmail.com)
 * This project's homepage is: 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "metadata_pipe.h"
#include "ringbuf.h"
#include "decode.h"
#include "os_compat.h"
#include "log.h"
#include "outputBuffer.h"
#include "gcc.h"

static struct ringbuf *mp;

/* Each one of these is a packet inside the metadata pipe */
struct tag_container {
	float metadata_time;
	uint8_t seq; /* ob.seq_decoder at time of metadata_pipe_send() */
	struct mpd_tag *tag; /* our payload */
};

/*
 * We have two readers even though ringbuf was designed for one (locklessly),
 * so we will use a lock to allow readers to safely read.  Writing is only
 * done from one thread, so it will never block or clobber.
 */
static pthread_mutex_t read_lock = PTHREAD_MUTEX_INITIALIZER;
static struct mpd_tag *current_tag; /* requires read_lock for both r/w access */

static void metadata_pipe_finish(void)
{
	ringbuf_free(mp);
	if (current_tag)
		tag_free(current_tag);
}

void init_metadata_pipe(void)
{
	mp = ringbuf_create(sizeof(struct tag_container) * 16);
	atexit(metadata_pipe_finish);
}

void metadata_pipe_send(struct mpd_tag *tag, float metadata_time)
{
	struct tag_container tc;
	size_t written;

	assert(pthread_equal(pthread_self(), dc.thread));

	if (mpd_unlikely(ringbuf_write_space(mp)
	                 < sizeof(struct tag_container))) {
		DEBUG("metadata_pipe: insufficient buffer space, dropping\n");
		tag_free(tag);
		return;
	}

	tc.tag = tag;
	tc.metadata_time = metadata_time;
	tc.seq = ob_get_decoder_sequence();
	written = ringbuf_write(mp, &tc, sizeof(struct tag_container));
	assert(written == sizeof(struct tag_container));
}

struct mpd_tag * metadata_pipe_recv(void)
{
	struct tag_container tc;
	size_t r;
	static const size_t uint8_t_max = 255; /* XXX CLEANUP */
	uint8_t expect_seq = ob_get_player_sequence();
	unsigned long current_time = ob_get_elapsed_time();
	struct mpd_tag *tag = NULL;

	if (pthread_mutex_trylock(&read_lock) == EBUSY)
		return NULL;
retry:
	if (!(r = ringbuf_peek(mp, &tc, sizeof(struct tag_container))))
		goto out;

	assert(r == sizeof(struct tag_container));
	assert(tc.tag);
	if (expect_seq == tc.seq) {
		if (current_time < tc.metadata_time)
			goto out; /* not ready for tag yet */
		if (tag_equal(tc.tag, current_tag)) {
			tag_free(tc.tag);
			ringbuf_read_advance(mp, sizeof(struct tag_container));
			goto out; /* nothing changed, don't bother */
		}
		tag = tag_dup(tc.tag);
		if (current_tag)
			tag_free(current_tag);
		current_tag = tc.tag;
		ringbuf_read_advance(mp, sizeof(struct tag_container));
	} else if (expect_seq > tc.seq ||
	           (!expect_seq && tc.seq == uint8_t_max)) {
		DEBUG("metadata_pipe: reader is ahead of writer\n");
		tag_free(tc.tag);
		ringbuf_read_advance(mp, sizeof(struct tag_container));
		goto retry; /* read and skip packets */
	} /* else not ready for tag yet */
out:
	pthread_mutex_unlock(&read_lock);
	return tag;
}

struct mpd_tag *metadata_pipe_current(void)
{
	struct mpd_tag *tag;

	assert(! pthread_equal(pthread_self(), dc.thread));
	if (pthread_mutex_trylock(&read_lock) == EBUSY)
		return NULL;
	tag = current_tag ? tag_dup(current_tag) : NULL;
	pthread_mutex_unlock(&read_lock);

	return tag;
}

void metadata_pipe_clear(void)
{
	struct tag_container tc;
	size_t r;

	pthread_mutex_lock(&read_lock);

	while ((r = ringbuf_read(mp, &tc, sizeof(struct tag_container)))) {
		assert(r == sizeof(struct tag_container));
		tag_free(tc.tag);
	}

	if (current_tag) {
		tag_free(current_tag);
		current_tag = NULL;
	}

	pthread_mutex_unlock(&read_lock);
}