| 1 |
/* |
| 2 |
* iaxclient: a cross-platform IAX softphone library |
| 3 |
* |
| 4 |
* Copyrights: |
| 5 |
* Copyright (C) 2005-2006, Horizon Wimba, inc. |
| 6 |
* Copyright (C) 2007, Wimba, Inc. |
| 7 |
* |
| 8 |
* Contributors: |
| 9 |
* Steve Kann <[email protected]> |
| 10 |
* Mihai Balea <mihai AT hates DOT ms> |
| 11 |
* Peter Grayson <[email protected]> |
| 12 |
* Erik Bunce <[email protected]> |
| 13 |
* |
| 14 |
* This program is free software, distributed under the terms of |
| 15 |
* the GNU Lesser (Library) General Public License. |
| 16 |
*/ |
| 17 |
|
| 18 |
#include <assert.h> |
| 19 |
#include <stdlib.h> |
| 20 |
|
| 21 |
#include <vidcap/vidcap.h> |
| 22 |
#include <vidcap/converters.h> |
| 23 |
|
| 24 |
#include "video.h" |
| 25 |
#include "slice.h" |
| 26 |
#include "iaxclient_lib.h" |
| 27 |
#include "iax-client.h" |
| 28 |
#ifdef USE_FFMPEG |
| 29 |
#include "codec_ffmpeg.h" |
| 30 |
#endif |
| 31 |
#ifdef USE_THEORA |
| 32 |
#include "codec_theora.h" |
| 33 |
#endif |
| 34 |
|
| 35 |
#if defined(WIN32) |
| 36 |
#define strdup _strdup |
| 37 |
#endif |
| 38 |
|
| 39 |
struct video_info |
| 40 |
{ |
| 41 |
vidcap_state * vc; |
| 42 |
vidcap_sapi * sapi; |
| 43 |
vidcap_src * src; |
| 44 |
struct vidcap_sapi_info sapi_info; |
| 45 |
struct vidcap_fmt_info fmt_info; |
| 46 |
|
| 47 |
/* these are the requested (post-scaling) dimensions */ |
| 48 |
int width; |
| 49 |
int height; |
| 50 |
|
| 51 |
MUTEX camera_lock; |
| 52 |
int capturing; |
| 53 |
|
| 54 |
char * converted_i420_buf; |
| 55 |
int converted_i420_buf_size; |
| 56 |
int (*convert_to_i420)(int, int, const char *, char *); |
| 57 |
|
| 58 |
char * converted_rgb_buf; |
| 59 |
int converted_rgb_buf_size; |
| 60 |
int (*convert_to_rgb32)(int, int, const char *, char *); |
| 61 |
|
| 62 |
char * scaled_buf; |
| 63 |
int scaled_buf_size; |
| 64 |
void (*scale_image)(const unsigned char *, int, int, |
| 65 |
unsigned char *, int, int); |
| 66 |
|
| 67 |
int prefs; |
| 68 |
|
| 69 |
struct slicer_context * sc; |
| 70 |
|
| 71 |
/* these two struct arrays are correlated by index */ |
| 72 |
struct vidcap_src_info * vc_src_info; |
| 73 |
struct iaxc_video_device * devices; |
| 74 |
MUTEX dev_list_lock; |
| 75 |
|
| 76 |
int device_count; |
| 77 |
int selected_device_id; |
| 78 |
int next_id; |
| 79 |
}; |
| 80 |
|
| 81 |
struct video_format_info |
| 82 |
{ |
| 83 |
int width; |
| 84 |
int height; |
| 85 |
int framerate; |
| 86 |
int bitrate; |
| 87 |
int fragsize; |
| 88 |
|
| 89 |
/* Note that here format really means codec (thoera, h264, etc) */ |
| 90 |
int format_preferred; |
| 91 |
int format_allowed; |
| 92 |
}; |
| 93 |
|
| 94 |
static struct video_info vinfo; |
| 95 |
|
| 96 |
/* TODO: This vfinfo instance is ... funny. The current semantic of |
| 97 |
* iaxc_video_format_set() requires it to be called _prior_ to |
| 98 |
* iaxc_initialize() which of course is where video initialize is called. |
| 99 |
* This means that no code in this video.c module is called prior to |
| 100 |
* iaxc_video_format_set(). This is silly, wrong, and bad. |
| 101 |
* |
| 102 |
* What would be better would be if iaxc_video_format_set() was called |
| 103 |
* by clients _after_ iaxc_initialize(). The TODO here is to do the |
| 104 |
* analysis and restructure things so that iaxc_video_format_set() and |
| 105 |
* probably several other iaxc_*() calls do not happen until after |
| 106 |
* iaxc_initialize(). |
| 107 |
* |
| 108 |
* Once that happens, these members of video_format_info can be rolled |
| 109 |
* back into video_info and we can initialize the members in |
| 110 |
* video_initialize(). |
| 111 |
*/ |
| 112 |
static struct video_format_info vfinfo = |
| 113 |
{ |
| 114 |
320, /* width */ |
| 115 |
240, /* height */ |
| 116 |
15, /* fps */ |
| 117 |
150000, /* bitrate */ |
| 118 |
1500, /* fragsize */ |
| 119 |
0, /* format preferred */ |
| 120 |
0, /* format allowed */ |
| 121 |
}; |
| 122 |
|
| 123 |
extern int selected_call; |
| 124 |
extern int test_mode; |
| 125 |
extern struct iaxc_call * calls; |
| 126 |
|
| 127 |
/* to prevent clearing a call while in capture callback */ |
| 128 |
extern __inline int try_iaxc_lock(); |
| 129 |
extern __inline void put_iaxc_lock(); |
| 130 |
|
| 131 |
EXPORT unsigned int iaxc_get_video_prefs(void) |
| 132 |
{ |
| 133 |
return vinfo.prefs; |
| 134 |
} |
| 135 |
|
| 136 |
static void reset_codec_stats(struct iaxc_video_codec *vcodec) |
| 137 |
{ |
| 138 |
if ( !vcodec ) |
| 139 |
return; |
| 140 |
|
| 141 |
memset(&vcodec->video_stats, 0, sizeof(struct iaxc_video_stats)); |
| 142 |
vcodec->video_stats.start_time = iax_tvnow(); |
| 143 |
} |
| 144 |
|
| 145 |
static void reset_video_stats(struct iaxc_call * call) |
| 146 |
{ |
| 147 |
if ( !call ) |
| 148 |
return; |
| 149 |
|
| 150 |
reset_codec_stats(call->vdecoder); |
| 151 |
reset_codec_stats(call->vencoder); |
| 152 |
} |
| 153 |
|
| 154 |
/* Collect and return video statistics. Also reset statistics if required. |
| 155 |
* Returns a pointer to the data. |
| 156 |
* Right now we use two different codecs for encoding and decoding. We need |
| 157 |
* to collate information from both and wrap it into one nice struct. |
| 158 |
*/ |
| 159 |
static int get_stats(struct iaxc_call *call, struct iaxc_video_stats *stats, |
| 160 |
int reset) |
| 161 |
{ |
| 162 |
if ( !call || !stats ) |
| 163 |
return -1; |
| 164 |
|
| 165 |
memset(stats, 0, sizeof(*stats)); |
| 166 |
|
| 167 |
if ( call->vencoder ) |
| 168 |
{ |
| 169 |
stats->sent_slices = call->vencoder->video_stats.sent_slices; |
| 170 |
stats->acc_sent_size = call->vencoder->video_stats.acc_sent_size; |
| 171 |
stats->outbound_frames = call->vencoder->video_stats.outbound_frames; |
| 172 |
stats->avg_outbound_bps = call->vencoder->video_stats.avg_outbound_bps; |
| 173 |
stats->avg_outbound_fps = call->vencoder->video_stats.avg_outbound_fps; |
| 174 |
} |
| 175 |
|
| 176 |
if ( call->vdecoder ) |
| 177 |
{ |
| 178 |
stats->received_slices = call->vdecoder->video_stats.received_slices; |
| 179 |
stats->acc_recv_size = call->vdecoder->video_stats.acc_recv_size; |
| 180 |
stats->inbound_frames = call->vdecoder->video_stats.inbound_frames; |
| 181 |
stats->dropped_frames = call->vdecoder->video_stats.dropped_frames; |
| 182 |
stats->avg_inbound_bps = call->vdecoder->video_stats.avg_inbound_bps; |
| 183 |
stats->avg_inbound_fps = call->vdecoder->video_stats.avg_inbound_fps; |
| 184 |
} |
| 185 |
|
| 186 |
if ( reset ) |
| 187 |
reset_video_stats(call); |
| 188 |
|
| 189 |
return 0; |
| 190 |
} |
| 191 |
|
| 192 |
static int maybe_send_stats(struct iaxc_call * call) |
| 193 |
{ |
| 194 |
const long video_stats_interval = 1000; /* milliseconds */ |
| 195 |
static struct timeval video_stats_start = {0, 0}; |
| 196 |
iaxc_event e; |
| 197 |
struct timeval now; |
| 198 |
|
| 199 |
if ( !call ) |
| 200 |
return -1; |
| 201 |
|
| 202 |
if ( video_stats_start.tv_sec == 0 && video_stats_start.tv_usec == 0 ) |
| 203 |
video_stats_start = iax_tvnow(); |
| 204 |
|
| 205 |
now = iax_tvnow(); |
| 206 |
|
| 207 |
if ( iaxci_msecdiff(&now, &video_stats_start) > video_stats_interval ) |
| 208 |
{ |
| 209 |
get_stats(call, &e.ev.videostats.stats, 1); |
| 210 |
e.type = IAXC_EVENT_VIDEOSTATS; |
| 211 |
e.ev.videostats.callNo = selected_call; |
| 212 |
iaxci_post_event(e); |
| 213 |
|
| 214 |
video_stats_start = now; |
| 215 |
} |
| 216 |
|
| 217 |
return 0; |
| 218 |
} |
| 219 |
|
| 220 |
/* TODO: The encode parameter to this function is unused within this |
| 221 |
* function. However, clients of this function still use this parameter. |
| 222 |
* What ends up happening is we instantiate the codec encoder/decoder |
| 223 |
* pairs multiple times. This seems not the best. For example, it is |
| 224 |
* not clear that all codecs are idempotent to the extent that they can |
| 225 |
* be initialized multiple times. Furthermore, within iaxclient itself, |
| 226 |
* we have a nasty habit of using global statically allocated data. |
| 227 |
* Multiple codec_video_*_new calls could easily result in race |
| 228 |
* conditions or more blatantly bad problems. |
| 229 |
* |
| 230 |
* Splitting encoder and decoder creation has some merit. |
| 231 |
* |
| 232 |
* - Avoid allocating encoder or decoder resources when not needed. |
| 233 |
* - Allows using different codecs for sending and receiving. |
| 234 |
* - Allows different codec parameters for sending and receiving. |
| 235 |
* |
| 236 |
*/ |
| 237 |
static struct iaxc_video_codec *create_codec(int format, int encode) |
| 238 |
{ |
| 239 |
struct iaxc_video_codec * vcodec = 0; |
| 240 |
|
| 241 |
iaxci_usermsg(IAXC_TEXT_TYPE_NOTICE, "Creating codec format 0x%x", |
| 242 |
format); |
| 243 |
|
| 244 |
switch ( format ) |
| 245 |
{ |
| 246 |
case IAXC_FORMAT_H261: |
| 247 |
case IAXC_FORMAT_H263: |
| 248 |
case IAXC_FORMAT_H263_PLUS: |
| 249 |
case IAXC_FORMAT_MPEG4: |
| 250 |
case IAXC_FORMAT_H264: |
| 251 |
#ifdef USE_FFMPEG |
| 252 |
vcodec = codec_video_ffmpeg_new(format, |
| 253 |
vfinfo.width, |
| 254 |
vfinfo.height, |
| 255 |
vfinfo.framerate, |
| 256 |
vfinfo.bitrate, |
| 257 |
vfinfo.fragsize); |
| 258 |
#endif |
| 259 |
break; |
| 260 |
|
| 261 |
case IAXC_FORMAT_THEORA: |
| 262 |
#ifdef USE_THEORA |
| 263 |
vcodec = codec_video_theora_new(format, |
| 264 |
vfinfo.width, |
| 265 |
vfinfo.height, |
| 266 |
vfinfo.framerate, |
| 267 |
vfinfo.bitrate, |
| 268 |
vfinfo.fragsize); |
| 269 |
#endif |
| 270 |
break; |
| 271 |
} |
| 272 |
|
| 273 |
reset_codec_stats(vcodec); |
| 274 |
|
| 275 |
return vcodec; |
| 276 |
} |
| 277 |
|
| 278 |
/* |
| 279 |
* Returns video data to the main application using the callback mechanism. |
| 280 |
* This function creates a dynamic copy of the video data. The memory is freed |
| 281 |
* in iaxci_post_event. This is because the event we post may be queued and the |
| 282 |
* frame data must live until after it is dequeued. |
| 283 |
|
| 284 |
\todo For encoded data, set the event format to the calls video format. |
| 285 |
For raw data, set the format to 0. |
| 286 |
|
| 287 |
\todo For encoded data, set the event format to the calls video format. For raw data, set the format to 0. |
| 288 |
*/ |
| 289 |
int show_video_frame(const char * in_buf, int in_buf_size, |
| 290 |
int call_number, int source, int encoded, |
| 291 |
unsigned int timestamp_ms) |
| 292 |
{ |
| 293 |
iaxc_event e; |
| 294 |
char * buf = malloc(in_buf_size); |
| 295 |
|
| 296 |
assert(buf); |
| 297 |
assert(source == IAXC_SOURCE_REMOTE || source == IAXC_SOURCE_LOCAL); |
| 298 |
|
| 299 |
memcpy(buf, in_buf, in_buf_size); |
| 300 |
|
| 301 |
e.type = IAXC_EVENT_VIDEO; |
| 302 |
e.ev.video.ts = timestamp_ms; |
| 303 |
e.ev.video.data = buf; |
| 304 |
e.ev.video.size = in_buf_size; |
| 305 |
e.ev.video.format = vfinfo.format_preferred; |
| 306 |
e.ev.video.width = vinfo.width; |
| 307 |
e.ev.video.height = vinfo.height; |
| 308 |
e.ev.video.callNo = call_number; |
| 309 |
e.ev.video.encoded = encoded; |
| 310 |
e.ev.video.source = source; |
| 311 |
|
| 312 |
iaxci_post_event(e); |
| 313 |
|
| 314 |
return 0; |
| 315 |
} |
| 316 |
|
| 317 |
// Resize the buffer to 25% (half resolution on both w and h ) |
| 318 |
// No checks are made to ensure that the source dimensions are even numbers |
| 319 |
static void quarter_rgb32(const unsigned char *src, int src_w, int src_h, |
| 320 |
unsigned char *dst) |
| 321 |
{ |
| 322 |
int i; |
| 323 |
const unsigned char * src_even = src; |
| 324 |
const unsigned char * src_odd = src + src_w * 4; |
| 325 |
|
| 326 |
for ( i = 0 ; i < src_h / 2 ; i++ ) |
| 327 |
{ |
| 328 |
int j; |
| 329 |
for ( j = 0 ; j < src_w / 2 ; j++ ) |
| 330 |
{ |
| 331 |
short r, g, b; |
| 332 |
b = *src_even++; |
| 333 |
g = *src_even++; |
| 334 |
r = *src_even++; |
| 335 |
++src_even; |
| 336 |
|
| 337 |
b += *src_even++; |
| 338 |
g += *src_even++; |
| 339 |
r += *src_even++; |
| 340 |
++src_even; |
| 341 |
|
| 342 |
b += *src_odd++; |
| 343 |
g += *src_odd++; |
| 344 |
r += *src_odd++; |
| 345 |
++src_odd; |
| 346 |
|
| 347 |
b += *src_odd++; |
| 348 |
g += *src_odd++; |
| 349 |
r += *src_odd++; |
| 350 |
++src_odd; |
| 351 |
|
| 352 |
*dst++ = (unsigned char)(b >> 2); |
| 353 |
*dst++ = (unsigned char)(g >> 2); |
| 354 |
*dst++ = (unsigned char)(r >> 2); |
| 355 |
*dst++ = (unsigned char)0xff; |
| 356 |
} |
| 357 |
src_even = src_odd; |
| 358 |
src_odd += src_w * 4; |
| 359 |
} |
| 360 |
} |
| 361 |
|
| 362 |
// Resize the buffer to 25% (half resolution on both w and h ) |
| 363 |
// No checks are made to ensure that the source dimensions are even numbers |
| 364 |
static void quarter_yuy2(const unsigned char *src, int src_w, int src_h, |
| 365 |
unsigned char *dst) |
| 366 |
{ |
| 367 |
int i; |
| 368 |
const unsigned char * src_even = src; |
| 369 |
const unsigned char * src_odd = src + src_w * 2; |
| 370 |
|
| 371 |
for ( i = 0 ; i < src_h / 2 ; i++ ) |
| 372 |
{ |
| 373 |
int j; |
| 374 |
for ( j = 0 ; j < src_w / 4 ; j++ ) |
| 375 |
{ |
| 376 |
short y1, u, y2, v; |
| 377 |
y1 = *src_even++; |
| 378 |
u = *src_even++; |
| 379 |
y1 += *src_even++; |
| 380 |
v = *src_even++; |
| 381 |
|
| 382 |
y1 += *src_odd++; |
| 383 |
u += *src_odd++; |
| 384 |
y1 += *src_odd++; |
| 385 |
v += *src_odd++; |
| 386 |
|
| 387 |
y2 = *src_even++; |
| 388 |
u += *src_even++; |
| 389 |
y2 += *src_even++; |
| 390 |
v += *src_even++; |
| 391 |
|
| 392 |
y2 += *src_odd++; |
| 393 |
u += *src_odd++; |
| 394 |
y2 += *src_odd++; |
| 395 |
v += *src_odd++; |
| 396 |
|
| 397 |
*dst++ = (unsigned char)(y1 >> 2); |
| 398 |
*dst++ = (unsigned char)(u >> 2); |
| 399 |
*dst++ = (unsigned char)(y2 >> 2); |
| 400 |
*dst++ = (unsigned char)(v >> 2); |
| 401 |
} |
| 402 |
src_even = src_odd; |
| 403 |
src_odd += src_w * 2; |
| 404 |
} |
| 405 |
} |
| 406 |
|
| 407 |
// Resize the channel to 25% (half resolution on both w and h ) |
| 408 |
// No checks are made to ensure that the source dimensions are even numbers |
| 409 |
static void quarter_channel(const unsigned char *src, int src_w, int src_h, |
| 410 |
unsigned char *dst) |
| 411 |
{ |
| 412 |
int i; |
| 413 |
const unsigned char * evenl = src; |
| 414 |
const unsigned char * oddl = src + src_w; |
| 415 |
|
| 416 |
for ( i = 0 ; i < src_h / 2 ; i++ ) |
| 417 |
{ |
| 418 |
int j; |
| 419 |
for ( j = 0 ; j < src_w / 2 ; j++ ) |
| 420 |
{ |
| 421 |
int s = *(evenl++); |
| 422 |
s += *(evenl++); |
| 423 |
s += *(oddl++); |
| 424 |
s += *(oddl++); |
| 425 |
*(dst++) = (unsigned char)(s >> 2); |
| 426 |
} |
| 427 |
evenl = oddl; |
| 428 |
oddl += src_w; |
| 429 |
} |
| 430 |
} |
| 431 |
|
| 432 |
// Resize an I420 image by resizing each of the three channels. |
| 433 |
// Destination buffer must be sufficiently large to accommodate |
| 434 |
// the resulting image |
| 435 |
static void resize_i420(const unsigned char *src, int src_w, int src_h, |
| 436 |
unsigned char *dst, int dst_w, int dst_h) |
| 437 |
{ |
| 438 |
const unsigned char *src_u = src + src_w * src_h; |
| 439 |
const unsigned char *src_v = src_u + src_w * src_h / 4; |
| 440 |
unsigned char *dst_u = dst + dst_w * dst_h; |
| 441 |
unsigned char *dst_v = dst_u + dst_w * dst_h / 4; |
| 442 |
|
| 443 |
// Resize each channel separately |
| 444 |
if ( dst_w * 2 == src_w && dst_h * 2 == src_h ) |
| 445 |
{ |
| 446 |
quarter_channel(src, src_w, src_h, dst); |
| 447 |
quarter_channel(src_u, src_w / 2, src_h / 2, dst_u); |
| 448 |
quarter_channel(src_v, src_w / 2, src_h / 2, dst_v); |
| 449 |
} |
| 450 |
/* |
| 451 |
else if ( dst_w * 4 == src_w && dst_h * 4 == src_h ) |
| 452 |
{ |
| 453 |
double_quarter_channel(src, src_w, src_h, dst); |
| 454 |
double_quarter_channel(src_u, src_w / 2, src_h / 2, dst_u); |
| 455 |
double_quarter_channel(src_v, src_w / 2, src_h / 2, dst_v); |
| 456 |
} |
| 457 |
else |
| 458 |
{ |
| 459 |
resize_channel(src, src_w, src_h, dst, dst_w, dst_h); |
| 460 |
resize_channel(src_u, src_w / 2, src_h / 2, |
| 461 |
dst_u, dst_w / 2, dst_h / 2); |
| 462 |
resize_channel(src_v, src_w / 2, src_h / 2, |
| 463 |
dst_v, dst_w / 2, dst_h / 2); |
| 464 |
} |
| 465 |
*/ |
| 466 |
} |
| 467 |
|
| 468 |
// Resize an rgb32 image |
| 469 |
// Destination buffer must be sufficiently large to accommodate |
| 470 |
// the resulting image |
| 471 |
static void resize_rgb32(const unsigned char *src, int src_w, int src_h, |
| 472 |
unsigned char *dst, int dst_w, int dst_h) |
| 473 |
{ |
| 474 |
if ( dst_w * 2 == src_w && dst_h * 2 == src_h ) |
| 475 |
{ |
| 476 |
quarter_rgb32(src, src_w, src_h, dst); |
| 477 |
} |
| 478 |
/* |
| 479 |
else if ( dst_w * 4 == src_w && dst_h * 4 == src_h ) |
| 480 |
{ |
| 481 |
double_quarter_rgb32(src, src_w, src_h, dst); |
| 482 |
} |
| 483 |
else |
| 484 |
{ |
| 485 |
resize_rgb32_buffer(src, src_w, src_h, dst, dst_w, dst_h); |
| 486 |
} |
| 487 |
*/ |
| 488 |
} |
| 489 |
|
| 490 |
// Resize a yuy2 image. |
| 491 |
// Destination buffer must be sufficiently large to accommodate |
| 492 |
// the resulting image |
| 493 |
static void resize_yuy2(const unsigned char *src, int src_w, int src_h, |
| 494 |
unsigned char *dst, int dst_w, int dst_h) |
| 495 |
{ |
| 496 |
if ( dst_w * 2 == src_w && dst_h * 2 == src_h ) |
| 497 |
{ |
| 498 |
quarter_yuy2(src, src_w, src_h, dst); |
| 499 |
} |
| 500 |
/* |
| 501 |
else if ( dst_w * 4 == src_w && dst_h * 4 == src_h ) |
| 502 |
{ |
| 503 |
double_quarter_yuy2(src, src_w, src_h, dst); |
| 504 |
} |
| 505 |
else |
| 506 |
{ |
| 507 |
resize_yuy2_buffer(src, src_w, src_h, dst, dst_w, dst_h); |
| 508 |
} |
| 509 |
*/ |
| 510 |
} |
| 511 |
|
| 512 |
static int video_device_notification_callback(vidcap_sapi *sapi, |
| 513 |
void * user_context) |
| 514 |
{ |
| 515 |
iaxc_event evt; |
| 516 |
|
| 517 |
if ( sapi != vinfo.sapi ) |
| 518 |
{ |
| 519 |
fprintf(stderr, "ERROR: wrong sapi in device notification\n"); |
| 520 |
return -1; |
| 521 |
} |
| 522 |
|
| 523 |
/* notify application that device list has been updated */ |
| 524 |
evt.type = IAXC_EVENT_VIDCAP_DEVICE; |
| 525 |
iaxci_post_event(evt); |
| 526 |
|
| 527 |
return 0; |
| 528 |
} |
| 529 |
|
| 530 |
static int capture_callback(vidcap_src * src, void * user_data, |
| 531 |
struct vidcap_capture_info * cap_info) |
| 532 |
{ |
| 533 |
static struct slice_set_t slice_set; |
| 534 |
|
| 535 |
/* The prefs may change inbetween capture callbacks, so we cannot |
| 536 |
* precompute any of this prior to starting capture. |
| 537 |
*/ |
| 538 |
const int send_encoded = !(vinfo.prefs & IAXC_VIDEO_PREF_SEND_DISABLE); |
| 539 |
const int recv_local_enc = vinfo.prefs & IAXC_VIDEO_PREF_RECV_LOCAL_ENCODED; |
| 540 |
const int recv_local_raw = vinfo.prefs & IAXC_VIDEO_PREF_RECV_LOCAL_RAW; |
| 541 |
const int need_encode = send_encoded || recv_local_enc; |
| 542 |
const int local_rgb = vinfo.prefs & IAXC_VIDEO_PREF_RECV_RGB32; |
| 543 |
|
| 544 |
struct iaxc_call * call; |
| 545 |
struct timeval now; |
| 546 |
long time_delta; |
| 547 |
|
| 548 |
const char * rgb_buf = 0; |
| 549 |
int rgb_buf_size = 0; |
| 550 |
const char * i420_buf = 0; |
| 551 |
int i420_buf_size = 0; |
| 552 |
const char * source_buf = 0; |
| 553 |
int source_buf_size = 0; |
| 554 |
|
| 555 |
iaxc_event evt; |
| 556 |
|
| 557 |
int i; |
| 558 |
|
| 559 |
if ( cap_info->error_status ) |
| 560 |
{ |
| 561 |
fprintf(stderr, "vidcap capture error %d\n", |
| 562 |
cap_info->error_status); |
| 563 |
vinfo.capturing = 0; |
| 564 |
|
| 565 |
/* NOTE: |
| 566 |
* We want to release now, but we're in the capture callback thread. |
| 567 |
* vidcap_src_release() fails during capture. |
| 568 |
* |
| 569 |
* We'll defer the release until the end-application's main thread |
| 570 |
* requires the release - if either iaxc_set_video_prefs(), |
| 571 |
* iaxc_video_device_set() or video_destroy() are called. |
| 572 |
*/ |
| 573 |
|
| 574 |
evt.type = IAXC_EVENT_VIDCAP_ERROR; |
| 575 |
iaxci_post_event(evt); |
| 576 |
return -1; |
| 577 |
} |
| 578 |
|
| 579 |
if ( cap_info->video_data_size < 1 ) |
| 580 |
{ |
| 581 |
fprintf(stderr, "FYI: callback with no data\n"); |
| 582 |
return 0; |
| 583 |
} |
| 584 |
|
| 585 |
if ( vinfo.prefs & IAXC_VIDEO_PREF_CAPTURE_DISABLE ) |
| 586 |
return 0; |
| 587 |
|
| 588 |
if ( vinfo.width != vinfo.fmt_info.width ) |
| 589 |
{ |
| 590 |
vinfo.scale_image((const unsigned char *)cap_info->video_data, |
| 591 |
vinfo.fmt_info.width, |
| 592 |
vinfo.fmt_info.height, |
| 593 |
(unsigned char *)vinfo.scaled_buf, |
| 594 |
vinfo.width, |
| 595 |
vinfo.height); |
| 596 |
|
| 597 |
source_buf = vinfo.scaled_buf; |
| 598 |
source_buf_size = vinfo.scaled_buf_size; |
| 599 |
} |
| 600 |
else |
| 601 |
{ |
| 602 |
source_buf = cap_info->video_data; |
| 603 |
source_buf_size = cap_info->video_data_size; |
| 604 |
} |
| 605 |
|
| 606 |
if ( vinfo.convert_to_rgb32 && recv_local_raw && local_rgb ) |
| 607 |
{ |
| 608 |
vinfo.convert_to_rgb32( |
| 609 |
vinfo.width, |
| 610 |
vinfo.height, |
| 611 |
source_buf, |
| 612 |
vinfo.converted_rgb_buf); |
| 613 |
|
| 614 |
rgb_buf = vinfo.converted_rgb_buf; |
| 615 |
rgb_buf_size = vinfo.converted_rgb_buf_size; |
| 616 |
} |
| 617 |
else |
| 618 |
{ |
| 619 |
rgb_buf = source_buf; |
| 620 |
rgb_buf_size = source_buf_size; |
| 621 |
} |
| 622 |
|
| 623 |
if ( vinfo.convert_to_i420 && (need_encode || |
| 624 |
(recv_local_raw && !local_rgb)) ) |
| 625 |
{ |
| 626 |
vinfo.convert_to_i420( |
| 627 |
vinfo.width, |
| 628 |
vinfo.height, |
| 629 |
source_buf, |
| 630 |
vinfo.converted_i420_buf); |
| 631 |
|
| 632 |
i420_buf = vinfo.converted_i420_buf; |
| 633 |
i420_buf_size = vinfo.converted_i420_buf_size; |
| 634 |
} |
| 635 |
else |
| 636 |
{ |
| 637 |
i420_buf = source_buf; |
| 638 |
i420_buf_size = source_buf_size; |
| 639 |
} |
| 640 |
|
| 641 |
if ( recv_local_raw ) |
| 642 |
{ |
| 643 |
show_video_frame(local_rgb ? rgb_buf : i420_buf, |
| 644 |
local_rgb ? rgb_buf_size : i420_buf_size, |
| 645 |
-1, /* local call number */ |
| 646 |
IAXC_SOURCE_LOCAL, |
| 647 |
0, /* not encoded */ |
| 648 |
0); /* timestamp (ms) */ |
| 649 |
} |
| 650 |
|
| 651 |
/* Don't block waiting for this lock. If the main thread has the lock |
| 652 |
* for the purpose of dumping the call, it may request that video |
| 653 |
* capture stop - which would block until this callback returned. |
| 654 |
*/ |
| 655 |
if ( try_iaxc_lock() ) |
| 656 |
{ |
| 657 |
/* give it a second try */ |
| 658 |
iaxc_millisleep(5); |
| 659 |
if ( try_iaxc_lock() ) |
| 660 |
{ |
| 661 |
fprintf(stderr, "skipping processing of a video frame\n"); |
| 662 |
return 0; |
| 663 |
} |
| 664 |
} |
| 665 |
|
| 666 |
if ( selected_call < 0 ) |
| 667 |
goto callback_done; |
| 668 |
|
| 669 |
call = &calls[selected_call]; |
| 670 |
|
| 671 |
if ( !call || !(call->state & (IAXC_CALL_STATE_COMPLETE | |
| 672 |
IAXC_CALL_STATE_OUTGOING)) ) |
| 673 |
{ |
| 674 |
goto callback_done; |
| 675 |
} |
| 676 |
|
| 677 |
if ( call->vformat == 0 ) |
| 678 |
{ |
| 679 |
goto callback_failed; |
| 680 |
} |
| 681 |
|
| 682 |
if ( !need_encode ) |
| 683 |
{ |
| 684 |
/* Since we are neither sending out encoded video on the |
| 685 |
* network nor to the application, we no longer need the |
| 686 |
* codec instance. |
| 687 |
*/ |
| 688 |
if ( call->vencoder ) |
| 689 |
{ |
| 690 |
fprintf(stderr, "destroying codec %s\n", |
| 691 |
call->vencoder->name); |
| 692 |
call->vencoder->destroy(call->vencoder); |
| 693 |
call->vencoder = 0; |
| 694 |
} |
| 695 |
|
| 696 |
goto callback_done; |
| 697 |
} |
| 698 |
else |
| 699 |
{ |
| 700 |
if ( call->vencoder && |
| 701 |
(call->vencoder->format != call->vformat || |
| 702 |
call->vencoder->params_changed) ) |
| 703 |
{ |
| 704 |
call->vencoder->destroy(call->vencoder); |
| 705 |
call->vencoder = 0; |
| 706 |
} |
| 707 |
|
| 708 |
if ( !call->vencoder ) |
| 709 |
{ |
| 710 |
if ( !(call->vencoder = create_codec(call->vformat, 1)) ) |
| 711 |
{ |
| 712 |
fprintf(stderr, "ERROR: failed to create codec " |
| 713 |
"for format 0x%08x\n", |
| 714 |
call->vformat); |
| 715 |
|
| 716 |
goto callback_failed; |
| 717 |
} |
| 718 |
|
| 719 |
fprintf(stderr, "created encoder codec %s\n", |
| 720 |
call->vencoder->name); |
| 721 |
} |
| 722 |
|
| 723 |
if ( call->vencoder->encode(call->vencoder, |
| 724 |
i420_buf_size, i420_buf, |
| 725 |
&slice_set) ) |
| 726 |
{ |
| 727 |
fprintf(stderr, "failed to encode captured video\n"); |
| 728 |
goto callback_failed; |
| 729 |
} |
| 730 |
} |
| 731 |
|
| 732 |
/* Gather statistics */ |
| 733 |
call->vencoder->video_stats.outbound_frames++; |
| 734 |
|
| 735 |
now = iax_tvnow(); |
| 736 |
time_delta = |
| 737 |
iaxci_msecdiff(&now, &call->vencoder->video_stats.start_time); |
| 738 |
|
| 739 |
if ( time_delta > 0 ) |
| 740 |
call->vencoder->video_stats.avg_outbound_fps = |
| 741 |
(float)call->vencoder->video_stats.outbound_frames * |
| 742 |
1000.0f / (float)time_delta; |
| 743 |
|
| 744 |
if ( !call->session ) |
| 745 |
{ |
| 746 |
fprintf(stderr, "not sending video to sessionless call\n"); |
| 747 |
goto callback_failed; |
| 748 |
} |
| 749 |
|
| 750 |
for ( i = 0; i < slice_set.num_slices; ++i ) |
| 751 |
{ |
| 752 |
//Pass the encoded frame to the main app |
| 753 |
// \todo Fix the call number |
| 754 |
if ( vinfo.prefs & IAXC_VIDEO_PREF_RECV_LOCAL_ENCODED ) |
| 755 |
{ |
| 756 |
show_video_frame(slice_set.data[i], |
| 757 |
slice_set.size[i], |
| 758 |
-1, /* local call number */ |
| 759 |
IAXC_SOURCE_LOCAL, |
| 760 |
1, /* encoded */ |
| 761 |
0); /* timestamp */ |
| 762 |
} |
| 763 |
|
| 764 |
if ( !(vinfo.prefs & IAXC_VIDEO_PREF_SEND_DISABLE) ) |
| 765 |
{ |
| 766 |
if ( iax_send_video_trunk(call->session, call->vformat, |
| 767 |
slice_set.data[i], |
| 768 |
slice_set.size[i], |
| 769 |
0, /* not fullframe */ |
| 770 |
i) == -1) |
| 771 |
{ |
| 772 |
fprintf(stderr, "failed sending slice call %d " |
| 773 |
"size %d\n", selected_call, |
| 774 |
slice_set.size[i]); |
| 775 |
goto callback_failed; |
| 776 |
} |
| 777 |
|
| 778 |
/* More statistics */ |
| 779 |
call->vencoder->video_stats.sent_slices++; |
| 780 |
call->vencoder->video_stats.acc_sent_size += |
| 781 |
slice_set.size[i]; |
| 782 |
if ( time_delta > 0 ) |
| 783 |
call->vencoder->video_stats.avg_outbound_bps = |
| 784 |
call->vencoder->video_stats.acc_sent_size * |
| 785 |
8000 / time_delta; |
| 786 |
} |
| 787 |
} |
| 788 |
|
| 789 |
maybe_send_stats(call); |
| 790 |
|
| 791 |
callback_done: |
| 792 |
put_iaxc_lock(); |
| 793 |
return 0; |
| 794 |
|
| 795 |
callback_failed: |
| 796 |
put_iaxc_lock(); |
| 797 |
return -1; |
| 798 |
} |
| 799 |
|
| 800 |
static int prepare_for_capture() |
| 801 |
{ |
| 802 |
static const int fourcc_list[] = |
| 803 |
{ |
| 804 |
VIDCAP_FOURCC_I420, |
| 805 |
VIDCAP_FOURCC_YUY2, |
| 806 |
VIDCAP_FOURCC_RGB32 |
| 807 |
}; |
| 808 |
|
| 809 |
static const int fourcc_list_len = sizeof(fourcc_list) / sizeof(int); |
| 810 |
static const int max_factor = 2; |
| 811 |
int scale_factor; |
| 812 |
int i; |
| 813 |
|
| 814 |
vinfo.width = vfinfo.width; |
| 815 |
vinfo.height = vfinfo.height; |
| 816 |
vinfo.fmt_info.fps_numerator = vfinfo.framerate; |
| 817 |
vinfo.fmt_info.fps_denominator = 1; |
| 818 |
|
| 819 |
for ( scale_factor = 1; scale_factor <= max_factor; scale_factor *= 2 ) |
| 820 |
{ |
| 821 |
vinfo.fmt_info.width = vfinfo.width * scale_factor; |
| 822 |
vinfo.fmt_info.height = vfinfo.height * scale_factor; |
| 823 |
|
| 824 |
for ( i = 0; i < fourcc_list_len; ++i ) |
| 825 |
{ |
| 826 |
vinfo.fmt_info.fourcc = fourcc_list[i]; |
| 827 |
|
| 828 |
if ( !vidcap_format_bind(vinfo.src, &vinfo.fmt_info) ) |
| 829 |
break; |
| 830 |
} |
| 831 |
|
| 832 |
if ( i != fourcc_list_len ) |
| 833 |
break; |
| 834 |
} |
| 835 |
|
| 836 |
if ( i == fourcc_list_len ) |
| 837 |
{ |
| 838 |
fprintf(stderr, "failed to bind format %dx%d %f fps\n", |
| 839 |
vinfo.fmt_info.width, |
| 840 |
vinfo.fmt_info.height, |
| 841 |
(float)vinfo.fmt_info.fps_numerator / |
| 842 |
(float)vinfo.fmt_info.fps_denominator); |
| 843 |
return -1; |
| 844 |
} |
| 845 |
|
| 846 |
/* Prepare various conversion buffers */ |
| 847 |
|
| 848 |
if ( vinfo.converted_i420_buf ) |
| 849 |
{ |
| 850 |
free(vinfo.converted_i420_buf); |
| 851 |
vinfo.converted_i420_buf = 0; |
| 852 |
} |
| 853 |
|
| 854 |
if ( vinfo.converted_rgb_buf ) |
| 855 |
{ |
| 856 |
free(vinfo.converted_rgb_buf); |
| 857 |
vinfo.converted_rgb_buf = 0; |
| 858 |
} |
| 859 |
|
| 860 |
vinfo.converted_i420_buf_size = |
| 861 |
vinfo.width * vinfo.height * 3 / 2; |
| 862 |
|
| 863 |
vinfo.converted_rgb_buf_size = |
| 864 |
vinfo.width * vinfo.height * 4; |
| 865 |
|
| 866 |
if ( vinfo.fmt_info.fourcc != VIDCAP_FOURCC_RGB32 ) |
| 867 |
vinfo.converted_rgb_buf = malloc(vinfo.converted_rgb_buf_size); |
| 868 |
|
| 869 |
if ( vinfo.fmt_info.fourcc != VIDCAP_FOURCC_I420 ) |
| 870 |
vinfo.converted_i420_buf = malloc(vinfo.converted_i420_buf_size); |
| 871 |
|
| 872 |
switch ( vinfo.fmt_info.fourcc ) |
| 873 |
{ |
| 874 |
case VIDCAP_FOURCC_RGB32: |
| 875 |
vinfo.convert_to_i420 = vidcap_rgb32_to_i420; |
| 876 |
vinfo.convert_to_rgb32 = 0; |
| 877 |
vinfo.scale_image = 0; |
| 878 |
vinfo.scaled_buf = 0; |
| 879 |
if ( vinfo.width != vinfo.fmt_info.width ) |
| 880 |
{ |
| 881 |
vinfo.scale_image = resize_rgb32; |
| 882 |
vinfo.scaled_buf_size = vinfo.converted_rgb_buf_size; |
| 883 |
vinfo.scaled_buf = malloc(vinfo.scaled_buf_size); |
| 884 |
fprintf(stderr, "scaling rgb32 images by 1/%d\n", |
| 885 |
scale_factor); |
| 886 |
} |
| 887 |
if ( !vinfo.converted_i420_buf ) |
| 888 |
return -1; |
| 889 |
break; |
| 890 |
case VIDCAP_FOURCC_I420: |
| 891 |
vinfo.convert_to_i420 = 0; |
| 892 |
vinfo.convert_to_rgb32 = vidcap_i420_to_rgb32; |
| 893 |
vinfo.scale_image = 0; |
| 894 |
vinfo.scaled_buf = 0; |
| 895 |
if ( vinfo.width != vinfo.fmt_info.width ) |
| 896 |
{ |
| 897 |
vinfo.scale_image = resize_i420; |
| 898 |
vinfo.scaled_buf_size = vinfo.converted_i420_buf_size; |
| 899 |
vinfo.scaled_buf = malloc(vinfo.scaled_buf_size); |
| 900 |
fprintf(stderr, "scaling i420 images by 1/%d\n", |
| 901 |
scale_factor); |
| 902 |
} |
| 903 |
if ( !vinfo.converted_rgb_buf ) |
| 904 |
return -1; |
| 905 |
break; |
| 906 |
case VIDCAP_FOURCC_YUY2: |
| 907 |
vinfo.convert_to_i420 = vidcap_yuy2_to_i420; |
| 908 |
vinfo.convert_to_rgb32 = vidcap_yuy2_to_rgb32; |
| 909 |
vinfo.scale_image = 0; |
| 910 |
vinfo.scaled_buf = 0; |
| 911 |
if ( vinfo.width != vinfo.fmt_info.width ) |
| 912 |
{ |
| 913 |
vinfo.scale_image = resize_yuy2; |
| 914 |
vinfo.scaled_buf_size = vinfo.width * |
| 915 |
vinfo.height * 2; |
| 916 |
vinfo.scaled_buf = malloc(vinfo.scaled_buf_size); |
| 917 |
fprintf(stderr, "scaling yuy2 images by 1/%d\n", |
| 918 |
scale_factor); |
| 919 |
} |
| 920 |
if ( !vinfo.converted_rgb_buf || !vinfo.converted_i420_buf ) |
| 921 |
return -1; |
| 922 |
break; |
| 923 |
default: |
| 924 |
fprintf(stderr, "do not know how to deal with vidcap " |
| 925 |
"fourcc '%s'\n", |
| 926 |
vidcap_fourcc_string_get(vinfo.fmt_info.fourcc)); |
| 927 |
return -1; |
| 928 |
} |
| 929 |
|
| 930 |
if ( vinfo.scale_image && !vinfo.scaled_buf ) |
| 931 |
return -1; |
| 932 |
|
| 933 |
return 0; |
| 934 |
} |
| 935 |
|
| 936 |
static int ensure_acquired(int dev_id) |
| 937 |
{ |
| 938 |
int dev_num; |
| 939 |
|
| 940 |
if ( !vinfo.src ) |
| 941 |
{ |
| 942 |
MUTEXLOCK(&vinfo.dev_list_lock); |
| 943 |
|
| 944 |
for ( dev_num = 0; dev_num < vinfo.device_count; dev_num++ ) |
| 945 |
{ |
| 946 |
if ( vinfo.devices[dev_num].id == dev_id ) |
| 947 |
break; |
| 948 |
} |
| 949 |
|
| 950 |
if ( dev_num == vinfo.device_count ) |
| 951 |
{ |
| 952 |
MUTEXUNLOCK(&vinfo.dev_list_lock); |
| 953 |
fprintf(stderr, "invalid vidcap dev id: %d\n", dev_id); |
| 954 |
return -1; |
| 955 |
} |
| 956 |
|
| 957 |
if ( !(vinfo.src = vidcap_src_acquire(vinfo.sapi, |
| 958 |
&vinfo.vc_src_info[dev_num])) ) |
| 959 |
{ |
| 960 |
vinfo.src = 0; |
| 961 |
MUTEXUNLOCK(&vinfo.dev_list_lock); |
| 962 |
fprintf(stderr, "failed to acquire video source\n"); |
| 963 |
return -1; |
| 964 |
} |
| 965 |
|
| 966 |
fprintf(stderr, "acquired vidcap source %s (%s)\n", |
| 967 |
vinfo.vc_src_info[dev_num].description, |
| 968 |
vinfo.vc_src_info[dev_num].identifier); |
| 969 |
|
| 970 |
MUTEXUNLOCK(&vinfo.dev_list_lock); |
| 971 |
} |
| 972 |
|
| 973 |
return 0; |
| 974 |
} |
| 975 |
|
| 976 |
EXPORT int iaxc_set_video_prefs(unsigned int prefs) |
| 977 |
{ |
| 978 |
const unsigned int prefs_mask = |
| 979 |
IAXC_VIDEO_PREF_RECV_LOCAL_RAW | |
| 980 |
IAXC_VIDEO_PREF_RECV_LOCAL_ENCODED | |
| 981 |
IAXC_VIDEO_PREF_RECV_REMOTE_RAW | |
| 982 |
IAXC_VIDEO_PREF_RECV_REMOTE_ENCODED | |
| 983 |
IAXC_VIDEO_PREF_SEND_DISABLE | |
| 984 |
IAXC_VIDEO_PREF_RECV_RGB32 | |
| 985 |
IAXC_VIDEO_PREF_CAPTURE_DISABLE; |
| 986 |
int ret; |
| 987 |
|
| 988 |
if ( prefs & ~prefs_mask ) |
| 989 |
{ |
| 990 |
fprintf(stderr, "ERROR: unexpected video preference: 0x%0x\n", |
| 991 |
prefs); |
| 992 |
return -1; |
| 993 |
} |
| 994 |
|
| 995 |
vinfo.prefs = prefs; |
| 996 |
|
| 997 |
if ( test_mode ) |
| 998 |
return 0; |
| 999 |
|
| 1000 |
/* Not sending any video and not needing any form of |
| 1001 |
* local video implies that we do not need to capture |
| 1002 |
* video. |
| 1003 |
*/ |
| 1004 |
if ( prefs & IAXC_VIDEO_PREF_CAPTURE_DISABLE || |
| 1005 |
((prefs & IAXC_VIDEO_PREF_SEND_DISABLE) && |
| 1006 |
!(prefs & IAXC_VIDEO_PREF_RECV_LOCAL_RAW) && |
| 1007 |
!(prefs & IAXC_VIDEO_PREF_RECV_LOCAL_ENCODED)) ) |
| 1008 |
{ |
| 1009 |
MUTEXLOCK(&vinfo.camera_lock); |
| 1010 |
if ( vinfo.capturing && vinfo.src && |
| 1011 |
vidcap_src_capture_stop(vinfo.src) ) |
| 1012 |
{ |
| 1013 |
fprintf(stderr, "failed vidcap_src_capture_stop\n"); |
| 1014 |
} |
| 1015 |
|
| 1016 |
if ( vinfo.src && vidcap_src_release(vinfo.src) ) |
| 1017 |
{ |
| 1018 |
fprintf(stderr, "failed to release a video source\n"); |
| 1019 |
} |
| 1020 |
|
| 1021 |
vinfo.src = 0; |
| 1022 |
vinfo.capturing = 0; |
| 1023 |
MUTEXUNLOCK(&vinfo.camera_lock); |
| 1024 |
} |
| 1025 |
else |
| 1026 |
{ |
| 1027 |
MUTEXLOCK(&vinfo.camera_lock); |
| 1028 |
if ( !vinfo.capturing ) |
| 1029 |
{ |
| 1030 |
if ( vinfo.selected_device_id < 0 ) |
| 1031 |
{ |
| 1032 |
MUTEXUNLOCK(&vinfo.camera_lock); |
| 1033 |
return -1; |
| 1034 |
} |
| 1035 |
|
| 1036 |
if ( ensure_acquired(vinfo.selected_device_id) ) |
| 1037 |
{ |
| 1038 |
MUTEXUNLOCK(&vinfo.camera_lock); |
| 1039 |
return -1; |
| 1040 |
} |
| 1041 |
|
| 1042 |
if ( prepare_for_capture() ) |
| 1043 |
{ |
| 1044 |
MUTEXUNLOCK(&vinfo.camera_lock); |
| 1045 |
return -1; |
| 1046 |
} |
| 1047 |
|
| 1048 |
if ( (ret = vidcap_src_capture_start(vinfo.src, |
| 1049 |
capture_callback, 0)) ) |
| 1050 |
{ |
| 1051 |
MUTEXUNLOCK(&vinfo.camera_lock); |
| 1052 |
fprintf(stderr, "failed to start video capture: %d\n", |
| 1053 |
ret); |
| 1054 |
return -1; |
| 1055 |
} |
| 1056 |
|
| 1057 |
vinfo.capturing = 1; |
| 1058 |
} |
| 1059 |
MUTEXUNLOCK(&vinfo.camera_lock); |
| 1060 |
} |
| 1061 |
|
| 1062 |
return 0; |
| 1063 |
} |
| 1064 |
|
| 1065 |
EXPORT void iaxc_video_format_set_cap(int preferred, int allowed) |
| 1066 |
{ |
| 1067 |
vfinfo.format_preferred = preferred; |
| 1068 |
vfinfo.format_allowed = allowed; |
| 1069 |
} |
| 1070 |
|
| 1071 |
EXPORT void iaxc_video_format_get_cap(int *preferred, int *allowed) |
| 1072 |
{ |
| 1073 |
*preferred = vfinfo.format_preferred; |
| 1074 |
*allowed = vfinfo.format_allowed; |
| 1075 |
} |
| 1076 |
|
| 1077 |
EXPORT void iaxc_video_format_set(int preferred, int allowed, int framerate, |
| 1078 |
int bitrate, int width, int height, int fs) |
| 1079 |
{ |
| 1080 |
int real_pref = 0; |
| 1081 |
int real_allowed = 0; |
| 1082 |
#ifdef USE_FFMPEG |
| 1083 |
int tmp_allowed; |
| 1084 |
int i; |
| 1085 |
#endif |
| 1086 |
|
| 1087 |
// Make sure resolution is in range |
| 1088 |
if ( width < IAXC_VIDEO_MIN_WIDTH ) |
| 1089 |
width = IAXC_VIDEO_MIN_WIDTH; |
| 1090 |
else if ( width > IAXC_VIDEO_MAX_WIDTH ) |
| 1091 |
width = IAXC_VIDEO_MAX_WIDTH; |
| 1092 |
|
| 1093 |
if ( height < IAXC_VIDEO_MIN_HEIGHT ) |
| 1094 |
height = IAXC_VIDEO_MIN_HEIGHT; |
| 1095 |
else if ( height > IAXC_VIDEO_MAX_HEIGHT ) |
| 1096 |
height = IAXC_VIDEO_MAX_HEIGHT; |
| 1097 |
|
| 1098 |
vfinfo.framerate = framerate; |
| 1099 |
vfinfo.bitrate = bitrate; |
| 1100 |
vfinfo.width = width; |
| 1101 |
vfinfo.height = height; |
| 1102 |
vfinfo.fragsize = fs; |
| 1103 |
|
| 1104 |
vfinfo.format_allowed = 0; |
| 1105 |
vfinfo.format_preferred = 0; |
| 1106 |
|
| 1107 |
if ( preferred && (preferred & ~IAXC_VIDEO_FORMAT_MASK) ) |
| 1108 |
{ |
| 1109 |
fprintf(stderr, "ERROR: Preferred video format invalid.\n"); |
| 1110 |
preferred = 0; |
| 1111 |
} |
| 1112 |
|
| 1113 |
/* This check: |
| 1114 |
* 1. Check if preferred is a supported and compiled in codec. If |
| 1115 |
* not, switch to the default preferred format. |
| 1116 |
* 2. Check if allowed contains a list of all supported and compiled |
| 1117 |
* in codec. If there are some unavailable codec, remove it from |
| 1118 |
* this list. |
| 1119 |
*/ |
| 1120 |
|
| 1121 |
if ( preferred & IAXC_FORMAT_THEORA ) |
| 1122 |
real_pref = IAXC_FORMAT_THEORA; |
| 1123 |
|
| 1124 |
#ifdef USE_FFMPEG |
| 1125 |
if ( codec_video_ffmpeg_check_codec(preferred) ) |
| 1126 |
real_pref = preferred; |
| 1127 |
#endif |
| 1128 |
|
| 1129 |
if ( !real_pref ) |
| 1130 |
{ |
| 1131 |
/* If preferred codec is not available switch to the |
| 1132 |
* supported default codec. |
| 1133 |
*/ |
| 1134 |
fprintf(stderr, "Preferred codec (0x%08x) is not available. " |
| 1135 |
"Switching to default\n", preferred); |
| 1136 |
real_pref = IAXC_FORMAT_THEORA; |
| 1137 |
} |
| 1138 |
|
| 1139 |
/* Check on allowed codecs availability */ |
| 1140 |
|
| 1141 |
if ( allowed & IAXC_FORMAT_THEORA ) |
| 1142 |
real_allowed |= IAXC_FORMAT_THEORA; |
| 1143 |
|
| 1144 |
#ifdef USE_FFMPEG |
| 1145 |
/* TODO: This codec_video_ffmpeg_check_codec stuff is bogus. We |
| 1146 |
* need a standard interface in our codec wrappers that allows us to |
| 1147 |
* (1) test if a selected codec is valid and/or (2) return the set of |
| 1148 |
* available valid codecs. With that, we should be able to come up |
| 1149 |
* with a more elegant algorithm here for determining the video codec. |
| 1150 |
*/ |
| 1151 |
for ( i = 0; i <= 24; i++) |
| 1152 |
{ |
| 1153 |
tmp_allowed = 1 << i; |
| 1154 |
if ( (allowed & tmp_allowed) && |
| 1155 |
codec_video_ffmpeg_check_codec(tmp_allowed) ) |
| 1156 |
real_allowed |= tmp_allowed; |
| 1157 |
} |
| 1158 |
#endif |
| 1159 |
|
| 1160 |
if ( !real_pref ) |
| 1161 |
{ |
| 1162 |
fprintf(stderr, "Audio-only client!\n"); |
| 1163 |
} else |
| 1164 |
{ |
| 1165 |
vfinfo.format_preferred = real_pref; |
| 1166 |
|
| 1167 |
/* |
| 1168 |
* When a client use a 'preferred' format, it can force to |
| 1169 |
* use allowed formats using a non-zero value for 'allowed' |
| 1170 |
* parameter. If it is left 0, the client will use all |
| 1171 |
* capabilities set by default in this code. |
| 1172 |
*/ |
| 1173 |
if ( real_allowed ) |
| 1174 |
{ |
| 1175 |
vfinfo.format_allowed = real_allowed; |
| 1176 |
} |
| 1177 |
else |
| 1178 |
{ |
| 1179 |
#ifdef USE_FFMPEG |
| 1180 |
vfinfo.format_allowed |= IAXC_FORMAT_H263_PLUS |
| 1181 |
| IAXC_FORMAT_H263 |
| 1182 |
| IAXC_FORMAT_MPEG4 |
| 1183 |
| IAXC_FORMAT_H264; |
| 1184 |
#endif |
| 1185 |
vfinfo.format_allowed |= IAXC_FORMAT_THEORA; |
| 1186 |
} |
| 1187 |
} |
| 1188 |
} |
| 1189 |
|
| 1190 |
void iaxc_video_params_change(int framerate, int bitrate, int width, |
| 1191 |
int height, int fs) |
| 1192 |
{ |
| 1193 |
struct iaxc_call *call; |
| 1194 |
|
| 1195 |
/* set default video params */ |
| 1196 |
if ( framerate > 0 ) |
| 1197 |
vfinfo.framerate = framerate; |
| 1198 |
if ( bitrate > 0 ) |
| 1199 |
vfinfo.bitrate = bitrate; |
| 1200 |
if ( width > 0 ) |
| 1201 |
vfinfo.width = width; |
| 1202 |
if ( height > 0 ) |
| 1203 |
vfinfo.height = height; |
| 1204 |
if ( fs > 0 ) |
| 1205 |
vfinfo.fragsize = fs; |
| 1206 |
|
| 1207 |
if ( selected_call < 0 ) |
| 1208 |
return; |
| 1209 |
|
| 1210 |
call = &calls[selected_call]; |
| 1211 |
|
| 1212 |
if ( !call || !call->vencoder ) |
| 1213 |
return; |
| 1214 |
|
| 1215 |
call->vencoder->params_changed = 1; |
| 1216 |
|
| 1217 |
if ( framerate > 0 ) |
| 1218 |
call->vencoder->framerate = framerate; |
| 1219 |
if ( bitrate > 0 ) |
| 1220 |
call->vencoder->bitrate = bitrate; |
| 1221 |
if ( width > 0 ) |
| 1222 |
call->vencoder->width = width; |
| 1223 |
if ( height > 0 ) |
| 1224 |
call->vencoder->height = height; |
| 1225 |
if ( fs > 0 ) |
| 1226 |
call->vencoder->fragsize = fs; |
| 1227 |
} |
| 1228 |
|
| 1229 |
/* process an incoming video frame */ |
| 1230 |
int video_recv_video(struct iaxc_call *call, int sel_call, |
| 1231 |
void *encoded_video, int encoded_video_len, |
| 1232 |
unsigned int ts, int format) |
| 1233 |
{ |
| 1234 |
enum |
| 1235 |
{ |
| 1236 |
max_pixels = IAXC_VIDEO_MAX_WIDTH * IAXC_VIDEO_MAX_HEIGHT, |
| 1237 |
max_yuv_buf_size = max_pixels * 3 / 2, |
| 1238 |
max_rgb_buf_size = max_pixels * 4 |
| 1239 |
}; |
| 1240 |
|
| 1241 |
static char yuv_buf[max_yuv_buf_size]; |
| 1242 |
static char rgb_buf[max_rgb_buf_size]; |
| 1243 |
|
| 1244 |
const int pixels = vfinfo.width * vfinfo.height; |
| 1245 |
const int yuv_size = pixels * 3 / 2; |
| 1246 |
const int rgb_size = pixels * 4; |
| 1247 |
|
| 1248 |
int out_size = max_yuv_buf_size; |
| 1249 |
int ret; |
| 1250 |
struct timeval now; |
| 1251 |
long time; |
| 1252 |
|
| 1253 |
if ( !call ) |
| 1254 |
return 0; |
| 1255 |
|
| 1256 |
if ( format == 0 ) |
| 1257 |
{ |
| 1258 |
fprintf(stderr, "video_recv_video: Format is zero (should't happen)!\n"); |
| 1259 |
return -1; |
| 1260 |
} |
| 1261 |
|
| 1262 |
// Send the encoded frame to the main app if necessary |
| 1263 |
if ( vinfo.prefs & IAXC_VIDEO_PREF_RECV_REMOTE_ENCODED ) |
| 1264 |
{ |
| 1265 |
show_video_frame((char *)encoded_video, |
| 1266 |
encoded_video_len, |
| 1267 |
-1, /* TODO: why is the callnumber -1? */ |
| 1268 |
IAXC_SOURCE_REMOTE, |
| 1269 |
1, /* encoded */ |
| 1270 |
ts); |
| 1271 |
} |
| 1272 |
|
| 1273 |
/* destroy vdecoder if it is incorrect type */ |
| 1274 |
if ( call->vdecoder && call->vdecoder->format != format ) |
| 1275 |
{ |
| 1276 |
call->vdecoder->destroy(call->vdecoder); |
| 1277 |
call->vdecoder = NULL; |
| 1278 |
} |
| 1279 |
|
| 1280 |
/* If the user does not need decoded video, then do not decode. */ |
| 1281 |
if ( !(vinfo.prefs & IAXC_VIDEO_PREF_RECV_REMOTE_RAW) ) |
| 1282 |
return 0; |
| 1283 |
|
| 1284 |
/* create decoder if necessary */ |
| 1285 |
if ( !call->vdecoder ) |
| 1286 |
{ |
| 1287 |
call->vdecoder = create_codec(format, 0); |
| 1288 |
if (call->vdecoder != NULL) |
| 1289 |
fprintf(stderr,"**** Created decoder codec %s\n",call->vdecoder->name); |
| 1290 |
} |
| 1291 |
|
| 1292 |
if ( !call->vdecoder ) |
| 1293 |
{ |
| 1294 |
fprintf(stderr, "ERROR: Video codec could not be created: %d\n", |
| 1295 |
format); |
| 1296 |
return -1; |
| 1297 |
} |
| 1298 |
|
| 1299 |
/* Statistics */ |
| 1300 |
now = iax_tvnow(); |
| 1301 |
time = iaxci_msecdiff(&now, &call->vdecoder->video_stats.start_time); |
| 1302 |
call->vdecoder->video_stats.received_slices++; |
| 1303 |
call->vdecoder->video_stats.acc_recv_size += encoded_video_len; |
| 1304 |
if ( time > 0 ) |
| 1305 |
call->vdecoder->video_stats.avg_inbound_bps = |
| 1306 |
call->vdecoder->video_stats.acc_recv_size * 8000 / time; |
| 1307 |
|
| 1308 |
ret = call->vdecoder->decode(call->vdecoder, encoded_video_len, |
| 1309 |
(char *)encoded_video, &out_size, yuv_buf); |
| 1310 |
|
| 1311 |
if ( ret < 0 ) |
| 1312 |
{ |
| 1313 |
fprintf(stderr, "ERROR: decode error\n"); |
| 1314 |
return -1; |
| 1315 |
} |
| 1316 |
else if ( ret > 0 ) |
| 1317 |
{ |
| 1318 |
/* This indicates that a complete frame cannot yet |
| 1319 |
* be decoded. This is okay. |
| 1320 |
*/ |
| 1321 |
return 0; |
| 1322 |
} |
| 1323 |
else if ( out_size != yuv_size ) |
| 1324 |
{ |
| 1325 |
fprintf(stderr, "ERROR: decoder returned unexpected sized " |
| 1326 |
"frame (expected %d got %d)\n", |
| 1327 |
yuv_size, out_size); |
| 1328 |
return -1; |
| 1329 |
} |
| 1330 |
|
| 1331 |
/* Statistics */ |
| 1332 |
call->vdecoder->video_stats.inbound_frames++; |
| 1333 |
if ( time > 0 ) |
| 1334 |
call->vdecoder->video_stats.avg_inbound_fps = |
| 1335 |
call->vdecoder->video_stats.inbound_frames * |
| 1336 |
1000.0F / time; |
| 1337 |
|
| 1338 |
if ( vinfo.prefs & IAXC_VIDEO_PREF_RECV_RGB32 ) |
| 1339 |
{ |
| 1340 |
vidcap_i420_to_rgb32(vfinfo.width, vfinfo.height, |
| 1341 |
yuv_buf, rgb_buf); |
| 1342 |
|
| 1343 |
show_video_frame(rgb_buf, rgb_size, sel_call, |
| 1344 |
IAXC_SOURCE_REMOTE, 0, ts); |
| 1345 |
} |
| 1346 |
else |
| 1347 |
{ |
| 1348 |
show_video_frame(yuv_buf, yuv_size, sel_call, |
| 1349 |
IAXC_SOURCE_REMOTE, 0, ts); |
| 1350 |
} |
| 1351 |
|
| 1352 |
return 0; |
| 1353 |
} |
| 1354 |
|
| 1355 |
EXPORT int iaxc_video_devices_get(struct iaxc_video_device **devs, |
| 1356 |
int *num_devs, int *id_selected) |
| 1357 |
{ |
| 1358 |
int new_device_count; |
| 1359 |
int old_device_count; |
| 1360 |
struct vidcap_src_info *new_src_list; |
| 1361 |
struct vidcap_src_info *old_src_list; |
| 1362 |
struct iaxc_video_device *new_iaxc_dev_list; |
| 1363 |
struct iaxc_video_device *old_iaxc_dev_list; |
| 1364 |
int found_selected_device = 0; |
| 1365 |
int list_changed; |
| 1366 |
int i, n; |
| 1367 |
|
| 1368 |
if ( !vinfo.sapi ) |
| 1369 |
return -1; |
| 1370 |
|
| 1371 |
/* update libvidcap's device list */ |
| 1372 |
new_device_count = vidcap_src_list_update(vinfo.sapi); |
| 1373 |
list_changed = new_device_count != vinfo.device_count; |
| 1374 |
|
| 1375 |
if ( new_device_count < 0 ) |
| 1376 |
{ |
| 1377 |
fprintf(stderr, "ERROR: failed getting updated vidcap device list: %d\n", |
| 1378 |
new_device_count); |
| 1379 |
return -1; |
| 1380 |
} |
| 1381 |
|
| 1382 |
new_src_list = calloc(new_device_count, sizeof(struct vidcap_src_info)); |
| 1383 |
|
| 1384 |
if ( !new_src_list ) |
| 1385 |
{ |
| 1386 |
fprintf(stderr, "ERROR: failed updated source allocation\n"); |
| 1387 |
return -1; |
| 1388 |
} |
| 1389 |
|
| 1390 |
new_iaxc_dev_list = calloc(new_device_count, |
| 1391 |
sizeof(struct iaxc_video_device)); |
| 1392 |
|
| 1393 |
if ( !new_iaxc_dev_list ) |
| 1394 |
{ |
| 1395 |
free(new_src_list); |
| 1396 |
fprintf(stderr, "ERROR: failed source allocation update\n"); |
| 1397 |
return -1; |
| 1398 |
} |
| 1399 |
|
| 1400 |
/* get an updated libvidcap device list */ |
| 1401 |
if ( vidcap_src_list_get(vinfo.sapi, new_device_count, new_src_list) ) |
| 1402 |
{ |
| 1403 |
fprintf(stderr, "ERROR: failed vidcap_srcList_get().\n"); |
| 1404 |
|
| 1405 |
free(new_src_list); |
| 1406 |
free(new_iaxc_dev_list); |
| 1407 |
return -1; |
| 1408 |
} |
| 1409 |
|
| 1410 |
/* build a new iaxclient video source list */ |
| 1411 |
for ( n = 0; n < new_device_count; n++ ) |
| 1412 |
{ |
| 1413 |
new_iaxc_dev_list[n].name = strdup(new_src_list[n].description); |
| 1414 |
new_iaxc_dev_list[n].id_string = strdup(new_src_list[n].identifier); |
| 1415 |
|
| 1416 |
/* This device may have been here all along. |
| 1417 |
* If it has, re-assign that device id |
| 1418 |
* else assign a new id |
| 1419 |
*/ |
| 1420 |
for ( i = 0; i < vinfo.device_count; i++ ) |
| 1421 |
{ |
| 1422 |
/* check both the device name and its unique identifier */ |
| 1423 |
if ( !strcmp(new_iaxc_dev_list[n].name, vinfo.devices[i].name) && |
| 1424 |
!strcmp(new_iaxc_dev_list[n].id_string, |
| 1425 |
vinfo.devices[i].id_string) ) |
| 1426 |
{ |
| 1427 |
new_iaxc_dev_list[n].id = vinfo.devices[i].id; |
| 1428 |
|
| 1429 |
if ( vinfo.selected_device_id == new_iaxc_dev_list[n].id ) |
| 1430 |
found_selected_device = 1; |
| 1431 |
break; |
| 1432 |
} |
| 1433 |
} |
| 1434 |
if ( i == vinfo.device_count ) |
| 1435 |
{ |
| 1436 |
new_iaxc_dev_list[n].id = vinfo.next_id++; |
| 1437 |
|
| 1438 |
list_changed = 1; |
| 1439 |
} |
| 1440 |
} |
| 1441 |
|
| 1442 |
if ( !list_changed ) |
| 1443 |
{ |
| 1444 |
/* Free new lists. Nothing's really changed */ |
| 1445 |
free(new_src_list); |
| 1446 |
for ( i = 0; i < new_device_count; i++ ) |
| 1447 |
{ |
| 1448 |
free((void *)new_iaxc_dev_list[i].name); |
| 1449 |
free((void *)new_iaxc_dev_list[i].id_string); |
| 1450 |
} |
| 1451 |
free(new_iaxc_dev_list); |
| 1452 |
} |
| 1453 |
else |
| 1454 |
{ |
| 1455 |
old_device_count = vinfo.device_count; |
| 1456 |
old_src_list = vinfo.vc_src_info; |
| 1457 |
old_iaxc_dev_list = vinfo.devices; |
| 1458 |
|
| 1459 |
/* Update iaxclient's device list info */ |
| 1460 |
/* Lock since other iaxclient funcs use these fields */ |
| 1461 |
MUTEXLOCK(&vinfo.dev_list_lock); |
| 1462 |
|
| 1463 |
vinfo.device_count = new_device_count; |
| 1464 |
vinfo.vc_src_info = new_src_list; |
| 1465 |
vinfo.devices = new_iaxc_dev_list; |
| 1466 |
|
| 1467 |
MUTEXUNLOCK(&vinfo.dev_list_lock); |
| 1468 |
|
| 1469 |
/* free old lists */ |
| 1470 |
free(old_src_list); |
| 1471 |
for ( i = 0; i < old_device_count; i++ ) |
| 1472 |
{ |
| 1473 |
free((void *)old_iaxc_dev_list[i].name); |
| 1474 |
free((void *)old_iaxc_dev_list[i].id_string); |
| 1475 |
} |
| 1476 |
free(old_iaxc_dev_list); |
| 1477 |
} |
| 1478 |
|
| 1479 |
/* If we can't find the selected device, defer releasing it. |
| 1480 |
* It'll happen when we set a new device, or shutdown |
| 1481 |
*/ |
| 1482 |
|
| 1483 |
if ( !found_selected_device ) |
| 1484 |
vinfo.selected_device_id = -1; |
| 1485 |
|
| 1486 |
*devs = vinfo.devices; |
| 1487 |
*num_devs = vinfo.device_count; |
| 1488 |
*id_selected = vinfo.selected_device_id; |
| 1489 |
|
| 1490 |
return list_changed; |
| 1491 |
} |
| 1492 |
|
| 1493 |
EXPORT int iaxc_video_device_set(int capture_dev_id) |
| 1494 |
{ |
| 1495 |
int ret = 0; |
| 1496 |
int dev_num = 0; |
| 1497 |
|
| 1498 |
MUTEXLOCK(&vinfo.camera_lock); |
| 1499 |
|
| 1500 |
if ( capture_dev_id == vinfo.selected_device_id ) |
| 1501 |
{ |
| 1502 |
MUTEXUNLOCK(&vinfo.camera_lock); |
| 1503 |
return 0; |
| 1504 |
} |
| 1505 |
|
| 1506 |
if ( capture_dev_id < 0 ) |
| 1507 |
{ |
| 1508 |
MUTEXUNLOCK(&vinfo.camera_lock); |
| 1509 |
fprintf(stderr, "invalid video device id ( < 0 )\n"); |
| 1510 |
return -1; |
| 1511 |
} |
| 1512 |
|
| 1513 |
MUTEXLOCK(&vinfo.dev_list_lock); |
| 1514 |
|
| 1515 |
for ( dev_num = 0; dev_num < vinfo.device_count; dev_num++ ) |
| 1516 |
{ |
| 1517 |
if ( vinfo.devices[dev_num].id == capture_dev_id ) |
| 1518 |
break; |
| 1519 |
} |
| 1520 |
|
| 1521 |
if ( dev_num == vinfo.device_count ) |
| 1522 |
{ |
| 1523 |
MUTEXUNLOCK(&vinfo.dev_list_lock); |
| 1524 |
MUTEXUNLOCK(&vinfo.camera_lock); |
| 1525 |
fprintf(stderr, "invalid video device id: %d\n", |
| 1526 |
capture_dev_id); |
| 1527 |
return -1; |
| 1528 |
} |
| 1529 |
|
| 1530 |
vinfo.selected_device_id = capture_dev_id; |
| 1531 |
|
| 1532 |
if ( vinfo.capturing && vinfo.src && |
| 1533 |
vidcap_src_capture_stop(vinfo.src) ) |
| 1534 |
{ |
| 1535 |
fprintf(stderr, "failed to stop video capture\n"); |
| 1536 |
} |
| 1537 |
|
| 1538 |
if ( vinfo.src && vidcap_src_release(vinfo.src) ) |
| 1539 |
{ |
| 1540 |
fprintf(stderr, "failed to release video source\n"); |
| 1541 |
} |
| 1542 |
|
| 1543 |
vinfo.src = 0; |
| 1544 |
|
| 1545 |
MUTEXUNLOCK(&vinfo.dev_list_lock); |
| 1546 |
|
| 1547 |
if ( vinfo.capturing ) |
| 1548 |
{ |
| 1549 |
if ( ensure_acquired(capture_dev_id) ) |
| 1550 |
{ |
| 1551 |
MUTEXUNLOCK(&vinfo.camera_lock); |
| 1552 |
return -1; |
| 1553 |
} |
| 1554 |
|
| 1555 |
if ( prepare_for_capture() ) |
| 1556 |
{ |
| 1557 |
MUTEXUNLOCK(&vinfo.camera_lock); |
| 1558 |
return -1; |
| 1559 |
} |
| 1560 |
|
| 1561 |
if ( (ret = vidcap_src_capture_start(vinfo.src, |
| 1562 |
capture_callback, 0)) ) |
| 1563 |
{ |
| 1564 |
MUTEXUNLOCK(&vinfo.camera_lock); |
| 1565 |
fprintf(stderr, "failed to restart video capture: %d\n", ret); |
| 1566 |
return -1; |
| 1567 |
} |
| 1568 |
} |
| 1569 |
|
| 1570 |
MUTEXUNLOCK(&vinfo.camera_lock); |
| 1571 |
|
| 1572 |
return 0; |
| 1573 |
} |
| 1574 |
|
| 1575 |
int video_initialize(void) |
| 1576 |
{ |
| 1577 |
int i; |
| 1578 |
const int starting_id = 50; |
| 1579 |
|
| 1580 |
memset(&vinfo, 0, sizeof(vinfo)); |
| 1581 |
|
| 1582 |
vinfo.width = vfinfo.width; |
| 1583 |
vinfo.height = vfinfo.height; |
| 1584 |
vinfo.selected_device_id = -1; |
| 1585 |
vinfo.next_id = starting_id; |
| 1586 |
|
| 1587 |
MUTEXINIT(&vinfo.camera_lock); |
| 1588 |
MUTEXINIT(&vinfo.dev_list_lock); |
| 1589 |
|
| 1590 |
if ( !(vinfo.vc = vidcap_initialize()) ) |
| 1591 |
{ |
| 1592 |
fprintf(stderr, "ERROR: failed vidcap_initialize\n"); |
| 1593 |
return -1; |
| 1594 |
} |
| 1595 |
|
| 1596 |
if ( !(vinfo.sapi = vidcap_sapi_acquire(vinfo.vc, 0)) ) |
| 1597 |
{ |
| 1598 |
fprintf(stderr, "ERROR: failed vidcap_sapi_acquire\n"); |
| 1599 |
goto bail; |
| 1600 |
} |
| 1601 |
|
| 1602 |
if ( vidcap_sapi_info_get(vinfo.sapi, &vinfo.sapi_info) ) |
| 1603 |
{ |
| 1604 |
fprintf(stderr, "ERROR: failed vidcap_sapi_info_get\n"); |
| 1605 |
goto bail; |
| 1606 |
} |
| 1607 |
|
| 1608 |
fprintf(stderr, "using vidcap sapi %s (%s)\n", |
| 1609 |
vinfo.sapi_info.description, |
| 1610 |
vinfo.sapi_info.identifier); |
| 1611 |
|
| 1612 |
vinfo.device_count = vidcap_src_list_update(vinfo.sapi); |
| 1613 |
if ( vinfo.device_count < 0 ) |
| 1614 |
{ |
| 1615 |
fprintf(stderr, |
| 1616 |
"ERROR: failed updating video capture devices list\n"); |
| 1617 |
goto bail; |
| 1618 |
} |
| 1619 |
|
| 1620 |
vinfo.vc_src_info = calloc(vinfo.device_count, |
| 1621 |
sizeof(struct vidcap_src_info)); |
| 1622 |
|
| 1623 |
if ( !vinfo.vc_src_info ) |
| 1624 |
{ |
| 1625 |
fprintf(stderr, "ERROR: failed vinfo field allocations\n"); |
| 1626 |
goto bail; |
| 1627 |
} |
| 1628 |
|
| 1629 |
vinfo.devices = calloc(vinfo.device_count, |
| 1630 |
sizeof(struct iaxc_video_device)); |
| 1631 |
|
| 1632 |
if ( !vinfo.devices ) |
| 1633 |
{ |
| 1634 |
fprintf(stderr, "ERROR: failed vinfo field allocation\n"); |
| 1635 |
free(vinfo.vc_src_info); |
| 1636 |
goto bail; |
| 1637 |
} |
| 1638 |
|
| 1639 |
if ( vidcap_src_list_get(vinfo.sapi, vinfo.device_count, |
| 1640 |
vinfo.vc_src_info) ) |
| 1641 |
{ |
| 1642 |
fprintf(stderr, "ERROR: failed vidcap_src_list_get()\n"); |
| 1643 |
free(vinfo.vc_src_info); |
| 1644 |
free(vinfo.devices); |
| 1645 |
goto bail; |
| 1646 |
} |
| 1647 |
|
| 1648 |
/* build initial iaxclient video source list */ |
| 1649 |
for ( i = 0; i < vinfo.device_count; i++ ) |
| 1650 |
{ |
| 1651 |
vinfo.devices[i].name = strdup(vinfo.vc_src_info[i].description); |
| 1652 |
vinfo.devices[i].id_string = strdup(vinfo.vc_src_info[i].identifier); |
| 1653 |
/* Let's be clear that the device id is not some |
| 1654 |
* base-zero index. Once plug-n-play is implemented, |
| 1655 |
* these ids may diverge as devices are added |
| 1656 |
* and removed. |
| 1657 |
*/ |
| 1658 |
vinfo.devices[i].id = vinfo.next_id++; |
| 1659 |
} |
| 1660 |
|
| 1661 |
if ( vinfo.device_count ) |
| 1662 |
{ |
| 1663 |
/* set default source - the first device */ |
| 1664 |
iaxc_video_device_set(vinfo.devices[0].id); |
| 1665 |
} |
| 1666 |
|
| 1667 |
/* setup device notification callback |
| 1668 |
* for device insertion and removal |
| 1669 |
*/ |
| 1670 |
if ( vidcap_srcs_notify(vinfo.sapi, &video_device_notification_callback, 0) ) |
| 1671 |
{ |
| 1672 |
fprintf(stderr, "ERROR: failed vidcap_srcs_notify()\n"); |
| 1673 |
goto late_bail; |
| 1674 |
} |
| 1675 |
|
| 1676 |
vinfo.prefs = |
| 1677 |
IAXC_VIDEO_PREF_RECV_LOCAL_RAW | |
| 1678 |
IAXC_VIDEO_PREF_RECV_REMOTE_RAW | |
| 1679 |
IAXC_VIDEO_PREF_CAPTURE_DISABLE; |
| 1680 |
|
| 1681 |
return 0; |
| 1682 |
|
| 1683 |
late_bail: |
| 1684 |
free(vinfo.vc_src_info); |
| 1685 |
|
| 1686 |
for ( i = 0; i < vinfo.device_count; i++ ) |
| 1687 |
{ |
| 1688 |
free((void *)vinfo.devices[i].name); |
| 1689 |
free((void *)vinfo.devices[i].id_string); |
| 1690 |
} |
| 1691 |
|
| 1692 |
free(vinfo.devices); |
| 1693 |
|
| 1694 |
bail: |
| 1695 |
if ( vinfo.sapi ) |
| 1696 |
{ |
| 1697 |
vidcap_sapi_release(vinfo.sapi); |
| 1698 |
vinfo.sapi = 0; |
| 1699 |
} |
| 1700 |
vidcap_destroy(vinfo.vc); |
| 1701 |
vinfo.vc = 0; |
| 1702 |
return -1; |
| 1703 |
} |
| 1704 |
|
| 1705 |
int video_destroy(void) |
| 1706 |
{ |
| 1707 |
int i; |
| 1708 |
|
| 1709 |
if ( !vinfo.vc ) |
| 1710 |
return -1; |
| 1711 |
|
| 1712 |
free(vinfo.vc_src_info); |
| 1713 |
for ( i = 0; i < vinfo.device_count; i++ ) |
| 1714 |
{ |
| 1715 |
free((void *)vinfo.devices[i].name); |
| 1716 |
free((void *)vinfo.devices[i].id_string); |
| 1717 |
} |
| 1718 |
free(vinfo.devices); |
| 1719 |
|
| 1720 |
if ( vinfo.capturing && vinfo.src ) |
| 1721 |
vidcap_src_capture_stop(vinfo.src); |
| 1722 |
|
| 1723 |
if ( vinfo.src ) |
| 1724 |
vidcap_src_release(vinfo.src); |
| 1725 |
|
| 1726 |
vidcap_destroy(vinfo.vc); |
| 1727 |
vinfo.vc = 0; |
| 1728 |
|
| 1729 |
MUTEXDESTROY(&vinfo.camera_lock); |
| 1730 |
MUTEXDESTROY(&vinfo.dev_list_lock); |
| 1731 |
|
| 1732 |
if ( vinfo.converted_i420_buf ) |
| 1733 |
{ |
| 1734 |
free(vinfo.converted_i420_buf); |
| 1735 |
vinfo.converted_i420_buf = 0; |
| 1736 |
} |
| 1737 |
|
| 1738 |
if ( vinfo.converted_rgb_buf ) |
| 1739 |
{ |
| 1740 |
free(vinfo.converted_rgb_buf); |
| 1741 |
vinfo.converted_rgb_buf = 0; |
| 1742 |
} |
| 1743 |
|
| 1744 |
if ( vinfo.scaled_buf ) |
| 1745 |
{ |
| 1746 |
free(vinfo.scaled_buf); |
| 1747 |
vinfo.scaled_buf = 0; |
| 1748 |
} |
| 1749 |
|
| 1750 |
return 0; |
| 1751 |
} |
| 1752 |
|
| 1753 |
void iaxc_YUV420_to_RGB32(int width, int height, const char * src, char * dst) |
| 1754 |
{ |
| 1755 |
if ( vidcap_i420_to_rgb32(width, height, src, dst) ) |
| 1756 |
iaxci_usermsg(IAXC_TEXT_TYPE_ERROR, |
| 1757 |
"failed iaxc_YUV420_to_RGB32()"); |
| 1758 |
} |
| 1759 |
|
| 1760 |
int iaxc_is_camera_working() |
| 1761 |
{ |
| 1762 |
/* This tells us how many video sources are available, so |
| 1763 |
* we are saying that the "camera is working" if there exists |
| 1764 |
* more than zero cameras. |
| 1765 |
*/ |
| 1766 |
return vinfo.sapi && vidcap_src_list_update(vinfo.sapi) > 0; |
| 1767 |
} |
| 1768 |
|
| 1769 |
int video_send_stats(struct iaxc_call * call) |
| 1770 |
{ |
| 1771 |
const long video_stats_interval = 1000; /* milliseconds */ |
| 1772 |
static struct timeval video_stats_start = {0, 0}; |
| 1773 |
iaxc_event e; |
| 1774 |
struct timeval now; |
| 1775 |
|
| 1776 |
if ( !call ) |
| 1777 |
return -1; |
| 1778 |
|
| 1779 |
if ( video_stats_start.tv_sec == 0 && video_stats_start.tv_usec == 0 ) |
| 1780 |
video_stats_start = iax_tvnow(); |
| 1781 |
|
| 1782 |
now = iax_tvnow(); |
| 1783 |
|
| 1784 |
if ( iaxci_msecdiff(&now, &video_stats_start) > video_stats_interval ) |
| 1785 |
{ |
| 1786 |
get_stats(call, &e.ev.videostats.stats, 1); |
| 1787 |
e.type = IAXC_EVENT_VIDEOSTATS; |
| 1788 |
e.ev.videostats.callNo = selected_call; |
| 1789 |
iaxci_post_event(e); |
| 1790 |
|
| 1791 |
video_stats_start = now; |
| 1792 |
} |
| 1793 |
|
| 1794 |
return 0; |
| 1795 |
} |
| 1796 |
|
| 1797 |
EXPORT int iaxc_push_video(void *data, unsigned int size, int fragment) |
| 1798 |
{ |
| 1799 |
struct iaxc_call *call; |
| 1800 |
|
| 1801 |
if (selected_call < 0) |
| 1802 |
return -1; |
| 1803 |
|
| 1804 |
call = &calls[selected_call]; |
| 1805 |
|
| 1806 |
if ( vinfo.prefs & IAXC_VIDEO_PREF_SEND_DISABLE ) |
| 1807 |
return 0; |
| 1808 |
|
| 1809 |
//fprintf(stderr, "iaxc_push_video: sending video size %d\n", size); |
| 1810 |
|
| 1811 |
// Fragment if needed |
| 1812 |
if ( fragment ) |
| 1813 |
{ |
| 1814 |
static struct slice_set_t slice_set; |
| 1815 |
int i; |
| 1816 |
|
| 1817 |
if ( !vinfo.sc ) |
| 1818 |
vinfo.sc = create_slicer_context((unsigned short)rand(), |
| 1819 |
vfinfo.fragsize); |
| 1820 |
|
| 1821 |
slice(data, size, &slice_set, vinfo.sc); |
| 1822 |
for ( i = 0 ; i < slice_set.num_slices ; i++ ) |
| 1823 |
{ |
| 1824 |
if ( iax_send_video_trunk(call->session, |
| 1825 |
call->vformat, |
| 1826 |
slice_set.data[i], |
| 1827 |
slice_set.size[i], |
| 1828 |
0, |
| 1829 |
i |
| 1830 |
) == -1 |
| 1831 |
) |
| 1832 |
{ |
| 1833 |
fprintf(stderr, "Failed to send a slice, call %d, size %d: %s\n", |
| 1834 |
selected_call, slice_set.size[i], iax_errstr); |
| 1835 |
return -1; |
| 1836 |
} |
| 1837 |
|
| 1838 |
} |
| 1839 |
} else |
| 1840 |
{ |
| 1841 |
if ( iax_send_video_trunk(call->session, call->vformat, data, |
| 1842 |
size, 0, 0) == -1 ) |
| 1843 |
{ |
| 1844 |
fprintf(stderr, "iaxc_push_video: failed to send " |
| 1845 |
"video frame of size %d on call %d: %s\n", |
| 1846 |
size, selected_call, iax_errstr); |
| 1847 |
return -1; |
| 1848 |
} |
| 1849 |
} |
| 1850 |
|
| 1851 |
return 0; |
| 1852 |
} |
| 1853 |
|