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

« back to all changes in this revision

Viewing changes to lib/engine/videoinput/skel/videoinput-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
 
/*
3
 
 * Ekiga -- A VoIP and Video-Conferencing application
4
 
 * Copyright (C) 2000-2008 Damien Sandras
5
 
 
6
 
 * This program is free software; you can  redistribute it and/or modify
7
 
 * it under the terms of the GNU General Public License as published by
8
 
 * the Free Software Foundation; either version 2 of the License, or (at
9
 
 * your option) any later version. This program is distributed in the hope
10
 
 * that it will be useful, but WITHOUT ANY WARRANTY; without even the
11
 
 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
 
 * See the GNU General Public License for more details.
13
 
 *
14
 
 * You should have received a copy of the GNU General Public License along
15
 
 * with this program; if not, write to the Free Software Foundation, Inc.,
16
 
 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
17
 
 *
18
 
 * Ekiga is licensed under the GPL license and as a special exception, you
19
 
 * have permission to link or otherwise combine this program with the
20
 
 * programs OPAL, OpenH323 and PWLIB, and distribute the combination, without
21
 
 * applying the requirements of the GNU GPL to the OPAL, OpenH323 and PWLIB
22
 
 * programs, as long as you do follow the requirements of the GNU GPL for all
23
 
 * the rest of the software thus combined.
24
 
 */
25
 
 
26
 
 
27
 
/*
28
 
 *                         videoinput-core.h  -  description
29
 
 *                         ------------------------------------------
30
 
 *   begin                : written in 2008 by Matthias Schneider
31
 
 *   copyright            : (c) 2008 by Matthias Schneider
32
 
 *   description          : Declaration of the interface of a videoinput core.
33
 
 *                          A videoinput core manages VideoInputManagers.
34
 
 *
35
 
 */
36
 
 
37
 
#ifndef __VIDEOINPUT_CORE_H__
38
 
#define __VIDEOINPUT_CORE_H__
39
 
 
40
 
#include "services.h"
41
 
#include "runtime.h"
42
 
#include "videooutput-core.h"
43
 
#include "hal-core.h"
44
 
#include "videoinput-gmconf-bridge.h"
45
 
#include "videoinput-info.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 VIDEO_INPUT_FALLBACK_DEVICE_TYPE   "Moving Logo"
55
 
#define VIDEO_INPUT_FALLBACK_DEVICE_SOURCE "Moving Logo"
56
 
#define VIDEO_INPUT_FALLBACK_DEVICE_NAME   "Moving Logo"
57
 
 
58
 
namespace Ekiga
59
 
{
60
 
/**
61
 
 * @defgroup videoinput
62
 
 * @{
63
 
 */
64
 
 
65
 
  class VideoInputManager;
66
 
  class VideoInputCore;
67
 
 
68
 
  /** Core object for the video input support
69
 
   * The video input core abstracts all functionality related to video input
70
 
   * in a thread safe manner. Typically, most of the functions except start_stream(),
71
 
   * stop_stream(), and get_frame_data() will be called from 
72
 
   * a UI thread, while the three mentioned funtions will be used by a video
73
 
   * streaming thread.
74
 
   * 
75
 
   * The video output core abstracts different video input managers, which can 
76
 
   * represent different backends like PTLIB, from the application and can 
77
 
   * switch the output device transparently for the video streaming thread,
78
 
   * even while capturing is in progress.
79
 
   *
80
 
   * If the removal of an video input device is detected by a failed
81
 
   * read or by a message from the HalCore, the video input core will 
82
 
   * determine the responsible video input manager and send a signal to the UI,
83
 
   * which can be used to update device lists. Also, if the removed device was the 
84
 
   * currently used one, the core falls back to the backup device.
85
 
   * 
86
 
   * A similar procedure is performed on the addition of a device. In case we fell 
87
 
   * back due to a removed device, and the respective device is re-added to the system,
88
 
   * it will be automatically activated.
89
 
   *
90
 
   * The video input core can also be used in a preview mode, where it starts a separate
91
 
   * thread (represented by the VideoPreviewManager), which grabs frames from the video 
92
 
   * input core and passes them to the video output core. This can be used for displaying
93
 
   * the local camera signal while not being in a call. If the preview is active and them
94
 
   * and the stream is started, the core will automatically determined if the device needs
95
 
   * to be reinitialized (the stream settings may be different from the preview settings
96
 
   * due to the capabilities negotiation). In case preview is set to active and them
97
 
   * the streaming is ended, the core will automatically switch back to preview mode,
98
 
   * also reinitializing the device if preview settings differ from stream settings.
99
 
   */
100
 
  class VideoInputCore
101
 
    : public Service
102
 
    {
103
 
 
104
 
  public:
105
 
 
106
 
      /** The constructor
107
 
       * @param _runtime reference to Ekiga runtime.
108
 
       * @param _videooutput_core reference ot the video output core.
109
 
       */
110
 
      VideoInputCore (Ekiga::Runtime & _runtime, VideoOutputCore& _videooutput_core);
111
 
 
112
 
      /** The destructor
113
 
       */
114
 
      ~VideoInputCore ();
115
 
 
116
 
      /** Set up gmconf bridge
117
 
       */
118
 
      void setup_conf_bridge();
119
 
 
120
 
 
121
 
      /*** Service Implementation ***/
122
 
 
123
 
      /** Returns the name of the service.
124
 
       * @return The service name.
125
 
       */
126
 
      const std::string get_name () const
127
 
        { return "videoinput-core"; }
128
 
 
129
 
 
130
 
      /** Returns the description of the service.
131
 
       * @return The service description.
132
 
       */
133
 
      const std::string get_description () const
134
 
        { return "\tVideoInput Core managing VideoInput Manager objects"; }
135
 
 
136
 
      /** Adds a VideoInputManager to the VideoInputCore service.
137
 
       * @param The manager to be added.
138
 
       */
139
 
       void add_manager (VideoInputManager &manager);
140
 
 
141
 
      /** Triggers a callback for all Ekiga::VideoInputManager sources of the
142
 
       * VideoInputCore service.
143
 
       */
144
 
       void visit_managers (sigc::slot<bool, VideoInputManager &> visitor);
145
 
 
146
 
      /** This signal is emitted when a Ekiga::VideoInputManager has been
147
 
       * added to the VideoInputCore Service.
148
 
       */
149
 
       sigc::signal<void, VideoInputManager &> manager_added;
150
 
 
151
 
 
152
 
      /*** VideoInput Device Management ***/
153
 
 
154
 
      /** Get a list of all devices supported by all managers registered to the core.
155
 
       * @param devices a vector of device names to be filled by the core.
156
 
       */
157
 
      void get_devices(std::vector <VideoInputDevice> & devices);
158
 
 
159
 
      /** Set a specific device
160
 
       * This function sets the current video input device. This function can
161
 
       * also be used while in a stream or in preview mode. In that case the old
162
 
       * device is closed and the new device opened automatically. 
163
 
       * @param device the new device to be used.
164
 
       * @param channel the new channel to be used.
165
 
       * @param format the new format to be used.
166
 
       */
167
 
      void set_device(const VideoInputDevice & device, int channel, VideoInputFormat format);
168
 
 
169
 
      /** Inform the core of an added videoinput device
170
 
       * This function is called by the HalCore when a video device is added.
171
 
       * It determines responsible managers for that specific device and informs the 
172
 
       * GUI about the device that was added (via device_added signal). 
173
 
       * In case the added device was the desired device and we fell back, 
174
 
       * we will reactivate it. MUST be called from main thread.
175
 
       * @param source the device source (e.g. video4linux).
176
 
       * @param device_name the name of the added device.
177
 
       * @param capabilities used for differentiating V4L1 and V4L2.
178
 
       * @param manager the HalManger detected the addition.
179
 
       */
180
 
      void add_device (const std::string & source, const std::string & device_name, unsigned capabilities, HalManager* manager);
181
 
 
182
 
      /** Inform the core of a removed videoinput device
183
 
       * This function is called by the HalCore when a video device is removed.
184
 
       * It determines responsible managers for that specific device and informs the 
185
 
       * GUI about the device that was removed (via device_removed signal). 
186
 
       * In case the removed device was the current device we fall back to the
187
 
       * fallback device. MUST be called from main thread.
188
 
       * @param source the device source (e.g. video4linux).
189
 
       * @param device_name the name of the removed device.
190
 
       * @param capabilities used for differentiating V4L1 and V4L2.
191
 
       * @param manager the HalManger detected the removal.
192
 
       */
193
 
      void remove_device (const std::string & source, const std::string & device_name, unsigned capabilities, HalManager* manager);
194
 
 
195
 
      /** Set still image data
196
 
       * In case there is a manager that allows trasmitting a still image, 
197
 
       * this function can be used to set the actual image data. All other 
198
 
       * managers can safely ignore this function.
199
 
       * @param width the width of the image in pixels.
200
 
       * @param height the height of the image in pixels.
201
 
       * @param data a pointer to the image data in YUV420P format.
202
 
       */
203
 
      void set_image_data (unsigned width, unsigned height, const char* data);
204
 
 
205
 
 
206
 
      /*** VideoInput Stream and Preview Management ***/
207
 
 
208
 
      /** Set the preview configuration
209
 
       * This function sets the resolution and framerate for the preview mode. In case
210
 
       * preview is not active (due to active stream or because it is simply off), it will 
211
 
       * be applied the next time it is (re)started. In case preview is active, 
212
 
       * the new configuration will be applied immediately by closing and reopening the device.
213
 
       * @param width the frame width.
214
 
       * @param height the frame height.
215
 
       * @param fps the frame rate.
216
 
       */
217
 
      void set_preview_config (unsigned width, unsigned height, unsigned fps);
218
 
 
219
 
      /** Start the preview mode
220
 
       * Start the preview thread implemented in the PreviewManager. 
221
 
       * In case a stream is active, this will not have an effect until the stream is stopped.
222
 
       */
223
 
      void start_preview ();
224
 
 
225
 
      /** Stop the preview mode
226
 
       */
227
 
      void stop_preview ();
228
 
 
229
 
 
230
 
      /** Set the stream configuration
231
 
       * This function sets the resolution and framerate for the stream mode, which 
232
 
       * can be different from the preview configuration due to negotiated capabilities. 
233
 
       * The configuration will be applied on the next call of start_stream(), in order 
234
 
       * not to confuse simple endpoints that do not support switching of the resolution in
235
 
       * mid-stream.
236
 
       * @param width the frame width.
237
 
       * @param height the frame height.
238
 
       * @param fps the frame rate.
239
 
       */
240
 
      void set_stream_config (unsigned width, unsigned height, unsigned fps);
241
 
 
242
 
      /** Start the stream mode
243
 
       * In case that the preview mode was active and had a different configuration,
244
 
       * the core will reopen the device automatically.
245
 
       */
246
 
      void start_stream ();
247
 
 
248
 
      /** Stop the stream mode
249
 
       * In case preview mode has been started, the core will switch to preview mode.
250
 
       * In that case, and if the preview mode has a different configuration,
251
 
       * the core will reopen the device automatically.
252
 
       */
253
 
      void stop_stream ();
254
 
 
255
 
      /** Get one video frame buffer from the current manager.
256
 
       * This function will block until the buffer is completely filled.
257
 
       * Requires the stream or the preview (when being called from the 
258
 
       * VideoPreviewManager) to be started.
259
 
       * In case the device returns an error reading the frame, get_frame_data()
260
 
       * falls back to the fallback device and reads the frame from there. Thus
261
 
       * get_frame_data() always returns a frame.
262
 
       * In case a new brightness, whiteness, etc. has bee set, it will be applied here.
263
 
       * @param data a pointer to the frame buffer that is to be filled. The memory has to be allocated already.
264
 
       * @param width returns the actual width of the frame read.
265
 
       * @param height returns the actual height of the frame read.
266
 
       */
267
 
      void get_frame_data (char *data,
268
 
                           unsigned & width,
269
 
                           unsigned & height);
270
 
 
271
 
 
272
 
      /** See vidinput-manager.h for the API
273
 
       */
274
 
      void set_colour     (unsigned colour);
275
 
      void set_brightness (unsigned brightness);
276
 
      void set_whiteness  (unsigned whiteness);
277
 
      void set_contrast   (unsigned contrast);
278
 
 
279
 
 
280
 
      /*** VideoInput related Signals ***/
281
 
 
282
 
      /** See videoinput-manager.h for the API
283
 
       */
284
 
      sigc::signal<void, VideoInputManager &, VideoInputDevice &, VideoInputSettings&> device_opened;
285
 
      sigc::signal<void, VideoInputManager &, VideoInputDevice &> device_closed;
286
 
      sigc::signal<void, VideoInputManager &, VideoInputDevice &, VideoInputErrorCodes> device_error;
287
 
 
288
 
      /** This signal is emitted when a video input has been added to the system.
289
 
       * This signal will be emitted if add_device was called with a device name and
290
 
       * a manager claimed support for this device.
291
 
       * @param device the video input device that was added.
292
 
       */
293
 
      sigc::signal<void, VideoInputDevice, bool> device_added;
294
 
 
295
 
      /** This signal is emitted when a video input has been removed from the system.
296
 
       * This signal will be emitted if remove_device was called with a device name and
297
 
       * a manager claimed support for this device.
298
 
       * @param device the video input device that was removed.
299
 
       */
300
 
      sigc::signal<void, VideoInputDevice, bool> device_removed;
301
 
 
302
 
  private:
303
 
      void on_device_opened (VideoInputDevice device,  
304
 
                             VideoInputSettings settings, 
305
 
                             VideoInputManager *manager);
306
 
      void on_device_closed (VideoInputDevice device, VideoInputManager *manager);
307
 
      void on_device_error  (VideoInputDevice device, VideoInputErrorCodes error_code, VideoInputManager *manager);
308
 
 
309
 
      void internal_set_device(const VideoInputDevice & vidinput_device, int channel, VideoInputFormat format);
310
 
      void internal_set_manager (const VideoInputDevice & vidinput_device, int channel, VideoInputFormat format);
311
 
      void internal_set_fallback ();
312
 
 
313
 
      void internal_open (unsigned width, unsigned height, unsigned fps);
314
 
      void internal_close();
315
 
 
316
 
      void internal_apply_settings();
317
 
 
318
 
private:
319
 
      /** VideoPreviewManager thread.
320
 
        *
321
 
        * VideoPreviewManager represents a thread that gets frames from the 
322
 
        * video input core and passes them to the video output core. This is 
323
 
        * used for displaying the preview video. This thread will run only 
324
 
        * while preview is active. It is called from the VideoInputCore, which
325
 
        * has the interface to the application for enabling and disabling the preview.
326
 
        */
327
 
      class VideoPreviewManager : public PThread
328
 
      {
329
 
        PCLASSINFO(VideoPreviewManager, PThread);
330
 
 
331
 
      public:
332
 
        /** The constructor
333
 
        * @param _videoinput_core reference to the video input core.
334
 
        * @param _videooutput_core reference to the video output core.
335
 
        */
336
 
        VideoPreviewManager(VideoInputCore & _videoinput_core, VideoOutputCore & _videooutput_core);
337
 
 
338
 
        /** The destructor
339
 
        */
340
 
        ~VideoPreviewManager();
341
 
 
342
 
        /** Start the preview thread.
343
 
        * Start the thread represented by the Main() function. Block until the thread is running.
344
 
        * Requires the the current device to be opened.
345
 
        * In case the resolution is changed, the preview manager has to be stopped and restarted.
346
 
        * @param width the frame width in pixels of the preview video.
347
 
        * @param height the frame width in pixels of the preview video.
348
 
        */
349
 
        virtual void start(unsigned width, unsigned height);
350
 
 
351
 
        /** Stop the preview thread.
352
 
        * Stop the thread represented by the Main() function. Blocks until the thread has terminated.
353
 
        */
354
 
        virtual void stop();
355
 
 
356
 
      protected:
357
 
        void Main ();
358
 
        char* frame;
359
 
 
360
 
        bool end_thread;
361
 
        bool pause_thread;
362
 
        PMutex     thread_ended;
363
 
        PSyncPoint thread_paused;
364
 
        PSyncPoint run_thread;
365
 
 
366
 
        VideoInputCore  & videoinput_core;
367
 
        VideoOutputCore & videooutput_core;
368
 
      };
369
 
 
370
 
      /** Class for storing the device configuration.
371
 
        *
372
 
        * This class is used for storing the device configuration when
373
 
        * streaming video or when in preview mode. The device configuration
374
 
        * consists of width, height and framerate. This class can be used to 
375
 
        * check whether stream and preview configuration is different and
376
 
        * thus if a device needs to be reopened when switching between stream
377
 
        * and preview mode.
378
 
        */
379
 
      class VideoDeviceConfig
380
 
      {
381
 
       public:
382
 
        bool active;
383
 
        unsigned width;
384
 
        unsigned height;
385
 
        unsigned fps;
386
 
 
387
 
        VideoDeviceConfig () {}
388
 
        VideoDeviceConfig (unsigned _width, unsigned _height, unsigned _fps) {
389
 
          width = _width;
390
 
          height = _height;
391
 
          fps = _fps;
392
 
        }
393
 
 
394
 
        friend std::ostream& operator <<(std::ostream & stream, const VideoDeviceConfig & config){
395
 
          stream << config.width << "x" << config.height << "/" << config.fps;
396
 
          return stream;
397
 
        }
398
 
 
399
 
        VideoDeviceConfig & operator= (const VideoDeviceConfig & rhs)
400
 
        {
401
 
            width  = rhs.width;
402
 
            height = rhs.height;
403
 
            fps    = rhs.fps;
404
 
            return *this;
405
 
        }
406
 
 
407
 
        bool operator==( const VideoDeviceConfig & rhs ) const
408
 
        {
409
 
          if ( (width  == rhs.width)   && 
410
 
               (height == rhs.height)  &&
411
 
               (fps    == rhs.fps)     )
412
 
          return true;
413
 
        else
414
 
          return false;
415
 
        }
416
 
 
417
 
        bool operator!=( const VideoDeviceConfig & rhs ) const
418
 
        {
419
 
          return (!(*this==rhs));
420
 
        }
421
 
 
422
 
      };
423
 
 
424
 
private:
425
 
      std::set<VideoInputManager *> managers;
426
 
 
427
 
      Ekiga::Runtime & runtime;
428
 
 
429
 
      VideoDeviceConfig       preview_config;
430
 
      VideoDeviceConfig       stream_config;
431
 
 
432
 
      VideoInputManager*      current_manager;
433
 
      VideoInputDevice        desired_device;
434
 
      VideoInputDevice        current_device;
435
 
      VideoInputFormat        current_format;
436
 
      int                     current_channel;
437
 
      VideoInputSettings      current_settings; 
438
 
      VideoInputSettings      desired_settings;
439
 
 
440
 
      PMutex core_mutex;
441
 
      PMutex settings_mutex;
442
 
 
443
 
      VideoPreviewManager preview_manager;
444
 
      VideoInputCoreConfBridge* videoinput_core_conf_bridge;
445
 
    };
446
 
/**
447
 
 * @}
448
 
 */
449
 
};
450
 
 
451
 
#endif