⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 directx.c

📁 video linux conference
💻 C
📖 第 1 页 / 共 3 页
字号:
/***************************************************************************** * directx.c: Windows DirectX audio output method ***************************************************************************** * Copyright (C) 2001 VideoLAN * $Id: directx.c 11336 2005-06-07 15:02:55Z gbazin $ * * Authors: Gildas Bazin <gbazin@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 * 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 <fcntl.h>                                       /* open(), O_WRONLY */#include <string.h>                                            /* strerror() */#include <stdlib.h>                            /* calloc(), malloc(), free() */#include <vlc/vlc.h>#include <vlc/aout.h>#include "aout_internal.h"#include <windows.h>#include <mmsystem.h>#include <dsound.h>#define FRAME_SIZE 2048              /* The size is in samples, not in bytes */#define FRAMES_NUM 8/* frame buffer status */#define FRAME_QUEUED 0#define FRAME_EMPTY 1/***************************************************************************** * DirectSound GUIDs. * Defining them here allows us to get rid of the dxguid library during * the linking stage. *****************************************************************************/#include <initguid.h>DEFINE_GUID(IID_IDirectSoundNotify, 0xb0210783, 0x89cd, 0x11d0, 0xaf, 0x8, 0x0, 0xa0, 0xc9, 0x25, 0xcd, 0x16);/***************************************************************************** * Useful macros *****************************************************************************/#ifndef WAVE_FORMAT_IEEE_FLOAT#   define WAVE_FORMAT_IEEE_FLOAT 0x0003#endif#ifndef WAVE_FORMAT_DOLBY_AC3_SPDIF#   define WAVE_FORMAT_DOLBY_AC3_SPDIF 0x0092#endif#ifndef WAVE_FORMAT_EXTENSIBLE#define  WAVE_FORMAT_EXTENSIBLE   0xFFFE#endif#ifndef SPEAKER_FRONT_LEFT#   define SPEAKER_FRONT_LEFT             0x1#   define SPEAKER_FRONT_RIGHT            0x2#   define SPEAKER_FRONT_CENTER           0x4#   define SPEAKER_LOW_FREQUENCY          0x8#   define SPEAKER_BACK_LEFT              0x10#   define SPEAKER_BACK_RIGHT             0x20#   define SPEAKER_FRONT_LEFT_OF_CENTER   0x40#   define SPEAKER_FRONT_RIGHT_OF_CENTER  0x80#   define SPEAKER_BACK_CENTER            0x100#   define SPEAKER_SIDE_LEFT              0x200#   define SPEAKER_SIDE_RIGHT             0x400#   define SPEAKER_TOP_CENTER             0x800#   define SPEAKER_TOP_FRONT_LEFT         0x1000#   define SPEAKER_TOP_FRONT_CENTER       0x2000#   define SPEAKER_TOP_FRONT_RIGHT        0x4000#   define SPEAKER_TOP_BACK_LEFT          0x8000#   define SPEAKER_TOP_BACK_CENTER        0x10000#   define SPEAKER_TOP_BACK_RIGHT         0x20000#   define SPEAKER_RESERVED               0x80000000#endif#ifndef DSSPEAKER_HEADPHONE#   define DSSPEAKER_HEADPHONE         0x00000001#endif#ifndef DSSPEAKER_MONO#   define DSSPEAKER_MONO              0x00000002#endif#ifndef DSSPEAKER_QUAD#   define DSSPEAKER_QUAD              0x00000003#endif#ifndef DSSPEAKER_STEREO#   define DSSPEAKER_STEREO            0x00000004#endif#ifndef DSSPEAKER_SURROUND#   define DSSPEAKER_SURROUND          0x00000005#endif#ifndef DSSPEAKER_5POINT1#   define DSSPEAKER_5POINT1           0x00000006#endif#ifndef _WAVEFORMATEXTENSIBLE_typedef struct {    WAVEFORMATEX    Format;    union {        WORD wValidBitsPerSample;       /* bits of precision  */        WORD wSamplesPerBlock;          /* valid if wBitsPerSample==0 */        WORD wReserved;                 /* If neither applies, set to zero. */    } Samples;    DWORD           dwChannelMask;      /* which channels are */                                        /* present in stream  */    GUID            SubFormat;} WAVEFORMATEXTENSIBLE, *PWAVEFORMATEXTENSIBLE;#endifDEFINE_GUID( _KSDATAFORMAT_SUBTYPE_IEEE_FLOAT, WAVE_FORMAT_IEEE_FLOAT, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 );DEFINE_GUID( _KSDATAFORMAT_SUBTYPE_PCM, WAVE_FORMAT_PCM, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 );DEFINE_GUID( _KSDATAFORMAT_SUBTYPE_DOLBY_AC3_SPDIF, WAVE_FORMAT_DOLBY_AC3_SPDIF, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 );/***************************************************************************** * notification_thread_t: DirectX event thread *****************************************************************************/typedef struct notification_thread_t{    VLC_COMMON_MEMBERS    aout_instance_t * p_aout;    int i_frame_status[FRAMES_NUM];           /* status of each frame buffer */    DSBPOSITIONNOTIFY p_events[FRAMES_NUM];      /* play notification events */    int i_frame_size;                         /* Size in bytes of one frame */    mtime_t start_date;} notification_thread_t;/***************************************************************************** * aout_sys_t: directx audio output method descriptor ***************************************************************************** * This structure is part of the audio output thread descriptor. * It describes the direct sound specific properties of an audio device. *****************************************************************************/struct aout_sys_t{    HINSTANCE           hdsound_dll;      /* handle of the opened dsound dll */    int                 i_device_id;                 /*  user defined device */    LPGUID              p_device_guid;    LPDIRECTSOUND       p_dsobject;              /* main Direct Sound object */    LPDIRECTSOUNDBUFFER p_dsbuffer;   /* the sound buffer we use (direct sound                                       * takes care of mixing all the                                       * secondary buffers into the primary) */    LPDIRECTSOUNDNOTIFY p_dsnotify;         /* the position notify interface */    notification_thread_t *p_notif;                  /* DirectSoundThread id */    int b_playing;                                         /* playing status */    int i_frame_size;                         /* Size in bytes of one frame */    vlc_bool_t b_chan_reorder;              /* do we need channel reordering */    int pi_chan_table[AOUT_CHAN_MAX];    uint32_t i_channel_mask;    uint32_t i_bits_per_sample;    uint32_t i_channels;};static const uint32_t pi_channels_src[] =    { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT,      AOUT_CHAN_MIDDLELEFT, AOUT_CHAN_MIDDLERIGHT,      AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT,      AOUT_CHAN_CENTER, AOUT_CHAN_LFE, 0 };static const uint32_t pi_channels_in[] =    { SPEAKER_FRONT_LEFT, SPEAKER_FRONT_RIGHT,      SPEAKER_SIDE_LEFT, SPEAKER_SIDE_RIGHT,      SPEAKER_BACK_LEFT, SPEAKER_BACK_RIGHT,      SPEAKER_FRONT_CENTER, SPEAKER_LOW_FREQUENCY, 0 };static const uint32_t pi_channels_out[] =    { SPEAKER_FRONT_LEFT, SPEAKER_FRONT_RIGHT,      SPEAKER_FRONT_CENTER, SPEAKER_LOW_FREQUENCY,      SPEAKER_BACK_LEFT, SPEAKER_BACK_RIGHT,      SPEAKER_SIDE_LEFT, SPEAKER_SIDE_RIGHT, 0 };/***************************************************************************** * Local prototypes. *****************************************************************************/static int  OpenAudio  ( vlc_object_t * );static void CloseAudio ( vlc_object_t * );static void Play       ( aout_instance_t * );/* local functions */static void Probe             ( aout_instance_t * );static int  InitDirectSound   ( aout_instance_t * );static int  CreateDSBuffer    ( aout_instance_t *, int, int, int, int, int, vlc_bool_t );static int  CreateDSBufferPCM ( aout_instance_t *, int*, int, int, int, vlc_bool_t );static void DestroyDSBuffer   ( aout_instance_t * );static void DirectSoundThread ( notification_thread_t * );static int  FillBuffer        ( aout_instance_t *, int, aout_buffer_t * );/***************************************************************************** * Module descriptor *****************************************************************************/#define DEVICE_TEXT N_("Output device")#define DEVICE_LONGTEXT N_( \    "DirectX device number: 0 default device, 1..N device by number" \    "(Note that the default device appears as 0 AND another number)." )#define FLOAT_TEXT N_("Use float32 output")#define FLOAT_LONGTEXT N_( \    "The option allows you to enable or disable the high-quality float32 " \    "audio output mode (which is not well supported by some soundcards)." )vlc_module_begin();    set_description( _("DirectX audio output") );    set_shortname( "DirectX" );    set_capability( "audio output", 100 );    set_category( CAT_AUDIO );    set_subcategory( SUBCAT_AUDIO_AOUT );    add_shortcut( "directx" );    add_integer( "directx-audio-device", 0, NULL, DEVICE_TEXT,                 DEVICE_LONGTEXT, VLC_TRUE );    add_bool( "directx-audio-float32", 1, 0, FLOAT_TEXT,              FLOAT_LONGTEXT, VLC_TRUE );    set_callbacks( OpenAudio, CloseAudio );vlc_module_end();/***************************************************************************** * OpenAudio: open the audio device ***************************************************************************** * This function opens and setups Direct Sound. *****************************************************************************/static int OpenAudio( vlc_object_t *p_this ){    aout_instance_t * p_aout = (aout_instance_t *)p_this;    vlc_value_t val;    int i;    msg_Dbg( p_aout, "OpenAudio" );   /* Allocate structure */    p_aout->output.p_sys = malloc( sizeof( aout_sys_t ) );    if( p_aout->output.p_sys == NULL )    {        msg_Err( p_aout, "out of memory" );        return VLC_EGENERIC;    }    /* Initialize some variables */    p_aout->output.p_sys->p_dsobject = NULL;    p_aout->output.p_sys->p_dsbuffer = NULL;    p_aout->output.p_sys->p_dsnotify = NULL;    p_aout->output.p_sys->p_notif = NULL;    p_aout->output.p_sys->b_playing = 0;    p_aout->output.pf_play = Play;    aout_VolumeSoftInit( p_aout );    /* Retrieve config values */    var_Create( p_aout, "directx-audio-float32",                VLC_VAR_BOOL | VLC_VAR_DOINHERIT );    var_Create( p_aout, "directx-audio-device",                VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );    var_Get( p_aout, "directx-audio-device", &val );    p_aout->output.p_sys->i_device_id = val.i_int;    p_aout->output.p_sys->p_device_guid = 0;    /* Initialise DirectSound */    if( InitDirectSound( p_aout ) )    {        msg_Err( p_aout, "cannot initialize DirectSound" );        goto error;    }    if( var_Type( p_aout, "audio-device" ) == 0 )    {        Probe( p_aout );    }    if( var_Get( p_aout, "audio-device", &val ) < 0 )    {        /* Probe() has failed. */        goto error;    }    /* Now we need to setup our DirectSound play notification structure */    p_aout->output.p_sys->p_notif =        vlc_object_create( p_aout, sizeof(notification_thread_t) );    p_aout->output.p_sys->p_notif->p_aout = p_aout;    /* Then create the notification events */    for( i = 0; i < FRAMES_NUM; i++ )        p_aout->output.p_sys->p_notif->p_events[i].hEventNotify =            CreateEvent( NULL, FALSE, FALSE, NULL );    /* Open the device */    if( val.i_int == AOUT_VAR_SPDIF )    {        p_aout->output.output.i_format = VLC_FOURCC('s','p','d','i');        /* Calculate the frame size in bytes */        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;        p_aout->output.p_sys->i_frame_size =            p_aout->output.output.i_bytes_per_frame;        if( CreateDSBuffer( p_aout, VLC_FOURCC('s','p','d','i'),                            p_aout->output.output.i_physical_channels,                            aout_FormatNbChannels( &p_aout->output.output ),                            p_aout->output.output.i_rate,                            p_aout->output.p_sys->i_frame_size, VLC_FALSE )            != VLC_SUCCESS )        {            msg_Err( p_aout, "cannot open directx audio device" );            free( p_aout->output.p_sys );            return VLC_EGENERIC;        }        aout_VolumeNoneInit( p_aout );    }    else    {        if( val.i_int == AOUT_VAR_5_1 )        {            p_aout->output.output.i_physical_channels                = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER                   | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT                   | AOUT_CHAN_LFE;        }        else if( val.i_int == AOUT_VAR_3F2R )        {            p_aout->output.output.i_physical_channels                = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER                   | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;        }        else if( val.i_int == AOUT_VAR_2F2R )        {            p_aout->output.output.i_physical_channels                = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT                   | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;        }        else if( val.i_int == AOUT_VAR_MONO )        {            p_aout->output.output.i_physical_channels = AOUT_CHAN_CENTER;        }        else        {            p_aout->output.output.i_physical_channels                = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;        }        if( CreateDSBufferPCM( p_aout, &p_aout->output.output.i_format,                               p_aout->output.output.i_physical_channels,                               aout_FormatNbChannels( &p_aout->output.output ),                               p_aout->output.output.i_rate, VLC_FALSE )            != VLC_SUCCESS )        {            msg_Err( p_aout, "cannot open directx audio device" );            free( p_aout->output.p_sys );            return VLC_EGENERIC;        }        /* Calculate the frame size in bytes */        p_aout->output.i_nb_samples = FRAME_SIZE;        aout_FormatPrepare( &p_aout->output.output );        p_aout->output.p_sys->i_frame_size =            FRAME_SIZE * p_aout->output.output.i_bytes_per_frame;        aout_VolumeSoftInit( p_aout );    }    /* then launch the notification thread */    msg_Dbg( p_aout, "creating DirectSoundThread" );    if( vlc_thread_create( p_aout->output.p_sys->p_notif,                           "DirectSound Notification Thread",                           DirectSoundThread,                           VLC_THREAD_PRIORITY_HIGHEST, VLC_FALSE ) )    {        msg_Err( p_aout, "cannot create DirectSoundThread" );        goto error;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -