File: videoinput-core.cpp

package info (click to toggle)
ekiga 4.0.1-5
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 31,612 kB
  • ctags: 14,511
  • sloc: cpp: 72,155; sh: 11,298; xml: 8,904; ansic: 8,572; makefile: 1,738; asm: 453; awk: 16
file content (567 lines) | stat: -rw-r--r-- 17,552 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567

/*
 * Ekiga -- A VoIP and Video-Conferencing application
 * Copyright (C) 2000-2009 Damien Sandras <dsandras@seconix.com>

 * This program is free software; you can  redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or (at
 * your option) any later version. This program is distributed in the hope
 * that it will be useful, but WITHOUT ANY WARRANTY; without even the
 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * Ekiga is licensed under the GPL license and as a special exception, you
 * have permission to link or otherwise combine this program with the
 * programs OPAL, OpenH323 and PWLIB, and distribute the combination, without
 * applying the requirements of the GNU GPL to the OPAL, OpenH323 and PWLIB
 * programs, as long as you do follow the requirements of the GNU GPL for all
 * the rest of the software thus combined.
 */


/*
 *                         videoinput-core.cpp  -  description
 *                         ------------------------------------------
 *   begin                : written in 2008 by Matthias Schneider
 *   copyright            : (c) 2008 by Matthias Schneider
 *   description          : declaration of the interface of a videoinput core.
 *                          A videoinput core manages VideoInputManagers.
 *
 */

#include <iostream>
#include <sstream>

#include <glib/gi18n.h>

#include "config.h"

#include "videoinput-core.h"
#include "videoinput-manager.h"

#define VIDEO_DEVICES_KEY "/apps/" PACKAGE_NAME "/devices/video/"

using namespace Ekiga;

VideoInputCore::VideoPreviewManager::VideoPreviewManager (VideoInputCore& _videoinput_core, boost::shared_ptr<VideoOutputCore> _videooutput_core)
: PThread (1000, AutoDeleteThread, HighestPriority, "VideoPreviewManager"),
    videoinput_core (_videoinput_core),
  videooutput_core (_videooutput_core)
{
  width = 176;
  height = 144;;
  pause_thread = true;
  end_thread = false;
  frame = NULL;
  // Since windows does not like to restart a thread that 
  // was never started, we do so here
  this->Resume ();
  thread_paused.Wait();
}

void VideoInputCore::VideoPreviewManager::quit ()
{
  if (!pause_thread)
    stop();
  end_thread = true;
  run_thread.Signal();
  thread_ended.Wait();
}

void VideoInputCore::VideoPreviewManager::start (unsigned _width, unsigned _height)
{
  PTRACE(4, "PreviewManager\tStarting Preview");
  width = _width;
  height = _height;
  end_thread = false;
  frame = (char*) malloc (unsigned (width * height * 3 / 2));

  videooutput_core->start();
  pause_thread = false;
  run_thread.Signal();
}

void VideoInputCore::VideoPreviewManager::stop ()
{
  PTRACE(4, "PreviewManager\tStopping Preview");
  pause_thread = true;
  thread_paused.Wait();

  if (frame) {
    free (frame);
    frame = NULL;
  }  
  videooutput_core->stop();
}

void VideoInputCore::VideoPreviewManager::Main ()
{
  PWaitAndSignal m(thread_ended);

    
  while (!end_thread) {

    thread_paused.Signal ();
    run_thread.Wait ();
    
    while (!pause_thread) {
      if (frame) {
        videoinput_core.get_frame_data(frame);
        videooutput_core->set_frame_data(frame, width, height, 0, 1);
      }
      // We have to sleep some time outside the mutex lock
      // to give other threads time to get the mutex
      // It will be taken into account by PAdaptiveDelay
      Current()->Sleep (5);
    }

  }
}

VideoInputCore::VideoInputCore (Ekiga::ServiceCore & _core,
                                boost::shared_ptr<VideoOutputCore> _videooutput_core)
  : core(_core)
{
  PWaitAndSignal m_var(core_mutex);
  PWaitAndSignal m_set(settings_mutex);

  preview_manager = new VideoPreviewManager (*this, _videooutput_core);

  preview_config.active = false;
  preview_config.width = 176;
  preview_config.height = 144;
  preview_config.fps = 30;

  stream_config.active = false;
  stream_config.width = 176;
  stream_config.height = 144;
  stream_config.fps = 30;

  current_settings.brightness = 0;
  current_settings.whiteness = 0;
  current_settings.colour = 0;
  current_settings.contrast = 0;

  desired_settings.brightness = 0;
  desired_settings.whiteness = 0;
  desired_settings.colour = 0;
  desired_settings.contrast = 0;

  current_manager = NULL;
  videoinput_core_conf_bridge = NULL;
  notification_core = core.get<Ekiga::NotificationCore> ("notification-core");
}

VideoInputCore::~VideoInputCore ()
{
  PWaitAndSignal m(core_mutex);

  if (videoinput_core_conf_bridge)
    delete videoinput_core_conf_bridge;

  preview_manager->quit ();

  for (std::set<VideoInputManager *>::iterator iter = managers.begin ();
       iter != managers.end ();
       iter++)
    delete (*iter);

  managers.clear();
}

void VideoInputCore::setup_conf_bridge ()
{
  PWaitAndSignal m(core_mutex);

  videoinput_core_conf_bridge = new VideoInputCoreConfBridge (*this);
}

void VideoInputCore::add_manager (VideoInputManager &manager)
{
  managers.insert (&manager);
  manager_added (manager);

  manager.device_opened.connect (boost::bind (&VideoInputCore::on_device_opened, this, _1, _2, &manager));
  manager.device_closed.connect (boost::bind (&VideoInputCore::on_device_closed, this, _1, &manager));
  manager.device_error.connect (boost::bind (&VideoInputCore::on_device_error, this, _1, _2, &manager));
}


void VideoInputCore::visit_managers (boost::function1<bool, VideoInputManager &> visitor) const
{
  PWaitAndSignal m(core_mutex);
  bool go_on = true;

  for (std::set<VideoInputManager *>::const_iterator iter = managers.begin ();
       iter != managers.end () && go_on;
       iter++)
      go_on = visitor (*(*iter));
}

void VideoInputCore::get_devices (std::vector <VideoInputDevice> & devices)
{
  PWaitAndSignal m(core_mutex);

  devices.clear();

  for (std::set<VideoInputManager *>::iterator iter = managers.begin ();
       iter != managers.end ();
       iter++)
    (*iter)->get_devices (devices);

#if PTRACING
  for (std::vector<VideoInputDevice>::iterator iter = devices.begin ();
       iter != devices.end ();
       iter++) {
    PTRACE(4, "VidInputCore\tDetected Device: " << *iter);
  }
#endif
}

void VideoInputCore::set_device(const VideoInputDevice & device, int channel, VideoInputFormat format)
{
  PWaitAndSignal m(core_mutex);
  internal_set_device(device, channel, format);
  desired_device  = device;
}

void VideoInputCore::add_device (const std::string & source, const std::string & device_name, unsigned capabilities, HalManager* /*manager*/)
{
  PTRACE(4, "VidInputCore\tAdding Device " << device_name);
  PWaitAndSignal m(core_mutex);

  VideoInputDevice device;
  for (std::set<VideoInputManager *>::iterator iter = managers.begin ();
       iter != managers.end ();
       iter++) {
    if ((*iter)->has_device (source, device_name, capabilities, device)) {

      if ( desired_device == device ) {
        internal_set_device(device, current_channel, current_format);
        boost::shared_ptr<Ekiga::Notification> notif (new Ekiga::Notification (Ekiga::Notification::Info, _("New device detected"), device.GetString ()));
        notification_core->push_notification (notif);
      }
      else {

        boost::shared_ptr<Ekiga::Notification> notif (new Ekiga::Notification (Ekiga::Notification::Info, _("New device detected"), device.GetString (), _("Use it"), boost::bind (&VideoInputCore::on_set_device, (VideoInputCore*) this, device)));
        notification_core->push_notification (notif);
      }

      device_added(device, desired_device == device);
    }
  }
}

void VideoInputCore::remove_device (const std::string & source, const std::string & device_name, unsigned capabilities, HalManager* /*manager*/)
{
  PTRACE(4, "VidInputCore\tRemoving Device " << device_name);
  PWaitAndSignal m(core_mutex);

  VideoInputDevice device;
  for (std::set<VideoInputManager *>::iterator iter = managers.begin ();
       iter != managers.end ();
       iter++) {
     if ((*iter)->has_device (source, device_name, capabilities, device)) {
       if ( (current_device == device) && (preview_config.active || stream_config.active) ) {

            VideoInputDevice new_device;
            new_device.type   = VIDEO_INPUT_FALLBACK_DEVICE_TYPE;
            new_device.source = VIDEO_INPUT_FALLBACK_DEVICE_SOURCE;
            new_device.name   = VIDEO_INPUT_FALLBACK_DEVICE_NAME;
            internal_set_device(new_device, current_channel, current_format);
       }

       device_removed(device, current_device == device);

       boost::shared_ptr<Ekiga::Notification> notif (new Ekiga::Notification (Ekiga::Notification::Info, _("Device removed"), device.GetString ()));
       notification_core->push_notification (notif);
     }
  }
}

void VideoInputCore::set_preview_config (unsigned width, unsigned height, unsigned fps)
{
  PWaitAndSignal m(core_mutex);

  VideoDeviceConfig new_preview_config(width, height, fps);

  PTRACE(4, "VidInputCore\tSetting new preview config: " << new_preview_config);
  // There is only one state where we have to reopen the preview device:
  // we have preview enabled, no stream is active and some value has changed
  if ( ( preview_config.active && !stream_config.active) &&
       ( preview_config        !=  new_preview_config) )
  {
    preview_manager->stop();
    internal_close();

    internal_open(new_preview_config.width, new_preview_config.height, new_preview_config.fps);
    preview_manager->start(new_preview_config.width, new_preview_config.height);
  }

  preview_config = new_preview_config;
}


void VideoInputCore::start_preview ()
{
  PWaitAndSignal m(core_mutex);

  PTRACE(4, "VidInputCore\tStarting preview " << preview_config);
  if (!preview_config.active && !stream_config.active) {
    internal_open(preview_config.width, preview_config.height, preview_config.fps);
    preview_manager->start(preview_config.width, preview_config.height);
  }

  preview_config.active = true;
}

void VideoInputCore::stop_preview ()
{
  PWaitAndSignal m(core_mutex);

  PTRACE(4, "VidInputCore\tStopping Preview");
  if (preview_config.active && !stream_config.active) {
    preview_manager->stop();
    internal_close();
    internal_set_manager(desired_device, current_channel, current_format);
  }

  preview_config.active = false;
}

void VideoInputCore::set_stream_config (unsigned width, unsigned height, unsigned fps)
{
  PWaitAndSignal m(core_mutex);

  VideoDeviceConfig new_stream_config(width, height, fps);
  PTRACE(4, "VidInputCore\tSetting new stream config: " << new_stream_config);

  // We do not support switching of framerate or resolution within a stream
  // since many endpoints will probably have problems with that. Also, it would add
  // a lot of complexity due to the capabilities exchange. Thus these values will 
  // not be used until the next start_stream.

  if (!stream_config.active)
    stream_config = new_stream_config;
}

void VideoInputCore::start_stream ()
{
  PWaitAndSignal m(core_mutex);

  PTRACE(4, "VidInputCore\tStarting stream " << stream_config);
  if (preview_config.active && !stream_config.active) {
    preview_manager->stop();
    if ( preview_config != stream_config ) 
    {
      internal_close();
      internal_open(stream_config.width, stream_config.height, stream_config.fps);
    }
  }

  if (!preview_config.active && !stream_config.active) {
    internal_open(stream_config.width, stream_config.height, stream_config.fps);
  }

  stream_config.active = true;
}

void VideoInputCore::stop_stream ()
{
  PWaitAndSignal m(core_mutex);

  PTRACE(4, "VidInputCore\tStopping Stream");
  if (preview_config.active && stream_config.active) {
    if ( preview_config != stream_config ) 
    {
      internal_close();
      internal_set_manager(desired_device, current_channel, current_format);
      internal_open(preview_config.width, preview_config.height, preview_config.fps);
    }
    preview_manager->start(preview_config.width, preview_config.height);
  }

  if (!preview_config.active && stream_config.active) {
    internal_close();
    internal_set_manager(desired_device, current_channel, current_format);
  }

  stream_config.active = false;
}

void VideoInputCore::get_frame_data (char *data)
{
  PWaitAndSignal m(core_mutex);

  if (current_manager) {
    if (!current_manager->get_frame_data(data)) {

      internal_close();

      internal_set_fallback();

      if (preview_config.active && !stream_config.active)
        internal_open(preview_config.width, preview_config.height, preview_config.fps);

      if (stream_config.active)
        internal_open(stream_config.width, stream_config.height, stream_config.fps);

      if (current_manager)
        current_manager->get_frame_data(data); // the default device must always return true
    }
    internal_apply_settings();
  }
}

void VideoInputCore::set_colour (unsigned colour)
{
  PWaitAndSignal m(settings_mutex);
  desired_settings.colour = colour;
}

void VideoInputCore::set_brightness (unsigned brightness)
{
  PWaitAndSignal m(settings_mutex);
  desired_settings.brightness = brightness;
}

void VideoInputCore::set_whiteness  (unsigned whiteness)
{
  PWaitAndSignal m(settings_mutex);
  desired_settings.whiteness = whiteness;
}

void VideoInputCore::set_contrast   (unsigned contrast)
{
  PWaitAndSignal m(settings_mutex);
  desired_settings.contrast = contrast;
}

void VideoInputCore::on_set_device (const VideoInputDevice & device)
{
  gm_conf_set_string (VIDEO_DEVICES_KEY "input_device", device.GetString ().c_str ());
}

void VideoInputCore::on_device_opened (VideoInputDevice device,
                                     VideoInputSettings settings,
                                     VideoInputManager *manager)
{
  device_opened (*manager, device, settings);
}

void VideoInputCore::on_device_closed (VideoInputDevice device, VideoInputManager *manager)
{
  device_closed (*manager, device);
}

void VideoInputCore::on_device_error (VideoInputDevice device, VideoInputErrorCodes error_code, VideoInputManager *manager)
{
  device_error (*manager, device, error_code);
}

void VideoInputCore::internal_set_device(const VideoInputDevice & device, int channel, VideoInputFormat format)
{
  PTRACE(4, "VidInputCore\tSetting device: " << device);

  if (preview_config.active && !stream_config.active)
    preview_manager->stop();

  if (preview_config.active || stream_config.active)
    internal_close();

  internal_set_manager (device, channel, format);

  if (preview_config.active && !stream_config.active) {
    internal_open(preview_config.width, preview_config.height, preview_config.fps);
    preview_manager->start(preview_config.width,preview_config.height);
  }

  if (stream_config.active)
    internal_open(stream_config.width, stream_config.height, stream_config.fps);
}

void VideoInputCore::internal_set_manager (const VideoInputDevice & device, int channel, VideoInputFormat format)
{
  current_manager = NULL;
  for (std::set<VideoInputManager *>::iterator iter = managers.begin ();
       iter != managers.end ();
       iter++) {
     if ((*iter)->set_device (device, channel, format)) {
       current_manager = (*iter);
     }
  }

  // If the desired manager could not be found,
  // we se the default device. The default device
  // MUST ALWAYS be loaded and openable
  if (current_manager) {
    current_device  = device;
  }
  else {
    PTRACE(1, "VidInputCore\tTried to set unexisting device " << device);
    internal_set_fallback();
  }

  current_channel = channel;
  current_format  = format;
}


void VideoInputCore::internal_set_fallback ()
{
  current_device.type   = VIDEO_INPUT_FALLBACK_DEVICE_TYPE;
  current_device.source = VIDEO_INPUT_FALLBACK_DEVICE_SOURCE;
  current_device.name   = VIDEO_INPUT_FALLBACK_DEVICE_NAME;
  PTRACE(3, "VidInputCore\tFalling back to " << current_device);

  internal_set_manager(current_device, current_channel, current_format);
}

void VideoInputCore::internal_open (unsigned width, unsigned height, unsigned fps)
{
  PTRACE(4, "VidInputCore\tOpening device with " << width << "x" << height << "/" << fps );

  if (current_manager && !current_manager->open(width, height, fps)) {

    internal_set_fallback();
    if (current_manager)
      current_manager->open(width, height, fps);
  }
}

void VideoInputCore::internal_close()
{
  PTRACE(4, "VidInputCore\tClosing current device");
  if (current_manager)
    current_manager->close();
}

void VideoInputCore::internal_apply_settings()
{
  PWaitAndSignal m_set(settings_mutex);

  if (desired_settings.colour != current_settings.colour) {
    current_manager->set_colour (desired_settings.colour);
    current_settings.colour = desired_settings.colour;
  }

  if (desired_settings.brightness != current_settings.brightness) {
    current_manager->set_brightness (desired_settings.brightness);
    current_settings.brightness = desired_settings.brightness;
  }

  if (desired_settings.whiteness != current_settings.whiteness) {
    current_manager->set_whiteness (desired_settings.whiteness);
    current_settings.whiteness = desired_settings.whiteness;
  }

  if (desired_settings.contrast != current_settings.contrast) {
    current_manager->set_contrast (desired_settings.contrast);
    current_settings.contrast = desired_settings.contrast;
  }
}