directsoundsrc: port to 1.0

https://bugzilla.gnome.org/show_bug.cgi?id=673414
This commit is contained in:
Raimo Järvi 2012-04-03 12:09:19 +03:00 committed by Tim-Philipp Müller
parent 5e6783f5af
commit a7258842ab
2 changed files with 79 additions and 128 deletions

View file

@ -324,7 +324,7 @@ GST_PLUGINS_NONPORTED=" androidmedia aiff \
apexsink cdaudio cog dc1394 dirac directfb \
gsettings jasper ladspa \
musepack musicbrainz nas neon ofa openal rsvg sdl sndfile timidity \
directsound directdraw direct3d9 acm wininet \
directdraw direct3d9 acm wininet \
wildmidi xvid lv2 teletextdec sndio uvch264"
AC_SUBST(GST_PLUGINS_NONPORTED)

View file

@ -48,12 +48,25 @@
TODO: add device selection and check rate etc.
*/
/**
* SECTION:element-directsoundsrc
*
* Reads audio data using the DirectSound API.
*
* <refsect2>
* <title>Example pipelines</title>
* |[
* gst-launch -v directsoundsrc ! audioconvert ! vorbisenc ! oggmux ! filesink location=dsound.ogg
* ]| Record from DirectSound and encode to Ogg/Vorbis.
* </refsect2>
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <gst/gst.h>
#include <gst/audio/gstbaseaudiosrc.h>
#include <gst/audio/gstaudiobasesrc.h>
#include "gstdirectsoundsrc.h"
@ -73,7 +86,6 @@ enum
PROP_DEVICE
};
static HRESULT (WINAPI * pDSoundCaptureCreate) (LPGUID,
LPDIRECTSOUNDCAPTURE *, LPUNKNOWN);
@ -88,47 +100,30 @@ static void gst_directsound_src_get_property (GObject * object,
static gboolean gst_directsound_src_open (GstAudioSrc * asrc);
static gboolean gst_directsound_src_close (GstAudioSrc * asrc);
static gboolean gst_directsound_src_prepare (GstAudioSrc * asrc,
GstRingBufferSpec * spec);
GstAudioRingBufferSpec * spec);
static gboolean gst_directsound_src_unprepare (GstAudioSrc * asrc);
static void gst_directsound_src_reset (GstAudioSrc * asrc);
static GstCaps *gst_directsound_src_getcaps (GstBaseSrc * bsrc);
static GstCaps *gst_directsound_src_getcaps (GstBaseSrc * bsrc,
GstCaps * filter);
static guint gst_directsound_src_read (GstAudioSrc * asrc,
gpointer data, guint length);
static void gst_directsound_src_dispose (GObject * object);
static void gst_directsound_src_do_init (GType type);
static guint gst_directsound_src_delay (GstAudioSrc * asrc);
static GstStaticPadTemplate directsound_src_src_factory =
GST_STATIC_PAD_TEMPLATE ("src",
GST_STATIC_PAD_TEMPLATE ("src",
GST_PAD_SRC,
GST_PAD_ALWAYS,
GST_STATIC_CAPS ("audio/x-raw-int, "
"endianness = (int) { LITTLE_ENDIAN, BIG_ENDIAN }, "
"signed = (boolean) { TRUE, FALSE }, "
"width = (int) 16, "
"depth = (int) 16, "
"rate = (int) [ 1, MAX ], " "channels = (int) [ 1, 2 ]; "
"audio/x-raw-int, "
"signed = (boolean) { TRUE, FALSE }, "
"width = (int) 8, "
"depth = (int) 8, "
GST_STATIC_CAPS ("audio/x-raw, "
"format = (string) { S16LE, S8 }, "
"layout = (string) interleaved, "
"rate = (int) [ 1, MAX ], " "channels = (int) [ 1, 2 ]"));
static void
gst_directsound_src_do_init (GType type)
{
GST_DEBUG_CATEGORY_INIT (directsoundsrc_debug, "directsoundsrc", 0,
"DirectSound Src");
}
GST_BOILERPLATE_FULL (GstDirectSoundSrc, gst_directsound_src, GstAudioSrc,
GST_TYPE_AUDIO_SRC, gst_directsound_src_do_init);
#define gst_directsound_src_parent_class parent_class
G_DEFINE_TYPE (GstDirectSoundSrc, gst_directsound_src, GST_TYPE_AUDIO_SRC);
static void
gst_directsound_src_dispose (GObject * object)
@ -146,40 +141,20 @@ gst_directsound_src_finalize (GObject * object)
G_OBJECT_CLASS (parent_class)->finalize (object);
}
static void
gst_directsound_src_base_init (gpointer g_class)
{
GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
GST_DEBUG ("initializing directsoundsrc base\n");
gst_element_class_set_metadata (element_class, "Direct Sound Audio Src",
"Source/Audio",
"Capture from a soundcard via DIRECTSOUND",
"Joni Valtanen <joni.valtanen@movial.fi>");
gst_element_class_add_pad_template (element_class,
gst_static_pad_template_get (&directsound_src_src_factory));
}
/* initialize the plugin's class */
static void
gst_directsound_src_class_init (GstDirectSoundSrcClass * klass)
{
GObjectClass *gobject_class;
GstElementClass *gstelement_class;
GstBaseSrcClass *gstbasesrc_class;
GstBaseAudioSrcClass *gstbaseaudiosrc_class;
GstAudioSrcClass *gstaudiosrc_class;
gobject_class = (GObjectClass *) klass;
gstelement_class = (GstElementClass *) klass;
gstbasesrc_class = (GstBaseSrcClass *) klass;
gstbaseaudiosrc_class = (GstBaseAudioSrcClass *) klass;
gstaudiosrc_class = (GstAudioSrcClass *) klass;
GST_DEBUG ("initializing directsoundsrc class\n");
GST_DEBUG ("initializing directsoundsrc class");
gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_directsound_src_finalize);
gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_directsound_src_dispose);
@ -199,23 +174,30 @@ gst_directsound_src_class_init (GstDirectSoundSrcClass * klass)
gstaudiosrc_class->delay = GST_DEBUG_FUNCPTR (gst_directsound_src_delay);
gstaudiosrc_class->reset = GST_DEBUG_FUNCPTR (gst_directsound_src_reset);
GST_DEBUG_CATEGORY_INIT (directsoundsrc_debug, "directsoundsrc", 0,
"DirectSound Src");
gst_element_class_set_static_metadata (gstelement_class,
"DirectSound audio source", "Source/Audio",
"Capture from a soundcard via DirectSound",
"Joni Valtanen <joni.valtanen@movial.fi>");
gst_element_class_add_pad_template (gstelement_class,
gst_static_pad_template_get (&directsound_src_src_factory));
}
static GstCaps *
gst_directsound_src_getcaps (GstBaseSrc * bsrc)
gst_directsound_src_getcaps (GstBaseSrc * bsrc, GstCaps * filter)
{
GstDirectSoundSrc *dsoundsrc;
GstCaps *caps = NULL;
GST_DEBUG ("get caps\n");
GST_DEBUG_OBJECT (bsrc, "get caps");
dsoundsrc = GST_DIRECTSOUND_SRC (bsrc);
caps = gst_caps_copy (gst_pad_get_pad_template_caps (GST_BASE_SRC_PAD
(bsrc)));
return caps;
}
static void
@ -223,7 +205,7 @@ gst_directsound_src_set_property (GObject * object, guint prop_id,
const GValue * value, GParamSpec * pspec)
{
// GstDirectSoundSrc *src = GST_DIRECTSOUND_SRC (object);
GST_DEBUG ("set property\n");
GST_DEBUG ("set property");
switch (prop_id) {
#if 0
@ -246,7 +228,7 @@ gst_directsound_src_get_property (GObject * object, guint prop_id,
GstDirectSoundSrc *src = GST_DIRECTSOUND_SRC (object);
#endif
GST_DEBUG ("get property\n");
GST_DEBUG ("get property");
switch (prop_id) {
#if 0
@ -268,22 +250,19 @@ gst_directsound_src_get_property (GObject * object, guint prop_id,
* initialize structure
*/
static void
gst_directsound_src_init (GstDirectSoundSrc * src,
GstDirectSoundSrcClass * gclass)
gst_directsound_src_init (GstDirectSoundSrc * src)
{
GST_DEBUG ("initializing directsoundsrc\n");
GST_DEBUG_OBJECT (src, "initializing directsoundsrc");
src->dsound_lock = g_mutex_new ();
}
static gboolean
gst_directsound_src_open (GstAudioSrc * asrc)
{
GstDirectSoundSrc *dsoundsrc;
HRESULT hRes; /* Result for windows functions */
GST_DEBUG ("initializing directsoundsrc\n");
GST_DEBUG_OBJECT (asrc, "opening directsoundsrc");
dsoundsrc = GST_DIRECTSOUND_SRC (asrc);
@ -342,7 +321,7 @@ gst_directsound_src_close (GstAudioSrc * asrc)
GstDirectSoundSrc *dsoundsrc;
HRESULT hRes; /* Result for windows functions */
GST_DEBUG ("initializing directsoundsrc\n");
GST_DEBUG_OBJECT (asrc, "closing directsoundsrc");
dsoundsrc = GST_DIRECTSOUND_SRC (asrc);
@ -356,49 +335,37 @@ gst_directsound_src_close (GstAudioSrc * asrc)
}
static gboolean
gst_directsound_src_prepare (GstAudioSrc * asrc, GstRingBufferSpec * spec)
gst_directsound_src_prepare (GstAudioSrc * asrc, GstAudioRingBufferSpec * spec)
{
GstDirectSoundSrc *dsoundsrc;
WAVEFORMATEX wfx; /* Wave format structure */
HRESULT hRes; /* Result for windows functions */
DSCBUFFERDESC descSecondary; /* Capturebuffer decsiption */
DSCBUFFERDESC descSecondary; /* Capturebuffer description */
dsoundsrc = GST_DIRECTSOUND_SRC (asrc);
GST_DEBUG ("initializing directsoundsrc\n");
GST_DEBUG_OBJECT (asrc, "preparing directsoundsrc");
/* Define buffer */
memset (&wfx, 0, sizeof (WAVEFORMATEX));
wfx.wFormatTag = WAVE_FORMAT_PCM; /* should be WAVE_FORMAT_PCM */
wfx.nChannels = spec->channels;
wfx.nSamplesPerSec = spec->rate; /* 8000|11025|22050|44100 */
wfx.wBitsPerSample = spec->width; // 8|16;
wfx.nBlockAlign = wfx.nChannels * (wfx.wBitsPerSample / 8);
wfx.wFormatTag = WAVE_FORMAT_PCM;
wfx.nChannels = GST_AUDIO_INFO_CHANNELS (&spec->info);
wfx.nSamplesPerSec = GST_AUDIO_INFO_RATE (&spec->info);
wfx.wBitsPerSample = GST_AUDIO_INFO_BPF (&spec->info) * 8 / wfx.nChannels;
wfx.nBlockAlign = GST_AUDIO_INFO_BPF (&spec->info);
wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign;
wfx.cbSize = 0; /* This size is allways for PCM-format */
/* Ignored for WAVE_FORMAT_PCM. */
wfx.cbSize = 0;
/* 1 or 2 Channels etc...
FIXME: Never really tested. Is this ok?
*/
if (spec->width == 16 && spec->channels == 1) {
spec->format = GST_S16_LE;
} else if (spec->width == 16 && spec->channels == 2) {
spec->format = GST_U16_LE;
} else if (spec->width == 8 && spec->channels == 1) {
spec->format = GST_S8;
} else if (spec->width == 8 && spec->channels == 2) {
spec->format = GST_U8;
}
if (wfx.wBitsPerSample != 16 && wfx.wBitsPerSample != 8)
goto dodgy_width;
/* Set the buffer size to two seconds.
This should never reached.
*/
dsoundsrc->buffer_size = wfx.nAvgBytesPerSec * 2;
//notifysize * 16; //spec->width; /*original 16*/
GST_DEBUG ("Buffer size: %d", dsoundsrc->buffer_size);
GST_DEBUG_OBJECT (asrc, "Buffer size: %d", dsoundsrc->buffer_size);
/* Init secondary buffer desciption */
memset (&descSecondary, 0, sizeof (DSCBUFFERDESC));
@ -413,19 +380,15 @@ gst_directsound_src_prepare (GstAudioSrc * asrc, GstRingBufferSpec * spec)
/* Create buffer */
hRes = IDirectSoundCapture_CreateCaptureBuffer (dsoundsrc->pDSC,
&descSecondary, &dsoundsrc->pDSBSecondary, NULL);
if (hRes != DS_OK) {
if (hRes != DS_OK)
goto capture_buffer;
}
spec->channels = wfx.nChannels;
spec->rate = wfx.nSamplesPerSec;
spec->bytes_per_sample = (spec->width / 8) * spec->channels;
dsoundsrc->bytes_per_sample = spec->bytes_per_sample;
dsoundsrc->bytes_per_sample = GST_AUDIO_INFO_BPF (&spec->info);
GST_DEBUG ("latency time: %" G_GUINT64_FORMAT " - buffer time: %"
G_GUINT64_FORMAT, spec->latency_time, spec->buffer_time);
/* Buffer-time should be allways more than 2*latency */
/* Buffer-time should be always more than 2*latency */
if (spec->buffer_time < spec->latency_time * 2) {
spec->buffer_time = spec->latency_time * 2;
GST_WARNING ("buffer-time was less than latency");
@ -438,7 +401,6 @@ gst_directsound_src_prepare (GstAudioSrc * asrc, GstRingBufferSpec * spec)
dsoundsrc->latency_size = (gint) wfx.nAvgBytesPerSec *
dsoundsrc->latency_time / 1000000.0;
spec->segsize = (guint) (((double) spec->buffer_time / 1000000.0) *
wfx.nAvgBytesPerSec);
@ -446,27 +408,23 @@ gst_directsound_src_prepare (GstAudioSrc * asrc, GstRingBufferSpec * spec)
if (spec->segsize < 1)
spec->segsize = 1;
spec->segtotal = spec->width * (wfx.nAvgBytesPerSec / spec->segsize);
spec->segtotal = GST_AUDIO_INFO_BPF (&spec->info) * 8 *
(wfx.nAvgBytesPerSec / spec->segsize);
GST_DEBUG ("bytes/sec: %lu, buffer size: %d, segsize: %d, segtotal: %d",
wfx.nAvgBytesPerSec,
dsoundsrc->buffer_size, spec->segsize, spec->segtotal);
GST_DEBUG_OBJECT (asrc,
"bytes/sec: %lu, buffer size: %d, segsize: %d, segtotal: %d",
wfx.nAvgBytesPerSec, dsoundsrc->buffer_size, spec->segsize,
spec->segtotal);
spec->silence_sample[0] = 0;
spec->silence_sample[1] = 0;
spec->silence_sample[2] = 0;
spec->silence_sample[3] = 0;
if (spec->width != 16 && spec->width != 8)
goto dodgy_width;
/* Not readed anything yet */
/* Not read anything yet */
dsoundsrc->current_circular_offset = 0;
GST_DEBUG ("GstRingBufferSpec->channels: %d, GstRingBufferSpec->rate: %d, \
GstRingBufferSpec->bytes_per_sample: %d\n\
WAVEFORMATEX.nSamplesPerSec: %ld, WAVEFORMATEX.wBitsPerSample: %d, \
WAVEFORMATEX.nBlockAlign: %d, WAVEFORMATEX.nAvgBytesPerSec: %ld\n", spec->channels, spec->rate, spec->bytes_per_sample, wfx.nSamplesPerSec, wfx.wBitsPerSample, wfx.nBlockAlign, wfx.nAvgBytesPerSec);
GST_DEBUG_OBJECT (asrc, "channels: %d, rate: %d, bytes_per_sample: %d"
" WAVEFORMATEX.nSamplesPerSec: %ld, WAVEFORMATEX.wBitsPerSample: %d,"
" WAVEFORMATEX.nBlockAlign: %d, WAVEFORMATEX.nAvgBytesPerSec: %ld",
GST_AUDIO_INFO_CHANNELS (&spec->info), GST_AUDIO_INFO_RATE (&spec->info),
GST_AUDIO_INFO_BPF (&spec->info), wfx.nSamplesPerSec, wfx.wBitsPerSample,
wfx.nBlockAlign, wfx.nAvgBytesPerSec);
return TRUE;
@ -479,21 +437,18 @@ capture_buffer:
dodgy_width:
{
GST_ELEMENT_ERROR (dsoundsrc, RESOURCE, OPEN_READ,
("Unexpected width %d", spec->width), (NULL));
("Unexpected width %d", wfx.wBitsPerSample), (NULL));
return FALSE;
}
}
static gboolean
gst_directsound_src_unprepare (GstAudioSrc * asrc)
{
GstDirectSoundSrc *dsoundsrc;
HRESULT hRes;
HRESULT hRes; /* Result for windows functions */
/* Resets */
GST_DEBUG ("unpreparing directsoundsrc");
GST_DEBUG_OBJECT (asrc, "unpreparing directsoundsrc");
dsoundsrc = GST_DIRECTSOUND_SRC (asrc);
@ -504,7 +459,6 @@ gst_directsound_src_unprepare (GstAudioSrc * asrc)
hRes = IDirectSoundCaptureBuffer_Release (dsoundsrc->pDSBSecondary);
return TRUE;
}
/*
@ -525,7 +479,7 @@ gst_directsound_src_read (GstAudioSrc * asrc, gpointer data, guint length)
DWORD dwStatus = 0;
GST_DEBUG ("reading directsoundsrc\n");
GST_DEBUG_OBJECT (asrc, "reading directsoundsrc");
dsoundsrc = GST_DIRECTSOUND_SRC (asrc);
@ -535,12 +489,12 @@ gst_directsound_src_read (GstAudioSrc * asrc, gpointer data, guint length)
hRes = IDirectSoundCaptureBuffer_GetStatus (dsoundsrc->pDSBSecondary,
&dwStatus);
/* Starting capturing if not allready */
/* Starting capturing if not already */
if (!(dwStatus & DSCBSTATUS_CAPTURING)) {
hRes = IDirectSoundCaptureBuffer_Start (dsoundsrc->pDSBSecondary,
DSCBSTART_LOOPING);
// Sleep (dsoundsrc->latency_time/1000);
GST_DEBUG ("capture started");
GST_DEBUG_OBJECT (asrc, "capture started");
}
// calculate_buffersize:
while (length > dwBufferSize) {
@ -558,8 +512,6 @@ gst_directsound_src_read (GstAudioSrc * asrc, gpointer data, guint length)
dwBufferSize =
dwCurrentCaptureCursor - dsoundsrc->current_circular_offset;
}
} // while (...
/* Lock the buffer */
@ -588,7 +540,6 @@ gst_directsound_src_read (GstAudioSrc * asrc, gpointer data, guint length)
/* return length (readed data size in bytes) */
return length;
}
static guint
@ -600,7 +551,7 @@ gst_directsound_src_delay (GstAudioSrc * asrc)
DWORD dwBytesInQueue = 0;
gint nNbSamplesInQueue = 0;
GST_DEBUG ("Delay\n");
GST_DEBUG_OBJECT (asrc, "Delay");
dsoundsrc = GST_DIRECTSOUND_SRC (asrc);
@ -632,7 +583,7 @@ gst_directsound_src_reset (GstAudioSrc * asrc)
LPVOID pLockedBuffer = NULL;
DWORD dwSizeBuffer = 0;
GST_DEBUG ("reset directsoundsrc\n");
GST_DEBUG_OBJECT (asrc, "reset directsoundsrc");
dsoundsrc = GST_DIRECTSOUND_SRC (asrc);