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

32
struct input_stream;
33

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

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

40 41 42 43
/**
 * Opens a new input stream.  You may not access it until the "ready"
 * flag is set.
 *
44 45 46 47 48
 * @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
49
 * @return an #input_stream object on success, NULL on error
50
 */
51
gcc_nonnull(1)
52
gcc_malloc
53
struct input_stream *
54
input_stream_open(const char *uri,
55
		  Mutex &mutex, Cond &cond,
56
		  GError **error_r);
57

58 59
#endif

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

69 70 71 72 73 74 75 76 77
/**
 * 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);

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

86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
/**
 * 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);

103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
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);

123 124 125 126 127 128 129
/**
 * 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);

130 131 132 133
/**
 * Seeks to the specified position in the stream.  This will most
 * likely fail if the "seekable" flag is false.
 *
134 135
 * The caller must lock the mutex.
 *
136 137 138 139
 * @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
 */
140
gcc_nonnull(1)
141
bool
142 143
input_stream_seek(struct input_stream *is, goffset offset, int whence,
		  GError **error_r);
144

145 146 147 148 149 150 151 152 153
/**
 * 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);

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

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

172 173 174
/**
 * Reads the tag from the stream.
 *
175 176
 * The caller must lock the mutex.
 *
177 178 179
 * @return a tag object which must be freed with tag_free(), or NULL
 * if the tag has not changed since the last call
 */
180
gcc_nonnull(1)
181
gcc_malloc
182 183 184
struct tag *
input_stream_tag(struct input_stream *is);

185
/**
186 187 188 189
 * Wrapper for input_stream_tag() which locks and unlocks the
 * mutex; the caller must not be holding it already.
 */
gcc_nonnull(1)
190
gcc_malloc
191 192 193 194 195 196 197
struct tag *
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.
198
 *
199
 * The caller must lock the mutex.
200
 */
201
gcc_nonnull(1)
202
gcc_pure
203 204
bool
input_stream_available(struct input_stream *is);
205

206 207 208 209
/**
 * Reads data from the stream into the caller-supplied buffer.
 * Returns 0 on error or eof (check with input_stream_eof()).
 *
210 211
 * The caller must lock the mutex.
 *
212 213 214 215 216
 * @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
 */
217
gcc_nonnull(1, 2)
218
size_t
219 220
input_stream_read(struct input_stream *is, void *ptr, size_t size,
		  GError **error_r);
221

222 223 224 225 226 227 228 229 230
/**
 * 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);

231 232 233 234
#ifdef __cplusplus
}
#endif

235
#endif