pipe.c 3.93 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2011 The Music Player Daemon Project
3
 * http://www.musicpd.org
4 5 6 7 8 9 10 11 12 13
 *
 * 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.
14 15 16 17
 *
 * 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.
18 19
 */

20
#include "config.h"
21
#include "pipe.h"
22
#include "buffer.h"
23
#include "chunk.h"
24

25
#include <glib.h>
26

27
#include <assert.h>
28

29 30 31
struct music_pipe {
	/** the first chunk */
	struct music_chunk *head;
32

33 34
	/** a pointer to the tail of the chunk */
	struct music_chunk **tail_r;
35

36 37
	/** the current number of chunks */
	unsigned size;
38

39 40
	/** a mutex which protects #head and #tail_r */
	GMutex *mutex;
41 42 43 44

#ifndef NDEBUG
	struct audio_format audio_format;
#endif
45
};
46

47 48
struct music_pipe *
music_pipe_new(void)
49
{
50
	struct music_pipe *mp = g_new(struct music_pipe, 1);
51

52 53 54 55
	mp->head = NULL;
	mp->tail_r = &mp->head;
	mp->size = 0;
	mp->mutex = g_mutex_new();
56

57 58 59 60
#ifndef NDEBUG
	audio_format_clear(&mp->audio_format);
#endif

61
	return mp;
62 63
}

64 65
void
music_pipe_free(struct music_pipe *mp)
66
{
67 68
	assert(mp->head == NULL);
	assert(mp->tail_r == &mp->head);
69

70 71
	g_mutex_free(mp->mutex);
	g_free(mp);
72 73
}

74
#ifndef NDEBUG
75

76 77 78 79 80 81 82 83 84 85
bool
music_pipe_check_format(const struct music_pipe *pipe,
			const struct audio_format *audio_format)
{
	assert(pipe != NULL);
	assert(audio_format != NULL);

	return !audio_format_defined(&pipe->audio_format) ||
		audio_format_equals(&pipe->audio_format, audio_format);
}
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105

bool
music_pipe_contains(const struct music_pipe *mp,
		    const struct music_chunk *chunk)
{
	g_mutex_lock(mp->mutex);

	for (const struct music_chunk *i = mp->head;
	     i != NULL; i = i->next) {
		if (i == chunk) {
			g_mutex_unlock(mp->mutex);
			return true;
		}
	}

	g_mutex_unlock(mp->mutex);

	return false;
}

106 107
#endif

108 109 110 111 112 113
const struct music_chunk *
music_pipe_peek(const struct music_pipe *mp)
{
	return mp->head;
}

114
struct music_chunk *
115
music_pipe_shift(struct music_pipe *mp)
116
{
117
	struct music_chunk *chunk;
118

119
	g_mutex_lock(mp->mutex);
120

121 122
	chunk = mp->head;
	if (chunk != NULL) {
123 124
		assert(!music_chunk_is_empty(chunk));

125 126
		mp->head = chunk->next;
		--mp->size;
127

128 129 130
		if (mp->head == NULL) {
			assert(mp->size == 0);
			assert(mp->tail_r == &chunk->next);
131

132 133 134 135 136
			mp->tail_r = &mp->head;
		} else {
			assert(mp->size > 0);
			assert(mp->tail_r != &chunk->next);
		}
137 138 139 140

#ifndef NDEBUG
		/* poison the "next" reference */
		chunk->next = (void*)0x01010101;
141 142 143

		if (mp->size == 0)
			audio_format_clear(&mp->audio_format);
144
#endif
145
	}
146

147
	g_mutex_unlock(mp->mutex);
148

149
	return chunk;
150 151
}

152
void
153
music_pipe_clear(struct music_pipe *mp, struct music_buffer *buffer)
154
{
155
	struct music_chunk *chunk;
156

157 158
	while ((chunk = music_pipe_shift(mp)) != NULL)
		music_buffer_return(buffer, chunk);
159
}
160

161 162
void
music_pipe_push(struct music_pipe *mp, struct music_chunk *chunk)
163
{
164
	assert(!music_chunk_is_empty(chunk));
165
	assert(chunk->length == 0 || audio_format_valid(&chunk->audio_format));
166

167
	g_mutex_lock(mp->mutex);
168

169 170 171 172 173 174 175 176 177
	assert(mp->size > 0 || !audio_format_defined(&mp->audio_format));
	assert(!audio_format_defined(&mp->audio_format) ||
	       music_chunk_check_format(chunk, &mp->audio_format));

#ifndef NDEBUG
	if (!audio_format_defined(&mp->audio_format) && chunk->length > 0)
		mp->audio_format = chunk->audio_format;
#endif

178 179 180
	chunk->next = NULL;
	*mp->tail_r = chunk;
	mp->tail_r = &chunk->next;
181

182
	++mp->size;
183

184
	g_mutex_unlock(mp->mutex);
185 186
}

187 188
unsigned
music_pipe_size(const struct music_pipe *mp)
189
{
190 191 192 193
	g_mutex_lock(mp->mutex);
	unsigned size = mp->size;
	g_mutex_unlock(mp->mutex);
	return size;
194
}