]> git.sesse.net Git - vlc/blobdiff - modules/audio_output/oss.c
auhal: fix minor memory leak when handling SPDIF devices
[vlc] / modules / audio_output / oss.c
index 6239160163c45079636194594d6990896d35e710..8a70eb310a19ee145db17f2a0b2ec799d74de838 100644 (file)
 /*****************************************************************************
- * oss.c : OSS /dev/dsp module for vlc
+ * oss.c: Open Sound System audio output plugin for VLC
  *****************************************************************************
- * Copyright (C) 2000-2002 VideoLAN
- * $Id: oss.c,v 1.15 2002/08/24 10:19:42 sam Exp $
+ * Copyright (C) 2000-2002 VLC authors and VideoLAN
+ * Copyright (C) 2007-2012 RĂ©mi Denis-Courmont
  *
  * Authors: Michel Kaempf <maxx@via.ecp.fr>
- *          Samuel Hocevar <sam@zoy.org>
+ *          Sam Hocevar <sam@zoy.org>
  *          Christophe Massiot <massiot@via.ecp.fr>
  *
- * 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
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2.1 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
- * GNU General Public License for more details.
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
  *
- * 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.
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
 
-/*****************************************************************************
- * Preamble
- *****************************************************************************/
-#include <errno.h>                                                 /* ENOMEM */
-#include <fcntl.h>                                       /* open(), O_WRONLY */
-#include <sys/ioctl.h>                                            /* ioctl() */
-#include <string.h>                                            /* strerror() */
-#include <unistd.h>                                      /* write(), close() */
-#include <stdlib.h>                            /* calloc(), malloc(), free() */
-
-#include <vlc/vlc.h>
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
 
-#ifdef HAVE_ALLOCA_H
-#   include <alloca.h>
+#include <stdlib.h>
+#include <math.h>
+#include <sys/types.h>
+#include <fcntl.h>
+#include <sys/ioctl.h>
+#ifdef HAVE_SOUNDCARD_H
+# include <soundcard.h>
+#else
+# include <sys/soundcard.h>
 #endif
 
-#include <vlc/aout.h>
+#ifndef SNDCTL_DSP_HALT
+# define SNDCTL_DSP_HALT SNDCTL_DSP_RESET
+#endif
 
-#include "aout_internal.h"
+#include <vlc_common.h>
+#include <vlc_plugin.h>
+#include <vlc_fs.h>
+#include <vlc_cpu.h>
+#include <vlc_aout.h>
 
-/* SNDCTL_DSP_RESET, SNDCTL_DSP_SETFMT, SNDCTL_DSP_STEREO, SNDCTL_DSP_SPEED,
- * SNDCTL_DSP_GETOSPACE */
-#ifdef HAVE_SOUNDCARD_H
-#   include <soundcard.h>
-#elif defined( HAVE_SYS_SOUNDCARD_H )
-#   include <sys/soundcard.h>
-#elif defined( HAVE_MACHINE_SOUNDCARD_H )
-#   include <machine/soundcard.h>
-#endif
+#define A52_FRAME_NB 1536
 
-/*****************************************************************************
- * aout_sys_t: OSS audio output method descriptor
- *****************************************************************************
- * This structure is part of the audio output thread descriptor.
- * It describes the dsp specific properties of an audio device.
- *****************************************************************************/
 struct aout_sys_t
 {
-    int                   i_fd;
-    volatile vlc_bool_t   b_initialized;
-};
+    int fd;
+    audio_sample_format_t format;
+    bool starting;
 
-#define FRAME_SIZE 1024
-#define A52_FRAME_NB 1536
+    bool mute;
+    uint8_t level;
+    char *device;
+};
 
-/*****************************************************************************
- * Local prototypes
- *****************************************************************************/
-static int  Open         ( vlc_object_t * );
-static void Close        ( vlc_object_t * );
+static int Open (vlc_object_t *);
+static void Close (vlc_object_t *);
+
+#define AUDIO_DEV_TEXT N_("Audio output device")
+#define AUDIO_DEV_LONGTEXT N_("OSS device node path.")
+
+vlc_module_begin ()
+    set_shortname( "OSS" )
+    set_description (N_("Open Sound System audio output"))
+    set_category( CAT_AUDIO )
+    set_subcategory( SUBCAT_AUDIO_AOUT )
+    add_string ("oss-audio-device", "",
+                AUDIO_DEV_TEXT, AUDIO_DEV_LONGTEXT, false)
+    set_capability( "audio output", 100 )
+    set_callbacks (Open, Close)
+vlc_module_end ()
+
+static int TimeGet (audio_output_t *, mtime_t *);
+static void Play (audio_output_t *, block_t *);
+static void Pause (audio_output_t *, bool, mtime_t);
+static void Flush (audio_output_t *, bool);
+static int VolumeSync (audio_output_t *);
+
+static int Start (audio_output_t *aout, audio_sample_format_t *restrict fmt)
+{
+    aout_sys_t* sys = aout->sys;
 
-static int  SetFormat    ( aout_instance_t * );
-static void Play         ( aout_instance_t * );
-static int  OSSThread    ( aout_instance_t * );
+    /* Open the device */
+    const char *device = sys->device;
+    if (device == NULL)
+        device = getenv ("OSS_AUDIODEV");
+    if (device == NULL)
+        device = "/dev/dsp";
 
-/*****************************************************************************
- * Module descriptor
- *****************************************************************************/
-vlc_module_begin();
-    add_category_hint( N_("OSS"), NULL );
-    add_file( "dspdev", "/dev/dsp", NULL, N_("OSS dsp device"), NULL );
-    set_description( _("Linux OSS /dev/dsp module") );
-    set_capability( "audio output", 100 );
-    add_shortcut( "dsp" );
-    set_callbacks( Open, Close );
-vlc_module_end();
+    int fd = vlc_open (device, O_WRONLY);
+    if (fd == -1)
+    {
+        msg_Err (aout, "cannot open OSS device %s: %m", device);
+        return VLC_EGENERIC;
+    }
+    sys->fd = fd;
+    msg_Dbg (aout, "using OSS device: %s", device);
 
-/*****************************************************************************
- * Open: open the audio device (the digital sound processor)
- *****************************************************************************
- * This function opens the dsp as a usual non-blocking write-only file, and
- * modifies the p_aout->p_sys->i_fd with the file's descriptor.
- *****************************************************************************/
-static int Open( vlc_object_t *p_this )
-{
-    aout_instance_t * p_aout = (aout_instance_t *)p_this;
-    struct aout_sys_t * p_sys;
-    char * psz_device;
+    /* Select audio format */
+    int format;
+    bool spdif = false;
 
-    /* Allocate structure */
-    p_aout->output.p_sys = p_sys = malloc( sizeof( aout_sys_t ) );
-    if( p_sys == NULL )
+    switch (fmt->i_format)
     {
-        msg_Err( p_aout, "out of memory" );
-        return 1;
+#ifdef AFMT_FLOAT
+        case VLC_CODEC_FL64:
+        case VLC_CODEC_FL32:
+            format = AFMT_FLOAT;
+            break;
+#endif
+        case VLC_CODEC_S32N:
+            format = AFMT_S32_NE;
+            break;
+        case VLC_CODEC_S16N:
+            format = AFMT_S16_NE;
+            break;
+        case VLC_CODEC_U8:
+            format = AFMT_U8;
+            break;
+        default:
+            if (AOUT_FMT_SPDIF(fmt))
+                spdif = var_InheritBool (aout, "spdif");
+            if (spdif)
+                format = AFMT_AC3;
+#ifdef AFMT_FLOAT
+            else if (HAVE_FPU)
+                format = AFMT_FLOAT;
+#endif
+            else
+                format = AFMT_S16_NE;
     }
 
-    /* Initialize some variables */
-    if( (psz_device = config_GetPsz( p_aout, "dspdev" )) == NULL )
+    if (ioctl (fd, SNDCTL_DSP_SETFMT, &format) < 0)
     {
-        msg_Err( p_aout, "no audio device given (maybe /dev/dsp ?)" );
-        free( p_sys );
-        return -1;
+        msg_Err (aout, "cannot set audio format 0x%X: %m", format);
+        goto error;
     }
 
-    /* Open the sound device */
-    if( (p_sys->i_fd = open( psz_device, O_WRONLY )) < 0 )
+    switch (format)
     {
-        msg_Err( p_aout, "cannot open audio device (%s)", psz_device );
-        free( psz_device );
-        free( p_sys );
-        return -1;
+        case AFMT_U8:     fmt->i_format = VLC_CODEC_U8;   break;
+        case AFMT_S16_NE: fmt->i_format = VLC_CODEC_S16N; break;
+        case AFMT_S32_NE: fmt->i_format = VLC_CODEC_S32N; break;
+#ifdef AFMT_FLOAT
+        case AFMT_FLOAT:  fmt->i_format = VLC_CODEC_FL32; break;
+#endif
+        case AFMT_AC3:
+            if (spdif)
+            {
+                fmt->i_format = VLC_CODEC_SPDIFL;
+                break;
+            }
+        default:
+            msg_Err (aout, "unsupported audio format 0x%X", format);
+            goto error;
     }
-    free( psz_device );
 
-    /* Create OSS thread and wait for its readiness. */
-    p_sys->b_initialized = VLC_FALSE;
-    if( vlc_thread_create( p_aout, "aout", OSSThread, VLC_FALSE ) )
+    /* Select channels count */
+    int channels = spdif ? 2 : aout_FormatNbChannels (fmt);
+    if (ioctl (fd, SNDCTL_DSP_CHANNELS, &channels) < 0)
     {
-        msg_Err( p_aout, "cannot create OSS thread (%s)", strerror(errno) );
-        close( p_sys->i_fd );
-        free( psz_device );
-        free( p_sys );
-        return -1;
+        msg_Err (aout, "cannot set %d channels: %m", channels);
+        goto error;
     }
 
-    p_aout->output.pf_setformat = SetFormat;
-    p_aout->output.pf_play = Play;
-
-    return 0;
-}
-
-/*****************************************************************************
- * SetFormat: reset the dsp and set its format
- *****************************************************************************
- * This functions resets the DSP device, tries to initialize the output
- * format with the value contained in the dsp structure, and if this value
- * could not be set, the default value returned by ioctl is set. It then
- * does the same for the stereo mode, and for the output rate.
- *****************************************************************************/
-static int SetFormat( aout_instance_t *p_aout )
-{
-    struct aout_sys_t * p_sys = p_aout->output.p_sys;
-    int i_format;
-    int i_rate;
-    vlc_bool_t b_stereo;
-
-    p_sys->b_initialized = VLC_FALSE;
+    switch (channels)
+    {
+        case 1: channels = AOUT_CHAN_CENTER;  break;
+        case 2: channels = AOUT_CHANS_STEREO; break;
+        case 4: channels = AOUT_CHANS_4_0;    break;
+        case 6: channels = AOUT_CHANS_5_1;    break;
+        case 8: channels = AOUT_CHANS_7_1;    break;
+        default:
+            msg_Err (aout, "unsupported channels count %d", channels);
+            goto error;
+    }
 
-    /* Reset the DSP device */
-    if( ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL ) < 0 )
+    /* Select sample rate */
+    int rate = spdif ? 48000 : fmt->i_rate;
+    if (ioctl (fd, SNDCTL_DSP_SPEED, &rate) < 0)
     {
-        msg_Err( p_aout, "cannot reset OSS audio device" );
-        return -1;
+        msg_Err (aout, "cannot set %d Hz sample rate: %m", rate);
+        goto error;
     }
 
-    /* Set the output format */
-    if ( p_aout->output.output.i_format == AOUT_FMT_SPDIF )
+    /* Setup audio_output_t */
+    aout->time_get = TimeGet;
+    aout->play = Play;
+    aout->pause = Pause;
+    aout->flush = Flush;
+
+    if (spdif)
     {
-        i_format = AOUT_FMT_SPDIF;
-        p_aout->output.i_nb_samples = A52_FRAME_NB;
-        p_aout->output.output.i_bytes_per_frame = AOUT_SPDIF_SIZE;
-        p_aout->output.output.i_frame_length = A52_FRAME_NB;
+        fmt->i_bytes_per_frame = AOUT_SPDIF_SIZE;
+        fmt->i_frame_length = A52_FRAME_NB;
     }
     else
     {
-        p_aout->output.output.i_format = i_format = AOUT_FMT_S16_NE;
-        p_aout->output.i_nb_samples = FRAME_SIZE;
+        fmt->i_rate = rate;
+        fmt->i_original_channels =
+        fmt->i_physical_channels = channels;
     }
 
-    if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0
-         || i_format != p_aout->output.output.i_format )
+    VolumeSync (aout);
+    sys->starting = true;
+    sys->format = *fmt;
+    return VLC_SUCCESS;
+error:
+    close (fd);
+    return VLC_EGENERIC;
+}
+
+static int TimeGet (audio_output_t *aout, mtime_t *restrict pts)
+{
+    aout_sys_t *sys = aout->sys;
+    int delay;
+
+    if (ioctl (sys->fd, SNDCTL_DSP_GETODELAY, &delay) < 0)
     {
-        msg_Err( p_aout, "cannot set audio output format (%i)",
-                          i_format );
+        msg_Warn (aout, "cannot get delay: %m");
         return -1;
     }
 
-    if ( p_aout->output.output.i_format != AOUT_FMT_SPDIF )
+    *pts = (delay * CLOCK_FREQ * sys->format.i_frame_length)
+                        / (sys->format.i_rate * sys->format.i_bytes_per_frame);
+    return 0;
+}
+
+/**
+ * Queues one audio buffer to the hardware.
+ */
+static void Play (audio_output_t *aout, block_t *block)
+{
+    aout_sys_t *sys = aout->sys;
+    int fd = sys->fd;
+
+    while (block->i_buffer > 0)
     {
-        /* FIXME */
-        if ( p_aout->output.output.i_channels > 2 )
+        ssize_t bytes = write (fd, block->p_buffer, block->i_buffer);
+        if (bytes >= 0)
         {
-            msg_Warn( p_aout, "only two channels are supported at the moment" );
-            /* Trigger downmixing */
-            p_aout->output.output.i_channels = 2;
+            block->p_buffer += bytes;
+            block->i_buffer -= bytes;
         }
+        else
+            msg_Err (aout, "cannot write samples: %m");
+    }
+    block_Release (block);
 
-        /* Set the number of channels */
-        b_stereo = p_aout->output.output.i_channels - 1;
+    /* Dumb OSS cannot send any kind of events for this... */
+    VolumeSync (aout);
+}
 
-        if( ioctl( p_sys->i_fd, SNDCTL_DSP_STEREO, &b_stereo ) < 0 )
-        {
-            msg_Err( p_aout, "cannot set number of audio channels (%i)",
-                              p_aout->output.output.i_channels );
-            return -1;
-        }
+/**
+ * Pauses/resumes the audio playback.
+ */
+static void Pause (audio_output_t *aout, bool pause, mtime_t date)
+{
+    aout_sys_t *sys = aout->sys;
+    int fd = sys->fd;
 
-        if ( b_stereo + 1 != p_aout->output.output.i_channels )
-        {
-            msg_Warn( p_aout, "driver forced up/downmixing %li->%li",
-                              p_aout->output.output.i_channels,
-                              b_stereo + 1 );
-            p_aout->output.output.i_channels = b_stereo + 1;
-        }
+    (void) date;
+    ioctl (fd, pause ? SNDCTL_DSP_SILENCE : SNDCTL_DSP_SKIP, NULL);
+}
 
-        /* Set the output rate */
-        i_rate = p_aout->output.output.i_rate;
-        if( ioctl( p_sys->i_fd, SNDCTL_DSP_SPEED, &i_rate ) < 0 )
-        {
-            msg_Err( p_aout, "cannot set audio output rate (%i)",
-                             p_aout->output.output.i_rate );
-            return -1;
-        }
+/**
+ * Flushes/drains the audio playback buffer.
+ */
+static void Flush (audio_output_t *aout, bool wait)
+{
+    aout_sys_t *sys = aout->sys;
+    int fd = sys->fd;
 
-        if( i_rate != p_aout->output.output.i_rate )
-        {
-            msg_Warn( p_aout, "driver forced resampling %li->%li",
-                              p_aout->output.output.i_rate, i_rate );
-            p_aout->output.output.i_rate = i_rate;
-        }
-    }
+    if (wait)
+        return; /* drain is implicit with OSS */
+    ioctl (fd, SNDCTL_DSP_HALT_OUTPUT, NULL);
+}
 
-    p_sys->b_initialized = VLC_TRUE;
+static int VolumeSync (audio_output_t *aout)
+{
+    aout_sys_t *sys = aout->sys;
+    int fd = sys->fd;
+
+    int level;
+    if (ioctl (fd, SNDCTL_DSP_GETPLAYVOL, &level) < 0)
+        return -1;
 
+    sys->mute = !level;
+    if (level) /* try to keep last volume before mute */
+        sys->level = level;
+    aout_MuteReport (aout, !level);
+    aout_VolumeReport (aout, (float)(level & 0xFF) / 100.f);
     return 0;
 }
 
-/*****************************************************************************
- * Play: queue a buffer for playing by OSSThread
- *****************************************************************************/
-static void Play( aout_instance_t *p_aout )
+/**
+ * Releases the audio output device.
+ */
+static void Stop (audio_output_t *aout)
 {
+    aout_sys_t *sys = aout->sys;
+    int fd = sys->fd;
+
+    ioctl (fd, SNDCTL_DSP_HALT, NULL);
+    close (fd);
+    sys->fd = -1;
 }
 
-/*****************************************************************************
- * Close: close the dsp audio device
- *****************************************************************************/
-static void Close( vlc_object_t * p_this )
+static int VolumeSet (audio_output_t *aout, float vol)
 {
-    aout_instance_t *p_aout = (aout_instance_t *)p_this;
-    struct aout_sys_t * p_sys = p_aout->output.p_sys;
-
-    p_aout->b_die = 1;
-    vlc_thread_join( p_aout );
+    aout_sys_t *sys = aout->sys;
+    int fd = sys->fd;
+    if (fd == -1)
+        return -1;
 
-    ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL );
-    close( p_sys->i_fd );
+    int level = lroundf (vol * 100.f);
+    if (level > 0xFF)
+        level = 0xFFFF;
+    else
+        level |= level << 8;
+    if (!sys->mute && ioctl (fd, SNDCTL_DSP_SETPLAYVOL, &level) < 0)
+    {
+        msg_Err (aout, "cannot set volume: %m");
+        return -1;
+    }
 
-    free( p_sys );
+    sys->level = level;
+    aout_VolumeReport (aout, (float)(level & 0xFF) / 100.f);
+    return 0;
 }
 
-
-/*****************************************************************************
- * 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 )
+static int MuteSet (audio_output_t *aout, bool mute)
 {
-    struct aout_sys_t * p_sys = p_aout->output.p_sys;
-    audio_buf_info audio_buf;
+    aout_sys_t *sys = aout->sys;
+    int fd = sys->fd;
+    if (fd == -1)
+        return -1;
 
-    ioctl( p_sys->i_fd, SNDCTL_DSP_GETOSPACE, &audio_buf );
+    int level = mute ? 0 : (sys->level | (sys->level << 8));
+    if (ioctl (fd, SNDCTL_DSP_SETPLAYVOL, &level) < 0)
+    {
+        msg_Err (aout, "cannot mute: %m");
+        return -1;
+    }
 
-    /* returns the allocated space in bytes */
-    return ( (audio_buf.fragstotal * audio_buf.fragsize) - audio_buf.bytes );
+    sys->mute = mute;
+    aout_MuteReport (aout, mute);
+    return 0;
 }
 
-/*****************************************************************************
- * OSSThread: asynchronous thread used to DMA the data to the device
- *****************************************************************************/
-static int OSSThread( aout_instance_t * p_aout )
+static int DevicesEnum (audio_output_t *aout, char ***idp, char ***namep)
 {
-    struct aout_sys_t * p_sys = p_aout->output.p_sys;
+    aout_sys_t *sys = aout->sys;
+    int fd = sys->fd;
+    oss_sysinfo si;
 
-    while ( !p_aout->b_die )
+    if (fd == -1)
+        return -1;
+    if (ioctl (fd, SNDCTL_SYSINFO, &si) < 0)
     {
-        aout_buffer_t * p_buffer;
-        int i_tmp, i_size;
-        byte_t * p_bytes;
+        msg_Err (aout, "cannot get system infos: %m");
+        return -1;
+    }
 
-        if( !p_sys->b_initialized )
-        {
-            msleep( THREAD_SLEEP );
-            continue;
-        }
+    msg_Dbg (aout, "using %s version %s (0x%06X) under %s", si.product,
+             si.version, si.versionnum, si.license);
 
-        if ( p_aout->output.output.i_format != AOUT_FMT_SPDIF )
-        {
-            mtime_t buffered = (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 buffer will be played at mdate()+buffered, and we tell
-             * the audio output that it can wait for a new packet for
-             * buffered/2 microseconds. */
-            p_buffer = aout_OutputNextBuffer( p_aout, mdate() + buffered,
-                                              buffered / 2, VLC_FALSE );
-        }
-        else
-        {
-            p_buffer = aout_OutputNextBuffer( p_aout, 0, 0, VLC_TRUE );
-        }
+    char **ids = xmalloc (sizeof (*ids) * si.numaudios);
+    char **names = xmalloc (sizeof (*names) * si.numaudios);
+    int n = 0;
 
-        if ( p_buffer != NULL )
-        {
-            p_bytes = p_buffer->p_buffer;
-            i_size = p_buffer->i_nb_bytes;
-        }
-        else
+    for (int i = 0; i < si.numaudios; i++)
+    {
+        oss_audioinfo ai = { .dev = i };
+
+        if (ioctl (fd, SNDCTL_AUDIOINFO, &ai) < 0)
         {
-            i_size = FRAME_SIZE / p_aout->output.output.i_frame_length
-                      * p_aout->output.output.i_bytes_per_frame;
-            p_bytes = alloca( i_size );
-            memset( p_bytes, 0, i_size );
+            msg_Warn (aout, "cannot get device %d infos: %m", i);
+            continue;
         }
+        if (ai.caps & (PCM_CAP_HIDDEN|PCM_CAP_MODEM))
+            continue;
+        if (!(ai.caps & PCM_CAP_OUTPUT))
+            continue;
+        if (!ai.enabled)
+            continue;
 
-        i_tmp = write( p_sys->i_fd, p_bytes, i_size );
+        ids[n] = xstrdup (ai.devnode);
+        names[n] = xstrdup (ai.name);
+        n++;
+    }
+    *idp = ids;
+    *namep = names;
+    return n;
+}
 
-        if( i_tmp < 0 )
-        {
-            msg_Err( p_aout, "write failed (%s)", strerror(errno) );
-        }
+static int DeviceSelect (audio_output_t *aout, const char *id)
+{
+    aout_sys_t *sys = aout->sys;
+    char *path = NULL;
 
-        if ( p_buffer != NULL )
-        {
-            aout_BufferFree( p_buffer );
-        }
+    if (id != NULL)
+    {
+        path = strdup (id);
+        if (unlikely(path == NULL))
+            return -1;
     }
 
+    free (sys->device);
+    sys->device = path;
+    aout_DeviceReport (aout, path);
+    aout_RestartRequest (aout, AOUT_RESTART_OUTPUT);
     return 0;
 }
+
+static int Open (vlc_object_t *obj)
+{
+    audio_output_t *aout = (audio_output_t *)obj;
+
+    aout_sys_t *sys = malloc (sizeof (*sys));
+    if(unlikely( sys == NULL ))
+        return VLC_ENOMEM;
+
+    sys->fd = -1;
+
+    sys->level = 100;
+    sys->mute = false;
+    sys->device = var_InheritString (aout, "oss-audio-device");
+
+    aout->sys = sys;
+    aout->start = Start;
+    aout->stop = Stop;
+    aout->volume_set = VolumeSet;
+    aout->mute_set = MuteSet;
+    aout->device_enum = DevicesEnum;
+    aout->device_select = DeviceSelect;
+    return VLC_SUCCESS;
+}
+
+static void Close (vlc_object_t *obj)
+{
+    audio_output_t *aout = (audio_output_t *)obj;
+    aout_sys_t *sys = aout->sys;
+
+    free (sys->device);
+    free (sys);
+}