]> git.sesse.net Git - vlc/blobdiff - modules/gui/beos/AudioOutput.cpp
Merge commit 'origin/1.0-bugfix'
[vlc] / modules / gui / beos / AudioOutput.cpp
index 01a1d26d8fbd203e85a5727f287ba495475ab4fc..51cee519937eae44fd39bd4aa8b1db87aae56fba 100644 (file)
@@ -1,17 +1,18 @@
 /*****************************************************************************
- * aout.cpp: BeOS audio output
+ * AudioOutput.cpp: BeOS audio output
  *****************************************************************************
- * Copyright (C) 1999, 2000, 2001 VideoLAN
- * $Id: AudioOutput.cpp,v 1.6 2002/08/29 23:53:22 massiot Exp $
+ * Copyright (C) 1999, 2000, 2001 the VideoLAN team
+ * $Id$
  *
  * 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
  * the Free Software Foundation; either version 2 of the License, or
  * (at your option) any later version.
- * 
+ *
  * This program 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
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
 
 /*****************************************************************************
  * Preamble
  *****************************************************************************/
-#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 <vlc/vlc.h>
-#include <vlc/aout.h>
-#include "aout_internal.h"
+#include <SoundPlayer.h>
+#include <media/MediaDefs.h>
+
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <vlc_common.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
-{
-    BPushGameSound   * p_sound;
-    gs_audio_format  * p_format;
-    void             * p_buffer;
-    int                 i_buffer_size;
-    uint             i_buffer_pos;
-    volatile vlc_bool_t   b_initialized;
-    mtime_t          clock_diff;
 
-};
+typedef struct aout_sys_t
+{
+    BSoundPlayer * p_player;
+    mtime_t        latency;
 
-#define FRAME_SIZE 2048
+} aout_sys_t;
 
 /*****************************************************************************
  * Local prototypes.
  *****************************************************************************/
-int  Open         ( vlc_object_t * );
-void Close        ( vlc_object_t * );
-
-static int  SetFormat    ( aout_instance_t * );
-static void Play         ( aout_instance_t * );
-static int  BeOSThread    ( aout_instance_t * );
+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 );
 
 /*****************************************************************************
- * OpenAudio: opens a BPushGameSound
+ * OpenAudio
  *****************************************************************************/
-int E_(OpenAudio) ( vlc_object_t * p_this )
-{       
+int 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 )
+        return -1;
+    aout_sys_t * p_sys = p_aout->output.p_sys;
 
-    aout_instance_t * p_aout = (aout_instance_t *)p_this;
-    struct aout_sys_t * p_sys;
+    aout_VolumeSoftInit( p_aout );
 
-    /* Allocate structure */
-    p_aout->output.p_sys = p_sys = (aout_sys_t *)malloc( sizeof( struct aout_sys_t ) );
-    memset( p_sys, 0, sizeof( struct aout_sys_t ) );
-    if( p_sys == 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 )
     {
-        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 format */
-    p_sys->p_format = (gs_audio_format *)malloc( sizeof( struct gs_audio_format));
-    SetFormat(p_aout);
 
-    /* Allocate BPushGameSound */
-    p_sys->p_sound = new BPushGameSound( 8192,
-                                         p_sys->p_format,
-                                         2, NULL );
-    if( p_sys->p_sound->InitCheck() != B_OK )
+    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_CODEC_FL32;
+    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_sys->p_format );
+        msg_Err( p_aout, "BSoundPlayer InitCheck failed" );
+        delete p_sys->p_player;
         free( p_sys );
-        msg_Err( p_aout, "cannot initialize BPushGameSound" );
         return -1;
     }
 
-    if( vlc_thread_create( p_aout, "aout", BeOSThread,
-                           VLC_THREAD_PRIORITY_OUTPUT, VLC_FALSE ) )
-    {
-        msg_Err( p_aout, "cannot create aout thread" );
-        delete p_sys->p_sound;
-        free( p_sys->p_format );
-        free( p_sys );
-        return -1;
-
-    }
-    
-    p_aout->output.pf_setformat = SetFormat;
-    p_aout->output.pf_play = Play;
+    /* Start playing */
+    p_sys->latency = p_sys->p_player->Latency();
+    p_sys->p_player->Start();
+    p_sys->p_player->SetHasData( true );
 
     return 0;
 }
 
 /*****************************************************************************
- * SetFormat: sets the dsp output format
+ * CloseAudio
  *****************************************************************************/
-static int SetFormat( aout_instance_t *p_aout )
+void CloseAudio ( vlc_object_t * p_this )
 {
-    /* Initialize some variables */
-    p_aout->output.p_sys->p_format->frame_rate = p_aout->output.output.i_rate;
-    p_aout->output.p_sys->p_format->channel_count = p_aout->output.output.i_channels;
-    
-    switch (p_aout->output.output.i_format)
-    {
-    case AOUT_FMT_S16_LE:
-        p_aout->output.p_sys->p_format->format = gs_audio_format::B_GS_S16;
-        p_aout->output.p_sys->p_format->byte_order = B_MEDIA_LITTLE_ENDIAN;
-        break;
-    case AOUT_FMT_S16_BE:
-        p_aout->output.p_sys->p_format->format = gs_audio_format::B_GS_S16;
-        p_aout->output.p_sys->p_format->byte_order = B_MEDIA_BIG_ENDIAN;
-        break;
-    case AOUT_FMT_S8:
-        p_aout->output.p_sys->p_format->format = gs_audio_format::B_GS_U8;
-        p_aout->output.p_sys->p_format->byte_order = B_MEDIA_LITTLE_ENDIAN;
-        break;
-    case AOUT_FMT_FLOAT32:
-        p_aout->output.p_sys->p_format->format = gs_audio_format::B_GS_F;
-        p_aout->output.p_sys->p_format->byte_order = B_MEDIA_LITTLE_ENDIAN;
-        break;
-    default:
-        msg_Err( p_aout, "cannot set audio format (%i)",
-                          p_aout->output.output.i_format );
-        return -1;
-    }
-
-    p_aout->output.p_sys->p_format->buffer_size = 4*8192;
+    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->output.p_sys->b_initialized = VLC_TRUE;
-
-    return( 0 );
+    /* Clean up */
+    p_sys->p_player->Stop();
+    delete p_sys->p_player;
+    free( p_sys );
 }
 
 /*****************************************************************************
- * Play: plays a sound samples buffer
- *****************************************************************************
- * This function writes a buffer of i_length bytes in the dsp
+ * Play
  *****************************************************************************/
-static void Play( aout_instance_t *p_aout )
+static void Play( void * _p_aout, void * _p_buffer, size_t i_size,
+                  const media_raw_audio_format &format )
 {
-}
-
-/*****************************************************************************
- * CloseAudio: closes the dsp audio device
- *****************************************************************************/
-void E_(CloseAudio) ( vlc_object_t *p_this )
-{       
-    aout_instance_t * p_aout = (aout_instance_t *)p_this;
+    aout_instance_t * p_aout = (aout_instance_t*) _p_aout;
+    float * p_buffer = (float*) _p_buffer;
+    aout_sys_t * p_sys = (aout_sys_t*) p_aout->output.p_sys;
+    aout_buffer_t * p_aout_buffer;
 
-    p_aout->output.p_sys->p_sound->UnlockCyclic();
-    p_aout->output.p_sys->p_sound->StopPlaying( );
-    delete p_aout->output.p_sys->p_sound;
-    
-    p_aout->b_die = 1;
-    free( p_aout->output.p_sys->p_format );
-    free( p_aout->output.p_sys );
-}
+    p_aout_buffer = aout_OutputNextBuffer( p_aout,
+                                           mdate() + p_sys->latency,
+                                           false );
 
-/*****************************************************************************
- * GetBufInfo: buffer status query
- *****************************************************************************
- * This function fills in the audio_buf_info structure :
- * - returns : number of available fragments (not partially used ones)
- * - int fragstotal : total number of fragments allocated
- * - int fragsize : size of a fragment in bytes
- * - int bytes : available space in bytes (includes partially used fragments)
- * Note! 'bytes' could be more than fragments*fragsize
- *****************************************************************************/
-static int GetBufInfo( aout_instance_t * p_aout )
-{
-    /* returns the allocated space in bytes */
-    return ( p_aout->output.p_sys->p_format->buffer_size );
-}
-
-/*****************************************************************************
- * BeOSThread: asynchronous thread used to DMA the data to the device
- *****************************************************************************/
-static int BeOSThread( aout_instance_t * p_aout )
-{
-    struct aout_sys_t * p_sys = p_aout->output.p_sys;
-
-    static uint i_buffer_pos;
-
-    p_sys->p_sound->StartPlaying( );
-    p_sys->p_sound->LockForCyclic( &p_sys->p_buffer,
-                                   (size_t *)&p_sys->i_buffer_size );
-
-    while ( !p_aout->b_die )
+    if( p_aout_buffer != NULL )
     {
-        aout_buffer_t * p_buffer;
-        int i_tmp, i_size;
-        byte_t * p_bytes;
-
-        if( !p_sys->b_initialized )
+        vlc_memcpy( p_buffer, p_aout_buffer->p_buffer,
+                    MIN( i_size, p_aout_buffer->i_nb_bytes ) );
+        if( p_aout_buffer->i_nb_bytes < i_size )
         {
-            msleep( THREAD_SLEEP );
-            continue;
-        }
-        mtime_t next_date = 0;
-        /* Get the presentation date of the next write() operation. It
-         * is equal to the current date + duration of buffered samples.
-         * Order is important here, since GetBufInfo is believed to take
-         * more time than mdate(). */
-        next_date = (mtime_t)GetBufInfo( p_aout ) * 1000000
-                  / p_aout->output.output.i_bytes_per_frame
-                  / p_aout->output.output.i_rate
-                  * p_aout->output.output.i_frame_length;
-        next_date += mdate();
-
-        p_buffer = aout_OutputNextBuffer( p_aout, next_date, VLC_FALSE );
-        int i_newbuf_pos;
-        if ( p_buffer != NULL )
-        {
-            p_bytes = p_buffer->p_buffer;
-            i_size = p_buffer->i_nb_bytes;
-        }
-        else
-        {
-            i_size = FRAME_SIZE / p_aout->output.output.i_frame_length
-                      * p_aout->output.output.i_bytes_per_frame;
-            p_bytes = (byte_t *)malloc( i_size );
-            memset( p_bytes, 0, i_size );
-        }
-   
-        if( (i_newbuf_pos = i_buffer_pos + p_buffer->i_size)
-                            > p_aout->output.p_sys->i_buffer_size )
-        {
-            p_aout->p_vlc->pf_memcpy( (void *)((int)p_aout->output.p_sys->p_buffer
-                            + i_buffer_pos),
-                              p_buffer->p_buffer,
-                              p_aout->output.p_sys->i_buffer_size - i_buffer_pos );
-
-            p_aout->p_vlc->pf_memcpy( (void *)((int)p_aout->output.p_sys->p_buffer),
-                            p_buffer->p_buffer + p_aout->output.p_sys->i_buffer_size - i_buffer_pos,
-                            p_buffer->i_size - ( p_aout->output.p_sys->i_buffer_size - i_buffer_pos ) );
-        
-            i_buffer_pos = i_newbuf_pos - i_buffer_pos;
-        }
-        else
-        {
-           p_aout->p_vlc->pf_memcpy( (void *)((int)p_aout->output.p_sys->p_buffer + i_buffer_pos),
-                    p_buffer->p_buffer, p_buffer->i_size );
-
-           i_buffer_pos = i_newbuf_pos;
+            vlc_memset(  p_buffer + p_aout_buffer->i_nb_bytes,
+                         0, i_size - p_aout_buffer->i_nb_bytes );
         }
+        aout_BufferFree( p_aout_buffer );
+    }
+    else
+    {
+        vlc_memset( p_buffer, 0, i_size );
     }
-    
-    return 0;
-
 }
 
+/*****************************************************************************
+ * DoNothing
+ *****************************************************************************/
+static void DoNothing( aout_instance_t *p_aout )
+{
+    return;
+}