~ubuntu-branches/ubuntu/saucy/ekiga/saucy

« back to all changes in this revision

Viewing changes to lib/engine/audiooutput/skel/audiooutput-core.h

  • 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-2008 Damien Sandras
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 (at
8
 
 * your option) any later version. This program is distributed in the hope
9
 
 * that it will be useful, but WITHOUT ANY WARRANTY; without even the
10
 
 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11
 
 * See the GNU General Public License for more details.
12
 
 *
13
 
 * You should have received a copy of the GNU General Public License along
14
 
 * with this program; if not, write to the Free Software Foundation, Inc.,
15
 
 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
16
 
 *
17
 
 * Ekiga is licensed under the GPL license and as a special exception, you
18
 
 * have permission to link or otherwise combine this program with the
19
 
 * programs OPAL, OpenH323 and PWLIB, and distribute the combination, without
20
 
 * applying the requirements of the GNU GPL to the OPAL, OpenH323 and PWLIB
21
 
 * programs, as long as you do follow the requirements of the GNU GPL for all
22
 
 * the rest of the software thus combined.
23
 
 */
24
 
 
25
 
 
26
 
/*
27
 
 *                         audiooutput-core.h  -  description
28
 
 *                         ------------------------------------------
29
 
 *   begin                : written in 2008 by Matthias Schneider
30
 
 *   copyright            : (c) 2008 by Matthias Schneider
31
 
 *   description          : Declaration of the interface of a audiooutput core.
32
 
 *                          A audioout core manages AudioOutputManagers.
33
 
 *
34
 
 */
35
 
 
36
 
#ifndef __AUDIOOUTPUT_CORE_H__
37
 
#define __AUDIOOUTPUT_CORE_H__
38
 
 
39
 
#include "services.h"
40
 
#include "runtime.h"
41
 
#include "audiooutput-core.h"
42
 
#include "hal-core.h"
43
 
#include "audiooutput-gmconf-bridge.h"
44
 
#include "audiooutput-info.h"
45
 
#include "audiooutput-scheduler.h"
46
 
 
47
 
#include <sigc++/sigc++.h>
48
 
#include <glib.h>
49
 
#include <set>
50
 
 
51
 
#include "ptbuildopts.h"
52
 
#include "ptlib.h"
53
 
 
54
 
#define AUDIO_OUTPUT_FALLBACK_DEVICE_TYPE "Ekiga"
55
 
#define AUDIO_OUTPUT_FALLBACK_DEVICE_SOURCE "Ekiga"
56
 
#define AUDIO_OUTPUT_FALLBACK_DEVICE_NAME   "SILENT"
57
 
 
58
 
namespace Ekiga
59
 
{
60
 
/**
61
 
 * @defgroup audiooutput
62
 
 * @{
63
 
 */
64
 
 
65
 
   class AudioOutputManager;
66
 
   class AudioOutputCore;
67
 
 
68
 
  /** Core object for the audio output support
69
 
   * The audio output core abstracts all functionality related to audio output
70
 
   * in a thread safe manner. Typically, most of the functions except start(),
71
 
   * stop(), set_buffer_size() and set_frame_data() will be called from a UI thread,
72
 
   * while the three mentioned funtions will be used by an audio streaming thread.
73
 
   * 
74
 
   * The audio output core abstracts different audio output managers, which can 
75
 
   * represent different backends like PTLIB, from the application and can 
76
 
   * switch the output device transparently for the audio streaming thread,
77
 
   * even while audio output is in progress.
78
 
   *
79
 
   * If the removal of an audio output device is detected by a failed
80
 
   * write or by a message from the HalCore, the audio output core will 
81
 
   * determine the responsible audio output manager and send a signal to the UI,
82
 
   * which can be used to update device lists. Also, if the removed device was the 
83
 
   * currently used one, the core falls back to the backup device.
84
 
   * 
85
 
   * A similar procedure is performed on the addition of a device. In case we fell 
86
 
   * back due to a removed device, and the respective device is re-added to the system,
87
 
   * it will be automatically activated.
88
 
   *
89
 
 
90
 
   */
91
 
  class AudioOutputCore
92
 
    : public Service
93
 
    {
94
 
 
95
 
  public:
96
 
 
97
 
      /** The constructor
98
 
       * @param _runtime reference to Ekiga runtime.
99
 
       */
100
 
      AudioOutputCore (Ekiga::Runtime & _runtime);
101
 
 
102
 
      /** The destructor
103
 
      */
104
 
      ~AudioOutputCore ();
105
 
 
106
 
      /** Set up gmconf bridge
107
 
       */
108
 
      void setup_conf_bridge();
109
 
 
110
 
 
111
 
      /*** Service Implementation ***/
112
 
 
113
 
      /** Returns the name of the service.
114
 
       * @return The service name.
115
 
       */
116
 
      const std::string get_name () const
117
 
        { return "audiooutput-core"; }
118
 
 
119
 
      /** Returns the description of the service.
120
 
       * @return The service description.
121
 
       */
122
 
      const std::string get_description () const
123
 
        { return "\tAudioOutput Core managing AudioOutput Manager objects"; }
124
 
 
125
 
      /** Adds a AudioOutputManager to the AudioOutputCore service.
126
 
       * @param The manager to be added.
127
 
       */
128
 
      void add_manager (AudioOutputManager &manager);
129
 
 
130
 
      /** Triggers a callback for all Ekiga::AudioOutputManager sources of the
131
 
       * AudioOutputCore service.
132
 
       */
133
 
      void visit_managers (sigc::slot<bool, AudioOutputManager &> visitor);
134
 
 
135
 
      /** This signal is emitted when a Ekiga::AudioOutputManager has been
136
 
       * added to the AudioOutputCore Service.
137
 
       */
138
 
      sigc::signal<void, AudioOutputManager &> manager_added;
139
 
 
140
 
 
141
 
      /** Get a list of all devices supported by all managers registered to the core.
142
 
       * @param devices a vector of device names to be filled by the core.
143
 
       */
144
 
      void get_devices(std::vector <AudioOutputDevice> & devices);
145
 
 
146
 
      /** Set a specific device
147
 
       * This function sets the current primary or secondary audio output device. This function can
148
 
       * also be used while in a stream or in preview mode. In that case the old
149
 
       * device is closed and the new device opened automatically.
150
 
       * @param ps whether referring to the primary or secondary device.
151
 
       * @param device the new device to be used.
152
 
       */
153
 
      void set_device(AudioOutputPS ps, const AudioOutputDevice & device);
154
 
 
155
 
      /** Inform the core of an added audiooutput device
156
 
       * This function is called by the HalCore when an audio output device is added.
157
 
       * It determines responsible managers for that specific device and informs the 
158
 
       * GUI about the device that was added (via device_added signal). 
159
 
       * In case the added device was the desired device and we fell back, 
160
 
       * we will reactivate it. MUST be called from main thread,
161
 
       * @param sink the device sink (e.g. alsa).
162
 
       * @param device_name the name of the added device.
163
 
       * @param manager the HalManger detected the addition.
164
 
       */
165
 
      void add_device (const std::string & sink, const std::string & device_name, HalManager* manager);
166
 
 
167
 
      /** Inform the core of a removed audiooutput device
168
 
       * This function is called by the HalCore when an audio output device is removed.
169
 
       * It determines responsible managers for that specific device and informs the 
170
 
       * GUI about the device that was removed (via device_removed signal). 
171
 
       * In case the removed device was the current device we fall back to the
172
 
       * fallback device. MUST be called from main thread,
173
 
       * @param sink the device sink (e.g. alsa).
174
 
       * @param device_name the name of the removed device.
175
 
       * @param manager the HalManger detected the removal.
176
 
       */
177
 
      void remove_device (const std::string & sink, const std::string & device_name, HalManager* manager);
178
 
 
179
 
 
180
 
      /*** Event Management ***/
181
 
 
182
 
      /** Add a mapping between event and file name to the event list
183
 
       * An event shall refer to a specific sound file. This mapping is set here.
184
 
       * @param event_name the name of the event.
185
 
       * @param file_name the name of the file.
186
 
       * @param ps whether the event shall be played on the primary or secondary device preferrably.
187
 
       * @param enabled if the event is enabled.
188
 
       */
189
 
      void add_event (const std::string & event_name, const std::string & file_name, AudioOutputPS ps, bool enabled);
190
 
 
191
 
      /** Play a sound specified by a file name
192
 
       * Play a sound file once.
193
 
       * The sound will be played in the background as soon as the Scheduler 
194
 
       * schedules it.
195
 
       * This function only adds the sound to the Scheduler queue and returns immediately.
196
 
       * The sound will be played on the primary device 
197
 
       * @param file_name the name of the file.
198
 
       */
199
 
      void play_file (const std::string & file_name);
200
 
 
201
 
      /** Play a sound specified by an event name
202
 
       * Play a sound associated to the event speficied by its name once.
203
 
       * The sound will be played in the background as soon as the Scheduler 
204
 
       * schedules it.
205
 
       * This function only adds the sound to the Scheduler queue and returns immediately.
206
 
       * The sound will be played on the primary or seconday device depending on
207
 
       * how this specific event was configured. In case it was to be played on the secondary device
208
 
       * and not secondary device is available or configured, it will be played on the primary device.
209
 
       * The event will only be played if it is enabled.
210
 
       * @param event_name the name of the event.
211
 
       */
212
 
      void play_event (const std::string & event_name);
213
 
 
214
 
      /** Play a sound specified by an event name
215
 
       * Play a sound associated to the event specified by its name repeatingly.
216
 
       * The sound will be played in the background as soon as the Scheduler 
217
 
       * schedules it.
218
 
       * This function only adds the sound to the Scheduler queue and returns immediately.
219
 
       * The sound will be played on the primary or seconday device depending on
220
 
       * how this specific event was configured. In case it was to be played on the secondary device
221
 
       * and not secondary device is available or configured, it will be played on the primary device.
222
 
       * The event will only be played if it is enabled.
223
 
       * The event will be removed from the scheduler queue once it has been repeated "repetitions" times
224
 
       * or if it has been removd from the queue via stop_play_event.
225
 
       * @param event_name the name of the event.
226
 
       * @param interval the interval of the repetitions in ms.
227
 
       * @param repetitions the maximum number of repetitions.
228
 
       */
229
 
      void start_play_event (const std::string & event_name, unsigned interval, unsigned repetitions);
230
 
 
231
 
      /** Stop playing a sound specified by an event name
232
 
       * Stop playing sound associated to the event specified by its name.
233
 
       * If the sound is currently playing, it will not be cut short.
234
 
       * @param event_name the name of the event.
235
 
       */
236
 
      void stop_play_event (const std::string & event_name);
237
 
 
238
 
      /** Play a sound event buffer
239
 
       * This function is called by the Scheduler in order to play an already loaded sound.
240
 
       * @param ps whether to play the sound on the primary or secondary device.
241
 
       * @param buffer pointer to the sound in raw format.
242
 
       * @param len the length in bytes of the sound.
243
 
       * @param channels the number of channels.
244
 
       * @param sample_rate the samplerate.
245
 
       * @param bps bits per sample.
246
 
       */
247
 
      void play_buffer(AudioOutputPS ps, const char* buffer, unsigned long len, unsigned channels, unsigned sample_rate, unsigned bps);
248
 
 
249
 
 
250
 
      /*** Stream Management ***/
251
 
 
252
 
      /** Set the number and size of buffers 
253
 
       * Will be applied the next time the device is opened.
254
 
       * @param buffer_size the size of each buffer in byte.
255
 
       * @param num_buffers the number of buffers.
256
 
       */
257
 
      void set_buffer_size (unsigned buffer_size, unsigned num_buffers);
258
 
 
259
 
     /** Start the audio output on the primary device
260
 
       * @param channels the number of channels (1 or 2).
261
 
       * @param samplerate the samplerate.
262
 
       * @param bits_per_sample the number of bits per sample (e.g. 8, 16).
263
 
       */ 
264
 
      void start (unsigned channels, unsigned samplerate, unsigned bits_per_sample);
265
 
 
266
 
      /** Stop the audio output of the primary device.
267
 
       */
268
 
      void stop ();
269
 
 
270
 
     /** Set one audio buffer in the current manager.
271
 
       * This function will pass one buffer to the current manager. 
272
 
       * Requires the audio output to be started.
273
 
       * In case the device returns an error writing the frame, set_frame_data()
274
 
       * falls back to the fallback device and writes the frame there. Thus
275
 
       * set_frame_data() always be succesful.
276
 
       * In case a new volume has bee set, it will be applied here.
277
 
       * @param data a pointer to the buffer that is to be written to the device.
278
 
       * @param size the number of bytes to be written.
279
 
       * @param bytes_written number of bytes actually written.
280
 
       */
281
 
      void set_frame_data (const char *data, unsigned size, unsigned & bytes_written); 
282
 
 
283
 
     /** Set the volume of the next opportunity
284
 
       * Sets the volume to the specified value the next time
285
 
       * get_frame_data() is called.
286
 
       * @param ps whether the volume of the primary or seconday device shall be set.
287
 
       * @param volume the new volume level (0..255).
288
 
       */
289
 
      void set_volume (AudioOutputPS ps, unsigned volume);
290
 
 
291
 
      /** Turn average collecion on and off
292
 
       * The average values can be collected via get_average_level()
293
 
       * This applies to primary device only.
294
 
       * @param on_off whether to turn the collection on or off.
295
 
       */
296
 
      void set_average_collection (bool on_off) { calculate_average = on_off; }
297
 
 
298
 
      /** Get the average volume level
299
 
       * Get the average volume level ove the last read buffer of the primary device.
300
 
       * @return the average volume level.
301
 
       */
302
 
      float get_average_level () { return average_level; }
303
 
 
304
 
 
305
 
      /*** Signals ***/
306
 
 
307
 
      /** See audiooutput-manager.h for the API
308
 
       */
309
 
      sigc::signal<void, AudioOutputManager &, AudioOutputPS, AudioOutputDevice&, AudioOutputSettings&> device_opened;
310
 
      sigc::signal<void, AudioOutputManager &, AudioOutputPS, AudioOutputDevice&> device_closed;
311
 
      sigc::signal<void, AudioOutputManager &, AudioOutputPS, AudioOutputDevice&, AudioOutputErrorCodes> device_error;
312
 
 
313
 
      /** This signal is emitted when an audio output device has been added to the system.
314
 
       * This signal will be emitted if add_device was called with a device name and
315
 
       * a manager claimed support for this device.
316
 
       * @param device the audio output device that was added.
317
 
       */
318
 
      sigc::signal<void, AudioOutputDevice, bool> device_added;
319
 
 
320
 
      /** This signal is emitted when an audio output device has been removed from the system.
321
 
       * This signal will be emitted if remove_device was called with a device name and
322
 
       * a manager claimed support for this device.
323
 
       * @param device the audio output device that was removed.
324
 
       */
325
 
      sigc::signal<void, AudioOutputDevice, bool> device_removed;
326
 
 
327
 
  private:
328
 
      void on_device_opened (AudioOutputPS ps, 
329
 
                             AudioOutputDevice device,
330
 
                             AudioOutputSettings settings, 
331
 
                             AudioOutputManager *manager);
332
 
      void on_device_closed (AudioOutputPS ps, AudioOutputDevice device, AudioOutputManager *manager);
333
 
      void on_device_error  (AudioOutputPS ps, AudioOutputDevice device, AudioOutputErrorCodes error_code, AudioOutputManager *manager);
334
 
 
335
 
      void internal_set_primary_device(const AudioOutputDevice & device);
336
 
      void internal_set_manager (AudioOutputPS ps, const AudioOutputDevice & device);
337
 
      void internal_set_primary_fallback();
338
 
 
339
 
      bool internal_open (AudioOutputPS ps, unsigned channels, unsigned samplerate, unsigned bits_per_sample);
340
 
      void internal_close(AudioOutputPS ps);
341
 
 
342
 
      void internal_play(AudioOutputPS ps, const char* buffer, unsigned long len, unsigned channels, unsigned sample_rate, unsigned bps);
343
 
 
344
 
      void calculate_average_level (const short *buffer, unsigned size);
345
 
 
346
 
      std::set<AudioOutputManager *> managers;
347
 
      Ekiga::Runtime & runtime;
348
 
 
349
 
      typedef struct DeviceConfig {
350
 
        bool active;
351
 
        unsigned channels;
352
 
        unsigned samplerate;
353
 
        unsigned bits_per_sample;
354
 
        unsigned buffer_size;
355
 
        unsigned num_buffers;
356
 
      } DeviceConfig;
357
 
 
358
 
      DeviceConfig current_primary_config;
359
 
 
360
 
      AudioOutputManager* current_manager[2];
361
 
      AudioOutputDevice desired_primary_device;
362
 
      AudioOutputDevice current_device[2];
363
 
      unsigned desired_primary_volume;
364
 
      unsigned current_primary_volume;
365
 
 
366
 
      PMutex core_mutex[2];
367
 
      PMutex volume_mutex;
368
 
 
369
 
      AudioOutputCoreConfBridge* audiooutput_core_conf_bridge;
370
 
      AudioEventScheduler audio_event_scheduler;
371
 
 
372
 
      float average_level;
373
 
      bool calculate_average;
374
 
      bool yield;
375
 
    };
376
 
/**
377
 
 * @}
378
 
 */
379
 
};
380
 
 
381
 
#endif