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 gboolean dxr3audiosink_set_clock (GstElement * element,
99 static GstPadLinkReturn dxr3audiosink_pcm_sinklink (GstPad * pad,
100 const GstCaps * caps);
101 static void dxr3audiosink_set_scr (Dxr3AudioSink * sink, guint32 scr);
103 static gboolean dxr3audiosink_handle_event (GstPad * pad, GstEvent * event);
104 static void dxr3audiosink_chain_pcm (GstPad * pad, GstData * buf);
105 static void dxr3audiosink_chain_ac3 (GstPad * pad, GstData * buf);
107 /* static void dxr3audiosink_wait (Dxr3AudioSink *sink, */
108 /* GstClockTime time); */
109 /* static int dxr3audiosink_mvcommand (Dxr3AudioSink *sink, */
112 static GstStateChangeReturn dxr3audiosink_change_state (GstElement * element,
113 GstStateChange transition);
115 static void dxr3audiosink_flushed (Dxr3AudioSink * sink);
117 static GstElementClass *parent_class = NULL;
118 static guint dxr3audiosink_signals[LAST_SIGNAL] = { 0 };
122 dxr3audiosink_get_type (void)
124 static GType dxr3audiosink_type = 0;
126 if (!dxr3audiosink_type) {
127 static const GTypeInfo dxr3audiosink_info = {
128 sizeof (Dxr3AudioSinkClass),
129 (GBaseInitFunc) dxr3audiosink_base_init,
131 (GClassInitFunc) dxr3audiosink_class_init,
134 sizeof (Dxr3AudioSink),
136 (GInstanceInitFunc) dxr3audiosink_init,
139 dxr3audiosink_type = g_type_register_static (GST_TYPE_ELEMENT,
140 "Dxr3AudioSink", &dxr3audiosink_info, 0);
143 return dxr3audiosink_type;
148 dxr3audiosink_base_init (Dxr3AudioSinkClass * klass)
150 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
152 gst_element_class_add_pad_template (element_class,
153 gst_static_pad_template_get (&dxr3audiosink_pcm_sink_factory));
154 gst_element_class_add_pad_template (element_class,
155 gst_static_pad_template_get (&dxr3audiosink_ac3_sink_factory));
156 gst_element_class_set_details_simple (element_class,
157 "dxr3/Hollywood+ mpeg decoder board audio plugin", "Audio/Sink",
158 "Feeds audio to Sigma Designs em8300 based boards",
159 "Martin Soto <martinsoto@users.sourceforge.net>");
163 dxr3audiosink_class_init (Dxr3AudioSinkClass * klass)
165 GObjectClass *gobject_class;
166 GstElementClass *gstelement_class;
168 gobject_class = (GObjectClass *) klass;
169 gstelement_class = (GstElementClass *) klass;
171 parent_class = g_type_class_peek_parent (klass);
173 dxr3audiosink_signals[SIGNAL_FLUSHED] =
174 g_signal_new ("flushed", G_TYPE_FROM_CLASS (klass),
176 G_STRUCT_OFFSET (Dxr3AudioSinkClass, flushed),
177 NULL, NULL, dxr3_marshal_VOID__VOID, G_TYPE_NONE, 0);
179 klass->flushed = dxr3audiosink_flushed;
181 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_DIGITAL_PCM,
182 g_param_spec_boolean ("digital-pcm", "Digital PCM",
183 "Use the digital output for PCM sound", FALSE, G_PARAM_READWRITE));
185 gobject_class->set_property = dxr3audiosink_set_property;
186 gobject_class->get_property = dxr3audiosink_get_property;
188 gstelement_class->change_state = dxr3audiosink_change_state;
189 gstelement_class->set_clock = dxr3audiosink_set_clock;
194 dxr3audiosink_init (Dxr3AudioSink * sink)
196 GstPadTemplate *temp;
198 /* Create the PCM pad. */
199 temp = gst_static_pad_template_get (&dxr3audiosink_pcm_sink_factory);
200 sink->pcm_sinkpad = gst_pad_new_from_template (temp, "pcm_sink");
201 gst_pad_set_chain_function (sink->pcm_sinkpad, dxr3audiosink_chain_pcm);
202 gst_pad_set_link_function (sink->pcm_sinkpad, dxr3audiosink_pcm_sinklink);
203 gst_element_add_pad (GST_ELEMENT (sink), sink->pcm_sinkpad);
205 /* Create the AC3 pad. */
206 temp = gst_static_pad_template_get (&dxr3audiosink_ac3_sink_factory);
207 sink->ac3_sinkpad = gst_pad_new_from_template (temp, "ac3_sink");
208 gst_pad_set_chain_function (sink->ac3_sinkpad, dxr3audiosink_chain_ac3);
209 gst_element_add_pad (GST_ELEMENT (sink), sink->ac3_sinkpad);
211 GST_OBJECT_FLAG_SET (GST_ELEMENT (sink), GST_ELEMENT_EVENT_AWARE);
213 sink->card_number = 0;
215 sink->audio_filename = NULL;
218 sink->control_filename = NULL;
219 sink->control_fd = -1;
221 /* Since we don't know any better, we set the initial scr to 0. */
224 /* Initially don't use digital output. */
225 sink->digital_pcm = FALSE;
227 /* Initially there's no padder. */
230 sink->mode = DXR3AUDIOSINK_MODE_NONE;
235 dxr3audiosink_set_property (GObject * object, guint prop_id,
236 const GValue * value, GParamSpec * pspec)
240 sink = DXR3AUDIOSINK (object);
243 case ARG_DIGITAL_PCM:
244 sink->digital_pcm = g_value_get_boolean (value);
245 /* Refresh the setup of the device. */
246 if (sink->mode == DXR3AUDIOSINK_MODE_PCM) {
247 dxr3audiosink_set_mode_pcm (sink);
249 g_object_notify (G_OBJECT (sink), "digital-pcm");
252 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
259 dxr3audiosink_get_property (GObject * object, guint prop_id,
260 GValue * value, GParamSpec * pspec)
264 g_return_if_fail (GST_IS_DXR3AUDIOSINK (object));
266 sink = DXR3AUDIOSINK (object);
269 case ARG_DIGITAL_PCM:
270 g_value_set_boolean (value, sink->digital_pcm);
273 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
280 dxr3audiosink_open (Dxr3AudioSink * sink)
282 g_return_val_if_fail (!GST_OBJECT_FLAG_IS_SET (sink, DXR3AUDIOSINK_OPEN),
285 /* Compute the name of the audio device file. */
286 sink->audio_filename = g_strdup_printf ("/dev/em8300_ma-%d",
289 sink->audio_fd = open (sink->audio_filename, O_WRONLY);
290 if (sink->audio_fd < 0) {
291 GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_WRITE,
292 (_("Could not open audio device \"%s\" for writing."),
293 sink->audio_filename), GST_ERROR_SYSTEM);
297 /* Open the control device. */
298 sink->control_filename = g_strdup_printf ("/dev/em8300-%d",
301 sink->control_fd = open (sink->control_filename, O_WRONLY);
302 if (sink->control_fd < 0) {
303 GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_WRITE,
304 (_("Could not open control device \"%s\" for writing."),
305 sink->control_filename), GST_ERROR_SYSTEM);
309 GST_OBJECT_FLAG_SET (sink, DXR3AUDIOSINK_OPEN);
311 dxr3audiosink_set_mode_pcm (sink);
318 * dxr3audiosink_set_mode:
319 * @sink: The sink element to operate on.
321 * Set the operation mode of the element to PCM.
324 dxr3audiosink_set_mode_pcm (Dxr3AudioSink * sink)
326 int tmp, oss_mode, audiomode;
328 if (sink->audio_fd == -1 || sink->control_fd == -1) {
332 /* Set the audio device mode. */
333 oss_mode = (G_BYTE_ORDER == G_BIG_ENDIAN ? AFMT_S16_BE : AFMT_S16_LE);
335 if (ioctl (sink->audio_fd, SNDCTL_DSP_SETFMT, &tmp) < 0 || tmp != oss_mode) {
336 GST_ELEMENT_ERROR (sink, RESOURCE, SETTINGS,
337 (_("Could not configure audio device \"%s\"."), sink->audio_filename),
342 /* Set the card's general audio output mode. */
343 audiomode = sink->digital_pcm ?
344 EM8300_AUDIOMODE_DIGITALPCM : EM8300_AUDIOMODE_ANALOG;
345 ioctl (sink->control_fd, EM8300_IOCTL_SET_AUDIOMODE, &audiomode);
347 /* Set the sampling rate. */
349 if (ioctl (sink->audio_fd, SNDCTL_DSP_SPEED, &tmp) < 0) {
350 GST_ELEMENT_ERROR (sink, RESOURCE, SETTINGS,
351 (_("Could not set audio device \"%s\" to %d Hz."), sink->audio_filename,
352 sink->rate), GST_ERROR_SYSTEM);
356 /* Get rid of the padder, if any. */
357 if (sink->padder != NULL) {
358 g_free (sink->padder);
362 sink->mode = DXR3AUDIOSINK_MODE_PCM;
369 * dxr3audiosink_set_mode:
370 * @sink: The sink element to operate on
372 * Set the operation mode of the element to AC3.
375 dxr3audiosink_set_mode_ac3 (Dxr3AudioSink * sink)
379 if (sink->audio_fd == -1 || sink->control_fd == -1) {
383 /* Set the sampling rate. */
385 if (ioctl (sink->audio_fd, SNDCTL_DSP_SPEED, &tmp) < 0 ||
386 tmp != AC3_BYTE_RATE) {
387 GST_ELEMENT_ERROR (sink, RESOURCE, SETTINGS,
388 (_("Could not set audio device \"%s\" to %d Hz."), sink->audio_filename,
389 AC3_BYTE_RATE), GST_ERROR_SYSTEM);
393 /* Set the card's general audio output mode to AC3. */
394 audiomode = EM8300_AUDIOMODE_DIGITALAC3;
395 ioctl (sink->control_fd, EM8300_IOCTL_SET_AUDIOMODE, &audiomode);
397 /* Create a padder if necessary, */
398 if (sink->padder == NULL) {
399 sink->padder = g_malloc (sizeof (ac3_padder));
400 ac3p_init (sink->padder);
403 sink->mode = DXR3AUDIOSINK_MODE_AC3;
410 dxr3audiosink_close (Dxr3AudioSink * sink)
412 g_return_if_fail (GST_OBJECT_FLAG_IS_SET (sink, DXR3AUDIOSINK_OPEN));
414 if (close (sink->audio_fd) != 0) {
415 GST_ELEMENT_ERROR (sink, RESOURCE, CLOSE,
416 (_("Could not close audio device \"%s\"."), sink->audio_filename),
421 if (close (sink->control_fd) != 0) {
422 GST_ELEMENT_ERROR (sink, RESOURCE, CLOSE,
423 (_("Could not close control device \"%s\"."), sink->audio_filename),
428 GST_OBJECT_FLAG_UNSET (sink, DXR3AUDIOSINK_OPEN);
430 g_free (sink->audio_filename);
431 sink->audio_filename = NULL;
433 g_free (sink->control_filename);
434 sink->control_filename = NULL;
436 /* Get rid of the padder, if any. */
437 if (sink->padder != NULL) {
438 g_free (sink->padder);
445 dxr3audiosink_set_clock (GstElement * element, GstClock * clock)
447 Dxr3AudioSink *src = DXR3AUDIOSINK (element);
451 return GST_ELEMENT_CLASS (parent_class)->set_clock (element, clock);
455 static GstPadLinkReturn
456 dxr3audiosink_pcm_sinklink (GstPad * pad, const GstCaps * caps)
458 Dxr3AudioSink *sink = DXR3AUDIOSINK (gst_pad_get_parent (pad));
459 GstStructure *structure = gst_caps_get_structure (caps, 0);
462 if (!gst_caps_is_fixed (caps)) {
463 return GST_PAD_LINK_DELAYED;
466 gst_structure_get_int (structure, "rate", &rate);
469 return GST_PAD_LINK_OK;
474 dxr3audiosink_set_scr (Dxr3AudioSink * sink, guint32 scr)
478 /* fprintf (stderr, "====== Adjusting SCR\n"); */
479 ioctl (sink->control_fd, EM8300_IOCTL_SCR_SET, &zero);
480 ioctl (sink->control_fd, EM8300_IOCTL_SCR_SET, &scr);
485 dxr3audiosink_handle_event (GstPad * pad, GstEvent * event)
488 Dxr3AudioSink *sink = DXR3AUDIOSINK (gst_pad_get_parent (pad));
490 type = event ? GST_EVENT_TYPE (event) : GST_EVENT_UNKNOWN;
493 case GST_EVENT_FLUSH:
494 if (sink->control_fd >= 0) {
497 if (sink->mode == DXR3AUDIOSINK_MODE_AC3) {
498 audiomode = EM8300_AUDIOMODE_DIGITALPCM;
499 ioctl (sink->control_fd, EM8300_IOCTL_SET_AUDIOMODE, &audiomode);
500 audiomode = EM8300_AUDIOMODE_DIGITALAC3;
501 ioctl (sink->control_fd, EM8300_IOCTL_SET_AUDIOMODE, &audiomode);
504 /* Report the flush operation. */
505 g_signal_emit (G_OBJECT (sink),
506 dxr3audiosink_signals[SIGNAL_FLUSHED], 0);
510 gst_pad_event_default (pad, event);
519 dxr3audiosink_chain_pcm (GstPad * pad, GstData * _data)
522 gint bytes_written = 0;
525 g_return_if_fail (pad != NULL);
526 g_return_if_fail (GST_IS_PAD (pad));
527 g_return_if_fail (_data != NULL);
529 sink = DXR3AUDIOSINK (gst_pad_get_parent (pad));
531 if (GST_IS_EVENT (_data)) {
532 dxr3audiosink_handle_event (pad, GST_EVENT (_data));
536 buf = GST_BUFFER (_data);
538 if (sink->mode != DXR3AUDIOSINK_MODE_PCM) {
539 /* Switch to PCM mode. */
540 dxr3audiosink_set_mode_pcm (sink);
543 if (GST_OBJECT_FLAG_IS_SET (sink, DXR3AUDIOSINK_OPEN)) {
544 if (GST_BUFFER_TIMESTAMP (buf) != GST_CLOCK_TIME_NONE) {
545 /* We have a new scr value. */
546 sink->scr = GSTTIME_TO_MPEGTIME (GST_BUFFER_TIMESTAMP (buf));
549 /* Update the system reference clock (SCR) in the card. */
551 unsigned in, out, odelay;
554 ioctl (sink->control_fd, EM8300_IOCTL_SCR_GET, &out);
556 ioctl (sink->audio_fd, SNDCTL_DSP_GETODELAY, &odelay);
558 in = MPEGTIME_TO_DXRTIME (sink->scr - (odelay * 90) / 192);
559 diff = in > out ? in - out : out - in;
561 dxr3audiosink_set_scr (sink, in);
565 /* Update our SCR value. */
566 sink->scr += (unsigned) (GST_BUFFER_SIZE (buf) *
567 (90000.0 / ((float) sink->rate * 4)));
569 /* Write the buffer to the sound device. */
570 bytes_written = write (sink->audio_fd, GST_BUFFER_DATA (buf),
571 GST_BUFFER_SIZE (buf));
572 if (bytes_written < GST_BUFFER_SIZE (buf)) {
573 fprintf (stderr, "dxr3audiosink: Warning: %d bytes should be "
574 "written, only %d bytes written\n",
575 GST_BUFFER_SIZE (buf), bytes_written);
579 gst_buffer_unref (buf);
584 dxr3audiosink_chain_ac3 (GstPad * pad, GstData * _data)
587 gint bytes_written = 0;
590 g_return_if_fail (pad != NULL);
591 g_return_if_fail (GST_IS_PAD (pad));
592 g_return_if_fail (_data != NULL);
594 sink = DXR3AUDIOSINK (gst_pad_get_parent (pad));
596 if (GST_IS_EVENT (_data)) {
597 dxr3audiosink_handle_event (pad, GST_EVENT (_data));
601 buf = GST_BUFFER (_data);
603 if (sink->mode != DXR3AUDIOSINK_MODE_AC3) {
604 /* Switch to AC3 mode. */
605 dxr3audiosink_set_mode_ac3 (sink);
608 if (GST_OBJECT_FLAG_IS_SET (sink, DXR3AUDIOSINK_OPEN)) {
611 if (GST_BUFFER_TIMESTAMP (buf) != GST_CLOCK_TIME_NONE) {
612 /* We have a new scr value. */
614 /* fprintf (stderr, "------ Audio Time %.04f\n", */
615 /* (double) GST_BUFFER_TIMESTAMP (buf) / GST_SECOND); */
617 sink->scr = GSTTIME_TO_MPEGTIME (GST_BUFFER_TIMESTAMP (buf));
620 /* Push the new data into the padder. */
621 ac3p_push_data (sink->padder, GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
623 /* Parse the data. */
624 event = ac3p_parse (sink->padder);
625 while (event != AC3P_EVENT_PUSH) {
627 case AC3P_EVENT_FRAME:
628 /* We have a new frame: */
630 /* Update the system reference clock (SCR) in the card. */
632 unsigned in, out, odelay;
635 ioctl (sink->control_fd, EM8300_IOCTL_SCR_GET, &out);
637 ioctl (sink->audio_fd, SNDCTL_DSP_GETODELAY, &odelay);
638 /* 192000 bytes/sec */
640 in = MPEGTIME_TO_DXRTIME (sink->scr - (odelay * 90) / 192);
641 diff = in > out ? in - out : out - in;
643 dxr3audiosink_set_scr (sink, in);
647 /* Update our SCR value. */
648 sink->scr += TIME_FOR_BYTES (ac3p_frame_size (sink->padder));
650 /* Write the frame to the sound device. */
651 bytes_written = write (sink->audio_fd, ac3p_frame (sink->padder),
652 AC3P_IEC_FRAME_SIZE);
654 if (bytes_written < AC3P_IEC_FRAME_SIZE) {
655 fprintf (stderr, "dxr3audiosink: Warning: %d bytes should be "
656 "written, only %d bytes written\n",
657 AC3P_IEC_FRAME_SIZE, bytes_written);
663 event = ac3p_parse (sink->padder);
667 gst_buffer_unref (buf);
672 * dxr3audiosink_wait:
674 * Make the sink wait the specified amount of time.
677 dxr3audiosink_wait (Dxr3AudioSink * sink, GstClockTime time)
680 GstClockTimeDiff jitter;
682 GstClockTime current_time = gst_clock_get_time (sink->clock);
684 id = gst_clock_new_single_shot_id (sink->clock, current_time + time);
685 ret = gst_clock_id_wait (id, &jitter);
686 gst_clock_id_free (id);
691 dxr3audiosink_mvcommand (Dxr3AudioSink * sink, int command)
693 em8300_register_t regs;
695 regs.microcode_register = 1;
699 return ioctl (sink->control_fd, EM8300_IOCTL_WRITEREG, ®s);
703 static GstStateChangeReturn
704 dxr3audiosink_change_state (GstElement * element, GstStateChange transition)
706 g_return_val_if_fail (GST_IS_DXR3AUDIOSINK (element),
707 GST_STATE_CHANGE_FAILURE);
709 if (GST_STATE_PENDING (element) == GST_STATE_NULL) {
710 if (GST_OBJECT_FLAG_IS_SET (element, DXR3AUDIOSINK_OPEN)) {
711 dxr3audiosink_close (DXR3AUDIOSINK (element));
714 if (!GST_OBJECT_FLAG_IS_SET (element, DXR3AUDIOSINK_OPEN)) {
715 if (!dxr3audiosink_open (DXR3AUDIOSINK (element))) {
716 return GST_STATE_CHANGE_FAILURE;
721 if (GST_ELEMENT_CLASS (parent_class)->change_state) {
722 return GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
725 return GST_STATE_CHANGE_SUCCESS;
730 * dxr3audiosink_flushed:
732 * Default do nothing implementation for the "flushed" signal. The
733 * "flushed" signal will be fired right after flushing the hardware
734 * queues due to a received flush event
737 dxr3audiosink_flushed (Dxr3AudioSink * sink)