eclient now passes NULL for 555 responses rather than calling the
[disorder] / clients / playrtp-alsa.c
1 /*
2 * This file is part of DisOrder.
3 * Copyright (C) 2008 Richard Kettlewell
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, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18 * USA
19 */
20 /** @file clients/playrtp-alsa.c
21 * @brief RTP player - ALSA support
22 *
23 * This has been rewritten to use the @ref alsabg.h interface and is therefore
24 * now closely modelled on @ref playrtp-coreaudio.c. Given a similar interface
25 * wrapping OSS the whole of playrtp could probably be greatly simplified.
26 */
27
28 #include <config.h>
29
30 #if HAVE_ALSA_ASOUNDLIB_H
31 #include "types.h"
32
33 #include <poll.h>
34 #include <alsa/asoundlib.h>
35 #include <assert.h>
36 #include <pthread.h>
37 #include <arpa/inet.h>
38
39 #include "mem.h"
40 #include "log.h"
41 #include "vector.h"
42 #include "heap.h"
43 #include "playrtp.h"
44 #include "alsabg.h"
45
46 /** @brief Callback from alsa_bg_collect() */
47 static int playrtp_alsa_supply(void *dst,
48 unsigned supply_nsamples) {
49 unsigned samples_available;
50
51 pthread_mutex_lock(&lock);
52 const struct packet *p = playrtp_next_packet();
53 if(p && contains(p, next_timestamp)) {
54 /* This packet is ready to play */
55 const uint32_t packet_end = p->timestamp + p->nsamples;
56 const uint32_t offset = next_timestamp - p->timestamp;
57 const uint16_t *src = (void *)(p->samples_raw + offset);
58 samples_available = packet_end - next_timestamp;
59 if(samples_available > supply_nsamples)
60 samples_available = supply_nsamples;
61 next_timestamp += samples_available;
62 memcpy(dst, src, samples_available * sizeof (int16_t));
63 /* We don't bother junking the packet - that'll be dealt with next time
64 * round */
65 } else {
66 /* No packet is ready to play (and there might be no packet at all) */
67 samples_available = p ? p->timestamp - next_timestamp : supply_nsamples;
68 if(samples_available > supply_nsamples)
69 samples_available = supply_nsamples;
70 /*info("infill %d", samples_available);*/
71 next_timestamp += samples_available;
72 /* Unlike Core Audio the buffer is not guaranteed to be 0-filled */
73 memset(dst, 0, samples_available * sizeof (int16_t));
74 }
75 pthread_mutex_unlock(&lock);
76 return samples_available;
77 }
78
79 void playrtp_alsa(void) {
80 alsa_bg_init(device ? device : "default",
81 playrtp_alsa_supply);
82 pthread_mutex_lock(&lock);
83 for(;;) {
84 /* Wait for the buffer to fill up a bit */
85 playrtp_fill_buffer();
86 /* Start playing now */
87 info("Playing...");
88 next_timestamp = pheap_first(&packets)->timestamp;
89 active = 1;
90 alsa_bg_enable();
91 /* Wait until the buffer empties out */
92 while(nsamples >= minbuffer
93 || (nsamples > 0
94 && contains(pheap_first(&packets), next_timestamp))) {
95 pthread_cond_wait(&cond, &lock);
96 }
97 /* Stop playing for a bit until the buffer re-fills */
98 alsa_bg_disable();
99 active = 0;
100 /* Go back round */
101 }
102 }
103
104 #endif
105
106 /*
107 Local Variables:
108 c-basic-offset:2
109 comment-column:40
110 fill-column:79
111 indent-tabs-mode:nil
112 End:
113 */