2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2005 Wim Taymans <wim@fluendo.com>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
28 #include <sys/types.h>
31 #ifdef HAVE_OSS_INCLUDE_IN_SYS
32 # include <sys/soundcard.h>
34 # ifdef HAVE_OSS_INCLUDE_IN_ROOT
35 # include <soundcard.h>
37 # ifdef HAVE_OSS_INCLUDE_IN_MACHINE
38 # include <machine/soundcard.h>
40 # error "What to include?"
41 # endif /* HAVE_OSS_INCLUDE_IN_MACHINE */
42 # endif /* HAVE_OSS_INCLUDE_IN_ROOT */
43 #endif /* HAVE_OSS_INCLUDE_IN_SYS */
46 #include <sys/ioctl.h>
49 #include "gstossdmabuffer.h"
51 GST_DEBUG_CATEGORY_EXTERN (oss_debug);
52 #define GST_CAT_DEFAULT oss_debug
54 static void gst_ossdmabuffer_class_init (GstOssDMABufferClass * klass);
55 static void gst_ossdmabuffer_init (GstOssDMABuffer * ossdmabuffer);
56 static void gst_ossdmabuffer_dispose (GObject * object);
57 static void gst_ossdmabuffer_finalize (GObject * object);
59 static gboolean gst_ossdmabuffer_acquire (GstRingBuffer * buf,
60 GstRingBufferSpec * spec);
61 static gboolean gst_ossdmabuffer_release (GstRingBuffer * buf);
62 static gboolean gst_ossdmabuffer_play (GstRingBuffer * buf);
63 static gboolean gst_ossdmabuffer_stop (GstRingBuffer * buf);
65 static GstRingBufferClass *parent_class = NULL;
68 gst_ossdmabuffer_get_type (void)
70 static GType ossdmabuffer_type = 0;
72 if (!ossdmabuffer_type) {
73 static const GTypeInfo ossdmabuffer_info = {
74 sizeof (GstOssDMABufferClass),
77 (GClassInitFunc) gst_ossdmabuffer_class_init,
80 sizeof (GstOssDMABuffer),
82 (GInstanceInitFunc) gst_ossdmabuffer_init,
87 g_type_register_static (GST_TYPE_RINGBUFFER, "GstOssDMABuffer",
88 &ossdmabuffer_info, 0);
90 return ossdmabuffer_type;
94 gst_ossdmabuffer_class_init (GstOssDMABufferClass * klass)
96 GObjectClass *gobject_class;
97 GstObjectClass *gstobject_class;
98 GstRingBufferClass *gstringbuffer_class;
100 gobject_class = (GObjectClass *) klass;
101 gstobject_class = (GstObjectClass *) klass;
102 gstringbuffer_class = (GstRingBufferClass *) klass;
104 parent_class = g_type_class_peek_parent (klass);
106 gobject_class->dispose = gst_ossdmabuffer_dispose;
107 gobject_class->finalize = gst_ossdmabuffer_finalize;
109 gstringbuffer_class->acquire = gst_ossdmabuffer_acquire;
110 gstringbuffer_class->release = gst_ossdmabuffer_release;
111 gstringbuffer_class->play = gst_ossdmabuffer_play;
112 gstringbuffer_class->stop = gst_ossdmabuffer_stop;
116 gst_ossdmabuffer_init (GstOssDMABuffer * ossdmabuffer)
118 ossdmabuffer->cond = g_cond_new ();
119 ossdmabuffer->element = g_object_new (GST_TYPE_OSSELEMENT, NULL);
123 gst_ossdmabuffer_dispose (GObject * object)
125 G_OBJECT_CLASS (parent_class)->dispose (object);
129 gst_ossdmabuffer_finalize (GObject * object)
131 GstOssDMABuffer *obuf = (GstOssDMABuffer *) object;
133 g_cond_free (obuf->cond);
135 G_OBJECT_CLASS (parent_class)->finalize (object);
139 gst_ossdmabuffer_func (GstRingBuffer * buf)
142 struct count_info count;
143 GstOssDMABuffer *obuf = (GstOssDMABuffer *) buf;
145 GST_OBJECT_LOCK (buf);
146 while (obuf->running) {
147 if (buf->state == GST_RINGBUFFER_STATE_PLAYING) {
150 GST_OBJECT_UNLOCK (buf);
152 segsize = buf->spec.segsize;
155 FD_SET (obuf->fd, &writeset);
157 select (obuf->fd + 1, NULL, &writeset, NULL, NULL);
159 if (ioctl (obuf->fd, SNDCTL_DSP_GETOPTR, &count) == -1) {
164 if (count.blocks > buf->spec.segtotal)
165 count.blocks = buf->spec.segtotal;
167 gst_ringbuffer_callback (buf, count.blocks);
169 GST_OBJECT_LOCK (buf);
171 GST_OSSDMABUFFER_SIGNAL (obuf);
172 GST_OSSDMABUFFER_WAIT (obuf);
175 GST_OBJECT_UNLOCK (buf);
179 gst_ossdmabuffer_acquire (GstRingBuffer * buf, GstRingBufferSpec * spec)
182 struct audio_buf_info info;
183 GstOssDMABuffer *obuf = (GstOssDMABuffer *) buf;
189 parsed = gst_osselement_parse_caps (obuf->element, spec->caps);
196 obuf->fd = open ("/dev/dsp", mode, 0);
197 if (obuf->fd == -1) {
201 //obuf->frag = 0x00040008;
202 obuf->frag = 0xffff000a;
204 tmp = obuf->element->format;
205 if (ioctl (obuf->fd, SNDCTL_DSP_SETFMT, &tmp) == -1) {
210 tmp = obuf->element->channels;
211 if (ioctl (obuf->fd, SNDCTL_DSP_STEREO, &tmp) == -1) {
216 tmp = obuf->element->channels;
217 if (ioctl (obuf->fd, SNDCTL_DSP_CHANNELS, &tmp) == -1) {
222 tmp = obuf->element->rate;
223 if (ioctl (obuf->fd, SNDCTL_DSP_SPEED, &tmp) == -1) {
228 if (ioctl (obuf->fd, SNDCTL_DSP_GETCAPS, &obuf->caps) == -1) {
230 fprintf (stderr, "Sorry but your sound driver is too old\n");
234 if (!(obuf->caps & DSP_CAP_TRIGGER) || !(obuf->caps & DSP_CAP_MMAP)) {
235 fprintf (stderr, "Sorry but your soundcard can't do this\n");
239 if (ioctl (obuf->fd, SNDCTL_DSP_SETFRAGMENT, &obuf->frag) == -1) {
240 perror ("SETFRAGMENT");
244 if (ioctl (obuf->fd, SNDCTL_DSP_GETOSPACE, &info) == -1) {
245 perror ("GETOSPACE");
249 buf->spec.segsize = info.fragsize;
250 buf->spec.segtotal = info.fragstotal;
251 size = info.fragsize * info.fragstotal;
252 g_print ("segsize %d, segtotal %d\n", info.fragsize, info.fragstotal);
254 mmap_buf = (caddr_t) mmap (NULL, size, PROT_WRITE,
255 MAP_FILE | MAP_SHARED, obuf->fd, 0);
257 if ((caddr_t) mmap_buf == (caddr_t) - 1) {
258 perror ("mmap (write)");
262 buf->data = gst_buffer_new ();
263 GST_BUFFER_DATA (buf->data) = mmap_buf;
264 GST_BUFFER_SIZE (buf->data) = size;
265 GST_BUFFER_FLAG_SET (buf->data, GST_BUFFER_DONTFREE);
268 if (ioctl (obuf->fd, SNDCTL_DSP_SETTRIGGER, &tmp) == -1) {
269 perror ("SETTRIGGER");
273 GST_OBJECT_LOCK (obuf);
274 obuf->running = TRUE;
275 obuf->thread = g_thread_create ((GThreadFunc) gst_ossdmabuffer_func,
277 GST_OSSDMABUFFER_WAIT (obuf);
278 GST_OBJECT_UNLOCK (obuf);
284 gst_ossdmabuffer_release (GstRingBuffer * buf)
286 GstOssDMABuffer *obuf = (GstOssDMABuffer *) buf;
288 gst_buffer_unref (buf->data);
290 GST_OBJECT_LOCK (obuf);
291 obuf->running = FALSE;
292 GST_OSSDMABUFFER_SIGNAL (obuf);
293 GST_OBJECT_UNLOCK (obuf);
295 g_thread_join (obuf->thread);
301 gst_ossdmabuffer_play (GstRingBuffer * buf)
304 GstOssDMABuffer *obuf;
306 obuf = (GstOssDMABuffer *) buf;
308 tmp = PCM_ENABLE_OUTPUT;
309 if (ioctl (obuf->fd, SNDCTL_DSP_SETTRIGGER, &tmp) == -1) {
310 perror ("SETTRIGGER");
312 GST_OSSDMABUFFER_SIGNAL (obuf);
318 gst_ossdmabuffer_stop (GstRingBuffer * buf)
321 GstOssDMABuffer *obuf;
323 obuf = (GstOssDMABuffer *) buf;
326 if (ioctl (obuf->fd, SNDCTL_DSP_SETTRIGGER, &tmp) == -1) {
327 perror ("SETTRIGGER");
329 GST_OSSDMABUFFER_WAIT (obuf);