summaryrefslogtreecommitdiff
path: root/swfdec-gtk/swfdec_playback_pulse.c
blob: e88400548ead0fc99e3fe4991b0197844bc69cae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/* Swfdec
 * Copyright © 2006 Benjamin Otte <otte@gnome.org>
 * Copyright © 2007 Eric Anholt <eric@anholt.net>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA  02110-1301  USA
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <stdlib.h>
#include <string.h>

#include "swfdec_playback.h"
#include "pulse/pulseaudio.h"
#include "pulse/glib-mainloop.h"

/** @file Implements swfdec audio playback by dumping swfdec streams out
 * using pulseaudio streams.
 */

/*** DEFINITIONS ***/

struct _SwfdecPlayback {
  SwfdecPlayer *	player;
  GList *		streams;	/* all Stream objects */
  GMainContext *	context;	/* glib context we work in */
  pa_glib_mainloop *	pa_mainloop;	/* PA to glib mainloop connection */
  pa_context *		pa;		/* PA context for sound rendering */
};

typedef struct {
  SwfdecPlayback *     	sound;		/* reference to sound object */
  SwfdecAudio *		audio;		/* the audio we play back */
  guint			offset;		/* offset into sound */
  pa_stream *		pa;		/* PA stream */
  pa_cvolume		volume;		/* Volume control.  Not yet used. */
  gboolean		no_more;
} Stream;

/* Size of one of our audio samples, in bytes */
#define SAMPLESIZE	2
#define CHANNELS	2

/*** STREAMS ***/

static void
stream_write_callback (pa_stream *pa,
		       size_t bytes,
		       void *data)
{
  Stream *stream = data;
  char *frag;
  unsigned int samples = bytes / SAMPLESIZE / CHANNELS;
  int err;

  if (stream->no_more)
    return;

  /* Adjust to our rounded-down number */
  bytes = samples * SAMPLESIZE * CHANNELS;

  frag = g_try_malloc0 (bytes);
  if (frag == NULL) {
    g_printerr ("Failed to allocate fragment of size %d\n", (int)bytes);
    return;
  }

  /* Set up our fragment and render swfdec's audio into it. The swfdec audio
   * decoder renders deltas from the existing data in the fragment.
   */
  swfdec_audio_render (stream->audio, (gint16 *)frag, stream->offset,
		       samples);

  /* Send the new fragment out the PA stream */
  err = pa_stream_write (pa, frag, bytes, NULL, 0, PA_SEEK_RELATIVE);
  if (err != 0) {
    g_printerr ("Failed to write fragment to PA stream: %s\n",
		pa_strerror(pa_context_errno(stream->sound->pa)));
  }

  /* Advance playback pointer */
  stream->offset += samples;

  g_free (frag);
}

static void
stream_drain_complete (pa_stream *pa, int success, void *data)
{
  Stream *stream = data;

  pa_stream_disconnect (stream->pa);
  pa_stream_unref (stream->pa);
  g_object_unref (stream->audio);
  g_free (stream);
}

static void
swfdec_playback_stream_close (Stream *stream)
{
  /* Pull it off of the active stream list. */
  stream->sound->streams = g_list_remove (stream->sound->streams, stream);

  /* If we have created a PA stream, defer freeing until we drain it. */
  if (stream->pa != NULL) {
    pa_operation *o;

    stream->no_more = 1;

    o = pa_stream_drain (stream->pa, stream_drain_complete, stream);
    if (o != NULL) {
      pa_operation_unref (o);
      return;
    } else {
      g_printerr("PA stream drain failed: %s\n",
		 pa_strerror(pa_context_errno(stream->sound->pa)));
    }
  }
  g_object_unref (stream->audio);
  g_free (stream);
}

static void
stream_state_callback (pa_stream *pa, void *data)
{
  switch (pa_stream_get_state(pa)) {
  case PA_STREAM_CREATING:
  case PA_STREAM_TERMINATED:
  case PA_STREAM_READY:
  case PA_STREAM_UNCONNECTED:
    break;

  case PA_STREAM_FAILED:
    g_printerr("PA stream failed: %s\n",
	       pa_strerror(pa_context_errno(pa_stream_get_context(pa))));
  default:
    break;
  }
}

static void
swfdec_playback_stream_open (SwfdecPlayback *sound, SwfdecAudio *audio)
{
  Stream *stream;
  pa_sample_spec spec = {
    .format = PA_SAMPLE_S16LE,
    .rate = 44100,
    .channels = CHANNELS,
  };
  int err;

  stream = g_new0 (Stream, 1);
  stream->sound = sound;
  stream->audio = g_object_ref (audio);
  sound->streams = g_list_prepend (sound->streams, stream);

  /* If we failed to initialize the context, don't try to create the stream.
   * We still have to get put in the list, because swfdec_playback.c expects
   * to find it in the list for removal later.
   */
  if (sound->pa == NULL)
    return;

  /* Create our stream */
  stream->pa = pa_stream_new(sound->pa,
			     "swfdec stream",
			     &spec,
			     NULL /* Default channel map */
			     );
  if (stream->pa == NULL) {
    g_printerr("Failed to create PA stream\n");
    swfdec_playback_stream_close(stream);
    return;
  }

  /* Start at default volume */
  pa_cvolume_set(&stream->volume, CHANNELS, PA_VOLUME_NORM);

  /* Hook up our stream write callback for when new data is needed */
  pa_stream_set_state_callback(stream->pa, stream_state_callback, stream);
  pa_stream_set_write_callback(stream->pa, stream_write_callback, stream);

  /* Connect it up as a playback stream. */
  err = pa_stream_connect_playback(stream->pa,
				   NULL, /* Default device */
				   NULL /* Default buffering */,
				   0, /* No flags */
				   &stream->volume,
				   NULL /* Don't sync to any stream */
				   );
  if (err != 0) {
    g_printerr ("Failed to connect PA stream: %s\n",
		pa_strerror(pa_context_errno(sound->pa)));
    swfdec_playback_stream_close(stream);
    return;
  }
}

/*** SOUND ***/

static void
advance_before (SwfdecPlayer *player, guint msecs, guint audio_samples, gpointer data)
{
  SwfdecPlayback *sound = data;
  GList *walk;

  for (walk = sound->streams; walk; walk = walk->next) {
    Stream *stream = walk->data;
    if (audio_samples >= stream->offset) {
      stream->offset = 0;
    } else {
      stream->offset -= audio_samples;
    }
  }
}
 
static void
audio_added (SwfdecPlayer *player, SwfdecAudio *audio, SwfdecPlayback *sound)
{
  swfdec_playback_stream_open (sound, audio);
}

static void
audio_removed (SwfdecPlayer *player, SwfdecAudio *audio, SwfdecPlayback *sound)
{
  GList *walk;

  for (walk = sound->streams; walk; walk = walk->next) {
    Stream *stream = walk->data;
    if (stream->audio == audio) {
      swfdec_playback_stream_close (stream);
      return;
    }
  }
  g_assert_not_reached ();
}
static void
context_state_callback (pa_context *pa, void *data)
{
  SwfdecPlayback *sound = data;

  switch (pa_context_get_state(pa)) {
  case PA_CONTEXT_FAILED:
    g_printerr ("PA context failed\n");
    pa_context_unref (pa);
    sound->pa = NULL;
    break;

  default:
  case PA_CONTEXT_TERMINATED:
  case PA_CONTEXT_UNCONNECTED:
  case PA_CONTEXT_CONNECTING:
  case PA_CONTEXT_AUTHORIZING:
  case PA_CONTEXT_SETTING_NAME:
  case PA_CONTEXT_READY:
    break;

  }
}

SwfdecPlayback *
swfdec_playback_open (SwfdecPlayer *player, GMainContext *context)
{
  SwfdecPlayback *sound;
  const GList *walk;
  pa_mainloop_api *pa_api;

  g_return_val_if_fail (SWFDEC_IS_PLAYER (player), NULL);
  g_return_val_if_fail (context != NULL, NULL);

  sound = g_new0 (SwfdecPlayback, 1);
  sound->player = player;
  g_signal_connect (player, "advance", G_CALLBACK (advance_before), sound);
  g_signal_connect (player, "audio-added", G_CALLBACK (audio_added), sound);
  g_signal_connect (player, "audio-removed", G_CALLBACK (audio_removed), sound);

  /* Create our mainloop attachment to glib.  XXX: I hope this means we don't
   * have to run the main loop using pa functions.
   */
  sound->pa_mainloop = pa_glib_mainloop_new (context);
  pa_api = pa_glib_mainloop_get_api (sound->pa_mainloop);

  sound->pa = pa_context_new (pa_api, "swfdec");

  pa_context_set_state_callback (sound->pa, context_state_callback, sound);
  pa_context_connect (sound->pa,
		      NULL, /* default server */
		      0, /* default flags */
		      NULL /* spawning api */
		      );

  for (walk = swfdec_player_get_audio (player); walk; walk = walk->next) {
    swfdec_playback_stream_open (sound, walk->data);
  }
  g_main_context_ref (context);
  sound->context = context;
  return sound;
}

static void
context_drain_complete (pa_context *pa, void *data)
{
  pa_context_disconnect (pa);
  pa_context_unref (pa);
}

void
swfdec_playback_close (SwfdecPlayback *sound)
{
  pa_operation *op;

#define REMOVE_HANDLER_FULL(obj,func,data,count) G_STMT_START {\
  if (g_signal_handlers_disconnect_by_func ((obj), \
	G_CALLBACK (func), (data)) != (count)) { \
    g_assert_not_reached (); \
  } \
} G_STMT_END
#define REMOVE_HANDLER(obj,func,data) REMOVE_HANDLER_FULL (obj, func, data, 1)

  while (sound->streams)
    swfdec_playback_stream_close (sound->streams->data);
  REMOVE_HANDLER (sound->player, advance_before, sound);
  REMOVE_HANDLER (sound->player, audio_added, sound);
  REMOVE_HANDLER (sound->player, audio_removed, sound);

  if (sound->pa != NULL) {
    op = pa_context_drain (sound->pa, context_drain_complete, NULL);
    if (op == NULL) {
      pa_context_disconnect (sound->pa);
      pa_context_unref (sound->pa);
    } else {
      pa_operation_unref (op);
    }
    pa_glib_mainloop_free (sound->pa_mainloop);
  }

  g_main_context_unref (sound->context);
  g_free (sound);
}