<commit_before>/*****************************************************************************
 * alsa.c : alsa plugin for vlc
 *****************************************************************************
 * Copyright (C) 2000-2001 the VideoLAN team
 * $Id$
 *
 * Authors: <NAME> <<EMAIL>> - Original Author
 *          <NAME> <<EMAIL>> - Port to ALSA 1.0 API
 *          <NAME> <<EMAIL>> - Device selection
 *          <NAME> <<EMAIL>> - S/PDIF and aout3
 *
 * 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
 * GNU 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.
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#include <errno.h>                                                 /* ENOMEM */
#include <string.h>                                            /* strerror() */
#include <stdlib.h>                            /* calloc(), malloc(), free() */

#include <vlc/vlc.h>

#include <vlc/aout.h>

#include "aout_internal.h"

/* ALSA part
   Note: we use the new API which is available since 0.9.0beta10a. */
#define ALSA_PCM_NEW_HW_PARAMS_API
#define ALSA_PCM_NEW_SW_PARAMS_API
#include <alsa/asoundlib.h>

/* Time to sleep between to attempts to open the device */
#define SLEEP_TIME 100000

/*****************************************************************************
 * aout_sys_t: ALSA audio output method descriptor
 *****************************************************************************
 * This structure is part of the audio output thread descriptor.
 * It describes the ALSA specific properties of an audio device.
 *****************************************************************************/
struct aout_sys_t
{
    snd_pcm_t         * p_snd_pcm;
    unsigned int                 i_period_time;

#ifdef ALSA_DEBUG
    snd_output_t      * p_snd_stderr;
#endif

    int b_playing;                                         /* playing status */
    mtime_t start_date;

    vlc_mutex_t lock;
    vlc_cond_t  wait ;

    snd_pcm_status_t *p_status;
};

#define A52_FRAME_NB 1536

/* These values are in frames.
   To convert them to a number of bytes you have to multiply them by the
   number of channel(s) (eg. 2 for stereo) and the size of a sample (eg.
   2 for int16_t). */
#define ALSA_DEFAULT_PERIOD_SIZE        1024
#define ALSA_DEFAULT_BUFFER_SIZE        ( ALSA_DEFAULT_PERIOD_SIZE << 8 )
#define ALSA_SPDIF_PERIOD_SIZE          A52_FRAME_NB
#define ALSA_SPDIF_BUFFER_SIZE          ( ALSA_SPDIF_PERIOD_SIZE << 4 )
/* Why << 4 ? --Meuuh */
/* Why not ? --Bozo */
/* Right. --Meuuh */

#define DEFAULT_ALSA_DEVICE N_("default")

/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
static int  Open         ( vlc_object_t * );
static void Close        ( vlc_object_t * );
static void Play         ( aout_instance_t * );
static int  ALSAThread   ( aout_instance_t * );
static void ALSAFill     ( aout_instance_t * );
static int FindDevicesCallback( vlc_object_t *p_this, char const *psz_name,
                                vlc_value_t newval, vlc_value_t oldval, void *p_unused );

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
static char *ppsz_devices[] = { "default" };
static char *ppsz_devices_text[] = { N_("Default") };
vlc_module_begin();
    set_shortname( "ALSA" );
    set_description( _("ALSA audio output") );
    set_category( CAT_AUDIO );
    set_subcategory( SUBCAT_AUDIO_AOUT );
    add_string( "alsadev", DEFAULT_ALSA_DEVICE, aout_FindAndRestart,
                N_("ALSA Device Name"), NULL, VLC_FALSE );
        change_string_list( ppsz_devices, ppsz_devices_text, FindDevicesCallback );
        change_action_add( FindDevicesCallback, N_("Refresh list") );



    set_capability( "audio output", 150 );
    set_callbacks( Open, Close );
vlc_module_end();


/*****************************************************************************
 * ProbeFormat: probe the audio device for available formats
 *****************************************************************************/
int ProbeFormat( aout_instance_t * p_aout, const char * psz_device,
                          int *pi_snd_pcm_format )
{
    int i_ret, i;
    struct aout_sys_t * p_sys = p_aout->output.p_sys;
    struct timeval tval;

    /* We'll open the audio device in non blocking mode so we can just exit
     * when it is already in use, but for the real stuff we'll still use
     * the blocking mode */

    /* Now test linear PCM capabilities */

    gettimeofday( &tval, NULL );
    msg_Err( p_aout, "Time before open: %i", tval.tv_usec );

    /* Since it seems snd_pcm_close hasen't really released the device at
       the time it returns, probe if the device is available in loop for 1s.
       We cannot use blocking mode since the we would wait indefinitely when
       switching from a dmx device to surround51. */

    for( i = 10; i >= 0; i-- )
    {
        if ( !(i_ret = snd_pcm_open( &p_sys->p_snd_pcm, psz_device,
                                 SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK) ) )
        {
            snd_pcm_hw_params_t * p_hw;
            snd_pcm_hw_params_alloca (&p_hw);

            if ( snd_pcm_hw_params_any( p_sys->p_snd_pcm, p_hw ) < 0 )
            {
                msg_Warn( p_aout, "unable to retrieve initial hardware"
                                "parameters, disabling linear PCM audio" );
                snd_pcm_close( p_sys->p_snd_pcm );
                var_Destroy( p_aout, "audio-device" );
                return -1;
            }

            if ( snd_pcm_hw_params_set_format( p_sys->p_snd_pcm, p_hw,
                                           *pi_snd_pcm_format ) < 0 )
            {
                if( *pi_snd_pcm_format != SND_PCM_FORMAT_S16 )
                {
                    *pi_snd_pcm_format = SND_PCM_FORMAT_S16;
                    if ( snd_pcm_hw_params_set_format( p_sys->p_snd_pcm, p_hw,
                                                   *pi_snd_pcm_format ) < 0 )<commit_msg>Forgot to remove some debug...<commit_after>/*****************************************************************************
 * alsa.c : alsa plugin for vlc
 *****************************************************************************
 * Copyright (C) 2000-2001 the VideoLAN team
 * $Id$
 *
 * Authors: <NAME> <<EMAIL>> - Original Author
 *          <NAME> <<EMAIL>> - Port to ALSA 1.0 API
 *          <NAME> <<EMAIL>> - Device selection
 *          <NAME> <<EMAIL>> - S/PDIF and aout3
 *
 * 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
 * GNU 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.
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#include <errno.h>                                                 /* ENOMEM */
#include <string.h>                                            /* strerror() */
#include <stdlib.h>                            /* calloc(), malloc(), free() */

#include <vlc/vlc.h>

#include <vlc/aout.h>

#include "aout_internal.h"

/* ALSA part
   Note: we use the new API which is available since 0.9.0beta10a. */
#define ALSA_PCM_NEW_HW_PARAMS_API
#define ALSA_PCM_NEW_SW_PARAMS_API
#include <alsa/asoundlib.h>

/* Time to sleep between to attempts to open the device */
#define SLEEP_TIME 100000

/*****************************************************************************
 * aout_sys_t: ALSA audio output method descriptor
 *****************************************************************************
 * This structure is part of the audio output thread descriptor.
 * It describes the ALSA specific properties of an audio device.
 *****************************************************************************/
struct aout_sys_t
{
    snd_pcm_t         * p_snd_pcm;
    unsigned int                 i_period_time;

#ifdef ALSA_DEBUG
    snd_output_t      * p_snd_stderr;
#endif

    int b_playing;                                         /* playing status */
    mtime_t start_date;

    vlc_mutex_t lock;
    vlc_cond_t  wait ;

    snd_pcm_status_t *p_status;
};

#define A52_FRAME_NB 1536

/* These values are in frames.
   To convert them to a number of bytes you have to multiply them by the
   number of channel(s) (eg. 2 for stereo) and the size of a sample (eg.
   2 for int16_t). */
#define ALSA_DEFAULT_PERIOD_SIZE        1024
#define ALSA_DEFAULT_BUFFER_SIZE        ( ALSA_DEFAULT_PERIOD_SIZE << 8 )
#define ALSA_SPDIF_PERIOD_SIZE          A52_FRAME_NB
#define ALSA_SPDIF_BUFFER_SIZE          ( ALSA_SPDIF_PERIOD_SIZE << 4 )
/* Why << 4 ? --Meuuh */
/* Why not ? --Bozo */
/* Right. --Meuuh */

#define DEFAULT_ALSA_DEVICE N_("default")

/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
static int  Open         ( vlc_object_t * );
static void Close        ( vlc_object_t * );
static void Play         ( aout_instance_t * );
static int  ALSAThread   ( aout_instance_t * );
static void ALSAFill     ( aout_instance_t * );
static int FindDevicesCallback( vlc_object_t *p_this, char const *psz_name,
                                vlc_value_t newval, vlc_value_t oldval, void *p_unused );

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
static char *ppsz_devices[] = { "default" };
static char *ppsz_devices_text[] = { N_("Default") };
vlc_module_begin();
    set_shortname( "ALSA" );
    set_description( _("ALSA audio output") );
    set_category( CAT_AUDIO );
    set_subcategory( SUBCAT_AUDIO_AOUT );
    add_string( "alsadev", DEFAULT_ALSA_DEVICE, aout_FindAndRestart,
                N_("ALSA Device Name"), NULL, VLC_FALSE );
        change_string_list( ppsz_devices, ppsz_devices_text, FindDevicesCallback );
        change_action_add( FindDevicesCallback, N_("Refresh list") );



    set_capability( "audio output", 150 );
    set_callbacks( Open, Close );
vlc_module_end();


/*****************************************************************************
 * ProbeFormat: probe the audio device for available formats
 *****************************************************************************/
int ProbeFormat( aout_instance_t * p_aout, const char * psz_device,
                          int *pi_snd_pcm_format )
{
    int i_ret, i;
    struct aout_sys_t * p_sys = p_aout->output.p_sys;
    struct timeval tval;

    /* We'll open the audio device in non blocking mode so we can just exit
     * when it is already in use, but for the real stuff we'll still use
     * the blocking mode */

    /* Now test linear PCM capabilities */

    /* Since it seems snd_pcm_close hasen't really released the device at
       the time it returns, probe if the device is available in loop for 1s.
       We cannot use blocking mode since the we would wait indefinitely when
       switching from a dmx device to surround51. */

    for( i = 10; i >= 0; i-- )
    {
        if ( !(i_ret = snd_pcm_open( &p_sys->p_snd_pcm, psz_device,
                                 SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK) ) )
        {
            snd_pcm_hw_params_t * p_hw;
            snd_pcm_hw_params_alloca (&p_hw);

            if ( snd_pcm_hw_params_any( p_sys->p_snd_pcm, p_hw ) < 0 )
            {
                msg_Warn( p_aout, "unable to retrieve initial hardware"
                                "parameters, disabling linear PCM audio" );
                snd_pcm_close( p_sys->p_snd_pcm );
                var_Destroy( p_aout, "audio-device" );
                return -1;
            }

            if ( snd_pcm_hw_params_set_format( p_sys->p_snd_pcm, p_hw,
                                           *pi_snd_pcm_format ) < 0 )
            {
                if( *pi_snd_pcm_format != SND_PCM_FORMAT_S16 )
                {
                    *pi_snd_pcm_format = SND_PCM_FORMAT_S16;
                    if ( snd_pcm_hw_params_set_format( p_sys->p_snd_pcm, p_hw,
                                                   *pi_snd_pcm_format ) < 0 )
{'fraction_non_alphanumeric': 0.2290851094678846, 'fraction_numerical': 0.010004349717268378, 'mean_word_length': 2.8598209289311693, 'pattern_counts': {'":': 0, '<': 57, '<?xml version=': 0, '>': 53, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 22, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7191838', 'n_tokens_mistral': 3959, 'n_tokens_neox': 3674, 'n_words': 1132}