aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/cubeb_alsa.c
blob: 295b5d1787af2b34907c091b5d446ec8910ed735 (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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
/*
 * Copyright © 2011 Mozilla Foundation
 *
 * This program is made available under an ISC-style license.  See the
 * accompanying file LICENSE for details.
 */
#undef NDEBUG
#define _BSD_SOURCE
#define _POSIX_SOURCE
#include <pthread.h>
#include <sys/time.h>
#include <assert.h>
#include <limits.h>
#include <poll.h>
#include <unistd.h>
#include <alsa/asoundlib.h>
#include "cubeb/cubeb.h"

#define CUBEB_STREAM_MAX 16
#define UNUSED __attribute__ ((__unused__))

/* ALSA is not thread-safe.  snd_pcm_t instances are individually protected
   by the owning cubeb_stream's mutex.  snd_pcm_t creation and destruction
   is not thread-safe until ALSA 1.0.24 (see alsa-lib.git commit 91c9c8f1),
   so those calls must be wrapped in the following mutex. */
static pthread_mutex_t cubeb_alsa_mutex = PTHREAD_MUTEX_INITIALIZER;
static int cubeb_alsa_set_error_handler = 0;

typedef void (*poll_waitable_callback)(void * user_ptr, struct pollfd * fds, nfds_t nfds);
typedef void (*poll_timer_callback)(void * user_ptr);

struct mutex {
  pthread_mutex_t mutex;
  pthread_t owner;
  int locked;
};

struct poll_timer {
  struct poll_timer * next;
  struct poll_timer * prev;

  struct cubeb * context;

  struct timeval wakeup;

  poll_timer_callback callback;
  void * user_ptr;
};

struct poll_waitable {
  struct poll_waitable * next;
  struct poll_waitable * prev;

  struct cubeb * context;

  struct pollfd * saved_fds; /* A copy of the pollfds passed in at init time. */
  struct pollfd * fds; /* Pointer to this waitable's pollfds within struct cubeb's fds. */
  nfds_t nfds;

  poll_waitable_callback callback;
  void * user_ptr;

  unsigned int idle_count;
  unsigned int refs;
};

struct cubeb {
  pthread_t thread;

  struct poll_timer * timer;
  struct poll_waitable * waitable;

  /* fds and nfds are only updated by cubeb_run when rebuild is set. */
  struct pollfd * fds;
  nfds_t nfds;
  int rebuild;

  int shutdown;

  /* Control pipe for forcing poll to wake and rebuild fds or recalculate timeout. */
  int control_fd_read;
  int control_fd_write;

  /* Mutex for timer and waitable lists, must not be held while blocked in
     poll(2) or when writing to control_fd */
  struct mutex mutex;
  pthread_cond_t cond;

  int phase;

  unsigned int active_streams;

  struct poll_timer * watchdog_timer;
};

struct cubeb_stream {
  cubeb * context;
  struct mutex mutex;
  snd_pcm_t * pcm;
  cubeb_data_callback data_callback;
  cubeb_state_callback state_callback;
  void * user_ptr;
  snd_pcm_uframes_t write_position;
  snd_pcm_uframes_t last_position;
  snd_pcm_uframes_t buffer_size;
  snd_pcm_uframes_t period_size;
  cubeb_stream_params params;

  struct poll_waitable * waitable;
  struct poll_timer * timer;
};

static void
mutex_init(struct mutex * mtx)
{
  int r;

  r = pthread_mutex_init(&mtx->mutex, NULL);
  assert(r == 0);

  mtx->locked = 0;
}

static void
mutex_destroy(struct mutex * mtx)
{
  assert(mtx->locked == 0);
  pthread_mutex_destroy(&mtx->mutex);
}

static void
mutex_lock(struct mutex * mtx)
{
  int r;

  r = pthread_mutex_lock(&mtx->mutex);
  assert(r == 0);
  mtx->owner = pthread_self();
  mtx->locked = 1;
}

static void
mutex_unlock(struct mutex * mtx)
{
  int r;

  memset(&mtx->owner, 0, sizeof(pthread_t));
  mtx->locked = 0;
  r = pthread_mutex_unlock(&mtx->mutex);
  assert(r == 0);
}

static void
mutex_assert_held(struct mutex * mtx)
{
  assert(mtx->locked && pthread_equal(mtx->owner, pthread_self()));
}

static void
mutex_assert_not_held(struct mutex * mtx)
{
  assert(!mtx->locked || !pthread_equal(mtx->owner, pthread_self()));
}

static long
stream_data_callback(cubeb_stream * stm, void * user_ptr, void * buffer, long nframes)
{
  mutex_assert_not_held(&stm->mutex);
  return stm->data_callback(stm, user_ptr, buffer, nframes);
}

static int
stream_state_callback(cubeb_stream * stm, void * user_ptr, cubeb_state state)
{
  mutex_assert_not_held(&stm->mutex);
  return stm->state_callback(stm, user_ptr, state);
}

static int
any_revents(struct pollfd * fds, nfds_t nfds)
{
  nfds_t i;

  for (i = 0; i < nfds; ++i) {
    if (fds[i].revents) {
      return 1;
    }
  }

  return 0;
}

static int
cmp_timeval(struct timeval * a, struct timeval * b)
{
  if (a->tv_sec == b->tv_sec) {
    if (a->tv_usec == b->tv_usec) {
      return 0;
    }
    return a->tv_usec > b->tv_usec ? 1 : -1;
  }
  return a->tv_sec > b->tv_sec ? 1 : -1;
}

static int
timeval_to_relative_ms(struct timeval * tv)
{
  struct timeval now;
  struct timeval dt;
  long long t;

  gettimeofday(&now, NULL);
  if (cmp_timeval(tv, &now) <= 0) {
    return 0;
  }

  timersub(tv, &now, &dt);
  t = dt.tv_sec;
  t *= 1000;
  t += (dt.tv_usec + 500) / 1000;
  return t <= INT_MAX ? t : INT_MAX;
}

static void
pipe_init(int * read_fd, int * write_fd)
{
  int r;
  int fd[2];

  r = pipe(fd);
  assert(r == 0);

  *read_fd = fd[0];
  *write_fd = fd[1];
}

static void
set_close_on_exec(int fd)
{
  long flags;
  int r;

  assert(fd >= 0);

  flags = fcntl(fd, F_GETFD);
  assert(flags >= 0);

  r = fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
  assert(r == 0);
}

static void
set_non_block(int fd)
{
  long flags;
  int r;

  assert(fd >= 0);

  flags = fcntl(fd, F_GETFL);
  assert(flags >= 0);

  r = fcntl(fd, F_SETFL, flags | O_NONBLOCK);
  assert(r == 0);
}

static void
rebuild(struct cubeb * ctx)
{
  nfds_t nfds;
  int i;
  struct poll_waitable * item;

  mutex_assert_held(&ctx->mutex);
  assert(ctx->rebuild);

  nfds = 0;
  for (item = ctx->waitable; item; item = item->next) {
    nfds += item->nfds;
  }

  /* Special case: add control pipe fd. */
  nfds += 1;

  free(ctx->fds);
  ctx->fds = calloc(nfds, sizeof(struct pollfd));
  assert(ctx->fds);
  ctx->nfds = nfds;

  for (i = 0, item = ctx->waitable; item; item = item->next) {
    memcpy(&ctx->fds[i], item->saved_fds, item->nfds * sizeof(struct pollfd));
    item->fds = &ctx->fds[i];
    i += item->nfds;
  }

  /* Special case: add control pipe fd. */
  ctx->fds[i].fd = ctx->control_fd_read;
  ctx->fds[i].events = POLLIN | POLLERR;

  ctx->rebuild = 0;
}

static void
poll_woke(struct cubeb * ctx)
{
  ssize_t r;
  char dummy;

  r = read(ctx->control_fd_read, &dummy, 1);
  assert(dummy == 'x' && r == 1);
}

static void
poll_wake(struct cubeb * ctx)
{
  ssize_t r;
  char dummy;

  dummy = 'x';
  r = write(ctx->control_fd_write, &dummy, 1);
  assert(r == 1);
}

static struct poll_waitable *
poll_waitable_init(struct cubeb * ctx, struct pollfd * fds, nfds_t nfds,
                   poll_waitable_callback callback, void * user_ptr)
{
  struct poll_waitable * waitable;

  waitable = calloc(1, sizeof(struct poll_waitable));
  assert(waitable);
  waitable->context = ctx;

  waitable->saved_fds = calloc(nfds, sizeof(struct pollfd));
  assert(waitable->saved_fds);
  waitable->nfds = nfds;
  memcpy(waitable->saved_fds, fds, nfds * sizeof(struct pollfd));

  waitable->callback = callback;
  waitable->user_ptr = user_ptr;

  waitable->idle_count = 0;
  waitable->refs = 1;

  mutex_lock(&ctx->mutex);

  waitable->next = ctx->waitable;
  if (ctx->waitable) {
    ctx->waitable->prev = waitable;
  }
  ctx->waitable = waitable;
  ctx->rebuild = 1;

  poll_wake(ctx);
  mutex_unlock(&ctx->mutex);

  return waitable;
}

static void
poll_waitable_ref(struct poll_waitable * w)
{
  struct cubeb * ctx = w->context;

  mutex_assert_held(&ctx->mutex);
  w->refs += 1;
}

static void
poll_waitable_unref(struct poll_waitable * w)
{
  struct cubeb * ctx = w->context;

  mutex_lock(&ctx->mutex);

  w->refs -= 1;

  if (w->refs == 0) {
    if (w->next) {
      w->next->prev = w->prev;
    }
    if (w->prev) {
      w->prev->next = w->next;
    }

    if (ctx->waitable == w) {
      ctx->waitable = w->next;
    }

    free(w->saved_fds);
    free(w);

    ctx->rebuild = 1;
    poll_wake(ctx);
  }

  mutex_unlock(&ctx->mutex);
}

static struct poll_timer *
poll_timer_absolute_init(struct cubeb * ctx, struct timeval * wakeup,
                         poll_timer_callback callback, void * user_ptr)
{
  struct poll_timer * timer;
  struct poll_timer * item;

  timer = calloc(1, sizeof(*timer));
  assert(timer);
  timer->context = ctx;
  timer->wakeup = *wakeup;
  timer->callback = callback;
  timer->user_ptr = user_ptr;

  mutex_lock(&ctx->mutex);

  for (item = ctx->timer; item; item = item->next) {
    if (cmp_timeval(&timer->wakeup, &item->wakeup) < 0) {
      timer->next = item;
      timer->prev = item->prev;

      if (timer->prev) {
        timer->prev->next = timer;
      }
      item->prev = timer;

      break;
    }

    if (!item->next) {
      item->next = timer;
      timer->prev = item;
      break;
    }
  }

  if (!timer->prev) {
    ctx->timer = timer;
  }

  poll_wake(ctx);
  mutex_unlock(&ctx->mutex);

  return timer;
}

static struct poll_timer *
poll_timer_relative_init(struct cubeb * ctx, unsigned int ms,
                         poll_timer_callback callback, void * user_ptr)
{
  struct timeval wakeup;

  gettimeofday(&wakeup, NULL);
  wakeup.tv_sec += ms / 1000;
  wakeup.tv_usec += (ms % 1000) * 1000;

  return poll_timer_absolute_init(ctx, &wakeup, callback, user_ptr);
}

static void
poll_timer_destroy(struct poll_timer * t)
{
  struct cubeb * ctx = t->context;

  mutex_lock(&ctx->mutex);

  if (t->next) {
    t->next->prev = t->prev;
  }
  if (t->prev) {
    t->prev->next = t->next;
  }

  if (ctx->timer == t) {
    ctx->timer = t->next;
  }

  free(t);

  poll_wake(ctx);
  mutex_unlock(&ctx->mutex);
}

static void
poll_phase_wait(cubeb * ctx)
{
  int phase;

  mutex_lock(&ctx->mutex);
  phase = ctx->phase;
  while (ctx->phase == phase) {
    pthread_cond_wait(&ctx->cond, &ctx->mutex.mutex);
  }
  mutex_unlock(&ctx->mutex);
}

static int
cubeb_run(struct cubeb * ctx)
{
  int r;
  int timeout;
  struct poll_waitable * waitable;
  struct poll_timer * timer;
  struct poll_waitable ** ready;
  int i;

  mutex_lock(&ctx->mutex);

  if (ctx->rebuild) {
    rebuild(ctx);
  }

  timeout = -1;
  timer = ctx->timer;
  if (timer) {
    timeout = timeval_to_relative_ms(&timer->wakeup);
  }

  /* No timers or waitables, we're done. */
  if (timeout == -1 && ctx->nfds == 0) {
    return -1;
  }

  mutex_unlock(&ctx->mutex);
  r = poll(ctx->fds, ctx->nfds, timeout);
  mutex_lock(&ctx->mutex);

  if (r > 0) {
    if (ctx->fds[ctx->nfds - 1].revents & POLLIN) {
      poll_woke(ctx);
      if (ctx->shutdown) {
        mutex_unlock(&ctx->mutex);
        return -1;
      }
    }

    i = 0;
    ready = calloc(r, sizeof(struct poll_waitable *));

    /* TODO: Break once r pfds have been processed, ideally with a waitable
       list sorted by latency. */
    for (waitable = ctx->waitable; waitable; waitable = waitable->next) {
      if (waitable->fds && any_revents(waitable->fds, waitable->nfds)) {
        poll_waitable_ref(waitable);
        ready[i++] = waitable;
        waitable->idle_count = 0;
      }
    }

    mutex_unlock(&ctx->mutex);
    for (i = 0; i < r; ++i) {
      if (!ready[i]) {
        break;
      }
      ready[i]->callback(ready[i]->user_ptr, ready[i]->fds, ready[i]->nfds);
      poll_waitable_unref(ready[i]);
    }
    mutex_lock(&ctx->mutex);

    free(ready);
  } else if (r == 0) {
    assert(timer);
    mutex_unlock(&ctx->mutex);
    timer->callback(timer->user_ptr);
    mutex_lock(&ctx->mutex);
  }

  ctx->phase += 1;
  pthread_cond_broadcast(&ctx->cond);

  mutex_unlock(&ctx->mutex);

  return 0;
}

static void
cubeb_watchdog(void * context)
{
  cubeb * ctx = context;
  struct poll_waitable * waitable;
  struct poll_waitable * tmp[16];
  int broken_streams;
  int i;

  if (ctx->watchdog_timer) {
    poll_timer_destroy(ctx->watchdog_timer);
  }

  ctx->watchdog_timer = poll_timer_relative_init(ctx, 1000, cubeb_watchdog, ctx);

  mutex_lock(&ctx->mutex);
  /* XXX: Horrible hack -- if an active (registered) stream has been idle
     for 10 ticks of the watchdog, kill it and mark the stream in error.
     This works around a bug seen with older versions of ALSA and PulseAudio
     where streams would stop requesting new data despite still being
     logically active and playing. */
  broken_streams = 0;
  for (waitable = ctx->waitable; waitable; waitable = waitable->next) {
    waitable->idle_count += 1;
    if (waitable->idle_count >= 10) {
      poll_waitable_ref(waitable);
      tmp[broken_streams++] = waitable;
    }
  }

  mutex_unlock(&ctx->mutex);

  for (i = 0; i < broken_streams; ++i) {
    cubeb_stream * stm = tmp[i]->user_ptr;
    assert(tmp[i]->idle_count >= 10);
    mutex_lock(&stm->mutex);
    if (stm->waitable) {
      poll_waitable_unref(stm->waitable);
      stm->waitable = NULL;
    }
    mutex_unlock(&stm->mutex);
    poll_waitable_unref(tmp[i]);
    stream_state_callback(stm, stm->user_ptr, CUBEB_STATE_ERROR);
  }
}

static void
cubeb_drain_stream(void * stream)
{
  cubeb_stream * stm = stream;
  int drained = 0;

  mutex_lock(&stm->mutex);
  /* It's possible that the stream was stopped after the timer fired but
     before we locked the stream. */
  if (stm->timer) {
    poll_timer_destroy(stm->timer);
    stm->timer = NULL;
    drained = 1;
  }
  mutex_unlock(&stm->mutex);
  if (drained) {
    stream_state_callback(stm, stm->user_ptr, CUBEB_STATE_DRAINED);
  }
}

static void
cubeb_refill_stream(void * stream, struct pollfd * fds, nfds_t nfds)
{
  cubeb_stream * stm = stream;
  int r;
  unsigned short revents;
  snd_pcm_sframes_t avail;
  long got;
  void * p;

  mutex_lock(&stm->mutex);

  r = snd_pcm_poll_descriptors_revents(stm->pcm, fds, nfds, &revents);
  if (r < 0 || revents != POLLOUT) {
    /* This should be a stream error; it makes no sense for poll(2) to wake
       for this stream and then have the stream report that it's not ready.
       Unfortunately, this does happen, so just bail out and try again. */
    mutex_unlock(&stm->mutex);
    return;
  }

  avail = snd_pcm_avail_update(stm->pcm);
  if (avail == -EPIPE) {
    snd_pcm_recover(stm->pcm, avail, 1);
    avail = snd_pcm_avail_update(stm->pcm);
  }

  /* Failed to recover from an xrun, this stream must be broken. */
  if (avail < 0) {
    poll_waitable_unref(stm->waitable);
    stm->waitable = NULL;
    mutex_unlock(&stm->mutex);
    stream_state_callback(stm, stm->user_ptr, CUBEB_STATE_ERROR);
    return;
  }

  /* This should never happen. */
  if ((unsigned int) avail > stm->buffer_size) {
    avail = stm->buffer_size;
  }

  /* poll(2) claims this stream is active, so there should be some space
     available to write.  If avail is still zero here, the stream must be in
     a funky state, so recover and try again. */
  if (avail == 0) {
    snd_pcm_recover(stm->pcm, -EPIPE, 1);
    avail = snd_pcm_avail_update(stm->pcm);
    if (avail <= 0) {
      poll_waitable_unref(stm->waitable);
      stm->waitable = NULL;
      mutex_unlock(&stm->mutex);
      stream_state_callback(stm, stm->user_ptr, CUBEB_STATE_ERROR);
      return;
    }
  }

  p = calloc(1, snd_pcm_frames_to_bytes(stm->pcm, avail));
  assert(p);

  mutex_unlock(&stm->mutex);
  got = stream_data_callback(stm, stm->user_ptr, p, avail);
  mutex_lock(&stm->mutex);
  if (got < 0) {
    poll_waitable_unref(stm->waitable);
    stm->waitable = NULL;
    mutex_unlock(&stm->mutex);
    stream_state_callback(stm, stm->user_ptr, CUBEB_STATE_ERROR);
    return;
  }
  if (got > 0) {
    snd_pcm_sframes_t wrote = snd_pcm_writei(stm->pcm, p, got);
    if (wrote == -EPIPE) {
      snd_pcm_recover(stm->pcm, wrote, 1);
      wrote = snd_pcm_writei(stm->pcm, p, got);
    }
    assert(wrote >= 0 && wrote == got);
    stm->write_position += wrote;
  }
  if (got != avail) {
    long buffer_fill = stm->buffer_size - (avail - got);
    double buffer_time = (double) buffer_fill / stm->params.rate;

    /* Fill the remaining buffer with silence to guarantee at least a period has been written. */
    snd_pcm_writei(stm->pcm, (char *) p + got, avail - got);

    poll_waitable_unref(stm->waitable);
    stm->waitable = NULL;

    stm->timer = poll_timer_relative_init(stm->context, buffer_time * 1000,
                                          cubeb_drain_stream, stm);
  }

  free(p);
  mutex_unlock(&stm->mutex);
}

static void *
cubeb_run_thread(void * context)
{
  cubeb * ctx = context;
  int r;

  do {
    r = cubeb_run(ctx);
  } while (r >= 0);

  return NULL;
}

static int
cubeb_locked_pcm_open(snd_pcm_t ** pcm, snd_pcm_stream_t stream)
{
  int r;

  pthread_mutex_lock(&cubeb_alsa_mutex);
  r = snd_pcm_open(pcm, "default", stream, SND_PCM_NONBLOCK);
  pthread_mutex_unlock(&cubeb_alsa_mutex);

  return r;
}

static int
cubeb_locked_pcm_close(snd_pcm_t * pcm)
{
  int r;

  pthread_mutex_lock(&cubeb_alsa_mutex);
  r = snd_pcm_close(pcm);
  pthread_mutex_unlock(&cubeb_alsa_mutex);

  return r;
}

static cubeb_stream *
cubeb_new_stream(cubeb * ctx)
{
  cubeb_stream * stm = NULL;

  stm = calloc(1, sizeof(*stm));
  assert(stm);

  mutex_lock(&ctx->mutex);
  if (ctx->active_streams < CUBEB_STREAM_MAX) {
    ctx->active_streams += 1;
  } else {
    free(stm);
    stm = NULL;
  }
  mutex_unlock(&ctx->mutex);

  return stm;
}

static void
cubeb_free_stream(cubeb * ctx, cubeb_stream * stm)
{
  mutex_lock(&ctx->mutex);
  assert(ctx->active_streams >= 1);
  ctx->active_streams -= 1;
  mutex_unlock(&ctx->mutex);
  free(stm);
}

static void
silent_error_handler(char const * file UNUSED, int line UNUSED, char const * function UNUSED,
                     int err UNUSED, char const * fmt UNUSED, ...)
{
}

int
cubeb_init(cubeb ** context, char const * context_name UNUSED)
{
  cubeb * ctx;
  int r;
  pthread_attr_t attr;

  assert(context);
  *context = NULL;

  pthread_mutex_lock(&cubeb_alsa_mutex);
  if (!cubeb_alsa_set_error_handler) {
    snd_lib_error_set_handler(silent_error_handler);
    cubeb_alsa_set_error_handler = 1;
  }
  pthread_mutex_unlock(&cubeb_alsa_mutex);

  ctx = calloc(1, sizeof(*ctx));
  assert(ctx);

  pipe_init(&ctx->control_fd_read, &ctx->control_fd_write);

  set_close_on_exec(ctx->control_fd_read);
  set_non_block(ctx->control_fd_read);

  set_close_on_exec(ctx->control_fd_write);
  set_non_block(ctx->control_fd_write);

  mutex_init(&ctx->mutex);

  r = pthread_cond_init(&ctx->cond, NULL);
  assert(r == 0);

  ctx->phase = 0;

  /* Force an early rebuild when cubeb_run is first called to ensure fds and
   * nfds have been initialized. */
  ctx->rebuild = 1;

  cubeb_watchdog(ctx);

  r = pthread_attr_init(&attr);
  assert(r == 0);

  r = pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN);
  assert(r == 0);

  r = pthread_create(&ctx->thread, &attr, cubeb_run_thread, ctx);
  assert(r == 0);

  r = pthread_attr_destroy(&attr);
  assert(r == 0);

  ctx->active_streams = 0;

  *context = ctx;

  return CUBEB_OK;
}

void
cubeb_destroy(cubeb * ctx)
{
  int r;

  assert(ctx);
  assert(ctx->active_streams == 0);

  mutex_lock(&ctx->mutex);
  ctx->shutdown = 1;
  poll_wake(ctx);
  mutex_unlock(&ctx->mutex);

  r = pthread_join(ctx->thread, NULL);
  assert(r == 0);

  poll_timer_destroy(ctx->watchdog_timer);
  ctx->watchdog_timer = NULL;

  assert(!ctx->waitable && !ctx->timer);
  close(ctx->control_fd_read);
  close(ctx->control_fd_write);
  pthread_cond_destroy(&ctx->cond);
  mutex_destroy(&ctx->mutex);
  free(ctx->fds);

  free(ctx);
}

int
cubeb_stream_init(cubeb * context, cubeb_stream ** stream, char const * stream_name UNUSED,
                  cubeb_stream_params stream_params, unsigned int latency,
                  cubeb_data_callback data_callback, cubeb_state_callback state_callback,
                  void * user_ptr)
{
  cubeb_stream * stm;
  int r;
  snd_pcm_format_t format;

  assert(context && stream);

  *stream = NULL;

  if (stream_params.rate < 1 || stream_params.rate > 192000 ||
      stream_params.channels < 1 || stream_params.channels > 32 ||
      latency < 1 || latency > 2000) {
    return CUBEB_ERROR_INVALID_FORMAT;
  }

  switch (stream_params.format) {
  case CUBEB_SAMPLE_S16LE:
    format = SND_PCM_FORMAT_S16_LE;
    break;
  case CUBEB_SAMPLE_S16BE:
    format = SND_PCM_FORMAT_S16_BE;
    break;
  case CUBEB_SAMPLE_FLOAT32LE:
    format = SND_PCM_FORMAT_FLOAT_LE;
    break;
  case CUBEB_SAMPLE_FLOAT32BE:
    format = SND_PCM_FORMAT_FLOAT_BE;
    break;
  default:
    return CUBEB_ERROR_INVALID_FORMAT;
  }

  stm = cubeb_new_stream(context);
  if (!stm) {
    return CUBEB_ERROR;
  }

  stm->context = context;
  stm->data_callback = data_callback;
  stm->state_callback = state_callback;
  stm->user_ptr = user_ptr;
  stm->params = stream_params;

  mutex_init(&stm->mutex);

  r = cubeb_locked_pcm_open(&stm->pcm, SND_PCM_STREAM_PLAYBACK);
  if (r < 0) {
    cubeb_stream_destroy(stm);
    return CUBEB_ERROR;
  }

  r = snd_pcm_nonblock(stm->pcm, 1);
  assert(r == 0);

  r = snd_pcm_set_params(stm->pcm, format, SND_PCM_ACCESS_RW_INTERLEAVED,
                         stm->params.channels, stm->params.rate, 1,
                         latency * 1000);
  if (r < 0) {
    cubeb_stream_destroy(stm);
    return CUBEB_ERROR_INVALID_FORMAT;
  }

  r = snd_pcm_get_params(stm->pcm, &stm->buffer_size, &stm->period_size);
  assert(r == 0);

  *stream = stm;

  return CUBEB_OK;
}

void
cubeb_stream_destroy(cubeb_stream * stm)
{
  assert(stm && !stm->waitable && !stm->timer);

  mutex_lock(&stm->mutex);
  if (stm->pcm) {
    cubeb_locked_pcm_close(stm->pcm);
    stm->pcm = NULL;
  }
  mutex_unlock(&stm->mutex);
  mutex_destroy(&stm->mutex);

  cubeb_free_stream(stm->context, stm);
}

int
cubeb_stream_start(cubeb_stream * stm)
{
  int nfds;
  struct pollfd * fds;
  int r;

  assert(stm);

  mutex_lock(&stm->mutex);

  if (stm->waitable) {
    mutex_unlock(&stm->mutex);
    return CUBEB_OK;
  }

  snd_pcm_pause(stm->pcm, 0);

  nfds = snd_pcm_poll_descriptors_count(stm->pcm);
  assert(nfds > 0);

  fds = calloc(nfds, sizeof(struct pollfd));
  assert(fds);
  r = snd_pcm_poll_descriptors(stm->pcm, fds, nfds);
  assert(r == nfds);

  stm->waitable = poll_waitable_init(stm->context, fds, nfds, cubeb_refill_stream, stm);

  free(fds);
  mutex_unlock(&stm->mutex);

  return CUBEB_OK;
}

int
cubeb_stream_stop(cubeb_stream * stm)
{
  assert(stm);

  mutex_lock(&stm->mutex);

  if (stm->waitable) {
    poll_waitable_unref(stm->waitable);
    stm->waitable = NULL;
  }

  if (stm->timer) {
    poll_timer_destroy(stm->timer);
    stm->timer = NULL;
  }

  mutex_unlock(&stm->mutex);

  poll_phase_wait(stm->context);

  mutex_lock(&stm->mutex);

  snd_pcm_pause(stm->pcm, 1);

  mutex_unlock(&stm->mutex);

  return CUBEB_OK;
}

int
cubeb_stream_get_position(cubeb_stream * stm, uint64_t * position)
{
  snd_pcm_sframes_t delay;

  assert(stm && position);

  mutex_lock(&stm->mutex);

  delay = -1;
  if (snd_pcm_state(stm->pcm) != SND_PCM_STATE_RUNNING ||
      snd_pcm_delay(stm->pcm, &delay) != 0) {
    *position = stm->last_position;
    mutex_unlock(&stm->mutex);
    return CUBEB_OK;
  }

  assert(delay >= 0);

  *position = 0;
  if (stm->write_position >= (snd_pcm_uframes_t) delay) {
    *position = stm->write_position - delay;
  }

  stm->last_position = *position;

  mutex_unlock(&stm->mutex);
  return CUBEB_OK;
}