console_video.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292
  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. ast_channel_lock(chan);
  865. /* AST_LIST_INSERT_TAIL is only good for one frame, cannot use here */
  866. if (ast_channel_readq(chan).first == NULL) {
  867. ast_channel_readq(chan).first = f;
  868. } else {
  869. ast_channel_readq(chan).last->frame_list.next = f;
  870. }
  871. ast_channel_readq(chan).last = p;
  872. /*
  873. * more or less same as ast_queue_frame, but extra
  874. * write on the alertpipe to signal frames.
  875. */
  876. if (ast_channel_alertable(chan)) {
  877. for (p = f; p; p = AST_LIST_NEXT(p, frame_list)) {
  878. if (ast_channel_alert(chan)) {
  879. ast_log(LOG_WARNING, "Unable to write to alert pipe on %s, frametype/subclass %d/%d: %s!\n",
  880. ast_channel_name(chan), f->frametype, f->subclass, strerror(errno));
  881. }
  882. }
  883. ast_channel_unlock(chan);
  884. }
  885. /* thread terminating, here could call the uninit */
  886. /* uninitialize the local and remote video environments */
  887. env->in = dec_uninit(env->in);
  888. video_out_uninit(env);
  889. if (env->gui)
  890. env->gui = cleanup_sdl(env->gui, env->out.device_num);
  891. ast_mutex_destroy(&env->dec_lock);
  892. env->shutdown = 0;
  893. return NULL;
  894. }
  895. static void copy_geometry(struct fbuf_t *src, struct fbuf_t *dst)
  896. {
  897. if (dst->w == 0)
  898. dst->w = src->w;
  899. if (dst->h == 0)
  900. dst->h = src->h;
  901. }
  902. /*! initialize the video environment.
  903. * Apart from the formats (constant) used by sdl and the codec,
  904. * we use enc_in as the basic geometry.
  905. */
  906. static void init_env(struct video_desc *env)
  907. {
  908. struct fbuf_t *c = &(env->out.loc_src_geometry); /* local source */
  909. struct fbuf_t *ei = &(env->enc_in); /* encoder input */
  910. struct fbuf_t *ld = &(env->loc_dpy); /* local display */
  911. struct fbuf_t *rd = &(env->rem_dpy); /* remote display */
  912. int i; /* integer working as iterator */
  913. c->pix_fmt = PIX_FMT_YUV420P; /* default - camera format */
  914. ei->pix_fmt = PIX_FMT_YUV420P; /* encoder input */
  915. if (ei->w == 0 || ei->h == 0) {
  916. ei->w = 352;
  917. ei->h = 288;
  918. }
  919. ld->pix_fmt = rd->pix_fmt = PIX_FMT_YUV420P; /* sdl format */
  920. /* inherit defaults */
  921. copy_geometry(ei, c); /* camera inherits from encoder input */
  922. copy_geometry(ei, rd); /* remote display inherits from encoder input */
  923. copy_geometry(rd, ld); /* local display inherits from remote display */
  924. /* fix the size of buffers for small windows */
  925. for (i = 0; i < env->out.device_num; i++) {
  926. env->src_dpy[i].pix_fmt = PIX_FMT_YUV420P;
  927. env->src_dpy[i].w = SRC_WIN_W;
  928. env->src_dpy[i].h = SRC_WIN_H;
  929. }
  930. /* now we set the default coordinates for the picture in picture
  931. frames inside the env_in buffers, those can be changed by dragging the
  932. picture in picture with left click */
  933. env->out.pip_x = ei->w - ei->w/3;
  934. env->out.pip_y = ei->h - ei->h/3;
  935. }
  936. /*!
  937. * The first call to the video code, called by oss_new() or similar.
  938. * Here we initialize the various components we use, namely SDL for display,
  939. * ffmpeg for encoding/decoding, and a local video source.
  940. * We do our best to progress even if some of the components are not
  941. * available.
  942. */
  943. void console_video_start(struct video_desc *env, struct ast_channel *owner)
  944. {
  945. ast_log(LOG_WARNING, "env %p chan %p\n", env, owner);
  946. if (env == NULL) /* video not initialized */
  947. return;
  948. env->owner = owner; /* work even if no owner is specified */
  949. if (env->vthread)
  950. return; /* already initialized, nothing to do */
  951. init_env(env);
  952. env->out.enc = map_config_video_format(env->codec_name);
  953. ast_log(LOG_WARNING, "start video out %s %dx%d\n",
  954. env->codec_name, env->enc_in.w, env->enc_in.h);
  955. /*
  956. * Register all codecs supported by the ffmpeg library.
  957. * We only need to do it once, but probably doesn't
  958. * harm to do it multiple times.
  959. */
  960. avcodec_init();
  961. avcodec_register_all();
  962. av_log_set_level(AV_LOG_ERROR); /* only report errors */
  963. if (env->out.fps == 0) {
  964. env->out.fps = 15;
  965. ast_log(LOG_WARNING, "fps unset, forcing to %d\n", env->out.fps);
  966. }
  967. if (env->out.bitrate == 0) {
  968. env->out.bitrate = 65000;
  969. ast_log(LOG_WARNING, "bitrate unset, forcing to %d\n", env->out.bitrate);
  970. }
  971. /* create the thread as detached so memory is freed on termination */
  972. ast_pthread_create_detached_background(&env->vthread,
  973. NULL, video_thread, env);
  974. }
  975. /*
  976. * Parse a geometry string, accepting also common names for the formats.
  977. * Trick: if we have a leading > or < and a numeric geometry,
  978. * return the larger or smaller one.
  979. * E.g. <352x288 gives the smaller one, 320x240
  980. */
  981. static int video_geom(struct fbuf_t *b, const char *s)
  982. {
  983. int w = 0, h = 0;
  984. static struct {
  985. const char *s; int w; int h;
  986. } *fp, formats[] = {
  987. {"16cif", 1408, 1152 },
  988. {"xga", 1024, 768 },
  989. {"4cif", 704, 576 },
  990. {"vga", 640, 480 },
  991. {"cif", 352, 288 },
  992. {"qvga", 320, 240 },
  993. {"qcif", 176, 144 },
  994. {"sqcif", 128, 96 },
  995. {NULL, 0, 0 },
  996. };
  997. if (*s == '<' || *s == '>')
  998. sscanf(s+1,"%dx%d", &w, &h);
  999. for (fp = formats; fp->s; fp++) {
  1000. if (*s == '>') { /* look for a larger one */
  1001. if (fp->w <= w) {
  1002. if (fp > formats)
  1003. fp--; /* back one step if possible */
  1004. break;
  1005. }
  1006. } else if (*s == '<') { /* look for a smaller one */
  1007. if (fp->w < w)
  1008. break;
  1009. } else if (!strcasecmp(s, fp->s)) { /* look for a string */
  1010. break;
  1011. }
  1012. }
  1013. if (*s == '<' && fp->s == NULL) /* smallest */
  1014. fp--;
  1015. if (fp->s) {
  1016. b->w = fp->w;
  1017. b->h = fp->h;
  1018. } else if (sscanf(s, "%dx%d", &b->w, &b->h) != 2) {
  1019. ast_log(LOG_WARNING, "Invalid video_size %s, using 352x288\n", s);
  1020. b->w = 352;
  1021. b->h = 288;
  1022. }
  1023. return 0;
  1024. }
  1025. /*! \brief add an entry to the video_device table,
  1026. * ignoring duplicate names.
  1027. * The table is a static array of 9 elements.
  1028. * The last_frame field of each entry of the table is initialized to
  1029. * the current time (we need a value inside this field, on stop of the
  1030. * GUI the last_frame value is not changed, to avoid checking if it is 0 we
  1031. * set the initial value on current time) XXX
  1032. *
  1033. * PARAMETERS:
  1034. * \param devices_p = pointer to the table of devices
  1035. * \param device_num_p = pointer to the number of devices
  1036. * \param s = name of the new device to insert
  1037. *
  1038. * returns 0 on success, 1 on error
  1039. */
  1040. static int device_table_fill(struct video_device *devices, int *device_num_p, const char *s)
  1041. {
  1042. int i;
  1043. struct video_device *p;
  1044. /* with the current implementation, we support a maximum of 9 devices.*/
  1045. if (*device_num_p >= 9)
  1046. return 0; /* more devices will be ignored */
  1047. /* ignore duplicate names */
  1048. for (i = 0; i < *device_num_p; i++) {
  1049. if (!strcmp(devices[i].name, s))
  1050. return 0;
  1051. }
  1052. /* inserts the new video device */
  1053. p = &devices[*device_num_p];
  1054. /* XXX the string is allocated but NEVER deallocated,
  1055. the good time to do that is when the module is unloaded, now we skip the problem */
  1056. p->name = ast_strdup(s); /* copy the name */
  1057. /* other fields initially NULL */
  1058. p->grabber = NULL;
  1059. p->grabber_data = NULL;
  1060. p->dev_buf = NULL;
  1061. p->last_frame = ast_tvnow();
  1062. p->status_index = 0;
  1063. (*device_num_p)++; /* one device added */
  1064. return 0;
  1065. }
  1066. /* extend ast_cli with video commands. Called by console_video_config */
  1067. int console_video_cli(struct video_desc *env, const char *var, int fd)
  1068. {
  1069. if (env == NULL)
  1070. return 1; /* unrecognised */
  1071. if (!strcasecmp(var, "videodevice")) {
  1072. ast_cli(fd, "videodevice is [%s]\n", env->out.devices[env->out.device_primary].name);
  1073. } else if (!strcasecmp(var, "videocodec")) {
  1074. ast_cli(fd, "videocodec is [%s]\n", env->codec_name);
  1075. } else if (!strcasecmp(var, "sendvideo")) {
  1076. ast_cli(fd, "sendvideo is [%s]\n", env->out.sendvideo ? "on" : "off");
  1077. } else if (!strcasecmp(var, "video_size")) {
  1078. int in_w = 0, in_h = 0;
  1079. if (env->in) {
  1080. in_w = env->in->dec_out.w;
  1081. in_h = env->in->dec_out.h;
  1082. }
  1083. ast_cli(fd, "sizes: video %dx%d camera %dx%d local %dx%d remote %dx%d in %dx%d\n",
  1084. env->enc_in.w, env->enc_in.h,
  1085. env->out.loc_src_geometry.w, env->out.loc_src_geometry.h,
  1086. env->loc_dpy.w, env->loc_dpy.h,
  1087. env->rem_dpy.w, env->rem_dpy.h,
  1088. in_w, in_h);
  1089. } else if (!strcasecmp(var, "bitrate")) {
  1090. ast_cli(fd, "bitrate is [%d]\n", env->out.bitrate);
  1091. } else if (!strcasecmp(var, "qmin")) {
  1092. ast_cli(fd, "qmin is [%d]\n", env->out.qmin);
  1093. } else if (!strcasecmp(var, "fps")) {
  1094. ast_cli(fd, "fps is [%d]\n", env->out.fps);
  1095. } else if (!strcasecmp(var, "startgui")) {
  1096. env->stayopen = 1;
  1097. console_video_start(env, NULL);
  1098. } else if (!strcasecmp(var, "stopgui") && env->stayopen != 0) {
  1099. env->stayopen = 0;
  1100. if (env->gui && env->owner)
  1101. ast_cli_command(-1, "console hangup");
  1102. else /* not in a call */
  1103. console_video_uninit(env);
  1104. } else {
  1105. return 1; /* unrecognised */
  1106. }
  1107. return 0; /* recognised */
  1108. }
  1109. /*! parse config command for video support. */
  1110. int console_video_config(struct video_desc **penv,
  1111. const char *var, const char *val)
  1112. {
  1113. struct video_desc *env;
  1114. if (penv == NULL) {
  1115. ast_log(LOG_WARNING, "bad argument penv=NULL\n");
  1116. return 1; /* error */
  1117. }
  1118. /* allocate the video descriptor first time we get here */
  1119. env = *penv;
  1120. if (env == NULL) {
  1121. env = *penv = ast_calloc(1, sizeof(struct video_desc));
  1122. if (env == NULL) {
  1123. ast_log(LOG_WARNING, "fail to allocate video_desc\n");
  1124. return 1; /* error */
  1125. }
  1126. /* set default values - 0's are already there */
  1127. env->out.device_primary = 0;
  1128. env->out.device_secondary = 0;
  1129. env->out.fps = 5;
  1130. env->out.bitrate = 65000;
  1131. env->out.sendvideo = 1;
  1132. env->out.qmin = 3;
  1133. env->out.device_num = 0;
  1134. }
  1135. CV_START(var, val);
  1136. CV_F("videodevice", device_table_fill(env->out.devices, &env->out.device_num, val));
  1137. CV_BOOL("sendvideo", env->out.sendvideo);
  1138. CV_F("video_size", video_geom(&env->enc_in, val));
  1139. CV_F("camera_size", video_geom(&env->out.loc_src_geometry, val));
  1140. CV_F("local_size", video_geom(&env->loc_dpy, val));
  1141. CV_F("remote_size", video_geom(&env->rem_dpy, val));
  1142. CV_STR("keypad", env->keypad_file);
  1143. CV_F("region", keypad_cfg_read(env->gui, val));
  1144. CV_UINT("startgui", env->stayopen); /* enable gui at startup */
  1145. CV_STR("keypad_font", env->keypad_font);
  1146. CV_STR("sdl_videodriver", env->sdl_videodriver);
  1147. CV_UINT("fps", env->out.fps);
  1148. CV_UINT("bitrate", env->out.bitrate);
  1149. CV_UINT("qmin", env->out.qmin);
  1150. CV_STR("videocodec", env->codec_name);
  1151. return 1; /* nothing found */
  1152. CV_END; /* the 'nothing found' case */
  1153. return 0; /* found something */
  1154. }
  1155. #endif /* video support */