~ubuntu-branches/ubuntu/utopic/mpd/utopic-proposed

« back to all changes in this revision

Viewing changes to src/input_stream.c

  • Committer: Package Import Robot
  • Author(s): Steve Kowalik
  • Date: 2013-11-12 18:17:40 UTC
  • mfrom: (2.2.36 sid)
  • Revision ID: package-import@ubuntu.com-20131112181740-72aa4zihehoobedp
Tags: 0.18.3-1ubuntu1
* Merge from Debian unstable.  Remaining changes:
  - Add libmp3lame-dev to Build-Depends, and enable LAME.
  - Read the user for the daemon from the config file in the init script.
  - Move avahi-daemon from Suggests to Recommends.
  - Added apport hook to include user configuration file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright (C) 2003-2011 The Music Player Daemon Project
3
 
 * http://www.musicpd.org
4
 
 *
5
 
 * This program is free software; you can redistribute it and/or modify
6
 
 * it under the terms of the GNU General Public License as published by
7
 
 * the Free Software Foundation; either version 2 of the License, or
8
 
 * (at your option) any later version.
9
 
 *
10
 
 * This program is distributed in the hope that it will be useful,
11
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 
 * GNU General Public License for more details.
14
 
 *
15
 
 * You should have received a copy of the GNU General Public License along
16
 
 * with this program; if not, write to the Free Software Foundation, Inc.,
17
 
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18
 
 */
19
 
 
20
 
#include "config.h"
21
 
#include "input_stream.h"
22
 
#include "input_registry.h"
23
 
#include "input_plugin.h"
24
 
#include "input/rewind_input_plugin.h"
25
 
 
26
 
#include <glib.h>
27
 
#include <assert.h>
28
 
 
29
 
static inline GQuark
30
 
input_quark(void)
31
 
{
32
 
        return g_quark_from_static_string("input");
33
 
}
34
 
 
35
 
struct input_stream *
36
 
input_stream_open(const char *url,
37
 
                  GMutex *mutex, GCond *cond,
38
 
                  GError **error_r)
39
 
{
40
 
        GError *error = NULL;
41
 
 
42
 
        assert(mutex != NULL);
43
 
        assert(error_r == NULL || *error_r == NULL);
44
 
 
45
 
        input_plugins_for_each_enabled(plugin) {
46
 
                struct input_stream *is;
47
 
 
48
 
                is = plugin->open(url, mutex, cond, &error);
49
 
                if (is != NULL) {
50
 
                        assert(is->plugin != NULL);
51
 
                        assert(is->plugin->close != NULL);
52
 
                        assert(is->plugin->read != NULL);
53
 
                        assert(is->plugin->eof != NULL);
54
 
                        assert(!is->seekable || is->plugin->seek != NULL);
55
 
 
56
 
                        is = input_rewind_open(is);
57
 
 
58
 
                        return is;
59
 
                } else if (error != NULL) {
60
 
                        g_propagate_error(error_r, error);
61
 
                        return NULL;
62
 
                }
63
 
        }
64
 
 
65
 
        g_set_error(error_r, input_quark(), 0, "Unrecognized URI");
66
 
        return NULL;
67
 
}
68
 
 
69
 
bool
70
 
input_stream_check(struct input_stream *is, GError **error_r)
71
 
{
72
 
        assert(is != NULL);
73
 
        assert(is->plugin != NULL);
74
 
 
75
 
        return is->plugin->check == NULL ||
76
 
                is->plugin->check(is, error_r);
77
 
}
78
 
 
79
 
void
80
 
input_stream_update(struct input_stream *is)
81
 
{
82
 
        assert(is != NULL);
83
 
        assert(is->plugin != NULL);
84
 
 
85
 
        if (is->plugin->update != NULL)
86
 
                is->plugin->update(is);
87
 
}
88
 
 
89
 
void
90
 
input_stream_wait_ready(struct input_stream *is)
91
 
{
92
 
        assert(is != NULL);
93
 
        assert(is->mutex != NULL);
94
 
        assert(is->cond != NULL);
95
 
 
96
 
        while (true) {
97
 
                input_stream_update(is);
98
 
                if (is->ready)
99
 
                        break;
100
 
 
101
 
                g_cond_wait(is->cond, is->mutex);
102
 
        }
103
 
}
104
 
 
105
 
void
106
 
input_stream_lock_wait_ready(struct input_stream *is)
107
 
{
108
 
        assert(is != NULL);
109
 
        assert(is->mutex != NULL);
110
 
        assert(is->cond != NULL);
111
 
 
112
 
        g_mutex_lock(is->mutex);
113
 
        input_stream_wait_ready(is);
114
 
        g_mutex_unlock(is->mutex);
115
 
}
116
 
 
117
 
bool
118
 
input_stream_seek(struct input_stream *is, goffset offset, int whence,
119
 
                  GError **error_r)
120
 
{
121
 
        assert(is != NULL);
122
 
        assert(is->plugin != NULL);
123
 
 
124
 
        if (is->plugin->seek == NULL)
125
 
                return false;
126
 
 
127
 
        return is->plugin->seek(is, offset, whence, error_r);
128
 
}
129
 
 
130
 
bool
131
 
input_stream_lock_seek(struct input_stream *is, goffset offset, int whence,
132
 
                       GError **error_r)
133
 
{
134
 
        assert(is != NULL);
135
 
        assert(is->plugin != NULL);
136
 
 
137
 
        if (is->plugin->seek == NULL)
138
 
                return false;
139
 
 
140
 
        if (is->mutex == NULL)
141
 
                /* no locking */
142
 
                return input_stream_seek(is, offset, whence, error_r);
143
 
 
144
 
        g_mutex_lock(is->mutex);
145
 
        bool success = input_stream_seek(is, offset, whence, error_r);
146
 
        g_mutex_unlock(is->mutex);
147
 
        return success;
148
 
}
149
 
 
150
 
struct tag *
151
 
input_stream_tag(struct input_stream *is)
152
 
{
153
 
        assert(is != NULL);
154
 
        assert(is->plugin != NULL);
155
 
 
156
 
        return is->plugin->tag != NULL
157
 
                ? is->plugin->tag(is)
158
 
                : NULL;
159
 
}
160
 
 
161
 
struct tag *
162
 
input_stream_lock_tag(struct input_stream *is)
163
 
{
164
 
        assert(is != NULL);
165
 
        assert(is->plugin != NULL);
166
 
 
167
 
        if (is->plugin->tag == NULL)
168
 
                return false;
169
 
 
170
 
        if (is->mutex == NULL)
171
 
                /* no locking */
172
 
                return input_stream_tag(is);
173
 
 
174
 
        g_mutex_lock(is->mutex);
175
 
        struct tag *tag = input_stream_tag(is);
176
 
        g_mutex_unlock(is->mutex);
177
 
        return tag;
178
 
}
179
 
 
180
 
bool
181
 
input_stream_available(struct input_stream *is)
182
 
{
183
 
        assert(is != NULL);
184
 
        assert(is->plugin != NULL);
185
 
 
186
 
        return is->plugin->available != NULL
187
 
                ? is->plugin->available(is)
188
 
                : true;
189
 
}
190
 
 
191
 
size_t
192
 
input_stream_read(struct input_stream *is, void *ptr, size_t size,
193
 
                  GError **error_r)
194
 
{
195
 
        assert(ptr != NULL);
196
 
        assert(size > 0);
197
 
 
198
 
        return is->plugin->read(is, ptr, size, error_r);
199
 
}
200
 
 
201
 
size_t
202
 
input_stream_lock_read(struct input_stream *is, void *ptr, size_t size,
203
 
                       GError **error_r)
204
 
{
205
 
        assert(ptr != NULL);
206
 
        assert(size > 0);
207
 
 
208
 
        if (is->mutex == NULL)
209
 
                /* no locking */
210
 
                return input_stream_read(is, ptr, size, error_r);
211
 
 
212
 
        g_mutex_lock(is->mutex);
213
 
        size_t nbytes = input_stream_read(is, ptr, size, error_r);
214
 
        g_mutex_unlock(is->mutex);
215
 
        return nbytes;
216
 
}
217
 
 
218
 
void input_stream_close(struct input_stream *is)
219
 
{
220
 
        is->plugin->close(is);
221
 
}
222
 
 
223
 
bool input_stream_eof(struct input_stream *is)
224
 
{
225
 
        return is->plugin->eof(is);
226
 
}
227
 
 
228
 
bool
229
 
input_stream_lock_eof(struct input_stream *is)
230
 
{
231
 
        assert(is != NULL);
232
 
        assert(is->plugin != NULL);
233
 
 
234
 
        if (is->mutex == NULL)
235
 
                /* no locking */
236
 
                return input_stream_eof(is);
237
 
 
238
 
        g_mutex_lock(is->mutex);
239
 
        bool eof = input_stream_eof(is);
240
 
        g_mutex_unlock(is->mutex);
241
 
        return eof;
242
 
}
243