1675073566696fdf9f85608f39df3d407db87813
[mafwsubrenderer] / tests / mafw-mock-pulseaudio.c
1 /*
2  * This file is a part of MAFW
3  *
4  * Copyright (C) 2007, 2008, 2009 Nokia Corporation, all rights reserved.
5  *
6  * Contact: Visa Smolander <visa.smolander@nokia.com>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public License
10  * as published by the Free Software Foundation; version 2.1 of
11  * the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21  * 02110-1301 USA
22  *
23  */
24
25 #include <glib.h>
26 #include "mafw-mock-pulseaudio.h"
27
28 typedef void pa_glib_mainloop;
29 typedef void pa_mainloop_api;
30 typedef void (*pa_context_notify_cb_t)(pa_context *c, void *userdata);
31 typedef guint pa_context_flags_t;
32 typedef void pa_spawn_api;
33 typedef void pa_operation;
34 typedef guint32 pa_volume_t;
35 typedef struct {
36         guint8 channels;
37         guint map[32];
38 } pa_channel_map;
39 typedef struct {
40         guint8 channels;
41         pa_volume_t values[32];
42 } pa_cvolume;
43 typedef struct {
44         const gchar *name;
45         pa_channel_map channel_map;
46         pa_cvolume volume;
47         const char *device;
48         gint mute;
49         gboolean volume_is_absolute;
50 } pa_ext_stream_restore_info;
51 typedef void (*pa_ext_stream_restore_read_cb_t)(
52         pa_context *c,
53         const pa_ext_stream_restore_info *info, int eol, void *userdata);
54 typedef void (*pa_ext_stream_restore_subscribe_cb_t)(pa_context *c,
55                                                      void *userdata);
56 typedef void (*pa_context_success_cb_t)(pa_context *c, int success,
57                                         void *userdata);
58 enum pa_context_state {
59   PA_CONTEXT_UNCONNECTED = 0,
60   PA_CONTEXT_CONNECTING,
61   PA_CONTEXT_AUTHORIZING,
62   PA_CONTEXT_SETTING_NAME,
63   PA_CONTEXT_READY,
64   PA_CONTEXT_FAILED,
65   PA_CONTEXT_TERMINATED
66 };
67 struct _pa_context {
68         pa_context_notify_cb_t state_cb;
69         gpointer state_cb_userdata;
70         enum pa_context_state state;
71         pa_ext_stream_restore_read_cb_t read_cb;
72         gpointer read_cb_userdata;
73         pa_ext_stream_restore_subscribe_cb_t subscribe_cb;
74         gpointer subscribe_cb_userdata;
75         pa_cvolume volume;
76         gboolean mute;
77 };
78
79 static pa_context *context = NULL;
80
81 pa_glib_mainloop *pa_glib_mainloop_new(GMainContext *c);
82 char *pa_get_binary_name(char *s, size_t l);
83 pa_mainloop_api *pa_glib_mainloop_get_api(pa_glib_mainloop *g);
84 pa_context *pa_context_new(pa_mainloop_api *mainloop, const char *name);
85 void pa_context_set_state_callback(pa_context *c, pa_context_notify_cb_t cb,
86                                    void *userdata);
87 int pa_context_connect(pa_context *c, const char *server,
88                        pa_context_flags_t flags, const pa_spawn_api *api);
89 gint pa_context_get_state(pa_context *c);
90 pa_operation *pa_ext_stream_restore2_read(pa_context *c,
91                                           pa_ext_stream_restore_read_cb_t cb,
92                                           void *userdata);
93 void pa_operation_unref(pa_operation *o);
94 pa_volume_t pa_cvolume_max(const pa_cvolume *volume);
95 void pa_ext_stream_restore_set_subscribe_cb(
96         pa_context *c,
97         pa_ext_stream_restore_subscribe_cb_t cb, void *userdata);
98 gint pa_operation_get_state(pa_operation *o);
99 void pa_operation_cancel(pa_operation *o);
100 void pa_glib_mainloop_free(pa_glib_mainloop *g);
101 pa_cvolume *pa_cvolume_init(pa_cvolume *a);
102 pa_cvolume *pa_cvolume_set(pa_cvolume *a, unsigned channels, pa_volume_t v);
103 pa_operation *pa_ext_stream_restore2_write(
104         pa_context *c, gint mode, const pa_ext_stream_restore_info *data[],
105         unsigned n, int apply_immediately, pa_context_success_cb_t cb,
106         void *userdata);
107 pa_operation *pa_ext_stream_restore_subscribe(pa_context *c, int enable,
108                                               pa_context_success_cb_t cb,
109                                               void *userdata);
110 void pa_context_unref(pa_context *c);
111
112 pa_glib_mainloop *pa_glib_mainloop_new(GMainContext *c)
113 {
114         return (gpointer) 0x1;
115 }
116
117 char *pa_get_binary_name(char *s, size_t l)
118 {
119         g_strlcpy(s, "mafw-gst-renderer-tests", l);
120
121         return NULL;
122 }
123
124 pa_mainloop_api *pa_glib_mainloop_get_api(pa_glib_mainloop *g)
125 {
126         return (gpointer) 0x1;
127 }
128
129 pa_context *pa_context_new(pa_mainloop_api *mainloop, const char *name)
130 {
131         pa_context *c = g_new0(pa_context, 1);
132
133         pa_cvolume_set(&c->volume, 1, 32000);
134
135         context = c;
136
137         return c;
138 }
139
140 void pa_context_set_state_callback(pa_context *c, pa_context_notify_cb_t cb,
141                                    void *userdata)
142 {
143         c->state_cb = cb;
144         c->state_cb_userdata = userdata;
145 }
146
147 static gboolean _pa_context_connect_idle(gpointer userdata)
148 {
149         pa_context *c = userdata;
150         c->state++;
151         if (c->state_cb != NULL) {
152                 c->state_cb(c, c->state_cb_userdata);
153         }
154         return c->state != PA_CONTEXT_READY;
155 }
156
157 int pa_context_connect(pa_context *c, const char *server,
158                        pa_context_flags_t flags, const pa_spawn_api *api)
159 {
160         g_idle_add(_pa_context_connect_idle, c);
161         return 1;
162 }
163
164 gint pa_context_get_state(pa_context *c)
165 {
166         return c->state;
167 }
168
169 static gboolean _pa_ext_stream_restore2_read_idle(gpointer userdata)
170 {
171         pa_context *c = userdata;
172         pa_ext_stream_restore_info info = { 0, };
173
174         info.name = "sink-input-by-media-role:x-maemo";
175         pa_cvolume_set(&info.volume, 1, c->volume.values[0]);
176         info.mute = c->mute;
177
178         c->read_cb(c, &info, 1, c->read_cb_userdata);
179
180         return FALSE;
181 }
182
183 pa_operation *pa_ext_stream_restore2_read(pa_context *c,
184                                           pa_ext_stream_restore_read_cb_t cb,
185                                           void *userdata)
186 {
187         c->read_cb = cb;
188         c->read_cb_userdata = userdata;
189         g_idle_add(_pa_ext_stream_restore2_read_idle, c);
190         return (gpointer) 0x1;
191 }
192
193 void pa_operation_unref(pa_operation *o)
194 {
195 }
196
197 pa_volume_t pa_cvolume_max(const pa_cvolume *volume)
198 {
199         return volume->values[0];
200 }
201
202 pa_operation *pa_ext_stream_restore_subscribe(pa_context *c, int enable,
203                                               pa_context_success_cb_t cb,
204                                               void *userdata)
205 {
206         if (cb != NULL) {
207                 cb(c, TRUE, userdata);
208         }
209         return (gpointer) 0x1;
210 }
211
212 void pa_ext_stream_restore_set_subscribe_cb(
213         pa_context *c,
214         pa_ext_stream_restore_subscribe_cb_t cb, void *userdata)
215 {
216         c->subscribe_cb = cb;
217         c->subscribe_cb_userdata = userdata;
218 }
219
220 gint pa_operation_get_state(pa_operation *o)
221 {
222         return 1;
223 }
224
225 void pa_operation_cancel(pa_operation *o)
226 {
227 }
228
229 void pa_context_unref(pa_context *c)
230 {
231         g_free(c);
232 }
233
234 void pa_glib_mainloop_free(pa_glib_mainloop *g)
235 {
236 }
237
238 pa_cvolume *pa_cvolume_init(pa_cvolume *a)
239 {
240         pa_cvolume_set(a, 1, 0);
241         return a;
242 }
243
244 pa_cvolume *pa_cvolume_set(pa_cvolume *a, unsigned channels, pa_volume_t v)
245 {
246         a->channels = 1;
247         a->values[0] = v;
248         return a;
249 }
250
251 static gboolean _pa_ext_stream_restore_write_idle(gpointer userdata)
252 {
253         pa_context *c = userdata;
254
255         if (c->subscribe_cb != NULL) {
256                 c->subscribe_cb(c, c->subscribe_cb_userdata);
257         }
258
259         return FALSE;
260 }
261
262 pa_operation *pa_ext_stream_restore2_write(
263         pa_context *c, gint mode, const pa_ext_stream_restore_info *data[],
264         unsigned n, int apply_immediately, pa_context_success_cb_t cb,
265         void *userdata)
266 {
267         const pa_ext_stream_restore_info *info = data[0];
268
269         pa_cvolume_set(&c->volume, 1, info->volume.values[0]);
270         c->mute = info->mute;
271
272         g_idle_add(_pa_ext_stream_restore_write_idle, c);
273
274         return (gpointer) 0x1;
275 }
276
277 static gboolean _pa_context_disconnect_idle(gpointer userdata)
278 {
279         pa_context *c = userdata;
280         c->state = PA_CONTEXT_TERMINATED;
281         if (c->state_cb != NULL) {
282                 c->state_cb(c, c->state_cb_userdata);
283         }
284         return FALSE;
285 }
286
287 void pa_context_disconnect(pa_context *c)
288 {
289         g_idle_add(_pa_context_disconnect_idle, c);
290 }
291
292 pa_context *pa_context_get_instance(void)
293 {
294         return context;
295 }