]> git.sesse.net Git - vlc/blobdiff - modules/audio_output/auhal.c
Merge fr and sv to 0.8.4
[vlc] / modules / audio_output / auhal.c
index 41f907955c86bde4f993a8062f87cc51a43c4bdd..f9d6fd532035cb54ff26026252a6f94f35947bc7 100644 (file)
@@ -1,8 +1,8 @@
 /*****************************************************************************
  * auhal.c: AUHAL output plugin
  *****************************************************************************
- * Copyright (C) 2005 VideoLAN
- * $Id: coreaudio.c 10101 2005-03-02 16:47:31Z robux4 $
+ * Copyright (C) 2005 the VideoLAN team
+ * $Id$
  *
  * Authors: Derk-Jan Hartman <hartman at videolan dot org>
  *
@@ -46,6 +46,7 @@
     sfm.mFramesPerPacket, sfm.mBytesPerFrame, \
     sfm.mChannelsPerFrame, sfm.mBitsPerChannel
 
+#define BUFSIZE 0xffffff
 
 /*****************************************************************************
  * aout_sys_t: private audio output method descriptor
@@ -63,6 +64,12 @@ struct aout_sys_t
     Component                   au_component;   /* The Audiocomponent we use */
     AudioUnit                   au_unit;        /* The AudioUnit we use */
     mtime_t                     clock_diff;
+    uint8_t                      p_remainder_buffer[BUFSIZE];
+    uint32_t                    i_read_bytes;
+    uint32_t                    i_total_bytes;
+    audio_date_t                end_date_t;
+    
+    
 };
 
 /*****************************************************************************
@@ -75,6 +82,7 @@ static void     Play                    ( aout_instance_t *);
 
 static int      Probe                   ( aout_instance_t * );
 static int      DeviceDigitalMode       ( aout_instance_t *, AudioDeviceID );
+int             AudioDeviceHasOutput    ( AudioDeviceID );
 static int      DigitalInit             ( aout_instance_t * );
 
 static OSStatus RenderCallbackAnalog    ( vlc_object_t *, AudioUnitRenderActionFlags *, const AudioTimeStamp *,
@@ -158,6 +166,7 @@ static int Open( vlc_object_t * p_this )
         return VLC_EGENERIC;
     }
     */
+    
     if( AOUT_FMT_NON_LINEAR( &p_aout->output.output ) && p_sys->b_supports_digital )
     {
         p_sys->b_digital = VLC_TRUE;
@@ -282,10 +291,10 @@ static int Open( vlc_object_t * p_this )
             case kAudioChannelLabel_RightSurround:
                 p_aout->output.output.i_physical_channels |= AOUT_CHAN_REARRIGHT;
                 continue;
-            case kAudioChannelLabel_LeftCenter:
+            case kAudioChannelLabel_RearSurroundLeft:
                 p_aout->output.output.i_physical_channels |= AOUT_CHAN_MIDDLELEFT;
                 continue;
-            case kAudioChannelLabel_RightCenter:
+            case kAudioChannelLabel_RearSurroundRight:
                 p_aout->output.output.i_physical_channels |= AOUT_CHAN_MIDDLERIGHT;
                 continue;
             case kAudioChannelLabel_CenterSurround:
@@ -293,7 +302,41 @@ static int Open( vlc_object_t * p_this )
                 continue;
             default:
                 msg_Warn( p_aout, "Unrecognized channel form provided by driver: %d", (int)layout->mChannelDescriptions[i].mChannelLabel );
-                p_aout->output.output.i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
+                if( i == 0 )
+                {
+                    msg_Warn( p_aout, "Probably no channellayout is set. force based on channelcount" );
+                    switch( layout->mNumberChannelDescriptions )
+                    {
+                        /* We make assumptions based on number of channels here.
+                         * Unfortunatly Apple has provided no 100% method to retrieve the speaker configuration */
+                        case 1:
+                            p_aout->output.output.i_physical_channels = AOUT_CHAN_CENTER;
+                            break;
+                        case 4:
+                            p_aout->output.output.i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
+                                                                        AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
+                            break;
+                        case 6:
+                            p_aout->output.output.i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
+                                                                        AOUT_CHAN_CENTER | AOUT_CHAN_LFE |
+                                                                        AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
+                            break;
+                        case 7:
+                            p_aout->output.output.i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
+                                                                        AOUT_CHAN_CENTER | AOUT_CHAN_LFE |
+                                                                        AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_REARCENTER;
+                            break;
+                        case 8:
+                            p_aout->output.output.i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
+                                                                        AOUT_CHAN_CENTER | AOUT_CHAN_LFE |
+                                                                        AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT |
+                                                                        AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
+                            break;
+                        case 2:
+                        default:
+                            p_aout->output.output.i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
+                    }
+                }
                 break;
         }
     }
@@ -301,6 +344,63 @@ static int Open( vlc_object_t * p_this )
 
     msg_Dbg( p_aout, "defined %d physical channels for vlc core", aout_FormatNbChannels( &p_aout->output.output ) );
     msg_Dbg( p_aout, "%s", aout_FormatPrintChannels( &p_aout->output.output ));
+    
+    AudioChannelLayout new_layout;
+    memset (&new_layout, 0, sizeof(new_layout));
+    switch( aout_FormatNbChannels( &p_aout->output.output ) )
+    {
+        case 1:
+            new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_Mono;
+            break;
+        case 2:
+            new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_Stereo;
+            break;
+        case 3:
+            if( p_aout->output.output.i_physical_channels & AOUT_CHAN_CENTER )
+            {
+                new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_DVD_7; // L R C
+            }
+            else if( p_aout->output.output.i_physical_channels & AOUT_CHAN_LFE )
+            {
+                new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_DVD_4; // L R LFE
+            }
+            break;
+        case 4:
+            if( p_aout->output.output.i_physical_channels & ( AOUT_CHAN_CENTER | AOUT_CHAN_LFE ) )
+            {
+                new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_DVD_10; // L R C LFE
+            }
+            else if( p_aout->output.output.i_physical_channels & ( AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT ) )
+            {
+                new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_DVD_3; // L R Ls Rs
+            }
+            else if( p_aout->output.output.i_physical_channels & ( AOUT_CHAN_CENTER | AOUT_CHAN_REARCENTER ) )
+            {
+                new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_DVD_3; // L R C Cs
+            }
+            break;
+        case 5:
+            if( p_aout->output.output.i_physical_channels & ( AOUT_CHAN_CENTER ) )
+            {
+                new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_DVD_19; // L R Ls Rs C
+            }
+            else if( p_aout->output.output.i_physical_channels & ( AOUT_CHAN_LFE ) )
+            {
+                new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_DVD_18; // L R Ls Rs LFE
+            }
+            break;
+        case 6:
+            new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_DVD_20; // L R Ls Rs C LFE
+            break;
+        case 7:
+            /* FIXME: This is incorrect. VLC uses the internal ordering: L R Lm Rm Lr Rr C LFE but this is wrong */
+            new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_MPEG_6_1_A; // L R C LFE Ls Rs Cs
+            break;
+        case 8:
+            /* FIXME: This is incorrect. VLC uses the internal ordering: L R Lm Rm Lr Rr C LFE but this is wrong */
+            new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_MPEG_7_1_A; // L R C LFE Ls Rs Lc Rc
+            break;
+    }
 
     /* Set up the format to be used */
     DeviceFormat.mSampleRate = p_aout->output.output.i_rate;
@@ -339,12 +439,16 @@ static int Open( vlc_object_t * p_this )
                                    
     msg_Dbg( p_aout, STREAM_FORMAT_MSG( "the actual set AU format is " , DeviceFormat ) );
 
+    p_aout->output.i_nb_samples = 2048;
     aout_VolumeSoftInit( p_aout );
 
     /* Let's pray for the following operation to be atomic... */
     p_sys->clock_diff = - (mtime_t)
         AudioConvertHostTimeToNanos( AudioGetCurrentHostTime() ) / 1000; 
     p_sys->clock_diff += mdate();
+    
+    p_sys->i_read_bytes = 0;
+    p_sys->i_total_bytes = 0;
 
     /* set the IOproc callback */
     AURenderCallbackStruct input;
@@ -359,6 +463,12 @@ static int Open( vlc_object_t * p_this )
     input.inputProc = (AURenderCallback) RenderCallbackAnalog;
     input.inputProcRefCon = p_aout;
     
+    /* Set the new_layout as the layout VLC feeds to the AU unit */
+    verify_noerr( AudioUnitSetProperty( p_sys->au_unit,
+                            kAudioUnitProperty_AudioChannelLayout,
+                            kAudioUnitScope_Input,
+                            0, &new_layout, sizeof(new_layout) ) );
+    
     /* AU initiliaze */
     verify_noerr( AudioUnitInitialize(p_sys->au_unit) );
 
@@ -460,7 +570,7 @@ static int Probe( aout_instance_t * p_aout )
         char psz_devuid[1024];
         char psz_name[1024];
         CFStringRef devUID;
-            
+
         i_param_size = sizeof psz_name;
         err = AudioDeviceGetProperty(
                     p_devices[i], 0, VLC_FALSE,
@@ -481,6 +591,12 @@ static int Probe( aout_instance_t * p_aout )
         msg_Dbg( p_aout, "DevID: %lu  DevName: %s  DevUID: %s", p_devices[i], psz_name, psz_devuid );
         CFRelease( devUID );
 
+        if( !AudioDeviceHasOutput( p_devices[i]) )
+        {
+            msg_Dbg( p_aout, "this device is INPUT only. skipping..." );
+            continue;
+        }
+
         val.i_int = (int) p_devices[i];
         text.psz_string = psz_name;
         var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text );
@@ -493,7 +609,7 @@ static int Probe( aout_instance_t * p_aout )
     var_AddCallback( p_aout, "audio-device", aout_ChannelsRestart, NULL );
     
     /* attach a Listener so that we are notified of a change in the Device setup */
-    /*err = AudioHardwareAddPropertyListener( kAudioHardwarePropertyDevices,
+    /* err = AudioHardwareAddPropertyListener( kAudioHardwarePropertyDevices,
                                             HardwareListener, 
                                             (void *)p_aout );
     if( err )
@@ -577,9 +693,9 @@ static OSStatus RenderCallbackAnalog( vlc_object_t *_p_aout,
                                       unsigned int inNumberFrames,
                                       AudioBufferList *ioData )
 {
-    aout_buffer_t * p_buffer;
     AudioTimeStamp  host_time;
     mtime_t         current_date = 0;
+    uint32_t        i_mData_bytes = 0;    
 
     aout_instance_t * p_aout = (aout_instance_t *)_p_aout;
     struct aout_sys_t * p_sys = p_aout->output.p_sys;
@@ -594,8 +710,6 @@ static OSStatus RenderCallbackAnalog( vlc_object_t *_p_aout,
     current_date = p_sys->clock_diff +
                    AudioConvertHostTimeToNanos( host_time.mHostTime ) / 1000;
 
-    p_aout->output.i_nb_samples = inNumberFrames;
-
     if( ioData == NULL && ioData->mNumberBuffers < 1 )
     {
         msg_Err( p_aout, "no iodata or buffers");
@@ -604,22 +718,50 @@ static OSStatus RenderCallbackAnalog( vlc_object_t *_p_aout,
     if( ioData->mNumberBuffers > 1 )
         msg_Err( p_aout, "well this is weird. seems like there is more than one buffer..." );
 
+
+    if( p_sys->i_total_bytes > 0 )
+    {
+        i_mData_bytes = __MIN( p_sys->i_total_bytes - p_sys->i_read_bytes, ioData->mBuffers[0].mDataByteSize );
+        p_aout->p_vlc->pf_memcpy( ioData->mBuffers[0].mData, &p_sys->p_remainder_buffer[p_sys->i_read_bytes], i_mData_bytes );
+        p_sys->i_read_bytes += i_mData_bytes;
+        current_date += (mtime_t) ( (mtime_t) 1000000 / p_aout->output.output.i_rate ) *
+                        ( i_mData_bytes / 4 / aout_FormatNbChannels( &p_aout->output.output )  ); // 4 is fl32 specific
+        
+        if( p_sys->i_read_bytes >= p_sys->i_total_bytes )
+            p_sys->i_read_bytes = p_sys->i_total_bytes = 0;
+    }
     
-    p_buffer = aout_OutputNextBuffer( p_aout, current_date , VLC_FALSE );
-    if( p_buffer != NULL )
+    while( i_mData_bytes < ioData->mBuffers[0].mDataByteSize )
     {
-        p_aout->p_vlc->pf_memcpy(ioData->mBuffers[0].mData, p_buffer->p_buffer, __MIN( p_buffer->i_nb_bytes, ioData->mBuffers[0].mDataByteSize ) );
-
-        if( p_buffer->i_nb_bytes != ioData->mBuffers[0].mDataByteSize )
+        /* We don't have enough data yet */
+        aout_buffer_t * p_buffer;
+        p_buffer = aout_OutputNextBuffer( p_aout, current_date , VLC_FALSE );
+        
+        if( p_buffer != NULL )
         {
-            /* FIXME */
-            //msg_Err( p_aout, "byte sizes don't match %d:%d\nframes: %d, nb_samples: %d", p_buffer->i_nb_bytes, ioData->mBuffers[0].mDataByteSize, inNumberFrames, p_aout->output.i_nb_samples);
+            uint32_t i_second_mData_bytes = __MIN( p_buffer->i_nb_bytes, ioData->mBuffers[0].mDataByteSize - i_mData_bytes );
+            
+            p_aout->p_vlc->pf_memcpy( (uint8_t *)ioData->mBuffers[0].mData + i_mData_bytes, p_buffer->p_buffer, i_second_mData_bytes );
+            i_mData_bytes += i_second_mData_bytes;
+
+            if( i_mData_bytes >= ioData->mBuffers[0].mDataByteSize )
+            {
+                p_sys->i_total_bytes = p_buffer->i_nb_bytes - i_second_mData_bytes;
+                p_aout->p_vlc->pf_memcpy( p_sys->p_remainder_buffer, &p_buffer->p_buffer[i_second_mData_bytes], p_sys->i_total_bytes );
+            }
+            else
+            {
+                // update current_date
+                current_date += (mtime_t) ( (mtime_t) 1000000 / p_aout->output.output.i_rate ) *
+                                ( i_second_mData_bytes / 4 / aout_FormatNbChannels( &p_aout->output.output )  ); // 4 is fl32 specific
+            }
+            aout_BufferFree( p_buffer );
+        }
+        else
+        {
+             p_aout->p_vlc->pf_memset( (uint8_t *)ioData->mBuffers[0].mData +i_mData_bytes, 0, ioData->mBuffers[0].mDataByteSize - i_mData_bytes );
+             i_mData_bytes += ioData->mBuffers[0].mDataByteSize - i_mData_bytes;
         }
-        aout_BufferFree( p_buffer );
-    }
-    else
-    {
-         p_aout->p_vlc->pf_memset(ioData->mBuffers[0].mData, 0, ioData->mBuffers[0].mDataByteSize);
     }
     return( noErr );     
 }
@@ -646,6 +788,17 @@ error:
     return VLC_EGENERIC;
 }
 
+int AudioDeviceHasOutput( AudioDeviceID i_dev_id )
+{
+    UInt32                     dataSize;
+    Boolean                    isWritable;
+       
+    verify_noerr( AudioDeviceGetPropertyInfo( i_dev_id, 0, FALSE, kAudioDevicePropertyStreams, &dataSize, &isWritable) );
+    if (dataSize == 0) return FALSE;
+    
+    return TRUE;
+}
+
 
 /*****************************************************************************
  * HardwareListener: Warns us of changes in the list of registered devices