chiark / gitweb /
debug dump (mac only) for playrtp
[disorder] / clients / playrtp-coreaudio.c
CommitLineData
c593cf7c 1/*
2 * This file is part of DisOrder.
3 * Copyright (C) 2007 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-coreaudio.c
21 * @brief RTP player - Core Audio support
22 */
23
24#include <config.h>
25
26#if HAVE_COREAUDIO_AUDIOHARDWARE_H
27#include "types.h"
28
29#include <assert.h>
30#include <pthread.h>
31#include <CoreAudio/AudioHardware.h>
32
33#include "mem.h"
34#include "log.h"
35#include "vector.h"
36#include "heap.h"
37#include "playrtp.h"
38
39/** @brief Callback from Core Audio */
40static OSStatus adioproc
41 (AudioDeviceID attribute((unused)) inDevice,
42 const AudioTimeStamp attribute((unused)) *inNow,
43 const AudioBufferList attribute((unused)) *inInputData,
44 const AudioTimeStamp attribute((unused)) *inInputTime,
45 AudioBufferList *outOutputData,
46 const AudioTimeStamp attribute((unused)) *inOutputTime,
47 void attribute((unused)) *inClientData) {
48 UInt32 nbuffers = outOutputData->mNumberBuffers;
49 AudioBuffer *ab = outOutputData->mBuffers;
50 uint32_t samples_available;
51
52 pthread_mutex_lock(&lock);
53 while(nbuffers > 0) {
54 float *samplesOut = ab->mData;
55 size_t samplesOutLeft = ab->mDataByteSize / sizeof (float);
56
57 while(samplesOutLeft > 0) {
58 const struct packet *p = playrtp_next_packet();
59 if(p && contains(p, next_timestamp)) {
60 /* This packet is ready to play */
61 const uint32_t packet_end = p->timestamp + p->nsamples;
62 const uint32_t offset = next_timestamp - p->timestamp;
63 const uint16_t *ptr = (void *)(p->samples_raw + offset);
64
65 samples_available = packet_end - next_timestamp;
66 if(samples_available > samplesOutLeft)
67 samples_available = samplesOutLeft;
68 next_timestamp += samples_available;
69 samplesOutLeft -= samples_available;
e9b635a3
RK
70 if(dump_buffer) {
71 size_t n;
72
73 for(n = 0; n < samples_available; ++n) {
74 dump_buffer[dump_index++] = (int16_t)ntohs(ptr[n]);
75 dump_index %= dump_size;
76 }
77 }
c593cf7c 78 while(samples_available-- > 0)
79 *samplesOut++ = (int16_t)ntohs(*ptr++) * (0.5 / 32767);
80 /* We don't bother junking the packet - that'll be dealt with next time
81 * round */
82 } else {
83 /* No packet is ready to play (and there might be no packet at all) */
84 samples_available = p ? p->timestamp - next_timestamp
85 : samplesOutLeft;
86 if(samples_available > samplesOutLeft)
87 samples_available = samplesOutLeft;
88 //info("infill by %"PRIu32, samples_available);
89 /* Conveniently the buffer is 0 to start with */
90 next_timestamp += samples_available;
91 samplesOut += samples_available;
92 samplesOutLeft -= samples_available;
93 }
94 }
95 ++ab;
96 --nbuffers;
97 }
98 pthread_mutex_unlock(&lock);
99 return 0;
100}
101
102void playrtp_coreaudio(void) {
103 OSStatus status;
104 UInt32 propertySize;
105 AudioDeviceID adid;
106 AudioStreamBasicDescription asbd;
107
108 /* If this looks suspiciously like libao's macosx driver there's an
109 * excellent reason for that... */
110
111 /* TODO report errors as strings not numbers */
112 propertySize = sizeof adid;
113 status = AudioHardwareGetProperty(kAudioHardwarePropertyDefaultOutputDevice,
114 &propertySize, &adid);
115 if(status)
116 fatal(0, "AudioHardwareGetProperty: %d", (int)status);
117 if(adid == kAudioDeviceUnknown)
118 fatal(0, "no output device");
119 propertySize = sizeof asbd;
120 status = AudioDeviceGetProperty(adid, 0, false,
121 kAudioDevicePropertyStreamFormat,
122 &propertySize, &asbd);
123 if(status)
124 fatal(0, "AudioHardwareGetProperty: %d", (int)status);
125 D(("mSampleRate %f", asbd.mSampleRate));
126 D(("mFormatID %08lx", asbd.mFormatID));
127 D(("mFormatFlags %08lx", asbd.mFormatFlags));
128 D(("mBytesPerPacket %08lx", asbd.mBytesPerPacket));
129 D(("mFramesPerPacket %08lx", asbd.mFramesPerPacket));
130 D(("mBytesPerFrame %08lx", asbd.mBytesPerFrame));
131 D(("mChannelsPerFrame %08lx", asbd.mChannelsPerFrame));
132 D(("mBitsPerChannel %08lx", asbd.mBitsPerChannel));
133 D(("mReserved %08lx", asbd.mReserved));
134 if(asbd.mFormatID != kAudioFormatLinearPCM)
135 fatal(0, "audio device does not support kAudioFormatLinearPCM");
136 status = AudioDeviceAddIOProc(adid, adioproc, 0);
137 if(status)
138 fatal(0, "AudioDeviceAddIOProc: %d", (int)status);
139 pthread_mutex_lock(&lock);
140 for(;;) {
141 /* Wait for the buffer to fill up a bit */
142 playrtp_fill_buffer();
143 /* Start playing now */
144 info("Playing...");
145 next_timestamp = pheap_first(&packets)->timestamp;
146 active = 1;
147 status = AudioDeviceStart(adid, adioproc);
148 if(status)
149 fatal(0, "AudioDeviceStart: %d", (int)status);
150 /* Wait until the buffer empties out */
151 while(nsamples >= minbuffer
152 || (nsamples > 0
153 && contains(pheap_first(&packets), next_timestamp)))
154 pthread_cond_wait(&cond, &lock);
155 /* Stop playing for a bit until the buffer re-fills */
156 status = AudioDeviceStop(adid, adioproc);
157 if(status)
158 fatal(0, "AudioDeviceStop: %d", (int)status);
159 active = 0;
160 /* Go back round */
161 }
162}
163
164#endif
165
166/*
167Local Variables:
168c-basic-offset:2
169comment-column:40
170fill-column:79
171indent-tabs-mode:nil
172End:
173*/