~ubuntu-branches/ubuntu/wily/sflphone/wily

« back to all changes in this revision

Viewing changes to daemon/libs/pjproject-2.2.1/pjmedia/src/pjmedia-videodev/colorbar_dev.c

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2015-01-07 14:51:16 UTC
  • mfrom: (4.3.5 sid)
  • Revision ID: package-import@ubuntu.com-20150107145116-yxnafinf4lrdvrmx
Tags: 1.4.1-0.1ubuntu1
* Merge with Debian, remaining changes:
 - Drop soprano, nepomuk build-dep
* Drop ubuntu patches, now upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* $Id: colorbar_dev.c 4722 2014-01-29 10:40:40Z nanang $ */
 
2
/*
 
3
 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
 
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
 
8
 * (at your option) any later version.
 
9
 *
 
10
 * This program is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 * GNU General Public License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU General Public License
 
16
 * along with this program; if not, write to the Free Software
 
17
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
18
 */
 
19
#include <pjmedia-videodev/videodev_imp.h>
 
20
#include <pj/assert.h>
 
21
#include <pj/log.h>
 
22
#include <pj/os.h>
 
23
#include <pj/rand.h>
 
24
 
 
25
 
 
26
#if defined(PJMEDIA_HAS_VIDEO) && PJMEDIA_HAS_VIDEO != 0 && \
 
27
    defined(PJMEDIA_VIDEO_DEV_HAS_CBAR_SRC) && \
 
28
    PJMEDIA_VIDEO_DEV_HAS_CBAR_SRC != 0
 
29
    
 
30
 
 
31
 
 
32
#define THIS_FILE               "colorbar_dev.c"
 
33
#define DEFAULT_CLOCK_RATE      90000
 
34
#define DEFAULT_WIDTH           352 //640
 
35
#define DEFAULT_HEIGHT          288 //480
 
36
#define DEFAULT_FPS             25
 
37
 
 
38
/* cbar_ device info */
 
39
struct cbar_dev_info
 
40
{
 
41
    pjmedia_vid_dev_info         info;
 
42
};
 
43
 
 
44
/* cbar_ factory */
 
45
struct cbar_factory
 
46
{
 
47
    pjmedia_vid_dev_factory      base;
 
48
    pj_pool_t                   *pool;
 
49
    pj_pool_factory             *pf;
 
50
 
 
51
    unsigned                     dev_count;
 
52
    struct cbar_dev_info        *dev_info;
 
53
};
 
54
 
 
55
struct cbar_fmt_info {
 
56
    pjmedia_format_id            fmt_id;        /* Format ID                */
 
57
 
 
58
    /* Info for packed formats. */
 
59
    unsigned                     c_offset[3];   /* Color component offset, 
 
60
                                                   in bytes                 */
 
61
    unsigned                     c_stride[3];   /* Color component stride,
 
62
                                                   or distance between two 
 
63
                                                   consecutive same color
 
64
                                                   components, in bytes     */
 
65
};
 
66
 
 
67
/* Colorbar video source supports */
 
68
static struct cbar_fmt_info cbar_fmts[] =
 
69
{
 
70
    /* Packed formats */
 
71
    { PJMEDIA_FORMAT_YUY2,      {0, 1, 3}, {2, 4, 4} },
 
72
    { PJMEDIA_FORMAT_UYVY,      {1, 0, 2}, {2, 4, 4} },
 
73
    { PJMEDIA_FORMAT_YVYU,      {0, 3, 1}, {2, 4, 4} },
 
74
    { PJMEDIA_FORMAT_RGBA,      {0, 1, 2}, {4, 4, 4} },
 
75
    { PJMEDIA_FORMAT_RGB24,     {0, 1, 2}, {3, 3, 3} },
 
76
    { PJMEDIA_FORMAT_BGRA,      {2, 1, 0}, {4, 4, 4} },
 
77
 
 
78
    /* Planar formats */
 
79
    { PJMEDIA_FORMAT_YV12 },
 
80
    { PJMEDIA_FORMAT_I420 },
 
81
    { PJMEDIA_FORMAT_I422 },
 
82
    { PJMEDIA_FORMAT_I420JPEG },
 
83
    { PJMEDIA_FORMAT_I422JPEG },
 
84
};
 
85
 
 
86
/* Video stream. */
 
87
struct cbar_stream
 
88
{
 
89
    pjmedia_vid_dev_stream           base;          /**< Base stream        */
 
90
    pjmedia_vid_dev_param            param;         /**< Settings           */
 
91
    pj_pool_t                       *pool;          /**< Memory pool.       */
 
92
 
 
93
    pjmedia_vid_dev_cb               vid_cb;        /**< Stream callback.   */
 
94
    void                            *user_data;     /**< Application data.  */
 
95
 
 
96
    const struct cbar_fmt_info      *cbfi;
 
97
    const pjmedia_video_format_info *vfi;
 
98
    pjmedia_video_apply_fmt_param    vafp;
 
99
    pj_uint8_t                      *first_line[PJMEDIA_MAX_VIDEO_PLANES];
 
100
    pj_timestamp                     ts;
 
101
    unsigned                         ts_inc;
 
102
};
 
103
 
 
104
 
 
105
/* Prototypes */
 
106
static pj_status_t cbar_factory_init(pjmedia_vid_dev_factory *f);
 
107
static pj_status_t cbar_factory_destroy(pjmedia_vid_dev_factory *f);
 
108
static pj_status_t cbar_factory_refresh(pjmedia_vid_dev_factory *f); 
 
109
static unsigned    cbar_factory_get_dev_count(pjmedia_vid_dev_factory *f);
 
110
static pj_status_t cbar_factory_get_dev_info(pjmedia_vid_dev_factory *f,
 
111
                                             unsigned index,
 
112
                                             pjmedia_vid_dev_info *info);
 
113
static pj_status_t cbar_factory_default_param(pj_pool_t *pool,
 
114
                                              pjmedia_vid_dev_factory *f,
 
115
                                              unsigned index,
 
116
                                              pjmedia_vid_dev_param *param);
 
117
static pj_status_t cbar_factory_create_stream(
 
118
                                        pjmedia_vid_dev_factory *f,
 
119
                                        pjmedia_vid_dev_param *param,
 
120
                                        const pjmedia_vid_dev_cb *cb,
 
121
                                        void *user_data,
 
122
                                        pjmedia_vid_dev_stream **p_vid_strm);
 
123
 
 
124
static pj_status_t cbar_stream_get_param(pjmedia_vid_dev_stream *strm,
 
125
                                         pjmedia_vid_dev_param *param);
 
126
static pj_status_t cbar_stream_get_cap(pjmedia_vid_dev_stream *strm,
 
127
                                       pjmedia_vid_dev_cap cap,
 
128
                                       void *value);
 
129
static pj_status_t cbar_stream_set_cap(pjmedia_vid_dev_stream *strm,
 
130
                                       pjmedia_vid_dev_cap cap,
 
131
                                       const void *value);
 
132
static pj_status_t cbar_stream_get_frame(pjmedia_vid_dev_stream *strm,
 
133
                                         pjmedia_frame *frame);
 
134
static pj_status_t cbar_stream_start(pjmedia_vid_dev_stream *strm);
 
135
static pj_status_t cbar_stream_stop(pjmedia_vid_dev_stream *strm);
 
136
static pj_status_t cbar_stream_destroy(pjmedia_vid_dev_stream *strm);
 
137
 
 
138
/* Operations */
 
139
static pjmedia_vid_dev_factory_op factory_op =
 
140
{
 
141
    &cbar_factory_init,
 
142
    &cbar_factory_destroy,
 
143
    &cbar_factory_get_dev_count,
 
144
    &cbar_factory_get_dev_info,
 
145
    &cbar_factory_default_param,
 
146
    &cbar_factory_create_stream,
 
147
    &cbar_factory_refresh
 
148
};
 
149
 
 
150
static pjmedia_vid_dev_stream_op stream_op =
 
151
{
 
152
    &cbar_stream_get_param,
 
153
    &cbar_stream_get_cap,
 
154
    &cbar_stream_set_cap,
 
155
    &cbar_stream_start,
 
156
    &cbar_stream_get_frame,
 
157
    NULL,
 
158
    &cbar_stream_stop,
 
159
    &cbar_stream_destroy
 
160
};
 
161
 
 
162
 
 
163
/****************************************************************************
 
164
 * Factory operations
 
165
 */
 
166
/*
 
167
 * Init cbar_ video driver.
 
168
 */
 
169
pjmedia_vid_dev_factory* pjmedia_cbar_factory(pj_pool_factory *pf)
 
170
{
 
171
    struct cbar_factory *f;
 
172
    pj_pool_t *pool;
 
173
 
 
174
    pool = pj_pool_create(pf, "cbar video", 512, 512, NULL);
 
175
    f = PJ_POOL_ZALLOC_T(pool, struct cbar_factory);
 
176
    f->pf = pf;
 
177
    f->pool = pool;
 
178
    f->base.op = &factory_op;
 
179
 
 
180
    return &f->base;
 
181
}
 
182
 
 
183
 
 
184
/* API: init factory */
 
185
static pj_status_t cbar_factory_init(pjmedia_vid_dev_factory *f)
 
186
{
 
187
    struct cbar_factory *cf = (struct cbar_factory*)f;
 
188
    struct cbar_dev_info *ddi;
 
189
    unsigned i;
 
190
 
 
191
    cf->dev_count = 1;
 
192
    cf->dev_info = (struct cbar_dev_info*)
 
193
                   pj_pool_calloc(cf->pool, cf->dev_count,
 
194
                                  sizeof(struct cbar_dev_info));
 
195
 
 
196
    ddi = &cf->dev_info[0];
 
197
    pj_bzero(ddi, sizeof(*ddi));
 
198
    pj_ansi_strncpy(ddi->info.name, "Colorbar generator",
 
199
                    sizeof(ddi->info.name));
 
200
    ddi->info.driver[sizeof(ddi->info.driver)-1] = '\0';
 
201
    pj_ansi_strncpy(ddi->info.driver, "Colorbar", sizeof(ddi->info.driver));
 
202
    ddi->info.driver[sizeof(ddi->info.driver)-1] = '\0';
 
203
    ddi->info.dir = PJMEDIA_DIR_CAPTURE;
 
204
    ddi->info.has_callback = PJ_FALSE;
 
205
 
 
206
    ddi->info.caps = PJMEDIA_VID_DEV_CAP_FORMAT;
 
207
    ddi->info.fmt_cnt = sizeof(cbar_fmts)/sizeof(cbar_fmts[0]);
 
208
    for (i = 0; i < ddi->info.fmt_cnt; i++) {
 
209
        pjmedia_format *fmt = &ddi->info.fmt[i];
 
210
        pjmedia_format_init_video(fmt, cbar_fmts[i].fmt_id,
 
211
                                  DEFAULT_WIDTH, DEFAULT_HEIGHT,
 
212
                                  DEFAULT_FPS, 1);
 
213
    }
 
214
 
 
215
    PJ_LOG(4, (THIS_FILE, "Colorbar video src initialized with %d device(s):",
 
216
               cf->dev_count));
 
217
    for (i = 0; i < cf->dev_count; i++) {
 
218
        PJ_LOG(4, (THIS_FILE, "%2d: %s", i, cf->dev_info[i].info.name));
 
219
    }
 
220
 
 
221
    return PJ_SUCCESS;
 
222
}
 
223
 
 
224
/* API: destroy factory */
 
225
static pj_status_t cbar_factory_destroy(pjmedia_vid_dev_factory *f)
 
226
{
 
227
    struct cbar_factory *cf = (struct cbar_factory*)f;
 
228
    pj_pool_t *pool = cf->pool;
 
229
 
 
230
    cf->pool = NULL;
 
231
    pj_pool_release(pool);
 
232
 
 
233
    return PJ_SUCCESS;
 
234
}
 
235
 
 
236
/* API: refresh the list of devices */
 
237
static pj_status_t cbar_factory_refresh(pjmedia_vid_dev_factory *f)
 
238
{
 
239
    PJ_UNUSED_ARG(f);
 
240
    return PJ_SUCCESS;
 
241
}
 
242
 
 
243
/* API: get number of devices */
 
244
static unsigned cbar_factory_get_dev_count(pjmedia_vid_dev_factory *f)
 
245
{
 
246
    struct cbar_factory *cf = (struct cbar_factory*)f;
 
247
    return cf->dev_count;
 
248
}
 
249
 
 
250
/* API: get device info */
 
251
static pj_status_t cbar_factory_get_dev_info(pjmedia_vid_dev_factory *f,
 
252
                                             unsigned index,
 
253
                                             pjmedia_vid_dev_info *info)
 
254
{
 
255
    struct cbar_factory *cf = (struct cbar_factory*)f;
 
256
 
 
257
    PJ_ASSERT_RETURN(index < cf->dev_count, PJMEDIA_EVID_INVDEV);
 
258
 
 
259
    pj_memcpy(info, &cf->dev_info[index].info, sizeof(*info));
 
260
 
 
261
    return PJ_SUCCESS;
 
262
}
 
263
 
 
264
/* API: create default device parameter */
 
265
static pj_status_t cbar_factory_default_param(pj_pool_t *pool,
 
266
                                              pjmedia_vid_dev_factory *f,
 
267
                                              unsigned index,
 
268
                                              pjmedia_vid_dev_param *param)
 
269
{
 
270
    struct cbar_factory *cf = (struct cbar_factory*)f;
 
271
    struct cbar_dev_info *di = &cf->dev_info[index];
 
272
 
 
273
    PJ_ASSERT_RETURN(index < cf->dev_count, PJMEDIA_EVID_INVDEV);
 
274
 
 
275
    PJ_UNUSED_ARG(pool);
 
276
 
 
277
    pj_bzero(param, sizeof(*param));
 
278
    param->dir = PJMEDIA_DIR_CAPTURE;
 
279
    param->cap_id = index;
 
280
    param->rend_id = PJMEDIA_VID_INVALID_DEV;
 
281
    param->flags = PJMEDIA_VID_DEV_CAP_FORMAT;
 
282
    param->clock_rate = DEFAULT_CLOCK_RATE;
 
283
    pj_memcpy(&param->fmt, &di->info.fmt[0], sizeof(param->fmt));
 
284
 
 
285
    return PJ_SUCCESS;
 
286
}
 
287
 
 
288
static const struct cbar_fmt_info* get_cbar_fmt_info(pjmedia_format_id id)
 
289
{
 
290
    unsigned i;
 
291
 
 
292
    for (i = 0; i < sizeof(cbar_fmts)/sizeof(cbar_fmts[0]); i++) {
 
293
        if (cbar_fmts[i].fmt_id == id)
 
294
            return &cbar_fmts[i];
 
295
    }
 
296
 
 
297
    return NULL;
 
298
}
 
299
 
 
300
static void fill_first_line(pj_uint8_t *first_lines[], 
 
301
                            const struct cbar_fmt_info *cbfi,
 
302
                            const pjmedia_video_format_info *vfi,
 
303
                            const pjmedia_video_apply_fmt_param *vafp)
 
304
{
 
305
    typedef pj_uint8_t color_comp_t[3];
 
306
    color_comp_t rgb_colors[] = 
 
307
    { 
 
308
        {255,255,255}, {255,255,0}, {0,255,255}, {0,255,0},
 
309
        {255,0,255}, {255,0,0}, {0,0,255}, {0,0,0}
 
310
    };
 
311
    color_comp_t yuv_colors[] = 
 
312
    { 
 
313
        //{235,128,128}, {162,44,142}, {131,156,44}, {112,72,58},
 
314
        //{84,184,198}, {65,100,212}, {35,212,114}, {16,128,128}
 
315
        {235,128,128}, {210,16,146}, {170,166,16}, {145,54,34},
 
316
        {106,202,222}, {81,90,240}, {41,240,110}, {16,128,128}
 
317
    };
 
318
 
 
319
    unsigned i, j, k;
 
320
 
 
321
    if (vfi->plane_cnt == 1) {
 
322
        /* Packed */
 
323
 
 
324
        for (i = 0; i < 8; ++i) {
 
325
            /* iterate bars */
 
326
            for (j = 0; j < 3; ++j) {
 
327
                /* iterate color components */
 
328
                pj_uint8_t *p = NULL, c;
 
329
                unsigned bar_width, inc_p;
 
330
 
 
331
                if (vfi->color_model == PJMEDIA_COLOR_MODEL_RGB)
 
332
                    c = rgb_colors[i][j];
 
333
                else
 
334
                    c = yuv_colors[i][j];
 
335
 
 
336
                bar_width = vafp->size.w/8;
 
337
                bar_width /= (cbfi->c_stride[j] * 8 / vfi->bpp);
 
338
                inc_p = cbfi->c_stride[j];
 
339
                p = first_lines[0] + bar_width*i*inc_p + cbfi->c_offset[j];
 
340
 
 
341
                /* draw this color */
 
342
                for (k = 0; k < bar_width; ++k) {
 
343
                    *p = c;
 
344
                    p += inc_p;
 
345
                }
 
346
            }
 
347
        }
 
348
 
 
349
    } else if (vfi->plane_cnt == 3) {
 
350
 
 
351
        for (i = 0; i < 8; ++i) {
 
352
            /* iterate bars */
 
353
            for (j = 0; j < 3; ++j) {
 
354
                /* iterate planes/color components */
 
355
                pj_uint8_t *p = NULL, c;
 
356
                unsigned bar_width;
 
357
 
 
358
                if (vfi->color_model == PJMEDIA_COLOR_MODEL_RGB)
 
359
                    c = rgb_colors[i][j];
 
360
                else {
 
361
                    if (vfi->id == PJMEDIA_FORMAT_YV12 && j > 0)
 
362
                        c = yuv_colors[i][3-j];
 
363
                    else
 
364
                        c = yuv_colors[i][j];
 
365
                }
 
366
 
 
367
                bar_width = vafp->strides[j]/8;
 
368
                p = first_lines[j] + bar_width*i;
 
369
 
 
370
                /* draw this plane/color */
 
371
                for (k = 0; k < bar_width; ++k)
 
372
                    *p++ = c;
 
373
            }
 
374
        }
 
375
    }
 
376
}
 
377
 
 
378
/* API: create stream */
 
379
static pj_status_t cbar_factory_create_stream(
 
380
                                        pjmedia_vid_dev_factory *f,
 
381
                                        pjmedia_vid_dev_param *param,
 
382
                                        const pjmedia_vid_dev_cb *cb,
 
383
                                        void *user_data,
 
384
                                        pjmedia_vid_dev_stream **p_vid_strm)
 
385
{
 
386
    struct cbar_factory *cf = (struct cbar_factory*)f;
 
387
    pj_pool_t *pool;
 
388
    struct cbar_stream *strm;
 
389
    const pjmedia_video_format_detail *vfd;
 
390
    const pjmedia_video_format_info *vfi;
 
391
    pjmedia_video_apply_fmt_param vafp;
 
392
    const struct cbar_fmt_info *cbfi;
 
393
    unsigned i;
 
394
 
 
395
    PJ_ASSERT_RETURN(f && param && p_vid_strm, PJ_EINVAL);
 
396
    PJ_ASSERT_RETURN(param->fmt.type == PJMEDIA_TYPE_VIDEO &&
 
397
                     param->fmt.detail_type == PJMEDIA_FORMAT_DETAIL_VIDEO &&
 
398
                     param->dir == PJMEDIA_DIR_CAPTURE,
 
399
                     PJ_EINVAL);
 
400
 
 
401
    pj_bzero(&vafp, sizeof(vafp));
 
402
 
 
403
    vfd = pjmedia_format_get_video_format_detail(&param->fmt, PJ_TRUE);
 
404
    vfi = pjmedia_get_video_format_info(NULL, param->fmt.id);
 
405
    cbfi = get_cbar_fmt_info(param->fmt.id);
 
406
    if (!vfi || !cbfi)
 
407
        return PJMEDIA_EVID_BADFORMAT;
 
408
 
 
409
    vafp.size = param->fmt.det.vid.size;
 
410
    if (vfi->apply_fmt(vfi, &vafp) != PJ_SUCCESS)
 
411
        return PJMEDIA_EVID_BADFORMAT;
 
412
 
 
413
    /* Create and Initialize stream descriptor */
 
414
    pool = pj_pool_create(cf->pf, "cbar-dev", 512, 512, NULL);
 
415
    PJ_ASSERT_RETURN(pool != NULL, PJ_ENOMEM);
 
416
 
 
417
    strm = PJ_POOL_ZALLOC_T(pool, struct cbar_stream);
 
418
    pj_memcpy(&strm->param, param, sizeof(*param));
 
419
    strm->pool = pool;
 
420
    pj_memcpy(&strm->vid_cb, cb, sizeof(*cb));
 
421
    strm->user_data = user_data;
 
422
    strm->vfi = vfi;
 
423
    strm->cbfi = cbfi;
 
424
    pj_memcpy(&strm->vafp, &vafp, sizeof(vafp));
 
425
    strm->ts_inc = PJMEDIA_SPF2(param->clock_rate, &vfd->fps, 1);
 
426
 
 
427
    for (i = 0; i < vfi->plane_cnt; ++i) {
 
428
        strm->first_line[i] = pj_pool_alloc(pool, vafp.strides[i]);
 
429
        pj_memset(strm->first_line[i], 255, vafp.strides[i]);
 
430
    }
 
431
 
 
432
    fill_first_line(strm->first_line, strm->cbfi, vfi, &strm->vafp);
 
433
 
 
434
    /* Apply the remaining settings */
 
435
/*    if (param->flags & PJMEDIA_VID_DEV_CAP_INPUT_SCALE) {
 
436
        cbar_stream_set_cap(&strm->base,
 
437
                            PJMEDIA_VID_DEV_CAP_INPUT_SCALE,
 
438
                            &param->fmt);
 
439
    }
 
440
*/
 
441
    /* Done */
 
442
    strm->base.op = &stream_op;
 
443
    *p_vid_strm = &strm->base;
 
444
 
 
445
    return PJ_SUCCESS;
 
446
}
 
447
 
 
448
/* API: Get stream info. */
 
449
static pj_status_t cbar_stream_get_param(pjmedia_vid_dev_stream *s,
 
450
                                         pjmedia_vid_dev_param *pi)
 
451
{
 
452
    struct cbar_stream *strm = (struct cbar_stream*)s;
 
453
 
 
454
    PJ_ASSERT_RETURN(strm && pi, PJ_EINVAL);
 
455
 
 
456
    pj_memcpy(pi, &strm->param, sizeof(*pi));
 
457
 
 
458
/*    if (cbar_stream_get_cap(s, PJMEDIA_VID_DEV_CAP_INPUT_SCALE,
 
459
                            &pi->fmt.info_size) == PJ_SUCCESS)
 
460
    {
 
461
        pi->flags |= PJMEDIA_VID_DEV_CAP_INPUT_SCALE;
 
462
    }
 
463
*/
 
464
    return PJ_SUCCESS;
 
465
}
 
466
 
 
467
/* API: get capability */
 
468
static pj_status_t cbar_stream_get_cap(pjmedia_vid_dev_stream *s,
 
469
                                       pjmedia_vid_dev_cap cap,
 
470
                                       void *pval)
 
471
{
 
472
    struct cbar_stream *strm = (struct cbar_stream*)s;
 
473
 
 
474
    PJ_UNUSED_ARG(strm);
 
475
 
 
476
    PJ_ASSERT_RETURN(s && pval, PJ_EINVAL);
 
477
 
 
478
    if (cap==PJMEDIA_VID_DEV_CAP_INPUT_SCALE)
 
479
    {
 
480
        return PJMEDIA_EVID_INVCAP;
 
481
//      return PJ_SUCCESS;
 
482
    } else {
 
483
        return PJMEDIA_EVID_INVCAP;
 
484
    }
 
485
}
 
486
 
 
487
/* API: set capability */
 
488
static pj_status_t cbar_stream_set_cap(pjmedia_vid_dev_stream *s,
 
489
                                       pjmedia_vid_dev_cap cap,
 
490
                                       const void *pval)
 
491
{
 
492
    struct cbar_stream *strm = (struct cbar_stream*)s;
 
493
 
 
494
    PJ_UNUSED_ARG(strm);
 
495
 
 
496
    PJ_ASSERT_RETURN(s && pval, PJ_EINVAL);
 
497
 
 
498
    if (cap==PJMEDIA_VID_DEV_CAP_INPUT_SCALE)
 
499
    {
 
500
        return PJ_SUCCESS;
 
501
    }
 
502
 
 
503
    return PJMEDIA_EVID_INVCAP;
 
504
}
 
505
 
 
506
static pj_status_t spectrum_run(struct cbar_stream *d, pj_uint8_t *p,
 
507
                                pj_size_t size)
 
508
{
 
509
    unsigned i;
 
510
    pj_uint8_t *ptr = p;
 
511
    pj_time_val tv;
 
512
 
 
513
    PJ_UNUSED_ARG(size);
 
514
 
 
515
    /* Subsequent lines */
 
516
    for (i=0; i<d->vfi->plane_cnt; ++i) {
 
517
        pj_uint8_t *plane_end;
 
518
 
 
519
        plane_end = ptr + d->vafp.plane_bytes[i];
 
520
        while (ptr < plane_end) {
 
521
            pj_memcpy(ptr, d->first_line[i], d->vafp.strides[i]);
 
522
            ptr += d->vafp.strides[i]; 
 
523
        }
 
524
    }
 
525
 
 
526
    /* blinking dot */
 
527
    pj_gettimeofday(&tv);
 
528
    if (tv.msec < 660) {
 
529
        enum { DOT_SIZE = 8 };
 
530
        pj_uint8_t dot_clr_rgb[3] = {255, 255, 255};
 
531
        pj_uint8_t dot_clr_yuv[3] = {235, 128, 128};
 
532
 
 
533
        if (d->vfi->plane_cnt == 1) {
 
534
            for (i = 0; i < 3; ++i) {
 
535
                pj_uint8_t *ptr;
 
536
                unsigned j, k, inc_ptr;
 
537
                pj_size_t dot_size = DOT_SIZE;
 
538
 
 
539
                dot_size /= (d->cbfi->c_stride[i] * 8 / d->vfi->bpp);
 
540
                inc_ptr = d->cbfi->c_stride[i];
 
541
                for (j = 0; j < dot_size; ++j) {
 
542
                    ptr = p + d->vafp.strides[0]*(dot_size+j+1) - 
 
543
                          2*dot_size*inc_ptr + d->cbfi->c_offset[i];
 
544
                    for (k = 0; k < dot_size; ++k) {
 
545
                        if (d->vfi->color_model == PJMEDIA_COLOR_MODEL_RGB)
 
546
                            *ptr = dot_clr_rgb[i];
 
547
                        else
 
548
                            *ptr = dot_clr_yuv[i];
 
549
                        ptr += inc_ptr;
 
550
                    }
 
551
                }
 
552
            }
 
553
        } else {
 
554
            pj_size_t offset_p = 0;
 
555
 
 
556
            for (i = 0; i < 3; ++i) {
 
557
                pj_uint8_t *ptr, c;
 
558
                unsigned j;
 
559
                pj_size_t dot_size = DOT_SIZE;
 
560
 
 
561
                if (d->vfi->color_model == PJMEDIA_COLOR_MODEL_RGB)
 
562
                    c = dot_clr_rgb[i];
 
563
                else
 
564
                    c = dot_clr_yuv[i];
 
565
 
 
566
                dot_size /= (d->vafp.size.w / d->vafp.strides[i]);
 
567
                ptr = p + offset_p + d->vafp.strides[i]*(dot_size+1) - 
 
568
                      2*dot_size;
 
569
                for (j = 0; j < dot_size; ++j) {
 
570
                    pj_memset(ptr, c, dot_size);
 
571
                    ptr += d->vafp.strides[i];
 
572
                }
 
573
                offset_p += d->vafp.plane_bytes[i];
 
574
            }
 
575
        }
 
576
    }
 
577
 
 
578
    return PJ_SUCCESS;
 
579
}
 
580
 
 
581
/* API: Get frame from stream */
 
582
static pj_status_t cbar_stream_get_frame(pjmedia_vid_dev_stream *strm,
 
583
                                         pjmedia_frame *frame)
 
584
{
 
585
    struct cbar_stream *stream = (struct cbar_stream*)strm;
 
586
 
 
587
    frame->type = PJMEDIA_FRAME_TYPE_VIDEO;
 
588
    frame->bit_info = 0;
 
589
    frame->timestamp = stream->ts;
 
590
    stream->ts.u64 += stream->ts_inc;
 
591
    return spectrum_run(stream, frame->buf, frame->size);
 
592
}
 
593
 
 
594
/* API: Start stream. */
 
595
static pj_status_t cbar_stream_start(pjmedia_vid_dev_stream *strm)
 
596
{
 
597
    struct cbar_stream *stream = (struct cbar_stream*)strm;
 
598
 
 
599
    PJ_UNUSED_ARG(stream);
 
600
 
 
601
    PJ_LOG(4, (THIS_FILE, "Starting cbar video stream"));
 
602
 
 
603
    return PJ_SUCCESS;
 
604
}
 
605
 
 
606
/* API: Stop stream. */
 
607
static pj_status_t cbar_stream_stop(pjmedia_vid_dev_stream *strm)
 
608
{
 
609
    struct cbar_stream *stream = (struct cbar_stream*)strm;
 
610
 
 
611
    PJ_UNUSED_ARG(stream);
 
612
 
 
613
    PJ_LOG(4, (THIS_FILE, "Stopping cbar video stream"));
 
614
 
 
615
    return PJ_SUCCESS;
 
616
}
 
617
 
 
618
 
 
619
/* API: Destroy stream. */
 
620
static pj_status_t cbar_stream_destroy(pjmedia_vid_dev_stream *strm)
 
621
{
 
622
    struct cbar_stream *stream = (struct cbar_stream*)strm;
 
623
 
 
624
    PJ_ASSERT_RETURN(stream != NULL, PJ_EINVAL);
 
625
 
 
626
    cbar_stream_stop(strm);
 
627
 
 
628
    pj_pool_release(stream->pool);
 
629
 
 
630
    return PJ_SUCCESS;
 
631
}
 
632
 
 
633
#endif  /* PJMEDIA_VIDEO_DEV_HAS_CBAR_SRC */