Mercurial > mplayer.hg
annotate libao2/ao_macosx.c @ 24942:efdd237bf968
Add include to fix warning:
ao_macosx.c:899: warning: implicit declaration of function 'gettimeofday'
author | ulion |
---|---|
date | Mon, 05 Nov 2007 12:02:21 +0000 |
parents | 5ce7bc88eb0c |
children | 19e208a0296d |
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 * |
21977
cea0eb833758
Fix FSF address and otherwise broken license headers.
diego
parents:
21352
diff
changeset
|
20 * You should have received a copy of the GNU General Public License along |
cea0eb833758
Fix FSF address and otherwise broken license headers.
diego
parents:
21352
diff
changeset
|
21 * with libao; if not, write to the Free Software Foundation, Inc., |
cea0eb833758
Fix FSF address and otherwise broken license headers.
diego
parents:
21352
diff
changeset
|
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
10147
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 |
15646
f46eae5e271b
Mac OS X Audio with AudioUnits and AudioToolbox format converters, Patch by Chris Roccati<roccati@pobox.com
nplourde
parents:
15486
diff
changeset
|
40 #include <CoreServices/CoreServices.h> |
f46eae5e271b
Mac OS X Audio with AudioUnits and AudioToolbox format converters, Patch by Chris Roccati<roccati@pobox.com
nplourde
parents:
15486
diff
changeset
|
41 #include <AudioUnit/AudioUnit.h> |
f46eae5e271b
Mac OS X Audio with AudioUnits and AudioToolbox format converters, Patch by Chris Roccati<roccati@pobox.com
nplourde
parents:
15486
diff
changeset
|
42 #include <AudioToolbox/AudioToolbox.h> |
10147
f2725d6717bd
Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff
changeset
|
43 #include <stdio.h> |
f2725d6717bd
Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff
changeset
|
44 #include <string.h> |
14903
21f44596f356
do not always request little-endian despite the actual sound format. by Alexander Strange - astrange@ithinksw.com
nplourde
parents:
14769
diff
changeset
|
45 #include <stdlib.h> |
10147
f2725d6717bd
Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff
changeset
|
46 #include <inttypes.h> |
f2725d6717bd
Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff
changeset
|
47 #include <pthread.h> |
24734 | 48 #include <sys/types.h> |
24942 | 49 #include <sys/time.h> |
24734 | 50 #include <unistd.h> |
10147
f2725d6717bd
Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff
changeset
|
51 |
14479 | 52 #include "config.h" |
14123 | 53 #include "mp_msg.h" |
10147
f2725d6717bd
Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff
changeset
|
54 |
f2725d6717bd
Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff
changeset
|
55 #include "audio_out.h" |
f2725d6717bd
Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff
changeset
|
56 #include "audio_out_internal.h" |
14245 | 57 #include "libaf/af_format.h" |
24734 | 58 #include "osdep/timer.h" |
10147
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 static ao_info_t info = |
f2725d6717bd
Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff
changeset
|
61 { |
f2725d6717bd
Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff
changeset
|
62 "Darwin/Mac OS X native audio output", |
f2725d6717bd
Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff
changeset
|
63 "macosx", |
15646
f46eae5e271b
Mac OS X Audio with AudioUnits and AudioToolbox format converters, Patch by Chris Roccati<roccati@pobox.com
nplourde
parents:
15486
diff
changeset
|
64 "Timothy J. Wood & Dan Christiansen & Chris Roccati", |
10147
f2725d6717bd
Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff
changeset
|
65 "" |
f2725d6717bd
Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff
changeset
|
66 }; |
f2725d6717bd
Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff
changeset
|
67 |
f2725d6717bd
Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff
changeset
|
68 LIBAO_EXTERN(macosx) |
f2725d6717bd
Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff
changeset
|
69 |
10152 | 70 /* Prefix for all mp_msg() calls */ |
71 #define ao_msg(a, b, c...) mp_msg(a, b, "AO: [macosx] " c) | |
72 | |
10147
f2725d6717bd
Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff
changeset
|
73 /* 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
|
74 * CoreAudio supposedly has an internal latency in the order of 2ms */ |
14290 | 75 #define NUM_BUFS 32 |
10147
f2725d6717bd
Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff
changeset
|
76 |
f2725d6717bd
Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff
changeset
|
77 typedef struct ao_macosx_s |
f2725d6717bd
Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff
changeset
|
78 { |
24734 | 79 AudioDeviceID i_selected_dev; /* Keeps DeviceID of the selected device. */ |
80 int b_supports_digital; /* Does the currently selected device support digital mode? */ | |
81 int b_digital; /* Are we running in digital mode? */ | |
82 | |
15646
f46eae5e271b
Mac OS X Audio with AudioUnits and AudioToolbox format converters, Patch by Chris Roccati<roccati@pobox.com
nplourde
parents:
15486
diff
changeset
|
83 /* AudioUnit */ |
f46eae5e271b
Mac OS X Audio with AudioUnits and AudioToolbox format converters, Patch by Chris Roccati<roccati@pobox.com
nplourde
parents:
15486
diff
changeset
|
84 AudioUnit theOutputUnit; |
24734 | 85 |
86 /* CoreAudio SPDIF mode specific */ | |
87 pid_t i_hog_pid; /* Keeps the pid of our hog status. */ | |
88 AudioStreamID i_stream_id; /* The StreamID that has a cac3 streamformat */ | |
89 int i_stream_index; /* The index of i_stream_id in an AudioBufferList */ | |
90 AudioStreamBasicDescription stream_format;/* The format we changed the stream to */ | |
91 AudioStreamBasicDescription sfmt_revert; /* The original format of the stream */ | |
92 int b_revert; /* Whether we need to revert the stream format */ | |
93 int b_changed_mixing; /* Whether we need to set the mixing mode back */ | |
94 int b_stream_format_changed; /* Flag for main thread to reset stream's format to digital and reset buffer */ | |
95 | |
96 /* Original common part */ | |
15646
f46eae5e271b
Mac OS X Audio with AudioUnits and AudioToolbox format converters, Patch by Chris Roccati<roccati@pobox.com
nplourde
parents:
15486
diff
changeset
|
97 int packetSize; |
19427
ada359817b61
fix buffering issues with short audio samples on macosx. patch by Chris Roccati <roccati@pobox.com>
nplourde
parents:
18915
diff
changeset
|
98 int paused; |
10147
f2725d6717bd
Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff
changeset
|
99 |
f2725d6717bd
Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff
changeset
|
100 /* Ring-buffer */ |
14769
e701873aa8ca
rids ao_macosx of the buffer mutex by using the same buffering scheme as ao_sdl - Patch by Reimar Doffinger
nplourde
parents:
14642
diff
changeset
|
101 /* does not need explicit synchronization, but needs to allocate |
e701873aa8ca
rids ao_macosx of the buffer mutex by using the same buffering scheme as ao_sdl - Patch by Reimar Doffinger
nplourde
parents:
14642
diff
changeset
|
102 * (num_chunks + 1) * chunk_size memory to store num_chunks * chunk_size |
e701873aa8ca
rids ao_macosx of the buffer mutex by using the same buffering scheme as ao_sdl - Patch by Reimar Doffinger
nplourde
parents:
14642
diff
changeset
|
103 * data */ |
e701873aa8ca
rids ao_macosx of the buffer mutex by using the same buffering scheme as ao_sdl - Patch by Reimar Doffinger
nplourde
parents:
14642
diff
changeset
|
104 unsigned char *buffer; |
e701873aa8ca
rids ao_macosx of the buffer mutex by using the same buffering scheme as ao_sdl - Patch by Reimar Doffinger
nplourde
parents:
14642
diff
changeset
|
105 unsigned int buffer_len; ///< must always be (num_chunks + 1) * chunk_size |
e701873aa8ca
rids ao_macosx of the buffer mutex by using the same buffering scheme as ao_sdl - Patch by Reimar Doffinger
nplourde
parents:
14642
diff
changeset
|
106 unsigned int num_chunks; |
e701873aa8ca
rids ao_macosx of the buffer mutex by using the same buffering scheme as ao_sdl - Patch by Reimar Doffinger
nplourde
parents:
14642
diff
changeset
|
107 unsigned int chunk_size; |
10147
f2725d6717bd
Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff
changeset
|
108 |
f2725d6717bd
Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff
changeset
|
109 unsigned int buf_read_pos; |
f2725d6717bd
Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff
changeset
|
110 unsigned int buf_write_pos; |
f2725d6717bd
Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff
changeset
|
111 } ao_macosx_t; |
f2725d6717bd
Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff
changeset
|
112 |
15853
6e994905f77b
more general ao_macosx cleanup. Patch by Alexander Strange <astrange@ithinksw.com>
nplourde
parents:
15722
diff
changeset
|
113 static ao_macosx_t *ao = NULL; |
10147
f2725d6717bd
Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff
changeset
|
114 |
14769
e701873aa8ca
rids ao_macosx of the buffer mutex by using the same buffering scheme as ao_sdl - Patch by Reimar Doffinger
nplourde
parents:
14642
diff
changeset
|
115 /** |
e701873aa8ca
rids ao_macosx of the buffer mutex by using the same buffering scheme as ao_sdl - Patch by Reimar Doffinger
nplourde
parents:
14642
diff
changeset
|
116 * \brief return number of free bytes in the buffer |
e701873aa8ca
rids ao_macosx of the buffer mutex by using the same buffering scheme as ao_sdl - Patch by Reimar Doffinger
nplourde
parents:
14642
diff
changeset
|
117 * may only be called by mplayer's thread |
e701873aa8ca
rids ao_macosx of the buffer mutex by using the same buffering scheme as ao_sdl - Patch by Reimar Doffinger
nplourde
parents:
14642
diff
changeset
|
118 * \return minimum number of free bytes in buffer, value may change between |
e701873aa8ca
rids ao_macosx of the buffer mutex by using the same buffering scheme as ao_sdl - Patch by Reimar Doffinger
nplourde
parents:
14642
diff
changeset
|
119 * two immediately following calls, and the real number of free bytes |
e701873aa8ca
rids ao_macosx of the buffer mutex by using the same buffering scheme as ao_sdl - Patch by Reimar Doffinger
nplourde
parents:
14642
diff
changeset
|
120 * might actually be larger! |
e701873aa8ca
rids ao_macosx of the buffer mutex by using the same buffering scheme as ao_sdl - Patch by Reimar Doffinger
nplourde
parents:
14642
diff
changeset
|
121 */ |
18915
99e20a22d5d0
modifies function declarations without parameters from ()
reynaldo
parents:
18885
diff
changeset
|
122 static int buf_free(void) { |
14769
e701873aa8ca
rids ao_macosx of the buffer mutex by using the same buffering scheme as ao_sdl - Patch by Reimar Doffinger
nplourde
parents:
14642
diff
changeset
|
123 int free = ao->buf_read_pos - ao->buf_write_pos - ao->chunk_size; |
e701873aa8ca
rids ao_macosx of the buffer mutex by using the same buffering scheme as ao_sdl - Patch by Reimar Doffinger
nplourde
parents:
14642
diff
changeset
|
124 if (free < 0) free += ao->buffer_len; |
e701873aa8ca
rids ao_macosx of the buffer mutex by using the same buffering scheme as ao_sdl - Patch by Reimar Doffinger
nplourde
parents:
14642
diff
changeset
|
125 return free; |
e701873aa8ca
rids ao_macosx of the buffer mutex by using the same buffering scheme as ao_sdl - Patch by Reimar Doffinger
nplourde
parents:
14642
diff
changeset
|
126 } |
10147
f2725d6717bd
Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff
changeset
|
127 |
14769
e701873aa8ca
rids ao_macosx of the buffer mutex by using the same buffering scheme as ao_sdl - Patch by Reimar Doffinger
nplourde
parents:
14642
diff
changeset
|
128 /** |
e701873aa8ca
rids ao_macosx of the buffer mutex by using the same buffering scheme as ao_sdl - Patch by Reimar Doffinger
nplourde
parents:
14642
diff
changeset
|
129 * \brief return number of buffered bytes |
e701873aa8ca
rids ao_macosx of the buffer mutex by using the same buffering scheme as ao_sdl - Patch by Reimar Doffinger
nplourde
parents:
14642
diff
changeset
|
130 * may only be called by playback thread |
e701873aa8ca
rids ao_macosx of the buffer mutex by using the same buffering scheme as ao_sdl - Patch by Reimar Doffinger
nplourde
parents:
14642
diff
changeset
|
131 * \return minimum number of buffered bytes, value may change between |
e701873aa8ca
rids ao_macosx of the buffer mutex by using the same buffering scheme as ao_sdl - Patch by Reimar Doffinger
nplourde
parents:
14642
diff
changeset
|
132 * two immediately following calls, and the real number of buffered bytes |
e701873aa8ca
rids ao_macosx of the buffer mutex by using the same buffering scheme as ao_sdl - Patch by Reimar Doffinger
nplourde
parents:
14642
diff
changeset
|
133 * might actually be larger! |
e701873aa8ca
rids ao_macosx of the buffer mutex by using the same buffering scheme as ao_sdl - Patch by Reimar Doffinger
nplourde
parents:
14642
diff
changeset
|
134 */ |
18915
99e20a22d5d0
modifies function declarations without parameters from ()
reynaldo
parents:
18885
diff
changeset
|
135 static int buf_used(void) { |
14769
e701873aa8ca
rids ao_macosx of the buffer mutex by using the same buffering scheme as ao_sdl - Patch by Reimar Doffinger
nplourde
parents:
14642
diff
changeset
|
136 int used = ao->buf_write_pos - ao->buf_read_pos; |
e701873aa8ca
rids ao_macosx of the buffer mutex by using the same buffering scheme as ao_sdl - Patch by Reimar Doffinger
nplourde
parents:
14642
diff
changeset
|
137 if (used < 0) used += ao->buffer_len; |
e701873aa8ca
rids ao_macosx of the buffer mutex by using the same buffering scheme as ao_sdl - Patch by Reimar Doffinger
nplourde
parents:
14642
diff
changeset
|
138 return used; |
10147
f2725d6717bd
Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff
changeset
|
139 } |
f2725d6717bd
Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff
changeset
|
140 |
14769
e701873aa8ca
rids ao_macosx of the buffer mutex by using the same buffering scheme as ao_sdl - Patch by Reimar Doffinger
nplourde
parents:
14642
diff
changeset
|
141 /** |
e701873aa8ca
rids ao_macosx of the buffer mutex by using the same buffering scheme as ao_sdl - Patch by Reimar Doffinger
nplourde
parents:
14642
diff
changeset
|
142 * \brief add data to ringbuffer |
e701873aa8ca
rids ao_macosx of the buffer mutex by using the same buffering scheme as ao_sdl - Patch by Reimar Doffinger
nplourde
parents:
14642
diff
changeset
|
143 */ |
e701873aa8ca
rids ao_macosx of the buffer mutex by using the same buffering scheme as ao_sdl - Patch by Reimar Doffinger
nplourde
parents:
14642
diff
changeset
|
144 static int write_buffer(unsigned char* data, int len){ |
e701873aa8ca
rids ao_macosx of the buffer mutex by using the same buffering scheme as ao_sdl - Patch by Reimar Doffinger
nplourde
parents:
14642
diff
changeset
|
145 int first_len = ao->buffer_len - ao->buf_write_pos; |
e701873aa8ca
rids ao_macosx of the buffer mutex by using the same buffering scheme as ao_sdl - Patch by Reimar Doffinger
nplourde
parents:
14642
diff
changeset
|
146 int free = buf_free(); |
e701873aa8ca
rids ao_macosx of the buffer mutex by using the same buffering scheme as ao_sdl - Patch by Reimar Doffinger
nplourde
parents:
14642
diff
changeset
|
147 if (len > free) len = free; |
e701873aa8ca
rids ao_macosx of the buffer mutex by using the same buffering scheme as ao_sdl - Patch by Reimar Doffinger
nplourde
parents:
14642
diff
changeset
|
148 if (first_len > len) first_len = len; |
e701873aa8ca
rids ao_macosx of the buffer mutex by using the same buffering scheme as ao_sdl - Patch by Reimar Doffinger
nplourde
parents:
14642
diff
changeset
|
149 // till end of buffer |
e701873aa8ca
rids ao_macosx of the buffer mutex by using the same buffering scheme as ao_sdl - Patch by Reimar Doffinger
nplourde
parents:
14642
diff
changeset
|
150 memcpy (&ao->buffer[ao->buf_write_pos], data, first_len); |
e701873aa8ca
rids ao_macosx of the buffer mutex by using the same buffering scheme as ao_sdl - Patch by Reimar Doffinger
nplourde
parents:
14642
diff
changeset
|
151 if (len > first_len) { // we have to wrap around |
e701873aa8ca
rids ao_macosx of the buffer mutex by using the same buffering scheme as ao_sdl - Patch by Reimar Doffinger
nplourde
parents:
14642
diff
changeset
|
152 // remaining part from beginning of buffer |
e701873aa8ca
rids ao_macosx of the buffer mutex by using the same buffering scheme as ao_sdl - Patch by Reimar Doffinger
nplourde
parents:
14642
diff
changeset
|
153 memcpy (ao->buffer, &data[first_len], len - first_len); |
e701873aa8ca
rids ao_macosx of the buffer mutex by using the same buffering scheme as ao_sdl - Patch by Reimar Doffinger
nplourde
parents:
14642
diff
changeset
|
154 } |
e701873aa8ca
rids ao_macosx of the buffer mutex by using the same buffering scheme as ao_sdl - Patch by Reimar Doffinger
nplourde
parents:
14642
diff
changeset
|
155 ao->buf_write_pos = (ao->buf_write_pos + len) % ao->buffer_len; |
e701873aa8ca
rids ao_macosx of the buffer mutex by using the same buffering scheme as ao_sdl - Patch by Reimar Doffinger
nplourde
parents:
14642
diff
changeset
|
156 return len; |
e701873aa8ca
rids ao_macosx of the buffer mutex by using the same buffering scheme as ao_sdl - Patch by Reimar Doffinger
nplourde
parents:
14642
diff
changeset
|
157 } |
10147
f2725d6717bd
Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff
changeset
|
158 |
14769
e701873aa8ca
rids ao_macosx of the buffer mutex by using the same buffering scheme as ao_sdl - Patch by Reimar Doffinger
nplourde
parents:
14642
diff
changeset
|
159 /** |
e701873aa8ca
rids ao_macosx of the buffer mutex by using the same buffering scheme as ao_sdl - Patch by Reimar Doffinger
nplourde
parents:
14642
diff
changeset
|
160 * \brief remove data from ringbuffer |
e701873aa8ca
rids ao_macosx of the buffer mutex by using the same buffering scheme as ao_sdl - Patch by Reimar Doffinger
nplourde
parents:
14642
diff
changeset
|
161 */ |
e701873aa8ca
rids ao_macosx of the buffer mutex by using the same buffering scheme as ao_sdl - Patch by Reimar Doffinger
nplourde
parents:
14642
diff
changeset
|
162 static int read_buffer(unsigned char* data,int len){ |
e701873aa8ca
rids ao_macosx of the buffer mutex by using the same buffering scheme as ao_sdl - Patch by Reimar Doffinger
nplourde
parents:
14642
diff
changeset
|
163 int first_len = ao->buffer_len - ao->buf_read_pos; |
e701873aa8ca
rids ao_macosx of the buffer mutex by using the same buffering scheme as ao_sdl - Patch by Reimar Doffinger
nplourde
parents:
14642
diff
changeset
|
164 int buffered = buf_used(); |
e701873aa8ca
rids ao_macosx of the buffer mutex by using the same buffering scheme as ao_sdl - Patch by Reimar Doffinger
nplourde
parents:
14642
diff
changeset
|
165 if (len > buffered) len = buffered; |
e701873aa8ca
rids ao_macosx of the buffer mutex by using the same buffering scheme as ao_sdl - Patch by Reimar Doffinger
nplourde
parents:
14642
diff
changeset
|
166 if (first_len > len) first_len = len; |
e701873aa8ca
rids ao_macosx of the buffer mutex by using the same buffering scheme as ao_sdl - Patch by Reimar Doffinger
nplourde
parents:
14642
diff
changeset
|
167 // till end of buffer |
e701873aa8ca
rids ao_macosx of the buffer mutex by using the same buffering scheme as ao_sdl - Patch by Reimar Doffinger
nplourde
parents:
14642
diff
changeset
|
168 memcpy (data, &ao->buffer[ao->buf_read_pos], first_len); |
e701873aa8ca
rids ao_macosx of the buffer mutex by using the same buffering scheme as ao_sdl - Patch by Reimar Doffinger
nplourde
parents:
14642
diff
changeset
|
169 if (len > first_len) { // we have to wrap around |
e701873aa8ca
rids ao_macosx of the buffer mutex by using the same buffering scheme as ao_sdl - Patch by Reimar Doffinger
nplourde
parents:
14642
diff
changeset
|
170 // remaining part from beginning of buffer |
e701873aa8ca
rids ao_macosx of the buffer mutex by using the same buffering scheme as ao_sdl - Patch by Reimar Doffinger
nplourde
parents:
14642
diff
changeset
|
171 memcpy (&data[first_len], ao->buffer, len - first_len); |
10147
f2725d6717bd
Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff
changeset
|
172 } |
14769
e701873aa8ca
rids ao_macosx of the buffer mutex by using the same buffering scheme as ao_sdl - Patch by Reimar Doffinger
nplourde
parents:
14642
diff
changeset
|
173 ao->buf_read_pos = (ao->buf_read_pos + len) % ao->buffer_len; |
e701873aa8ca
rids ao_macosx of the buffer mutex by using the same buffering scheme as ao_sdl - Patch by Reimar Doffinger
nplourde
parents:
14642
diff
changeset
|
174 return len; |
10147
f2725d6717bd
Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff
changeset
|
175 } |
f2725d6717bd
Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff
changeset
|
176 |
15722
b53c4b26dc96
removes the use of AudioConverters. patch by Alexander Strange <alexander.strange@ithinksw.com>
nplourde
parents:
15646
diff
changeset
|
177 OSStatus theRenderProc(void *inRefCon, AudioUnitRenderActionFlags *inActionFlags, const AudioTimeStamp *inTimeStamp, UInt32 inBusNumber, UInt32 inNumFrames, AudioBufferList *ioData) |
10147
f2725d6717bd
Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff
changeset
|
178 { |
15646
f46eae5e271b
Mac OS X Audio with AudioUnits and AudioToolbox format converters, Patch by Chris Roccati<roccati@pobox.com
nplourde
parents:
15486
diff
changeset
|
179 int amt=buf_used(); |
15722
b53c4b26dc96
removes the use of AudioConverters. patch by Alexander Strange <alexander.strange@ithinksw.com>
nplourde
parents:
15646
diff
changeset
|
180 int req=(inNumFrames)*ao->packetSize; |
15646
f46eae5e271b
Mac OS X Audio with AudioUnits and AudioToolbox format converters, Patch by Chris Roccati<roccati@pobox.com
nplourde
parents:
15486
diff
changeset
|
181 |
f46eae5e271b
Mac OS X Audio with AudioUnits and AudioToolbox format converters, Patch by Chris Roccati<roccati@pobox.com
nplourde
parents:
15486
diff
changeset
|
182 if(amt>req) |
f46eae5e271b
Mac OS X Audio with AudioUnits and AudioToolbox format converters, Patch by Chris Roccati<roccati@pobox.com
nplourde
parents:
15486
diff
changeset
|
183 amt=req; |
f46eae5e271b
Mac OS X Audio with AudioUnits and AudioToolbox format converters, Patch by Chris Roccati<roccati@pobox.com
nplourde
parents:
15486
diff
changeset
|
184 |
f46eae5e271b
Mac OS X Audio with AudioUnits and AudioToolbox format converters, Patch by Chris Roccati<roccati@pobox.com
nplourde
parents:
15486
diff
changeset
|
185 if(amt) |
f46eae5e271b
Mac OS X Audio with AudioUnits and AudioToolbox format converters, Patch by Chris Roccati<roccati@pobox.com
nplourde
parents:
15486
diff
changeset
|
186 read_buffer((unsigned char *)ioData->mBuffers[0].mData, amt); |
15853
6e994905f77b
more general ao_macosx cleanup. Patch by Alexander Strange <astrange@ithinksw.com>
nplourde
parents:
15722
diff
changeset
|
187 else audio_pause(); |
15722
b53c4b26dc96
removes the use of AudioConverters. patch by Alexander Strange <alexander.strange@ithinksw.com>
nplourde
parents:
15646
diff
changeset
|
188 ioData->mBuffers[0].mDataByteSize = amt; |
15646
f46eae5e271b
Mac OS X Audio with AudioUnits and AudioToolbox format converters, Patch by Chris Roccati<roccati@pobox.com
nplourde
parents:
15486
diff
changeset
|
189 |
f46eae5e271b
Mac OS X Audio with AudioUnits and AudioToolbox format converters, Patch by Chris Roccati<roccati@pobox.com
nplourde
parents:
15486
diff
changeset
|
190 return noErr; |
10147
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 |
f2725d6717bd
Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff
changeset
|
193 static int control(int cmd,void *arg){ |
21352
2d786b7625d7
adds support for AOCONTROL_GET_VOLUME/AOCONTROL_SET_VOLUME in ao_macosx, patcg by Crhis Roccati<roccati@pobox.com>
nplourde
parents:
19427
diff
changeset
|
194 ao_control_vol_t *control_vol; |
2d786b7625d7
adds support for AOCONTROL_GET_VOLUME/AOCONTROL_SET_VOLUME in ao_macosx, patcg by Crhis Roccati<roccati@pobox.com>
nplourde
parents:
19427
diff
changeset
|
195 OSStatus err; |
23381
300e9b7c499f
Remove some unused variables, patch by timwoj ieee org.
diego
parents:
21977
diff
changeset
|
196 Float32 vol; |
10147
f2725d6717bd
Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff
changeset
|
197 switch (cmd) { |
14290 | 198 case AOCONTROL_GET_VOLUME: |
21352
2d786b7625d7
adds support for AOCONTROL_GET_VOLUME/AOCONTROL_SET_VOLUME in ao_macosx, patcg by Crhis Roccati<roccati@pobox.com>
nplourde
parents:
19427
diff
changeset
|
199 control_vol = (ao_control_vol_t*)arg; |
24734 | 200 if (ao->b_digital) { |
201 // Digital output has no volume adjust. | |
202 return CONTROL_FALSE; | |
203 } | |
21352
2d786b7625d7
adds support for AOCONTROL_GET_VOLUME/AOCONTROL_SET_VOLUME in ao_macosx, patcg by Crhis Roccati<roccati@pobox.com>
nplourde
parents:
19427
diff
changeset
|
204 err = AudioUnitGetParameter(ao->theOutputUnit, kHALOutputParam_Volume, kAudioUnitScope_Global, 0, &vol); |
2d786b7625d7
adds support for AOCONTROL_GET_VOLUME/AOCONTROL_SET_VOLUME in ao_macosx, patcg by Crhis Roccati<roccati@pobox.com>
nplourde
parents:
19427
diff
changeset
|
205 |
2d786b7625d7
adds support for AOCONTROL_GET_VOLUME/AOCONTROL_SET_VOLUME in ao_macosx, patcg by Crhis Roccati<roccati@pobox.com>
nplourde
parents:
19427
diff
changeset
|
206 if(err==0) { |
2d786b7625d7
adds support for AOCONTROL_GET_VOLUME/AOCONTROL_SET_VOLUME in ao_macosx, patcg by Crhis Roccati<roccati@pobox.com>
nplourde
parents:
19427
diff
changeset
|
207 // printf("GET VOL=%f\n", vol); |
2d786b7625d7
adds support for AOCONTROL_GET_VOLUME/AOCONTROL_SET_VOLUME in ao_macosx, patcg by Crhis Roccati<roccati@pobox.com>
nplourde
parents:
19427
diff
changeset
|
208 control_vol->left=control_vol->right=vol*100.0/4.0; |
2d786b7625d7
adds support for AOCONTROL_GET_VOLUME/AOCONTROL_SET_VOLUME in ao_macosx, patcg by Crhis Roccati<roccati@pobox.com>
nplourde
parents:
19427
diff
changeset
|
209 return CONTROL_TRUE; |
2d786b7625d7
adds support for AOCONTROL_GET_VOLUME/AOCONTROL_SET_VOLUME in ao_macosx, patcg by Crhis Roccati<roccati@pobox.com>
nplourde
parents:
19427
diff
changeset
|
210 } |
2d786b7625d7
adds support for AOCONTROL_GET_VOLUME/AOCONTROL_SET_VOLUME in ao_macosx, patcg by Crhis Roccati<roccati@pobox.com>
nplourde
parents:
19427
diff
changeset
|
211 else { |
24734 | 212 ao_msg(MSGT_AO, MSGL_WARN, "could not get HAL output volume: [%4.4s]\n", (char *)&err); |
21352
2d786b7625d7
adds support for AOCONTROL_GET_VOLUME/AOCONTROL_SET_VOLUME in ao_macosx, patcg by Crhis Roccati<roccati@pobox.com>
nplourde
parents:
19427
diff
changeset
|
213 return CONTROL_FALSE; |
2d786b7625d7
adds support for AOCONTROL_GET_VOLUME/AOCONTROL_SET_VOLUME in ao_macosx, patcg by Crhis Roccati<roccati@pobox.com>
nplourde
parents:
19427
diff
changeset
|
214 } |
2d786b7625d7
adds support for AOCONTROL_GET_VOLUME/AOCONTROL_SET_VOLUME in ao_macosx, patcg by Crhis Roccati<roccati@pobox.com>
nplourde
parents:
19427
diff
changeset
|
215 |
14290 | 216 case AOCONTROL_SET_VOLUME: |
24734 | 217 if (ao->b_digital) |
218 // Digital output can not set volume. Here we have to return true | |
219 // to make mixer forget it. Else mixer will add a soft filter, | |
220 // that's not we expected and the filter not support ac3 stream | |
221 // will cause mplayer die. | |
222 return CONTROL_TRUE; | |
21352
2d786b7625d7
adds support for AOCONTROL_GET_VOLUME/AOCONTROL_SET_VOLUME in ao_macosx, patcg by Crhis Roccati<roccati@pobox.com>
nplourde
parents:
19427
diff
changeset
|
223 control_vol = (ao_control_vol_t*)arg; |
2d786b7625d7
adds support for AOCONTROL_GET_VOLUME/AOCONTROL_SET_VOLUME in ao_macosx, patcg by Crhis Roccati<roccati@pobox.com>
nplourde
parents:
19427
diff
changeset
|
224 |
2d786b7625d7
adds support for AOCONTROL_GET_VOLUME/AOCONTROL_SET_VOLUME in ao_macosx, patcg by Crhis Roccati<roccati@pobox.com>
nplourde
parents:
19427
diff
changeset
|
225 vol=(control_vol->left+control_vol->right)*4.0/200.0; |
2d786b7625d7
adds support for AOCONTROL_GET_VOLUME/AOCONTROL_SET_VOLUME in ao_macosx, patcg by Crhis Roccati<roccati@pobox.com>
nplourde
parents:
19427
diff
changeset
|
226 err = AudioUnitSetParameter(ao->theOutputUnit, kHALOutputParam_Volume, kAudioUnitScope_Global, 0, vol, 0); |
2d786b7625d7
adds support for AOCONTROL_GET_VOLUME/AOCONTROL_SET_VOLUME in ao_macosx, patcg by Crhis Roccati<roccati@pobox.com>
nplourde
parents:
19427
diff
changeset
|
227 if(err==0) { |
2d786b7625d7
adds support for AOCONTROL_GET_VOLUME/AOCONTROL_SET_VOLUME in ao_macosx, patcg by Crhis Roccati<roccati@pobox.com>
nplourde
parents:
19427
diff
changeset
|
228 // printf("SET VOL=%f\n", vol); |
2d786b7625d7
adds support for AOCONTROL_GET_VOLUME/AOCONTROL_SET_VOLUME in ao_macosx, patcg by Crhis Roccati<roccati@pobox.com>
nplourde
parents:
19427
diff
changeset
|
229 return CONTROL_TRUE; |
2d786b7625d7
adds support for AOCONTROL_GET_VOLUME/AOCONTROL_SET_VOLUME in ao_macosx, patcg by Crhis Roccati<roccati@pobox.com>
nplourde
parents:
19427
diff
changeset
|
230 } |
2d786b7625d7
adds support for AOCONTROL_GET_VOLUME/AOCONTROL_SET_VOLUME in ao_macosx, patcg by Crhis Roccati<roccati@pobox.com>
nplourde
parents:
19427
diff
changeset
|
231 else { |
24734 | 232 ao_msg(MSGT_AO, MSGL_WARN, "could not set HAL output volume: [%4.4s]\n", (char *)&err); |
21352
2d786b7625d7
adds support for AOCONTROL_GET_VOLUME/AOCONTROL_SET_VOLUME in ao_macosx, patcg by Crhis Roccati<roccati@pobox.com>
nplourde
parents:
19427
diff
changeset
|
233 return CONTROL_FALSE; |
2d786b7625d7
adds support for AOCONTROL_GET_VOLUME/AOCONTROL_SET_VOLUME in ao_macosx, patcg by Crhis Roccati<roccati@pobox.com>
nplourde
parents:
19427
diff
changeset
|
234 } |
15646
f46eae5e271b
Mac OS X Audio with AudioUnits and AudioToolbox format converters, Patch by Chris Roccati<roccati@pobox.com
nplourde
parents:
15486
diff
changeset
|
235 /* Everything is currently unimplemented */ |
10147
f2725d6717bd
Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff
changeset
|
236 default: |
10152 | 237 return CONTROL_FALSE; |
10147
f2725d6717bd
Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff
changeset
|
238 } |
f2725d6717bd
Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff
changeset
|
239 |
f2725d6717bd
Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff
changeset
|
240 } |
f2725d6717bd
Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff
changeset
|
241 |
f2725d6717bd
Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff
changeset
|
242 |
24734 | 243 static void print_format(int lev, const char* str, const AudioStreamBasicDescription *f){ |
14290 | 244 uint32_t flags=(uint32_t) f->mFormatFlags; |
24734 | 245 ao_msg(MSGT_AO,lev, "%s %7.1fHz %lubit [%c%c%c%c][%lu][%lu][%lu][%lu][%lu] %s %s %s%s%s%s\n", |
14317 | 246 str, f->mSampleRate, f->mBitsPerChannel, |
14290 | 247 (int)(f->mFormatID & 0xff000000) >> 24, |
248 (int)(f->mFormatID & 0x00ff0000) >> 16, | |
249 (int)(f->mFormatID & 0x0000ff00) >> 8, | |
250 (int)(f->mFormatID & 0x000000ff) >> 0, | |
24734 | 251 f->mFormatFlags, f->mBytesPerPacket, |
252 f->mFramesPerPacket, f->mBytesPerFrame, | |
253 f->mChannelsPerFrame, | |
14290 | 254 (flags&kAudioFormatFlagIsFloat) ? "float" : "int", |
255 (flags&kAudioFormatFlagIsBigEndian) ? "BE" : "LE", | |
256 (flags&kAudioFormatFlagIsSignedInteger) ? "S" : "U", | |
257 (flags&kAudioFormatFlagIsPacked) ? " packed" : "", | |
258 (flags&kAudioFormatFlagIsAlignedHigh) ? " aligned" : "", | |
259 (flags&kAudioFormatFlagIsNonInterleaved) ? " ni" : "" ); | |
260 } | |
261 | |
262 | |
24734 | 263 static int AudioDeviceSupportsDigital( AudioDeviceID i_dev_id ); |
264 static int AudioStreamSupportsDigital( AudioStreamID i_stream_id ); | |
265 static int OpenSPDIF(); | |
266 static int AudioStreamChangeFormat( AudioStreamID i_stream_id, AudioStreamBasicDescription change_format ); | |
267 static OSStatus RenderCallbackSPDIF( AudioDeviceID inDevice, | |
268 const AudioTimeStamp * inNow, | |
269 const void * inInputData, | |
270 const AudioTimeStamp * inInputTime, | |
271 AudioBufferList * outOutputData, | |
272 const AudioTimeStamp * inOutputTime, | |
273 void * threadGlobals ); | |
274 static OSStatus StreamListener( AudioStreamID inStream, | |
275 UInt32 inChannel, | |
276 AudioDevicePropertyID inPropertyID, | |
277 void * inClientData ); | |
278 static OSStatus DeviceListener( AudioDeviceID inDevice, | |
279 UInt32 inChannel, | |
280 Boolean isInput, | |
281 AudioDevicePropertyID inPropertyID, | |
282 void* inClientData ); | |
283 | |
10147
f2725d6717bd
Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff
changeset
|
284 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
|
285 { |
23381
300e9b7c499f
Remove some unused variables, patch by timwoj ieee org.
diego
parents:
21977
diff
changeset
|
286 AudioStreamBasicDescription inDesc; |
15646
f46eae5e271b
Mac OS X Audio with AudioUnits and AudioToolbox format converters, Patch by Chris Roccati<roccati@pobox.com
nplourde
parents:
15486
diff
changeset
|
287 ComponentDescription desc; |
f46eae5e271b
Mac OS X Audio with AudioUnits and AudioToolbox format converters, Patch by Chris Roccati<roccati@pobox.com
nplourde
parents:
15486
diff
changeset
|
288 Component comp; |
f46eae5e271b
Mac OS X Audio with AudioUnits and AudioToolbox format converters, Patch by Chris Roccati<roccati@pobox.com
nplourde
parents:
15486
diff
changeset
|
289 AURenderCallbackStruct renderCallback; |
f46eae5e271b
Mac OS X Audio with AudioUnits and AudioToolbox format converters, Patch by Chris Roccati<roccati@pobox.com
nplourde
parents:
15486
diff
changeset
|
290 OSStatus err; |
24734 | 291 UInt32 size, maxFrames, i_param_size; |
292 char *psz_name; | |
15853
6e994905f77b
more general ao_macosx cleanup. Patch by Alexander Strange <astrange@ithinksw.com>
nplourde
parents:
15722
diff
changeset
|
293 int aoIsCreated = ao != NULL; |
24734 | 294 AudioDeviceID devid_def = 0; |
295 int b_alive; | |
14317 | 296 |
24734 | 297 ao_msg(MSGT_AO,MSGL_V, "init([%dHz][%dch][%s][%d])\n", rate, channels, af_fmt2str_short(format), flags); |
298 | |
299 if (!aoIsCreated) { ao = malloc(sizeof(ao_macosx_t)); ao->buffer = NULL;} | |
300 | |
301 ao->i_selected_dev = 0; | |
302 ao->b_supports_digital = 0; | |
303 ao->b_digital = 0; | |
304 ao->b_stream_format_changed = 0; | |
305 ao->i_hog_pid = -1; | |
306 ao->i_stream_id = 0; | |
307 ao->i_stream_index = -1; | |
308 ao->b_revert = 0; | |
309 ao->b_changed_mixing = 0; | |
310 | |
311 /* Probe whether device support S/PDIF stream output if input is AC3 stream. */ | |
312 if ((format & AF_FORMAT_SPECIAL_MASK) == AF_FORMAT_AC3) | |
313 { | |
314 /* Find the ID of the default Device. */ | |
315 i_param_size = sizeof(AudioDeviceID); | |
316 err = AudioHardwareGetProperty(kAudioHardwarePropertyDefaultOutputDevice, | |
317 &i_param_size, &devid_def); | |
318 if (err != noErr) | |
319 { | |
320 ao_msg(MSGT_AO, MSGL_WARN, "could not get default audio device: [%4.4s]\n", (char *)&err); | |
321 return CONTROL_FALSE; | |
322 } | |
323 | |
324 /* Retrieve the length of the device name. */ | |
325 i_param_size = 0; | |
326 err = AudioDeviceGetPropertyInfo(devid_def, 0, 0, | |
327 kAudioDevicePropertyDeviceName, | |
328 &i_param_size, NULL); | |
329 if (err != noErr) | |
330 { | |
331 ao_msg(MSGT_AO, MSGL_WARN, "could not get default audio device name length: [%4.4s]\n", (char *)&err); | |
332 return CONTROL_FALSE; | |
333 } | |
334 | |
335 /* Retrieve the name of the device. */ | |
336 psz_name = (char *)malloc(i_param_size); | |
337 err = AudioDeviceGetProperty(devid_def, 0, 0, | |
338 kAudioDevicePropertyDeviceName, | |
339 &i_param_size, psz_name); | |
340 if (err != noErr) | |
341 { | |
342 ao_msg(MSGT_AO, MSGL_WARN, "could not get default audio device name: [%4.4s]\n", (char *)&err); | |
343 return CONTROL_FALSE; | |
344 } | |
345 | |
346 ao_msg(MSGT_AO,MSGL_V, "got default audio output device ID: %#lx Name: %s\n", devid_def, psz_name ); | |
347 | |
348 if (AudioDeviceSupportsDigital(devid_def)) | |
349 { | |
350 ao->b_supports_digital = 1; | |
351 ao->i_selected_dev = devid_def; | |
352 } | |
353 ao_msg(MSGT_AO,MSGL_V, "probe default audio output device whether support digital s/pdif output:%d\n", ao->b_supports_digital ); | |
354 | |
355 free( psz_name); | |
356 } | |
14290 | 357 |
15646
f46eae5e271b
Mac OS X Audio with AudioUnits and AudioToolbox format converters, Patch by Chris Roccati<roccati@pobox.com
nplourde
parents:
15486
diff
changeset
|
358 // Build Description for the input format |
f46eae5e271b
Mac OS X Audio with AudioUnits and AudioToolbox format converters, Patch by Chris Roccati<roccati@pobox.com
nplourde
parents:
15486
diff
changeset
|
359 inDesc.mSampleRate=rate; |
24734 | 360 inDesc.mFormatID=ao->b_supports_digital ? kAudioFormat60958AC3 : kAudioFormatLinearPCM; |
15646
f46eae5e271b
Mac OS X Audio with AudioUnits and AudioToolbox format converters, Patch by Chris Roccati<roccati@pobox.com
nplourde
parents:
15486
diff
changeset
|
361 inDesc.mChannelsPerFrame=channels; |
f46eae5e271b
Mac OS X Audio with AudioUnits and AudioToolbox format converters, Patch by Chris Roccati<roccati@pobox.com
nplourde
parents:
15486
diff
changeset
|
362 switch(format&AF_FORMAT_BITS_MASK){ |
f46eae5e271b
Mac OS X Audio with AudioUnits and AudioToolbox format converters, Patch by Chris Roccati<roccati@pobox.com
nplourde
parents:
15486
diff
changeset
|
363 case AF_FORMAT_8BIT: |
f46eae5e271b
Mac OS X Audio with AudioUnits and AudioToolbox format converters, Patch by Chris Roccati<roccati@pobox.com
nplourde
parents:
15486
diff
changeset
|
364 inDesc.mBitsPerChannel=8; |
f46eae5e271b
Mac OS X Audio with AudioUnits and AudioToolbox format converters, Patch by Chris Roccati<roccati@pobox.com
nplourde
parents:
15486
diff
changeset
|
365 break; |
f46eae5e271b
Mac OS X Audio with AudioUnits and AudioToolbox format converters, Patch by Chris Roccati<roccati@pobox.com
nplourde
parents:
15486
diff
changeset
|
366 case AF_FORMAT_16BIT: |
f46eae5e271b
Mac OS X Audio with AudioUnits and AudioToolbox format converters, Patch by Chris Roccati<roccati@pobox.com
nplourde
parents:
15486
diff
changeset
|
367 inDesc.mBitsPerChannel=16; |
f46eae5e271b
Mac OS X Audio with AudioUnits and AudioToolbox format converters, Patch by Chris Roccati<roccati@pobox.com
nplourde
parents:
15486
diff
changeset
|
368 break; |
f46eae5e271b
Mac OS X Audio with AudioUnits and AudioToolbox format converters, Patch by Chris Roccati<roccati@pobox.com
nplourde
parents:
15486
diff
changeset
|
369 case AF_FORMAT_24BIT: |
f46eae5e271b
Mac OS X Audio with AudioUnits and AudioToolbox format converters, Patch by Chris Roccati<roccati@pobox.com
nplourde
parents:
15486
diff
changeset
|
370 inDesc.mBitsPerChannel=24; |
f46eae5e271b
Mac OS X Audio with AudioUnits and AudioToolbox format converters, Patch by Chris Roccati<roccati@pobox.com
nplourde
parents:
15486
diff
changeset
|
371 break; |
f46eae5e271b
Mac OS X Audio with AudioUnits and AudioToolbox format converters, Patch by Chris Roccati<roccati@pobox.com
nplourde
parents:
15486
diff
changeset
|
372 case AF_FORMAT_32BIT: |
f46eae5e271b
Mac OS X Audio with AudioUnits and AudioToolbox format converters, Patch by Chris Roccati<roccati@pobox.com
nplourde
parents:
15486
diff
changeset
|
373 inDesc.mBitsPerChannel=32; |
f46eae5e271b
Mac OS X Audio with AudioUnits and AudioToolbox format converters, Patch by Chris Roccati<roccati@pobox.com
nplourde
parents:
15486
diff
changeset
|
374 break; |
f46eae5e271b
Mac OS X Audio with AudioUnits and AudioToolbox format converters, Patch by Chris Roccati<roccati@pobox.com
nplourde
parents:
15486
diff
changeset
|
375 default: |
f46eae5e271b
Mac OS X Audio with AudioUnits and AudioToolbox format converters, Patch by Chris Roccati<roccati@pobox.com
nplourde
parents:
15486
diff
changeset
|
376 ao_msg(MSGT_AO, MSGL_WARN, "Unsupported format (0x%08x)\n", format); |
14290 | 377 return CONTROL_FALSE; |
15646
f46eae5e271b
Mac OS X Audio with AudioUnits and AudioToolbox format converters, Patch by Chris Roccati<roccati@pobox.com
nplourde
parents:
15486
diff
changeset
|
378 break; |
f46eae5e271b
Mac OS X Audio with AudioUnits and AudioToolbox format converters, Patch by Chris Roccati<roccati@pobox.com
nplourde
parents:
15486
diff
changeset
|
379 } |
10147
f2725d6717bd
Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff
changeset
|
380 |
15646
f46eae5e271b
Mac OS X Audio with AudioUnits and AudioToolbox format converters, Patch by Chris Roccati<roccati@pobox.com
nplourde
parents:
15486
diff
changeset
|
381 if((format&AF_FORMAT_POINT_MASK)==AF_FORMAT_F) { |
f46eae5e271b
Mac OS X Audio with AudioUnits and AudioToolbox format converters, Patch by Chris Roccati<roccati@pobox.com
nplourde
parents:
15486
diff
changeset
|
382 // float |
f46eae5e271b
Mac OS X Audio with AudioUnits and AudioToolbox format converters, Patch by Chris Roccati<roccati@pobox.com
nplourde
parents:
15486
diff
changeset
|
383 inDesc.mFormatFlags = kAudioFormatFlagIsFloat|kAudioFormatFlagIsPacked; |
10147
f2725d6717bd
Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff
changeset
|
384 } |
15646
f46eae5e271b
Mac OS X Audio with AudioUnits and AudioToolbox format converters, Patch by Chris Roccati<roccati@pobox.com
nplourde
parents:
15486
diff
changeset
|
385 else if((format&AF_FORMAT_SIGN_MASK)==AF_FORMAT_SI) { |
f46eae5e271b
Mac OS X Audio with AudioUnits and AudioToolbox format converters, Patch by Chris Roccati<roccati@pobox.com
nplourde
parents:
15486
diff
changeset
|
386 // signed int |
f46eae5e271b
Mac OS X Audio with AudioUnits and AudioToolbox format converters, Patch by Chris Roccati<roccati@pobox.com
nplourde
parents:
15486
diff
changeset
|
387 inDesc.mFormatFlags = kAudioFormatFlagIsSignedInteger|kAudioFormatFlagIsPacked; |
f46eae5e271b
Mac OS X Audio with AudioUnits and AudioToolbox format converters, Patch by Chris Roccati<roccati@pobox.com
nplourde
parents:
15486
diff
changeset
|
388 } |
f46eae5e271b
Mac OS X Audio with AudioUnits and AudioToolbox format converters, Patch by Chris Roccati<roccati@pobox.com
nplourde
parents:
15486
diff
changeset
|
389 else { |
f46eae5e271b
Mac OS X Audio with AudioUnits and AudioToolbox format converters, Patch by Chris Roccati<roccati@pobox.com
nplourde
parents:
15486
diff
changeset
|
390 // unsigned int |
f46eae5e271b
Mac OS X Audio with AudioUnits and AudioToolbox format converters, Patch by Chris Roccati<roccati@pobox.com
nplourde
parents:
15486
diff
changeset
|
391 inDesc.mFormatFlags = kAudioFormatFlagIsPacked; |
10147
f2725d6717bd
Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff
changeset
|
392 } |
24734 | 393 if ((format & AF_FORMAT_SPECIAL_MASK) == AF_FORMAT_AC3) { |
394 // Currently ac3 input (comes from hwac3) is always in native byte-order. | |
395 #ifdef WORDS_BIGENDIAN | |
396 inDesc.mFormatFlags |= kAudioFormatFlagIsBigEndian; | |
397 #endif | |
398 } | |
399 else if ((format & AF_FORMAT_END_MASK) == AF_FORMAT_BE) | |
400 inDesc.mFormatFlags |= kAudioFormatFlagIsBigEndian; | |
15646
f46eae5e271b
Mac OS X Audio with AudioUnits and AudioToolbox format converters, Patch by Chris Roccati<roccati@pobox.com
nplourde
parents:
15486
diff
changeset
|
401 |
f46eae5e271b
Mac OS X Audio with AudioUnits and AudioToolbox format converters, Patch by Chris Roccati<roccati@pobox.com
nplourde
parents:
15486
diff
changeset
|
402 inDesc.mFramesPerPacket = 1; |
f46eae5e271b
Mac OS X Audio with AudioUnits and AudioToolbox format converters, Patch by Chris Roccati<roccati@pobox.com
nplourde
parents:
15486
diff
changeset
|
403 ao->packetSize = inDesc.mBytesPerPacket = inDesc.mBytesPerFrame = inDesc.mFramesPerPacket*channels*(inDesc.mBitsPerChannel/8); |
24734 | 404 print_format(MSGL_V, "source:",&inDesc); |
405 | |
406 if (ao->b_supports_digital) | |
407 { | |
408 b_alive = 1; | |
409 i_param_size = sizeof(b_alive); | |
410 err = AudioDeviceGetProperty(ao->i_selected_dev, 0, FALSE, | |
411 kAudioDevicePropertyDeviceIsAlive, | |
412 &i_param_size, &b_alive); | |
413 if (err != noErr) | |
414 ao_msg(MSGT_AO, MSGL_WARN, "could not check whether device is alive: [%4.4s]\n", (char *)&err); | |
415 if (!b_alive) | |
416 ao_msg(MSGT_AO, MSGL_WARN, "device is not alive\n" ); | |
417 /* S/PDIF output need device in HogMode. */ | |
418 i_param_size = sizeof(ao->i_hog_pid); | |
419 err = AudioDeviceGetProperty(ao->i_selected_dev, 0, FALSE, | |
420 kAudioDevicePropertyHogMode, | |
421 &i_param_size, &ao->i_hog_pid); | |
15646
f46eae5e271b
Mac OS X Audio with AudioUnits and AudioToolbox format converters, Patch by Chris Roccati<roccati@pobox.com
nplourde
parents:
15486
diff
changeset
|
422 |
24734 | 423 if (err != noErr) |
424 { | |
425 /* This is not a fatal error. Some drivers simply don't support this property. */ | |
426 ao_msg(MSGT_AO, MSGL_WARN, "could not check whether device is hogged: [%4.4s]\n", | |
427 (char *)&err); | |
428 ao->i_hog_pid = -1; | |
429 } | |
430 | |
431 if (ao->i_hog_pid != -1 && ao->i_hog_pid != getpid()) | |
432 { | |
433 ao_msg(MSGT_AO, MSGL_WARN, "Selected audio device is exclusively in use by another program.\n" ); | |
434 return CONTROL_FALSE; | |
435 } | |
436 ao->stream_format = inDesc; | |
437 return OpenSPDIF(); | |
438 } | |
439 | |
440 /* original analog output code */ | |
15853
6e994905f77b
more general ao_macosx cleanup. Patch by Alexander Strange <astrange@ithinksw.com>
nplourde
parents:
15722
diff
changeset
|
441 if (!aoIsCreated) { |
15646
f46eae5e271b
Mac OS X Audio with AudioUnits and AudioToolbox format converters, Patch by Chris Roccati<roccati@pobox.com
nplourde
parents:
15486
diff
changeset
|
442 desc.componentType = kAudioUnitType_Output; |
f46eae5e271b
Mac OS X Audio with AudioUnits and AudioToolbox format converters, Patch by Chris Roccati<roccati@pobox.com
nplourde
parents:
15486
diff
changeset
|
443 desc.componentSubType = kAudioUnitSubType_DefaultOutput; |
f46eae5e271b
Mac OS X Audio with AudioUnits and AudioToolbox format converters, Patch by Chris Roccati<roccati@pobox.com
nplourde
parents:
15486
diff
changeset
|
444 desc.componentManufacturer = kAudioUnitManufacturer_Apple; |
f46eae5e271b
Mac OS X Audio with AudioUnits and AudioToolbox format converters, Patch by Chris Roccati<roccati@pobox.com
nplourde
parents:
15486
diff
changeset
|
445 desc.componentFlags = 0; |
f46eae5e271b
Mac OS X Audio with AudioUnits and AudioToolbox format converters, Patch by Chris Roccati<roccati@pobox.com
nplourde
parents:
15486
diff
changeset
|
446 desc.componentFlagsMask = 0; |
f46eae5e271b
Mac OS X Audio with AudioUnits and AudioToolbox format converters, Patch by Chris Roccati<roccati@pobox.com
nplourde
parents:
15486
diff
changeset
|
447 |
f46eae5e271b
Mac OS X Audio with AudioUnits and AudioToolbox format converters, Patch by Chris Roccati<roccati@pobox.com
nplourde
parents:
15486
diff
changeset
|
448 comp = FindNextComponent(NULL, &desc); //Finds an component that meets the desc spec's |
f46eae5e271b
Mac OS X Audio with AudioUnits and AudioToolbox format converters, Patch by Chris Roccati<roccati@pobox.com
nplourde
parents:
15486
diff
changeset
|
449 if (comp == NULL) { |
f46eae5e271b
Mac OS X Audio with AudioUnits and AudioToolbox format converters, Patch by Chris Roccati<roccati@pobox.com
nplourde
parents:
15486
diff
changeset
|
450 ao_msg(MSGT_AO, MSGL_WARN, "Unable to find Output Unit component\n"); |
f46eae5e271b
Mac OS X Audio with AudioUnits and AudioToolbox format converters, Patch by Chris Roccati<roccati@pobox.com
nplourde
parents:
15486
diff
changeset
|
451 return CONTROL_FALSE; |
f46eae5e271b
Mac OS X Audio with AudioUnits and AudioToolbox format converters, Patch by Chris Roccati<roccati@pobox.com
nplourde
parents:
15486
diff
changeset
|
452 } |
f46eae5e271b
Mac OS X Audio with AudioUnits and AudioToolbox format converters, Patch by Chris Roccati<roccati@pobox.com
nplourde
parents:
15486
diff
changeset
|
453 |
f46eae5e271b
Mac OS X Audio with AudioUnits and AudioToolbox format converters, Patch by Chris Roccati<roccati@pobox.com
nplourde
parents:
15486
diff
changeset
|
454 err = OpenAComponent(comp, &(ao->theOutputUnit)); //gains access to the services provided by the component |
f46eae5e271b
Mac OS X Audio with AudioUnits and AudioToolbox format converters, Patch by Chris Roccati<roccati@pobox.com
nplourde
parents:
15486
diff
changeset
|
455 if (err) { |
24734 | 456 ao_msg(MSGT_AO, MSGL_WARN, "Unable to open Output Unit component: [%4.4s]\n", (char *)&err); |
15646
f46eae5e271b
Mac OS X Audio with AudioUnits and AudioToolbox format converters, Patch by Chris Roccati<roccati@pobox.com
nplourde
parents:
15486
diff
changeset
|
457 return CONTROL_FALSE; |
f46eae5e271b
Mac OS X Audio with AudioUnits and AudioToolbox format converters, Patch by Chris Roccati<roccati@pobox.com
nplourde
parents:
15486
diff
changeset
|
458 } |
f46eae5e271b
Mac OS X Audio with AudioUnits and AudioToolbox format converters, Patch by Chris Roccati<roccati@pobox.com
nplourde
parents:
15486
diff
changeset
|
459 |
f46eae5e271b
Mac OS X Audio with AudioUnits and AudioToolbox format converters, Patch by Chris Roccati<roccati@pobox.com
nplourde
parents:
15486
diff
changeset
|
460 // Initialize AudioUnit |
f46eae5e271b
Mac OS X Audio with AudioUnits and AudioToolbox format converters, Patch by Chris Roccati<roccati@pobox.com
nplourde
parents:
15486
diff
changeset
|
461 err = AudioUnitInitialize(ao->theOutputUnit); |
f46eae5e271b
Mac OS X Audio with AudioUnits and AudioToolbox format converters, Patch by Chris Roccati<roccati@pobox.com
nplourde
parents:
15486
diff
changeset
|
462 if (err) { |
24734 | 463 ao_msg(MSGT_AO, MSGL_WARN, "Unable to initialize Output Unit component: [%4.4s]\n", (char *)&err); |
15646
f46eae5e271b
Mac OS X Audio with AudioUnits and AudioToolbox format converters, Patch by Chris Roccati<roccati@pobox.com
nplourde
parents:
15486
diff
changeset
|
464 return CONTROL_FALSE; |
f46eae5e271b
Mac OS X Audio with AudioUnits and AudioToolbox format converters, Patch by Chris Roccati<roccati@pobox.com
nplourde
parents:
15486
diff
changeset
|
465 } |
15853
6e994905f77b
more general ao_macosx cleanup. Patch by Alexander Strange <astrange@ithinksw.com>
nplourde
parents:
15722
diff
changeset
|
466 } |
15646
f46eae5e271b
Mac OS X Audio with AudioUnits and AudioToolbox format converters, Patch by Chris Roccati<roccati@pobox.com
nplourde
parents:
15486
diff
changeset
|
467 |
f46eae5e271b
Mac OS X Audio with AudioUnits and AudioToolbox format converters, Patch by Chris Roccati<roccati@pobox.com
nplourde
parents:
15486
diff
changeset
|
468 size = sizeof(AudioStreamBasicDescription); |
15722
b53c4b26dc96
removes the use of AudioConverters. patch by Alexander Strange <alexander.strange@ithinksw.com>
nplourde
parents:
15646
diff
changeset
|
469 err = AudioUnitSetProperty(ao->theOutputUnit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, 0, &inDesc, size); |
15646
f46eae5e271b
Mac OS X Audio with AudioUnits and AudioToolbox format converters, Patch by Chris Roccati<roccati@pobox.com
nplourde
parents:
15486
diff
changeset
|
470 |
f46eae5e271b
Mac OS X Audio with AudioUnits and AudioToolbox format converters, Patch by Chris Roccati<roccati@pobox.com
nplourde
parents:
15486
diff
changeset
|
471 if (err) { |
24734 | 472 ao_msg(MSGT_AO, MSGL_WARN, "Unable to set the input format: [%4.4s]\n", (char *)&err); |
15646
f46eae5e271b
Mac OS X Audio with AudioUnits and AudioToolbox format converters, Patch by Chris Roccati<roccati@pobox.com
nplourde
parents:
15486
diff
changeset
|
473 return CONTROL_FALSE; |
f46eae5e271b
Mac OS X Audio with AudioUnits and AudioToolbox format converters, Patch by Chris Roccati<roccati@pobox.com
nplourde
parents:
15486
diff
changeset
|
474 } |
f46eae5e271b
Mac OS X Audio with AudioUnits and AudioToolbox format converters, Patch by Chris Roccati<roccati@pobox.com
nplourde
parents:
15486
diff
changeset
|
475 |
16014 | 476 size = sizeof(UInt32); |
477 err = AudioUnitGetProperty(ao->theOutputUnit, kAudioDevicePropertyBufferSize, kAudioUnitScope_Input, 0, &maxFrames, &size); | |
15646
f46eae5e271b
Mac OS X Audio with AudioUnits and AudioToolbox format converters, Patch by Chris Roccati<roccati@pobox.com
nplourde
parents:
15486
diff
changeset
|
478 |
16014 | 479 if (err) |
480 { | |
24734 | 481 ao_msg(MSGT_AO,MSGL_WARN, "AudioUnitGetProperty returned [%4.4s] when getting kAudioDevicePropertyBufferSize\n", (char *)&err); |
15646
f46eae5e271b
Mac OS X Audio with AudioUnits and AudioToolbox format converters, Patch by Chris Roccati<roccati@pobox.com
nplourde
parents:
15486
diff
changeset
|
482 return CONTROL_FALSE; |
f46eae5e271b
Mac OS X Audio with AudioUnits and AudioToolbox format converters, Patch by Chris Roccati<roccati@pobox.com
nplourde
parents:
15486
diff
changeset
|
483 } |
f46eae5e271b
Mac OS X Audio with AudioUnits and AudioToolbox format converters, Patch by Chris Roccati<roccati@pobox.com
nplourde
parents:
15486
diff
changeset
|
484 |
16014 | 485 ao->chunk_size = maxFrames;//*inDesc.mBytesPerFrame; |
486 | |
19427
ada359817b61
fix buffering issues with short audio samples on macosx. patch by Chris Roccati <roccati@pobox.com>
nplourde
parents:
18915
diff
changeset
|
487 ao_data.samplerate = inDesc.mSampleRate; |
ada359817b61
fix buffering issues with short audio samples on macosx. patch by Chris Roccati <roccati@pobox.com>
nplourde
parents:
18915
diff
changeset
|
488 ao_data.channels = inDesc.mChannelsPerFrame; |
ada359817b61
fix buffering issues with short audio samples on macosx. patch by Chris Roccati <roccati@pobox.com>
nplourde
parents:
18915
diff
changeset
|
489 ao_data.bps = ao_data.samplerate * inDesc.mBytesPerFrame; |
ada359817b61
fix buffering issues with short audio samples on macosx. patch by Chris Roccati <roccati@pobox.com>
nplourde
parents:
18915
diff
changeset
|
490 ao_data.outburst = ao->chunk_size; |
ada359817b61
fix buffering issues with short audio samples on macosx. patch by Chris Roccati <roccati@pobox.com>
nplourde
parents:
18915
diff
changeset
|
491 ao_data.buffersize = ao_data.bps; |
ada359817b61
fix buffering issues with short audio samples on macosx. patch by Chris Roccati <roccati@pobox.com>
nplourde
parents:
18915
diff
changeset
|
492 |
ada359817b61
fix buffering issues with short audio samples on macosx. patch by Chris Roccati <roccati@pobox.com>
nplourde
parents:
18915
diff
changeset
|
493 ao->num_chunks = (ao_data.bps+ao->chunk_size-1)/ao->chunk_size; |
14769
e701873aa8ca
rids ao_macosx of the buffer mutex by using the same buffering scheme as ao_sdl - Patch by Reimar Doffinger
nplourde
parents:
14642
diff
changeset
|
494 ao->buffer_len = (ao->num_chunks + 1) * ao->chunk_size; |
18885 | 495 ao->buffer = aoIsCreated ? realloc(ao->buffer,(ao->num_chunks + 1)*ao->chunk_size) |
496 : calloc(ao->num_chunks + 1, ao->chunk_size); | |
15853
6e994905f77b
more general ao_macosx cleanup. Patch by Alexander Strange <astrange@ithinksw.com>
nplourde
parents:
15722
diff
changeset
|
497 |
19427
ada359817b61
fix buffering issues with short audio samples on macosx. patch by Chris Roccati <roccati@pobox.com>
nplourde
parents:
18915
diff
changeset
|
498 ao_msg(MSGT_AO,MSGL_V, "using %5d chunks of %d bytes (buffer len %d bytes)\n", (int)ao->num_chunks, (int)ao->chunk_size, (int)ao->buffer_len); |
10147
f2725d6717bd
Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff
changeset
|
499 |
15646
f46eae5e271b
Mac OS X Audio with AudioUnits and AudioToolbox format converters, Patch by Chris Roccati<roccati@pobox.com
nplourde
parents:
15486
diff
changeset
|
500 renderCallback.inputProc = theRenderProc; |
f46eae5e271b
Mac OS X Audio with AudioUnits and AudioToolbox format converters, Patch by Chris Roccati<roccati@pobox.com
nplourde
parents:
15486
diff
changeset
|
501 renderCallback.inputProcRefCon = 0; |
f46eae5e271b
Mac OS X Audio with AudioUnits and AudioToolbox format converters, Patch by Chris Roccati<roccati@pobox.com
nplourde
parents:
15486
diff
changeset
|
502 err = AudioUnitSetProperty(ao->theOutputUnit, kAudioUnitProperty_SetRenderCallback, kAudioUnitScope_Input, 0, &renderCallback, sizeof(AURenderCallbackStruct)); |
f46eae5e271b
Mac OS X Audio with AudioUnits and AudioToolbox format converters, Patch by Chris Roccati<roccati@pobox.com
nplourde
parents:
15486
diff
changeset
|
503 if (err) { |
24734 | 504 ao_msg(MSGT_AO, MSGL_WARN, "Unable to set the render callback: [%4.4s]\n", (char *)&err); |
15646
f46eae5e271b
Mac OS X Audio with AudioUnits and AudioToolbox format converters, Patch by Chris Roccati<roccati@pobox.com
nplourde
parents:
15486
diff
changeset
|
505 return CONTROL_FALSE; |
f46eae5e271b
Mac OS X Audio with AudioUnits and AudioToolbox format converters, Patch by Chris Roccati<roccati@pobox.com
nplourde
parents:
15486
diff
changeset
|
506 } |
f46eae5e271b
Mac OS X Audio with AudioUnits and AudioToolbox format converters, Patch by Chris Roccati<roccati@pobox.com
nplourde
parents:
15486
diff
changeset
|
507 |
19427
ada359817b61
fix buffering issues with short audio samples on macosx. patch by Chris Roccati <roccati@pobox.com>
nplourde
parents:
18915
diff
changeset
|
508 reset(); |
10147
f2725d6717bd
Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff
changeset
|
509 |
f2725d6717bd
Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff
changeset
|
510 return CONTROL_OK; |
f2725d6717bd
Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff
changeset
|
511 } |
f2725d6717bd
Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff
changeset
|
512 |
24734 | 513 /***************************************************************************** |
514 * Setup a encoded digital stream (SPDIF) | |
515 *****************************************************************************/ | |
516 static int OpenSPDIF() | |
517 { | |
518 OSStatus err = noErr; | |
519 UInt32 i_param_size, b_mix = 0; | |
520 Boolean b_writeable = 0; | |
521 AudioStreamID *p_streams = NULL; | |
522 int i, i_streams = 0; | |
523 | |
524 /* Start doing the SPDIF setup process. */ | |
525 ao->b_digital = 1; | |
526 | |
527 /* Hog the device. */ | |
528 i_param_size = sizeof(ao->i_hog_pid); | |
529 ao->i_hog_pid = getpid() ; | |
530 | |
531 err = AudioDeviceSetProperty(ao->i_selected_dev, 0, 0, FALSE, | |
532 kAudioDevicePropertyHogMode, i_param_size, &ao->i_hog_pid); | |
533 | |
534 if (err != noErr) | |
535 { | |
536 ao_msg(MSGT_AO, MSGL_WARN, "failed to set hogmode: [%4.4s]\n", (char *)&err); | |
537 return CONTROL_FALSE; | |
538 } | |
539 | |
540 /* Set mixable to false if we are allowed to. */ | |
541 err = AudioDeviceGetPropertyInfo(ao->i_selected_dev, 0, FALSE, | |
542 kAudioDevicePropertySupportsMixing, | |
543 &i_param_size, &b_writeable); | |
544 err = AudioDeviceGetProperty(ao->i_selected_dev, 0, FALSE, | |
545 kAudioDevicePropertySupportsMixing, | |
546 &i_param_size, &b_mix); | |
547 if (err != noErr && b_writeable) | |
548 { | |
549 b_mix = 0; | |
550 err = AudioDeviceSetProperty(ao->i_selected_dev, 0, 0, FALSE, | |
551 kAudioDevicePropertySupportsMixing, | |
552 i_param_size, &b_mix); | |
553 ao->b_changed_mixing = 1; | |
554 } | |
555 if (err != noErr) | |
556 { | |
557 ao_msg(MSGT_AO, MSGL_WARN, "failed to set mixmode: [%4.4s]\n", (char *)&err); | |
558 return CONTROL_FALSE; | |
559 } | |
560 | |
561 /* Get a list of all the streams on this device. */ | |
562 err = AudioDeviceGetPropertyInfo(ao->i_selected_dev, 0, FALSE, | |
563 kAudioDevicePropertyStreams, | |
564 &i_param_size, NULL); | |
565 if (err != noErr) | |
566 { | |
567 ao_msg(MSGT_AO, MSGL_WARN, "could not get number of streams: [%4.4s]\n", (char *)&err); | |
568 return CONTROL_FALSE; | |
569 } | |
570 | |
571 i_streams = i_param_size / sizeof(AudioStreamID); | |
572 p_streams = (AudioStreamID *)malloc(i_param_size); | |
573 if (p_streams == NULL) | |
574 { | |
575 ao_msg(MSGT_AO, MSGL_WARN, "out of memory\n" ); | |
576 return CONTROL_FALSE; | |
577 } | |
578 | |
579 err = AudioDeviceGetProperty(ao->i_selected_dev, 0, FALSE, | |
580 kAudioDevicePropertyStreams, | |
581 &i_param_size, p_streams); | |
582 if (err != noErr) | |
583 { | |
584 ao_msg(MSGT_AO, MSGL_WARN, "could not get number of streams: [%4.4s]\n", (char *)&err); | |
585 if (p_streams) free(p_streams); | |
586 return CONTROL_FALSE; | |
587 } | |
588 | |
589 ao_msg(MSGT_AO, MSGL_V, "current device stream number: %d\n", i_streams); | |
590 | |
591 for (i = 0; i < i_streams && ao->i_stream_index < 0; ++i) | |
592 { | |
593 /* Find a stream with a cac3 stream. */ | |
594 AudioStreamBasicDescription *p_format_list = NULL; | |
595 int i_formats = 0, j = 0, b_digital = 0; | |
596 | |
597 /* Retrieve all the stream formats supported by each output stream. */ | |
598 err = AudioStreamGetPropertyInfo(p_streams[i], 0, | |
599 kAudioStreamPropertyPhysicalFormats, | |
600 &i_param_size, NULL); | |
601 if (err != noErr) | |
602 { | |
603 ao_msg(MSGT_AO, MSGL_WARN, "could not get number of streamformats: [%4.4s]\n", (char *)&err); | |
604 continue; | |
605 } | |
606 | |
607 i_formats = i_param_size / sizeof(AudioStreamBasicDescription); | |
608 p_format_list = (AudioStreamBasicDescription *)malloc(i_param_size); | |
609 if (p_format_list == NULL) | |
610 { | |
611 ao_msg(MSGT_AO, MSGL_WARN, "could not malloc the memory\n" ); | |
612 continue; | |
613 } | |
614 | |
615 err = AudioStreamGetProperty(p_streams[i], 0, | |
616 kAudioStreamPropertyPhysicalFormats, | |
617 &i_param_size, p_format_list); | |
618 if (err != noErr) | |
619 { | |
620 ao_msg(MSGT_AO, MSGL_WARN, "could not get the list of streamformats: [%4.4s]\n", (char *)&err); | |
621 if (p_format_list) free(p_format_list); | |
622 continue; | |
623 } | |
624 | |
625 /* Check if one of the supported formats is a digital format. */ | |
626 for (j = 0; j < i_formats; ++j) | |
627 { | |
628 if (p_format_list[j].mFormatID == 'IAC3' || | |
629 p_format_list[j].mFormatID == kAudioFormat60958AC3) | |
630 { | |
631 b_digital = 1; | |
632 break; | |
633 } | |
634 } | |
635 | |
636 if (b_digital) | |
637 { | |
638 /* If this stream supports a digital (cac3) format, then set it. */ | |
639 int i_requested_rate_format = -1; | |
640 int i_current_rate_format = -1; | |
641 int i_backup_rate_format = -1; | |
642 | |
643 ao->i_stream_id = p_streams[i]; | |
644 ao->i_stream_index = i; | |
645 | |
646 if (ao->b_revert == 0) | |
647 { | |
648 /* Retrieve the original format of this stream first if not done so already. */ | |
649 i_param_size = sizeof(ao->sfmt_revert); | |
650 err = AudioStreamGetProperty(ao->i_stream_id, 0, | |
651 kAudioStreamPropertyPhysicalFormat, | |
652 &i_param_size, | |
653 &ao->sfmt_revert); | |
654 if (err != noErr) | |
655 { | |
656 ao_msg(MSGT_AO, MSGL_WARN, "could not retrieve the original streamformat: [%4.4s]\n", (char *)&err); | |
657 if (p_format_list) free(p_format_list); | |
658 continue; | |
659 } | |
660 ao->b_revert = 1; | |
661 } | |
662 | |
663 for (j = 0; j < i_formats; ++j) | |
664 if (p_format_list[j].mFormatID == 'IAC3' || | |
665 p_format_list[j].mFormatID == kAudioFormat60958AC3) | |
666 { | |
667 if (p_format_list[j].mSampleRate == ao->stream_format.mSampleRate) | |
668 { | |
669 i_requested_rate_format = j; | |
670 break; | |
671 } | |
672 if (p_format_list[j].mSampleRate == ao->sfmt_revert.mSampleRate) | |
673 i_current_rate_format = j; | |
674 else if (i_backup_rate_format < 0 || p_format_list[j].mSampleRate > p_format_list[i_backup_rate_format].mSampleRate) | |
675 i_backup_rate_format = j; | |
676 } | |
677 | |
678 if (i_requested_rate_format >= 0) /* We prefer to output at the samplerate of the original audio. */ | |
679 ao->stream_format = p_format_list[i_requested_rate_format]; | |
680 else if (i_current_rate_format >= 0) /* If not possible, we will try to use the current samplerate of the device. */ | |
681 ao->stream_format = p_format_list[i_current_rate_format]; | |
682 else ao->stream_format = p_format_list[i_backup_rate_format]; /* And if we have to, any digital format will be just fine (highest rate possible). */ | |
683 } | |
684 if (p_format_list) free(p_format_list); | |
685 } | |
686 if (p_streams) free(p_streams); | |
687 | |
688 if (ao->i_stream_index < 0) | |
689 { | |
690 ao_msg(MSGT_AO, MSGL_WARN, "can not find any digital output stream format when OpenSPDIF().\n"); | |
691 return CONTROL_FALSE; | |
692 } | |
693 | |
694 print_format(MSGL_V, "original stream format:", &ao->sfmt_revert); | |
695 | |
696 if (!AudioStreamChangeFormat(ao->i_stream_id, ao->stream_format)) | |
697 return CONTROL_FALSE; | |
698 | |
699 err = AudioDeviceAddPropertyListener(ao->i_selected_dev, | |
700 kAudioPropertyWildcardChannel, | |
701 0, | |
702 kAudioDevicePropertyDeviceHasChanged, | |
703 DeviceListener, | |
704 NULL); | |
705 if (err != noErr) | |
706 ao_msg(MSGT_AO, MSGL_WARN, "AudioDeviceAddPropertyListener for kAudioDevicePropertyDeviceHasChanged failed: [%4.4s]\n", (char *)&err); | |
707 | |
708 | |
709 /* FIXME: If output stream is not native byte-order, we need change endian somewhere. */ | |
710 /* Although there's no such case reported. */ | |
711 #ifdef WORDS_BIGENDIAN | |
712 if (!(ao->stream_format.mFormatFlags & kAudioFormatFlagIsBigEndian)) | |
713 #else | |
714 if (ao->stream_format.mFormatFlags & kAudioFormatFlagIsBigEndian) | |
715 #endif | |
24741
5ce7bc88eb0c
bugfix for ao_macosx last dts passthrough patch, patch by Ulion, ulion2002 gmail com
nplourde
parents:
24734
diff
changeset
|
716 ao_msg(MSGT_AO, MSGL_WARN, "output stream has a no-native byte-order, digital output may failed.\n"); |
24734 | 717 |
718 /* For ac3/dts, just use packet size 6144 bytes as chunk size. */ | |
719 ao->chunk_size = ao->stream_format.mBytesPerPacket; | |
720 | |
721 ao_data.samplerate = ao->stream_format.mSampleRate; | |
722 ao_data.channels = ao->stream_format.mChannelsPerFrame; | |
723 ao_data.bps = ao_data.samplerate * (ao->stream_format.mBytesPerPacket/ao->stream_format.mFramesPerPacket); | |
724 ao_data.outburst = ao->chunk_size; | |
725 ao_data.buffersize = ao_data.bps; | |
726 | |
727 ao->num_chunks = (ao_data.bps+ao->chunk_size-1)/ao->chunk_size; | |
728 ao->buffer_len = (ao->num_chunks + 1) * ao->chunk_size; | |
729 ao->buffer = NULL!=ao->buffer ? realloc(ao->buffer,(ao->num_chunks + 1)*ao->chunk_size) | |
730 : calloc(ao->num_chunks + 1, ao->chunk_size); | |
731 | |
732 ao_msg(MSGT_AO,MSGL_V, "using %5d chunks of %d bytes (buffer len %d bytes)\n", (int)ao->num_chunks, (int)ao->chunk_size, (int)ao->buffer_len); | |
733 | |
734 | |
735 /* Add IOProc callback. */ | |
736 err = AudioDeviceAddIOProc(ao->i_selected_dev, | |
737 (AudioDeviceIOProc)RenderCallbackSPDIF, | |
738 (void *)ao); | |
739 if (err != noErr) | |
740 { | |
741 ao_msg(MSGT_AO, MSGL_WARN, "AudioDeviceAddIOProc failed: [%4.4s]\n", (char *)&err); | |
742 return CONTROL_FALSE; | |
743 } | |
744 | |
745 reset(); | |
746 | |
747 return CONTROL_TRUE; | |
748 } | |
749 | |
750 /***************************************************************************** | |
751 * AudioDeviceSupportsDigital: Check i_dev_id for digital stream support. | |
752 *****************************************************************************/ | |
753 static int AudioDeviceSupportsDigital( AudioDeviceID i_dev_id ) | |
754 { | |
755 OSStatus err = noErr; | |
756 UInt32 i_param_size = 0; | |
757 AudioStreamID *p_streams = NULL; | |
758 int i = 0, i_streams = 0; | |
759 int b_return = CONTROL_FALSE; | |
760 | |
761 /* Retrieve all the output streams. */ | |
762 err = AudioDeviceGetPropertyInfo(i_dev_id, 0, FALSE, | |
763 kAudioDevicePropertyStreams, | |
764 &i_param_size, NULL); | |
765 if (err != noErr) | |
766 { | |
767 ao_msg(MSGT_AO,MSGL_V, "could not get number of streams: [%4.4s]\n", (char *)&err); | |
768 return CONTROL_FALSE; | |
769 } | |
770 | |
771 i_streams = i_param_size / sizeof(AudioStreamID); | |
772 p_streams = (AudioStreamID *)malloc(i_param_size); | |
773 if (p_streams == NULL) | |
774 { | |
775 ao_msg(MSGT_AO,MSGL_V, "out of memory\n"); | |
776 return CONTROL_FALSE; | |
777 } | |
778 | |
779 err = AudioDeviceGetProperty(i_dev_id, 0, FALSE, | |
780 kAudioDevicePropertyStreams, | |
781 &i_param_size, p_streams); | |
782 | |
783 if (err != noErr) | |
784 { | |
785 ao_msg(MSGT_AO,MSGL_V, "could not get number of streams: [%4.4s]\n", (char *)&err); | |
786 free(p_streams); | |
787 return CONTROL_FALSE; | |
788 } | |
789 | |
790 for (i = 0; i < i_streams; ++i) | |
791 { | |
792 if (AudioStreamSupportsDigital(p_streams[i])) | |
793 b_return = CONTROL_OK; | |
794 } | |
795 | |
796 free(p_streams); | |
797 return b_return; | |
798 } | |
799 | |
800 /***************************************************************************** | |
801 * AudioStreamSupportsDigital: Check i_stream_id for digital stream support. | |
802 *****************************************************************************/ | |
803 static int AudioStreamSupportsDigital( AudioStreamID i_stream_id ) | |
804 { | |
805 OSStatus err = noErr; | |
806 UInt32 i_param_size; | |
807 AudioStreamBasicDescription *p_format_list = NULL; | |
808 int i, i_formats, b_return = CONTROL_FALSE; | |
809 | |
810 /* Retrieve all the stream formats supported by each output stream. */ | |
811 err = AudioStreamGetPropertyInfo(i_stream_id, 0, | |
812 kAudioStreamPropertyPhysicalFormats, | |
813 &i_param_size, NULL); | |
814 if (err != noErr) | |
815 { | |
816 ao_msg(MSGT_AO,MSGL_V, "could not get number of streamformats: [%4.4s]\n", (char *)&err); | |
817 return CONTROL_FALSE; | |
818 } | |
819 | |
820 i_formats = i_param_size / sizeof(AudioStreamBasicDescription); | |
821 p_format_list = (AudioStreamBasicDescription *)malloc(i_param_size); | |
822 if (p_format_list == NULL) | |
823 { | |
824 ao_msg(MSGT_AO,MSGL_V, "could not malloc the memory\n" ); | |
825 return CONTROL_FALSE; | |
826 } | |
827 | |
828 err = AudioStreamGetProperty(i_stream_id, 0, | |
829 kAudioStreamPropertyPhysicalFormats, | |
830 &i_param_size, p_format_list); | |
831 if (err != noErr) | |
832 { | |
833 ao_msg(MSGT_AO,MSGL_V, "could not get the list of streamformats: [%4.4s]\n", (char *)&err); | |
834 free(p_format_list); | |
835 return CONTROL_FALSE; | |
836 } | |
837 | |
838 for (i = 0; i < i_formats; ++i) | |
839 { | |
840 print_format(MSGL_V, "supported format:", &p_format_list[i]); | |
841 | |
842 if (p_format_list[i].mFormatID == 'IAC3' || | |
843 p_format_list[i].mFormatID == kAudioFormat60958AC3) | |
844 b_return = CONTROL_OK; | |
845 } | |
846 | |
847 free(p_format_list); | |
848 return b_return; | |
849 } | |
850 | |
851 /***************************************************************************** | |
852 * AudioStreamChangeFormat: Change i_stream_id to change_format | |
853 *****************************************************************************/ | |
854 static int AudioStreamChangeFormat( AudioStreamID i_stream_id, AudioStreamBasicDescription change_format ) | |
855 { | |
856 OSStatus err = noErr; | |
857 UInt32 i_param_size = 0; | |
858 int i; | |
859 | |
860 struct timeval now; | |
861 struct timespec timeout; | |
862 struct { pthread_mutex_t lock; pthread_cond_t cond; } w; | |
863 | |
864 print_format(MSGL_V, "setting stream format:", &change_format); | |
865 | |
866 /* Condition because SetProperty is asynchronious. */ | |
867 pthread_cond_init(&w.cond, NULL); | |
868 pthread_mutex_init(&w.lock, NULL); | |
869 pthread_mutex_lock(&w.lock); | |
870 | |
871 /* Install the callback. */ | |
872 err = AudioStreamAddPropertyListener(i_stream_id, 0, | |
873 kAudioStreamPropertyPhysicalFormat, | |
874 StreamListener, (void *)&w); | |
875 if (err != noErr) | |
876 { | |
877 ao_msg(MSGT_AO, MSGL_WARN, "AudioStreamAddPropertyListener failed: [%4.4s]\n", (char *)&err); | |
878 return CONTROL_FALSE; | |
879 } | |
880 | |
881 /* Change the format. */ | |
882 err = AudioStreamSetProperty(i_stream_id, 0, 0, | |
883 kAudioStreamPropertyPhysicalFormat, | |
884 sizeof(AudioStreamBasicDescription), | |
885 &change_format); | |
886 if (err != noErr) | |
887 { | |
888 ao_msg(MSGT_AO, MSGL_WARN, "could not set the stream format: [%4.4s]\n", (char *)&err); | |
889 return CONTROL_FALSE; | |
890 } | |
891 | |
892 /* The AudioStreamSetProperty is not only asynchronious (requiring the locks), | |
893 * it is also not Atomic, in its behaviour. | |
894 * Therefore we check 5 times before we really give up. | |
895 * FIXME: failing isn't actually implemented yet. */ | |
896 for (i = 0; i < 5; ++i) | |
897 { | |
898 AudioStreamBasicDescription actual_format; | |
899 | |
900 gettimeofday(&now, NULL); | |
901 timeout.tv_sec = now.tv_sec; | |
902 timeout.tv_nsec = (now.tv_usec + 500000) * 1000; | |
903 | |
904 if (pthread_cond_timedwait(&w.cond, &w.lock, &timeout)) | |
905 ao_msg(MSGT_AO, MSGL_V, "reached timeout\n" ); | |
906 | |
907 i_param_size = sizeof(AudioStreamBasicDescription); | |
908 err = AudioStreamGetProperty(i_stream_id, 0, | |
909 kAudioStreamPropertyPhysicalFormat, | |
910 &i_param_size, | |
911 &actual_format); | |
912 | |
913 print_format(MSGL_V, "actual format in use:", &actual_format); | |
914 if (actual_format.mSampleRate == change_format.mSampleRate && | |
915 actual_format.mFormatID == change_format.mFormatID && | |
916 actual_format.mFramesPerPacket == change_format.mFramesPerPacket) | |
917 { | |
918 /* The right format is now active. */ | |
919 break; | |
920 } | |
921 /* We need to check again. */ | |
922 } | |
923 | |
924 /* Removing the property listener. */ | |
925 err = AudioStreamRemovePropertyListener(i_stream_id, 0, | |
926 kAudioStreamPropertyPhysicalFormat, | |
927 StreamListener); | |
928 if (err != noErr) | |
929 { | |
930 ao_msg(MSGT_AO, MSGL_WARN, "AudioStreamRemovePropertyListener failed: [%4.4s]\n", (char *)&err); | |
931 return CONTROL_FALSE; | |
932 } | |
933 | |
934 /* Destroy the lock and condition. */ | |
935 pthread_mutex_unlock(&w.lock); | |
936 pthread_mutex_destroy(&w.lock); | |
937 pthread_cond_destroy(&w.cond); | |
938 | |
939 return CONTROL_TRUE; | |
940 } | |
941 | |
942 /***************************************************************************** | |
943 * RenderCallbackSPDIF: callback for SPDIF audio output | |
944 *****************************************************************************/ | |
945 static OSStatus RenderCallbackSPDIF( AudioDeviceID inDevice, | |
946 const AudioTimeStamp * inNow, | |
947 const void * inInputData, | |
948 const AudioTimeStamp * inInputTime, | |
949 AudioBufferList * outOutputData, | |
950 const AudioTimeStamp * inOutputTime, | |
951 void * threadGlobals ) | |
952 { | |
953 int amt = buf_used(); | |
954 int req = outOutputData->mBuffers[ao->i_stream_index].mDataByteSize; | |
955 | |
956 if (amt > req) | |
957 amt = req; | |
958 if (amt) | |
959 read_buffer((unsigned char *)outOutputData->mBuffers[ao->i_stream_index].mData, amt); | |
960 | |
961 return noErr; | |
962 } | |
963 | |
10147
f2725d6717bd
Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff
changeset
|
964 |
f2725d6717bd
Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff
changeset
|
965 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
|
966 { |
24734 | 967 int wrote, b_digital; |
968 | |
969 // Check whether we need to reset the digital output stream. | |
970 if (ao->b_digital && ao->b_stream_format_changed) | |
971 { | |
972 ao->b_stream_format_changed = 0; | |
973 b_digital = AudioStreamSupportsDigital(ao->i_stream_id); | |
974 if (b_digital) | |
975 { | |
976 /* Current stream support digital format output, let's set it. */ | |
977 ao_msg(MSGT_AO, MSGL_V, "detected current stream support digital, try to restore digital output...\n"); | |
19427
ada359817b61
fix buffering issues with short audio samples on macosx. patch by Chris Roccati <roccati@pobox.com>
nplourde
parents:
18915
diff
changeset
|
978 |
24734 | 979 if (!AudioStreamChangeFormat(ao->i_stream_id, ao->stream_format)) |
980 { | |
981 ao_msg(MSGT_AO, MSGL_WARN, "restore digital output failed.\n"); | |
982 } | |
983 else | |
984 { | |
985 ao_msg(MSGT_AO, MSGL_WARN, "restore digital output succeed.\n"); | |
986 reset(); | |
987 } | |
988 } | |
989 else | |
990 ao_msg(MSGT_AO, MSGL_V, "detected current stream do not support digital.\n"); | |
991 } | |
992 | |
993 wrote=write_buffer(output_samples, num_bytes); | |
994 audio_resume(); | |
995 return wrote; | |
10147
f2725d6717bd
Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff
changeset
|
996 } |
f2725d6717bd
Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff
changeset
|
997 |
f2725d6717bd
Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff
changeset
|
998 /* set variables and buffer to initial state */ |
18915
99e20a22d5d0
modifies function declarations without parameters from ()
reynaldo
parents:
18885
diff
changeset
|
999 static void reset(void) |
10147
f2725d6717bd
Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff
changeset
|
1000 { |
14769
e701873aa8ca
rids ao_macosx of the buffer mutex by using the same buffering scheme as ao_sdl - Patch by Reimar Doffinger
nplourde
parents:
14642
diff
changeset
|
1001 audio_pause(); |
10147
f2725d6717bd
Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff
changeset
|
1002 /* reset ring-buffer state */ |
f2725d6717bd
Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff
changeset
|
1003 ao->buf_read_pos=0; |
f2725d6717bd
Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff
changeset
|
1004 ao->buf_write_pos=0; |
f2725d6717bd
Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff
changeset
|
1005 |
f2725d6717bd
Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff
changeset
|
1006 return; |
f2725d6717bd
Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff
changeset
|
1007 } |
f2725d6717bd
Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff
changeset
|
1008 |
f2725d6717bd
Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff
changeset
|
1009 |
f2725d6717bd
Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff
changeset
|
1010 /* return available space */ |
18915
99e20a22d5d0
modifies function declarations without parameters from ()
reynaldo
parents:
18885
diff
changeset
|
1011 static int get_space(void) |
10147
f2725d6717bd
Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff
changeset
|
1012 { |
14769
e701873aa8ca
rids ao_macosx of the buffer mutex by using the same buffering scheme as ao_sdl - Patch by Reimar Doffinger
nplourde
parents:
14642
diff
changeset
|
1013 return buf_free(); |
10147
f2725d6717bd
Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff
changeset
|
1014 } |
f2725d6717bd
Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff
changeset
|
1015 |
f2725d6717bd
Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff
changeset
|
1016 |
10152 | 1017 /* return delay until audio is played */ |
18915
99e20a22d5d0
modifies function declarations without parameters from ()
reynaldo
parents:
18885
diff
changeset
|
1018 static float get_delay(void) |
10147
f2725d6717bd
Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff
changeset
|
1019 { |
14769
e701873aa8ca
rids ao_macosx of the buffer mutex by using the same buffering scheme as ao_sdl - Patch by Reimar Doffinger
nplourde
parents:
14642
diff
changeset
|
1020 int buffered = ao->buffer_len - ao->chunk_size - buf_free(); // could be less |
e701873aa8ca
rids ao_macosx of the buffer mutex by using the same buffering scheme as ao_sdl - Patch by Reimar Doffinger
nplourde
parents:
14642
diff
changeset
|
1021 // inaccurate, should also contain the data buffered e.g. by the OS |
e701873aa8ca
rids ao_macosx of the buffer mutex by using the same buffering scheme as ao_sdl - Patch by Reimar Doffinger
nplourde
parents:
14642
diff
changeset
|
1022 return (float)(buffered)/(float)ao_data.bps; |
10147
f2725d6717bd
Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff
changeset
|
1023 } |
f2725d6717bd
Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff
changeset
|
1024 |
f2725d6717bd
Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff
changeset
|
1025 |
f2725d6717bd
Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff
changeset
|
1026 /* unload plugin and deregister from coreaudio */ |
12145 | 1027 static void uninit(int immed) |
10147
f2725d6717bd
Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff
changeset
|
1028 { |
24734 | 1029 OSStatus err = noErr; |
1030 UInt32 i_param_size = 0; | |
10147
f2725d6717bd
Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff
changeset
|
1031 |
19427
ada359817b61
fix buffering issues with short audio samples on macosx. patch by Chris Roccati <roccati@pobox.com>
nplourde
parents:
18915
diff
changeset
|
1032 if (!immed) { |
ada359817b61
fix buffering issues with short audio samples on macosx. patch by Chris Roccati <roccati@pobox.com>
nplourde
parents:
18915
diff
changeset
|
1033 long long timeleft=(1000000LL*buf_used())/ao_data.bps; |
24734 | 1034 ao_msg(MSGT_AO,MSGL_DBG2, "%d bytes left @%d bps (%d usec)\n", buf_used(), ao_data.bps, (int)timeleft); |
19427
ada359817b61
fix buffering issues with short audio samples on macosx. patch by Chris Roccati <roccati@pobox.com>
nplourde
parents:
18915
diff
changeset
|
1035 usec_sleep((int)timeleft); |
ada359817b61
fix buffering issues with short audio samples on macosx. patch by Chris Roccati <roccati@pobox.com>
nplourde
parents:
18915
diff
changeset
|
1036 } |
10147
f2725d6717bd
Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff
changeset
|
1037 |
24734 | 1038 if (!ao->b_digital) { |
1039 AudioOutputUnitStop(ao->theOutputUnit); | |
1040 AudioUnitUninitialize(ao->theOutputUnit); | |
1041 CloseComponent(ao->theOutputUnit); | |
1042 } | |
1043 else { | |
1044 /* Stop device. */ | |
1045 err = AudioDeviceStop(ao->i_selected_dev, | |
1046 (AudioDeviceIOProc)RenderCallbackSPDIF); | |
1047 if (err != noErr) | |
1048 ao_msg(MSGT_AO, MSGL_WARN, "AudioDeviceStop failed: [%4.4s]\n", (char *)&err); | |
1049 | |
1050 /* Remove IOProc callback. */ | |
1051 err = AudioDeviceRemoveIOProc(ao->i_selected_dev, | |
1052 (AudioDeviceIOProc)RenderCallbackSPDIF); | |
1053 if (err != noErr) | |
1054 ao_msg(MSGT_AO, MSGL_WARN, "AudioDeviceRemoveIOProc failed: [%4.4s]\n", (char *)&err); | |
1055 | |
1056 if (ao->b_revert) | |
1057 AudioStreamChangeFormat(ao->i_stream_id, ao->sfmt_revert); | |
1058 | |
1059 if (ao->b_changed_mixing && ao->sfmt_revert.mFormatID != kAudioFormat60958AC3) | |
1060 { | |
1061 int b_mix; | |
1062 Boolean b_writeable; | |
1063 /* Revert mixable to true if we are allowed to. */ | |
1064 err = AudioDeviceGetPropertyInfo(ao->i_selected_dev, 0, FALSE, kAudioDevicePropertySupportsMixing, | |
1065 &i_param_size, &b_writeable); | |
1066 err = AudioDeviceGetProperty(ao->i_selected_dev, 0, FALSE, kAudioDevicePropertySupportsMixing, | |
1067 &i_param_size, &b_mix); | |
1068 if (err != noErr && b_writeable) | |
1069 { | |
1070 b_mix = 1; | |
1071 err = AudioDeviceSetProperty(ao->i_selected_dev, 0, 0, FALSE, | |
1072 kAudioDevicePropertySupportsMixing, i_param_size, &b_mix); | |
1073 } | |
1074 if (err != noErr) | |
1075 ao_msg(MSGT_AO, MSGL_WARN, "failed to set mixmode: [%4.4s]\n", (char *)&err); | |
1076 } | |
1077 if (ao->i_hog_pid == getpid()) | |
1078 { | |
1079 ao->i_hog_pid = -1; | |
1080 i_param_size = sizeof(ao->i_hog_pid); | |
1081 err = AudioDeviceSetProperty(ao->i_selected_dev, 0, 0, FALSE, | |
1082 kAudioDevicePropertyHogMode, i_param_size, &ao->i_hog_pid); | |
1083 if (err != noErr) ao_msg(MSGT_AO, MSGL_WARN, "Could not release hogmode: [%4.4s]\n", (char *)&err); | |
1084 } | |
1085 } | |
10147
f2725d6717bd
Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff
changeset
|
1086 |
14769
e701873aa8ca
rids ao_macosx of the buffer mutex by using the same buffering scheme as ao_sdl - Patch by Reimar Doffinger
nplourde
parents:
14642
diff
changeset
|
1087 free(ao->buffer); |
10147
f2725d6717bd
Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff
changeset
|
1088 free(ao); |
15853
6e994905f77b
more general ao_macosx cleanup. Patch by Alexander Strange <astrange@ithinksw.com>
nplourde
parents:
15722
diff
changeset
|
1089 ao = NULL; |
10147
f2725d6717bd
Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff
changeset
|
1090 } |
f2725d6717bd
Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff
changeset
|
1091 |
f2725d6717bd
Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff
changeset
|
1092 |
f2725d6717bd
Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff
changeset
|
1093 /* stop playing, keep buffers (for pause) */ |
18915
99e20a22d5d0
modifies function declarations without parameters from ()
reynaldo
parents:
18885
diff
changeset
|
1094 static void audio_pause(void) |
10147
f2725d6717bd
Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff
changeset
|
1095 { |
24734 | 1096 OSErr err=noErr; |
10147
f2725d6717bd
Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff
changeset
|
1097 |
24734 | 1098 /* Stop callback. */ |
1099 if (!ao->b_digital) | |
1100 { | |
1101 err=AudioOutputUnitStop(ao->theOutputUnit); | |
1102 if (err != noErr) | |
1103 ao_msg(MSGT_AO,MSGL_WARN, "AudioOutputUnitStop returned [%4.4s]\n", (char *)&err); | |
1104 } | |
1105 else | |
1106 { | |
1107 err = AudioDeviceStop(ao->i_selected_dev, (AudioDeviceIOProc)RenderCallbackSPDIF); | |
1108 if (err != noErr) | |
1109 ao_msg(MSGT_AO, MSGL_WARN, "AudioDeviceStop failed: [%4.4s]\n", (char *)&err); | |
1110 } | |
1111 ao->paused = 1; | |
10147
f2725d6717bd
Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff
changeset
|
1112 } |
f2725d6717bd
Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff
changeset
|
1113 |
f2725d6717bd
Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff
changeset
|
1114 |
f2725d6717bd
Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff
changeset
|
1115 /* resume playing, after audio_pause() */ |
18915
99e20a22d5d0
modifies function declarations without parameters from ()
reynaldo
parents:
18885
diff
changeset
|
1116 static void audio_resume(void) |
10147
f2725d6717bd
Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff
changeset
|
1117 { |
24734 | 1118 OSErr err=noErr; |
1119 | |
1120 if (!ao->paused) | |
1121 return; | |
1122 | |
1123 /* Start callback. */ | |
1124 if (!ao->b_digital) | |
1125 { | |
1126 err = AudioOutputUnitStart(ao->theOutputUnit); | |
1127 if (err != noErr) | |
1128 ao_msg(MSGT_AO,MSGL_WARN, "AudioOutputUnitStart returned [%4.4s]\n", (char *)&err); | |
1129 } | |
1130 else | |
1131 { | |
1132 err = AudioDeviceStart(ao->i_selected_dev, (AudioDeviceIOProc)RenderCallbackSPDIF); | |
1133 if (err != noErr) | |
1134 ao_msg(MSGT_AO, MSGL_WARN, "AudioDeviceStart failed: [%4.4s]\n", (char *)&err); | |
1135 } | |
1136 ao->paused = 0; | |
10147
f2725d6717bd
Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
alex
parents:
diff
changeset
|
1137 } |
24734 | 1138 |
1139 /***************************************************************************** | |
1140 * StreamListener | |
1141 *****************************************************************************/ | |
1142 static OSStatus StreamListener( AudioStreamID inStream, | |
1143 UInt32 inChannel, | |
1144 AudioDevicePropertyID inPropertyID, | |
1145 void * inClientData ) | |
1146 { | |
1147 struct { pthread_mutex_t lock; pthread_cond_t cond; } * w = inClientData; | |
1148 | |
1149 switch (inPropertyID) | |
1150 { | |
1151 case kAudioStreamPropertyPhysicalFormat: | |
1152 if (NULL!=w) | |
1153 { | |
1154 pthread_mutex_lock(&w->lock); | |
1155 pthread_cond_signal(&w->cond); | |
1156 pthread_mutex_unlock(&w->lock); | |
1157 } | |
1158 default: | |
1159 break; | |
1160 } | |
1161 return noErr; | |
1162 } | |
1163 | |
1164 static OSStatus DeviceListener( AudioDeviceID inDevice, | |
1165 UInt32 inChannel, | |
1166 Boolean isInput, | |
1167 AudioDevicePropertyID inPropertyID, | |
1168 void* inClientData ) | |
1169 { | |
1170 switch (inPropertyID) | |
1171 { | |
1172 case kAudioDevicePropertyDeviceHasChanged: | |
1173 ao_msg(MSGT_AO, MSGL_WARN, "got notify kAudioDevicePropertyDeviceHasChanged.\n"); | |
1174 ao->b_stream_format_changed = 1; | |
1175 default: | |
1176 break; | |
1177 } | |
1178 return noErr; | |
1179 } |