~ubuntu-branches/ubuntu/utopic/lebiniou/utopic

« back to all changes in this revision

Viewing changes to src/context.c

  • Committer: Package Import Robot
  • Author(s): Olivier Girondel
  • Date: 2012-04-22 22:07:40 UTC
  • mfrom: (6.1.7 sid)
  • Revision ID: package-import@ubuntu.com-20120422220740-xncgwhc3g71nopnu
Tags: 3.18-1
* New upstream release 3.18.
* Support older libswscale.
* Add missing Build-Depends: libfreetype6-dev, libasound2-dev,
  libpulse-dev. (Closes: #669437)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
 *  Copyright 1994-2011 Olivier Girondel
 
2
 *  Copyright 1994-2012 Olivier Girondel
3
3
 *
4
4
 *  This file is part of lebiniou.
5
5
 *
29
29
#define DELAY_MAX 30
30
30
 
31
31
 
 
32
#ifdef WITH_WEBCAM
 
33
#include "webcam.h"
 
34
 
 
35
extern int webcams;
 
36
extern char *video_base;
 
37
 
 
38
 
 
39
static webcam_t *cams[MAX_CAMS];
 
40
static pthread_t thr[MAX_CAMS];
 
41
 
 
42
#define MAX_TRIES 5
 
43
 
 
44
 
 
45
static void
 
46
Context_open_webcam(Context_t *ctx)
 
47
{
 
48
  int i;
 
49
  uint8_t try = 0;
 
50
 
 
51
  parse_options();
 
52
 
 
53
  for (i = 0; i < MAX_CAMS; i++)
 
54
    pthread_mutex_init(&ctx->cam_mtx[i], NULL);
 
55
 
 
56
  for (i = 0; (i < webcams) && (try < MAX_TRIES); i++) {
 
57
  retry:
 
58
    cams[i] = xmalloc(sizeof(webcam_t));
 
59
 
 
60
    cams[i]->io = IO_METHOD_MMAP;
 
61
    cams[i]->fd = -1;
 
62
    cams[i]->ctx = ctx;
 
63
    cams[i]->cam_no = i;
 
64
 
 
65
    if (-1 != open_device(cams[i], try)) {
 
66
      if (-1 != init_device(cams[i])) {
 
67
        enumerate_cids(cams[i]);
 
68
        list_inputs(cams[i]);
 
69
        start_capturing(cams[i]);
 
70
 
 
71
        pthread_create(&thr[i], NULL, loop, (void *)cams[i]);
 
72
      } else {
 
73
        fprintf(stderr, "[i] Webcam: failed to initialize device #%d\n", i);
 
74
        close_device(cams[i]);
 
75
        xfree(cams[i]);
 
76
        try++;
 
77
        goto retry;
 
78
      }
 
79
    } else
 
80
      xfree(cams[i]);
 
81
  }
 
82
}
 
83
#endif
 
84
 
 
85
 
32
86
Context_t *
33
87
Context_new()
34
88
{
35
89
  int i;
 
90
 
36
91
  Context_t *ctx = xcalloc(1, sizeof(Context_t));
37
92
 
38
93
  ctx->running = 1;
44
99
      printf("%d ", i);
45
100
    ctx->buffers[i] = Buffer8_new();
46
101
  }
47
 
  printf("\n");
 
102
#ifdef WITH_WEBCAM
 
103
  {
 
104
    int k;
 
105
    for (k = 0; k < webcams; k++) {
 
106
      for (i = 0; i < CAM_SAVE; i++)
 
107
        ctx->cam_save[k][i] = Buffer8_new();
 
108
      ctx->cam_ref[k] = Buffer8_new();
 
109
      ctx->rgba_cam_buffers[k] = BufferRGBA_new();
 
110
    }
 
111
  }
 
112
#endif
 
113
  ctx->rgba_buffers[ACTIVE_BUFFER] = BufferRGBA_new();
 
114
  if (libbiniou_verbose)
 
115
    printf("\n");
 
116
 
 
117
#if WITH_GL
 
118
  glGenTextures(NSCREENS, ctx->textures); // TODO: delete on exit
 
119
  glGenTextures(MAX_CAMS, ctx->cam_textures); // TODO: delete on exit
 
120
#endif
48
121
 
49
122
  if (pictures != NULL) {
50
123
    if (libbiniou_verbose)
73
146
    printf("[+] Initializing sequence manager\n");
74
147
  ctx->sm = SequenceManager_new();
75
148
 
 
149
  Context_load_banks(ctx);
 
150
 
76
151
  if (libbiniou_verbose)
77
152
    printf("[+] Initializing 3D engine\n");
78
153
  Params3d_init(&ctx->params3d);
90
165
 
91
166
  ctx->outputs = NULL;
92
167
 
 
168
#ifdef FEAT_TARGET
 
169
  ctx->target = 1; /* maybe someday we want a switch to disable,
 
170
                      or worse, a global variable :) */
 
171
  ctx->target_pic = Picture8_new();
 
172
  /* FIXME temp */
 
173
  {
 
174
    extern char *data_dir;
 
175
    gchar *tmp;
 
176
    int res;
 
177
 
 
178
    tmp = g_strdup_printf("%s/%s", data_dir, "/images/zebulon/z-biniou-tv-1.png");
 
179
    printf("[+] Loading '%s'\n", tmp);
 
180
    res = Picture8_load_PNG(ctx->target_pic, tmp);
 
181
    assert(!res);
 
182
    if (res == -1)
 
183
      xerror("Picture8_load_PNG(%s)\n", tmp);
 
184
    g_free(tmp);
 
185
  }
 
186
#endif
 
187
 
 
188
#ifdef WITH_WEBCAM
 
189
  printf("[i] Initialiazing %d webcams base: %s\n", webcams, video_base);
 
190
 
 
191
  ctx->cam = 0;
 
192
  Context_open_webcam(ctx);
 
193
 
 
194
  for (i = 0; i < webcams; i++)
 
195
    Buffer8_copy(ctx->target_pic->buff, ctx->cam_save[i][0]);
 
196
#endif
 
197
 
93
198
  return ctx;
94
199
}
95
200
 
96
201
 
 
202
#ifdef WITH_WEBCAM
 
203
static void
 
204
Context_close_webcam(const u_char cam_no)
 
205
{
 
206
  if (NULL != cams[cam_no]) {
 
207
    pthread_join(thr[cam_no], NULL);
 
208
    stop_capturing(cams[cam_no]);
 
209
    uninit_device(cams[cam_no]);
 
210
    close_device(cams[cam_no]);
 
211
  }
 
212
}
 
213
#endif
 
214
 
 
215
 
97
216
void
98
217
Context_delete(Context_t *ctx)
99
218
{
100
219
  int i;
101
220
  GSList *outputs = ctx->outputs;
102
221
 
 
222
#ifdef WITH_WEBCAM
 
223
  if (webcams) {
 
224
    int i;
 
225
 
 
226
    for (i = 0; i < webcams; i++)
 
227
      Context_close_webcam(i);
 
228
  }
 
229
  xfree(video_base);
 
230
#endif
 
231
 
103
232
  if (libbiniou_verbose)
104
233
    printf("[i] %lu frames, %lu events\n", ctx->frames, ctx->nb_events);
105
234
 
120
249
      printf("%d ", i);
121
250
    Buffer8_delete(ctx->buffers[i]);
122
251
  }
 
252
#ifdef WITH_WEBCAM
 
253
  {
 
254
    int k;
 
255
    for (k = 0; k < webcams; k++) {
 
256
      for (i = 0; i < CAM_SAVE; i++)
 
257
        Buffer8_delete(ctx->cam_save[k][i]);
 
258
      Buffer8_delete(ctx->cam_ref[k]);
 
259
      BufferRGBA_delete(ctx->rgba_cam_buffers[k]);
 
260
    }
 
261
  }
 
262
#endif
 
263
  BufferRGBA_delete(ctx->rgba_buffers[ACTIVE_BUFFER]);
123
264
  if (libbiniou_verbose)
124
265
    printf("\n");
125
266
 
148
289
  b_timer_delete(ctx->timer);
149
290
  b_timer_delete(ctx->fps_timer);
150
291
 
 
292
#ifdef FEAT_TARGET
 
293
  if (NULL != ctx->target_pic)
 
294
    Picture8_delete(ctx->target_pic);
 
295
#endif
 
296
 
151
297
  xfree(ctx);
152
298
}
153
299
 
263
409
  } while (Sequence_find(seq, p) != NULL);
264
410
 
265
411
  if (*p->options & BEQ_FLUSH)
266
 
    Sequence_clear(seq);
 
412
    Sequence_clear(seq, 0);
267
413
 
268
414
  Sequence_insert(seq, p);
269
415
 
454
600
{
455
601
  Sequence_t *seq = Sequences_find(id);
456
602
 
 
603
  if (NULL == seq)
 
604
    if (ctx->sm->transient->id == id)
 
605
      seq = ctx->sm->transient;
 
606
 
457
607
  assert(NULL != seq);
458
608
  ctx->sm->curseq = seq->layers;
459
609
  Sequence_copy(seq, ctx->sm->next);
463
613
 
464
614
 
465
615
void
466
 
Context_use_bank(Context_t *ctx, const u_char bank)
 
616
Context_use_sequence_bank(Context_t *ctx, const u_char bank)
467
617
{
468
618
  u_long id;
469
619
 
470
 
  id = ctx->sm->banks[ctx->sm->cur_bankset][bank];
 
620
  id = ctx->banks[SEQUENCES][ctx->bankset[SEQUENCES]][bank];
471
621
  if (id) {
472
622
    Sequence_t *seq;
473
623
 
 
624
    printf("[i] Using sequence in bank #%d\n", (bank+1));
 
625
 
474
626
    if (libbiniou_verbose)
475
627
      printf("[s] Set sequence: %li\n", id);
476
628
 
477
629
    seq = Sequences_find(id);
 
630
    if (NULL == seq)
 
631
      if (ctx->sm->transient->id == id)
 
632
        seq = ctx->sm->transient;
478
633
    assert(NULL != seq);
479
634
    ctx->sm->curseq = seq->layers;
480
635
    Sequence_copy(seq, ctx->sm->next);
481
636
 
482
 
    ctx->bank = bank; /* ca sert a quoi de garder ctx->bank ? --oliv3 */
 
637
    ctx->bank[SEQUENCES] = bank;
483
638
 
484
639
    Context_set(ctx);
485
640
  } else
486
 
    printf("[i] Bank %d/%d is empty\n", ctx->sm->cur_bankset+1, bank+1);
 
641
    printf("[i] Bank %d/%d is empty\n", ctx->bankset[SEQUENCES]+1, bank+1);
487
642
}
488
643
 
489
644
 
490
645
void
491
646
Context_clear_bank(Context_t *ctx, const u_char bank)
492
647
{
493
 
  ctx->sm->banks[ctx->sm->cur_bankset][bank] = 0;
 
648
  ctx->banks[ctx->bank_mode][ctx->bankset[ctx->bank_mode]][bank] = 0;
494
649
}
495
650
 
496
651
 
506
661
{
507
662
  return ctx->buffers[PASSIVE_BUFFER];
508
663
}
 
664
 
 
665
 
 
666
#ifdef WITH_WEBCAM
 
667
void
 
668
Context_push_webcam(Context_t *ctx, Buffer8_t *buff, const int cam)
 
669
{
 
670
  int i;
 
671
 
 
672
  Buffer8_delete(ctx->cam_save[cam][CAM_SAVE-1]);
 
673
  for (i = CAM_SAVE-1; i >= 1; i--)
 
674
    ctx->cam_save[cam][i] = ctx->cam_save[cam][i-1];
 
675
  ctx->cam_save[cam][0] = buff;
 
676
}
 
677
#endif