input_stream.h 5.61 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 21
#ifndef MPD_INPUT_STREAM_H
#define MPD_INPUT_STREAM_H
22

23
#include "check.h"
24
#include "gcc.h"
25

26
#include <glib.h>
27

28
#include <stddef.h>
29
#include <stdbool.h>
30
#include <sys/types.h>
31

Max Kellermann's avatar
Max Kellermann committed
32
struct Tag;
33
struct input_stream;
34

35 36
#ifdef __cplusplus
extern "C" {
37 38 39

#include "thread/Mutex.hxx"
#include "thread/Cond.hxx"
40

41 42 43 44
/**
 * Opens a new input stream.  You may not access it until the "ready"
 * flag is set.
 *
45 46 47 48 49
 * @param mutex a mutex that is used to protect this object; must be
 * locked before calling any of the public methods
 * @param cond a cond that gets signalled when the state of
 * this object changes; may be NULL if the caller doesn't want to get
 * notifications
50
 * @return an #input_stream object on success, NULL on error
51
 */
52
gcc_nonnull(1)
53
gcc_malloc
54
struct input_stream *
55
input_stream_open(const char *uri,
56
		  Mutex &mutex, Cond &cond,
57
		  GError **error_r);
58

59 60
#endif

61
/**
62
 * Close the input stream and free resources.
63 64
 *
 * The caller must not lock the mutex.
65
 */
66
gcc_nonnull(1)
67 68 69
void
input_stream_close(struct input_stream *is);

70 71 72 73 74 75 76 77 78
/**
 * Check for errors that may have occurred in the I/O thread.
 *
 * @return false on error
 */
gcc_nonnull(1)
bool
input_stream_check(struct input_stream *is, GError **error_r);

79 80 81 82
/**
 * Update the public attributes.  Call before accessing attributes
 * such as "ready" or "offset".
 */
83
gcc_nonnull(1)
84 85 86
void
input_stream_update(struct input_stream *is);

87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
/**
 * Wait until the stream becomes ready.
 *
 * The caller must lock the mutex.
 */
gcc_nonnull(1)
void
input_stream_wait_ready(struct input_stream *is);

/**
 * Wrapper for input_stream_wait_locked() which locks and unlocks the
 * mutex; the caller must not be holding it already.
 */
gcc_nonnull(1)
void
input_stream_lock_wait_ready(struct input_stream *is);

104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
gcc_nonnull_all gcc_pure
const char *
input_stream_get_mime_type(const struct input_stream *is);

gcc_nonnull_all
void
input_stream_override_mime_type(struct input_stream *is, const char *mime);

gcc_nonnull_all gcc_pure
goffset
input_stream_get_size(const struct input_stream *is);

gcc_nonnull_all gcc_pure
goffset
input_stream_get_offset(const struct input_stream *is);

gcc_nonnull_all gcc_pure
bool
input_stream_is_seekable(const struct input_stream *is);

124 125 126 127 128 129 130
/**
 * Determines whether seeking is cheap.  This is true for local files.
 */
gcc_pure gcc_nonnull(1)
bool
input_stream_cheap_seeking(const struct input_stream *is);

131 132 133 134
/**
 * Seeks to the specified position in the stream.  This will most
 * likely fail if the "seekable" flag is false.
 *
135 136
 * The caller must lock the mutex.
 *
137 138 139 140
 * @param is the input_stream object
 * @param offset the relative offset
 * @param whence the base of the seek, one of SEEK_SET, SEEK_CUR, SEEK_END
 */
141
gcc_nonnull(1)
142
bool
143 144
input_stream_seek(struct input_stream *is, goffset offset, int whence,
		  GError **error_r);
145

146 147 148 149 150 151 152 153 154
/**
 * Wrapper for input_stream_seek() which locks and unlocks the
 * mutex; the caller must not be holding it already.
 */
gcc_nonnull(1)
bool
input_stream_lock_seek(struct input_stream *is, goffset offset, int whence,
		       GError **error_r);

155 156
/**
 * Returns true if the stream has reached end-of-file.
157 158
 *
 * The caller must lock the mutex.
159
 */
160
gcc_nonnull(1)
161
gcc_pure
162
bool input_stream_eof(struct input_stream *is);
163

164 165 166 167 168
/**
 * Wrapper for input_stream_eof() which locks and unlocks the mutex;
 * the caller must not be holding it already.
 */
gcc_nonnull(1)
169
gcc_pure
170 171 172
bool
input_stream_lock_eof(struct input_stream *is);

173 174 175
/**
 * Reads the tag from the stream.
 *
176 177
 * The caller must lock the mutex.
 *
Max Kellermann's avatar
Max Kellermann committed
178
 * @return a tag object which must be freed by the caller, or nullptr
179 180
 * if the tag has not changed since the last call
 */
181
gcc_nonnull(1)
182
gcc_malloc
Max Kellermann's avatar
Max Kellermann committed
183
Tag *
184 185
input_stream_tag(struct input_stream *is);

186
/**
187 188 189 190
 * Wrapper for input_stream_tag() which locks and unlocks the
 * mutex; the caller must not be holding it already.
 */
gcc_nonnull(1)
191
gcc_malloc
Max Kellermann's avatar
Max Kellermann committed
192
Tag *
193 194 195 196 197 198
input_stream_lock_tag(struct input_stream *is);

/**
 * Returns true if the next read operation will not block: either data
 * is available, or end-of-stream has been reached, or an error has
 * occurred.
199
 *
200
 * The caller must lock the mutex.
201
 */
202
gcc_nonnull(1)
203
gcc_pure
204 205
bool
input_stream_available(struct input_stream *is);
206

207 208 209 210
/**
 * Reads data from the stream into the caller-supplied buffer.
 * Returns 0 on error or eof (check with input_stream_eof()).
 *
211 212
 * The caller must lock the mutex.
 *
213 214 215 216 217
 * @param is the input_stream object
 * @param ptr the buffer to read into
 * @param size the maximum number of bytes to read
 * @return the number of bytes read
 */
218
gcc_nonnull(1, 2)
219
size_t
220 221
input_stream_read(struct input_stream *is, void *ptr, size_t size,
		  GError **error_r);
222

223 224 225 226 227 228 229 230 231
/**
 * Wrapper for input_stream_tag() which locks and unlocks the
 * mutex; the caller must not be holding it already.
 */
gcc_nonnull(1, 2)
size_t
input_stream_lock_read(struct input_stream *is, void *ptr, size_t size,
		       GError **error_r);

232 233 234 235
#ifdef __cplusplus
}
#endif

236
#endif