annotate libao2/ao_macosx.c @ 11254:f42b8a99a5e0

make 'your system is too slow' appear only once
author iive
date Thu, 23 Oct 2003 22:44:25 +0000
parents b252d1b6829e
children 99798c3cdb93
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
10147
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
1 /*
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
2 *
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
3 * ao_macosx.c
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
4 *
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
5 * Original Copyright (C) Timothy J. Wood - Aug 2000
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
6 *
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
7 * This file is part of libao, a cross-platform library. See
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
8 * README for a history of this source code.
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
9 *
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
10 * libao is free software; you can redistribute it and/or modify
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
11 * it under the terms of the GNU General Public License as published by
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
12 * the Free Software Foundation; either version 2, or (at your option)
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
13 * any later version.
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
14 *
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
15 * libao is distributed in the hope that it will be useful,
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
18 * GNU General Public License for more details.
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
19 *
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
20 * You should have received a copy of the GNU General Public License
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
21 * along with GNU Make; see the file COPYING. If not, write to
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
22 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
23 */
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
24
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
25 /*
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
26 * The MacOS X CoreAudio framework doesn't mesh as simply as some
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
27 * simpler frameworks do. This is due to the fact that CoreAudio pulls
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
28 * audio samples rather than having them pushed at it (which is nice
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
29 * when you are wanting to do good buffering of audio).
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
30 */
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
31
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
32 /* Change log:
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
33 *
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
34 * 14/5-2003: Ported to MPlayer libao2 by Dan Christiansen
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
35 *
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
36 * AC-3 and MPEG audio passthrough is possible, but I don't have
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
37 * access to a sound card that supports it.
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
38 */
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
39
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
40 #include <CoreAudio/AudioHardware.h>
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
41 #include <stdio.h>
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
42 #include <string.h>
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
43 #include <inttypes.h>
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
44 #include <pthread.h>
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
45
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
46 #include "../mp_msg.h"
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
47
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
48 #include "audio_out.h"
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
49 #include "audio_out_internal.h"
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
50 #include "afmt.h"
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
51
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
52 static ao_info_t info =
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
53 {
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
54 "Darwin/Mac OS X native audio output",
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
55 "macosx",
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
56 "Timothy J. Wood & Dan Christiansen",
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
57 ""
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
58 };
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
59
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
60 LIBAO_EXTERN(macosx)
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
61
10152
b252d1b6829e cleanup/cosmetic patch by the author :)
alex
parents: 10147
diff changeset
62 /* Prefix for all mp_msg() calls */
b252d1b6829e cleanup/cosmetic patch by the author :)
alex
parents: 10147
diff changeset
63 #define ao_msg(a, b, c...) mp_msg(a, b, "AO: [macosx] " c)
b252d1b6829e cleanup/cosmetic patch by the author :)
alex
parents: 10147
diff changeset
64
10147
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
65 /* This is large, but best (maybe it should be even larger).
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
66 * CoreAudio supposedly has an internal latency in the order of 2ms */
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
67 #define NUM_BUFS 128
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
68
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
69 typedef struct ao_macosx_s
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
70 {
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
71 /* CoreAudio */
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
72 AudioDeviceID outputDeviceID;
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
73 AudioStreamBasicDescription outputStreamBasicDescription;
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
74
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
75 /* Ring-buffer */
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
76 pthread_mutex_t buffer_mutex; /* mutex covering buffer variables */
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
77
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
78 unsigned char *buffer[NUM_BUFS];
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
79 unsigned int buffer_len;
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
80
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
81 unsigned int buf_read;
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
82 unsigned int buf_write;
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
83 unsigned int buf_read_pos;
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
84 unsigned int buf_write_pos;
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
85 int full_buffers;
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
86 int buffered_bytes;
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
87 } ao_macosx_t;
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
88
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
89 static ao_macosx_t *ao;
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
90
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
91 /* General purpose Ring-buffering routines */
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
92 static int write_buffer(unsigned char* data,int len){
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
93 int len2=0;
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
94 int x;
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
95
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
96 while(len>0){
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
97 if(ao->full_buffers==NUM_BUFS) {
10152
b252d1b6829e cleanup/cosmetic patch by the author :)
alex
parents: 10147
diff changeset
98 ao_msg(MSGT_AO,MSGL_V, "Buffer overrun\n");
10147
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
99 break;
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
100 }
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
101
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
102 x=ao->buffer_len-ao->buf_write_pos;
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
103 if(x>len) x=len;
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
104 memcpy(ao->buffer[ao->buf_write]+ao->buf_write_pos,data+len2,x);
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
105
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
106 /* accessing common variables, locking mutex */
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
107 pthread_mutex_lock(&ao->buffer_mutex);
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
108 len2+=x; len-=x;
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
109 ao->buffered_bytes+=x; ao->buf_write_pos+=x;
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
110 if(ao->buf_write_pos>=ao->buffer_len) {
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
111 /* block is full, find next! */
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
112 ao->buf_write=(ao->buf_write+1)%NUM_BUFS;
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
113 ++ao->full_buffers;
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
114 ao->buf_write_pos=0;
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
115 }
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
116 pthread_mutex_unlock(&ao->buffer_mutex);
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
117 }
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
118
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
119 return len2;
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
120 }
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
121
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
122 static int read_buffer(unsigned char* data,int len){
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
123 int len2=0;
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
124 int x;
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
125
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
126 while(len>0){
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
127 if(ao->full_buffers==0) {
10152
b252d1b6829e cleanup/cosmetic patch by the author :)
alex
parents: 10147
diff changeset
128 ao_msg(MSGT_AO,MSGL_V, "Buffer underrun\n");
10147
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
129 break;
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
130 }
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
131
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
132 x=ao->buffer_len-ao->buf_read_pos;
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
133 if(x>len) x=len;
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
134 memcpy(data+len2,ao->buffer[ao->buf_read]+ao->buf_read_pos,x);
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
135 len2+=x; len-=x;
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
136
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
137 /* accessing common variables, locking mutex */
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
138 pthread_mutex_lock(&ao->buffer_mutex);
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
139 ao->buffered_bytes-=x; ao->buf_read_pos+=x;
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
140 if(ao->buf_read_pos>=ao->buffer_len){
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
141 /* block is empty, find next! */
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
142 ao->buf_read=(ao->buf_read+1)%NUM_BUFS;
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
143 --ao->full_buffers;
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
144 ao->buf_read_pos=0;
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
145 }
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
146 pthread_mutex_unlock(&ao->buffer_mutex);
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
147 }
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
148
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
149
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
150 return len2;
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
151 }
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
152
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
153 /* end ring buffer stuff */
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
154
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
155 /* The function that the CoreAudio thread calls when it wants more data */
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
156 static OSStatus audioDeviceIOProc(AudioDeviceID inDevice, const AudioTimeStamp *inNow, const AudioBufferList *inInputData, const AudioTimeStamp *inInputTime, AudioBufferList *outOutputData, const AudioTimeStamp *inOutputTime, void *inClientData)
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
157 {
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
158 outOutputData->mBuffers[0].mDataByteSize =
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
159 read_buffer((char *)outOutputData->mBuffers[0].mData, ao->buffer_len);
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
160
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
161 return 0;
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
162 }
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
163
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
164
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
165 static int control(int cmd,void *arg){
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
166 switch (cmd) {
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
167 case AOCONTROL_SET_DEVICE:
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
168 case AOCONTROL_GET_DEVICE:
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
169 case AOCONTROL_GET_VOLUME:
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
170 case AOCONTROL_SET_VOLUME:
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
171 /* unimplemented/meaningless */
10152
b252d1b6829e cleanup/cosmetic patch by the author :)
alex
parents: 10147
diff changeset
172 return CONTROL_FALSE;
10147
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
173 case AOCONTROL_QUERY_FORMAT:
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
174 /* stick with what CoreAudio requests */
10152
b252d1b6829e cleanup/cosmetic patch by the author :)
alex
parents: 10147
diff changeset
175 return CONTROL_FALSE;
10147
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
176 default:
10152
b252d1b6829e cleanup/cosmetic patch by the author :)
alex
parents: 10147
diff changeset
177 return CONTROL_FALSE;
10147
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
178 }
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
179
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
180 }
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
181
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
182
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
183 static int init(int rate,int channels,int format,int flags)
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
184 {
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
185 OSStatus status;
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
186 UInt32 propertySize;
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
187 int rc;
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
188 int i;
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
189
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
190 ao = (ao_macosx_t *)malloc(sizeof(ao_macosx_t));
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
191
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
192 /* initialise mutex */
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
193 pthread_mutex_init(&ao->buffer_mutex, NULL);
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
194 pthread_mutex_unlock(&ao->buffer_mutex);
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
195
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
196 /* get default output device */
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
197 propertySize = sizeof(ao->outputDeviceID);
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
198 status = AudioHardwareGetProperty(kAudioHardwarePropertyDefaultOutputDevice, &propertySize, &(ao->outputDeviceID));
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
199 if (status) {
10152
b252d1b6829e cleanup/cosmetic patch by the author :)
alex
parents: 10147
diff changeset
200 ao_msg(MSGT_AO,MSGL_WARN,
b252d1b6829e cleanup/cosmetic patch by the author :)
alex
parents: 10147
diff changeset
201 "AudioHardwareGetProperty returned %d\n",
10147
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
202 (int)status);
10152
b252d1b6829e cleanup/cosmetic patch by the author :)
alex
parents: 10147
diff changeset
203 return CONTROL_FALSE;
10147
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
204 }
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
205
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
206 if (ao->outputDeviceID == kAudioDeviceUnknown) {
10152
b252d1b6829e cleanup/cosmetic patch by the author :)
alex
parents: 10147
diff changeset
207 ao_msg(MSGT_AO,MSGL_WARN, "AudioHardwareGetProperty: ao->outputDeviceID is kAudioDeviceUnknown\n");
b252d1b6829e cleanup/cosmetic patch by the author :)
alex
parents: 10147
diff changeset
208 return CONTROL_FALSE;
10147
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
209 }
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
210
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
211 /* get default output format
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
212 * TODO: get all support formats and iterate through them
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
213 */
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
214 propertySize = sizeof(ao->outputStreamBasicDescription);
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
215 status = AudioDeviceGetProperty(ao->outputDeviceID, 0, false, kAudioDevicePropertyStreamFormat, &propertySize, &ao->outputStreamBasicDescription);
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
216 if (status) {
10152
b252d1b6829e cleanup/cosmetic patch by the author :)
alex
parents: 10147
diff changeset
217 ao_msg(MSGT_AO,MSGL_WARN, "AudioDeviceGetProperty returned %d when getting kAudioDevicePropertyStreamFormat\n", (int)status);
b252d1b6829e cleanup/cosmetic patch by the author :)
alex
parents: 10147
diff changeset
218 return CONTROL_FALSE;
10147
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
219 }
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
220
10152
b252d1b6829e cleanup/cosmetic patch by the author :)
alex
parents: 10147
diff changeset
221 ao_msg(MSGT_AO,MSGL_V, "hardware format...\n");
b252d1b6829e cleanup/cosmetic patch by the author :)
alex
parents: 10147
diff changeset
222 ao_msg(MSGT_AO,MSGL_V, "%f mSampleRate\n", ao->outputStreamBasicDescription.mSampleRate);
b252d1b6829e cleanup/cosmetic patch by the author :)
alex
parents: 10147
diff changeset
223 ao_msg(MSGT_AO,MSGL_V, " %c%c%c%c mFormatID\n",
10147
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
224 (int)(ao->outputStreamBasicDescription.mFormatID & 0xff000000) >> 24,
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
225 (int)(ao->outputStreamBasicDescription.mFormatID & 0x00ff0000) >> 16,
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
226 (int)(ao->outputStreamBasicDescription.mFormatID & 0x0000ff00) >> 8,
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
227 (int)(ao->outputStreamBasicDescription.mFormatID & 0x000000ff) >> 0);
10152
b252d1b6829e cleanup/cosmetic patch by the author :)
alex
parents: 10147
diff changeset
228 ao_msg(MSGT_AO,MSGL_V, "%5d mBytesPerPacket\n",
10147
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
229 (int)ao->outputStreamBasicDescription.mBytesPerPacket);
10152
b252d1b6829e cleanup/cosmetic patch by the author :)
alex
parents: 10147
diff changeset
230 ao_msg(MSGT_AO,MSGL_V, "%5d mFramesPerPacket\n",
10147
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
231 (int)ao->outputStreamBasicDescription.mFramesPerPacket);
10152
b252d1b6829e cleanup/cosmetic patch by the author :)
alex
parents: 10147
diff changeset
232 ao_msg(MSGT_AO,MSGL_V, "%5d mBytesPerFrame\n",
10147
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
233 (int)ao->outputStreamBasicDescription.mBytesPerFrame);
10152
b252d1b6829e cleanup/cosmetic patch by the author :)
alex
parents: 10147
diff changeset
234 ao_msg(MSGT_AO,MSGL_V, "%5d mChannelsPerFrame\n",
10147
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
235 (int)ao->outputStreamBasicDescription.mChannelsPerFrame);
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
236
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
237 /* get requested buffer length */
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
238 propertySize = sizeof(ao->buffer_len);
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
239 status = AudioDeviceGetProperty(ao->outputDeviceID, 0, false, kAudioDevicePropertyBufferSize, &propertySize, &ao->buffer_len);
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
240 if (status) {
10152
b252d1b6829e cleanup/cosmetic patch by the author :)
alex
parents: 10147
diff changeset
241 ao_msg(MSGT_AO,MSGL_WARN, "AudioDeviceGetProperty returned %d when getting kAudioDevicePropertyBufferSize\n", (int)status);
b252d1b6829e cleanup/cosmetic patch by the author :)
alex
parents: 10147
diff changeset
242 return CONTROL_FALSE;
10147
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
243 }
10152
b252d1b6829e cleanup/cosmetic patch by the author :)
alex
parents: 10147
diff changeset
244 ao_msg(MSGT_AO,MSGL_V, "%5d ao->buffer_len\n", (int)ao->buffer_len);
10147
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
245
10152
b252d1b6829e cleanup/cosmetic patch by the author :)
alex
parents: 10147
diff changeset
246 /* FIXME:
b252d1b6829e cleanup/cosmetic patch by the author :)
alex
parents: 10147
diff changeset
247 *
b252d1b6829e cleanup/cosmetic patch by the author :)
alex
parents: 10147
diff changeset
248 * Resampling of 32-bit float audio is broken in MPlayer. Refuse to
b252d1b6829e cleanup/cosmetic patch by the author :)
alex
parents: 10147
diff changeset
249 * handle anything other than the native format until this is fixed
b252d1b6829e cleanup/cosmetic patch by the author :)
alex
parents: 10147
diff changeset
250 * or this module is rewritten, whichever comes first.
b252d1b6829e cleanup/cosmetic patch by the author :)
alex
parents: 10147
diff changeset
251 */
b252d1b6829e cleanup/cosmetic patch by the author :)
alex
parents: 10147
diff changeset
252 if (ao_data.samplerate == ao->outputStreamBasicDescription.mSampleRate) {
b252d1b6829e cleanup/cosmetic patch by the author :)
alex
parents: 10147
diff changeset
253 ao_data.samplerate = (int)ao->outputStreamBasicDescription.mSampleRate;
b252d1b6829e cleanup/cosmetic patch by the author :)
alex
parents: 10147
diff changeset
254 } else {
b252d1b6829e cleanup/cosmetic patch by the author :)
alex
parents: 10147
diff changeset
255 ao_msg(MSGT_AO,MSGL_WARN, "Resampling not supported yet.\n");
b252d1b6829e cleanup/cosmetic patch by the author :)
alex
parents: 10147
diff changeset
256 return 0;
b252d1b6829e cleanup/cosmetic patch by the author :)
alex
parents: 10147
diff changeset
257 }
b252d1b6829e cleanup/cosmetic patch by the author :)
alex
parents: 10147
diff changeset
258
10147
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
259 ao_data.channels = ao->outputStreamBasicDescription.mChannelsPerFrame;
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
260 ao_data.outburst = ao_data.buffersize = ao->buffer_len;
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
261 ao_data.bps =
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
262 ao_data.samplerate * ao->outputStreamBasicDescription.mBytesPerFrame;
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
263
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
264 if (ao->outputStreamBasicDescription.mFormatID == kAudioFormatLinearPCM) {
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
265 uint32_t flags = ao->outputStreamBasicDescription.mFormatFlags;
10152
b252d1b6829e cleanup/cosmetic patch by the author :)
alex
parents: 10147
diff changeset
266 if (flags & kAudioFormatFlagIsFloat) {
b252d1b6829e cleanup/cosmetic patch by the author :)
alex
parents: 10147
diff changeset
267 ao_data.format = AFMT_FLOAT;
b252d1b6829e cleanup/cosmetic patch by the author :)
alex
parents: 10147
diff changeset
268 } else {
b252d1b6829e cleanup/cosmetic patch by the author :)
alex
parents: 10147
diff changeset
269 ao_msg(MSGT_AO,MSGL_WARN, "Unsupported audio output "
b252d1b6829e cleanup/cosmetic patch by the author :)
alex
parents: 10147
diff changeset
270 "format %d. Please report this to the developer\n",
b252d1b6829e cleanup/cosmetic patch by the author :)
alex
parents: 10147
diff changeset
271 (int)status);
b252d1b6829e cleanup/cosmetic patch by the author :)
alex
parents: 10147
diff changeset
272 return CONTROL_FALSE;
b252d1b6829e cleanup/cosmetic patch by the author :)
alex
parents: 10147
diff changeset
273 }
b252d1b6829e cleanup/cosmetic patch by the author :)
alex
parents: 10147
diff changeset
274
10147
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
275 } else {
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
276 /* TODO: handle AFMT_AC3, AFMT_MPEG & friends */
10152
b252d1b6829e cleanup/cosmetic patch by the author :)
alex
parents: 10147
diff changeset
277 ao_msg(MSGT_AO,MSGL_WARN, "Default Audio Device doesn't "
10147
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
278 "support Linear PCM!\n");
10152
b252d1b6829e cleanup/cosmetic patch by the author :)
alex
parents: 10147
diff changeset
279 return CONTROL_FALSE;
10147
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
280 }
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
281
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
282 /* Allocate ring-buffer memory */
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
283 for(i=0;i<NUM_BUFS;i++)
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
284 ao->buffer[i]=(unsigned char *) malloc(ao->buffer_len);
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
285
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
286
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
287 /* Prepare for playback */
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
288
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
289 reset();
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
290
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
291 /* Set the IO proc that CoreAudio will call when it needs data */
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
292 status = AudioDeviceAddIOProc(ao->outputDeviceID, audioDeviceIOProc, NULL);
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
293 if (status) {
10152
b252d1b6829e cleanup/cosmetic patch by the author :)
alex
parents: 10147
diff changeset
294 ao_msg(MSGT_AO,MSGL_WARN, "AudioDeviceAddIOProc returned %d\n", (int)status);
b252d1b6829e cleanup/cosmetic patch by the author :)
alex
parents: 10147
diff changeset
295 return CONTROL_FALSE;
10147
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
296 }
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
297
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
298 /* Start callback */
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
299 status = AudioDeviceStart(ao->outputDeviceID, audioDeviceIOProc);
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
300 if (status) {
10152
b252d1b6829e cleanup/cosmetic patch by the author :)
alex
parents: 10147
diff changeset
301 ao_msg(MSGT_AO,MSGL_WARN, "AudioDeviceStart returned %d\n",
10147
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
302 (int)status);
10152
b252d1b6829e cleanup/cosmetic patch by the author :)
alex
parents: 10147
diff changeset
303 return CONTROL_FALSE;
10147
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
304 }
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
305
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
306 return CONTROL_OK;
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
307 }
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
308
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
309
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
310 static int play(void* output_samples,int num_bytes,int flags)
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
311 {
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
312 return write_buffer(output_samples, num_bytes);
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
313 }
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
314
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
315 /* set variables and buffer to initial state */
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
316 static void reset()
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
317 {
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
318 int i;
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
319
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
320 pthread_mutex_lock(&ao->buffer_mutex);
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
321
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
322 /* reset ring-buffer state */
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
323 ao->buf_read=0;
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
324 ao->buf_write=0;
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
325 ao->buf_read_pos=0;
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
326 ao->buf_write_pos=0;
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
327
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
328 ao->full_buffers=0;
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
329 ao->buffered_bytes=0;
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
330
10152
b252d1b6829e cleanup/cosmetic patch by the author :)
alex
parents: 10147
diff changeset
331 /* zero output buffer */
10147
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
332 for (i = 0; i < NUM_BUFS; i++)
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
333 bzero(ao->buffer[i], ao->buffer_len);
10152
b252d1b6829e cleanup/cosmetic patch by the author :)
alex
parents: 10147
diff changeset
334
10147
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
335 pthread_mutex_unlock(&ao->buffer_mutex);
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
336
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
337 return;
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
338 }
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
339
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
340
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
341 /* return available space */
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
342 static int get_space()
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
343 {
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
344 return (NUM_BUFS-ao->full_buffers)*ao_data.buffersize - ao->buf_write_pos;
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
345 }
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
346
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
347
10152
b252d1b6829e cleanup/cosmetic patch by the author :)
alex
parents: 10147
diff changeset
348 /* return delay until audio is played */
10147
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
349 static float get_delay()
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
350 {
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
351 return (float)(ao->buffered_bytes)/(float)ao_data.bps;
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
352 }
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
353
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
354
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
355 /* unload plugin and deregister from coreaudio */
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
356 static void uninit()
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
357 {
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
358 int i;
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
359 OSErr status;
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
360
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
361 reset();
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
362
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
363 status = AudioDeviceRemoveIOProc(ao->outputDeviceID, audioDeviceIOProc);
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
364 if (status)
10152
b252d1b6829e cleanup/cosmetic patch by the author :)
alex
parents: 10147
diff changeset
365 ao_msg(MSGT_AO,MSGL_WARN, "AudioDeviceRemoveIOProc "
10147
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
366 "returned %d\n", (int)status);
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
367
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
368 for(i=0;i<NUM_BUFS;i++) free(ao->buffer[i]);
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
369 free(ao);
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
370 }
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
371
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
372
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
373 /* stop playing, keep buffers (for pause) */
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
374 static void audio_pause()
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
375 {
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
376 OSErr status;
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
377
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
378 /* stop callback */
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
379 status = AudioDeviceStop(ao->outputDeviceID, audioDeviceIOProc);
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
380 if (status)
10152
b252d1b6829e cleanup/cosmetic patch by the author :)
alex
parents: 10147
diff changeset
381 ao_msg(MSGT_AO,MSGL_WARN, "AudioDeviceStop returned %d\n",
10147
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
382 (int)status);
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
383 }
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
384
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
385
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
386 /* resume playing, after audio_pause() */
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
387 static void audio_resume()
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
388 {
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
389 OSErr status = AudioDeviceStart(ao->outputDeviceID, audioDeviceIOProc);
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
390 if (status)
10152
b252d1b6829e cleanup/cosmetic patch by the author :)
alex
parents: 10147
diff changeset
391 ao_msg(MSGT_AO,MSGL_WARN, "AudioDeviceStart returned %d\n",
10147
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
392 (int)status);
f2725d6717bd Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff changeset
393 }