2006-01-05 23:17:44 +00:00
|
|
|
/* GStreamer
|
|
|
|
* Copyright (C) 2005 Sebastien Moutte <sebastien@moutte.net>
|
2007-02-27 12:02:03 +00:00
|
|
|
* Copyright (C) 2007 Pioneers of the Inevitable <songbird@songbirdnest.com>
|
2010-03-26 22:20:10 +00:00
|
|
|
* Copyright (C) 2010 Fluendo S.A. <support@fluendo.com>
|
2006-01-05 23:17:44 +00:00
|
|
|
*
|
|
|
|
* 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
|
2012-11-04 00:07:18 +00:00
|
|
|
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
2007-02-27 12:02:03 +00:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* The development of this code was made possible due to the involvement
|
|
|
|
* of Pioneers of the Inevitable, the creators of the Songbird Music player
|
|
|
|
*
|
2006-01-05 23:17:44 +00:00
|
|
|
*/
|
|
|
|
|
2007-02-18 18:00:51 +00:00
|
|
|
/**
|
2007-02-20 21:34:00 +00:00
|
|
|
* SECTION:element-directsoundsink
|
2007-02-18 18:00:51 +00:00
|
|
|
*
|
|
|
|
* This element lets you output sound using the DirectSound API.
|
2009-01-29 08:10:08 +00:00
|
|
|
*
|
2007-02-18 18:00:51 +00:00
|
|
|
* 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).
|
2009-01-29 08:10:08 +00:00
|
|
|
*
|
|
|
|
* <refsect2>
|
2007-02-18 18:00:51 +00:00
|
|
|
* <title>Example pipelines</title>
|
2009-01-29 08:10:08 +00:00
|
|
|
* |[
|
2012-08-26 21:39:55 +00:00
|
|
|
* gst-launch-1.0 -v audiotestsrc ! audioconvert ! volume volume=0.1 ! directsoundsink
|
2009-01-29 08:10:08 +00:00
|
|
|
* ]| will output a sine wave (continuous beep sound) to your sound card (with
|
2007-02-18 18:00:51 +00:00
|
|
|
* a very low volume as precaution).
|
2009-01-29 08:10:08 +00:00
|
|
|
* |[
|
2012-08-26 21:39:55 +00:00
|
|
|
* gst-launch-1.0 -v filesrc location=music.ogg ! decodebin ! audioconvert ! audioresample ! directsoundsink
|
2009-01-29 08:10:08 +00:00
|
|
|
* ]| will play an Ogg/Vorbis audio file and output it.
|
2007-02-18 18:00:51 +00:00
|
|
|
* </refsect2>
|
|
|
|
*/
|
|
|
|
|
2006-01-05 23:17:44 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2012-01-27 15:37:19 +00:00
|
|
|
#include <gst/base/gstbasesink.h>
|
2006-01-05 23:17:44 +00:00
|
|
|
#include "gstdirectsoundsink.h"
|
2012-07-04 13:42:49 +00:00
|
|
|
#include <gst/audio/gstaudioiec61937.h>
|
2006-01-05 23:17:44 +00:00
|
|
|
|
2008-10-01 21:22:26 +00:00
|
|
|
#include <math.h>
|
|
|
|
|
2011-02-01 18:40:58 +00:00
|
|
|
#ifdef __CYGWIN__
|
|
|
|
#include <unistd.h>
|
|
|
|
#ifndef _swab
|
|
|
|
#define _swab swab
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2012-01-27 15:37:19 +00:00
|
|
|
#define DEFAULT_MUTE FALSE
|
|
|
|
|
2006-01-05 23:17:44 +00:00
|
|
|
GST_DEBUG_CATEGORY_STATIC (directsoundsink_debug);
|
2010-03-26 22:20:10 +00:00
|
|
|
#define GST_CAT_DEFAULT directsoundsink_debug
|
|
|
|
|
2012-01-27 15:37:19 +00:00
|
|
|
static void gst_directsound_sink_finalize (GObject * object);
|
2006-01-05 23:17:44 +00:00
|
|
|
|
2008-10-01 21:22:26 +00:00
|
|
|
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);
|
2012-07-04 13:42:49 +00:00
|
|
|
static GstBuffer *gst_directsound_sink_payload (GstAudioBaseSink * sink,
|
|
|
|
GstBuffer * buf);
|
2007-02-18 18:00:51 +00:00
|
|
|
static gboolean gst_directsound_sink_prepare (GstAudioSink * asink,
|
2012-01-27 15:37:19 +00:00
|
|
|
GstAudioRingBufferSpec * spec);
|
2007-02-18 18:00:51 +00:00
|
|
|
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);
|
2007-02-18 18:00:51 +00:00
|
|
|
static guint gst_directsound_sink_delay (GstAudioSink * asink);
|
|
|
|
static void gst_directsound_sink_reset (GstAudioSink * asink);
|
2010-03-26 22:20:10 +00:00
|
|
|
static GstCaps *gst_directsound_probe_supported_formats (GstDirectSoundSink *
|
|
|
|
dsoundsink, const GstCaps * template_caps);
|
2012-07-27 14:13:49 +00:00
|
|
|
static gboolean gst_directsound_sink_query (GstBaseSink * pad,
|
|
|
|
GstQuery * query);
|
2006-01-05 23:17:44 +00:00
|
|
|
|
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);
|
2015-08-16 19:21:51 +00:00
|
|
|
static const gchar *gst_directsound_sink_get_device (GstDirectSoundSink *
|
|
|
|
dsoundsink);
|
|
|
|
static void gst_directsound_sink_set_device (GstDirectSoundSink * dsoundsink,
|
|
|
|
const gchar * device_id);
|
2006-01-05 23:17:44 +00:00
|
|
|
|
2012-07-04 15:41:52 +00:00
|
|
|
static gboolean gst_directsound_sink_is_spdif_format (GstAudioRingBufferSpec *
|
|
|
|
spec);
|
2012-05-25 10:14:57 +00:00
|
|
|
|
2006-01-05 23:17:44 +00:00
|
|
|
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, "
|
2006-01-05 23:17:44 +00:00
|
|
|
"rate = (int) [ 1, MAX ], " "channels = (int) [ 1, 2 ]; "
|
2012-01-27 15:37:19 +00:00
|
|
|
"audio/x-raw, "
|
2013-08-21 14:32:50 +00:00
|
|
|
"format = (string) U8, "
|
2012-01-27 15:37:19 +00:00
|
|
|
"layout = (string) interleaved, "
|
2010-03-26 22:20:10 +00:00
|
|
|
"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;"));
|
2006-01-05 23:17:44 +00:00
|
|
|
|
2008-10-01 21:22:26 +00:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
PROP_0,
|
2012-01-27 15:37:19 +00:00
|
|
|
PROP_VOLUME,
|
2015-08-16 19:21:51 +00:00
|
|
|
PROP_MUTE,
|
|
|
|
PROP_DEVICE
|
2008-10-01 21:22:26 +00:00
|
|
|
};
|
|
|
|
|
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)
|
|
|
|
);
|
2007-05-23 22:44:12 +00:00
|
|
|
|
|
|
|
static void
|
2012-01-27 15:37:19 +00:00
|
|
|
gst_directsound_sink_finalize (GObject * object)
|
2007-05-23 22:44:12 +00:00
|
|
|
{
|
2012-09-11 10:36:56 +00:00
|
|
|
GstDirectSoundSink *dsoundsink = GST_DIRECTSOUND_SINK (object);
|
|
|
|
|
2015-08-16 19:21:51 +00:00
|
|
|
g_free (dsoundsink->device_id);
|
|
|
|
dsoundsink->device_id = NULL;
|
|
|
|
|
2012-09-11 10:36:56 +00:00
|
|
|
g_mutex_clear (&dsoundsink->dsound_lock);
|
|
|
|
|
2006-07-24 21:43:06 +00:00
|
|
|
G_OBJECT_CLASS (parent_class)->finalize (object);
|
|
|
|
}
|
|
|
|
|
2006-01-05 23:17:44 +00:00
|
|
|
static void
|
2007-02-18 18:00:51 +00:00
|
|
|
gst_directsound_sink_class_init (GstDirectSoundSinkClass * klass)
|
2006-01-05 23:17:44 +00:00
|
|
|
{
|
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);
|
2012-07-27 14:13:49 +00:00
|
|
|
GstAudioBaseSinkClass *gstaudiobasesink_class =
|
|
|
|
GST_AUDIO_BASE_SINK_CLASS (klass);
|
2012-01-27 15:37:19 +00:00
|
|
|
GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
|
2006-01-05 23:17:44 +00:00
|
|
|
|
2007-05-23 22:44:12 +00:00
|
|
|
GST_DEBUG_CATEGORY_INIT (directsoundsink_debug, "directsoundsink", 0,
|
|
|
|
"DirectSound sink");
|
|
|
|
|
2012-01-27 15:37:19 +00:00
|
|
|
gobject_class->finalize = gst_directsound_sink_finalize;
|
2010-06-06 15:57:03 +00:00
|
|
|
gobject_class->set_property = gst_directsound_sink_set_property;
|
|
|
|
gobject_class->get_property = gst_directsound_sink_get_property;
|
2006-01-05 23:17:44 +00:00
|
|
|
|
2007-02-18 18:00:51 +00:00
|
|
|
gstbasesink_class->get_caps =
|
|
|
|
GST_DEBUG_FUNCPTR (gst_directsound_sink_getcaps);
|
2006-01-05 23:17:44 +00:00
|
|
|
|
2012-07-27 14:13:49 +00:00
|
|
|
gstbasesink_class->query = GST_DEBUG_FUNCPTR (gst_directsound_sink_query);
|
2012-07-04 13:42:49 +00:00
|
|
|
|
|
|
|
gstaudiobasesink_class->payload =
|
|
|
|
GST_DEBUG_FUNCPTR (gst_directsound_sink_payload);
|
|
|
|
|
2007-02-18 18:00:51 +00:00
|
|
|
gstaudiosink_class->prepare =
|
|
|
|
GST_DEBUG_FUNCPTR (gst_directsound_sink_prepare);
|
2006-01-05 23:17:44 +00:00
|
|
|
gstaudiosink_class->unprepare =
|
2007-02-18 18:00:51 +00:00
|
|
|
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);
|
2008-10-01 21:22:26 +00:00
|
|
|
|
|
|
|
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));
|
|
|
|
|
2015-08-16 19:21:51 +00:00
|
|
|
g_object_class_install_property (gobject_class,
|
|
|
|
PROP_DEVICE,
|
|
|
|
g_param_spec_string ("device", "Device",
|
|
|
|
"DirectSound playback device as a GUID string",
|
|
|
|
NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
|
|
|
|
|
2012-04-09 23:51:41 +00:00
|
|
|
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>");
|
|
|
|
|
2016-03-04 01:30:12 +00:00
|
|
|
gst_element_class_add_static_pad_template (element_class,
|
|
|
|
&directsoundsink_sink_factory);
|
2006-01-05 23:17:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-01-27 15:37:19 +00:00
|
|
|
gst_directsound_sink_init (GstDirectSoundSink * dsoundsink)
|
2006-01-05 23:17:44 +00:00
|
|
|
{
|
2012-01-27 15:37:19 +00:00
|
|
|
dsoundsink->volume = 100;
|
|
|
|
dsoundsink->mute = FALSE;
|
2015-08-16 19:21:51 +00:00
|
|
|
dsoundsink->device_id = NULL;
|
2006-01-05 23:17:44 +00:00
|
|
|
dsoundsink->pDS = NULL;
|
2010-03-26 22:20:10 +00:00
|
|
|
dsoundsink->cached_caps = NULL;
|
2006-01-05 23:17:44 +00:00
|
|
|
dsoundsink->pDSBSecondary = NULL;
|
|
|
|
dsoundsink->current_circular_offset = 0;
|
|
|
|
dsoundsink->buffer_size = DSBSIZE_MIN;
|
2007-05-23 22:44:12 +00:00
|
|
|
dsoundsink->volume = 100;
|
2012-09-11 10:36:56 +00:00
|
|
|
g_mutex_init (&dsoundsink->dsound_lock);
|
2006-07-24 21:43:06 +00:00
|
|
|
dsoundsink->first_buffer_after_reset = FALSE;
|
2006-01-05 23:17:44 +00:00
|
|
|
}
|
|
|
|
|
2008-10-01 21:22:26 +00:00
|
|
|
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));
|
2008-10-01 21:22:26 +00:00
|
|
|
break;
|
2015-08-16 19:21:51 +00:00
|
|
|
case PROP_DEVICE:
|
|
|
|
gst_directsound_sink_set_device (sink, g_value_get_string (value));
|
|
|
|
break;
|
2008-10-01 21:22:26 +00:00
|
|
|
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));
|
2008-10-01 21:22:26 +00:00
|
|
|
break;
|
2015-08-16 19:21:51 +00:00
|
|
|
case PROP_DEVICE:
|
|
|
|
g_value_set_string (value, gst_directsound_sink_get_device (sink));
|
|
|
|
break;
|
2008-10-01 21:22:26 +00:00
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-01-05 23:17:44 +00:00
|
|
|
static GstCaps *
|
2012-01-27 15:37:19 +00:00
|
|
|
gst_directsound_sink_getcaps (GstBaseSink * bsink, GstCaps * filter)
|
2006-01-05 23:17:44 +00:00
|
|
|
{
|
2010-03-26 22:20:10 +00:00
|
|
|
GstElementClass *element_class;
|
|
|
|
GstPadTemplate *pad_template;
|
|
|
|
GstDirectSoundSink *dsoundsink = GST_DIRECTSOUND_SINK (bsink);
|
|
|
|
GstCaps *caps;
|
|
|
|
|
|
|
|
if (dsoundsink->pDS == NULL) {
|
|
|
|
GST_DEBUG_OBJECT (dsoundsink, "device not open, using template caps");
|
|
|
|
return NULL; /* base class will get template caps for us */
|
|
|
|
}
|
2006-01-05 23:17:44 +00:00
|
|
|
|
2010-03-26 22:20:10 +00:00
|
|
|
if (dsoundsink->cached_caps) {
|
2013-03-26 13:39:53 +00:00
|
|
|
caps = gst_caps_ref (dsoundsink->cached_caps);
|
|
|
|
} else {
|
|
|
|
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);
|
2010-03-26 22:20:10 +00:00
|
|
|
}
|
|
|
|
|
2013-03-26 13:39:53 +00:00
|
|
|
if (caps && filter) {
|
|
|
|
GstCaps *tmp =
|
|
|
|
gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
|
|
|
|
gst_caps_unref (caps);
|
|
|
|
caps = tmp;
|
2010-03-26 22:20:10 +00:00
|
|
|
}
|
|
|
|
|
2010-06-09 18:07:09 +00:00
|
|
|
if (caps) {
|
|
|
|
gchar *caps_string = gst_caps_to_string (caps);
|
|
|
|
GST_DEBUG_OBJECT (dsoundsink, "returning caps %s", caps_string);
|
|
|
|
g_free (caps_string);
|
|
|
|
}
|
2010-03-26 22:20:10 +00:00
|
|
|
|
|
|
|
return caps;
|
2006-01-05 23:17:44 +00:00
|
|
|
}
|
|
|
|
|
2012-07-04 13:42:49 +00:00
|
|
|
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) {
|
2013-03-26 13:44:36 +00:00
|
|
|
gboolean cret = gst_caps_is_subset (caps, pad_caps);
|
2012-07-04 13:42:49 +00:00
|
|
|
gst_caps_unref (pad_caps);
|
2012-07-27 14:13:49 +00:00
|
|
|
if (!cret) {
|
2013-03-26 13:44:36 +00:00
|
|
|
GST_DEBUG_OBJECT (dsink,
|
|
|
|
"Caps are not a subset of the pad caps, not accepting caps");
|
2012-07-04 13:42:49 +00:00
|
|
|
goto done;
|
2012-07-04 16:59:03 +00:00
|
|
|
}
|
2012-07-04 13:42:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* If we've not got fixed caps, creating a stream might fail, so let's just
|
|
|
|
* return from here with default acceptcaps behaviour */
|
2012-07-27 14:13:49 +00:00
|
|
|
if (!gst_caps_is_fixed (caps)) {
|
2012-07-04 16:59:03 +00:00
|
|
|
GST_DEBUG_OBJECT (dsink, "Caps are not fixed, not accepting caps");
|
2012-07-04 13:42:49 +00:00
|
|
|
goto done;
|
2012-07-04 16:59:03 +00:00
|
|
|
}
|
2012-07-04 13:42:49 +00:00
|
|
|
|
2012-07-04 15:41:52 +00:00
|
|
|
spec.latency_time = GST_SECOND;
|
2012-07-27 14:13:49 +00:00
|
|
|
if (!gst_audio_ring_buffer_parse_caps (&spec, caps)) {
|
2012-07-04 16:59:03 +00:00
|
|
|
GST_DEBUG_OBJECT (dsink, "Failed to parse caps, not accepting");
|
2012-07-04 13:42:49 +00:00
|
|
|
goto done;
|
2012-07-04 16:59:03 +00:00
|
|
|
}
|
2012-07-04 13:42:49 +00:00
|
|
|
|
|
|
|
/* Make sure input is framed (one frame per buffer) and can be payloaded */
|
2012-07-27 14:13:49 +00:00
|
|
|
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:
|
2012-07-04 13:42:49 +00:00
|
|
|
{
|
|
|
|
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);
|
2012-07-27 14:13:49 +00:00
|
|
|
if ((!framed && !parsed) || gst_audio_iec61937_frame_size (&spec) <= 0) {
|
2012-07-04 16:59:03 +00:00
|
|
|
GST_DEBUG_OBJECT (dsink, "Wrong AC3/DTS caps, not accepting");
|
2012-07-04 13:42:49 +00:00
|
|
|
goto done;
|
2012-07-04 16:59:03 +00:00
|
|
|
}
|
2012-07-04 13:42:49 +00:00
|
|
|
}
|
2012-05-29 11:37:59 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2012-07-04 13:42:49 +00:00
|
|
|
ret = TRUE;
|
2012-07-04 16:59:03 +00:00
|
|
|
GST_DEBUG_OBJECT (dsink, "Accepting caps");
|
2012-07-04 13:42:49 +00:00
|
|
|
|
|
|
|
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:
|
2012-07-04 16:59:03 +00:00
|
|
|
res = GST_BASE_SINK_CLASS (parent_class)->query (sink, query);
|
2012-07-04 13:42:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2015-08-16 19:21:51 +00:00
|
|
|
static LPGUID
|
|
|
|
string_to_guid (const gchar * str)
|
|
|
|
{
|
|
|
|
HRESULT ret;
|
|
|
|
gunichar2 *wstr;
|
|
|
|
LPGUID out;
|
|
|
|
|
|
|
|
wstr = g_utf8_to_utf16 (str, -1, NULL, NULL, NULL);
|
|
|
|
if (!wstr)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
out = g_new (GUID, 1);
|
|
|
|
ret = CLSIDFromString ((LPOLESTR) wstr, out);
|
|
|
|
g_free (wstr);
|
|
|
|
if (ret != NOERROR) {
|
|
|
|
g_free (out);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2006-01-05 23:17:44 +00:00
|
|
|
static gboolean
|
2007-02-18 18:00:51 +00:00
|
|
|
gst_directsound_sink_open (GstAudioSink * asink)
|
2006-01-05 23:17:44 +00:00
|
|
|
{
|
2012-01-27 15:37:19 +00:00
|
|
|
GstDirectSoundSink *dsoundsink;
|
2006-01-05 23:17:44 +00:00
|
|
|
HRESULT hRes;
|
2015-08-16 19:21:51 +00:00
|
|
|
LPGUID lpGuid = NULL;
|
2006-01-05 23:17:44 +00:00
|
|
|
|
2012-01-27 15:37:19 +00:00
|
|
|
dsoundsink = GST_DIRECTSOUND_SINK (asink);
|
|
|
|
|
2015-12-14 13:55:12 +00:00
|
|
|
if (dsoundsink->device_id) {
|
2015-08-16 19:21:51 +00:00
|
|
|
lpGuid = string_to_guid (dsoundsink->device_id);
|
2015-12-14 13:55:12 +00:00
|
|
|
if (lpGuid == NULL) {
|
|
|
|
GST_ELEMENT_ERROR (dsoundsink, RESOURCE, OPEN_READ,
|
|
|
|
("gst_directsound_sink_open: device set, but guid not found: %s",
|
|
|
|
dsoundsink->device_id), (NULL));
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
2015-08-16 19:21:51 +00:00
|
|
|
|
2006-01-05 23:17:44 +00:00
|
|
|
/* create and initialize a DirecSound object */
|
2015-08-16 19:21:51 +00:00
|
|
|
if (FAILED (hRes = DirectSoundCreate (lpGuid, &dsoundsink->pDS, NULL))) {
|
2006-01-05 23:17:44 +00:00
|
|
|
GST_ELEMENT_ERROR (dsoundsink, RESOURCE, OPEN_READ,
|
2007-02-18 18:00:51 +00:00
|
|
|
("gst_directsound_sink_open: DirectSoundCreate: %s",
|
2006-01-05 23:17:44 +00:00
|
|
|
DXGetErrorString9 (hRes)), (NULL));
|
2015-08-16 19:21:51 +00:00
|
|
|
g_free (lpGuid);
|
2006-01-05 23:17:44 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2015-08-16 19:21:51 +00:00
|
|
|
g_free (lpGuid);
|
|
|
|
|
2006-03-03 23:45:23 +00:00
|
|
|
if (FAILED (hRes = IDirectSound_SetCooperativeLevel (dsoundsink->pDS,
|
2006-01-05 23:17:44 +00:00
|
|
|
GetDesktopWindow (), DSSCL_PRIORITY))) {
|
|
|
|
GST_ELEMENT_ERROR (dsoundsink, RESOURCE, OPEN_READ,
|
2007-02-18 18:00:51 +00:00
|
|
|
("gst_directsound_sink_open: IDirectSound_SetCooperativeLevel: %s",
|
2006-01-05 23:17:44 +00:00
|
|
|
DXGetErrorString9 (hRes)), (NULL));
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2012-05-25 10:14:57 +00:00
|
|
|
static gboolean
|
2012-07-04 15:41:52 +00:00
|
|
|
gst_directsound_sink_is_spdif_format (GstAudioRingBufferSpec * spec)
|
2012-07-04 13:42:49 +00:00
|
|
|
{
|
2012-07-04 15:41:52 +00:00
|
|
|
return spec->type == GST_AUDIO_RING_BUFFER_FORMAT_TYPE_AC3 ||
|
|
|
|
spec->type == GST_AUDIO_RING_BUFFER_FORMAT_TYPE_DTS;
|
2012-07-04 13:42:49 +00:00
|
|
|
}
|
|
|
|
|
2006-01-05 23:17:44 +00:00
|
|
|
static gboolean
|
2012-01-27 15:37:19 +00:00
|
|
|
gst_directsound_sink_prepare (GstAudioSink * asink,
|
|
|
|
GstAudioRingBufferSpec * spec)
|
2006-01-05 23:17:44 +00:00
|
|
|
{
|
2012-01-27 15:37:19 +00:00
|
|
|
GstDirectSoundSink *dsoundsink;
|
2006-01-05 23:17:44 +00:00
|
|
|
HRESULT hRes;
|
|
|
|
DSBUFFERDESC descSecondary;
|
|
|
|
WAVEFORMATEX wfx;
|
|
|
|
|
2012-01-27 15:37:19 +00:00
|
|
|
dsoundsink = GST_DIRECTSOUND_SINK (asink);
|
|
|
|
|
2010-03-26 22:20:10 +00:00
|
|
|
/*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;
|
2006-01-05 23:17:44 +00:00
|
|
|
|
2010-03-26 22:20:10 +00:00
|
|
|
/* fill the WAVEFORMATEX structure with spec params */
|
2006-01-05 23:17:44 +00:00
|
|
|
memset (&wfx, 0, sizeof (wfx));
|
2012-07-04 15:41:52 +00:00
|
|
|
if (!gst_directsound_sink_is_spdif_format (spec)) {
|
2010-03-26 22:20:10 +00:00
|
|
|
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;
|
2010-03-26 22:20:10 +00:00
|
|
|
wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign;
|
|
|
|
|
2012-01-27 15:37:19 +00:00
|
|
|
/* Create directsound buffer with size based on our configured
|
2010-03-26 22:20:10 +00:00
|
|
|
* buffer_size (which is 200 ms by default) */
|
|
|
|
dsoundsink->buffer_size =
|
|
|
|
gst_util_uint64_scale_int (wfx.nAvgBytesPerSec, spec->buffer_time,
|
|
|
|
GST_MSECOND);
|
2010-06-09 18:07:09 +00:00
|
|
|
/* Make sure we make those numbers multiple of our sample size in bytes */
|
2015-03-05 20:54:43 +00:00
|
|
|
dsoundsink->buffer_size -= dsoundsink->buffer_size % spec->info.bpf;
|
2010-03-26 22:20:10 +00:00
|
|
|
|
|
|
|
spec->segsize =
|
|
|
|
gst_util_uint64_scale_int (wfx.nAvgBytesPerSec, spec->latency_time,
|
|
|
|
GST_MSECOND);
|
2015-03-05 20:54:43 +00:00
|
|
|
spec->segsize -= spec->segsize % spec->info.bpf;
|
2010-03-26 22:20:10 +00:00
|
|
|
spec->segtotal = dsoundsink->buffer_size / spec->segsize;
|
|
|
|
} else {
|
2011-03-24 21:34:24 +00:00
|
|
|
#ifdef WAVE_FORMAT_DOLBY_AC3_SPDIF
|
2010-03-26 22:20:10 +00:00
|
|
|
wfx.cbSize = 0;
|
|
|
|
wfx.wFormatTag = WAVE_FORMAT_DOLBY_AC3_SPDIF;
|
|
|
|
wfx.nChannels = 2;
|
2012-05-08 14:23:42 +00:00
|
|
|
wfx.nSamplesPerSec = 48000;
|
2010-03-26 22:20:10 +00:00
|
|
|
wfx.wBitsPerSample = 16;
|
|
|
|
wfx.nBlockAlign = wfx.wBitsPerSample / 8 * wfx.nChannels;
|
|
|
|
wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign;
|
|
|
|
|
|
|
|
spec->segsize = 6144;
|
|
|
|
spec->segtotal = 10;
|
2011-03-24 21:34:24 +00:00
|
|
|
#else
|
|
|
|
g_assert_not_reached ();
|
|
|
|
#endif
|
2010-03-26 22:20:10 +00:00
|
|
|
}
|
2008-10-01 21:22:26 +00:00
|
|
|
|
|
|
|
// Make the final buffer size be an integer number of segments
|
|
|
|
dsoundsink->buffer_size = spec->segsize * spec->segtotal;
|
2006-01-05 23:17:44 +00:00
|
|
|
|
2007-02-18 18:00:51 +00:00
|
|
|
GST_INFO_OBJECT (dsoundsink,
|
2012-07-04 13:42:49 +00:00
|
|
|
"GstAudioRingBufferSpec->channels: %d, GstAudioRingBufferSpec->rate: %d, GstAudioRingBufferSpec->bytes_per_sample: %d\n"
|
2006-07-24 21:43:06 +00:00
|
|
|
"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,
|
2006-07-24 21:43:06 +00:00
|
|
|
wfx.nBlockAlign, wfx.nAvgBytesPerSec, dsoundsink->buffer_size);
|
|
|
|
|
2006-01-05 23:17:44 +00:00
|
|
|
/* create a secondary directsound buffer */
|
|
|
|
memset (&descSecondary, 0, sizeof (DSBUFFERDESC));
|
|
|
|
descSecondary.dwSize = sizeof (DSBUFFERDESC);
|
2010-03-26 22:20:10 +00:00
|
|
|
descSecondary.dwFlags = DSBCAPS_GETCURRENTPOSITION2 | DSBCAPS_GLOBALFOCUS;
|
2012-07-04 15:41:52 +00:00
|
|
|
if (!gst_directsound_sink_is_spdif_format (spec))
|
2010-03-26 22:20:10 +00:00
|
|
|
descSecondary.dwFlags |= DSBCAPS_CTRLVOLUME;
|
2006-01-05 23:17:44 +00:00
|
|
|
|
|
|
|
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,
|
2007-02-18 18:00:51 +00:00
|
|
|
("gst_directsound_sink_prepare: IDirectSound_CreateSoundBuffer: %s",
|
2006-01-05 23:17:44 +00:00
|
|
|
DXGetErrorString9 (hRes)), (NULL));
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2012-01-27 15:37:19 +00:00
|
|
|
gst_directsound_sink_set_volume (dsoundsink, dsoundsink->volume, FALSE);
|
2015-12-14 13:15:00 +00:00
|
|
|
gst_directsound_sink_set_mute (dsoundsink, dsoundsink->mute);
|
2008-10-01 21:22:26 +00:00
|
|
|
|
2006-01-05 23:17:44 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2007-02-18 18:00:51 +00:00
|
|
|
gst_directsound_sink_unprepare (GstAudioSink * asink)
|
2006-01-05 23:17:44 +00:00
|
|
|
{
|
|
|
|
GstDirectSoundSink *dsoundsink;
|
|
|
|
|
|
|
|
dsoundsink = GST_DIRECTSOUND_SINK (asink);
|
|
|
|
|
|
|
|
/* release secondary DirectSound buffer */
|
2010-03-26 22:20:10 +00:00
|
|
|
if (dsoundsink->pDSBSecondary) {
|
2006-01-05 23:17:44 +00:00
|
|
|
IDirectSoundBuffer_Release (dsoundsink->pDSBSecondary);
|
2010-03-26 22:20:10 +00:00
|
|
|
dsoundsink->pDSBSecondary = NULL;
|
|
|
|
}
|
2006-01-05 23:17:44 +00:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2007-02-18 18:00:51 +00:00
|
|
|
gst_directsound_sink_close (GstAudioSink * asink)
|
2006-01-05 23:17:44 +00:00
|
|
|
{
|
|
|
|
GstDirectSoundSink *dsoundsink = NULL;
|
|
|
|
|
|
|
|
dsoundsink = GST_DIRECTSOUND_SINK (asink);
|
|
|
|
|
|
|
|
/* release DirectSound object */
|
|
|
|
g_return_val_if_fail (dsoundsink->pDS != NULL, FALSE);
|
|
|
|
IDirectSound_Release (dsoundsink->pDS);
|
2010-03-26 22:20:10 +00:00
|
|
|
dsoundsink->pDS = NULL;
|
|
|
|
|
|
|
|
gst_caps_replace (&dsoundsink->cached_caps, NULL);
|
2006-01-05 23:17:44 +00:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2012-01-27 15:37:19 +00:00
|
|
|
static gint
|
2007-02-18 18:00:51 +00:00
|
|
|
gst_directsound_sink_write (GstAudioSink * asink, gpointer data, guint length)
|
2006-01-05 23:17:44 +00:00
|
|
|
{
|
|
|
|
GstDirectSoundSink *dsoundsink;
|
2015-12-11 10:23:13 +00:00
|
|
|
DWORD dwStatus = 0;
|
|
|
|
HRESULT hRes, hRes2;
|
2006-01-05 23:17:44 +00:00
|
|
|
LPVOID pLockedBuffer1 = NULL, pLockedBuffer2 = NULL;
|
|
|
|
DWORD dwSizeBuffer1, dwSizeBuffer2;
|
|
|
|
DWORD dwCurrentPlayCursor;
|
|
|
|
|
|
|
|
dsoundsink = GST_DIRECTSOUND_SINK (asink);
|
|
|
|
|
2006-07-24 21:43:06 +00:00
|
|
|
GST_DSOUND_LOCK (dsoundsink);
|
|
|
|
|
2006-01-05 23:17:44 +00:00
|
|
|
/* get current buffer status */
|
|
|
|
hRes = IDirectSoundBuffer_GetStatus (dsoundsink->pDSBSecondary, &dwStatus);
|
|
|
|
|
|
|
|
/* get current play cursor position */
|
2015-12-11 10:23:13 +00:00
|
|
|
hRes2 = IDirectSoundBuffer_GetCurrentPosition (dsoundsink->pDSBSecondary,
|
2006-01-05 23:17:44 +00:00
|
|
|
&dwCurrentPlayCursor, NULL);
|
2006-07-24 21:43:06 +00:00
|
|
|
|
2015-12-11 10:23:13 +00:00
|
|
|
if (SUCCEEDED (hRes) && SUCCEEDED (hRes2) && (dwStatus & DSBSTATUS_PLAYING)) {
|
2015-12-21 10:37:26 +00:00
|
|
|
DWORD dwFreeBufferSize = 0;
|
|
|
|
DWORD sleepTime = 0;
|
2006-01-05 23:17:44 +00:00
|
|
|
|
|
|
|
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) {
|
2015-12-21 10:37:26 +00:00
|
|
|
sleepTime =
|
|
|
|
((length -
|
|
|
|
dwFreeBufferSize) * 1000) / (dsoundsink->bytes_per_sample *
|
|
|
|
GST_AUDIO_BASE_SINK (asink)->ringbuffer->spec.info.rate);
|
|
|
|
if (sleepTime > 0) {
|
|
|
|
GST_DEBUG_OBJECT (dsoundsink,
|
|
|
|
"gst_directsound_sink_write: length:%i, FreeBufSiz: %ld, sleepTime: %ld, bps: %i, rate: %i",
|
|
|
|
length, dwFreeBufferSize, sleepTime, dsoundsink->bytes_per_sample,
|
|
|
|
GST_AUDIO_BASE_SINK (asink)->ringbuffer->spec.info.rate);
|
|
|
|
Sleep (sleepTime);
|
|
|
|
}
|
2006-01-05 23:17:44 +00:00
|
|
|
hRes = IDirectSoundBuffer_GetCurrentPosition (dsoundsink->pDSBSecondary,
|
|
|
|
&dwCurrentPlayCursor, NULL);
|
2006-07-24 21:43:06 +00:00
|
|
|
|
2015-12-11 10:23:13 +00:00
|
|
|
hRes2 =
|
2006-07-24 21:43:06 +00:00
|
|
|
IDirectSoundBuffer_GetStatus (dsoundsink->pDSBSecondary, &dwStatus);
|
2015-12-11 10:23:13 +00:00
|
|
|
if (SUCCEEDED (hRes) && SUCCEEDED (hRes2)
|
|
|
|
&& (dwStatus & DSBSTATUS_PLAYING))
|
2006-07-24 21:43:06 +00:00
|
|
|
goto calculate_freesize;
|
|
|
|
else {
|
|
|
|
dsoundsink->first_buffer_after_reset = FALSE;
|
|
|
|
GST_DSOUND_UNLOCK (dsoundsink);
|
2015-12-11 10:23:13 +00:00
|
|
|
GST_ELEMENT_ERROR (dsoundsink, RESOURCE, OPEN_WRITE,
|
|
|
|
("gst_directsound_sink_write: IDirectSoundBuffer_GetStatus %s, IDirectSoundBuffer_GetCurrentPosition: %s, dwStatus: %lu",
|
|
|
|
DXGetErrorString9 (hRes2), DXGetErrorString9 (hRes), dwStatus),
|
|
|
|
(NULL));
|
|
|
|
return -1;
|
2006-07-24 21:43:06 +00:00
|
|
|
}
|
2006-01-05 23:17:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2006-07-24 21:43:06 +00:00
|
|
|
/* 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) {
|
2006-01-05 23:17:44 +00:00
|
|
|
hRes = IDirectSoundBuffer_Play (dsoundsink->pDSBSecondary, 0, 0,
|
|
|
|
DSBPLAY_LOOPING);
|
|
|
|
}
|
|
|
|
|
2006-07-24 21:43:06 +00:00
|
|
|
dsoundsink->first_buffer_after_reset = FALSE;
|
|
|
|
|
|
|
|
GST_DSOUND_UNLOCK (dsoundsink);
|
|
|
|
|
2006-01-05 23:17:44 +00:00
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
|
|
|
static guint
|
2007-02-18 18:00:51 +00:00
|
|
|
gst_directsound_sink_delay (GstAudioSink * asink)
|
2006-01-05 23:17:44 +00:00
|
|
|
{
|
|
|
|
GstDirectSoundSink *dsoundsink;
|
|
|
|
HRESULT hRes;
|
|
|
|
DWORD dwCurrentPlayCursor;
|
|
|
|
DWORD dwBytesInQueue = 0;
|
|
|
|
gint nNbSamplesInQueue = 0;
|
2006-07-24 21:43:06 +00:00
|
|
|
DWORD dwStatus;
|
2006-01-05 23:17:44 +00:00
|
|
|
|
|
|
|
dsoundsink = GST_DIRECTSOUND_SINK (asink);
|
|
|
|
|
2006-07-24 21:43:06 +00:00
|
|
|
/* get current buffer status */
|
|
|
|
hRes = IDirectSoundBuffer_GetStatus (dsoundsink->pDSBSecondary, &dwStatus);
|
2006-01-05 23:17:44 +00:00
|
|
|
|
2015-12-11 10:23:13 +00:00
|
|
|
if (SUCCEEDED (hRes) && (dwStatus & DSBSTATUS_PLAYING)) {
|
2006-07-24 21:43:06 +00:00
|
|
|
/*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;
|
|
|
|
}
|
2006-01-05 23:17:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nNbSamplesInQueue;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2007-02-18 18:00:51 +00:00
|
|
|
gst_directsound_sink_reset (GstAudioSink * asink)
|
2006-01-05 23:17:44 +00:00
|
|
|
{
|
|
|
|
GstDirectSoundSink *dsoundsink;
|
2007-01-04 09:44:57 +00:00
|
|
|
LPVOID pLockedBuffer = NULL;
|
2006-07-24 21:43:06 +00:00
|
|
|
DWORD dwSizeBuffer = 0;
|
2006-01-05 23:17:44 +00:00
|
|
|
|
|
|
|
dsoundsink = GST_DIRECTSOUND_SINK (asink);
|
|
|
|
|
2006-07-24 21:43:06 +00:00
|
|
|
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);
|
2006-01-05 23:17:44 +00:00
|
|
|
}
|
2010-03-26 22:20:10 +00:00
|
|
|
|
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.
|
|
|
|
*
|
2010-03-26 22:20:10 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
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;
|
2012-07-27 14:13:49 +00:00
|
|
|
LPDIRECTSOUNDBUFFER tmpBuffer;
|
2010-03-26 22:20:10 +00:00
|
|
|
|
|
|
|
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
|
2010-03-26 22:20:10 +00:00
|
|
|
*/
|
|
|
|
|
2011-03-24 21:34:24 +00:00
|
|
|
#ifdef WAVE_FORMAT_DOLBY_AC3_SPDIF
|
2010-03-26 22:20:10 +00:00
|
|
|
/* 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
|
2010-03-26 22:20:10 +00:00
|
|
|
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,
|
2012-07-27 14:13:49 +00:00
|
|
|
&tmpBuffer, NULL);
|
2010-03-26 22:20:10 +00:00
|
|
|
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;
|
2010-03-26 22:20:10 +00:00
|
|
|
} else {
|
|
|
|
GST_INFO_OBJECT (dsoundsink, "AC3 passthrough supported");
|
2012-07-27 14:13:49 +00:00
|
|
|
hRes = IDirectSoundBuffer_Release (tmpBuffer);
|
2010-03-26 22:20:10 +00:00
|
|
|
if (FAILED (hRes)) {
|
|
|
|
GST_DEBUG_OBJECT (dsoundsink,
|
|
|
|
"(IDirectSoundBuffer_Release returned: %s)\n",
|
|
|
|
DXGetErrorString9 (hRes));
|
|
|
|
}
|
|
|
|
}
|
2011-03-24 21:34:24 +00:00
|
|
|
#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;
|
2011-03-24 21:34:24 +00:00
|
|
|
#endif
|
2010-03-26 22:20:10 +00:00
|
|
|
|
|
|
|
return caps;
|
|
|
|
}
|
2012-01-27 15:37:19 +00:00
|
|
|
|
2012-07-04 13:42:49 +00:00
|
|
|
static GstBuffer *
|
|
|
|
gst_directsound_sink_payload (GstAudioBaseSink * sink, GstBuffer * buf)
|
|
|
|
{
|
2012-07-27 14:13:49 +00:00
|
|
|
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;
|
2012-07-04 13:42:49 +00:00
|
|
|
|
2012-07-27 14:13:49 +00:00
|
|
|
if (framesize <= 0)
|
|
|
|
return NULL;
|
2012-07-04 13:42:49 +00:00
|
|
|
|
2012-07-27 14:13:49 +00:00
|
|
|
out = gst_buffer_new_and_alloc (framesize);
|
2012-07-04 13:42:49 +00:00
|
|
|
|
2012-07-27 14:13:49 +00:00
|
|
|
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,
|
2012-09-19 03:45:53 +00:00
|
|
|
infoout.data, infoout.size, &sink->ringbuffer->spec, G_BYTE_ORDER);
|
2012-07-27 14:13:49 +00:00
|
|
|
if (!success) {
|
2012-07-04 13:42:49 +00:00
|
|
|
gst_buffer_unmap (out, &infoout);
|
|
|
|
gst_buffer_unmap (buf, &infobuf);
|
2012-07-27 14:13:49 +00:00
|
|
|
gst_buffer_unref (out);
|
|
|
|
return NULL;
|
2012-07-04 13:42:49 +00:00
|
|
|
}
|
2012-07-27 14:13:49 +00:00
|
|
|
|
|
|
|
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-07-04 13:42:49 +00:00
|
|
|
}
|
|
|
|
|
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;
|
2016-03-08 16:37:17 +00:00
|
|
|
if (volume == 0 || dsoundsink->mute)
|
2012-01-27 15:37:19 +00:00
|
|
|
dsVolume = -10000;
|
|
|
|
else
|
2015-12-14 13:15:00 +00:00
|
|
|
dsVolume = 100 * (long) (20 * log10 ((double) volume / 100.));
|
2012-01-27 15:37:19 +00:00
|
|
|
dsVolume = CLAMP (dsVolume, -10000, 0);
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (dsoundsink,
|
|
|
|
"Setting volume on secondary buffer to %d from %d", (int) dsVolume,
|
2016-03-08 16:37:17 +00:00
|
|
|
(int) volume);
|
2012-01-27 15:37:19 +00:00
|
|
|
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)
|
|
|
|
{
|
2015-12-14 13:15:00 +00:00
|
|
|
if (mute) {
|
2012-01-27 15:37:19 +00:00
|
|
|
gst_directsound_sink_set_volume (dsoundsink, 0, FALSE);
|
2015-12-14 13:15:00 +00:00
|
|
|
dsoundsink->mute = TRUE;
|
|
|
|
} else {
|
2016-03-08 12:57:24 +00:00
|
|
|
gst_directsound_sink_set_volume (dsoundsink,
|
|
|
|
gst_directsound_sink_get_volume (dsoundsink), FALSE);
|
2015-12-14 13:15:00 +00:00
|
|
|
dsoundsink->mute = FALSE;
|
|
|
|
}
|
|
|
|
|
2012-01-27 15:37:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_directsound_sink_get_mute (GstDirectSoundSink * dsoundsink)
|
|
|
|
{
|
2015-12-14 13:15:00 +00:00
|
|
|
return dsoundsink->mute;
|
2012-01-27 15:37:19 +00:00
|
|
|
}
|
2015-08-16 19:21:51 +00:00
|
|
|
|
|
|
|
static const gchar *
|
|
|
|
gst_directsound_sink_get_device (GstDirectSoundSink * dsoundsink)
|
|
|
|
{
|
|
|
|
return dsoundsink->device_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_directsound_sink_set_device (GstDirectSoundSink * dsoundsink,
|
|
|
|
const gchar * device_id)
|
|
|
|
{
|
|
|
|
g_free (dsoundsink->device_id);
|
|
|
|
dsoundsink->device_id = g_strdup (device_id);
|
|
|
|
}
|