gstreamer/sys/directsound/gstdirectsoundsink.c

873 lines
27 KiB
C
Raw Normal View History

/* GStreamer
* Copyright (C) 2005 Sebastien Moutte <sebastien@moutte.net>
* Copyright (C) 2007 Pioneers of the Inevitable <songbird@songbirdnest.com>
* Copyright (C) 2010 Fluendo S.A. <support@fluendo.com>
*
* gstdirectsoundsink.c:
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*
*
* The development of this code was made possible due to the involvement
* of Pioneers of the Inevitable, the creators of the Songbird Music player
*
*/
/**
* SECTION:element-directsoundsink
*
* This element lets you output sound using the DirectSound API.
*
* Note that you should almost always use generic audio conversion elements
* like audioconvert and audioresample in front of an audiosink to make sure
* your pipeline works under all circumstances (those conversion elements will
* act in passthrough-mode if no conversion is necessary).
*
* <refsect2>
* <title>Example pipelines</title>
* |[
* gst-launch-1.0 -v audiotestsrc ! audioconvert ! volume volume=0.1 ! directsoundsink
* ]| will output a sine wave (continuous beep sound) to your sound card (with
* a very low volume as precaution).
* |[
* gst-launch-1.0 -v filesrc location=music.ogg ! decodebin ! audioconvert ! audioresample ! directsoundsink
* ]| will play an Ogg/Vorbis audio file and output it.
* </refsect2>
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
2012-01-27 15:37:19 +00:00
#include <gst/base/gstbasesink.h>
#include <gst/audio/streamvolume.h>
#include "gstdirectsoundsink.h"
#include <gst/audio/gstaudioiec61937.h>
#include <math.h>
#ifdef __CYGWIN__
#include <unistd.h>
#ifndef _swab
#define _swab swab
#endif
#endif
2012-01-27 15:37:19 +00:00
#define DEFAULT_MUTE FALSE
GST_DEBUG_CATEGORY_STATIC (directsoundsink_debug);
#define GST_CAT_DEFAULT directsoundsink_debug
2012-01-27 15:37:19 +00:00
static void gst_directsound_sink_finalize (GObject * object);
static void gst_directsound_sink_set_property (GObject * object, guint prop_id,
const GValue * value, GParamSpec * pspec);
static void gst_directsound_sink_get_property (GObject * object, guint prop_id,
GValue * value, GParamSpec * pspec);
2012-01-27 15:37:19 +00:00
static GstCaps *gst_directsound_sink_getcaps (GstBaseSink * bsink,
GstCaps * filter);
static GstBuffer *gst_directsound_sink_payload (GstAudioBaseSink * sink,
GstBuffer * buf);
static gboolean gst_directsound_sink_prepare (GstAudioSink * asink,
2012-01-27 15:37:19 +00:00
GstAudioRingBufferSpec * spec);
static gboolean gst_directsound_sink_unprepare (GstAudioSink * asink);
static gboolean gst_directsound_sink_open (GstAudioSink * asink);
static gboolean gst_directsound_sink_close (GstAudioSink * asink);
2012-01-27 15:37:19 +00:00
static gint gst_directsound_sink_write (GstAudioSink * asink,
gpointer data, guint length);
static guint gst_directsound_sink_delay (GstAudioSink * asink);
static void gst_directsound_sink_reset (GstAudioSink * asink);
static GstCaps *gst_directsound_probe_supported_formats (GstDirectSoundSink *
dsoundsink, const GstCaps * template_caps);
static gboolean gst_directsound_sink_query (GstBaseSink * pad,
GstQuery * query);
2012-01-27 15:37:19 +00:00
static void gst_directsound_sink_set_volume (GstDirectSoundSink * sink,
gdouble volume, gboolean store);
static gdouble gst_directsound_sink_get_volume (GstDirectSoundSink * sink);
static void gst_directsound_sink_set_mute (GstDirectSoundSink * sink,
gboolean mute);
static gboolean gst_directsound_sink_get_mute (GstDirectSoundSink * sink);
static gboolean gst_directsound_sink_is_spdif_format (GstAudioRingBufferSpec *
spec);
static GstStaticPadTemplate directsoundsink_sink_factory =
GST_STATIC_PAD_TEMPLATE ("sink",
GST_PAD_SINK,
GST_PAD_ALWAYS,
2012-01-27 15:37:19 +00:00
GST_STATIC_CAPS ("audio/x-raw, "
"format = (string) S16LE, "
"layout = (string) interleaved, "
"rate = (int) [ 1, MAX ], " "channels = (int) [ 1, 2 ]; "
2012-01-27 15:37:19 +00:00
"audio/x-raw, "
"format = (string) S8, "
"layout = (string) interleaved, "
"rate = (int) [ 1, MAX ], " "channels = (int) [ 1, 2 ];"
2012-05-25 10:19:09 +00:00
"audio/x-ac3, framed = (boolean) true;"
"audio/x-dts, framed = (boolean) true;"));
enum
{
PROP_0,
2012-01-27 15:37:19 +00:00
PROP_VOLUME,
PROP_MUTE
};
2012-01-27 15:37:19 +00:00
#define gst_directsound_sink_parent_class parent_class
G_DEFINE_TYPE_WITH_CODE (GstDirectSoundSink, gst_directsound_sink,
GST_TYPE_AUDIO_SINK, G_IMPLEMENT_INTERFACE (GST_TYPE_STREAM_VOLUME, NULL)
);
docs/plugins/gst-plugins-bad-plugins.args: Remove directsoundsink property doc as this sink use the mixer interface now. Original commit message from CVS: * docs/plugins/gst-plugins-bad-plugins.args: Remove directsoundsink property doc as this sink use the mixer interface now. * docs/plugins/gst-plugins-bad-plugins.interfaces: Add interfaces implemented by Windows sinks. * sys/directsound/gstdirectsoundsink.c: * sys/directsound/gstdirectsoundsink.h: Remove directsoundsink property and implement the mixer interface. * win32/vs6/gst_plugins_bad.dsw: * win32/vs6/libgstdirectsound.dsp: Update project files. * gst-libs/gst/dshow/gstdshow.cpp: * gst-libs/gst/dshow/gstdshow.h: * gst-libs/gst/dshow/gstdshowfakesink.cpp: * gst-libs/gst/dshow/gstdshowfakesink.h: * gst-libs/gst/dshow/gstdshowfakesrc.cpp: * gst-libs/gst/dshow/gstdshowfakesrc.h: * gst-libs/gst/dshow/gstdshowinterface.cpp: * gst-libs/gst/dshow/gstdshowinterface.h: * win32/common/libgstdshow.def: * win32/vs6/libgstdshow.dsp: Add a new gst library which allow to create internal Direct Show graph (pipelines) to wrap Windows sources, decoders or encoders. It includes a DirectShow fake source and sink and utility functions. * sys/dshowsrcwrapper/gstdshowaudiosrc.c: * sys/dshowsrcwrapper/gstdshowaudiosrc.h: * sys/dshowsrcwrapper/gstdshowsrcwrapper.c: * sys/dshowsrcwrapper/gstdshowsrcwrapper.h: * sys/dshowsrcwrapper/gstdshowvideosrc.c: * sys/dshowsrcwrapper/gstdshowvideosrc.h: * win32/vs6/libdshowsrcwrapper.dsp: Add a new plugin to wrap DirectShow sources on Windows. It gets data from any webcam, dv cam, micro. We could add tv tunner card later.
2007-05-23 22:44:12 +00:00
static void
2012-01-27 15:37:19 +00:00
gst_directsound_sink_finalize (GObject * object)
docs/plugins/gst-plugins-bad-plugins.args: Remove directsoundsink property doc as this sink use the mixer interface now. Original commit message from CVS: * docs/plugins/gst-plugins-bad-plugins.args: Remove directsoundsink property doc as this sink use the mixer interface now. * docs/plugins/gst-plugins-bad-plugins.interfaces: Add interfaces implemented by Windows sinks. * sys/directsound/gstdirectsoundsink.c: * sys/directsound/gstdirectsoundsink.h: Remove directsoundsink property and implement the mixer interface. * win32/vs6/gst_plugins_bad.dsw: * win32/vs6/libgstdirectsound.dsp: Update project files. * gst-libs/gst/dshow/gstdshow.cpp: * gst-libs/gst/dshow/gstdshow.h: * gst-libs/gst/dshow/gstdshowfakesink.cpp: * gst-libs/gst/dshow/gstdshowfakesink.h: * gst-libs/gst/dshow/gstdshowfakesrc.cpp: * gst-libs/gst/dshow/gstdshowfakesrc.h: * gst-libs/gst/dshow/gstdshowinterface.cpp: * gst-libs/gst/dshow/gstdshowinterface.h: * win32/common/libgstdshow.def: * win32/vs6/libgstdshow.dsp: Add a new gst library which allow to create internal Direct Show graph (pipelines) to wrap Windows sources, decoders or encoders. It includes a DirectShow fake source and sink and utility functions. * sys/dshowsrcwrapper/gstdshowaudiosrc.c: * sys/dshowsrcwrapper/gstdshowaudiosrc.h: * sys/dshowsrcwrapper/gstdshowsrcwrapper.c: * sys/dshowsrcwrapper/gstdshowsrcwrapper.h: * sys/dshowsrcwrapper/gstdshowvideosrc.c: * sys/dshowsrcwrapper/gstdshowvideosrc.h: * win32/vs6/libdshowsrcwrapper.dsp: Add a new plugin to wrap DirectShow sources on Windows. It gets data from any webcam, dv cam, micro. We could add tv tunner card later.
2007-05-23 22:44:12 +00:00
{
G_OBJECT_CLASS (parent_class)->finalize (object);
}
static void
gst_directsound_sink_class_init (GstDirectSoundSinkClass * klass)
{
2012-01-27 15:37:19 +00:00
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
GstBaseSinkClass *gstbasesink_class = GST_BASE_SINK_CLASS (klass);
GstAudioSinkClass *gstaudiosink_class = GST_AUDIO_SINK_CLASS (klass);
GstAudioBaseSinkClass *gstaudiobasesink_class =
GST_AUDIO_BASE_SINK_CLASS (klass);
2012-01-27 15:37:19 +00:00
GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
docs/plugins/gst-plugins-bad-plugins.args: Remove directsoundsink property doc as this sink use the mixer interface now. Original commit message from CVS: * docs/plugins/gst-plugins-bad-plugins.args: Remove directsoundsink property doc as this sink use the mixer interface now. * docs/plugins/gst-plugins-bad-plugins.interfaces: Add interfaces implemented by Windows sinks. * sys/directsound/gstdirectsoundsink.c: * sys/directsound/gstdirectsoundsink.h: Remove directsoundsink property and implement the mixer interface. * win32/vs6/gst_plugins_bad.dsw: * win32/vs6/libgstdirectsound.dsp: Update project files. * gst-libs/gst/dshow/gstdshow.cpp: * gst-libs/gst/dshow/gstdshow.h: * gst-libs/gst/dshow/gstdshowfakesink.cpp: * gst-libs/gst/dshow/gstdshowfakesink.h: * gst-libs/gst/dshow/gstdshowfakesrc.cpp: * gst-libs/gst/dshow/gstdshowfakesrc.h: * gst-libs/gst/dshow/gstdshowinterface.cpp: * gst-libs/gst/dshow/gstdshowinterface.h: * win32/common/libgstdshow.def: * win32/vs6/libgstdshow.dsp: Add a new gst library which allow to create internal Direct Show graph (pipelines) to wrap Windows sources, decoders or encoders. It includes a DirectShow fake source and sink and utility functions. * sys/dshowsrcwrapper/gstdshowaudiosrc.c: * sys/dshowsrcwrapper/gstdshowaudiosrc.h: * sys/dshowsrcwrapper/gstdshowsrcwrapper.c: * sys/dshowsrcwrapper/gstdshowsrcwrapper.h: * sys/dshowsrcwrapper/gstdshowvideosrc.c: * sys/dshowsrcwrapper/gstdshowvideosrc.h: * win32/vs6/libdshowsrcwrapper.dsp: Add a new plugin to wrap DirectShow sources on Windows. It gets data from any webcam, dv cam, micro. We could add tv tunner card later.
2007-05-23 22:44:12 +00:00
GST_DEBUG_CATEGORY_INIT (directsoundsink_debug, "directsoundsink", 0,
"DirectSound sink");
Fix #337365 (g_type_class_ref <-> g_type_class_peek_parent) Original commit message from CVS: * ext/amrwb/gstamrwbdec.c: (gst_amrwbdec_class_init): * ext/amrwb/gstamrwbenc.c: (gst_amrwbenc_class_init): * ext/amrwb/gstamrwbparse.c: (gst_amrwbparse_class_init): * ext/arts/gst_arts.c: (gst_arts_class_init): * ext/artsd/gstartsdsink.c: (gst_artsdsink_class_init): * ext/audiofile/gstafsink.c: (gst_afsink_class_init): * ext/audiofile/gstafsrc.c: (gst_afsrc_class_init): * ext/audioresample/gstaudioresample.c: * ext/cdaudio/gstcdaudio.c: (gst_cdaudio_class_init): * ext/directfb/dfbvideosink.c: (gst_dfbvideosink_class_init): * ext/divx/gstdivxdec.c: (gst_divxdec_class_init): * ext/hermes/gsthermescolorspace.c: (gst_hermes_colorspace_class_init): * ext/ivorbis/vorbisfile.c: (gst_ivorbisfile_class_init): * ext/jack/gstjack.c: (gst_jack_class_init): * ext/jack/gstjackbin.c: (gst_jack_bin_class_init): * ext/lcs/gstcolorspace.c: (gst_colorspace_class_init): * ext/libfame/gstlibfame.c: (gst_fameenc_class_init): * ext/musicbrainz/gsttrm.c: (gst_musicbrainz_class_init): * ext/nas/nassink.c: (gst_nassink_class_init): * ext/shout/gstshout.c: (gst_icecastsend_class_init): * ext/snapshot/gstsnapshot.c: (gst_snapshot_class_init): * ext/sndfile/gstsf.c: (gst_sf_class_init): * ext/swfdec/gstswfdec.c: (gst_swfdecbuffer_class_init), (gst_swfdec_class_init): * ext/tarkin/gsttarkindec.c: (gst_tarkindec_class_init): * ext/tarkin/gsttarkinenc.c: (gst_tarkinenc_class_init): * gst/cdxaparse/gstcdxastrip.c: (gst_cdxastrip_class_init): * gst/chart/gstchart.c: (gst_chart_class_init): * gst/colorspace/gstcolorspace.c: (gst_colorspace_class_init): * gst/deinterlace/gstdeinterlace.c: (gst_deinterlace_class_init): * gst/festival/gstfestival.c: (gst_festival_class_init): * gst/filter/gstbpwsinc.c: (gst_bpwsinc_class_init): * gst/filter/gstiir.c: (gst_iir_class_init): * gst/filter/gstlpwsinc.c: (gst_lpwsinc_class_init): * gst/librfb/gstrfbsrc.c: (gst_rfbsrc_class_init): * gst/mixmatrix/mixmatrix.c: (gst_mixmatrix_class_init): * gst/mpeg1sys/gstmpeg1systemencode.c: (gst_system_encode_class_init): * gst/mpeg1videoparse/gstmp1videoparse.c: (gst_mp1videoparse_class_init): * gst/mpeg2sub/gstmpeg2subt.c: (gst_mpeg2subt_class_init): * gst/mpegaudioparse/gstmpegaudioparse.c: (gst_mp3parse_class_init): * gst/overlay/gstoverlay.c: (gst_overlay_class_init): * gst/passthrough/gstpassthrough.c: (passthrough_class_init): * gst/playondemand/gstplayondemand.c: (play_on_demand_class_init): * gst/rtjpeg/gstrtjpegdec.c: (gst_rtjpegdec_class_init): * gst/rtjpeg/gstrtjpegenc.c: (gst_rtjpegenc_class_init): * gst/smooth/gstsmooth.c: (gst_smooth_class_init): * gst/smoothwave/gstsmoothwave.c: (gst_smoothwave_class_init): * gst/spectrum/gstspectrum.c: (gst_spectrum_class_init): * gst/stereo/gststereo.c: (gst_stereo_class_init): * gst/switch/gstswitch.c: (gst_switch_class_init): * gst/tta/gstttadec.c: (gst_tta_dec_class_init): * gst/tta/gstttaparse.c: (gst_tta_parse_class_init): * gst/vbidec/gstvbidec.c: (gst_vbidec_class_init): * gst/videocrop/gstvideocrop.c: (gst_video_crop_class_init): * gst/virtualdub/gstxsharpen.c: (gst_xsharpen_class_init): * gst/y4m/gsty4mencode.c: (gst_y4mencode_class_init): * sys/cdrom/gstcdplayer.c: (cdplayer_class_init): * sys/directsound/gstdirectsoundsink.c: (gst_directsoundsink_class_init): * sys/dxr3/dxr3audiosink.c: (dxr3audiosink_class_init): * sys/dxr3/dxr3spusink.c: (dxr3spusink_class_init): * sys/dxr3/dxr3videosink.c: (dxr3videosink_class_init): * sys/qcam/gstqcamsrc.c: (gst_qcamsrc_class_init): * sys/v4l2/gstv4l2colorbalance.c: (gst_v4l2_color_balance_channel_class_init): * sys/v4l2/gstv4l2tuner.c: (gst_v4l2_tuner_channel_class_init), (gst_v4l2_tuner_norm_class_init): * sys/ximagesrc/ximagesrc.c: (gst_ximagesrc_class_init): Fix #337365 (g_type_class_ref <-> g_type_class_peek_parent)
2006-04-08 21:48:01 +00:00
parent_class = g_type_class_peek_parent (klass);
2012-01-27 15:37:19 +00:00
gobject_class->finalize = gst_directsound_sink_finalize;
gobject_class->set_property = gst_directsound_sink_set_property;
gobject_class->get_property = gst_directsound_sink_get_property;
gstbasesink_class->get_caps =
GST_DEBUG_FUNCPTR (gst_directsound_sink_getcaps);
gstbasesink_class->query = GST_DEBUG_FUNCPTR (gst_directsound_sink_query);
gstaudiobasesink_class->payload =
GST_DEBUG_FUNCPTR (gst_directsound_sink_payload);
gstaudiosink_class->prepare =
GST_DEBUG_FUNCPTR (gst_directsound_sink_prepare);
gstaudiosink_class->unprepare =
GST_DEBUG_FUNCPTR (gst_directsound_sink_unprepare);
gstaudiosink_class->open = GST_DEBUG_FUNCPTR (gst_directsound_sink_open);
gstaudiosink_class->close = GST_DEBUG_FUNCPTR (gst_directsound_sink_close);
gstaudiosink_class->write = GST_DEBUG_FUNCPTR (gst_directsound_sink_write);
gstaudiosink_class->delay = GST_DEBUG_FUNCPTR (gst_directsound_sink_delay);
gstaudiosink_class->reset = GST_DEBUG_FUNCPTR (gst_directsound_sink_reset);
g_object_class_install_property (gobject_class,
PROP_VOLUME,
g_param_spec_double ("volume", "Volume",
"Volume of this stream", 0.0, 1.0, 1.0,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2012-01-27 15:37:19 +00:00
g_object_class_install_property (gobject_class,
PROP_MUTE,
g_param_spec_boolean ("mute", "Mute",
"Mute state of this stream", DEFAULT_MUTE,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
gst_element_class_set_static_metadata (element_class,
2012-01-27 15:37:19 +00:00
"Direct Sound Audio Sink", "Sink/Audio",
"Output to a sound card via Direct Sound",
"Sebastien Moutte <sebastien@moutte.net>");
gst_element_class_add_pad_template (element_class,
gst_static_pad_template_get (&directsoundsink_sink_factory));
}
static void
2012-01-27 15:37:19 +00:00
gst_directsound_sink_init (GstDirectSoundSink * dsoundsink)
{
2012-01-27 15:37:19 +00:00
dsoundsink->volume = 100;
dsoundsink->mute = FALSE;
dsoundsink->pDS = NULL;
dsoundsink->cached_caps = NULL;
dsoundsink->pDSBSecondary = NULL;
dsoundsink->current_circular_offset = 0;
dsoundsink->buffer_size = DSBSIZE_MIN;
docs/plugins/gst-plugins-bad-plugins.args: Remove directsoundsink property doc as this sink use the mixer interface now. Original commit message from CVS: * docs/plugins/gst-plugins-bad-plugins.args: Remove directsoundsink property doc as this sink use the mixer interface now. * docs/plugins/gst-plugins-bad-plugins.interfaces: Add interfaces implemented by Windows sinks. * sys/directsound/gstdirectsoundsink.c: * sys/directsound/gstdirectsoundsink.h: Remove directsoundsink property and implement the mixer interface. * win32/vs6/gst_plugins_bad.dsw: * win32/vs6/libgstdirectsound.dsp: Update project files. * gst-libs/gst/dshow/gstdshow.cpp: * gst-libs/gst/dshow/gstdshow.h: * gst-libs/gst/dshow/gstdshowfakesink.cpp: * gst-libs/gst/dshow/gstdshowfakesink.h: * gst-libs/gst/dshow/gstdshowfakesrc.cpp: * gst-libs/gst/dshow/gstdshowfakesrc.h: * gst-libs/gst/dshow/gstdshowinterface.cpp: * gst-libs/gst/dshow/gstdshowinterface.h: * win32/common/libgstdshow.def: * win32/vs6/libgstdshow.dsp: Add a new gst library which allow to create internal Direct Show graph (pipelines) to wrap Windows sources, decoders or encoders. It includes a DirectShow fake source and sink and utility functions. * sys/dshowsrcwrapper/gstdshowaudiosrc.c: * sys/dshowsrcwrapper/gstdshowaudiosrc.h: * sys/dshowsrcwrapper/gstdshowsrcwrapper.c: * sys/dshowsrcwrapper/gstdshowsrcwrapper.h: * sys/dshowsrcwrapper/gstdshowvideosrc.c: * sys/dshowsrcwrapper/gstdshowvideosrc.h: * win32/vs6/libdshowsrcwrapper.dsp: Add a new plugin to wrap DirectShow sources on Windows. It gets data from any webcam, dv cam, micro. We could add tv tunner card later.
2007-05-23 22:44:12 +00:00
dsoundsink->volume = 100;
dsoundsink->dsound_lock = g_mutex_new ();
dsoundsink->first_buffer_after_reset = FALSE;
}
static void
gst_directsound_sink_set_property (GObject * object,
guint prop_id, const GValue * value, GParamSpec * pspec)
{
GstDirectSoundSink *sink = GST_DIRECTSOUND_SINK (object);
switch (prop_id) {
case PROP_VOLUME:
2012-01-27 15:37:19 +00:00
gst_directsound_sink_set_volume (sink, g_value_get_double (value), TRUE);
break;
case PROP_MUTE:
gst_directsound_sink_set_mute (sink, g_value_get_boolean (value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
gst_directsound_sink_get_property (GObject * object,
guint prop_id, GValue * value, GParamSpec * pspec)
{
GstDirectSoundSink *sink = GST_DIRECTSOUND_SINK (object);
switch (prop_id) {
case PROP_VOLUME:
2012-01-27 15:37:19 +00:00
g_value_set_double (value, gst_directsound_sink_get_volume (sink));
break;
case PROP_MUTE:
g_value_set_boolean (value, gst_directsound_sink_get_mute (sink));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static GstCaps *
2012-01-27 15:37:19 +00:00
gst_directsound_sink_getcaps (GstBaseSink * bsink, GstCaps * filter)
{
GstElementClass *element_class;
GstPadTemplate *pad_template;
GstDirectSoundSink *dsoundsink = GST_DIRECTSOUND_SINK (bsink);
GstCaps *caps;
gchar *caps_string = NULL;
if (dsoundsink->pDS == NULL) {
GST_DEBUG_OBJECT (dsoundsink, "device not open, using template caps");
return NULL; /* base class will get template caps for us */
}
if (dsoundsink->cached_caps) {
caps_string = gst_caps_to_string (dsoundsink->cached_caps);
GST_DEBUG_OBJECT (dsoundsink, "Returning cached caps: %s", caps_string);
g_free (caps_string);
return gst_caps_ref (dsoundsink->cached_caps);
}
element_class = GST_ELEMENT_GET_CLASS (dsoundsink);
pad_template = gst_element_class_get_pad_template (element_class, "sink");
g_return_val_if_fail (pad_template != NULL, NULL);
caps = gst_directsound_probe_supported_formats (dsoundsink,
gst_pad_template_get_caps (pad_template));
if (caps) {
dsoundsink->cached_caps = gst_caps_ref (caps);
}
if (caps) {
gchar *caps_string = gst_caps_to_string (caps);
GST_DEBUG_OBJECT (dsoundsink, "returning caps %s", caps_string);
g_free (caps_string);
}
return caps;
}
static gboolean
gst_directsound_sink_acceptcaps (GstBaseSink * sink, GstQuery * query)
{
GstDirectSoundSink *dsink = GST_DIRECTSOUND_SINK (sink);
GstPad *pad;
GstCaps *caps;
GstCaps *pad_caps;
GstStructure *st;
gboolean ret = FALSE;
GstAudioRingBufferSpec spec = { 0 };
if (G_UNLIKELY (dsink == NULL))
return FALSE;
pad = sink->sinkpad;
gst_query_parse_accept_caps (query, &caps);
GST_DEBUG_OBJECT (pad, "caps %" GST_PTR_FORMAT, caps);
pad_caps = gst_pad_query_caps (pad, NULL);
if (pad_caps) {
2012-05-29 11:37:59 +00:00
gboolean cret = gst_caps_can_intersect (pad_caps, caps);
gst_caps_unref (pad_caps);
if (!cret) {
GST_DEBUG_OBJECT (dsink, "Can't intersect caps, not accepting caps");
goto done;
}
}
/* If we've not got fixed caps, creating a stream might fail, so let's just
* return from here with default acceptcaps behaviour */
if (!gst_caps_is_fixed (caps)) {
GST_DEBUG_OBJECT (dsink, "Caps are not fixed, not accepting caps");
goto done;
}
spec.latency_time = GST_SECOND;
if (!gst_audio_ring_buffer_parse_caps (&spec, caps)) {
GST_DEBUG_OBJECT (dsink, "Failed to parse caps, not accepting");
goto done;
}
/* Make sure input is framed (one frame per buffer) and can be payloaded */
switch (spec.type) {
2012-05-29 11:37:59 +00:00
case GST_AUDIO_RING_BUFFER_FORMAT_TYPE_AC3:
case GST_AUDIO_RING_BUFFER_FORMAT_TYPE_DTS:
{
gboolean framed = FALSE, parsed = FALSE;
st = gst_caps_get_structure (caps, 0);
gst_structure_get_boolean (st, "framed", &framed);
gst_structure_get_boolean (st, "parsed", &parsed);
if ((!framed && !parsed) || gst_audio_iec61937_frame_size (&spec) <= 0) {
GST_DEBUG_OBJECT (dsink, "Wrong AC3/DTS caps, not accepting");
goto done;
}
}
2012-05-29 11:37:59 +00:00
default:
break;
}
ret = TRUE;
GST_DEBUG_OBJECT (dsink, "Accepting caps");
done:
gst_query_set_accept_caps_result (query, ret);
return TRUE;
}
static gboolean
gst_directsound_sink_query (GstBaseSink * sink, GstQuery * query)
{
gboolean res = TRUE;
switch (GST_QUERY_TYPE (query)) {
case GST_QUERY_ACCEPT_CAPS:
res = gst_directsound_sink_acceptcaps (sink, query);
break;
default:
res = GST_BASE_SINK_CLASS (parent_class)->query (sink, query);
}
return res;
}
static gboolean
gst_directsound_sink_open (GstAudioSink * asink)
{
2012-01-27 15:37:19 +00:00
GstDirectSoundSink *dsoundsink;
HRESULT hRes;
2012-01-27 15:37:19 +00:00
dsoundsink = GST_DIRECTSOUND_SINK (asink);
/* create and initialize a DirecSound object */
if (FAILED (hRes = DirectSoundCreate (NULL, &dsoundsink->pDS, NULL))) {
GST_ELEMENT_ERROR (dsoundsink, RESOURCE, OPEN_READ,
("gst_directsound_sink_open: DirectSoundCreate: %s",
DXGetErrorString9 (hRes)), (NULL));
return FALSE;
}
if (FAILED (hRes = IDirectSound_SetCooperativeLevel (dsoundsink->pDS,
GetDesktopWindow (), DSSCL_PRIORITY))) {
GST_ELEMENT_ERROR (dsoundsink, RESOURCE, OPEN_READ,
("gst_directsound_sink_open: IDirectSound_SetCooperativeLevel: %s",
DXGetErrorString9 (hRes)), (NULL));
return FALSE;
}
return TRUE;
}
static gboolean
gst_directsound_sink_is_spdif_format (GstAudioRingBufferSpec * spec)
{
return spec->type == GST_AUDIO_RING_BUFFER_FORMAT_TYPE_AC3 ||
spec->type == GST_AUDIO_RING_BUFFER_FORMAT_TYPE_DTS;
}
static gboolean
2012-01-27 15:37:19 +00:00
gst_directsound_sink_prepare (GstAudioSink * asink,
GstAudioRingBufferSpec * spec)
{
2012-01-27 15:37:19 +00:00
GstDirectSoundSink *dsoundsink;
HRESULT hRes;
DSBUFFERDESC descSecondary;
WAVEFORMATEX wfx;
2012-01-27 15:37:19 +00:00
dsoundsink = GST_DIRECTSOUND_SINK (asink);
/*save number of bytes per sample and buffer format */
2012-01-27 15:37:19 +00:00
dsoundsink->bytes_per_sample = spec->info.bpf;
dsoundsink->type = spec->type;
/* fill the WAVEFORMATEX structure with spec params */
memset (&wfx, 0, sizeof (wfx));
if (!gst_directsound_sink_is_spdif_format (spec)) {
wfx.cbSize = sizeof (wfx);
wfx.wFormatTag = WAVE_FORMAT_PCM;
2012-01-27 15:37:19 +00:00
wfx.nChannels = spec->info.channels;
wfx.nSamplesPerSec = spec->info.rate;
wfx.wBitsPerSample = (spec->info.bpf * 8) / wfx.nChannels;
wfx.nBlockAlign = spec->info.bpf;
wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign;
2012-01-27 15:37:19 +00:00
/* Create directsound buffer with size based on our configured
* buffer_size (which is 200 ms by default) */
dsoundsink->buffer_size =
gst_util_uint64_scale_int (wfx.nAvgBytesPerSec, spec->buffer_time,
GST_MSECOND);
/* Make sure we make those numbers multiple of our sample size in bytes */
2012-01-27 15:37:19 +00:00
dsoundsink->buffer_size += dsoundsink->buffer_size % spec->info.bpf;
spec->segsize =
gst_util_uint64_scale_int (wfx.nAvgBytesPerSec, spec->latency_time,
GST_MSECOND);
2012-01-27 15:37:19 +00:00
spec->segsize += spec->segsize % spec->info.bpf;
spec->segtotal = dsoundsink->buffer_size / spec->segsize;
} else {
#ifdef WAVE_FORMAT_DOLBY_AC3_SPDIF
wfx.cbSize = 0;
wfx.wFormatTag = WAVE_FORMAT_DOLBY_AC3_SPDIF;
wfx.nChannels = 2;
wfx.nSamplesPerSec = 48000;
wfx.wBitsPerSample = 16;
wfx.nBlockAlign = wfx.wBitsPerSample / 8 * wfx.nChannels;
wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign;
spec->segsize = 6144;
spec->segtotal = 10;
#else
g_assert_not_reached ();
#endif
}
// Make the final buffer size be an integer number of segments
dsoundsink->buffer_size = spec->segsize * spec->segtotal;
GST_INFO_OBJECT (dsoundsink,
"GstAudioRingBufferSpec->channels: %d, GstAudioRingBufferSpec->rate: %d, GstAudioRingBufferSpec->bytes_per_sample: %d\n"
"WAVEFORMATEX.nSamplesPerSec: %ld, WAVEFORMATEX.wBitsPerSample: %d, WAVEFORMATEX.nBlockAlign: %d, WAVEFORMATEX.nAvgBytesPerSec: %ld\n"
2012-01-27 15:37:19 +00:00
"Size of dsound circular buffer=>%d\n", spec->info.channels,
spec->info.rate, spec->info.bpf, wfx.nSamplesPerSec, wfx.wBitsPerSample,
wfx.nBlockAlign, wfx.nAvgBytesPerSec, dsoundsink->buffer_size);
/* create a secondary directsound buffer */
memset (&descSecondary, 0, sizeof (DSBUFFERDESC));
descSecondary.dwSize = sizeof (DSBUFFERDESC);
descSecondary.dwFlags = DSBCAPS_GETCURRENTPOSITION2 | DSBCAPS_GLOBALFOCUS;
if (!gst_directsound_sink_is_spdif_format (spec))
descSecondary.dwFlags |= DSBCAPS_CTRLVOLUME;
descSecondary.dwBufferBytes = dsoundsink->buffer_size;
descSecondary.lpwfxFormat = (WAVEFORMATEX *) & wfx;
hRes = IDirectSound_CreateSoundBuffer (dsoundsink->pDS, &descSecondary,
&dsoundsink->pDSBSecondary, NULL);
if (FAILED (hRes)) {
GST_ELEMENT_ERROR (dsoundsink, RESOURCE, OPEN_READ,
("gst_directsound_sink_prepare: IDirectSound_CreateSoundBuffer: %s",
DXGetErrorString9 (hRes)), (NULL));
return FALSE;
}
2012-01-27 15:37:19 +00:00
gst_directsound_sink_set_volume (dsoundsink, dsoundsink->volume, FALSE);
return TRUE;
}
static gboolean
gst_directsound_sink_unprepare (GstAudioSink * asink)
{
GstDirectSoundSink *dsoundsink;
dsoundsink = GST_DIRECTSOUND_SINK (asink);
/* release secondary DirectSound buffer */
if (dsoundsink->pDSBSecondary) {
IDirectSoundBuffer_Release (dsoundsink->pDSBSecondary);
dsoundsink->pDSBSecondary = NULL;
}
return TRUE;
}
static gboolean
gst_directsound_sink_close (GstAudioSink * asink)
{
GstDirectSoundSink *dsoundsink = NULL;
dsoundsink = GST_DIRECTSOUND_SINK (asink);
/* release DirectSound object */
g_return_val_if_fail (dsoundsink->pDS != NULL, FALSE);
IDirectSound_Release (dsoundsink->pDS);
dsoundsink->pDS = NULL;
gst_caps_replace (&dsoundsink->cached_caps, NULL);
return TRUE;
}
2012-01-27 15:37:19 +00:00
static gint
gst_directsound_sink_write (GstAudioSink * asink, gpointer data, guint length)
{
GstDirectSoundSink *dsoundsink;
DWORD dwStatus;
HRESULT hRes;
LPVOID pLockedBuffer1 = NULL, pLockedBuffer2 = NULL;
DWORD dwSizeBuffer1, dwSizeBuffer2;
DWORD dwCurrentPlayCursor;
dsoundsink = GST_DIRECTSOUND_SINK (asink);
GST_DSOUND_LOCK (dsoundsink);
/* get current buffer status */
hRes = IDirectSoundBuffer_GetStatus (dsoundsink->pDSBSecondary, &dwStatus);
/* get current play cursor position */
hRes = IDirectSoundBuffer_GetCurrentPosition (dsoundsink->pDSBSecondary,
&dwCurrentPlayCursor, NULL);
if (SUCCEEDED (hRes) && (dwStatus & DSBSTATUS_PLAYING)) {
DWORD dwFreeBufferSize;
calculate_freesize:
/* calculate the free size of the circular buffer */
if (dwCurrentPlayCursor < dsoundsink->current_circular_offset)
dwFreeBufferSize =
dsoundsink->buffer_size - (dsoundsink->current_circular_offset -
dwCurrentPlayCursor);
else
dwFreeBufferSize =
dwCurrentPlayCursor - dsoundsink->current_circular_offset;
if (length >= dwFreeBufferSize) {
Sleep (100);
hRes = IDirectSoundBuffer_GetCurrentPosition (dsoundsink->pDSBSecondary,
&dwCurrentPlayCursor, NULL);
hRes =
IDirectSoundBuffer_GetStatus (dsoundsink->pDSBSecondary, &dwStatus);
if (SUCCEEDED (hRes) && (dwStatus & DSBSTATUS_PLAYING))
goto calculate_freesize;
else {
dsoundsink->first_buffer_after_reset = FALSE;
GST_DSOUND_UNLOCK (dsoundsink);
return 0;
}
}
}
if (dwStatus & DSBSTATUS_BUFFERLOST) {
hRes = IDirectSoundBuffer_Restore (dsoundsink->pDSBSecondary); /*need a loop waiting the buffer is restored?? */
dsoundsink->current_circular_offset = 0;
}
hRes = IDirectSoundBuffer_Lock (dsoundsink->pDSBSecondary,
dsoundsink->current_circular_offset, length, &pLockedBuffer1,
&dwSizeBuffer1, &pLockedBuffer2, &dwSizeBuffer2, 0L);
if (SUCCEEDED (hRes)) {
// Write to pointers without reordering.
memcpy (pLockedBuffer1, data, dwSizeBuffer1);
if (pLockedBuffer2 != NULL)
memcpy (pLockedBuffer2, (LPBYTE) data + dwSizeBuffer1, dwSizeBuffer2);
// Update where the buffer will lock (for next time)
dsoundsink->current_circular_offset += dwSizeBuffer1 + dwSizeBuffer2;
dsoundsink->current_circular_offset %= dsoundsink->buffer_size; /* Circular buffer */
hRes = IDirectSoundBuffer_Unlock (dsoundsink->pDSBSecondary, pLockedBuffer1,
dwSizeBuffer1, pLockedBuffer2, dwSizeBuffer2);
}
/* if the buffer was not in playing state yet, call play on the buffer
except if this buffer is the fist after a reset (base class call reset and write a buffer when setting the sink to pause) */
if (!(dwStatus & DSBSTATUS_PLAYING) &&
dsoundsink->first_buffer_after_reset == FALSE) {
hRes = IDirectSoundBuffer_Play (dsoundsink->pDSBSecondary, 0, 0,
DSBPLAY_LOOPING);
}
dsoundsink->first_buffer_after_reset = FALSE;
GST_DSOUND_UNLOCK (dsoundsink);
return length;
}
static guint
gst_directsound_sink_delay (GstAudioSink * asink)
{
GstDirectSoundSink *dsoundsink;
HRESULT hRes;
DWORD dwCurrentPlayCursor;
DWORD dwBytesInQueue = 0;
gint nNbSamplesInQueue = 0;
DWORD dwStatus;
dsoundsink = GST_DIRECTSOUND_SINK (asink);
/* get current buffer status */
hRes = IDirectSoundBuffer_GetStatus (dsoundsink->pDSBSecondary, &dwStatus);
if (dwStatus & DSBSTATUS_PLAYING) {
/*evaluate the number of samples in queue in the circular buffer */
hRes = IDirectSoundBuffer_GetCurrentPosition (dsoundsink->pDSBSecondary,
&dwCurrentPlayCursor, NULL);
if (hRes == S_OK) {
if (dwCurrentPlayCursor < dsoundsink->current_circular_offset)
dwBytesInQueue =
dsoundsink->current_circular_offset - dwCurrentPlayCursor;
else
dwBytesInQueue =
dsoundsink->current_circular_offset + (dsoundsink->buffer_size -
dwCurrentPlayCursor);
nNbSamplesInQueue = dwBytesInQueue / dsoundsink->bytes_per_sample;
}
}
return nNbSamplesInQueue;
}
static void
gst_directsound_sink_reset (GstAudioSink * asink)
{
GstDirectSoundSink *dsoundsink;
LPVOID pLockedBuffer = NULL;
DWORD dwSizeBuffer = 0;
dsoundsink = GST_DIRECTSOUND_SINK (asink);
GST_DSOUND_LOCK (dsoundsink);
if (dsoundsink->pDSBSecondary) {
/*stop playing */
HRESULT hRes = IDirectSoundBuffer_Stop (dsoundsink->pDSBSecondary);
/*reset position */
hRes = IDirectSoundBuffer_SetCurrentPosition (dsoundsink->pDSBSecondary, 0);
dsoundsink->current_circular_offset = 0;
/*reset the buffer */
hRes = IDirectSoundBuffer_Lock (dsoundsink->pDSBSecondary,
dsoundsink->current_circular_offset, dsoundsink->buffer_size,
&pLockedBuffer, &dwSizeBuffer, NULL, NULL, 0L);
if (SUCCEEDED (hRes)) {
memset (pLockedBuffer, 0, dwSizeBuffer);
hRes =
IDirectSoundBuffer_Unlock (dsoundsink->pDSBSecondary, pLockedBuffer,
dwSizeBuffer, NULL, 0);
}
}
dsoundsink->first_buffer_after_reset = TRUE;
GST_DSOUND_UNLOCK (dsoundsink);
}
2012-01-27 15:37:19 +00:00
/*
* gst_directsound_probe_supported_formats:
*
* Takes the template caps and returns the subset which is actually
* supported by this device.
*
*/
static GstCaps *
gst_directsound_probe_supported_formats (GstDirectSoundSink * dsoundsink,
const GstCaps * template_caps)
{
HRESULT hRes;
DSBUFFERDESC descSecondary;
WAVEFORMATEX wfx;
GstCaps *caps;
2012-06-13 10:12:39 +00:00
GstCaps *tmp, *tmp2;
LPDIRECTSOUNDBUFFER tmpBuffer;
caps = gst_caps_copy (template_caps);
2012-01-27 15:37:19 +00:00
/*
* Check availability of digital output by trying to create an SPDIF buffer
*/
#ifdef WAVE_FORMAT_DOLBY_AC3_SPDIF
/* fill the WAVEFORMATEX structure with some standard AC3 over SPDIF params */
memset (&wfx, 0, sizeof (wfx));
wfx.cbSize = 0;
wfx.wFormatTag = WAVE_FORMAT_DOLBY_AC3_SPDIF;
wfx.nChannels = 2;
wfx.nSamplesPerSec = 48000;
wfx.wBitsPerSample = 16;
wfx.nBlockAlign = 4;
wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign;
2012-01-27 15:37:19 +00:00
// create a secondary directsound buffer
memset (&descSecondary, 0, sizeof (DSBUFFERDESC));
descSecondary.dwSize = sizeof (DSBUFFERDESC);
descSecondary.dwFlags = DSBCAPS_GETCURRENTPOSITION2 | DSBCAPS_GLOBALFOCUS;
descSecondary.dwBufferBytes = 6144;
descSecondary.lpwfxFormat = &wfx;
hRes = IDirectSound_CreateSoundBuffer (dsoundsink->pDS, &descSecondary,
&tmpBuffer, NULL);
if (FAILED (hRes)) {
GST_INFO_OBJECT (dsoundsink, "AC3 passthrough not supported "
"(IDirectSound_CreateSoundBuffer returned: %s)\n",
DXGetErrorString9 (hRes));
2012-06-13 10:12:39 +00:00
tmp = gst_caps_new_empty_simple ("audio/x-ac3");
tmp2 = gst_caps_subtract (caps, tmp);
gst_caps_unref (tmp);
gst_caps_unref (caps);
caps = tmp2;
tmp = gst_caps_new_empty_simple ("audio/x-dts");
tmp2 = gst_caps_subtract (caps, tmp);
gst_caps_unref (tmp);
gst_caps_unref (caps);
caps = tmp2;
} else {
GST_INFO_OBJECT (dsoundsink, "AC3 passthrough supported");
hRes = IDirectSoundBuffer_Release (tmpBuffer);
if (FAILED (hRes)) {
GST_DEBUG_OBJECT (dsoundsink,
"(IDirectSoundBuffer_Release returned: %s)\n",
DXGetErrorString9 (hRes));
}
}
#else
2012-06-13 10:12:39 +00:00
tmp = gst_caps_new_empty_simple ("audio/x-ac3");
tmp2 = gst_caps_subtract (caps, tmp);
gst_caps_unref (tmp);
gst_caps_unref (caps);
caps = tmp2;
tmp = gst_caps_new_empty_simple ("audio/x-dts");
tmp2 = gst_caps_subtract (caps, tmp);
gst_caps_unref (tmp);
gst_caps_unref (caps);
caps = tmp2;
#endif
return caps;
}
2012-01-27 15:37:19 +00:00
static GstBuffer *
gst_directsound_sink_payload (GstAudioBaseSink * sink, GstBuffer * buf)
{
if (gst_directsound_sink_is_spdif_format (&sink->ringbuffer->spec)) {
gint framesize = gst_audio_iec61937_frame_size (&sink->ringbuffer->spec);
GstBuffer *out;
GstMapInfo infobuf, infoout;
gboolean success;
if (framesize <= 0)
return NULL;
out = gst_buffer_new_and_alloc (framesize);
if (!gst_buffer_map (buf, &infobuf, GST_MAP_READWRITE)) {
gst_buffer_unref (out);
return NULL;
}
if (!gst_buffer_map (out, &infoout, GST_MAP_READWRITE)) {
gst_buffer_unmap (buf, &infobuf);
gst_buffer_unref (out);
return NULL;
}
success = gst_audio_iec61937_payload (infobuf.data, infobuf.size,
infoout.data, infoout.size, &sink->ringbuffer->spec);
if (!success) {
gst_buffer_unmap (out, &infoout);
gst_buffer_unmap (buf, &infobuf);
gst_buffer_unref (out);
return NULL;
}
gst_buffer_copy_into (out, buf, GST_BUFFER_COPY_ALL, 0, -1);
/* Fix endianness */
_swab ((gchar *) infoout.data, (gchar *) infoout.data, infobuf.size);
gst_buffer_unmap (out, &infoout);
gst_buffer_unmap (buf, &infobuf);
return out;
} else
return gst_buffer_ref (buf);
}
2012-01-27 15:37:19 +00:00
static void
gst_directsound_sink_set_volume (GstDirectSoundSink * dsoundsink,
gdouble dvolume, gboolean store)
{
glong volume;
volume = dvolume * 100;
if (store)
dsoundsink->volume = volume;
if (dsoundsink->pDSBSecondary) {
/* DirectSound controls volume using units of 100th of a decibel,
* ranging from -10000 to 0. We use a linear scale of 0 - 100
* here, so remap.
*/
long dsVolume;
if (dsoundsink->volume == 0)
dsVolume = -10000;
else
dsVolume = 100 * (long) (20 * log10 ((double) dsoundsink->volume / 100.));
dsVolume = CLAMP (dsVolume, -10000, 0);
GST_DEBUG_OBJECT (dsoundsink,
"Setting volume on secondary buffer to %d from %d", (int) dsVolume,
(int) dsoundsink->volume);
IDirectSoundBuffer_SetVolume (dsoundsink->pDSBSecondary, dsVolume);
}
}
gdouble
gst_directsound_sink_get_volume (GstDirectSoundSink * dsoundsink)
{
return (gdouble) dsoundsink->volume / 100;
}
static void
gst_directsound_sink_set_mute (GstDirectSoundSink * dsoundsink, gboolean mute)
{
if (mute)
gst_directsound_sink_set_volume (dsoundsink, 0, FALSE);
else
gst_directsound_sink_set_volume (dsoundsink, dsoundsink->volume, FALSE);
}
static gboolean
gst_directsound_sink_get_mute (GstDirectSoundSink * dsoundsink)
{
return FALSE;
}