source: roaraudio/plugins/gstreamer0.10/roarsink.c @ 2558:1318938953f6

Last change on this file since 2558:1318938953f6 was 2558:1318938953f6, checked in by phi, 15 years ago

set numer of channels correctly

File size: 11.2 KB
Line 
1//roarsink.c:
2/* GStreamer
3 * Copyright (C) <2005>      Arwed v. Merkatz <v.merkatz@gmx.net>
4 * Copyright (C) <2008,2009> Philipp 'ph3-der-loewe' Schafft
5 *                            <lion@lion.leolix.org>
6 *
7 * Roughly based on the gstreamer 0.8 esdsink plugin:
8 * Copyright (C) <2001> Richard Boulton <richard-gst@tartarus.org>
9 *
10 * roarsink.c: an RoarAudio audio sink
11 *
12 * This library is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Library General Public
14 * License as published by the Free Software Foundation; either
15 * version 2 of the License, or (at your option) any later version.
16 *
17 * This library is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20 * Library General Public License for more details.
21 *
22 * You should have received a copy of the GNU Library General Public
23 * License along with this library; if not, write to the
24 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 * Boston, MA 02111-1307, USA.
26 */
27
28
29#ifdef HAVE_CONFIG_H
30#include "config.h"
31#endif
32
33#include "roarsink.h"
34#include <unistd.h>
35#include <errno.h>
36
37#define _(x) (x)
38
39//#include <gst/gst-i18n-plugin.h>
40
41#ifndef ROAR_MAX_WRITE_SIZE
42#define ROAR_MAX_WRITE_SIZE (21 * 4096)
43#endif
44#define ROAR_BUF_SIZE 1764 /* this is dynamic for RoarAudio, use the most common value here for the moment */
45
46//GST_DEBUG_CATEGORY_EXTERN (roar_debug);
47//#define GST_CAT_DEFAULT roar_debug
48
49/* elementfactory information */
50static const GstElementDetails roarsink_details =
51GST_ELEMENT_DETAILS("RoarAudio audio sink",
52    "Sink/Audio",
53    "Plays audio to an RoarAudio server",
54    "Philipp 'ph3-der-loewe' Schafft <lion@lion.leolix.org>");
55
56enum
57{
58  PROP_0,
59  PROP_HOST
60};
61
62#define _QM(x) #x
63#define  QM(x) _QM(x)
64
65static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE("sink",
66    GST_PAD_SINK,
67    GST_PAD_ALWAYS,
68    GST_STATIC_CAPS("audio/x-raw-int, "
69        "endianness = (int) BYTE_ORDER, "
70        "signed = (boolean) TRUE, "
71        "width = (int) 16, "
72        "depth = (int) 16, "
73        "rate = (int) [ 1, MAX ], "
74        "channels = (int) [ 1, " QM(ROAR_MAX_CHANNELS) " ]; "
75
76        "audio/x-raw-int, "
77        "signed = (boolean) { true, false }, "
78        "width = (int) 8, "
79        "depth = (int) 8, "
80        "rate = (int) [ 1, MAX ], "
81        "channels = (int) [ 1, " QM(ROAR_MAX_CHANNELS) " ]"
82       )
83    );
84
85#undef _QM
86#undef  QM
87
88static void gst_roarsink_finalize (GObject * object);
89
90static GstCaps *gst_roarsink_getcaps (GstBaseSink * bsink);
91
92static gboolean gst_roarsink_open (GstAudioSink * asink);
93static gboolean gst_roarsink_close (GstAudioSink * asink);
94static gboolean gst_roarsink_prepare (GstAudioSink * asink,
95    GstRingBufferSpec * spec);
96static gboolean gst_roarsink_unprepare (GstAudioSink * asink);
97static guint gst_roarsink_write (GstAudioSink * asink, gpointer data,
98    guint length);
99static guint gst_roarsink_delay (GstAudioSink * asink);
100static void gst_roarsink_reset (GstAudioSink * asink);
101
102static void gst_roarsink_set_property (GObject * object, guint prop_id,
103    const GValue * value, GParamSpec * pspec);
104static void gst_roarsink_get_property (GObject * object, guint prop_id,
105    GValue * value, GParamSpec * pspec);
106
107GST_BOILERPLATE (GstRoarSink, gst_roarsink, GstAudioSink, GST_TYPE_AUDIO_SINK);
108
109static void gst_roarsink_base_init (gpointer g_class) {
110  GstElementClass *element_class = GST_ELEMENT_CLASS(g_class);
111
112  gst_element_class_add_pad_template(element_class,
113      gst_static_pad_template_get(&sink_factory));
114  gst_element_class_set_details(element_class, &roarsink_details);
115}
116
117static void gst_roarsink_class_init (GstRoarSinkClass * klass) {
118  GObjectClass *gobject_class;
119  GstBaseSinkClass *gstbasesink_class;
120  GstBaseAudioSinkClass *gstbaseaudiosink_class;
121  GstAudioSinkClass *gstaudiosink_class;
122
123  gobject_class          = (GObjectClass *) klass;
124  gstbasesink_class      = (GstBaseSinkClass *) klass;
125  gstbaseaudiosink_class = (GstBaseAudioSinkClass *) klass;
126  gstaudiosink_class     = (GstAudioSinkClass *) klass;
127
128  parent_class = g_type_class_peek_parent(klass);
129
130  gobject_class->finalize = gst_roarsink_finalize;
131
132  gstbasesink_class->get_caps   = GST_DEBUG_FUNCPTR(gst_roarsink_getcaps);
133
134  gstaudiosink_class->open      = GST_DEBUG_FUNCPTR(gst_roarsink_open);
135  gstaudiosink_class->close     = GST_DEBUG_FUNCPTR(gst_roarsink_close);
136  gstaudiosink_class->prepare   = GST_DEBUG_FUNCPTR(gst_roarsink_prepare);
137  gstaudiosink_class->unprepare = GST_DEBUG_FUNCPTR(gst_roarsink_unprepare);
138  gstaudiosink_class->write     = GST_DEBUG_FUNCPTR(gst_roarsink_write);
139  gstaudiosink_class->delay     = GST_DEBUG_FUNCPTR(gst_roarsink_delay);
140  gstaudiosink_class->reset     = GST_DEBUG_FUNCPTR(gst_roarsink_reset);
141
142  gobject_class->set_property   = gst_roarsink_set_property;
143  gobject_class->get_property   = gst_roarsink_get_property;
144
145  /* default value is filled in the _init method */
146  g_object_class_install_property(gobject_class, PROP_HOST,
147      g_param_spec_string("host", "Host",
148          "The host running the RoarAudio daemon", NULL, G_PARAM_READWRITE));
149}
150
151static void gst_roarsink_init (GstRoarSink * roarsink, GstRoarSinkClass * klass) {
152  memset(&(roarsink->con), 0, sizeof(roarsink->con));
153  roar_connect_fh(&(roarsink->con), -1);
154  roarsink->fd     = -1;
155  roarsink->host   = NULL;
156}
157
158static void gst_roarsink_finalize (GObject * object) {
159  GstRoarSink *roarsink = GST_ROARSINK(object);
160
161  gst_caps_replace(&roarsink->cur_caps, NULL);
162  if ( roarsink->host != NULL )
163   g_free(roarsink->host);
164
165  G_OBJECT_CLASS(parent_class)->finalize(object);
166}
167
168static GstCaps * gst_roarsink_getcaps (GstBaseSink * bsink) {
169  GstRoarSink *roarsink;
170
171  roarsink = GST_ROARSINK(bsink);
172
173  /* no fd, we're done with the template caps */
174  if (roar_get_connection_fh(&(roarsink->con)) < 0 || roarsink->cur_caps == NULL) {
175    GST_LOG_OBJECT(roarsink, "getcaps called, returning template caps");
176    return NULL;
177  }
178
179  GST_LOG_OBJECT(roarsink, "returning %" GST_PTR_FORMAT, roarsink->cur_caps);
180
181  return gst_caps_ref(roarsink->cur_caps);
182}
183
184static gboolean gst_roarsink_open(GstAudioSink * asink) {
185  GstPadTemplate *pad_template;
186  GstRoarSink *roarsink;
187  gint i;
188  struct roar_stream oinfo;
189
190  roarsink = GST_ROARSINK(asink);
191
192  GST_DEBUG_OBJECT(roarsink, "open");
193
194  /* now try to connect to any existing/running sound daemons */
195  if ( roar_simple_connect(&(roarsink->con), roarsink->host, "gstreamer client") == -1 )
196    goto couldnt_connect;
197
198  /* get server info */
199  if ( roar_server_oinfo(&(roarsink->con), &oinfo) == -1 )
200    goto no_server_info;
201
202  GST_INFO_OBJECT(roarsink, "got server info rate: %i", oinfo.info.rate);
203
204  pad_template = gst_static_pad_template_get(&sink_factory);
205  roarsink->cur_caps = gst_caps_copy(gst_pad_template_get_caps(pad_template));
206
207  for (i = 0; i < roarsink->cur_caps->structs->len; i++) {
208    GstStructure *s;
209
210    s = gst_caps_get_structure(roarsink->cur_caps, i);
211    gst_structure_set(s, "rate", G_TYPE_INT, oinfo.info.rate, NULL);
212  }
213
214  GST_INFO_OBJECT(roarsink, "server caps: %" GST_PTR_FORMAT, roarsink->cur_caps);
215
216  return TRUE;
217
218  /* ERRORS */
219couldnt_connect:
220  {
221    GST_ELEMENT_ERROR(roarsink, RESOURCE, OPEN_WRITE,
222        (_("Could not establish connection to sound server")),
223        ("can't open connection to esound server"));
224    return FALSE;
225  }
226no_server_info:
227  {
228    GST_ELEMENT_ERROR(roarsink, RESOURCE, OPEN_WRITE,
229        (_("Failed to query sound server capabilities")),
230        ("couldn't get server info!"));
231    return FALSE;
232  }
233}
234
235static gboolean gst_roarsink_close (GstAudioSink * asink) {
236  GstRoarSink *roarsink = GST_ROARSINK(asink);
237
238  GST_DEBUG_OBJECT(roarsink, "close");
239
240  gst_caps_replace(&roarsink->cur_caps, NULL);
241
242  roar_disconnect(&(roarsink->con));
243
244  if ( roarsink->fd != -1 )
245   close(roarsink->fd);
246
247  return TRUE;
248}
249
250static gboolean gst_roarsink_prepare (GstAudioSink * asink, GstRingBufferSpec * spec) {
251  GstRoarSink *roarsink = GST_ROARSINK(asink);
252
253  GST_DEBUG_OBJECT(roarsink, "prepare");
254
255 
256  GST_INFO_OBJECT(roarsink, "attempting to open data connection to esound server");
257
258  roarsink->fd = roar_simple_new_stream(&(roarsink->con), spec->rate, spec->channels, spec->depth,
259                                        ROAR_CODEC_DEFAULT, ROAR_DIR_PLAY);
260
261  if ( roarsink->fd == -1 )
262    goto cannot_open;
263
264  roarsink->rate = spec->rate;
265
266  spec->segsize  = ROAR_BUF_SIZE;
267  spec->segtotal = (ROAR_MAX_WRITE_SIZE / spec->segsize);
268
269  /* FIXME: this is wrong for signed ints (and the
270   * audioringbuffers should do it for us anyway) */
271  spec->silence_sample[0] = 0;
272  spec->silence_sample[1] = 0;
273  spec->silence_sample[2] = 0;
274  spec->silence_sample[3] = 0;
275
276  GST_INFO_OBJECT(roarsink, "successfully opened connection to esound server");
277
278  return TRUE;
279
280  /* ERRORS */
281cannot_open:
282  {
283    GST_ELEMENT_ERROR(roarsink, RESOURCE, OPEN_WRITE,
284        (_("Could not establish connection to sound server")),
285        ("can't open connection to RoarAudio server"));
286    return FALSE;
287  }
288}
289
290static gboolean gst_roarsink_unprepare (GstAudioSink * asink) {
291  GstRoarSink *roarsink = GST_ROARSINK(asink);
292
293  if ((roarsink->fd == -1) && (roar_get_connection_fh(&(roarsink->con)) == -1))
294    return TRUE;
295
296  close(roarsink->fd);
297  roarsink->fd = -1;
298
299  GST_INFO_OBJECT(roarsink, "closed sound device");
300
301  return TRUE;
302}
303
304
305static guint gst_roarsink_write (GstAudioSink * asink, gpointer data, guint length) {
306  GstRoarSink *roarsink = GST_ROARSINK(asink);
307  gint to_write = 0;
308
309  to_write = length;
310
311  while (to_write > 0) {
312    int done;
313
314    done = write(roarsink->fd, data, to_write);
315
316    if (done < 0)
317      goto write_error;
318
319    to_write -= done;
320    data += done;
321  }
322  return length;
323
324  /* ERRORS */
325write_error:
326  {
327    GST_ELEMENT_ERROR(roarsink, RESOURCE, WRITE,
328        ("Failed to write data to the RoarAudio daemon"), GST_ERROR_SYSTEM);
329    return 0;
330  }
331}
332
333static guint gst_roarsink_delay (GstAudioSink * asink) {
334//  GstRoarSink *roarsink = GST_ROARSINK (asink);
335  guint latency;
336
337  latency = 441; // compile type depending and link level deppendent,
338                 // use default value for local operations here for the moment
339
340  GST_DEBUG_OBJECT(asink, "got latency: %u", latency);
341
342  return latency;
343}
344
345static void gst_roarsink_reset (GstAudioSink * asink) {
346  GST_DEBUG_OBJECT(asink, "reset called");
347}
348
349static void gst_roarsink_set_property (GObject * object, guint prop_id, const GValue * value,
350                                       GParamSpec * pspec) {
351  GstRoarSink *roarsink = GST_ROARSINK(object);
352
353  switch (prop_id) {
354    case PROP_HOST:
355      if ( roarsink->host != NULL )
356       g_free(roarsink->host);
357      roarsink->host = g_value_dup_string(value);
358     break;
359    default:
360      break;
361  }
362}
363
364static void gst_roarsink_get_property (GObject * object, guint prop_id, GValue * value,
365                                       GParamSpec * pspec) {
366  GstRoarSink *roarsink = GST_ROARSINK(object);
367
368  switch (prop_id) {
369    case PROP_HOST:
370      g_value_set_string(value, roarsink->host);
371     break;
372    default:
373      G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
374     break;
375  }
376}
377
378gboolean gst_roarsink_factory_init (GstPlugin * plugin) {
379  if (!gst_element_register(plugin, "roarsink", GST_RANK_MARGINAL,
380                            GST_TYPE_ROARSINK))
381    return FALSE;
382
383  return TRUE;
384}
Note: See TracBrowser for help on using the repository browser.