From f3ab456a17af1c89a6e3be4d20c5944853df1cb0 Mon Sep 17 00:00:00 2001 From: "David M. Lee" Date: Mon, 7 Jan 2013 14:24:28 -0600 Subject: Import pjproject-2.0.1 --- .../src/3rdparty_media_sample/alt_pjsua_vid.c | 602 +++++++++++++++++++++ 1 file changed, 602 insertions(+) create mode 100644 pjsip-apps/src/3rdparty_media_sample/alt_pjsua_vid.c (limited to 'pjsip-apps/src/3rdparty_media_sample/alt_pjsua_vid.c') diff --git a/pjsip-apps/src/3rdparty_media_sample/alt_pjsua_vid.c b/pjsip-apps/src/3rdparty_media_sample/alt_pjsua_vid.c new file mode 100644 index 0000000..08f7aff --- /dev/null +++ b/pjsip-apps/src/3rdparty_media_sample/alt_pjsua_vid.c @@ -0,0 +1,602 @@ +/* $Id: alt_pjsua_vid.c 4174 2012-06-21 08:09:53Z bennylp $ */ +/* + * Copyright (C) 2011-2011 Teluu Inc. (http://www.teluu.com) + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ +#include +#include + +#if defined(PJSUA_MEDIA_HAS_PJMEDIA) && PJSUA_MEDIA_HAS_PJMEDIA != 0 +# error The PJSUA_MEDIA_HAS_PJMEDIA should be declared as zero +#endif + +#if PJSUA_HAS_VIDEO + +#define THIS_FILE "alt_pjsua_vid.c" +#define UNIMPLEMENTED(func) PJ_LOG(2,(THIS_FILE, "*** Call to unimplemented function %s ***", #func)); + +/***************************************************************************** + * Our video codec descriptors + */ +struct alt_codec_desc +{ + /* Predefined info */ + pjmedia_vid_codec_info info; + pjmedia_format_id base_fmt_id; + pj_uint32_t avg_bps; + pj_uint32_t max_bps; + pjmedia_codec_fmtp dec_fmtp; +} alt_vid_codecs[] = +{ + /* H.263+ */ + { + {PJMEDIA_FORMAT_H263P, PJMEDIA_RTP_PT_H263P, {"H263-1998",9}, + {"H.263 codec", 11}, 90000, PJMEDIA_DIR_ENCODING_DECODING, + 0, {PJMEDIA_FORMAT_RGB24}, PJMEDIA_VID_PACKING_PACKETS + }, + PJMEDIA_FORMAT_H263, 256000, 512000, + {2, { {{"CIF",3}, {"1",1}}, + {{"QCIF",4}, {"1",1}}, } }, + } +}; + +static const struct alt_codec_desc* find_codec_desc_by_info(const pjmedia_vid_codec_info *info) +{ + unsigned i; + for (i=0; iinfo.fmt_id == info->fmt_id) && + ((desc->info.dir & info->dir) == info->dir) && + (desc->info.pt == info->pt) && + (desc->info.packings & info->packings)) + { + return desc; + } + } + + return NULL; +} + +static pj_status_t alt_vid_codec_test_alloc( pjmedia_vid_codec_factory *factory, + const pjmedia_vid_codec_info *id ) +{ + const struct alt_codec_desc *desc = find_codec_desc_by_info(id); + return desc? PJ_SUCCESS : PJMEDIA_CODEC_EUNSUP; +} + +static pj_status_t alt_vid_codec_default_attr( pjmedia_vid_codec_factory *factory, + const pjmedia_vid_codec_info *info, + pjmedia_vid_codec_param *attr ) +{ + const struct alt_codec_desc *desc = find_codec_desc_by_info(info); + unsigned i; + + if (!desc) + return PJMEDIA_CODEC_EUNSUP; + + pj_bzero(attr, sizeof(pjmedia_vid_codec_param)); + + /* Scan the requested packings and use the lowest number */ + attr->packing = 0; + for (i=0; i<15; ++i) { + unsigned packing = (1 << i); + if ((desc->info.packings & info->packings) & packing) { + attr->packing = (pjmedia_vid_packing)packing; + break; + } + } + if (attr->packing == 0) { + /* No supported packing in info */ + return PJMEDIA_CODEC_EUNSUP; + } + + /* Direction */ + attr->dir = desc->info.dir; + + /* Encoded format */ + pjmedia_format_init_video(&attr->enc_fmt, desc->info.fmt_id, + 720, 480, 30000, 1001); + + /* Decoded format */ + pjmedia_format_init_video(&attr->dec_fmt, desc->info.dec_fmt_id[0], + //352, 288, 30000, 1001); + 720, 576, 30000, 1001); + + /* Decoding fmtp */ + attr->dec_fmtp = desc->dec_fmtp; + + /* Bitrate */ + attr->enc_fmt.det.vid.avg_bps = desc->avg_bps; + attr->enc_fmt.det.vid.max_bps = desc->max_bps; + + /* MTU */ + attr->enc_mtu = PJMEDIA_MAX_MTU; + + return PJ_SUCCESS; +} + + +static pj_status_t alt_vid_codec_enum_codecs( pjmedia_vid_codec_factory *factory, + unsigned *count, + pjmedia_vid_codec_info codecs[]) +{ + unsigned i, max_cnt; + + PJ_ASSERT_RETURN(codecs && *count > 0, PJ_EINVAL); + + max_cnt = PJ_MIN(*count, PJ_ARRAY_SIZE(alt_vid_codecs)); + *count = 0; + + for (i=0; itp) { + pjmedia_transport_detach(call_med->tp, call_med); + } + + /* + * TODO: + * - stop your video stream here + */ + +} + +/* Our callback to receive incoming RTP packets */ +static void vid_rtp_cb(void *user_data, void *pkt, pj_ssize_t size) +{ + pjsua_call_media *call_med = (pjsua_call_media*) user_data; + + /* TODO: Do something with the packet */ + PJ_LOG(4,(THIS_FILE, "RX %d bytes video RTP packet", (int)size)); +} + +/* Our callback to receive RTCP packets */ +static void vid_rtcp_cb(void *user_data, void *pkt, pj_ssize_t size) +{ + pjsua_call_media *call_med = (pjsua_call_media*) user_data; + + /* TODO: Do something with the packet here */ + PJ_LOG(4,(THIS_FILE, "RX %d bytes video RTCP packet", (int)size)); +} + +/* A demo function to send dummy "RTP" packets periodically. You would not + * need to have this function in the real app! + */ +static void timer_to_send_vid_rtp(void *user_data) +{ + pjsua_call_media *call_med = (pjsua_call_media*) user_data; + const char *pkt = "Not RTP packet"; + + if (!call_med->call || !call_med->call->inv || !call_med->tp) { + /* Call has been disconnected. There is race condition here as + * this cb may be called sometime after call has been disconnected */ + return; + } + + pjmedia_transport_send_rtp(call_med->tp, pkt, strlen(pkt)); + + pjsua_schedule_timer2(&timer_to_send_vid_rtp, call_med, 2000); +} + +static void timer_to_send_vid_rtcp(void *user_data) +{ + pjsua_call_media *call_med = (pjsua_call_media*) user_data; + const char *pkt = "Not RTCP packet"; + + if (!call_med->call || !call_med->call->inv || !call_med->tp) { + /* Call has been disconnected. There is race condition here as + * this cb may be called sometime after call has been disconnected */ + return; + } + + pjmedia_transport_send_rtcp(call_med->tp, pkt, strlen(pkt)); + + pjsua_schedule_timer2(&timer_to_send_vid_rtcp, call_med, 5000); +} + +/* update video channel after SDP negotiation */ +pj_status_t pjsua_vid_channel_update(pjsua_call_media *call_med, + pj_pool_t *tmp_pool, + pjmedia_vid_stream_info *si, + const pjmedia_sdp_session *local_sdp, + const pjmedia_sdp_session *remote_sdp) +{ + pj_status_t status; + + PJ_LOG(4,(THIS_FILE, "Video channel update..")); + pj_log_push_indent(); + + /* Check if no media is active */ + if (si->dir != PJMEDIA_DIR_NONE) { + /* Attach our RTP and RTCP callbacks to the media transport */ + status = pjmedia_transport_attach(call_med->tp, call_med, + &si->rem_addr, &si->rem_rtcp, + pj_sockaddr_get_len(&si->rem_addr), + &vid_rtp_cb, &vid_rtcp_cb); + /* + * TODO: + * - Create and start your video stream based on the parameters + * in si + */ + + /* For a demonstration, let's use a timer to send "RTP" packet + * periodically. + */ + pjsua_schedule_timer2(&timer_to_send_vid_rtp, call_med, 1000); + pjsua_schedule_timer2(&timer_to_send_vid_rtcp, call_med, 3500); + } + + pj_log_pop_indent(); + return PJ_SUCCESS; +} + + +/***************************************************************************** + * Preview + */ + +PJ_DEF(void) +pjsua_call_vid_strm_op_param_default(pjsua_call_vid_strm_op_param *param) +{ + pj_bzero(param, sizeof(*param)); + param->med_idx = -1; + param->dir = PJMEDIA_DIR_ENCODING_DECODING; + param->cap_dev = PJMEDIA_VID_DEFAULT_CAPTURE_DEV; +} + +PJ_DEF(void) pjsua_vid_preview_param_default(pjsua_vid_preview_param *p) +{ + p->rend_id = PJMEDIA_VID_DEFAULT_RENDER_DEV; + p->show = PJ_TRUE; +} + +PJ_DEF(pjsua_vid_win_id) pjsua_vid_preview_get_win(pjmedia_vid_dev_index id) +{ + UNIMPLEMENTED(pjsua_vid_preview_get_win) + return PJSUA_INVALID_ID; +} + +/* Reset internal window structure. Not sure if this is needed?. */ +PJ_DEF(void) pjsua_vid_win_reset(pjsua_vid_win_id wid) +{ + pjsua_vid_win *w = &pjsua_var.win[wid]; + pj_pool_t *pool = w->pool; + + pj_bzero(w, sizeof(*w)); + if (pool) pj_pool_reset(pool); + w->ref_cnt = 0; + w->pool = pool; + w->preview_cap_id = PJMEDIA_VID_INVALID_DEV; +} + +/* Does it have built-in preview support. */ +PJ_DEF(pj_bool_t) pjsua_vid_preview_has_native(pjmedia_vid_dev_index id) +{ + UNIMPLEMENTED(pjsua_vid_preview_has_native) + return PJ_FALSE; +} + +/* Start video preview window for the specified capture device. */ +PJ_DEF(pj_status_t) pjsua_vid_preview_start(pjmedia_vid_dev_index id, + const pjsua_vid_preview_param *prm) +{ + UNIMPLEMENTED(pjsua_vid_preview_start) + return PJ_ENOTSUP; +} + +/* Stop video preview. */ +PJ_DEF(pj_status_t) pjsua_vid_preview_stop(pjmedia_vid_dev_index id) +{ + UNIMPLEMENTED(pjsua_vid_preview_stop) + return PJ_ENOTSUP; +} + + +/***************************************************************************** + * Devices. + */ + +/* Get the number of video devices installed in the system. */ +PJ_DEF(unsigned) pjsua_vid_dev_count(void) +{ + UNIMPLEMENTED(pjsua_vid_dev_count) + return 0; +} + +/* Retrieve the video device info for the specified device index. */ +PJ_DEF(pj_status_t) pjsua_vid_dev_get_info(pjmedia_vid_dev_index id, + pjmedia_vid_dev_info *vdi) +{ + UNIMPLEMENTED(pjsua_vid_dev_get_info) + return PJ_ENOTSUP; +} + +/* Enum all video devices installed in the system. */ +PJ_DEF(pj_status_t) pjsua_vid_enum_devs(pjmedia_vid_dev_info info[], + unsigned *count) +{ + UNIMPLEMENTED(pjsua_vid_enum_devs) + return PJ_ENOTSUP; +} + + +/***************************************************************************** + * Codecs. + */ + +/* Enum all supported video codecs in the system. */ +PJ_DEF(pj_status_t) pjsua_vid_enum_codecs( pjsua_codec_info id[], + unsigned *p_count ) +{ + pjmedia_vid_codec_info info[32]; + unsigned i, j, count, prio[32]; + pj_status_t status; + + count = PJ_ARRAY_SIZE(info); + status = pjmedia_vid_codec_mgr_enum_codecs(NULL, &count, info, prio); + if (status != PJ_SUCCESS) { + *p_count = 0; + return status; + } + for (i=0, j=0; i