Commit | Line | Data |
---|---|---|
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 */ | |
40 | static 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; | |
abf849cf RK |
93 | if(dump_buffer) { |
94 | size_t n; | |
95 | ||
96 | for(n = 0; n < samples_available; ++n) { | |
97 | dump_buffer[dump_index++] = 0; | |
98 | dump_index %= dump_size; | |
99 | } | |
100 | } | |
c593cf7c | 101 | } |
102 | } | |
103 | ++ab; | |
104 | --nbuffers; | |
105 | } | |
106 | pthread_mutex_unlock(&lock); | |
107 | return 0; | |
108 | } | |
109 | ||
110 | void playrtp_coreaudio(void) { | |
111 | OSStatus status; | |
112 | UInt32 propertySize; | |
113 | AudioDeviceID adid; | |
114 | AudioStreamBasicDescription asbd; | |
115 | ||
116 | /* If this looks suspiciously like libao's macosx driver there's an | |
117 | * excellent reason for that... */ | |
118 | ||
119 | /* TODO report errors as strings not numbers */ | |
120 | propertySize = sizeof adid; | |
121 | status = AudioHardwareGetProperty(kAudioHardwarePropertyDefaultOutputDevice, | |
122 | &propertySize, &adid); | |
123 | if(status) | |
124 | fatal(0, "AudioHardwareGetProperty: %d", (int)status); | |
125 | if(adid == kAudioDeviceUnknown) | |
126 | fatal(0, "no output device"); | |
127 | propertySize = sizeof asbd; | |
128 | status = AudioDeviceGetProperty(adid, 0, false, | |
129 | kAudioDevicePropertyStreamFormat, | |
130 | &propertySize, &asbd); | |
131 | if(status) | |
132 | fatal(0, "AudioHardwareGetProperty: %d", (int)status); | |
133 | D(("mSampleRate %f", asbd.mSampleRate)); | |
134 | D(("mFormatID %08lx", asbd.mFormatID)); | |
135 | D(("mFormatFlags %08lx", asbd.mFormatFlags)); | |
136 | D(("mBytesPerPacket %08lx", asbd.mBytesPerPacket)); | |
137 | D(("mFramesPerPacket %08lx", asbd.mFramesPerPacket)); | |
138 | D(("mBytesPerFrame %08lx", asbd.mBytesPerFrame)); | |
139 | D(("mChannelsPerFrame %08lx", asbd.mChannelsPerFrame)); | |
140 | D(("mBitsPerChannel %08lx", asbd.mBitsPerChannel)); | |
141 | D(("mReserved %08lx", asbd.mReserved)); | |
142 | if(asbd.mFormatID != kAudioFormatLinearPCM) | |
143 | fatal(0, "audio device does not support kAudioFormatLinearPCM"); | |
144 | status = AudioDeviceAddIOProc(adid, adioproc, 0); | |
145 | if(status) | |
146 | fatal(0, "AudioDeviceAddIOProc: %d", (int)status); | |
147 | pthread_mutex_lock(&lock); | |
148 | for(;;) { | |
149 | /* Wait for the buffer to fill up a bit */ | |
150 | playrtp_fill_buffer(); | |
151 | /* Start playing now */ | |
152 | info("Playing..."); | |
153 | next_timestamp = pheap_first(&packets)->timestamp; | |
154 | active = 1; | |
155 | status = AudioDeviceStart(adid, adioproc); | |
156 | if(status) | |
157 | fatal(0, "AudioDeviceStart: %d", (int)status); | |
158 | /* Wait until the buffer empties out */ | |
159 | while(nsamples >= minbuffer | |
160 | || (nsamples > 0 | |
161 | && contains(pheap_first(&packets), next_timestamp))) | |
162 | pthread_cond_wait(&cond, &lock); | |
163 | /* Stop playing for a bit until the buffer re-fills */ | |
164 | status = AudioDeviceStop(adid, adioproc); | |
165 | if(status) | |
166 | fatal(0, "AudioDeviceStop: %d", (int)status); | |
167 | active = 0; | |
168 | /* Go back round */ | |
169 | } | |
170 | } | |
171 | ||
172 | #endif | |
173 | ||
174 | /* | |
175 | Local Variables: | |
176 | c-basic-offset:2 | |
177 | comment-column:40 | |
178 | fill-column:79 | |
179 | indent-tabs-mode:nil | |
180 | End: | |
181 | */ |