source: roaraudio/plugins/gstreamer0.10/roarsink.c @ 2559:4e150a46ac8d

Last change on this file since 2559:4e150a46ac8d was 2559:4e150a46ac8d, checked in by phi, 15 years ago

better formating

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(
69        "audio/x-raw-int, "
70        "endianness = (int) BYTE_ORDER, "
71        "signed = (boolean) TRUE, "
72        "width = (int) 16, "
73        "depth = (int) 16, "
74        "rate = (int) [ 1, MAX ], "
75        "channels = (int) [ 1, " QM(ROAR_MAX_CHANNELS) " ]; "
76
77        "audio/x-raw-int, "
78        "signed = (boolean) { true, false }, "
79        "width = (int) 8, "
80        "depth = (int) 8, "
81        "rate = (int) [ 1, MAX ], "
82        "channels = (int) [ 1, " QM(ROAR_MAX_CHANNELS) " ]"
83       )
84    );
85
86#undef _QM
87#undef  QM
88
89static void gst_roarsink_finalize (GObject * object);
90
91static GstCaps *gst_roarsink_getcaps (GstBaseSink * bsink);
92
93static gboolean gst_roarsink_open (GstAudioSink * asink);
94static gboolean gst_roarsink_close (GstAudioSink * asink);
95static gboolean gst_roarsink_prepare (GstAudioSink * asink,
96    GstRingBufferSpec * spec);
97static gboolean gst_roarsink_unprepare (GstAudioSink * asink);
98static guint gst_roarsink_write (GstAudioSink * asink, gpointer data,
99    guint length);
100static guint gst_roarsink_delay (GstAudioSink * asink);
101static void gst_roarsink_reset (GstAudioSink * asink);
102
103static void gst_roarsink_set_property (GObject * object, guint prop_id,
104    const GValue * value, GParamSpec * pspec);
105static void gst_roarsink_get_property (GObject * object, guint prop_id,
106    GValue * value, GParamSpec * pspec);
107
108GST_BOILERPLATE (GstRoarSink, gst_roarsink, GstAudioSink, GST_TYPE_AUDIO_SINK);
109
110static void gst_roarsink_base_init (gpointer g_class) {
111  GstElementClass *element_class = GST_ELEMENT_CLASS(g_class);
112
113  gst_element_class_add_pad_template(element_class,
114      gst_static_pad_template_get(&sink_factory));
115  gst_element_class_set_details(element_class, &roarsink_details);
116}
117
118static void gst_roarsink_class_init (GstRoarSinkClass * klass) {
119  GObjectClass *gobject_class;
120  GstBaseSinkClass *gstbasesink_class;
121  GstBaseAudioSinkClass *gstbaseaudiosink_class;
122  GstAudioSinkClass *gstaudiosink_class;
123
124  gobject_class          = (GObjectClass *) klass;
125  gstbasesink_class      = (GstBaseSinkClass *) klass;
126  gstbaseaudiosink_class = (GstBaseAudioSinkClass *) klass;
127  gstaudiosink_class     = (GstAudioSinkClass *) klass;
128
129  parent_class = g_type_class_peek_parent(klass);
130
131  gobject_class->finalize = gst_roarsink_finalize;
132
133  gstbasesink_class->get_caps   = GST_DEBUG_FUNCPTR(gst_roarsink_getcaps);
134
135  gstaudiosink_class->open      = GST_DEBUG_FUNCPTR(gst_roarsink_open);
136  gstaudiosink_class->close     = GST_DEBUG_FUNCPTR(gst_roarsink_close);
137  gstaudiosink_class->prepare   = GST_DEBUG_FUNCPTR(gst_roarsink_prepare);
138  gstaudiosink_class->unprepare = GST_DEBUG_FUNCPTR(gst_roarsink_unprepare);
139  gstaudiosink_class->write     = GST_DEBUG_FUNCPTR(gst_roarsink_write);
140  gstaudiosink_class->delay     = GST_DEBUG_FUNCPTR(gst_roarsink_delay);
141  gstaudiosink_class->reset     = GST_DEBUG_FUNCPTR(gst_roarsink_reset);
142
143  gobject_class->set_property   = gst_roarsink_set_property;
144  gobject_class->get_property   = gst_roarsink_get_property;
145
146  /* default value is filled in the _init method */
147  g_object_class_install_property(gobject_class, PROP_HOST,
148      g_param_spec_string("host", "Host",
149          "The host running the RoarAudio daemon", NULL, G_PARAM_READWRITE));
150}
151
152static void gst_roarsink_init (GstRoarSink * roarsink, GstRoarSinkClass * klass) {
153  memset(&(roarsink->con), 0, sizeof(roarsink->con));
154  roar_connect_fh(&(roarsink->con), -1);
155  roarsink->fd     = -1;
156  roarsink->host   = NULL;
157}
158
159static void gst_roarsink_finalize (GObject * object) {
160  GstRoarSink *roarsink = GST_ROARSINK(object);
161
162  gst_caps_replace(&roarsink->cur_caps, NULL);
163  if ( roarsink->host != NULL )
164   g_free(roarsink->host);
165
166  G_OBJECT_CLASS(parent_class)->finalize(object);
167}
168
169static GstCaps * gst_roarsink_getcaps (GstBaseSink * bsink) {
170  GstRoarSink *roarsink;
171
172  roarsink = GST_ROARSINK(bsink);
173
174  /* no fd, we're done with the template caps */
175  if (roar_get_connection_fh(&(roarsink->con)) < 0 || roarsink->cur_caps == NULL) {
176    GST_LOG_OBJECT(roarsink, "getcaps called, returning template caps");
177    return NULL;
178  }
179
180  GST_LOG_OBJECT(roarsink, "returning %" GST_PTR_FORMAT, roarsink->cur_caps);
181
182  return gst_caps_ref(roarsink->cur_caps);
183}
184
185static gboolean gst_roarsink_open(GstAudioSink * asink) {
186  GstPadTemplate *pad_template;
187  GstRoarSink *roarsink;
188  gint i;
189  struct roar_stream oinfo;
190
191  roarsink = GST_ROARSINK(asink);
192
193  GST_DEBUG_OBJECT(roarsink, "open");
194
195  /* now try to connect to any existing/running sound daemons */
196  if ( roar_simple_connect(&(roarsink->con), roarsink->host, "gstreamer client") == -1 )
197    goto couldnt_connect;
198
199  /* get server info */
200  if ( roar_server_oinfo(&(roarsink->con), &oinfo) == -1 )
201    goto no_server_info;
202
203  GST_INFO_OBJECT(roarsink, "got server info rate: %i", oinfo.info.rate);
204
205  pad_template = gst_static_pad_template_get(&sink_factory);
206  roarsink->cur_caps = gst_caps_copy(gst_pad_template_get_caps(pad_template));
207
208  for (i = 0; i < roarsink->cur_caps->structs->len; i++) {
209    GstStructure *s;
210
211    s = gst_caps_get_structure(roarsink->cur_caps, i);
212    gst_structure_set(s, "rate", G_TYPE_INT, oinfo.info.rate, NULL);
213  }
214
215  GST_INFO_OBJECT(roarsink, "server caps: %" GST_PTR_FORMAT, roarsink->cur_caps);
216
217  return TRUE;
218
219  /* ERRORS */
220couldnt_connect:
221  {
222    GST_ELEMENT_ERROR(roarsink, RESOURCE, OPEN_WRITE,
223        (_("Could not establish connection to sound server")),
224        ("can't open connection to esound server"));
225    return FALSE;
226  }
227no_server_info:
228  {
229    GST_ELEMENT_ERROR(roarsink, RESOURCE, OPEN_WRITE,
230        (_("Failed to query sound server capabilities")),
231        ("couldn't get server info!"));
232    return FALSE;
233  }
234}
235
236static gboolean gst_roarsink_close (GstAudioSink * asink) {
237  GstRoarSink *roarsink = GST_ROARSINK(asink);
238
239  GST_DEBUG_OBJECT(roarsink, "close");
240
241  gst_caps_replace(&roarsink->cur_caps, NULL);
242
243  roar_disconnect(&(roarsink->con));
244
245  if ( roarsink->fd != -1 )
246   close(roarsink->fd);
247
248  return TRUE;
249}
250
251static gboolean gst_roarsink_prepare (GstAudioSink * asink, GstRingBufferSpec * spec) {
252  GstRoarSink *roarsink = GST_ROARSINK(asink);
253
254  GST_DEBUG_OBJECT(roarsink, "prepare");
255
256 
257  GST_INFO_OBJECT(roarsink, "attempting to open data connection to esound server");
258
259  roarsink->fd = roar_simple_new_stream(&(roarsink->con), spec->rate, spec->channels, spec->depth,
260                                        ROAR_CODEC_DEFAULT, ROAR_DIR_PLAY);
261
262  if ( roarsink->fd == -1 )
263    goto cannot_open;
264
265  roarsink->rate = spec->rate;
266
267  spec->segsize  = ROAR_BUF_SIZE;
268  spec->segtotal = (ROAR_MAX_WRITE_SIZE / spec->segsize);
269
270  /* FIXME: this is wrong for signed ints (and the
271   * audioringbuffers should do it for us anyway) */
272  spec->silence_sample[0] = 0;
273  spec->silence_sample[1] = 0;
274  spec->silence_sample[2] = 0;
275  spec->silence_sample[3] = 0;
276
277  GST_INFO_OBJECT(roarsink, "successfully opened connection to esound server");
278
279  return TRUE;
280
281  /* ERRORS */
282cannot_open:
283  {
284    GST_ELEMENT_ERROR(roarsink, RESOURCE, OPEN_WRITE,
285        (_("Could not establish connection to sound server")),
286        ("can't open connection to RoarAudio server"));
287    return FALSE;
288  }
289}
290
291static gboolean gst_roarsink_unprepare (GstAudioSink * asink) {
292  GstRoarSink *roarsink = GST_ROARSINK(asink);
293
294  if ((roarsink->fd == -1) && (roar_get_connection_fh(&(roarsink->con)) == -1))
295    return TRUE;
296
297  close(roarsink->fd);
298  roarsink->fd = -1;
299
300  GST_INFO_OBJECT(roarsink, "closed sound device");
301
302  return TRUE;
303}
304
305
306static guint gst_roarsink_write (GstAudioSink * asink, gpointer data, guint length) {
307  GstRoarSink *roarsink = GST_ROARSINK(asink);
308  gint to_write = 0;
309
310  to_write = length;
311
312  while (to_write > 0) {
313    int done;
314
315    done = write(roarsink->fd, data, to_write);
316
317    if (done < 0)
318      goto write_error;
319
320    to_write -= done;
321    data += done;
322  }
323  return length;
324
325  /* ERRORS */
326write_error:
327  {
328    GST_ELEMENT_ERROR(roarsink, RESOURCE, WRITE,
329        ("Failed to write data to the RoarAudio daemon"), GST_ERROR_SYSTEM);
330    return 0;
331  }
332}
333
334static guint gst_roarsink_delay (GstAudioSink * asink) {
335//  GstRoarSink *roarsink = GST_ROARSINK (asink);
336  guint latency;
337
338  latency = 441; // compile type depending and link level deppendent,
339                 // use default value for local operations here for the moment
340
341  GST_DEBUG_OBJECT(asink, "got latency: %u", latency);
342
343  return latency;
344}
345
346static void gst_roarsink_reset (GstAudioSink * asink) {
347  GST_DEBUG_OBJECT(asink, "reset called");
348}
349
350static void gst_roarsink_set_property (GObject * object, guint prop_id, const GValue * value,
351                                       GParamSpec * pspec) {
352  GstRoarSink *roarsink = GST_ROARSINK(object);
353
354  switch (prop_id) {
355    case PROP_HOST:
356      if ( roarsink->host != NULL )
357       g_free(roarsink->host);
358      roarsink->host = g_value_dup_string(value);
359     break;
360    default:
361      break;
362  }
363}
364
365static void gst_roarsink_get_property (GObject * object, guint prop_id, GValue * value,
366                                       GParamSpec * pspec) {
367  GstRoarSink *roarsink = GST_ROARSINK(object);
368
369  switch (prop_id) {
370    case PROP_HOST:
371      g_value_set_string(value, roarsink->host);
372     break;
373    default:
374      G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
375     break;
376  }
377}
378
379gboolean gst_roarsink_factory_init (GstPlugin * plugin) {
380  if (!gst_element_register(plugin, "roarsink", GST_RANK_MARGINAL,
381                            GST_TYPE_ROARSINK))
382    return FALSE;
383
384  return TRUE;
385}
Note: See TracBrowser for help on using the repository browser.