2 * Copyright (C) 2003 Martin Soto <martinsoto@users.sourceforge.net>
4 * dxr3audiosink.c: Audio sink for em8300 based DVD cards.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
29 #include <sys/ioctl.h>
31 #include <linux/soundcard.h>
32 #include <linux/em8300.h>
34 #include <gst/gst-i18n-plugin.h>
37 #include "dxr3audiosink.h"
38 #include "dxr3marshal.h"
39 #include "dxr3common.h"
41 /* Our only supported AC3 byte rate. */
42 #define AC3_BYTE_RATE 48000
44 /* Determines the amount of time to play the given number of bytes of
45 the original AC3 stream. The result is expressed as MPEG2. */
46 #define TIME_FOR_BYTES(bytes) (((bytes) * 90) / 48)
48 /* Dxr3AudioSink signals and args */
61 static GstStaticPadTemplate dxr3audiosink_pcm_sink_factory =
62 GST_STATIC_PAD_TEMPLATE ("pcm_sink",
65 GST_STATIC_CAPS ("audio/x-raw-int, "
66 "endianness = (int) BYTE_ORDER, "
67 "signed = (boolean) TRUE, "
70 "rate = (int) { 32000, 44100, 48000, 66000 }, " "channels = (int) 2")
73 static GstStaticPadTemplate dxr3audiosink_ac3_sink_factory =
74 GST_STATIC_PAD_TEMPLATE ("ac3_sink",
77 GST_STATIC_CAPS ("audio/x-ac3"
78 /* no parameters needed, we don't need a parsed stream */
83 static void dxr3audiosink_class_init (Dxr3AudioSinkClass * klass);
84 static void dxr3audiosink_base_init (Dxr3AudioSinkClass * klass);
85 static void dxr3audiosink_init (Dxr3AudioSink * sink);
87 static void dxr3audiosink_set_property (GObject * object,
88 guint prop_id, const GValue * value, GParamSpec * pspec);
89 static void dxr3audiosink_get_property (GObject * object,
90 guint prop_id, GValue * value, GParamSpec * pspec);
92 static gboolean dxr3audiosink_open (Dxr3AudioSink * sink);
93 static gboolean dxr3audiosink_set_mode_pcm (Dxr3AudioSink * sink);
94 static gboolean dxr3audiosink_set_mode_ac3 (Dxr3AudioSink * sink);
95 static void dxr3audiosink_close (Dxr3AudioSink * sink);
96 static void dxr3audiosink_set_clock (GstElement * element, GstClock * clock);
98 static GstPadLinkReturn dxr3audiosink_pcm_sinklink (GstPad * pad,
99 const GstCaps * caps);
100 static void dxr3audiosink_set_scr (Dxr3AudioSink * sink, guint32 scr);
102 static gboolean dxr3audiosink_handle_event (GstPad * pad, GstEvent * event);
103 static void dxr3audiosink_chain_pcm (GstPad * pad, GstData * buf);
104 static void dxr3audiosink_chain_ac3 (GstPad * pad, GstData * buf);
106 /* static void dxr3audiosink_wait (Dxr3AudioSink *sink, */
107 /* GstClockTime time); */
108 /* static int dxr3audiosink_mvcommand (Dxr3AudioSink *sink, */
111 static GstStateChangeReturn dxr3audiosink_change_state (GstElement * element,
112 GstStateChange transition);
114 static void dxr3audiosink_flushed (Dxr3AudioSink * sink);
116 static GstElementClass *parent_class = NULL;
117 static guint dxr3audiosink_signals[LAST_SIGNAL] = { 0 };
121 dxr3audiosink_get_type (void)
123 static GType dxr3audiosink_type = 0;
125 if (!dxr3audiosink_type) {
126 static const GTypeInfo dxr3audiosink_info = {
127 sizeof (Dxr3AudioSinkClass),
128 (GBaseInitFunc) dxr3audiosink_base_init,
130 (GClassInitFunc) dxr3audiosink_class_init,
133 sizeof (Dxr3AudioSink),
135 (GInstanceInitFunc) dxr3audiosink_init,
138 dxr3audiosink_type = g_type_register_static (GST_TYPE_ELEMENT,
139 "Dxr3AudioSink", &dxr3audiosink_info, 0);
142 return dxr3audiosink_type;
147 dxr3audiosink_base_init (Dxr3AudioSinkClass * klass)
149 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
151 gst_element_class_add_pad_template (element_class,
152 gst_static_pad_template_get (&dxr3audiosink_pcm_sink_factory));
153 gst_element_class_add_pad_template (element_class,
154 gst_static_pad_template_get (&dxr3audiosink_ac3_sink_factory));
155 gst_element_class_set_details_simple (element_class,
156 "dxr3/Hollywood+ mpeg decoder board audio plugin", "Audio/Sink",
157 "Feeds audio to Sigma Designs em8300 based boards",
158 "Martin Soto <martinsoto@users.sourceforge.net>");
162 dxr3audiosink_class_init (Dxr3AudioSinkClass * klass)
164 GObjectClass *gobject_class;
165 GstElementClass *gstelement_class;
167 gobject_class = (GObjectClass *) klass;
168 gstelement_class = (GstElementClass *) klass;
170 parent_class = g_type_class_peek_parent (klass);
172 dxr3audiosink_signals[SIGNAL_FLUSHED] =
173 g_signal_new ("flushed", G_TYPE_FROM_CLASS (klass),
175 G_STRUCT_OFFSET (Dxr3AudioSinkClass, flushed),
176 NULL, NULL, dxr3_marshal_VOID__VOID, G_TYPE_NONE, 0);
178 klass->flushed = dxr3audiosink_flushed;
180 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_DIGITAL_PCM,
181 g_param_spec_boolean ("digital-pcm", "Digital PCM",
182 "Use the digital output for PCM sound", FALSE, G_PARAM_READWRITE));
184 gobject_class->set_property = dxr3audiosink_set_property;
185 gobject_class->get_property = dxr3audiosink_get_property;
187 gstelement_class->change_state = dxr3audiosink_change_state;
188 gstelement_class->set_clock = dxr3audiosink_set_clock;
193 dxr3audiosink_init (Dxr3AudioSink * sink)
195 GstPadTemplate *temp;
197 /* Create the PCM pad. */
198 temp = gst_static_pad_template_get (&dxr3audiosink_pcm_sink_factory);
199 sink->pcm_sinkpad = gst_pad_new_from_template (temp, "pcm_sink");
200 gst_pad_set_chain_function (sink->pcm_sinkpad, dxr3audiosink_chain_pcm);
201 gst_pad_set_link_function (sink->pcm_sinkpad, dxr3audiosink_pcm_sinklink);
202 gst_element_add_pad (GST_ELEMENT (sink), sink->pcm_sinkpad);
204 /* Create the AC3 pad. */
205 temp = gst_static_pad_template_get (&dxr3audiosink_ac3_sink_factory);
206 sink->ac3_sinkpad = gst_pad_new_from_template (temp, "ac3_sink");
207 gst_pad_set_chain_function (sink->ac3_sinkpad, dxr3audiosink_chain_ac3);
208 gst_element_add_pad (GST_ELEMENT (sink), sink->ac3_sinkpad);
210 GST_OBJECT_FLAG_SET (GST_ELEMENT (sink), GST_ELEMENT_EVENT_AWARE);
212 sink->card_number = 0;
214 sink->audio_filename = NULL;
217 sink->control_filename = NULL;
218 sink->control_fd = -1;
220 /* Since we don't know any better, we set the initial scr to 0. */
223 /* Initially don't use digital output. */
224 sink->digital_pcm = FALSE;
226 /* Initially there's no padder. */
229 sink->mode = DXR3AUDIOSINK_MODE_NONE;
234 dxr3audiosink_set_property (GObject * object, guint prop_id,
235 const GValue * value, GParamSpec * pspec)
239 sink = DXR3AUDIOSINK (object);
242 case ARG_DIGITAL_PCM:
243 sink->digital_pcm = g_value_get_boolean (value);
244 /* Refresh the setup of the device. */
245 if (sink->mode == DXR3AUDIOSINK_MODE_PCM) {
246 dxr3audiosink_set_mode_pcm (sink);
248 g_object_notify (G_OBJECT (sink), "digital-pcm");
251 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
258 dxr3audiosink_get_property (GObject * object, guint prop_id,
259 GValue * value, GParamSpec * pspec)
263 g_return_if_fail (GST_IS_DXR3AUDIOSINK (object));
265 sink = DXR3AUDIOSINK (object);
268 case ARG_DIGITAL_PCM:
269 g_value_set_boolean (value, sink->digital_pcm);
272 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
279 dxr3audiosink_open (Dxr3AudioSink * sink)
281 g_return_val_if_fail (!GST_OBJECT_FLAG_IS_SET (sink, DXR3AUDIOSINK_OPEN),
284 /* Compute the name of the audio device file. */
285 sink->audio_filename = g_strdup_printf ("/dev/em8300_ma-%d",
288 sink->audio_fd = open (sink->audio_filename, O_WRONLY);
289 if (sink->audio_fd < 0) {
290 GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_WRITE,
291 (_("Could not open audio device \"%s\" for writing."),
292 sink->audio_filename), GST_ERROR_SYSTEM);
296 /* Open the control device. */
297 sink->control_filename = g_strdup_printf ("/dev/em8300-%d",
300 sink->control_fd = open (sink->control_filename, O_WRONLY);
301 if (sink->control_fd < 0) {
302 GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_WRITE,
303 (_("Could not open control device \"%s\" for writing."),
304 sink->control_filename), GST_ERROR_SYSTEM);
308 GST_OBJECT_FLAG_SET (sink, DXR3AUDIOSINK_OPEN);
310 dxr3audiosink_set_mode_pcm (sink);
317 * dxr3audiosink_set_mode:
318 * @sink: The sink element to operate on.
320 * Set the operation mode of the element to PCM.
323 dxr3audiosink_set_mode_pcm (Dxr3AudioSink * sink)
325 int tmp, oss_mode, audiomode;
327 if (sink->audio_fd == -1 || sink->control_fd == -1) {
331 /* Set the audio device mode. */
332 oss_mode = (G_BYTE_ORDER == G_BIG_ENDIAN ? AFMT_S16_BE : AFMT_S16_LE);
334 if (ioctl (sink->audio_fd, SNDCTL_DSP_SETFMT, &tmp) < 0 || tmp != oss_mode) {
335 GST_ELEMENT_ERROR (sink, RESOURCE, SETTINGS,
336 (_("Could not configure audio device \"%s\"."), sink->audio_filename),
341 /* Set the card's general audio output mode. */
342 audiomode = sink->digital_pcm ?
343 EM8300_AUDIOMODE_DIGITALPCM : EM8300_AUDIOMODE_ANALOG;
344 ioctl (sink->control_fd, EM8300_IOCTL_SET_AUDIOMODE, &audiomode);
346 /* Set the sampling rate. */
348 if (ioctl (sink->audio_fd, SNDCTL_DSP_SPEED, &tmp) < 0) {
349 GST_ELEMENT_ERROR (sink, RESOURCE, SETTINGS,
350 (_("Could not set audio device \"%s\" to %d Hz."), sink->audio_filename,
351 sink->rate), GST_ERROR_SYSTEM);
355 /* Get rid of the padder, if any. */
356 if (sink->padder != NULL) {
357 g_free (sink->padder);
361 sink->mode = DXR3AUDIOSINK_MODE_PCM;
368 * dxr3audiosink_set_mode:
369 * @sink: The sink element to operate on
371 * Set the operation mode of the element to AC3.
374 dxr3audiosink_set_mode_ac3 (Dxr3AudioSink * sink)
378 if (sink->audio_fd == -1 || sink->control_fd == -1) {
382 /* Set the sampling rate. */
384 if (ioctl (sink->audio_fd, SNDCTL_DSP_SPEED, &tmp) < 0 ||
385 tmp != AC3_BYTE_RATE) {
386 GST_ELEMENT_ERROR (sink, RESOURCE, SETTINGS,
387 (_("Could not set audio device \"%s\" to %d Hz."), sink->audio_filename,
388 AC3_BYTE_RATE), GST_ERROR_SYSTEM);
392 /* Set the card's general audio output mode to AC3. */
393 audiomode = EM8300_AUDIOMODE_DIGITALAC3;
394 ioctl (sink->control_fd, EM8300_IOCTL_SET_AUDIOMODE, &audiomode);
396 /* Create a padder if necessary, */
397 if (sink->padder == NULL) {
398 sink->padder = g_malloc (sizeof (ac3_padder));
399 ac3p_init (sink->padder);
402 sink->mode = DXR3AUDIOSINK_MODE_AC3;
409 dxr3audiosink_close (Dxr3AudioSink * sink)
411 g_return_if_fail (GST_OBJECT_FLAG_IS_SET (sink, DXR3AUDIOSINK_OPEN));
413 if (close (sink->audio_fd) != 0) {
414 GST_ELEMENT_ERROR (sink, RESOURCE, CLOSE,
415 (_("Could not close audio device \"%s\"."), sink->audio_filename),
420 if (close (sink->control_fd) != 0) {
421 GST_ELEMENT_ERROR (sink, RESOURCE, CLOSE,
422 (_("Could not close control device \"%s\"."), sink->audio_filename),
427 GST_OBJECT_FLAG_UNSET (sink, DXR3AUDIOSINK_OPEN);
429 g_free (sink->audio_filename);
430 sink->audio_filename = NULL;
432 g_free (sink->control_filename);
433 sink->control_filename = NULL;
435 /* Get rid of the padder, if any. */
436 if (sink->padder != NULL) {
437 g_free (sink->padder);
444 dxr3audiosink_set_clock (GstElement * element, GstClock * clock)
446 Dxr3AudioSink *src = DXR3AUDIOSINK (element);
452 static GstPadLinkReturn
453 dxr3audiosink_pcm_sinklink (GstPad * pad, const GstCaps * caps)
455 Dxr3AudioSink *sink = DXR3AUDIOSINK (gst_pad_get_parent (pad));
456 GstStructure *structure = gst_caps_get_structure (caps, 0);
459 if (!gst_caps_is_fixed (caps)) {
460 return GST_PAD_LINK_DELAYED;
463 gst_structure_get_int (structure, "rate", &rate);
466 return GST_PAD_LINK_OK;
471 dxr3audiosink_set_scr (Dxr3AudioSink * sink, guint32 scr)
475 /* fprintf (stderr, "====== Adjusting SCR\n"); */
476 ioctl (sink->control_fd, EM8300_IOCTL_SCR_SET, &zero);
477 ioctl (sink->control_fd, EM8300_IOCTL_SCR_SET, &scr);
482 dxr3audiosink_handle_event (GstPad * pad, GstEvent * event)
485 Dxr3AudioSink *sink = DXR3AUDIOSINK (gst_pad_get_parent (pad));
487 type = event ? GST_EVENT_TYPE (event) : GST_EVENT_UNKNOWN;
490 case GST_EVENT_FLUSH:
491 if (sink->control_fd >= 0) {
494 if (sink->mode == DXR3AUDIOSINK_MODE_AC3) {
495 audiomode = EM8300_AUDIOMODE_DIGITALPCM;
496 ioctl (sink->control_fd, EM8300_IOCTL_SET_AUDIOMODE, &audiomode);
497 audiomode = EM8300_AUDIOMODE_DIGITALAC3;
498 ioctl (sink->control_fd, EM8300_IOCTL_SET_AUDIOMODE, &audiomode);
501 /* Report the flush operation. */
502 g_signal_emit (G_OBJECT (sink),
503 dxr3audiosink_signals[SIGNAL_FLUSHED], 0);
507 gst_pad_event_default (pad, event);
516 dxr3audiosink_chain_pcm (GstPad * pad, GstData * _data)
519 gint bytes_written = 0;
522 g_return_if_fail (pad != NULL);
523 g_return_if_fail (GST_IS_PAD (pad));
524 g_return_if_fail (_data != NULL);
526 sink = DXR3AUDIOSINK (gst_pad_get_parent (pad));
528 if (GST_IS_EVENT (_data)) {
529 dxr3audiosink_handle_event (pad, GST_EVENT (_data));
533 buf = GST_BUFFER (_data);
535 if (sink->mode != DXR3AUDIOSINK_MODE_PCM) {
536 /* Switch to PCM mode. */
537 dxr3audiosink_set_mode_pcm (sink);
540 if (GST_OBJECT_FLAG_IS_SET (sink, DXR3AUDIOSINK_OPEN)) {
541 if (GST_BUFFER_TIMESTAMP (buf) != GST_CLOCK_TIME_NONE) {
542 /* We have a new scr value. */
543 sink->scr = GSTTIME_TO_MPEGTIME (GST_BUFFER_TIMESTAMP (buf));
546 /* Update the system reference clock (SCR) in the card. */
548 unsigned in, out, odelay;
551 ioctl (sink->control_fd, EM8300_IOCTL_SCR_GET, &out);
553 ioctl (sink->audio_fd, SNDCTL_DSP_GETODELAY, &odelay);
555 in = MPEGTIME_TO_DXRTIME (sink->scr - (odelay * 90) / 192);
556 diff = in > out ? in - out : out - in;
558 dxr3audiosink_set_scr (sink, in);
562 /* Update our SCR value. */
563 sink->scr += (unsigned) (GST_BUFFER_SIZE (buf) *
564 (90000.0 / ((float) sink->rate * 4)));
566 /* Write the buffer to the sound device. */
567 bytes_written = write (sink->audio_fd, GST_BUFFER_DATA (buf),
568 GST_BUFFER_SIZE (buf));
569 if (bytes_written < GST_BUFFER_SIZE (buf)) {
570 fprintf (stderr, "dxr3audiosink: Warning: %d bytes should be "
571 "written, only %d bytes written\n",
572 GST_BUFFER_SIZE (buf), bytes_written);
576 gst_buffer_unref (buf);
581 dxr3audiosink_chain_ac3 (GstPad * pad, GstData * _data)
584 gint bytes_written = 0;
587 g_return_if_fail (pad != NULL);
588 g_return_if_fail (GST_IS_PAD (pad));
589 g_return_if_fail (_data != NULL);
591 sink = DXR3AUDIOSINK (gst_pad_get_parent (pad));
593 if (GST_IS_EVENT (_data)) {
594 dxr3audiosink_handle_event (pad, GST_EVENT (_data));
598 buf = GST_BUFFER (_data);
600 if (sink->mode != DXR3AUDIOSINK_MODE_AC3) {
601 /* Switch to AC3 mode. */
602 dxr3audiosink_set_mode_ac3 (sink);
605 if (GST_OBJECT_FLAG_IS_SET (sink, DXR3AUDIOSINK_OPEN)) {
608 if (GST_BUFFER_TIMESTAMP (buf) != GST_CLOCK_TIME_NONE) {
609 /* We have a new scr value. */
611 /* fprintf (stderr, "------ Audio Time %.04f\n", */
612 /* (double) GST_BUFFER_TIMESTAMP (buf) / GST_SECOND); */
614 sink->scr = GSTTIME_TO_MPEGTIME (GST_BUFFER_TIMESTAMP (buf));
617 /* Push the new data into the padder. */
618 ac3p_push_data (sink->padder, GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
620 /* Parse the data. */
621 event = ac3p_parse (sink->padder);
622 while (event != AC3P_EVENT_PUSH) {
624 case AC3P_EVENT_FRAME:
625 /* We have a new frame: */
627 /* Update the system reference clock (SCR) in the card. */
629 unsigned in, out, odelay;
632 ioctl (sink->control_fd, EM8300_IOCTL_SCR_GET, &out);
634 ioctl (sink->audio_fd, SNDCTL_DSP_GETODELAY, &odelay);
635 /* 192000 bytes/sec */
637 in = MPEGTIME_TO_DXRTIME (sink->scr - (odelay * 90) / 192);
638 diff = in > out ? in - out : out - in;
640 dxr3audiosink_set_scr (sink, in);
644 /* Update our SCR value. */
645 sink->scr += TIME_FOR_BYTES (ac3p_frame_size (sink->padder));
647 /* Write the frame to the sound device. */
648 bytes_written = write (sink->audio_fd, ac3p_frame (sink->padder),
649 AC3P_IEC_FRAME_SIZE);
651 if (bytes_written < AC3P_IEC_FRAME_SIZE) {
652 fprintf (stderr, "dxr3audiosink: Warning: %d bytes should be "
653 "written, only %d bytes written\n",
654 AC3P_IEC_FRAME_SIZE, bytes_written);
660 event = ac3p_parse (sink->padder);
664 gst_buffer_unref (buf);
669 * dxr3audiosink_wait:
671 * Make the sink wait the specified amount of time.
674 dxr3audiosink_wait (Dxr3AudioSink * sink, GstClockTime time)
677 GstClockTimeDiff jitter;
679 GstClockTime current_time = gst_clock_get_time (sink->clock);
681 id = gst_clock_new_single_shot_id (sink->clock, current_time + time);
682 ret = gst_clock_id_wait (id, &jitter);
683 gst_clock_id_free (id);
688 dxr3audiosink_mvcommand (Dxr3AudioSink * sink, int command)
690 em8300_register_t regs;
692 regs.microcode_register = 1;
696 return ioctl (sink->control_fd, EM8300_IOCTL_WRITEREG, ®s);
700 static GstStateChangeReturn
701 dxr3audiosink_change_state (GstElement * element, GstStateChange transition)
703 g_return_val_if_fail (GST_IS_DXR3AUDIOSINK (element),
704 GST_STATE_CHANGE_FAILURE);
706 if (GST_STATE_PENDING (element) == GST_STATE_NULL) {
707 if (GST_OBJECT_FLAG_IS_SET (element, DXR3AUDIOSINK_OPEN)) {
708 dxr3audiosink_close (DXR3AUDIOSINK (element));
711 if (!GST_OBJECT_FLAG_IS_SET (element, DXR3AUDIOSINK_OPEN)) {
712 if (!dxr3audiosink_open (DXR3AUDIOSINK (element))) {
713 return GST_STATE_CHANGE_FAILURE;
718 if (GST_ELEMENT_CLASS (parent_class)->change_state) {
719 return GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
722 return GST_STATE_CHANGE_SUCCESS;
727 * dxr3audiosink_flushed:
729 * Default do nothing implementation for the "flushed" signal. The
730 * "flushed" signal will be fired right after flushing the hardware
731 * queues due to a received flush event
734 dxr3audiosink_flushed (Dxr3AudioSink * sink)