console_video.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright 2007-2008, Marta Carbone, Sergio Fadda, Luigi Rizzo
  5. *
  6. * See http://www.asterisk.org for more information about
  7. * the Asterisk project. Please do not directly contact
  8. * any of the maintainers of this project for assistance;
  9. * the project provides a web site, mailing lists and IRC
  10. * channels for your use.
  11. *
  12. * This program is free software, distributed under the terms of
  13. * the GNU General Public License Version 2. See the LICENSE file
  14. * at the top of the source tree.
  15. */
  16. /*
  17. * Experimental support for video sessions. We use SDL for rendering, ffmpeg
  18. * as the codec library for encoding and decoding, and Video4Linux and X11
  19. * to generate the local video stream.
  20. *
  21. * If one of these pieces is not available, either at compile time or at
  22. * runtime, we do our best to run without it. Of course, no codec library
  23. * means we can only deal with raw data, no SDL means we cannot do rendering,
  24. * no V4L or X11 means we cannot generate data (but in principle we could
  25. * stream from or record to a file).
  26. *
  27. * We need a recent (2007.07.12 or newer) version of ffmpeg to avoid warnings.
  28. * Older versions might give 'deprecated' messages during compilation,
  29. * thus not compiling in AST_DEVMODE, or don't have swscale, in which case
  30. * you can try to compile #defining OLD_FFMPEG here.
  31. *
  32. * $Revision$
  33. */
  34. //#define DROP_PACKETS 5 /* if set, drop this % of video packets */
  35. //#define OLD_FFMPEG 1 /* set for old ffmpeg with no swscale */
  36. /*** MODULEINFO
  37. <support_level>extended</support_level>
  38. ***/
  39. #include "asterisk.h"
  40. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  41. #include <sys/ioctl.h>
  42. #include "asterisk/cli.h"
  43. #include "asterisk/file.h"
  44. #include "asterisk/channel.h"
  45. #include "console_video.h"
  46. /*
  47. The code is structured as follows.
  48. When a new console channel is created, we call console_video_start()
  49. to initialize SDL, the source, and the encoder/ decoder for the
  50. formats in use (XXX the latter two should be done later, once the
  51. codec negotiation is complete). Also, a thread is created to handle
  52. the video source and generate frames.
  53. While communication is on, the local source is generated by the
  54. video thread, which wakes up periodically, generates frames and
  55. enqueues them in chan->readq. Incoming rtp frames are passed to
  56. console_write_video(), decoded and passed to SDL for display.
  57. For as unfortunate and confusing as it can be, we need to deal with a
  58. number of different video representations (size, codec/pixel format,
  59. codec parameters), as follows:
  60. loc_src is the data coming from the camera/X11/etc.
  61. The format is typically constrained by the video source.
  62. enc_in is the input required by the encoder.
  63. Typically constrained in size by the encoder type.
  64. enc_out is the bitstream transmitted over RTP.
  65. Typically negotiated while the call is established.
  66. loc_dpy is the format used to display the local video source.
  67. Depending on user preferences this can have the same size as
  68. loc_src_fmt, or enc_in_fmt, or thumbnail size (e.g. PiP output)
  69. dec_in is the incoming RTP bitstream. Negotiated
  70. during call establishment, it is not necessarily the same as
  71. enc_in_fmt
  72. dec_out the output of the decoder.
  73. The format is whatever the other side sends, and the
  74. buffer is allocated by avcodec_decode_... so we only
  75. copy the data here.
  76. rem_dpy the format used to display the remote stream
  77. src_dpy is the format used to display the local video source streams
  78. The number of these fbuf_t is determined at run time, with dynamic allocation
  79. We store the format info together with the buffer storing the data.
  80. As a future optimization, a format/buffer may reference another one
  81. if the formats are equivalent. This will save some unnecessary format
  82. conversion.
  83. In order to handle video you need to add to sip.conf (and presumably
  84. iax.conf too) the following:
  85. [general](+)
  86. videosupport=yes
  87. allow=h263 ; this or other video formats
  88. allow=h263p ; this or other video formats
  89. */
  90. /*
  91. * Codecs are absolutely necessary or we cannot do anything.
  92. * SDL is optional (used for rendering only), so that we can still
  93. * stream video withouth displaying it.
  94. */
  95. #if !defined(HAVE_VIDEO_CONSOLE) || !defined(HAVE_FFMPEG)
  96. /* stubs if required pieces are missing */
  97. int console_write_video(struct ast_channel *chan, struct ast_frame *f)
  98. {
  99. return 0; /* writing video not supported */
  100. }
  101. int console_video_cli(struct video_desc *env, const char *var, int fd)
  102. {
  103. return 1; /* nothing matched */
  104. }
  105. int console_video_config(struct video_desc **penv, const char *var, const char *val)
  106. {
  107. return 1; /* no configuration */
  108. }
  109. void console_video_start(struct video_desc *env, struct ast_channel *owner)
  110. {
  111. ast_log(LOG_NOTICE, "voice only, console video support not present\n");
  112. }
  113. void console_video_uninit(struct video_desc *env)
  114. {
  115. }
  116. int get_gui_startup(struct video_desc* env)
  117. {
  118. return 0; /* no gui here */
  119. }
  120. int console_video_formats = 0;
  121. #else /* defined(HAVE_FFMPEG) && defined(HAVE_SDL) */
  122. /*! The list of video formats we support. */
  123. int console_video_formats =
  124. AST_FORMAT_H263_PLUS | AST_FORMAT_H263 |
  125. AST_FORMAT_MP4_VIDEO | AST_FORMAT_H264 | AST_FORMAT_H261 ;
  126. /* function to scale and encode buffers */
  127. static void my_scale(struct fbuf_t *in, AVPicture *p_in,
  128. struct fbuf_t *out, AVPicture *p_out);
  129. /*
  130. * this structure will be an entry in the table containing
  131. * every device specified in the file oss.conf, it contains various infomation
  132. * about the device
  133. */
  134. struct video_device {
  135. char *name; /* name of the device */
  136. /* allocated dynamically (see fill_table function) */
  137. struct grab_desc *grabber; /* the grabber for the device type */
  138. void *grabber_data; /* device's private data structure */
  139. struct fbuf_t *dev_buf; /* buffer for incoming data */
  140. struct timeval last_frame; /* when we read the last frame ? */
  141. int status_index; /* what is the status of the device (source) */
  142. /* status index is set using the IS_ON, IS_PRIMARY and IS_SECONDARY costants */
  143. /* status_index is the index of the status message in the src_msgs array in console_gui.c */
  144. };
  145. struct video_codec_desc; /* forward declaration */
  146. /*
  147. * Descriptor of the local source, made of the following pieces:
  148. * + configuration info (geometry, device name, fps...). These are read
  149. * from the config file and copied here before calling video_out_init();
  150. * + the frame buffer (buf) and source pixel format, allocated at init time;
  151. * + the encoding and RTP info, including timestamps to generate
  152. * frames at the correct rate;
  153. * + source-specific info, i.e. fd for /dev/video, dpy-image for x11, etc,
  154. * filled in by grabber_open, part of source_specific information are in
  155. * the device table (devices member), others are shared;
  156. * NOTE: loc_src.data == NULL means the rest of the struct is invalid, and
  157. * the video source is not available.
  158. */
  159. struct video_out_desc {
  160. /* video device support.
  161. * videodevice and geometry are read from the config file.
  162. * At the right time we try to open it and allocate a buffer.
  163. * If we are successful, webcam_bufsize > 0 and we can read.
  164. */
  165. /* all the following is config file info copied from the parent */
  166. int fps;
  167. int bitrate;
  168. int qmin;
  169. int sendvideo;
  170. struct fbuf_t loc_src_geometry; /* local source geometry only (from config file) */
  171. struct fbuf_t enc_out; /* encoder output buffer, allocated in video_out_init() */
  172. struct video_codec_desc *enc; /* encoder */
  173. void *enc_ctx; /* encoding context */
  174. AVCodec *codec;
  175. AVFrame *enc_in_frame; /* enc_in mapped into avcodec format. */
  176. /* The initial part of AVFrame is an AVPicture */
  177. int mtu;
  178. /* Table of devices specified with "videodevice=" in oss.conf.
  179. * Static size as we have a limited number of entries.
  180. */
  181. struct video_device devices[MAX_VIDEO_SOURCES];
  182. int device_num; /*number of devices in table*/
  183. int device_primary; /*index of the actual primary device in the table*/
  184. int device_secondary; /*index of the actual secondary device in the table*/
  185. int picture_in_picture; /*Is the PiP mode activated? 0 = NO | 1 = YES*/
  186. /* these are the coordinates of the picture inside the picture (visible if PiP mode is active)
  187. these coordinates are valid considering the containing buffer with cif geometry*/
  188. int pip_x;
  189. int pip_y;
  190. };
  191. /*
  192. * The overall descriptor, with room for config info, video source and
  193. * received data descriptors, SDL info, etc.
  194. * This should be globally visible to all modules (grabber, vcodecs, gui)
  195. * and contain all configurtion info.
  196. */
  197. struct video_desc {
  198. char codec_name[64]; /* the codec we use */
  199. int stayopen; /* set if gui starts manually */
  200. pthread_t vthread; /* video thread */
  201. ast_mutex_t dec_lock; /* sync decoder and video thread */
  202. int shutdown; /* set to shutdown vthread */
  203. struct ast_channel *owner; /* owner channel */
  204. struct fbuf_t enc_in; /* encoder input buffer, allocated in video_out_init() */
  205. char keypad_file[256]; /* image for the keypad */
  206. char keypad_font[256]; /* font for the keypad */
  207. char sdl_videodriver[256];
  208. struct fbuf_t rem_dpy; /* display remote video, no buffer (it is in win[WIN_REMOTE].bmp) */
  209. struct fbuf_t loc_dpy; /* display local source, no buffer (managed by SDL in bmp[1]) */
  210. /* geometry of the thumbnails for all video sources. */
  211. struct fbuf_t src_dpy[MAX_VIDEO_SOURCES]; /* no buffer allocated here */
  212. int frame_freeze; /* flag to freeze the incoming frame */
  213. /* local information for grabbers, codecs, gui */
  214. struct gui_info *gui;
  215. struct video_dec_desc *in; /* remote video descriptor */
  216. struct video_out_desc out; /* local video descriptor */
  217. };
  218. static AVPicture *fill_pict(struct fbuf_t *b, AVPicture *p);
  219. void fbuf_free(struct fbuf_t *b)
  220. {
  221. struct fbuf_t x = *b;
  222. if (b->data && b->size)
  223. ast_free(b->data);
  224. memset(b, '\0', sizeof(*b));
  225. /* restore some fields */
  226. b->w = x.w;
  227. b->h = x.h;
  228. b->pix_fmt = x.pix_fmt;
  229. }
  230. /* return the status of env->stayopen to chan_oss, as the latter
  231. * does not have access to fields of struct video_desc
  232. */
  233. int get_gui_startup(struct video_desc* env)
  234. {
  235. return env ? env->stayopen : 0;
  236. }
  237. #if 0
  238. /* helper function to print the amount of memory used by the process.
  239. * Useful to track memory leaks, unfortunately this code is OS-specific
  240. * so we keep it commented out.
  241. */
  242. static int
  243. used_mem(const char *msg)
  244. {
  245. char in[128];
  246. pid_t pid = getpid();
  247. sprintf(in, "ps -o vsz= -o rss= %d", pid);
  248. ast_log(LOG_WARNING, "used mem (vsize, rss) %s ", msg);
  249. system(in);
  250. return 0;
  251. }
  252. #endif
  253. #include "vcodecs.c"
  254. #include "console_gui.c"
  255. /*! \brief Try to open video sources, return 0 on success, 1 on error
  256. * opens all video sources found in the oss.conf configuration files.
  257. * Saves the grabber and the datas in the device table (in the devices field
  258. * of the descriptor referenced by v).
  259. * Initializes the device_primary and device_secondary
  260. * fields of v with the first devices that was
  261. * successfully opened.
  262. *
  263. * \param v = video out environment descriptor
  264. *
  265. * returns 0 on success, 1 on error
  266. */
  267. static int grabber_open(struct video_out_desc *v)
  268. {
  269. struct grab_desc *g;
  270. void *g_data;
  271. int i, j;
  272. /* for each device in the device table... */
  273. for (i = 0; i < v->device_num; i++) {
  274. /* device already open */
  275. if (v->devices[i].grabber)
  276. continue;
  277. /* for each type of grabber supported... */
  278. for (j = 0; (g = console_grabbers[j]); j++) {
  279. /* the grabber is opened and the informations saved in the device table */
  280. g_data = g->open(v->devices[i].name, &v->loc_src_geometry, v->fps);
  281. if (!g_data)
  282. continue;
  283. v->devices[i].grabber = g;
  284. v->devices[i].grabber_data = g_data;
  285. v->devices[i].status_index |= IS_ON;
  286. }
  287. }
  288. /* the first working device is selected as the primary one and the secondary one */
  289. for (i = 0; i < v->device_num; i++) {
  290. if (!v->devices[i].grabber)
  291. continue;
  292. v->device_primary = i;
  293. v->device_secondary = i;
  294. return 0; /* source found */
  295. }
  296. return 1; /* no source found */
  297. }
  298. /*! \brief complete a buffer from the specified local video source.
  299. * Called by get_video_frames(), in turn called by the video thread.
  300. *
  301. * \param dev = video environment descriptor
  302. * \param fps = frame per seconds, for every device
  303. *
  304. * returns:
  305. * - NULL on falure
  306. * - reference to the device buffer on success
  307. */
  308. static struct fbuf_t *grabber_read(struct video_device *dev, int fps)
  309. {
  310. struct timeval now = ast_tvnow();
  311. if (dev->grabber == NULL) /* not initialized */
  312. return NULL;
  313. /* the last_frame field in this row of the device table (dev)
  314. is always initialized, it is set during the parsing of the config
  315. file, and never unset, function fill_device_table(). */
  316. /* check if it is time to read */
  317. if (ast_tvdiff_ms(now, dev->last_frame) < 1000/fps)
  318. return NULL; /* too early */
  319. dev->last_frame = now; /* XXX actually, should correct for drift */
  320. return dev->grabber->read(dev->grabber_data);
  321. }
  322. /*! \brief handler run when dragging with the left button on
  323. * the local source window - the effect is to move the offset
  324. * of the captured area.
  325. */
  326. static void grabber_move(struct video_device *dev, int dx, int dy)
  327. {
  328. if (dev->grabber && dev->grabber->move) {
  329. dev->grabber->move(dev->grabber_data, dx, dy);
  330. }
  331. }
  332. /*
  333. * Map the codec name to the library. If not recognised, use a default.
  334. * This is useful in the output path where we decide by name, presumably.
  335. */
  336. static struct video_codec_desc *map_config_video_format(char *name)
  337. {
  338. int i;
  339. for (i = 0; supported_codecs[i]; i++)
  340. if (!strcasecmp(name, supported_codecs[i]->name))
  341. break;
  342. if (supported_codecs[i] == NULL) {
  343. ast_log(LOG_WARNING, "Cannot find codec for '%s'\n", name);
  344. i = 0;
  345. strcpy(name, supported_codecs[i]->name);
  346. }
  347. ast_log(LOG_WARNING, "Using codec '%s'\n", name);
  348. return supported_codecs[i];
  349. }
  350. /*! \brief uninitialize the descriptor for local video stream */
  351. static int video_out_uninit(struct video_desc *env)
  352. {
  353. struct video_out_desc *v = &env->out;
  354. int i; /* integer variable used as iterator */
  355. /* XXX this should be a codec callback */
  356. if (v->enc_ctx) {
  357. AVCodecContext *enc_ctx = (AVCodecContext *)v->enc_ctx;
  358. avcodec_close(enc_ctx);
  359. av_free(enc_ctx);
  360. v->enc_ctx = NULL;
  361. }
  362. if (v->enc_in_frame) {
  363. av_free(v->enc_in_frame);
  364. v->enc_in_frame = NULL;
  365. }
  366. v->codec = NULL; /* nothing to free, this is only a reference */
  367. /* release the buffers */
  368. fbuf_free(&env->enc_in);
  369. fbuf_free(&v->enc_out);
  370. /* close the grabbers */
  371. for (i = 0; i < v->device_num; i++) {
  372. if (v->devices[i].grabber){
  373. v->devices[i].grabber_data =
  374. v->devices[i].grabber->close(v->devices[i].grabber_data);
  375. v->devices[i].grabber = NULL;
  376. /* dev_buf is already freed by grabber->close() */
  377. v->devices[i].dev_buf = NULL;
  378. }
  379. v->devices[i].status_index = 0;
  380. }
  381. v->picture_in_picture = 0;
  382. env->frame_freeze = 0;
  383. return -1;
  384. }
  385. /*
  386. * Initialize the encoder for the local source:
  387. * - enc_ctx, codec, enc_in_frame are used by ffmpeg for encoding;
  388. * - enc_out is used to store the encoded frame (to be sent)
  389. * - mtu is used to determine the max size of video fragment
  390. * NOTE: we enter here with the video source already open.
  391. */
  392. static int video_out_init(struct video_desc *env)
  393. {
  394. int codec;
  395. int size;
  396. struct fbuf_t *enc_in;
  397. struct video_out_desc *v = &env->out;
  398. v->enc_ctx = NULL;
  399. v->codec = NULL;
  400. v->enc_in_frame = NULL;
  401. v->enc_out.data = NULL;
  402. codec = map_video_format(v->enc->format, CM_WR);
  403. v->codec = avcodec_find_encoder(codec);
  404. if (!v->codec) {
  405. ast_log(LOG_WARNING, "Cannot find the encoder for format %d\n",
  406. codec);
  407. return -1; /* error, but nothing to undo yet */
  408. }
  409. v->mtu = 1400; /* set it early so the encoder can use it */
  410. /* allocate the input buffer for encoding.
  411. * ffmpeg only supports PIX_FMT_YUV420P for the encoding.
  412. */
  413. enc_in = &env->enc_in;
  414. enc_in->pix_fmt = PIX_FMT_YUV420P;
  415. enc_in->size = (enc_in->w * enc_in->h * 3)/2;
  416. enc_in->data = ast_calloc(1, enc_in->size);
  417. if (!enc_in->data) {
  418. ast_log(LOG_WARNING, "Cannot allocate encoder input buffer\n");
  419. return video_out_uninit(env);
  420. }
  421. /* construct an AVFrame that points into buf_in */
  422. v->enc_in_frame = avcodec_alloc_frame();
  423. if (!v->enc_in_frame) {
  424. ast_log(LOG_WARNING, "Unable to allocate the encoding video frame\n");
  425. return video_out_uninit(env);
  426. }
  427. /* parameters for PIX_FMT_YUV420P */
  428. size = enc_in->w * enc_in->h;
  429. v->enc_in_frame->data[0] = enc_in->data;
  430. v->enc_in_frame->data[1] = v->enc_in_frame->data[0] + size;
  431. v->enc_in_frame->data[2] = v->enc_in_frame->data[1] + size/4;
  432. v->enc_in_frame->linesize[0] = enc_in->w;
  433. v->enc_in_frame->linesize[1] = enc_in->w/2;
  434. v->enc_in_frame->linesize[2] = enc_in->w/2;
  435. /* now setup the parameters for the encoder.
  436. * XXX should be codec-specific
  437. */
  438. {
  439. AVCodecContext *enc_ctx = avcodec_alloc_context();
  440. v->enc_ctx = enc_ctx;
  441. enc_ctx->pix_fmt = enc_in->pix_fmt;
  442. enc_ctx->width = enc_in->w;
  443. enc_ctx->height = enc_in->h;
  444. /* XXX rtp_callback ?
  445. * rtp_mode so ffmpeg inserts as many start codes as possible.
  446. */
  447. enc_ctx->rtp_mode = 1;
  448. enc_ctx->rtp_payload_size = v->mtu / 2; // mtu/2
  449. enc_ctx->bit_rate = v->bitrate;
  450. enc_ctx->bit_rate_tolerance = enc_ctx->bit_rate/2;
  451. enc_ctx->qmin = v->qmin; /* should be configured */
  452. enc_ctx->time_base = (AVRational){1, v->fps};
  453. enc_ctx->gop_size = v->fps*5; // emit I frame every 5 seconds
  454. v->enc->enc_init(v->enc_ctx);
  455. if (avcodec_open(enc_ctx, v->codec) < 0) {
  456. ast_log(LOG_WARNING, "Unable to initialize the encoder %d\n", codec);
  457. av_free(enc_ctx);
  458. v->enc_ctx = NULL;
  459. return video_out_uninit(env);
  460. }
  461. }
  462. /*
  463. * Allocate enough for the encoded bitstream. As we are compressing,
  464. * we hope that the output is never larger than the input size.
  465. */
  466. v->enc_out.data = ast_calloc(1, enc_in->size);
  467. v->enc_out.size = enc_in->size;
  468. v->enc_out.used = 0;
  469. return 0;
  470. }
  471. /*! \brief possibly uninitialize the video console.
  472. * Called at the end of a call, should reset the 'owner' field,
  473. * then possibly terminate the video thread if the gui has
  474. * not been started manually.
  475. * In practice, signal the thread and give it a bit of time to
  476. * complete, giving up if it gets stuck. Because uninit
  477. * is called from hangup with the channel locked, and the thread
  478. * uses the chan lock, we need to unlock here. This is unsafe,
  479. * and we should really use refcounts for the channels.
  480. */
  481. void console_video_uninit(struct video_desc *env)
  482. {
  483. int i, t = 100; /* initial wait is shorter, than make it longer */
  484. if (env->stayopen == 0) { /* gui opened by a call, do the shutdown */
  485. env->shutdown = 1;
  486. for (i=0; env->shutdown && i < 10; i++) {
  487. if (env->owner)
  488. ast_channel_unlock(env->owner);
  489. usleep(t);
  490. t = 1000000;
  491. if (env->owner)
  492. ast_channel_lock(env->owner);
  493. }
  494. env->vthread = NULL;
  495. }
  496. env->owner = NULL; /* this is unconditional */
  497. }
  498. /*! fill an AVPicture from our fbuf info, as it is required by
  499. * the image conversion routines in ffmpeg. Note that the pointers
  500. * are recalculated if the fbuf has an offset (and so represents a picture in picture)
  501. * XXX This depends on the format.
  502. */
  503. static AVPicture *fill_pict(struct fbuf_t *b, AVPicture *p)
  504. {
  505. /* provide defaults for commonly used formats */
  506. int l4 = b->w * b->h/4; /* size of U or V frame */
  507. int len = b->w; /* Y linesize, bytes */
  508. int luv = b->w/2; /* U/V linesize, bytes */
  509. int sample_size = 1;
  510. memset(p, '\0', sizeof(*p));
  511. switch (b->pix_fmt) {
  512. case PIX_FMT_RGB555:
  513. case PIX_FMT_RGB565:
  514. sample_size = 2;
  515. luv = 0;
  516. break;
  517. case PIX_FMT_RGBA32:
  518. sample_size = 4;
  519. luv = 0;
  520. break;
  521. case PIX_FMT_YUYV422: /* Packed YUV 4:2:2, 16bpp, Y0 Cb Y1 Cr */
  522. sample_size = 2; /* all data in first plane, probably */
  523. luv = 0;
  524. break;
  525. }
  526. len *= sample_size;
  527. p->data[0] = b->data;
  528. p->linesize[0] = len;
  529. /* these are only valid for component images */
  530. p->data[1] = luv ? b->data + 4*l4 : b->data+len;
  531. p->data[2] = luv ? b->data + 5*l4 : b->data+len;
  532. p->linesize[1] = luv;
  533. p->linesize[2] = luv;
  534. /* add the offsets to the pointers previously calculated,
  535. it is necessary for the picture in picture mode */
  536. p->data[0] += len*b->win_y + b->win_x*sample_size;
  537. if (luv) {
  538. p->data[1] += luv*(b->win_y/2) + (b->win_x/2) * sample_size;
  539. p->data[2] += luv*(b->win_y/2) + (b->win_x/2) * sample_size;
  540. }
  541. return p;
  542. }
  543. /*! convert/scale between an input and an output format.
  544. * Old version of ffmpeg only have img_convert, which does not rescale.
  545. * New versions use sws_scale which does both.
  546. */
  547. static void my_scale(struct fbuf_t *in, AVPicture *p_in,
  548. struct fbuf_t *out, AVPicture *p_out)
  549. {
  550. AVPicture my_p_in, my_p_out;
  551. int eff_w=out->w, eff_h=out->h;
  552. if (p_in == NULL)
  553. p_in = fill_pict(in, &my_p_in);
  554. if (p_out == NULL)
  555. p_out = fill_pict(out, &my_p_out);
  556. /*if win_w is different from zero then we must change
  557. the size of the scaled buffer (the position is already
  558. encoded into the out parameter)*/
  559. if (out->win_w) { /* picture in picture enabled */
  560. eff_w=out->win_w;
  561. eff_h=out->win_h;
  562. }
  563. #ifdef OLD_FFMPEG
  564. /* XXX img_convert is deprecated, and does not do rescaling, PiP not supported */
  565. img_convert(p_out, out->pix_fmt,
  566. p_in, in->pix_fmt, in->w, in->h);
  567. #else /* XXX replacement */
  568. {
  569. struct SwsContext *convert_ctx;
  570. convert_ctx = sws_getContext(in->w, in->h, in->pix_fmt,
  571. eff_w, eff_h, out->pix_fmt,
  572. SWS_BICUBIC, NULL, NULL, NULL);
  573. if (convert_ctx == NULL) {
  574. ast_log(LOG_ERROR, "FFMPEG::convert_cmodel : swscale context initialization failed\n");
  575. return;
  576. }
  577. if (0)
  578. ast_log(LOG_WARNING, "in %d %dx%d out %d %dx%d\n",
  579. in->pix_fmt, in->w, in->h, out->pix_fmt, eff_w, eff_h);
  580. sws_scale(convert_ctx,
  581. p_in->data, p_in->linesize,
  582. in->w, in->h, /* src slice */
  583. p_out->data, p_out->linesize);
  584. sws_freeContext(convert_ctx);
  585. }
  586. #endif /* XXX replacement */
  587. }
  588. struct video_desc *get_video_desc(struct ast_channel *c);
  589. /*
  590. * This function is called (by asterisk) for each video packet
  591. * coming from the network (the 'in' path) that needs to be processed.
  592. * We need to reconstruct the entire video frame before we can decode it.
  593. * After a video packet is received we have to:
  594. * - extract the bitstream with pre_process_data()
  595. * - append the bitstream to a buffer
  596. * - if the fragment is the last (RTP Marker) we decode it with decode_video()
  597. * - after the decoding is completed we display the decoded frame with show_frame()
  598. */
  599. int console_write_video(struct ast_channel *chan, struct ast_frame *f);
  600. int console_write_video(struct ast_channel *chan, struct ast_frame *f)
  601. {
  602. struct video_desc *env = get_video_desc(chan);
  603. struct video_dec_desc *v = env->in;
  604. if (!env->gui) /* no gui, no rendering */
  605. return 0;
  606. if (v == NULL)
  607. env->in = v = dec_init(f->subclass & ~1);
  608. if (v == NULL) {
  609. /* This is not fatal, but we won't have incoming video */
  610. ast_log(LOG_WARNING, "Cannot initialize input decoder\n");
  611. return 0;
  612. }
  613. if (v->dec_in_cur == NULL) /* no buffer for incoming frames, drop */
  614. return 0;
  615. #if defined(DROP_PACKETS) && DROP_PACKETS > 0
  616. /* Simulate lost packets */
  617. if ((random() % 10000) <= 100*DROP_PACKETS) {
  618. ast_log(LOG_NOTICE, "Packet lost [%d]\n", f->seqno);
  619. return 0;
  620. }
  621. #endif
  622. if (v->discard) {
  623. /*
  624. * In discard mode, drop packets until we find one with
  625. * the RTP marker set (which is the end of frame).
  626. * Note that the RTP marker flag is sent as the LSB of the
  627. * subclass, which is a bitmask of formats. The low bit is
  628. * normally used for audio so there is no interference.
  629. */
  630. if (f->subclass & 0x01) {
  631. v->dec_in_cur->used = 0;
  632. v->dec_in_cur->ebit = 0;
  633. v->next_seq = f->seqno + 1; /* wrap at 16 bit */
  634. v->discard = 0;
  635. ast_log(LOG_WARNING, "out of discard mode, frame %d\n", f->seqno);
  636. }
  637. return 0;
  638. }
  639. /*
  640. * Only in-order fragments will be accepted. Remember seqno
  641. * has 16 bit so there is wraparound. Also, ideally we could
  642. * accept a bit of reordering, but at the moment we don't.
  643. */
  644. if (v->next_seq != f->seqno) {
  645. ast_log(LOG_WARNING, "discarding frame out of order, %d %d\n",
  646. v->next_seq, f->seqno);
  647. v->discard = 1;
  648. return 0;
  649. }
  650. v->next_seq++;
  651. if (f->data.ptr == NULL || f->datalen < 2) {
  652. ast_log(LOG_WARNING, "empty video frame, discard\n");
  653. return 0;
  654. }
  655. if (v->d_callbacks->dec_decap(v->dec_in_cur, f->data.ptr, f->datalen)) {
  656. ast_log(LOG_WARNING, "error in dec_decap, enter discard\n");
  657. v->discard = 1;
  658. }
  659. if (f->subclass & 0x01) { // RTP Marker
  660. /* prepare to decode: advance the buffer so the video thread knows. */
  661. struct fbuf_t *tmp = v->dec_in_cur; /* store current pointer */
  662. ast_mutex_lock(&env->dec_lock);
  663. if (++v->dec_in_cur == &v->dec_in[N_DEC_IN]) /* advance to next, circular */
  664. v->dec_in_cur = &v->dec_in[0];
  665. if (v->dec_in_dpy == NULL) { /* were not displaying anything, so set it */
  666. v->dec_in_dpy = tmp;
  667. } else if (v->dec_in_dpy == v->dec_in_cur) { /* current slot is busy */
  668. v->dec_in_cur = NULL;
  669. }
  670. ast_mutex_unlock(&env->dec_lock);
  671. }
  672. return 0;
  673. }
  674. /*! \brief refreshes the buffers of all the device by calling the
  675. * grabber_read on each device in the device table.
  676. * it encodes the primary source buffer, if the picture in picture mode is
  677. * enabled it encodes (in the buffer to split) the secondary source buffer too.
  678. * The encoded buffer is splitted to build the local and the remote view.
  679. * Return a list of ast_frame representing the video fragments.
  680. * The head pointer is returned by the function, the tail pointer
  681. * is returned as an argument.
  682. *
  683. * \param env = video environment descriptor
  684. * \param tail = tail ponter (pratically a return value)
  685. */
  686. static struct ast_frame *get_video_frames(struct video_desc *env, struct ast_frame **tail)
  687. {
  688. struct video_out_desc *v = &env->out;
  689. struct ast_frame *dummy;
  690. struct fbuf_t *loc_src_primary = NULL, *p_read;
  691. int i;
  692. /* if no device was found in the config file */
  693. if (!env->out.device_num)
  694. return NULL;
  695. /* every time this function is called we refresh the buffers of every device,
  696. updating the private device buffer in the device table */
  697. for (i = 0; i < env->out.device_num; i++) {
  698. p_read = grabber_read(&env->out.devices[i], env->out.fps);
  699. /* it is used only if different from NULL, we mantain last good buffer otherwise */
  700. if (p_read)
  701. env->out.devices[i].dev_buf = p_read;
  702. }
  703. /* select the primary device buffer as the one to encode */
  704. loc_src_primary = env->out.devices[env->out.device_primary].dev_buf;
  705. /* loc_src_primary can be NULL if the device has been turned off during
  706. execution of it is read too early */
  707. if (loc_src_primary) {
  708. /* Scale the video for the encoder, then use it for local rendering
  709. so we will see the same as the remote party */
  710. my_scale(loc_src_primary, NULL, &env->enc_in, NULL);
  711. }
  712. if (env->out.picture_in_picture) { /* the picture in picture mode is enabled */
  713. struct fbuf_t *loc_src_secondary;
  714. /* reads from the secondary source */
  715. loc_src_secondary = env->out.devices[env->out.device_secondary].dev_buf;
  716. if (loc_src_secondary) {
  717. env->enc_in.win_x = env->out.pip_x;
  718. env->enc_in.win_y = env->out.pip_y;
  719. env->enc_in.win_w = env->enc_in.w/3;
  720. env->enc_in.win_h = env->enc_in.h/3;
  721. /* scales to the correct geometry and inserts in
  722. the enc_in buffer the picture in picture */
  723. my_scale(loc_src_secondary, NULL, &env->enc_in, NULL);
  724. /* returns to normal parameters (not picture in picture) */
  725. env->enc_in.win_x = 0;
  726. env->enc_in.win_y = 0;
  727. env->enc_in.win_w = 0;
  728. env->enc_in.win_h = 0;
  729. }
  730. else {
  731. /* loc_src_secondary can be NULL if the device has been turned off during
  732. execution of it is read too early */
  733. env->out.picture_in_picture = 0; /* disable picture in picture */
  734. }
  735. }
  736. show_frame(env, WIN_LOCAL); /* local rendering */
  737. for (i = 0; i < env->out.device_num; i++)
  738. show_frame(env, i+WIN_SRC1); /* rendering of every source device in thumbnails */
  739. if (tail == NULL)
  740. tail = &dummy;
  741. *tail = NULL;
  742. /* if no reason for encoding, do not encode */
  743. if (!env->owner || !loc_src_primary || !v->sendvideo)
  744. return NULL;
  745. if (v->enc_out.data == NULL) {
  746. static volatile int a = 0;
  747. if (a++ < 2)
  748. ast_log(LOG_WARNING, "fail, no encoder output buffer\n");
  749. return NULL;
  750. }
  751. v->enc->enc_run(v);
  752. return v->enc->enc_encap(&v->enc_out, v->mtu, tail);
  753. }
  754. /*
  755. * Helper thread to periodically poll the video sources and enqueue the
  756. * generated frames directed to the remote party to the channel's queue.
  757. * Using a separate thread also helps because the encoding can be
  758. * computationally expensive so we don't want to starve the main thread.
  759. */
  760. static void *video_thread(void *arg)
  761. {
  762. struct video_desc *env = arg;
  763. int count = 0;
  764. char save_display[128] = "";
  765. int i; /* integer variable used as iterator */
  766. /* if sdl_videodriver is set, override the environment. Also,
  767. * if it contains 'console' override DISPLAY around the call to SDL_Init
  768. * so we use the console as opposed to the x11 version of aalib
  769. */
  770. if (!ast_strlen_zero(env->sdl_videodriver)) { /* override */
  771. const char *s = getenv("DISPLAY");
  772. setenv("SDL_VIDEODRIVER", env->sdl_videodriver, 1);
  773. if (s && !strcasecmp(env->sdl_videodriver, "aalib-console")) {
  774. ast_copy_string(save_display, s, sizeof(save_display));
  775. unsetenv("DISPLAY");
  776. }
  777. }
  778. sdl_setup(env);
  779. if (!ast_strlen_zero(save_display)) {
  780. setenv("DISPLAY", save_display, 1);
  781. }
  782. ast_mutex_init(&env->dec_lock); /* used to sync decoder and renderer */
  783. if (grabber_open(&env->out)) {
  784. ast_log(LOG_WARNING, "cannot open local video source\n");
  785. }
  786. if (env->out.device_num) {
  787. env->out.devices[env->out.device_primary].status_index |= IS_PRIMARY | IS_SECONDARY;
  788. }
  789. /* even if no device is connected, we must call video_out_init,
  790. * as some of the data structures it initializes are
  791. * used in get_video_frames()
  792. */
  793. video_out_init(env);
  794. /* Writes intial status of the sources. */
  795. if (env->gui) {
  796. for (i = 0; i < env->out.device_num; i++) {
  797. print_message(env->gui->thumb_bd_array[i].board,
  798. src_msgs[env->out.devices[i].status_index]);
  799. }
  800. }
  801. for (;;) {
  802. struct timespec t = { 0, 50000000 }; /* XXX 20 times/sec */
  803. struct ast_frame *p, *f;
  804. struct ast_channel *chan;
  805. int fd;
  806. char *caption = NULL, buf[160];
  807. /* determine if video format changed */
  808. if (count++ % 10 == 0) {
  809. if (env->out.sendvideo && env->out.devices) {
  810. snprintf(buf, sizeof(buf), "%s %s %dx%d @@ %dfps %dkbps",
  811. env->out.devices[env->out.device_primary].name, env->codec_name,
  812. env->enc_in.w, env->enc_in.h,
  813. env->out.fps, env->out.bitrate / 1000);
  814. } else {
  815. sprintf(buf, "hold");
  816. }
  817. caption = buf;
  818. }
  819. /* manage keypad events */
  820. /* XXX here we should always check for events,
  821. * otherwise the drag will not work */
  822. if (env->gui)
  823. eventhandler(env, caption);
  824. /* sleep for a while */
  825. nanosleep(&t, NULL);
  826. if (env->in) {
  827. struct video_dec_desc *v = env->in;
  828. /*
  829. * While there is something to display, call the decoder and free
  830. * the buffer, possibly enabling the receiver to store new data.
  831. */
  832. while (v->dec_in_dpy) {
  833. struct fbuf_t *tmp = v->dec_in_dpy; /* store current pointer */
  834. /* decode the frame, but show it only if not frozen */
  835. if (v->d_callbacks->dec_run(v, tmp) && !env->frame_freeze)
  836. show_frame(env, WIN_REMOTE);
  837. tmp->used = 0; /* mark buffer as free */
  838. tmp->ebit = 0;
  839. ast_mutex_lock(&env->dec_lock);
  840. if (++v->dec_in_dpy == &v->dec_in[N_DEC_IN]) /* advance to next, circular */
  841. v->dec_in_dpy = &v->dec_in[0];
  842. if (v->dec_in_cur == NULL) /* receiver was idle, enable it... */
  843. v->dec_in_cur = tmp; /* using the slot just freed */
  844. else if (v->dec_in_dpy == v->dec_in_cur) /* this was the last slot */
  845. v->dec_in_dpy = NULL; /* nothing more to display */
  846. ast_mutex_unlock(&env->dec_lock);
  847. }
  848. }
  849. if (env->shutdown)
  850. break;
  851. f = get_video_frames(env, &p); /* read and display */
  852. if (!f)
  853. continue;
  854. chan = env->owner;
  855. if (chan == NULL) {
  856. /* drop the chain of frames, nobody uses them */
  857. while (f) {
  858. struct ast_frame *g = AST_LIST_NEXT(f, frame_list);
  859. ast_frfree(f);
  860. f = g;
  861. }
  862. continue;
  863. }
  864. fd = chan->alertpipe[1];
  865. ast_channel_lock(chan);
  866. /* AST_LIST_INSERT_TAIL is only good for one frame, cannot use here */
  867. if (chan->readq.first == NULL) {
  868. chan->readq.first = f;
  869. } else {
  870. chan->readq.last->frame_list.next = f;
  871. }
  872. chan->readq.last = p;
  873. /*
  874. * more or less same as ast_queue_frame, but extra
  875. * write on the alertpipe to signal frames.
  876. */
  877. if (fd > -1) {
  878. int blah = 1, l = sizeof(blah);
  879. for (p = f; p; p = AST_LIST_NEXT(p, frame_list)) {
  880. if (write(fd, &blah, l) != l)
  881. ast_log(LOG_WARNING, "Unable to write to alert pipe on %s, frametype/subclass %d/%d: %s!\n",
  882. chan->name, f->frametype, f->subclass, strerror(errno));
  883. }
  884. }
  885. ast_channel_unlock(chan);
  886. }
  887. /* thread terminating, here could call the uninit */
  888. /* uninitialize the local and remote video environments */
  889. env->in = dec_uninit(env->in);
  890. video_out_uninit(env);
  891. if (env->gui)
  892. env->gui = cleanup_sdl(env->gui, env->out.device_num);
  893. ast_mutex_destroy(&env->dec_lock);
  894. env->shutdown = 0;
  895. return NULL;
  896. }
  897. static void copy_geometry(struct fbuf_t *src, struct fbuf_t *dst)
  898. {
  899. if (dst->w == 0)
  900. dst->w = src->w;
  901. if (dst->h == 0)
  902. dst->h = src->h;
  903. }
  904. /*! initialize the video environment.
  905. * Apart from the formats (constant) used by sdl and the codec,
  906. * we use enc_in as the basic geometry.
  907. */
  908. static void init_env(struct video_desc *env)
  909. {
  910. struct fbuf_t *c = &(env->out.loc_src_geometry); /* local source */
  911. struct fbuf_t *ei = &(env->enc_in); /* encoder input */
  912. struct fbuf_t *ld = &(env->loc_dpy); /* local display */
  913. struct fbuf_t *rd = &(env->rem_dpy); /* remote display */
  914. int i; /* integer working as iterator */
  915. c->pix_fmt = PIX_FMT_YUV420P; /* default - camera format */
  916. ei->pix_fmt = PIX_FMT_YUV420P; /* encoder input */
  917. if (ei->w == 0 || ei->h == 0) {
  918. ei->w = 352;
  919. ei->h = 288;
  920. }
  921. ld->pix_fmt = rd->pix_fmt = PIX_FMT_YUV420P; /* sdl format */
  922. /* inherit defaults */
  923. copy_geometry(ei, c); /* camera inherits from encoder input */
  924. copy_geometry(ei, rd); /* remote display inherits from encoder input */
  925. copy_geometry(rd, ld); /* local display inherits from remote display */
  926. /* fix the size of buffers for small windows */
  927. for (i = 0; i < env->out.device_num; i++) {
  928. env->src_dpy[i].pix_fmt = PIX_FMT_YUV420P;
  929. env->src_dpy[i].w = SRC_WIN_W;
  930. env->src_dpy[i].h = SRC_WIN_H;
  931. }
  932. /* now we set the default coordinates for the picture in picture
  933. frames inside the env_in buffers, those can be changed by dragging the
  934. picture in picture with left click */
  935. env->out.pip_x = ei->w - ei->w/3;
  936. env->out.pip_y = ei->h - ei->h/3;
  937. }
  938. /*!
  939. * The first call to the video code, called by oss_new() or similar.
  940. * Here we initialize the various components we use, namely SDL for display,
  941. * ffmpeg for encoding/decoding, and a local video source.
  942. * We do our best to progress even if some of the components are not
  943. * available.
  944. */
  945. void console_video_start(struct video_desc *env, struct ast_channel *owner)
  946. {
  947. ast_log(LOG_WARNING, "env %p chan %p\n", env, owner);
  948. if (env == NULL) /* video not initialized */
  949. return;
  950. env->owner = owner; /* work even if no owner is specified */
  951. if (env->vthread)
  952. return; /* already initialized, nothing to do */
  953. init_env(env);
  954. env->out.enc = map_config_video_format(env->codec_name);
  955. ast_log(LOG_WARNING, "start video out %s %dx%d\n",
  956. env->codec_name, env->enc_in.w, env->enc_in.h);
  957. /*
  958. * Register all codecs supported by the ffmpeg library.
  959. * We only need to do it once, but probably doesn't
  960. * harm to do it multiple times.
  961. */
  962. avcodec_init();
  963. avcodec_register_all();
  964. av_log_set_level(AV_LOG_ERROR); /* only report errors */
  965. if (env->out.fps == 0) {
  966. env->out.fps = 15;
  967. ast_log(LOG_WARNING, "fps unset, forcing to %d\n", env->out.fps);
  968. }
  969. if (env->out.bitrate == 0) {
  970. env->out.bitrate = 65000;
  971. ast_log(LOG_WARNING, "bitrate unset, forcing to %d\n", env->out.bitrate);
  972. }
  973. /* create the thread as detached so memory is freed on termination */
  974. ast_pthread_create_detached_background(&env->vthread,
  975. NULL, video_thread, env);
  976. }
  977. /*
  978. * Parse a geometry string, accepting also common names for the formats.
  979. * Trick: if we have a leading > or < and a numeric geometry,
  980. * return the larger or smaller one.
  981. * E.g. <352x288 gives the smaller one, 320x240
  982. */
  983. static int video_geom(struct fbuf_t *b, const char *s)
  984. {
  985. int w = 0, h = 0;
  986. static struct {
  987. const char *s; int w; int h;
  988. } *fp, formats[] = {
  989. {"16cif", 1408, 1152 },
  990. {"xga", 1024, 768 },
  991. {"4cif", 704, 576 },
  992. {"vga", 640, 480 },
  993. {"cif", 352, 288 },
  994. {"qvga", 320, 240 },
  995. {"qcif", 176, 144 },
  996. {"sqcif", 128, 96 },
  997. {NULL, 0, 0 },
  998. };
  999. if (*s == '<' || *s == '>')
  1000. sscanf(s+1,"%dx%d", &w, &h);
  1001. for (fp = formats; fp->s; fp++) {
  1002. if (*s == '>') { /* look for a larger one */
  1003. if (fp->w <= w) {
  1004. if (fp > formats)
  1005. fp--; /* back one step if possible */
  1006. break;
  1007. }
  1008. } else if (*s == '<') { /* look for a smaller one */
  1009. if (fp->w < w)
  1010. break;
  1011. } else if (!strcasecmp(s, fp->s)) { /* look for a string */
  1012. break;
  1013. }
  1014. }
  1015. if (*s == '<' && fp->s == NULL) /* smallest */
  1016. fp--;
  1017. if (fp->s) {
  1018. b->w = fp->w;
  1019. b->h = fp->h;
  1020. } else if (sscanf(s, "%dx%d", &b->w, &b->h) != 2) {
  1021. ast_log(LOG_WARNING, "Invalid video_size %s, using 352x288\n", s);
  1022. b->w = 352;
  1023. b->h = 288;
  1024. }
  1025. return 0;
  1026. }
  1027. /*! \brief add an entry to the video_device table,
  1028. * ignoring duplicate names.
  1029. * The table is a static array of 9 elements.
  1030. * The last_frame field of each entry of the table is initialized to
  1031. * the current time (we need a value inside this field, on stop of the
  1032. * GUI the last_frame value is not changed, to avoid checking if it is 0 we
  1033. * set the initial value on current time) XXX
  1034. *
  1035. * PARAMETERS:
  1036. * \param devices_p = pointer to the table of devices
  1037. * \param device_num_p = pointer to the number of devices
  1038. * \param s = name of the new device to insert
  1039. *
  1040. * returns 0 on success, 1 on error
  1041. */
  1042. static int device_table_fill(struct video_device *devices, int *device_num_p, const char *s)
  1043. {
  1044. int i;
  1045. struct video_device *p;
  1046. /* with the current implementation, we support a maximum of 9 devices.*/
  1047. if (*device_num_p >= 9)
  1048. return 0; /* more devices will be ignored */
  1049. /* ignore duplicate names */
  1050. for (i = 0; i < *device_num_p; i++) {
  1051. if (!strcmp(devices[i].name, s))
  1052. return 0;
  1053. }
  1054. /* inserts the new video device */
  1055. p = &devices[*device_num_p];
  1056. /* XXX the string is allocated but NEVER deallocated,
  1057. the good time to do that is when the module is unloaded, now we skip the problem */
  1058. p->name = ast_strdup(s); /* copy the name */
  1059. /* other fields initially NULL */
  1060. p->grabber = NULL;
  1061. p->grabber_data = NULL;
  1062. p->dev_buf = NULL;
  1063. p->last_frame = ast_tvnow();
  1064. p->status_index = 0;
  1065. (*device_num_p)++; /* one device added */
  1066. return 0;
  1067. }
  1068. /* extend ast_cli with video commands. Called by console_video_config */
  1069. int console_video_cli(struct video_desc *env, const char *var, int fd)
  1070. {
  1071. if (env == NULL)
  1072. return 1; /* unrecognised */
  1073. if (!strcasecmp(var, "videodevice")) {
  1074. ast_cli(fd, "videodevice is [%s]\n", env->out.devices[env->out.device_primary].name);
  1075. } else if (!strcasecmp(var, "videocodec")) {
  1076. ast_cli(fd, "videocodec is [%s]\n", env->codec_name);
  1077. } else if (!strcasecmp(var, "sendvideo")) {
  1078. ast_cli(fd, "sendvideo is [%s]\n", env->out.sendvideo ? "on" : "off");
  1079. } else if (!strcasecmp(var, "video_size")) {
  1080. int in_w = 0, in_h = 0;
  1081. if (env->in) {
  1082. in_w = env->in->dec_out.w;
  1083. in_h = env->in->dec_out.h;
  1084. }
  1085. ast_cli(fd, "sizes: video %dx%d camera %dx%d local %dx%d remote %dx%d in %dx%d\n",
  1086. env->enc_in.w, env->enc_in.h,
  1087. env->out.loc_src_geometry.w, env->out.loc_src_geometry.h,
  1088. env->loc_dpy.w, env->loc_dpy.h,
  1089. env->rem_dpy.w, env->rem_dpy.h,
  1090. in_w, in_h);
  1091. } else if (!strcasecmp(var, "bitrate")) {
  1092. ast_cli(fd, "bitrate is [%d]\n", env->out.bitrate);
  1093. } else if (!strcasecmp(var, "qmin")) {
  1094. ast_cli(fd, "qmin is [%d]\n", env->out.qmin);
  1095. } else if (!strcasecmp(var, "fps")) {
  1096. ast_cli(fd, "fps is [%d]\n", env->out.fps);
  1097. } else if (!strcasecmp(var, "startgui")) {
  1098. env->stayopen = 1;
  1099. console_video_start(env, NULL);
  1100. } else if (!strcasecmp(var, "stopgui") && env->stayopen != 0) {
  1101. env->stayopen = 0;
  1102. if (env->gui && env->owner)
  1103. ast_cli_command(-1, "console hangup");
  1104. else /* not in a call */
  1105. console_video_uninit(env);
  1106. } else {
  1107. return 1; /* unrecognised */
  1108. }
  1109. return 0; /* recognised */
  1110. }
  1111. /*! parse config command for video support. */
  1112. int console_video_config(struct video_desc **penv,
  1113. const char *var, const char *val)
  1114. {
  1115. struct video_desc *env;
  1116. if (penv == NULL) {
  1117. ast_log(LOG_WARNING, "bad argument penv=NULL\n");
  1118. return 1; /* error */
  1119. }
  1120. /* allocate the video descriptor first time we get here */
  1121. env = *penv;
  1122. if (env == NULL) {
  1123. env = *penv = ast_calloc(1, sizeof(struct video_desc));
  1124. if (env == NULL) {
  1125. ast_log(LOG_WARNING, "fail to allocate video_desc\n");
  1126. return 1; /* error */
  1127. }
  1128. /* set default values - 0's are already there */
  1129. env->out.device_primary = 0;
  1130. env->out.device_secondary = 0;
  1131. env->out.fps = 5;
  1132. env->out.bitrate = 65000;
  1133. env->out.sendvideo = 1;
  1134. env->out.qmin = 3;
  1135. env->out.device_num = 0;
  1136. }
  1137. CV_START(var, val);
  1138. CV_F("videodevice", device_table_fill(env->out.devices, &env->out.device_num, val));
  1139. CV_BOOL("sendvideo", env->out.sendvideo);
  1140. CV_F("video_size", video_geom(&env->enc_in, val));
  1141. CV_F("camera_size", video_geom(&env->out.loc_src_geometry, val));
  1142. CV_F("local_size", video_geom(&env->loc_dpy, val));
  1143. CV_F("remote_size", video_geom(&env->rem_dpy, val));
  1144. CV_STR("keypad", env->keypad_file);
  1145. CV_F("region", keypad_cfg_read(env->gui, val));
  1146. CV_UINT("startgui", env->stayopen); /* enable gui at startup */
  1147. CV_STR("keypad_font", env->keypad_font);
  1148. CV_STR("sdl_videodriver", env->sdl_videodriver);
  1149. CV_UINT("fps", env->out.fps);
  1150. CV_UINT("bitrate", env->out.bitrate);
  1151. CV_UINT("qmin", env->out.qmin);
  1152. CV_STR("videocodec", env->codec_name);
  1153. return 1; /* nothing found */
  1154. CV_END; /* the 'nothing found' case */
  1155. return 0; /* found something */
  1156. }
  1157. #endif /* video support */