]> git.sesse.net Git - vlc/blobdiff - modules/gui/beos/AudioOutput.cpp
* skeleton of the arm crosscompilation howto
[vlc] / modules / gui / beos / AudioOutput.cpp
index d89716545757b03506e189a08aa8302525cd6376..e3b1508abf8f9b7da55e21e0f754f1d8b7d0db61 100644 (file)
@@ -1,11 +1,12 @@
 /*****************************************************************************
- * aout.cpp: BeOS audio output
+ * AudioOutput.cpp: BeOS audio output
  *****************************************************************************
  * Copyright (C) 1999, 2000, 2001 VideoLAN
- * $Id: AudioOutput.cpp,v 1.1 2002/08/04 17:23:43 sam Exp $
+ * $Id: AudioOutput.cpp,v 1.27 2003/01/14 15:31:12 titer Exp $
  *
  * Authors: Jean-Marc Dressler <polux@via.ecp.fr>
  *          Samuel Hocevar <sam@zoy.org>
+ *          Eric Petit <titer@videolan.org>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  *****************************************************************************/
 #include <stdio.h>
 #include <stdlib.h>                                      /* malloc(), free() */
-#include <kernel/OS.h>
-#include <View.h>
-#include <Application.h>
-#include <Message.h>
-#include <Locker.h>
-#include <media/MediaDefs.h>
-#include <game/PushGameSound.h>
 #include <malloc.h>
 #include <string.h>
 
+#include <SoundPlayer.h>
+#include <media/MediaDefs.h>
+
+
 #include <vlc/vlc.h>
 #include <vlc/aout.h>
+extern "C"
+{
+    #include <aout_internal.h>
+}
 
 /*****************************************************************************
  * aout_sys_t: BeOS audio output method descriptor
- *****************************************************************************
- * This structure is part of the audio output thread descriptor.
- * It describes some BeOS specific variables.
  *****************************************************************************/
-struct aout_sys_t
+
+typedef struct aout_sys_t
 {
-    BPushGameSound * p_sound;
-    gs_audio_format * p_format;
-    void * p_buffer;
-    int i_buffer_size;
-    int i_buffer_pos;
-};
+    BSoundPlayer *p_player;
+    vlc_mutex_t   lock;
+    mtime_t       latency;
+    
+} aout_sys_t;
 
 /*****************************************************************************
  * Local prototypes.
  *****************************************************************************/
-static int     SetFormat   ( aout_thread_t * );
-static int     GetBufInfo  ( aout_thread_t *, int );
-static void    Play        ( aout_thread_t *, byte_t *, int );
+static void Play         ( void *p_aout, void *p_buffer, size_t size,
+                           const media_raw_audio_format &format );
+static void DoNothing    ( aout_instance_t *p_aout );
+static int  CheckLatency ( aout_instance_t *p_aout );
 
 /*****************************************************************************
- * OpenAudio: opens a BPushGameSound
+ * OpenAudio
  *****************************************************************************/
-int E_(OpenAudio) ( vlc_object_t *p_this )
-{       
-    aout_thread_t * p_aout = (aout_thread_t *)p_this;
-
-    /* Allocate structure */
-    p_aout->p_sys = (aout_sys_t*) malloc( sizeof( aout_sys_t ) );
-    if( p_aout->p_sys == NULL )
+int E_(OpenAudio) ( vlc_object_t * p_this )
+{
+    aout_instance_t *p_aout = (aout_instance_t*) p_this;
+    p_aout->output.p_sys = (aout_sys_t *) malloc( sizeof( aout_sys_t ) );
+    if( p_aout->output.p_sys == NULL )
     {
-        msg_Err( p_aout, "out of memory" );
-        return( 1 );
+        msg_Err( p_aout, "Not enough memory" );
+        return -1;
     }
+    aout_sys_t *p_sys = p_aout->output.p_sys;
+
+    aout_VolumeSoftInit( p_aout );
 
-    /* Allocate gs_audio_format */
-    p_aout->p_sys->p_format = (gs_audio_format *) malloc( sizeof( gs_audio_format ) );
-    if( p_aout->p_sys->p_format == NULL )
+    int i_nb_channels = aout_FormatNbChannels( &p_aout->output.output );
+    /* BSoundPlayer does not support more than 2 channels AFAIK */
+    if ( i_nb_channels > 2 )
     {
-        free( p_aout->p_sys );
-        msg_Err( p_aout, "out of memory" );
-        return( 1 );
+        i_nb_channels = 2;
+        p_aout->output.output.i_physical_channels
+            = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
     }
-
-    /* Initialize some variables */
-    p_aout->p_sys->p_format->frame_rate = 44100.0;
-    p_aout->p_sys->p_format->channel_count = p_aout->i_channels;
-    p_aout->p_sys->p_format->format = gs_audio_format::B_GS_S16;
-    p_aout->p_sys->p_format->byte_order = B_MEDIA_LITTLE_ENDIAN;
-    p_aout->p_sys->p_format->buffer_size = 4*8192;
-    p_aout->p_sys->i_buffer_pos = 0;
-
-    p_aout->pf_setformat = SetFormat;
-    p_aout->pf_getbufinfo = GetBufInfo;
-    p_aout->pf_play = Play;
-
-    /* Allocate BPushGameSound */
-    p_aout->p_sys->p_sound = new BPushGameSound( 8192,
-                                                 p_aout->p_sys->p_format,
-                                                 2, NULL );
-    if( p_aout->p_sys->p_sound == NULL )
+    media_raw_audio_format *p_format;
+    p_format = (media_raw_audio_format*)
+        malloc( sizeof( media_raw_audio_format ) );
+    
+    p_format->channel_count = i_nb_channels;
+    p_format->frame_rate = p_aout->output.output.i_rate;    
+    p_format->format = media_raw_audio_format::B_AUDIO_FLOAT;
+#ifdef WORDS_BIGENDIAN
+    p_format->byte_order = B_MEDIA_BIG_ENDIAN;
+#else
+    p_format->byte_order = B_MEDIA_LITTLE_ENDIAN;
+#endif
+    p_format->buffer_size = 8192;
+
+    p_aout->output.output.i_format = VLC_FOURCC('f','l','3','2');
+    p_aout->output.i_nb_samples = 2048 / i_nb_channels;
+    p_aout->output.pf_play = DoNothing;
+
+    p_sys->p_player = new BSoundPlayer( p_format, "player", Play, NULL, p_aout );
+    if( p_sys->p_player->InitCheck() != B_OK )
     {
-        free( p_aout->p_sys->p_format );
-        free( p_aout->p_sys );
-        msg_Err( p_aout, "cannot allocate BPushGameSound" );
-        return( 1 );
+        msg_Err( p_aout, "BSoundPlayer InitCheck failed" );
+        delete p_sys->p_player;
+        free( p_sys );
+        return -1;
     }
 
-    if( p_aout->p_sys->p_sound->InitCheck() != B_OK )
+    /* FIXME FIXME FIXME
+     * We should check BSoundPlayer Latency() everytime we call
+     * aout_OutputNextBuffer(). Unfortunatly, it does not seem to work
+     * correctly: on my computer, it hangs for about 5 seconds at the
+     * beginning of the file. This is not acceptable, so we will start
+     * playing the file with a default latency (my computer's one ;p )
+     * and we create a thread who's going to update it ASAP. According
+     * to what I've seen, the latency is almost constant most of the
+     * time anyway -- titer */
+    p_sys->latency = 16209;
+    vlc_mutex_init( p_aout, &p_sys->lock );
+    if( vlc_thread_create( p_aout, "latency", CheckLatency,
+                           VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
     {
-        free( p_aout->p_sys->p_format );
-        free( p_aout->p_sys );
-        msg_Err( p_aout, "cannot initialize BPushGameSound" );
-        return( 1 );
+        msg_Err( p_aout, "cannot create Latency thread" );
     }
 
-    p_aout->p_sys->p_sound->StartPlaying( );
-
-    p_aout->p_sys->p_sound->LockForCyclic( &p_aout->p_sys->p_buffer,
-                            (size_t *)&p_aout->p_sys->i_buffer_size );
+    p_sys->p_player->Start();
+    p_sys->p_player->SetHasData( true );
 
-    return( 0 );
+    return 0;
 }
 
 /*****************************************************************************
- * SetFormat: sets the dsp output format
+ * CloseAudio
  *****************************************************************************/
-static int SetFormat( aout_thread_t *p_aout )
+void E_(CloseAudio) ( vlc_object_t *p_this )
 {
-    return( 0 );
+    aout_instance_t * p_aout = (aout_instance_t *) p_this;
+    aout_sys_t * p_sys = (aout_sys_t *) p_aout->output.p_sys;
+    
+    p_aout->b_die = VLC_TRUE;
+    vlc_thread_join( p_aout );
+    p_aout->b_die = VLC_FALSE;
+    vlc_mutex_destroy( &p_sys->lock );
+    
+    p_sys->p_player->Stop();
+    delete p_sys->p_player;
+    free( p_sys );
 }
 
 /*****************************************************************************
- * GetBufInfo: buffer status query
+ * Play
  *****************************************************************************/
-static int GetBufInfo( aout_thread_t *p_aout, int i_buffer_limit )
+static void Play( void *aout, void *p_buffer, size_t i_size,
+                  const media_raw_audio_format &format )
 {
-    /* Each value is 4 bytes long (stereo signed 16 bits) */
-    int i_hard_pos = 4 * p_aout->p_sys->p_sound->CurrentPosition();
-
-    i_hard_pos = p_aout->p_sys->i_buffer_pos - i_hard_pos;
-    if( i_hard_pos < 0 )
+    aout_instance_t *p_aout = (aout_instance_t*)aout;
+    aout_sys_t *p_sys = (aout_sys_t*)p_aout->output.p_sys;
+    aout_buffer_t * p_aout_buffer;
+    mtime_t play_time = 0;
+    
+    /* FIXME (see above) */
+    vlc_mutex_lock( &p_sys->lock );
+    play_time = mdate() + p_sys->latency;
+    vlc_mutex_unlock( &p_sys->lock );
+    
+    p_aout_buffer = aout_OutputNextBuffer( p_aout, play_time, VLC_FALSE );
+
+    if( p_aout_buffer != NULL )
     {
-         i_hard_pos += p_aout->p_sys->i_buffer_size;
+        p_aout->p_vlc->pf_memcpy( (float*)p_buffer,
+                                  p_aout_buffer->p_buffer, i_size );
+        aout_BufferFree( p_aout_buffer );
+    }
+    else
+    {
+        p_aout->p_vlc->pf_memset( (float*)p_buffer, 0, i_size );
     }
-
-    return( i_hard_pos );
 }
 
 /*****************************************************************************
- * Play: plays a sound samples buffer
- *****************************************************************************
- * This function writes a buffer of i_length bytes in the dsp
+ * DoNothing 
  *****************************************************************************/
-static void Play( aout_thread_t *p_aout, byte_t *buffer, int i_size )
+static void DoNothing( aout_instance_t *p_aout )
 {
-    int i_newbuf_pos;
-
-    if( (i_newbuf_pos = p_aout->p_sys->i_buffer_pos + i_size)
-              > p_aout->p_sys->i_buffer_size )
-    {
-        memcpy( (void *)((int)p_aout->p_sys->p_buffer
-                        + p_aout->p_sys->i_buffer_pos),
-                buffer,
-                p_aout->p_sys->i_buffer_size - p_aout->p_sys->i_buffer_pos );
-
-        memcpy( (void *)((int)p_aout->p_sys->p_buffer),
-                buffer + p_aout->p_sys->i_buffer_size - p_aout->p_sys->i_buffer_pos,
-                i_size - ( p_aout->p_sys->i_buffer_size
-                             - p_aout->p_sys->i_buffer_pos ) );
-        
-        p_aout->p_sys->i_buffer_pos = i_newbuf_pos - p_aout->p_sys->i_buffer_size;
-
-    }
-    else
-    {
-        memcpy( (void *)((int)p_aout->p_sys->p_buffer + p_aout->p_sys->i_buffer_pos),
-                buffer, i_size );
-
-        p_aout->p_sys->i_buffer_pos = i_newbuf_pos;
-    }
+    return;
 }
 
 /*****************************************************************************
- * CloseAudio: closes the dsp audio device
+ * CheckLatency
  *****************************************************************************/
-void E_(CloseAudio) ( vlc_object_t *p_this )
-{       
-    aout_thread_t * p_aout = (aout_thread_t *)p_this;
-
-    p_aout->p_sys->p_sound->UnlockCyclic();
-    p_aout->p_sys->p_sound->StopPlaying( );
-    delete p_aout->p_sys->p_sound;
-    free( p_aout->p_sys->p_format );
-    free( p_aout->p_sys );
+static int CheckLatency( aout_instance_t *p_aout )
+{
+    aout_sys_t *p_sys = (aout_sys_t*)p_aout->output.p_sys;
+    mtime_t last_check = 0;
+    mtime_t latency;
+    
+    while( !p_aout->b_die )
+    {
+        /* check every 0.1 second */
+        if( mdate() > last_check + 100000 )
+        {
+            latency = p_sys->p_player->Latency();
+            if( latency > 0 )
+            {
+                vlc_mutex_lock( &p_sys->lock );
+                p_sys->latency = latency;
+                vlc_mutex_unlock( &p_sys->lock );
+            }
+            last_check = mdate();
+        }
+        snooze( 5000 );
+    }
+    return VLC_SUCCESS;
 }
-