summaryrefslogtreecommitdiff
path: root/swfdec-gtk/swfdec_playback_alsa.c
blob: 733c94ac5baf5b7b060eca65e60312d656a02e6b (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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
/* Swfdec
 * Copyright (C) 2006 Benjamin Otte <otte@gnome.org>
 *
 * 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 <alsa/asoundlib.h>
#include "swfdec_playback.h"

/* Why ALSA sucks for beginners:
 * - snd_pcm_delay is not sample-exact, but period-exact most of the time.
 *   Yay for getting told the time every 512 samples when a human notices
 *   a delay of 100 samples (oooops)
 * - lots of functions are simply not implemented. So the super-smart idea
 *   of using snd_pcm_rewind to avoid XRUNS and still get low latency has
 *   some issues when dmix just returns -EIO all of the time. That wouldn't
 *   be so bad if there was actually a way to query if it's supported.
 * - But to make up for all this, you have 10 hardware parameters, 10 
 *   software parameters and 10 configuration parameters. All of this is
 *   naturally supported on 10 driver APIs depending on kernel. So if your
 *   sound card seems to do weird stuff, that is not my fault.
 * Welcome to Linux sound in the 21st century.
 */

/*** DEFINITIONS ***/

struct _SwfdecPlayback {
  SwfdecPlayer *	player;
  GList *		streams;	/* all Stream objects */
  GMainContext *	context;	/* context we work in */
};

typedef struct _Stream Stream;
struct _Stream {
  SwfdecPlayback *     	sound;		/* reference to sound object */
  SwfdecAudio *		audio;		/* the audio we play back */
  snd_pcm_t *		pcm;		/* the pcm we play back to */
  GSource **		sources;	/* sources for writing data */
  guint			n_sources;	/* number of sources */
  gsize			offset;		/* offset into sound */
  gboolean		(* write)	(Stream *);
};

#define ALSA_TRY(func,msg) G_STMT_START{ \
  int err = func; \
  if (err < 0) \
    g_printerr (msg ": %s\n", snd_strerror (err)); \
}G_STMT_END

#define ALSA_ERROR(func,msg,retval) G_STMT_START { \
  int err = func; \
  if (err < 0) { \
    g_printerr (msg ": %s\n", snd_strerror (err)); \
    return retval; \
  } \
}G_STMT_END

/*** STREAMS ***/

static void
swfdec_playback_stream_remove_handlers (Stream *stream)
{
  guint i;

  for (i = 0; i < stream->n_sources; i++) {
    if (stream->sources[i]) {
      g_source_destroy (stream->sources[i]);
      g_source_unref (stream->sources[i]);
      stream->sources[i] = NULL;
    }
  }
}

static snd_pcm_uframes_t
write_player (Stream *stream, const snd_pcm_channel_area_t *dst, 
    snd_pcm_uframes_t offset, snd_pcm_uframes_t avail)
{
  snd_pcm_uframes_t rendered;
  /* FIXME: do a long path if this doesn't hold */
  g_assert (dst[1].first - dst[0].first == 16);
  g_assert (dst[0].addr == dst[1].addr);
  g_assert (dst[0].step == dst[1].step);
  g_assert (dst[0].step == 32);

  rendered = swfdec_audio_render (stream->audio, (gint16 *) ((guint8 *) dst[0].addr + offset * dst[0].step / 8), 
      stream->offset, avail);
  //g_print ("rendering %u %u\n", stream->offset, (guint) avail);
  return rendered;
}

static void swfdec_playback_stream_start (Stream *stream);

static snd_pcm_sframes_t
swfdec_playback_stream_avail_update (Stream *stream)
{
  snd_pcm_sframes_t avail;

  avail = snd_pcm_avail_update (stream->pcm);
  if (avail == -EPIPE) {
    /* We don't do a while (avail == -EPIPE) here, we don't want infloops on weird situations */
    swfdec_playback_stream_start (stream);
    avail = snd_pcm_avail_update (stream->pcm);
  }
  return avail;
}

static gboolean
try_write_mmap (Stream *stream)
{
  snd_pcm_sframes_t avail_result;
  snd_pcm_uframes_t offset, avail, rendered;
  const snd_pcm_channel_area_t *dst;

  do {
    avail_result = swfdec_playback_stream_avail_update (stream);
    ALSA_ERROR (avail_result, "snd_pcm_avail_update failed", FALSE);
    if (avail_result == 0)
      return TRUE;
    avail = avail_result;
    ALSA_ERROR (snd_pcm_mmap_begin (stream->pcm, &dst, &offset, &avail),
	"snd_pcm_mmap_begin failed", FALSE);
    //g_print ("  avail = %u\n", (guint) avail);

    rendered = write_player (stream, dst, offset, avail);
    if (snd_pcm_mmap_commit (stream->pcm, offset, rendered) < 0) {
      g_printerr ("snd_pcm_mmap_commit failed\n");
      return FALSE;
    }
    stream->offset += rendered;
    //g_print ("offset: %u (+%u)\n", stream->offset, (guint) avail);
  } while (rendered == avail);
  swfdec_playback_stream_remove_handlers (stream);
  return TRUE;
}

static gboolean
try_write_so_pa_gets_it (Stream *stream)
{
#define STEP 1024
  snd_pcm_sframes_t avail, step, written;
  gboolean finish = FALSE;

  avail = swfdec_playback_stream_avail_update (stream);
  ALSA_ERROR (avail, "snd_pcm_avail_update failed", FALSE);

  while (avail > 0 && !finish) {
    gint16 data[2 * STEP];

    step = MIN (avail, STEP);
    written = swfdec_audio_render (stream->audio, data, stream->offset, step);
    finish = written < step;
    step = snd_pcm_writei (stream->pcm, data, written);
    finish &= step == written;
    ALSA_ERROR (step, "snd_pcm_writei failed", FALSE);
    avail -= step;
    stream->offset += step;
  }

  if (finish) {
    swfdec_playback_stream_remove_handlers (stream);
    return FALSE;
  } else {
    return TRUE;
  }
#undef STEP
}

#define try_write(stream) ((stream)->write (stream))

static gboolean
handle_stream (GIOChannel *source, GIOCondition cond, gpointer data)
{
  Stream *stream = data;
  snd_pcm_state_t state;

  state = snd_pcm_state (stream->pcm);
  if (state != SND_PCM_STATE_RUNNING) {
    swfdec_playback_stream_start (stream);
    return TRUE;
  } else {
    return try_write (stream);
  }
}

static void
swfdec_playback_stream_install_handlers (Stream *stream)
{
  GIOChannel *channel;

  if (stream->n_sources > 0) {
    struct pollfd polls[stream->n_sources];
    guint i, count;
    if (stream->n_sources > 1)
      g_printerr ("attention: more than one fd!\n");
    count = snd_pcm_poll_descriptors (stream->pcm, polls, stream->n_sources);
    for (i = 0; i < count; i++) {
      if (stream->sources[i] != NULL)
	continue;
      channel = g_io_channel_unix_new (polls[i].fd);
      stream->sources[i] = g_io_create_watch (channel, polls[i].events);
      g_source_set_priority (stream->sources[i], G_PRIORITY_HIGH);
      g_source_set_callback (stream->sources[i], (GSourceFunc) handle_stream, stream, NULL);
      g_io_channel_unref (channel);
      g_source_attach (stream->sources[i], stream->sound->context);
    }
  }
}

static void
swfdec_playback_stream_start (Stream *stream)
{
  snd_pcm_state_t state = snd_pcm_state (stream->pcm);
  switch (state) {
    case SND_PCM_STATE_XRUN:
      ALSA_ERROR (snd_pcm_prepare (stream->pcm), "no prepare",);
      //g_print ("XRUN!\n");
      /* fall through */
    case SND_PCM_STATE_SUSPENDED:
    case SND_PCM_STATE_PREPARED:
      stream->offset = 0;
      //g_print ("offset: %u (delay: %ld)\n", sound->offset, delay);
      if (try_write (stream)) {
	if (stream->write == try_write_mmap) {
	  ALSA_ERROR (snd_pcm_start (stream->pcm), "error starting",);
	}
	swfdec_playback_stream_install_handlers (stream);
      }
      break;
    case SND_PCM_STATE_OPEN:
    case SND_PCM_STATE_SETUP:
    case SND_PCM_STATE_RUNNING:
    case SND_PCM_STATE_DRAINING:
    case SND_PCM_STATE_PAUSED:
    case SND_PCM_STATE_DISCONNECTED:
    default:
      g_assert_not_reached ();
  }
}

static void
swfdec_playback_stream_changed (SwfdecAudio *audio, Stream *stream)
{
#if 0
  snd_pcm_sframes_t back;

  /* doesn't exist in Hardy yet and is broken on dmix */
  back = snd_pcm_rewindable (stream->pcm);
  if (back <= 0)
    return;
  if ((gsize) back > stream->offset)
    back = stream->offset;

  back = snd_pcm_rewind (stream->pcm, back);
  if (back <= 0)
    return;

  stream->offset -= back;
  try_write (stream);
#endif
}

static void
swfdec_playback_stream_new_data (SwfdecAudio *audio, Stream *stream)
{
  swfdec_playback_stream_install_handlers (stream);
}

static void
swfdec_playback_stream_open (SwfdecPlayback *sound, SwfdecAudio *audio)
{
  Stream *stream;
  snd_pcm_t *ret;
  snd_pcm_hw_params_t *hw_params;
  guint rate;
  snd_pcm_uframes_t uframes;
  gboolean (* try_write) (Stream *);

  /* "default" uses dmix, and dmix ticks way slow, so this thingy here stutters */
  ALSA_ERROR (snd_pcm_open (&ret, "default", SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK),
      "Failed to open sound device", );

  snd_pcm_hw_params_alloca (&hw_params);
  if (snd_pcm_hw_params_any (ret, hw_params) < 0) {
    g_printerr ("No sound format available\n");
    return;
  }
  if (snd_pcm_hw_params_set_access (ret, hw_params, SND_PCM_ACCESS_MMAP_INTERLEAVED) >= 0) {
    try_write = try_write_mmap;
  } else if (snd_pcm_hw_params_set_access (ret, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED) >= 0) {
    try_write = try_write_so_pa_gets_it;
  } else {
    g_printerr ("Failed setting access\n");
    goto fail;
  }
  if (snd_pcm_hw_params_set_format (ret, hw_params, SND_PCM_FORMAT_S16) < 0) {
    g_printerr ("Failed setting format\n");
    goto fail;
  }
  if (snd_pcm_hw_params_set_channels (ret, hw_params, 2) < 0) {
    g_printerr ("Failed setting channels\n");
    goto fail;
  }
  rate = 44100;
  if (snd_pcm_hw_params_set_rate_near (ret, hw_params, &rate, 0) < 0) {
    g_printerr ("Failed setting rate\n");
    goto fail;
  }
  uframes = 16384;
  if (snd_pcm_hw_params_set_buffer_size_near (ret, hw_params, &uframes) < 0) {
    g_printerr ("Failed setting buffer size\n");
    goto fail;
  }
  if (snd_pcm_hw_params (ret, hw_params) < 0) {
    g_printerr ("Could not set hardware parameters\n");
    goto fail;
  }
#if 0
  {
    snd_output_t *log;
    snd_output_stdio_attach (&log, stderr, 0);
    snd_pcm_hw_params_dump (hw_params, log);
  }
#endif
  stream = g_new0 (Stream, 1);
  stream->write = try_write;
  stream->sound = sound;
  stream->audio = g_object_ref (audio);
  stream->pcm = ret;
  stream->n_sources = snd_pcm_poll_descriptors_count (ret);
  if (stream->n_sources > 0)
    stream->sources = g_new0 (GSource *, stream->n_sources);
  sound->streams = g_list_prepend (sound->streams, stream);
  g_signal_connect (stream->audio, "changed", 
      G_CALLBACK (swfdec_playback_stream_changed), stream);
  g_signal_connect (stream->audio, "new-data", 
      G_CALLBACK (swfdec_playback_stream_new_data), stream);
  swfdec_playback_stream_start (stream);
  return;

fail:
  snd_pcm_close (ret);
}

static void
swfdec_playback_stream_close (Stream *stream)
{
  ALSA_TRY (snd_pcm_close (stream->pcm), "failed closing");
  swfdec_playback_stream_remove_handlers (stream);
  g_free (stream->sources);
  stream->sound->streams = g_list_remove (stream->sound->streams, stream);
  g_signal_handlers_disconnect_by_func (stream->audio, 
      swfdec_playback_stream_changed, stream);
  g_signal_handlers_disconnect_by_func (stream->audio, 
      swfdec_playback_stream_new_data, stream);
  g_object_unref (stream->audio);
  g_free (stream);
}

/*** 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;
    }
  }
}

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

  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);
  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;
}

void
swfdec_playback_close (SwfdecPlayback *sound)
{
#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);
  g_main_context_unref (sound->context);
  g_free (sound);
}