summaryrefslogtreecommitdiffstats
path: root/libao2/ao_macosx.c
blob: af8635ddfc3d59a6927306e7ead4dae784d81650 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/*
 *
 *  ao_macosx.c
 *
 *      Original Copyright (C) Timothy J. Wood - Aug 2000
 *
 *  This file is part of libao, a cross-platform library.  See
 *  README for a history of this source code.
 *
 *  libao is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2, or (at your option)
 *  any later version.
 *
 *  libao is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with GNU Make; see the file COPYING.  If not, write to
 *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
 */

/*
 * The MacOS X CoreAudio framework doesn't mesh as simply as some
 * simpler frameworks do.  This is due to the fact that CoreAudio pulls
 * audio samples rather than having them pushed at it (which is nice
 * when you are wanting to do good buffering of audio). 
 */

/* Change log:
 * 
 * 14/5-2003: Ported to MPlayer libao2 by Dan Christiansen
 *
 *            AC-3 and MPEG audio passthrough is possible, but I don't have
 *            access to a sound card that supports it.
 */

#include <CoreAudio/AudioHardware.h>
#include <stdio.h>
#include <string.h>
#include <inttypes.h>
#include <pthread.h>

#include "../mp_msg.h"

#include "audio_out.h"
#include "audio_out_internal.h"
#include "afmt.h"

static ao_info_t info =
  {
    "Darwin/Mac OS X native audio output",
    "macosx",
    "Timothy J. Wood & Dan Christiansen",
    ""
  };

LIBAO_EXTERN(macosx)

/* Prefix for all mp_msg() calls */
#define ao_msg(a, b, c...) mp_msg(a, b, "AO: [macosx] " c)

/* This is large, but best (maybe it should be even larger).
 * CoreAudio supposedly has an internal latency in the order of 2ms */
#define NUM_BUFS 128

typedef struct ao_macosx_s
{
  /* CoreAudio */
  AudioDeviceID outputDeviceID;
  AudioStreamBasicDescription outputStreamBasicDescription;

  /* Ring-buffer */
  pthread_mutex_t buffer_mutex; /* mutex covering buffer variables */

  unsigned char *buffer[NUM_BUFS];
  unsigned int buffer_len;
  
  unsigned int buf_read;
  unsigned int buf_write;
  unsigned int buf_read_pos;
  unsigned int buf_write_pos;
  int full_buffers;
  int buffered_bytes;
} ao_macosx_t;

static ao_macosx_t *ao;

/* General purpose Ring-buffering routines */
static int write_buffer(unsigned char* data,int len){
  int len2=0;
  int x;

  while(len>0){
    if(ao->full_buffers==NUM_BUFS) {
      ao_msg(MSGT_AO,MSGL_V, "Buffer overrun\n");
      break;
    }

    x=ao->buffer_len-ao->buf_write_pos;
    if(x>len) x=len;
    memcpy(ao->buffer[ao->buf_write]+ao->buf_write_pos,data+len2,x);

    /* accessing common variables, locking mutex */
    pthread_mutex_lock(&ao->buffer_mutex);
    len2+=x; len-=x;
    ao->buffered_bytes+=x; ao->buf_write_pos+=x;
    if(ao->buf_write_pos>=ao->buffer_len) {
      /* block is full, find next! */
      ao->buf_write=(ao->buf_write+1)%NUM_BUFS;
      ++ao->full_buffers;
      ao->buf_write_pos=0;
    }
    pthread_mutex_unlock(&ao->buffer_mutex);
  }

  return len2;
}

static int read_buffer(unsigned char* data,int len){
  int len2=0;
  int x;

  while(len>0){
    if(ao->full_buffers==0) {
      ao_msg(MSGT_AO,MSGL_V, "Buffer underrun\n");
      break;
    }

    x=ao->buffer_len-ao->buf_read_pos;
    if(x>len) x=len;
    memcpy(data+len2,ao->buffer[ao->buf_read]+ao->buf_read_pos,x);
    len2+=x; len-=x;

    /* accessing common variables, locking mutex */
    pthread_mutex_lock(&ao->buffer_mutex);
    ao->buffered_bytes-=x; ao->buf_read_pos+=x;
    if(ao->buf_read_pos>=ao->buffer_len){
      /* block is empty, find next! */
       ao->buf_read=(ao->buf_read+1)%NUM_BUFS;
       --ao->full_buffers;
       ao->buf_read_pos=0;
    }
    pthread_mutex_unlock(&ao->buffer_mutex);
  }
  
  
  return len2;
}

/* end ring buffer stuff */

/* The function that the CoreAudio thread calls when it wants more data */
static OSStatus audioDeviceIOProc(AudioDeviceID inDevice, const AudioTimeStamp *inNow, const AudioBufferList *inInputData, const AudioTimeStamp *inInputTime, AudioBufferList *outOutputData, const AudioTimeStamp *inOutputTime, void *inClientData)
{
  outOutputData->mBuffers[0].mDataByteSize =
    read_buffer((char *)outOutputData->mBuffers[0].mData, ao->buffer_len);

  return 0;
}


static int control(int cmd,void *arg){
	switch (cmd) {
	case AOCONTROL_SET_DEVICE:
	case AOCONTROL_GET_DEVICE:
	case AOCONTROL_GET_VOLUME:
	case AOCONTROL_SET_VOLUME:
	  /* unimplemented/meaningless */
	  return CONTROL_FALSE;
	case AOCONTROL_QUERY_FORMAT:
	  /* stick with what CoreAudio requests */
	  return CONTROL_FALSE;
	default:
	  return CONTROL_FALSE;
	}
	
}


static int init(int rate,int channels,int format,int flags)
{
  OSStatus status;
  UInt32 propertySize;
  int rc;
  int i;

  ao = (ao_macosx_t *)malloc(sizeof(ao_macosx_t));

  /* initialise mutex */
  pthread_mutex_init(&ao->buffer_mutex, NULL);
  pthread_mutex_unlock(&ao->buffer_mutex);

  /* get default output device */ 
  propertySize = sizeof(ao->outputDeviceID);
  status = AudioHardwareGetProperty(kAudioHardwarePropertyDefaultOutputDevice, &propertySize, &(ao->outputDeviceID));
  if (status) {
        ao_msg(MSGT_AO,MSGL_WARN, 
		"AudioHardwareGetProperty returned %d\n",
		(int)status);
	return CONTROL_FALSE;
    }
    
    if (ao->outputDeviceID == kAudioDeviceUnknown) {
        ao_msg(MSGT_AO,MSGL_WARN, "AudioHardwareGetProperty: ao->outputDeviceID is kAudioDeviceUnknown\n");
	return CONTROL_FALSE;
    }
    
    /* get default output format
     * TODO: get all support formats and iterate through them
     */
    propertySize = sizeof(ao->outputStreamBasicDescription);
    status = AudioDeviceGetProperty(ao->outputDeviceID, 0, false, kAudioDevicePropertyStreamFormat, &propertySize, &ao->outputStreamBasicDescription);
    if (status) {
        ao_msg(MSGT_AO,MSGL_WARN, "AudioDeviceGetProperty returned %d when getting kAudioDevicePropertyStreamFormat\n", (int)status);
	return CONTROL_FALSE;
    }

    ao_msg(MSGT_AO,MSGL_V, "hardware format...\n");
    ao_msg(MSGT_AO,MSGL_V, "%f mSampleRate\n", ao->outputStreamBasicDescription.mSampleRate);
    ao_msg(MSGT_AO,MSGL_V, " %c%c%c%c mFormatID\n", 
	    (int)(ao->outputStreamBasicDescription.mFormatID & 0xff000000) >> 24,
	    (int)(ao->outputStreamBasicDescription.mFormatID & 0x00ff0000) >> 16,
	    (int)(ao->outputStreamBasicDescription.mFormatID & 0x0000ff00) >>  8,
	    (int)(ao->outputStreamBasicDescription.mFormatID & 0x000000ff) >>  0);
    ao_msg(MSGT_AO,MSGL_V, "%5d mBytesPerPacket\n",
	    (int)ao->outputStreamBasicDescription.mBytesPerPacket);
    ao_msg(MSGT_AO,MSGL_V, "%5d mFramesPerPacket\n",
	    (int)ao->outputStreamBasicDescription.mFramesPerPacket);
    ao_msg(MSGT_AO,MSGL_V, "%5d mBytesPerFrame\n",
	    (int)ao->outputStreamBasicDescription.mBytesPerFrame);
    ao_msg(MSGT_AO,MSGL_V, "%5d mChannelsPerFrame\n",
	    (int)ao->outputStreamBasicDescription.mChannelsPerFrame);

    /* get requested buffer length */
    propertySize = sizeof(ao->buffer_len);
    status = AudioDeviceGetProperty(ao->outputDeviceID, 0, false, kAudioDevicePropertyBufferSize, &propertySize, &ao->buffer_len);
    if (status) {
        ao_msg(MSGT_AO,MSGL_WARN, "AudioDeviceGetProperty returned %d when getting kAudioDevicePropertyBufferSize\n", (int)status);
	return CONTROL_FALSE;
    }
    ao_msg(MSGT_AO,MSGL_V, "%5d ao->buffer_len\n", (int)ao->buffer_len);

    /* FIXME:
     *
     * Resampling of 32-bit float audio is broken in MPlayer. Refuse to 
     * handle anything other than the native format until this is fixed
     * or this module is rewritten, whichever comes first.
     */
    if (ao_data.samplerate == ao->outputStreamBasicDescription.mSampleRate) {
      ao_data.samplerate = (int)ao->outputStreamBasicDescription.mSampleRate;
    } else {
      ao_msg(MSGT_AO,MSGL_WARN, "Resampling not supported yet.\n");
      return 0;
    }

    ao_data.channels = ao->outputStreamBasicDescription.mChannelsPerFrame;
    ao_data.outburst = ao_data.buffersize = ao->buffer_len;
    ao_data.bps = 
      ao_data.samplerate * ao->outputStreamBasicDescription.mBytesPerFrame;

    if (ao->outputStreamBasicDescription.mFormatID == kAudioFormatLinearPCM) {
      uint32_t flags = ao->outputStreamBasicDescription.mFormatFlags;
      if (flags & kAudioFormatFlagIsFloat) {
	ao_data.format = AFMT_FLOAT;
      } else {
	ao_msg(MSGT_AO,MSGL_WARN, "Unsupported audio output "
	       "format %d. Please report this to the developer\n",
	       (int)status);
	return CONTROL_FALSE;
      }
      
    } else {
      /* TODO: handle AFMT_AC3, AFMT_MPEG & friends */
      ao_msg(MSGT_AO,MSGL_WARN, "Default Audio Device doesn't "
	     "support Linear PCM!\n");
      return CONTROL_FALSE;
    }
  
    /* Allocate ring-buffer memory */
    for(i=0;i<NUM_BUFS;i++) 
      ao->buffer[i]=(unsigned char *) malloc(ao->buffer_len);


    /* Prepare for playback */

    reset();
    
    /* Set the IO proc that CoreAudio will call when it needs data */
    status = AudioDeviceAddIOProc(ao->outputDeviceID, audioDeviceIOProc, NULL);
    if (status) {
        ao_msg(MSGT_AO,MSGL_WARN, "AudioDeviceAddIOProc returned %d\n", (int)status);
	return CONTROL_FALSE;
    }
 
    /* Start callback */
    status = AudioDeviceStart(ao->outputDeviceID, audioDeviceIOProc);
    if (status) {
      ao_msg(MSGT_AO,MSGL_WARN, "AudioDeviceStart returned %d\n",
	     (int)status);
      return CONTROL_FALSE;
    }
    
    return CONTROL_OK;
}


static int play(void* output_samples,int num_bytes,int flags)
{  
  return write_buffer(output_samples, num_bytes);
}

/* set variables and buffer to initial state */
static void reset()
{
  int i;
  
  pthread_mutex_lock(&ao->buffer_mutex);
  
  /* reset ring-buffer state */
  ao->buf_read=0;
  ao->buf_write=0;
  ao->buf_read_pos=0;
  ao->buf_write_pos=0;
  
  ao->full_buffers=0;
  ao->buffered_bytes=0;
  
  /* zero output buffer */
  for (i = 0; i < NUM_BUFS; i++)
    bzero(ao->buffer[i], ao->buffer_len);

  pthread_mutex_unlock(&ao->buffer_mutex);
       
  return;
}


/* return available space */
static int get_space()
{
  return (NUM_BUFS-ao->full_buffers)*ao_data.buffersize - ao->buf_write_pos;
}


/* return delay until audio is played */
static float get_delay()
{
  return (float)(ao->buffered_bytes)/(float)ao_data.bps;
}


/* unload plugin and deregister from coreaudio */
static void uninit()
{
  int i;
  OSErr status;

  reset();

  status = AudioDeviceRemoveIOProc(ao->outputDeviceID, audioDeviceIOProc);
  if (status)
    ao_msg(MSGT_AO,MSGL_WARN, "AudioDeviceRemoveIOProc "
	   "returned %d\n", (int)status);

  for(i=0;i<NUM_BUFS;i++) free(ao->buffer[i]);
  free(ao);
}


/* stop playing, keep buffers (for pause) */
static void audio_pause()
{
  OSErr status;

  /* stop callback */
  status = AudioDeviceStop(ao->outputDeviceID, audioDeviceIOProc);
  if (status)
    ao_msg(MSGT_AO,MSGL_WARN, "AudioDeviceStop returned %d\n",
	   (int)status);
}


/* resume playing, after audio_pause() */
static void audio_resume()
{
  OSErr status = AudioDeviceStart(ao->outputDeviceID, audioDeviceIOProc);
  if (status)
    ao_msg(MSGT_AO,MSGL_WARN, "AudioDeviceStart returned %d\n",
	   (int)status);
}