~ubuntu-branches/ubuntu/vivid/ekiga/vivid-proposed

« back to all changes in this revision

Viewing changes to plugins/gstreamer/gst-audiooutput.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Kilian Krause
  • Date: 2011-07-17 00:24:50 UTC
  • mfrom: (5.1.5 upstream) (7.1.7 sid)
  • Revision ID: james.westby@ubuntu.com-20110717002450-ytg3wsrc1ptd3153
Tags: 3.3.1-1
* New upstream release.
 - Required libpt-dev 2.10 and libopal-dev 3.10
* Fix debian/watch to catch new version
* Remove libnotify0.7.patch - included upstream
* Add libboost-dev and libboost-signals-dev to Build-Depends
* debian/rules: Don't install *.la files for new internal shared libs
* Fix Vcs URIs to point to correct desktop/experimental/ekiga tree

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
/* Ekiga -- A VoIP and Video-Conferencing application
 
3
 * Copyright (C) 2000-2009 Damien Sandras <dsandras@seconix.com>
 
4
 *
 
5
 * This program is free software; you can redistribute it and/or modify
 
6
 * it under the terms of the GNU General Public License as published by
 
7
 * the Free Software Foundation; either version 2 of the License, or
 
8
 * (at your option) any later version.
 
9
 *
 
10
 * This program is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 * GNU General Public License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU General Public License
 
16
 * along with this program; if not, write to the Free Software Foundation,
 
17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
 
18
 *
 
19
 *
 
20
 * Ekiga is licensed under the GPL license and as a special exception,
 
21
 * you have permission to link or otherwise combine this program with the
 
22
 * programs OPAL, OpenH323 and PWLIB, and distribute the combination,
 
23
 * without applying the requirements of the GNU GPL to the OPAL, OpenH323
 
24
 * and PWLIB programs, as long as you do follow the requirements of the
 
25
 * GNU GPL for all the rest of the software thus combined.
 
26
 */
 
27
 
 
28
 
 
29
/*
 
30
 *                         gst-audiooutput.cpp  -  description
 
31
 *                         ------------------------------------
 
32
 *   begin                : Sat 27 September 2008
 
33
 *   copyright            : (C) 2008 by Julien Puydt
 
34
 *   description          : Gstreamer audio output code
 
35
 *
 
36
 */
 
37
 
 
38
#include <glib/gi18n.h>
 
39
 
 
40
#include "gst-audiooutput.h"
 
41
 
 
42
#include <gst/interfaces/propertyprobe.h>
 
43
#include <gst/app/gstappsrc.h>
 
44
#include <gst/app/gstappbuffer.h>
 
45
 
 
46
#include <string.h>
 
47
 
 
48
static gboolean
 
49
pipeline_cleaner (GstBus* /*bus*/,
 
50
                  GstMessage* message,
 
51
                  gpointer pipeline)
 
52
{
 
53
  bool result = TRUE;
 
54
 
 
55
  if (GST_MESSAGE_TYPE (message) == GST_MESSAGE_EOS
 
56
      && GST_MESSAGE_SRC (message) == GST_OBJECT_CAST (pipeline)) {
 
57
 
 
58
    result = FALSE;
 
59
    gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_NULL);
 
60
    g_object_unref (pipeline);
 
61
  }
 
62
 
 
63
  return result;
 
64
}
 
65
 
 
66
 
 
67
GST::AudioOutputManager::AudioOutputManager ():
 
68
  already_detected_devices(false)
 
69
{
 
70
}
 
71
 
 
72
GST::AudioOutputManager::~AudioOutputManager ()
 
73
{
 
74
}
 
75
 
 
76
void
 
77
GST::AudioOutputManager::get_devices (std::vector<Ekiga::AudioOutputDevice>& devices)
 
78
{
 
79
  detect_devices ();
 
80
 
 
81
  for (std::map<std::pair<std::string, std::string>, std::string>::const_iterator iter
 
82
         = devices_by_name.begin ();
 
83
       iter != devices_by_name.end ();
 
84
       ++iter) {
 
85
 
 
86
    Ekiga::AudioOutputDevice device;
 
87
    device.type = "GStreamer";
 
88
    device.source = iter->first.first;
 
89
    device.name = iter->first.second;
 
90
    devices.push_back (device);
 
91
  }
 
92
}
 
93
 
 
94
bool
 
95
GST::AudioOutputManager::set_device (Ekiga::AudioOutputPS ps,
 
96
                                     const Ekiga::AudioOutputDevice& device)
 
97
{
 
98
  bool result = false;
 
99
 
 
100
  if ( !already_detected_devices)
 
101
    detect_devices ();
 
102
 
 
103
  if (device.type == "GStreamer"
 
104
      && devices_by_name.find (std::pair<std::string,std::string>(device.source, device.name)) != devices_by_name.end ()) {
 
105
 
 
106
    unsigned ii = (ps == Ekiga::primary)?0:1;
 
107
    current_state[ii].opened = false;
 
108
    current_state[ii].device = device;
 
109
    result = true;
 
110
  }
 
111
  return result;
 
112
}
 
113
 
 
114
bool
 
115
GST::AudioOutputManager::open (Ekiga::AudioOutputPS ps,
 
116
                               unsigned channels,
 
117
                               unsigned samplerate,
 
118
                               unsigned bits_per_sample)
 
119
{
 
120
  bool result = false;
 
121
  unsigned ii = (ps == Ekiga::primary)?0:1;
 
122
  gchar* command = NULL;
 
123
  GError* error = NULL;
 
124
  GstState current;
 
125
 
 
126
  if ( !already_detected_devices)
 
127
    detect_devices ();
 
128
 
 
129
  command = g_strdup_printf ("appsrc is-live=true name=ekiga_src"
 
130
                             " ! audio/x-raw-int"
 
131
                             ",rate=%d"
 
132
                             ",channels=%d"
 
133
                             ",width=%d"
 
134
                             ",depth=%d"
 
135
                             ",signed=true,endianness=1234"
 
136
                             " ! %s",
 
137
                             samplerate, channels, bits_per_sample, bits_per_sample,
 
138
                             devices_by_name[std::pair<std::string,std::string>(current_state[ii].device.source, current_state[ii].device.name)].c_str ());
 
139
  //g_print ("Pipeline: %s\n", command);
 
140
  pipeline[ii] = gst_parse_launch (command, &error);
 
141
 
 
142
  if (error == NULL) {
 
143
 
 
144
    (void)gst_element_set_state (pipeline[ii], GST_STATE_PLAYING);
 
145
 
 
146
    // this will make us wait so we can return the right value...
 
147
    (void)gst_element_get_state (pipeline[ii],
 
148
                                 &current,
 
149
                                 NULL,
 
150
                                 GST_SECOND);
 
151
 
 
152
    if ( !(current == GST_STATE_PLAYING
 
153
           || current == GST_STATE_PAUSED)) {
 
154
 
 
155
      gst_element_set_state (pipeline[ii], GST_STATE_NULL);
 
156
      gst_object_unref (GST_OBJECT (pipeline[ii]));
 
157
      pipeline[ii] = NULL;
 
158
      result = false;
 
159
    } else {
 
160
 
 
161
      Ekiga::AudioOutputSettings settings;
 
162
      GstElement* volume = NULL;
 
163
      gfloat val;
 
164
 
 
165
      volume = gst_bin_get_by_name (GST_BIN (pipeline[ii]), "ekiga_volume");
 
166
      if (volume != NULL) {
 
167
 
 
168
        g_object_get (G_OBJECT (volume),
 
169
                      "volume", &val,
 
170
                      NULL);
 
171
        settings.volume = (unsigned)(255*val);
 
172
        settings.modifyable = true;
 
173
        g_object_unref (volume);
 
174
      } else {
 
175
 
 
176
        settings.modifyable = false;
 
177
      }
 
178
      current_state[ii].channels = channels;
 
179
      current_state[ii].samplerate = samplerate;
 
180
      current_state[ii].bits_per_sample = bits_per_sample;
 
181
      device_opened (ps, current_state[ii].device, settings);
 
182
      result = true;
 
183
    }
 
184
 
 
185
  } else {
 
186
 
 
187
    g_error_free (error);
 
188
    result = false;
 
189
  }
 
190
 
 
191
  g_free (command);
 
192
 
 
193
  current_state[ii].opened = result;
 
194
 
 
195
//   std::cout << __PRETTY_FUNCTION__
 
196
//          << " result=";
 
197
//   if (result)
 
198
//     std::cout << "TRUE";
 
199
//   else
 
200
//     std::cout << "FALSE";
 
201
//   std::cout << std::endl;
 
202
 
 
203
  return result;
 
204
}
 
205
 
 
206
void
 
207
GST::AudioOutputManager::close (Ekiga::AudioOutputPS ps)
 
208
{
 
209
  unsigned ii = (ps == Ekiga::primary)?0:1;
 
210
  if (pipeline[ii] != NULL) {
 
211
 
 
212
    GstElement* src = gst_bin_get_by_name (GST_BIN (pipeline[ii]), "ekiga_src");
 
213
 
 
214
    if (src != NULL) {
 
215
 
 
216
      gst_app_src_end_of_stream (GST_APP_SRC (src));
 
217
      GstBus* bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline[ii]));
 
218
      gst_bus_add_watch (bus, pipeline_cleaner, pipeline[ii]);
 
219
      gst_object_unref (bus);
 
220
      gst_element_set_state (pipeline[ii], GST_STATE_NULL);
 
221
      pipeline[ii] = NULL;
 
222
      device_closed (ps, current_state[ii].device);
 
223
    }
 
224
  }
 
225
  current_state[ii].opened = false;
 
226
}
 
227
 
 
228
void
 
229
GST::AudioOutputManager::set_buffer_size (Ekiga::AudioOutputPS ps,
 
230
                                          unsigned buffer_size,
 
231
                                          unsigned /*num_buffers*/)
 
232
{
 
233
  unsigned ii = (ps == Ekiga::primary)?0:1;
 
234
  GstElement* src = NULL;
 
235
 
 
236
  src = gst_bin_get_by_name (GST_BIN (pipeline[ii]), "ekiga_src");
 
237
 
 
238
  if (src != NULL) {
 
239
 
 
240
    g_object_set (G_OBJECT (src),
 
241
                  "blocksize", buffer_size,
 
242
                  NULL);
 
243
    g_object_unref (src);
 
244
  }
 
245
}
 
246
 
 
247
bool
 
248
GST::AudioOutputManager::set_frame_data (Ekiga::AudioOutputPS ps,
 
249
                                         const char* data,
 
250
                                         unsigned size,
 
251
                                         unsigned& written)
 
252
{
 
253
  bool result = false;
 
254
  unsigned ii = (ps == Ekiga::primary)?0:1;
 
255
  gchar* tmp = NULL;
 
256
  GstBuffer* buffer = NULL;
 
257
  GstElement* src = NULL;
 
258
 
 
259
  written = 0;
 
260
 
 
261
  g_return_val_if_fail (GST_IS_BIN (pipeline[ii]), false);
 
262
 
 
263
  src = gst_bin_get_by_name (GST_BIN (pipeline[ii]), "ekiga_src");
 
264
 
 
265
  if (src != NULL) {
 
266
 
 
267
    tmp = (gchar*)g_malloc0 (size);
 
268
    memcpy (tmp, data, size);
 
269
    buffer = gst_app_buffer_new (tmp, size,
 
270
                                 (GstAppBufferFinalizeFunc)g_free, tmp);
 
271
    gst_app_src_push_buffer (GST_APP_SRC (src), buffer);
 
272
    written = size;
 
273
    result = true;
 
274
    g_object_unref (src);
 
275
  }
 
276
 
 
277
  return result;
 
278
}
 
279
 
 
280
void
 
281
GST::AudioOutputManager::set_volume (Ekiga::AudioOutputPS ps,
 
282
                                     unsigned valu)
 
283
{
 
284
  unsigned ii = (ps == Ekiga::primary)?0:1;
 
285
  GstElement* volume = NULL;
 
286
  gfloat valf;
 
287
 
 
288
  valf = valu / 255.0;
 
289
 
 
290
  volume = gst_bin_get_by_name (GST_BIN (pipeline[ii]), "ekiga_volume");
 
291
  if (volume != NULL) {
 
292
 
 
293
    g_object_set (G_OBJECT (volume),
 
294
                  "volume", valf,
 
295
                  NULL);
 
296
    g_object_unref (volume);
 
297
  }
 
298
}
 
299
 
 
300
bool
 
301
GST::AudioOutputManager::has_device (const std::string& source,
 
302
                                     const std::string& device_name,
 
303
                                     Ekiga::AudioOutputDevice& /*device*/)
 
304
{
 
305
  return (devices_by_name.find (std::pair<std::string,std::string>(source, device_name)) != devices_by_name.end ());
 
306
}
 
307
 
 
308
void
 
309
GST::AudioOutputManager::detect_devices ()
 
310
{
 
311
  devices_by_name.clear ();
 
312
  detect_fakesink_devices ();
 
313
  detect_alsasink_devices ();
 
314
  detect_pulsesink_devices ();
 
315
  detect_sdlsink_devices ();
 
316
devices_by_name[std::pair<std::string,std::string>("FILE","/tmp/sound.wav")] = "volume name=ekiga_volume ! filesink location=/tmp/sound.wav";
 
317
}
 
318
 
 
319
void
 
320
GST::AudioOutputManager::detect_fakesink_devices ()
 
321
{
 
322
  GstElement* elt = NULL;
 
323
 
 
324
  elt = gst_element_factory_make ("fakesink", "fakesinkpresencetest");
 
325
 
 
326
  if (elt != NULL) {
 
327
 
 
328
    devices_by_name[std::pair<std::string,std::string>(_("Silent"), _("Silent"))] = "fakesink";
 
329
    gst_object_unref (GST_OBJECT (elt));
 
330
  }
 
331
}
 
332
 
 
333
void
 
334
GST::AudioOutputManager::detect_alsasink_devices ()
 
335
{
 
336
  GstElement* elt = NULL;
 
337
 
 
338
  elt = gst_element_factory_make ("alsasink", "alsasinkpresencetest");
 
339
 
 
340
  if (elt != NULL) {
 
341
 
 
342
    GstPropertyProbe* probe = NULL;
 
343
    const GParamSpec* pspec = NULL;
 
344
    GValueArray* array = NULL;
 
345
 
 
346
    gst_element_set_state (elt, GST_STATE_PAUSED);
 
347
    probe = GST_PROPERTY_PROBE (elt);
 
348
    pspec = gst_property_probe_get_property (probe, "device");
 
349
 
 
350
    array = gst_property_probe_probe_and_get_values (probe, pspec);
 
351
    if (array != NULL) {
 
352
 
 
353
      for (guint index = 0; index < array->n_values; index++) {
 
354
 
 
355
        GValue* device = NULL;
 
356
        gchar* name = NULL;
 
357
        gchar* descr = NULL;
 
358
 
 
359
        device = g_value_array_get_nth (array, index);
 
360
        g_object_set_property (G_OBJECT (elt), "device", device);
 
361
        g_object_get (G_OBJECT (elt), "device-name", &name, NULL);
 
362
        descr = g_strdup_printf ("volume name=ekiga_volume ! alsasink device=%s",
 
363
                                 g_value_get_string (device));
 
364
 
 
365
        if (name != 0) {
 
366
 
 
367
          devices_by_name[std::pair<std::string,std::string>("ALSA", name)] = descr;
 
368
          g_free (name);
 
369
        }
 
370
        g_free (descr);
 
371
      }
 
372
      g_value_array_free (array);
 
373
    }
 
374
 
 
375
    devices_by_name[std::pair<std::string,std::string>("ALSA","---")] = "volume name=ekiga_volume ! alsasink";
 
376
 
 
377
    gst_element_set_state (elt, GST_STATE_NULL);
 
378
    gst_object_unref (GST_OBJECT (elt));
 
379
  }
 
380
}
 
381
 
 
382
void
 
383
GST::AudioOutputManager::detect_pulsesink_devices ()
 
384
{
 
385
  GstElement* elt = NULL;
 
386
 
 
387
  elt = gst_element_factory_make ("pulsesink", "pulsesinkpresencetest");
 
388
 
 
389
  if (elt != NULL) {
 
390
 
 
391
    GstPropertyProbe* probe = NULL;
 
392
    const GParamSpec* pspec = NULL;
 
393
    GValueArray* array = NULL;
 
394
 
 
395
    gst_element_set_state (elt, GST_STATE_PAUSED);
 
396
    probe = GST_PROPERTY_PROBE (elt);
 
397
    pspec = gst_property_probe_get_property (probe, "device");
 
398
 
 
399
    array = gst_property_probe_probe_and_get_values (probe, pspec);
 
400
    if (array != NULL) {
 
401
 
 
402
      for (guint index = 0; index < array->n_values; index++) {
 
403
 
 
404
        GValue* device = NULL;
 
405
        gchar* name = NULL;
 
406
        gchar* descr = NULL;
 
407
 
 
408
        device = g_value_array_get_nth (array, index);
 
409
        g_object_set_property (G_OBJECT (elt), "device", device);
 
410
        g_object_get (G_OBJECT (elt), "device-name", &name, NULL);
 
411
        descr = g_strdup_printf ("volume name=ekiga_volume ! pulsesink device=%s",
 
412
                                 g_value_get_string (device));
 
413
 
 
414
        if (name != 0) {
 
415
 
 
416
          devices_by_name[std::pair<std::string,std::string>("PULSEAUDIO", name)] = descr;
 
417
 
 
418
          g_free (name);
 
419
        }
 
420
        g_free (descr);
 
421
      }
 
422
      g_value_array_free (array);
 
423
    }
 
424
 
 
425
    gst_element_set_state (elt, GST_STATE_NULL);
 
426
    gst_object_unref (GST_OBJECT (elt));
 
427
  }
 
428
}
 
429
 
 
430
void
 
431
GST::AudioOutputManager::detect_sdlsink_devices ()
 
432
{
 
433
  gchar* descr = NULL;
 
434
  descr = g_strdup_printf ("volume name=ekiga_volume ! sdlaudiosink");
 
435
  devices_by_name[std::pair<std::string,std::string>("SDL", "Default")] = descr;
 
436
  g_free (descr);
 
437
}