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