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

« back to all changes in this revision

Viewing changes to lib/engine/videoinput/skel/videoinput-core.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
 
/*
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.cpp  -  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
 
#include <iostream>
38
 
#include <sstream>
39
 
 
40
 
#include "config.h"
41
 
 
42
 
#include "videoinput-core.h"
43
 
#include "videoinput-manager.h"
44
 
 
45
 
using namespace Ekiga;
46
 
 
47
 
VideoInputCore::VideoPreviewManager::VideoPreviewManager (VideoInputCore& _videoinput_core, VideoOutputCore& _videooutput_core)
48
 
: PThread (1000, NoAutoDeleteThread, HighestPriority, "VideoPreviewManager"),
49
 
    videoinput_core (_videoinput_core),
50
 
  videooutput_core (_videooutput_core)
51
 
{
52
 
  pause_thread = true;
53
 
  end_thread = false;
54
 
  frame = NULL;
55
 
  // Since windows does not like to restart a thread that 
56
 
  // was never started, we do so here
57
 
  this->Resume ();
58
 
  thread_paused.Wait();
59
 
}
60
 
 
61
 
VideoInputCore::VideoPreviewManager::~VideoPreviewManager ()
62
 
{
63
 
  if (!pause_thread)
64
 
    stop();
65
 
  end_thread = true;
66
 
  run_thread.Signal();
67
 
  thread_ended.Wait();
68
 
}
69
 
 
70
 
void VideoInputCore::VideoPreviewManager::start (unsigned width, unsigned height)
71
 
{
72
 
  PTRACE(4, "PreviewManager\tStarting Preview");
73
 
  end_thread = false;
74
 
  frame = (char*) malloc (unsigned (width * height * 3 / 2));
75
 
 
76
 
  videooutput_core.start();
77
 
  pause_thread = false;
78
 
  run_thread.Signal();
79
 
}
80
 
 
81
 
void VideoInputCore::VideoPreviewManager::stop ()
82
 
{
83
 
  PTRACE(4, "PreviewManager\tStopping Preview");
84
 
  pause_thread = true;
85
 
  thread_paused.Wait();
86
 
 
87
 
  if (frame) {
88
 
    free (frame);
89
 
    frame = NULL;
90
 
  }  
91
 
  videooutput_core.stop();
92
 
}
93
 
 
94
 
void VideoInputCore::VideoPreviewManager::Main ()
95
 
{
96
 
  PWaitAndSignal m(thread_ended);
97
 
 
98
 
  unsigned width = 176;
99
 
  unsigned height = 144;;
100
 
  while (!end_thread) {
101
 
 
102
 
    thread_paused.Signal ();
103
 
    run_thread.Wait ();
104
 
 
105
 
    while (!pause_thread) {
106
 
      if (frame) {
107
 
        videoinput_core.get_frame_data(frame, width, height);
108
 
        videooutput_core.set_frame_data(frame, width, height, true, 1);
109
 
      }
110
 
      // We have to sleep some time outside the mutex lock
111
 
      // to give other threads time to get the mutex
112
 
      // It will be taken into account by PAdaptiveDelay
113
 
      Current()->Sleep (5);
114
 
    }
115
 
  }
116
 
}
117
 
 
118
 
VideoInputCore::VideoInputCore (Ekiga::Runtime & _runtime, VideoOutputCore& _videooutput_core)
119
 
:  runtime (_runtime),
120
 
   preview_manager(*this, _videooutput_core)
121
 
{
122
 
  PWaitAndSignal m_var(core_mutex);
123
 
  PWaitAndSignal m_set(settings_mutex);
124
 
 
125
 
  preview_config.active = false;
126
 
  preview_config.width = 176;
127
 
  preview_config.height = 144;
128
 
  preview_config.fps = 30;
129
 
 
130
 
  stream_config.active = false;
131
 
  stream_config.width = 176;
132
 
  stream_config.height = 144;
133
 
  stream_config.fps = 30;
134
 
 
135
 
  current_settings.brightness = 0;
136
 
  current_settings.whiteness = 0;
137
 
  current_settings.colour = 0;
138
 
  current_settings.contrast = 0;
139
 
 
140
 
  desired_settings.brightness = 0;
141
 
  desired_settings.whiteness = 0;
142
 
  desired_settings.colour = 0;
143
 
  desired_settings.contrast = 0;
144
 
 
145
 
  current_manager = NULL;
146
 
  videoinput_core_conf_bridge = NULL;
147
 
}
148
 
 
149
 
VideoInputCore::~VideoInputCore ()
150
 
{
151
 
  PWaitAndSignal m(core_mutex);
152
 
 
153
 
  if (videoinput_core_conf_bridge)
154
 
    delete videoinput_core_conf_bridge;
155
 
 
156
 
  for (std::set<VideoInputManager *>::iterator iter = managers.begin ();
157
 
       iter != managers.end ();
158
 
       iter++)
159
 
    delete (*iter);
160
 
 
161
 
  managers.clear();
162
 
}
163
 
 
164
 
void VideoInputCore::setup_conf_bridge ()
165
 
{
166
 
  PWaitAndSignal m(core_mutex);
167
 
 
168
 
  videoinput_core_conf_bridge = new VideoInputCoreConfBridge (*this);
169
 
}
170
 
 
171
 
void VideoInputCore::add_manager (VideoInputManager &manager)
172
 
{
173
 
  managers.insert (&manager);
174
 
  manager_added.emit (manager);
175
 
 
176
 
  manager.device_opened.connect (sigc::bind (sigc::mem_fun (this, &VideoInputCore::on_device_opened), &manager));
177
 
  manager.device_closed.connect (sigc::bind (sigc::mem_fun (this, &VideoInputCore::on_device_closed), &manager));
178
 
  manager.device_error.connect (sigc::bind (sigc::mem_fun (this, &VideoInputCore::on_device_error), &manager));
179
 
}
180
 
 
181
 
 
182
 
void VideoInputCore::visit_managers (sigc::slot<bool, VideoInputManager &> visitor)
183
 
{
184
 
  PWaitAndSignal m(core_mutex);
185
 
  bool go_on = true;
186
 
  
187
 
  for (std::set<VideoInputManager *>::iterator iter = managers.begin ();
188
 
       iter != managers.end () && go_on;
189
 
       iter++)
190
 
      go_on = visitor (*(*iter));
191
 
}
192
 
 
193
 
void VideoInputCore::get_devices (std::vector <VideoInputDevice> & devices)
194
 
{
195
 
  PWaitAndSignal m(core_mutex);
196
 
 
197
 
  devices.clear();
198
 
  
199
 
  for (std::set<VideoInputManager *>::iterator iter = managers.begin ();
200
 
       iter != managers.end ();
201
 
       iter++)
202
 
    (*iter)->get_devices (devices);
203
 
 
204
 
  if (PTrace::CanTrace(4)) {
205
 
     for (std::vector<VideoInputDevice>::iterator iter = devices.begin ();
206
 
         iter != devices.end ();
207
 
         iter++) {
208
 
      PTRACE(4, "VidInputCore\tDetected Device: " << *iter);
209
 
    }
210
 
  }
211
 
}
212
 
 
213
 
void VideoInputCore::set_device(const VideoInputDevice & device, int channel, VideoInputFormat format)
214
 
{
215
 
  PWaitAndSignal m(core_mutex);
216
 
  internal_set_device(device, channel, format);
217
 
  desired_device  = device;
218
 
}
219
 
 
220
 
void VideoInputCore::add_device (const std::string & source, const std::string & device_name, unsigned capabilities, HalManager* /*manager*/)
221
 
{
222
 
  PTRACE(4, "VidInputCore\tAdding Device " << device_name);
223
 
  PWaitAndSignal m(core_mutex);
224
 
 
225
 
  VideoInputDevice device;
226
 
  for (std::set<VideoInputManager *>::iterator iter = managers.begin ();
227
 
       iter != managers.end ();
228
 
       iter++) {
229
 
    if ((*iter)->has_device (source, device_name, capabilities, device)) {
230
 
 
231
 
      if ( desired_device == device )
232
 
        internal_set_device(device, current_channel, current_format);
233
 
 
234
 
      device_added.emit(device, desired_device == device);
235
 
    }
236
 
  }
237
 
}
238
 
 
239
 
void VideoInputCore::remove_device (const std::string & source, const std::string & device_name, unsigned capabilities, HalManager* /*manager*/)
240
 
{
241
 
  PTRACE(4, "VidInputCore\tRemoving Device " << device_name);
242
 
  PWaitAndSignal m(core_mutex);
243
 
 
244
 
  VideoInputDevice device;
245
 
  for (std::set<VideoInputManager *>::iterator iter = managers.begin ();
246
 
       iter != managers.end ();
247
 
       iter++) {
248
 
     if ((*iter)->has_device (source, device_name, capabilities, device)) {
249
 
       if ( (current_device == device) && (preview_config.active || stream_config.active) ) {
250
 
 
251
 
            VideoInputDevice new_device;
252
 
            new_device.type   = VIDEO_INPUT_FALLBACK_DEVICE_TYPE;
253
 
            new_device.source = VIDEO_INPUT_FALLBACK_DEVICE_SOURCE;
254
 
            new_device.name   = VIDEO_INPUT_FALLBACK_DEVICE_NAME;
255
 
            internal_set_device(new_device, current_channel, current_format);
256
 
       }
257
 
 
258
 
       device_removed.emit(device, current_device == device);
259
 
     }
260
 
  }
261
 
}
262
 
 
263
 
void VideoInputCore::set_preview_config (unsigned width, unsigned height, unsigned fps)
264
 
{
265
 
  PWaitAndSignal m(core_mutex);
266
 
 
267
 
  VideoDeviceConfig new_preview_config(width, height, fps);
268
 
 
269
 
  PTRACE(4, "VidInputCore\tSetting new preview config: " << new_preview_config);
270
 
  // There is only one state where we have to reopen the preview device:
271
 
  // we have preview enabled, no stream is active and some value has changed
272
 
  if ( ( preview_config.active && !stream_config.active) &&
273
 
       ( preview_config        !=  new_preview_config) )
274
 
  {
275
 
    preview_manager.stop();
276
 
    internal_close();
277
 
 
278
 
    internal_open(new_preview_config.width, new_preview_config.height, new_preview_config.fps);
279
 
    preview_manager.start(new_preview_config.width, new_preview_config.height);
280
 
  }
281
 
 
282
 
  preview_config = new_preview_config;
283
 
}
284
 
 
285
 
 
286
 
void VideoInputCore::start_preview ()
287
 
{
288
 
  PWaitAndSignal m(core_mutex);
289
 
 
290
 
  PTRACE(4, "VidInputCore\tStarting preview " << preview_config);
291
 
  if (!preview_config.active && !stream_config.active) {
292
 
    internal_open(preview_config.width, preview_config.height, preview_config.fps);
293
 
    preview_manager.start(preview_config.width, preview_config.height);
294
 
  }
295
 
 
296
 
  preview_config.active = true;
297
 
}
298
 
 
299
 
void VideoInputCore::stop_preview ()
300
 
{
301
 
  PWaitAndSignal m(core_mutex);
302
 
 
303
 
  PTRACE(4, "VidInputCore\tStopping Preview");
304
 
  if (preview_config.active && !stream_config.active) {
305
 
    preview_manager.stop();
306
 
    internal_close();
307
 
    internal_set_manager(desired_device, current_channel, current_format);
308
 
  }
309
 
 
310
 
  preview_config.active = false;
311
 
}
312
 
 
313
 
void VideoInputCore::set_stream_config (unsigned width, unsigned height, unsigned fps)
314
 
{
315
 
  PWaitAndSignal m(core_mutex);
316
 
 
317
 
  VideoDeviceConfig new_stream_config(width, height, fps);
318
 
  PTRACE(4, "VidInputCore\tSetting new stream config: " << new_stream_config);
319
 
 
320
 
  // We do not support switching of framerate or resolution within a stream
321
 
  // since many endpoints will probably have problems with that. Also, it would add
322
 
  // a lot of complexity due to the capabilities exchange. Thus these values will 
323
 
  // not be used until the next start_stream.
324
 
 
325
 
  if (!stream_config.active)
326
 
    stream_config = new_stream_config;
327
 
}
328
 
 
329
 
void VideoInputCore::start_stream ()
330
 
{
331
 
  PWaitAndSignal m(core_mutex);
332
 
 
333
 
  PTRACE(4, "VidInputCore\tStarting stream " << stream_config);
334
 
  if (preview_config.active && !stream_config.active) {
335
 
    preview_manager.stop();
336
 
    if ( preview_config != stream_config ) 
337
 
    {
338
 
      internal_close();
339
 
      internal_open(stream_config.width, stream_config.height, stream_config.fps);
340
 
    }
341
 
  }
342
 
 
343
 
  if (!preview_config.active && !stream_config.active) {
344
 
    internal_open(stream_config.width, stream_config.height, stream_config.fps);
345
 
  }
346
 
 
347
 
  stream_config.active = true;
348
 
}
349
 
 
350
 
void VideoInputCore::stop_stream ()
351
 
{
352
 
  PWaitAndSignal m(core_mutex);
353
 
 
354
 
  PTRACE(4, "VidInputCore\tStopping Stream");
355
 
  if (preview_config.active && stream_config.active) {
356
 
    if ( preview_config != stream_config ) 
357
 
    {
358
 
      internal_close();
359
 
      internal_set_manager(desired_device, current_channel, current_format);
360
 
      internal_open(preview_config.width, preview_config.height, preview_config.fps);
361
 
    }
362
 
    preview_manager.start(preview_config.width, preview_config.height);
363
 
  }
364
 
 
365
 
  if (!preview_config.active && stream_config.active) {
366
 
    internal_close();
367
 
    internal_set_manager(desired_device, current_channel, current_format);
368
 
  }
369
 
 
370
 
  stream_config.active = false;
371
 
}
372
 
 
373
 
void VideoInputCore::get_frame_data (char *data,
374
 
                                   unsigned & width,
375
 
                                   unsigned & height)
376
 
{
377
 
  PWaitAndSignal m(core_mutex);
378
 
 
379
 
  if (current_manager) {
380
 
    if (!current_manager->get_frame_data(data, width, height)) {
381
 
 
382
 
      internal_close();
383
 
 
384
 
      internal_set_fallback();
385
 
 
386
 
      if (preview_config.active && !stream_config.active)
387
 
        internal_open(preview_config.width, preview_config.height, preview_config.fps);
388
 
 
389
 
      if (stream_config.active)
390
 
        internal_open(stream_config.width, stream_config.height, stream_config.fps);
391
 
 
392
 
      if (current_manager)
393
 
        current_manager->get_frame_data(data, width, height); // the default device must always return true
394
 
    }
395
 
    internal_apply_settings();
396
 
  }
397
 
}
398
 
 
399
 
void VideoInputCore::set_colour (unsigned colour)
400
 
{
401
 
  PWaitAndSignal m(settings_mutex);
402
 
  desired_settings.colour = colour;
403
 
}
404
 
 
405
 
void VideoInputCore::set_brightness (unsigned brightness)
406
 
{
407
 
  PWaitAndSignal m(settings_mutex);
408
 
  desired_settings.brightness = brightness;
409
 
}
410
 
 
411
 
void VideoInputCore::set_whiteness  (unsigned whiteness)
412
 
{
413
 
  PWaitAndSignal m(settings_mutex);
414
 
  desired_settings.whiteness = whiteness;
415
 
}
416
 
 
417
 
void VideoInputCore::set_contrast   (unsigned contrast)
418
 
{
419
 
  PWaitAndSignal m(settings_mutex);
420
 
  desired_settings.contrast = contrast;
421
 
}
422
 
 
423
 
void VideoInputCore::on_device_opened (VideoInputDevice device,
424
 
                                     VideoInputSettings settings, 
425
 
                                     VideoInputManager *manager)
426
 
{
427
 
  device_opened.emit (*manager, device, settings);
428
 
}
429
 
 
430
 
void VideoInputCore::on_device_closed (VideoInputDevice device, VideoInputManager *manager)
431
 
{
432
 
  device_closed.emit (*manager, device);
433
 
}
434
 
 
435
 
void VideoInputCore::on_device_error (VideoInputDevice device, VideoInputErrorCodes error_code, VideoInputManager *manager)
436
 
{
437
 
  device_error.emit (*manager, device, error_code);
438
 
}
439
 
 
440
 
void VideoInputCore::internal_set_device(const VideoInputDevice & device, int channel, VideoInputFormat format)
441
 
{
442
 
  PTRACE(4, "VidInputCore\tSetting device: " << device);
443
 
 
444
 
  if (preview_config.active && !stream_config.active)
445
 
    preview_manager.stop();
446
 
 
447
 
  if (preview_config.active || stream_config.active)
448
 
    internal_close();
449
 
 
450
 
  internal_set_manager (device, channel, format);
451
 
 
452
 
  if (preview_config.active && !stream_config.active) {
453
 
    internal_open(preview_config.width, preview_config.height, preview_config.fps);
454
 
    preview_manager.start(preview_config.width,preview_config.height);
455
 
  }
456
 
 
457
 
  if (stream_config.active)
458
 
    internal_open(stream_config.width, stream_config.height, stream_config.fps);
459
 
}
460
 
 
461
 
void VideoInputCore::internal_set_manager (const VideoInputDevice & device, int channel, VideoInputFormat format)
462
 
{
463
 
  current_manager = NULL;
464
 
  for (std::set<VideoInputManager *>::iterator iter = managers.begin ();
465
 
       iter != managers.end ();
466
 
       iter++) {
467
 
     if ((*iter)->set_device (device, channel, format)) {
468
 
       current_manager = (*iter);
469
 
     }
470
 
  }
471
 
 
472
 
  // If the desired manager could not be found,
473
 
  // we se the default device. The default device
474
 
  // MUST ALWAYS be loaded and openable
475
 
  if (current_manager) {
476
 
    current_device  = device;
477
 
  }
478
 
  else {
479
 
    PTRACE(1, "VidInputCore\tTried to set unexisting device " << device);
480
 
    internal_set_fallback();
481
 
  }
482
 
 
483
 
  current_channel = channel;
484
 
  current_format  = format;
485
 
}
486
 
 
487
 
 
488
 
void VideoInputCore::internal_set_fallback ()
489
 
{
490
 
  current_device.type   = VIDEO_INPUT_FALLBACK_DEVICE_TYPE;
491
 
  current_device.source = VIDEO_INPUT_FALLBACK_DEVICE_SOURCE;
492
 
  current_device.name   = VIDEO_INPUT_FALLBACK_DEVICE_NAME;
493
 
  PTRACE(3, "VidInputCore\tFalling back to " << current_device);
494
 
 
495
 
  internal_set_manager(current_device, current_channel, current_format);
496
 
}
497
 
 
498
 
void VideoInputCore::internal_open (unsigned width, unsigned height, unsigned fps)
499
 
{
500
 
  PTRACE(4, "VidInputCore\tOpening device with " << width << "x" << height << "/" << fps );
501
 
 
502
 
  if (current_manager && !current_manager->open(width, height, fps)) {
503
 
 
504
 
    internal_set_fallback();
505
 
    if (current_manager)
506
 
      current_manager->open(width, height, fps);
507
 
  }
508
 
}
509
 
 
510
 
void VideoInputCore::internal_close()
511
 
{
512
 
  PTRACE(4, "VidInputCore\tClosing current device");
513
 
  if (current_manager)
514
 
    current_manager->close();
515
 
}
516
 
 
517
 
void VideoInputCore::internal_apply_settings()
518
 
{
519
 
  PWaitAndSignal m_set(settings_mutex);
520
 
 
521
 
  if (desired_settings.colour != current_settings.colour) {
522
 
    current_manager->set_colour (desired_settings.colour);
523
 
    current_settings.colour = desired_settings.colour;
524
 
  }
525
 
 
526
 
  if (desired_settings.brightness != current_settings.brightness) {
527
 
    current_manager->set_brightness (desired_settings.brightness);
528
 
    current_settings.brightness = desired_settings.brightness;
529
 
  }
530
 
 
531
 
  if (desired_settings.whiteness != current_settings.whiteness) {
532
 
    current_manager->set_whiteness (desired_settings.whiteness);
533
 
    current_settings.whiteness = desired_settings.whiteness;
534
 
  }
535
 
 
536
 
  if (desired_settings.contrast != current_settings.contrast) {
537
 
    current_manager->set_contrast (desired_settings.contrast);
538
 
    current_settings.contrast = desired_settings.contrast;
539
 
  }
540
 
}