Benny Prijono | bf13fee | 2006-04-20 11:13:32 +0000 | [diff] [blame] | 1 | /* $Id$ */ |
| 2 | /* |
| 3 | * Copyright (C) 2003-2006 Benny Prijono <benny@prijono.org> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 | */ |
| 19 | |
| 20 | |
Benny Prijono | 1ec70b3 | 2006-06-20 15:39:07 +0000 | [diff] [blame] | 21 | /** |
| 22 | * \page page_pjmedia_samples_streamutil_c Samples: Remote Streaming |
| 23 | * |
| 24 | * This example mainly demonstrates how to stream media file to remote |
| 25 | * peer using RTP. |
| 26 | * |
| 27 | * This file is pjsip-apps/src/samples/streamutil.c |
| 28 | * |
| 29 | * \includelineno streamutil.c |
| 30 | */ |
| 31 | |
| 32 | |
Benny Prijono | bf13fee | 2006-04-20 11:13:32 +0000 | [diff] [blame] | 33 | static const char *desc = |
Benny Prijono | 6d7a45f | 2006-04-24 23:13:00 +0000 | [diff] [blame] | 34 | " streamutil \n" |
Benny Prijono | bf13fee | 2006-04-20 11:13:32 +0000 | [diff] [blame] | 35 | " \n" |
| 36 | " PURPOSE: \n" |
| 37 | " Demonstrate how to use pjmedia stream component to transmit/receive \n" |
| 38 | " RTP packets to/from sound device. \n" |
| 39 | "\n" |
| 40 | "\n" |
| 41 | " USAGE: \n" |
Benny Prijono | 6d7a45f | 2006-04-24 23:13:00 +0000 | [diff] [blame] | 42 | " streamutil [options] \n" |
Benny Prijono | bf13fee | 2006-04-20 11:13:32 +0000 | [diff] [blame] | 43 | "\n" |
| 44 | "\n" |
| 45 | " Options:\n" |
Benny Prijono | 1595301 | 2006-04-27 22:37:08 +0000 | [diff] [blame] | 46 | " --codec=CODEC Set the codec name. \n" |
Benny Prijono | bf13fee | 2006-04-20 11:13:32 +0000 | [diff] [blame] | 47 | " --local-port=PORT Set local RTP port (default=4000) \n" |
| 48 | " --remote=IP:PORT Set the remote peer. If this option is set, \n" |
| 49 | " the program will transmit RTP audio to the \n" |
| 50 | " specified address. (default: recv only) \n" |
| 51 | " --play-file=WAV Send audio from the WAV file instead of from \n" |
| 52 | " the sound device. \n" |
| 53 | // " --record-file=WAV Record incoming audio to WAV file instead of \n" |
| 54 | // " playing it to sound device. \n" |
| 55 | " --send-recv Set stream direction to bidirectional. \n" |
| 56 | " --send-only Set stream direction to send only \n" |
| 57 | " --recv-only Set stream direction to recv only (default) \n" |
| 58 | "\n" |
| 59 | ; |
| 60 | |
| 61 | |
| 62 | |
| 63 | #include <pjlib.h> |
| 64 | #include <pjlib-util.h> |
| 65 | #include <pjmedia.h> |
| 66 | #include <pjmedia-codec.h> |
| 67 | |
| 68 | #include <stdlib.h> /* atoi() */ |
| 69 | #include <stdio.h> |
| 70 | |
| 71 | #include "util.h" |
| 72 | |
| 73 | |
| 74 | #define THIS_FILE "stream.c" |
| 75 | |
| 76 | |
Benny Prijono | bf13fee | 2006-04-20 11:13:32 +0000 | [diff] [blame] | 77 | |
| 78 | /* Prototype */ |
| 79 | static void print_stream_stat(pjmedia_stream *stream); |
| 80 | |
| 81 | |
| 82 | /* |
| 83 | * Register all codecs. |
| 84 | */ |
| 85 | static pj_status_t init_codecs(pjmedia_endpt *med_endpt) |
| 86 | { |
| 87 | pj_status_t status; |
| 88 | |
Benny Prijono | f996213 | 2006-05-16 13:20:00 +0000 | [diff] [blame] | 89 | #if defined(PJMEDIA_HAS_G711_CODEC) && PJMEDIA_HAS_G711_CODEC!=0 |
Benny Prijono | bf13fee | 2006-04-20 11:13:32 +0000 | [diff] [blame] | 90 | status = pjmedia_codec_g711_init(med_endpt); |
| 91 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, status); |
Benny Prijono | f996213 | 2006-05-16 13:20:00 +0000 | [diff] [blame] | 92 | #endif |
Benny Prijono | bf13fee | 2006-04-20 11:13:32 +0000 | [diff] [blame] | 93 | |
Benny Prijono | f996213 | 2006-05-16 13:20:00 +0000 | [diff] [blame] | 94 | #if defined(PJMEDIA_HAS_GSM_CODEC) && PJMEDIA_HAS_GSM_CODEC!=0 |
Benny Prijono | bf13fee | 2006-04-20 11:13:32 +0000 | [diff] [blame] | 95 | status = pjmedia_codec_gsm_init(med_endpt); |
| 96 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, status); |
Benny Prijono | f996213 | 2006-05-16 13:20:00 +0000 | [diff] [blame] | 97 | #endif |
Benny Prijono | bf13fee | 2006-04-20 11:13:32 +0000 | [diff] [blame] | 98 | |
Benny Prijono | f996213 | 2006-05-16 13:20:00 +0000 | [diff] [blame] | 99 | #if defined(PJMEDIA_HAS_SPEEX_CODEC) && PJMEDIA_HAS_SPEEX_CODEC!=0 |
Benny Prijono | bf13fee | 2006-04-20 11:13:32 +0000 | [diff] [blame] | 100 | status = pjmedia_codec_speex_init(med_endpt, 0, -1, -1); |
| 101 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, status); |
Benny Prijono | f996213 | 2006-05-16 13:20:00 +0000 | [diff] [blame] | 102 | #endif |
Benny Prijono | bf13fee | 2006-04-20 11:13:32 +0000 | [diff] [blame] | 103 | |
Benny Prijono | f996213 | 2006-05-16 13:20:00 +0000 | [diff] [blame] | 104 | #if defined(PJMEDIA_HAS_L16_CODEC) && PJMEDIA_HAS_L16_CODEC!=0 |
Benny Prijono | 1595301 | 2006-04-27 22:37:08 +0000 | [diff] [blame] | 105 | status = pjmedia_codec_l16_init(med_endpt, 0); |
| 106 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, status); |
Benny Prijono | f996213 | 2006-05-16 13:20:00 +0000 | [diff] [blame] | 107 | #endif |
Benny Prijono | 1595301 | 2006-04-27 22:37:08 +0000 | [diff] [blame] | 108 | |
Benny Prijono | bf13fee | 2006-04-20 11:13:32 +0000 | [diff] [blame] | 109 | return PJ_SUCCESS; |
| 110 | } |
| 111 | |
| 112 | |
| 113 | /* |
| 114 | * Create stream based on the codec, dir, remote address, etc. |
| 115 | */ |
| 116 | static pj_status_t create_stream( pj_pool_t *pool, |
| 117 | pjmedia_endpt *med_endpt, |
Benny Prijono | 1595301 | 2006-04-27 22:37:08 +0000 | [diff] [blame] | 118 | const pjmedia_codec_info *codec_info, |
Benny Prijono | bf13fee | 2006-04-20 11:13:32 +0000 | [diff] [blame] | 119 | pjmedia_dir dir, |
| 120 | pj_uint16_t local_port, |
| 121 | const pj_sockaddr_in *rem_addr, |
| 122 | pjmedia_stream **p_stream ) |
| 123 | { |
| 124 | pjmedia_stream_info info; |
Benny Prijono | b04c9e0 | 2006-05-17 17:17:39 +0000 | [diff] [blame] | 125 | pjmedia_transport *transport; |
Benny Prijono | bf13fee | 2006-04-20 11:13:32 +0000 | [diff] [blame] | 126 | pj_status_t status; |
| 127 | |
| 128 | |
| 129 | /* Reset stream info. */ |
| 130 | pj_memset(&info, 0, sizeof(info)); |
| 131 | |
| 132 | |
| 133 | /* Initialize stream info formats */ |
| 134 | info.type = PJMEDIA_TYPE_AUDIO; |
| 135 | info.dir = dir; |
Benny Prijono | 1595301 | 2006-04-27 22:37:08 +0000 | [diff] [blame] | 136 | pj_memcpy(&info.fmt, codec_info, sizeof(pjmedia_codec_info)); |
| 137 | info.tx_pt = codec_info->pt; |
Benny Prijono | bf13fee | 2006-04-20 11:13:32 +0000 | [diff] [blame] | 138 | info.ssrc = pj_rand(); |
| 139 | |
| 140 | |
| 141 | /* Copy remote address */ |
| 142 | pj_memcpy(&info.rem_addr, rem_addr, sizeof(pj_sockaddr_in)); |
| 143 | |
| 144 | |
Benny Prijono | b04c9e0 | 2006-05-17 17:17:39 +0000 | [diff] [blame] | 145 | /* Create media transport */ |
| 146 | status = pjmedia_transport_udp_create(med_endpt, NULL, local_port, |
Benny Prijono | 30c658b | 2006-06-01 11:37:30 +0000 | [diff] [blame] | 147 | 0, &transport); |
Benny Prijono | b04c9e0 | 2006-05-17 17:17:39 +0000 | [diff] [blame] | 148 | if (status != PJ_SUCCESS) |
Benny Prijono | bf13fee | 2006-04-20 11:13:32 +0000 | [diff] [blame] | 149 | return status; |
Benny Prijono | bf13fee | 2006-04-20 11:13:32 +0000 | [diff] [blame] | 150 | |
| 151 | |
| 152 | /* Now that the stream info is initialized, we can create the |
| 153 | * stream. |
| 154 | */ |
| 155 | |
Benny Prijono | b04c9e0 | 2006-05-17 17:17:39 +0000 | [diff] [blame] | 156 | status = pjmedia_stream_create( med_endpt, pool, &info, |
| 157 | transport, NULL, p_stream); |
Benny Prijono | bf13fee | 2006-04-20 11:13:32 +0000 | [diff] [blame] | 158 | |
| 159 | if (status != PJ_SUCCESS) { |
| 160 | app_perror(THIS_FILE, "Error creating stream", status); |
Benny Prijono | b04c9e0 | 2006-05-17 17:17:39 +0000 | [diff] [blame] | 161 | pjmedia_transport_udp_close(transport); |
Benny Prijono | bf13fee | 2006-04-20 11:13:32 +0000 | [diff] [blame] | 162 | return status; |
| 163 | } |
| 164 | |
| 165 | |
| 166 | return PJ_SUCCESS; |
| 167 | } |
| 168 | |
| 169 | |
| 170 | /* |
Benny Prijono | 1595301 | 2006-04-27 22:37:08 +0000 | [diff] [blame] | 171 | * usage() |
| 172 | */ |
| 173 | static void usage() |
| 174 | { |
| 175 | puts(desc); |
| 176 | } |
| 177 | |
| 178 | /* |
Benny Prijono | bf13fee | 2006-04-20 11:13:32 +0000 | [diff] [blame] | 179 | * main() |
| 180 | */ |
| 181 | int main(int argc, char *argv[]) |
| 182 | { |
| 183 | pj_caching_pool cp; |
| 184 | pjmedia_endpt *med_endpt; |
| 185 | pj_pool_t *pool; |
| 186 | pjmedia_port *rec_file_port = NULL, *play_file_port = NULL; |
| 187 | pjmedia_master_port *master_port = NULL; |
| 188 | pjmedia_snd_port *snd_port = NULL; |
| 189 | pjmedia_stream *stream = NULL; |
| 190 | pjmedia_port *stream_port; |
| 191 | char tmp[10]; |
Benny Prijono | b04c9e0 | 2006-05-17 17:17:39 +0000 | [diff] [blame] | 192 | pj_status_t status; |
Benny Prijono | bf13fee | 2006-04-20 11:13:32 +0000 | [diff] [blame] | 193 | |
| 194 | |
| 195 | /* Default values */ |
Benny Prijono | 1595301 | 2006-04-27 22:37:08 +0000 | [diff] [blame] | 196 | const pjmedia_codec_info *codec_info; |
Benny Prijono | bf13fee | 2006-04-20 11:13:32 +0000 | [diff] [blame] | 197 | pjmedia_dir dir = PJMEDIA_DIR_DECODING; |
| 198 | pj_sockaddr_in remote_addr; |
| 199 | pj_uint16_t local_port = 4000; |
Benny Prijono | 1595301 | 2006-04-27 22:37:08 +0000 | [diff] [blame] | 200 | char *codec_id = NULL; |
Benny Prijono | bf13fee | 2006-04-20 11:13:32 +0000 | [diff] [blame] | 201 | char *rec_file = NULL; |
| 202 | char *play_file = NULL; |
| 203 | |
| 204 | enum { |
| 205 | OPT_CODEC = 'c', |
| 206 | OPT_LOCAL_PORT = 'p', |
| 207 | OPT_REMOTE = 'r', |
| 208 | OPT_PLAY_FILE = 'w', |
| 209 | OPT_RECORD_FILE = 'R', |
| 210 | OPT_SEND_RECV = 'b', |
| 211 | OPT_SEND_ONLY = 's', |
| 212 | OPT_RECV_ONLY = 'i', |
Benny Prijono | 1595301 | 2006-04-27 22:37:08 +0000 | [diff] [blame] | 213 | OPT_HELP = 'h', |
Benny Prijono | bf13fee | 2006-04-20 11:13:32 +0000 | [diff] [blame] | 214 | }; |
| 215 | |
| 216 | struct pj_getopt_option long_options[] = { |
| 217 | { "codec", 1, 0, OPT_CODEC }, |
| 218 | { "local-port", 1, 0, OPT_LOCAL_PORT }, |
| 219 | { "remote", 1, 0, OPT_REMOTE }, |
| 220 | { "play-file", 1, 0, OPT_PLAY_FILE }, |
| 221 | { "record-file", 1, 0, OPT_RECORD_FILE }, |
| 222 | { "send-recv", 0, 0, OPT_SEND_RECV }, |
| 223 | { "send-only", 0, 0, OPT_SEND_ONLY }, |
| 224 | { "recv-only", 0, 0, OPT_RECV_ONLY }, |
Benny Prijono | 1595301 | 2006-04-27 22:37:08 +0000 | [diff] [blame] | 225 | { "help", 0, 0, OPT_HELP }, |
Benny Prijono | bf13fee | 2006-04-20 11:13:32 +0000 | [diff] [blame] | 226 | { NULL, 0, 0, 0 }, |
| 227 | }; |
| 228 | |
| 229 | int c; |
| 230 | int option_index; |
| 231 | |
| 232 | |
| 233 | pj_memset(&remote_addr, 0, sizeof(remote_addr)); |
| 234 | |
| 235 | |
| 236 | /* init PJLIB : */ |
| 237 | status = pj_init(); |
| 238 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); |
| 239 | |
| 240 | |
| 241 | /* Parse arguments */ |
| 242 | pj_optind = 0; |
Benny Prijono | 1595301 | 2006-04-27 22:37:08 +0000 | [diff] [blame] | 243 | while((c=pj_getopt_long(argc,argv, "h", long_options, &option_index))!=-1) { |
Benny Prijono | bf13fee | 2006-04-20 11:13:32 +0000 | [diff] [blame] | 244 | |
| 245 | switch (c) { |
| 246 | case OPT_CODEC: |
Benny Prijono | 1595301 | 2006-04-27 22:37:08 +0000 | [diff] [blame] | 247 | codec_id = pj_optarg; |
Benny Prijono | bf13fee | 2006-04-20 11:13:32 +0000 | [diff] [blame] | 248 | break; |
| 249 | |
| 250 | case OPT_LOCAL_PORT: |
| 251 | local_port = (pj_uint16_t) atoi(pj_optarg); |
| 252 | if (local_port < 1) { |
| 253 | printf("Error: invalid local port %s\n", pj_optarg); |
| 254 | return 1; |
| 255 | } |
| 256 | break; |
| 257 | |
| 258 | case OPT_REMOTE: |
| 259 | { |
| 260 | pj_str_t ip = pj_str(strtok(pj_optarg, ":")); |
| 261 | pj_uint16_t port = (pj_uint16_t) atoi(strtok(NULL, ":")); |
| 262 | |
| 263 | status = pj_sockaddr_in_init(&remote_addr, &ip, port); |
| 264 | if (status != PJ_SUCCESS) { |
| 265 | app_perror(THIS_FILE, "Invalid remote address", status); |
| 266 | return 1; |
| 267 | } |
| 268 | } |
| 269 | break; |
| 270 | |
| 271 | case OPT_PLAY_FILE: |
| 272 | play_file = pj_optarg; |
| 273 | break; |
| 274 | |
| 275 | case OPT_RECORD_FILE: |
| 276 | rec_file = pj_optarg; |
| 277 | break; |
| 278 | |
| 279 | case OPT_SEND_RECV: |
| 280 | dir = PJMEDIA_DIR_ENCODING_DECODING; |
| 281 | break; |
| 282 | |
| 283 | case OPT_SEND_ONLY: |
| 284 | dir = PJMEDIA_DIR_ENCODING; |
| 285 | break; |
| 286 | |
| 287 | case OPT_RECV_ONLY: |
| 288 | dir = PJMEDIA_DIR_DECODING; |
| 289 | break; |
| 290 | |
Benny Prijono | 1595301 | 2006-04-27 22:37:08 +0000 | [diff] [blame] | 291 | case OPT_HELP: |
| 292 | usage(); |
| 293 | return 1; |
| 294 | |
Benny Prijono | bf13fee | 2006-04-20 11:13:32 +0000 | [diff] [blame] | 295 | default: |
| 296 | printf("Invalid options %s\n", argv[pj_optind]); |
| 297 | return 1; |
| 298 | } |
| 299 | |
| 300 | } |
| 301 | |
| 302 | |
| 303 | /* Verify arguments. */ |
| 304 | if (dir & PJMEDIA_DIR_ENCODING) { |
| 305 | if (remote_addr.sin_addr.s_addr == 0) { |
| 306 | printf("Error: remote address must be set\n"); |
| 307 | return 1; |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | if (play_file != NULL && dir != PJMEDIA_DIR_ENCODING) { |
| 312 | printf("Direction is set to --send-only because of --play-file\n"); |
| 313 | dir = PJMEDIA_DIR_ENCODING; |
| 314 | } |
| 315 | |
| 316 | |
| 317 | /* Must create a pool factory before we can allocate any memory. */ |
| 318 | pj_caching_pool_init(&cp, &pj_pool_factory_default_policy, 0); |
| 319 | |
| 320 | /* |
| 321 | * Initialize media endpoint. |
| 322 | * This will implicitly initialize PJMEDIA too. |
| 323 | */ |
| 324 | status = pjmedia_endpt_create(&cp.factory, NULL, 1, &med_endpt); |
| 325 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); |
| 326 | |
| 327 | /* Create memory pool for application purpose */ |
| 328 | pool = pj_pool_create( &cp.factory, /* pool factory */ |
| 329 | "app", /* pool name. */ |
| 330 | 4000, /* init size */ |
| 331 | 4000, /* increment size */ |
| 332 | NULL /* callback on error */ |
| 333 | ); |
| 334 | |
| 335 | |
| 336 | /* Register all supported codecs */ |
| 337 | status = init_codecs(med_endpt); |
| 338 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); |
| 339 | |
| 340 | |
Benny Prijono | 1595301 | 2006-04-27 22:37:08 +0000 | [diff] [blame] | 341 | /* Find which codec to use. */ |
| 342 | if (codec_id) { |
| 343 | unsigned count = 1; |
| 344 | pj_str_t str_codec_id = pj_str(codec_id); |
| 345 | pjmedia_codec_mgr *codec_mgr = pjmedia_endpt_get_codec_mgr(med_endpt); |
| 346 | status = pjmedia_codec_mgr_find_codecs_by_id( codec_mgr, |
| 347 | &str_codec_id, &count, |
| 348 | &codec_info, NULL); |
| 349 | if (status != PJ_SUCCESS) { |
| 350 | printf("Error: unable to find codec %s\n", codec_id); |
| 351 | return 1; |
| 352 | } |
| 353 | } else { |
| 354 | /* Default to pcmu */ |
| 355 | pjmedia_codec_mgr_get_codec_info( pjmedia_endpt_get_codec_mgr(med_endpt), |
| 356 | 0, &codec_info); |
| 357 | } |
| 358 | |
Benny Prijono | bf13fee | 2006-04-20 11:13:32 +0000 | [diff] [blame] | 359 | /* Create stream based on program arguments */ |
Benny Prijono | 1595301 | 2006-04-27 22:37:08 +0000 | [diff] [blame] | 360 | status = create_stream(pool, med_endpt, codec_info, dir, local_port, |
Benny Prijono | bf13fee | 2006-04-20 11:13:32 +0000 | [diff] [blame] | 361 | &remote_addr, &stream); |
| 362 | if (status != PJ_SUCCESS) |
| 363 | goto on_exit; |
| 364 | |
| 365 | |
| 366 | /* Get the port interface of the stream */ |
| 367 | status = pjmedia_stream_get_port( stream, &stream_port); |
| 368 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); |
| 369 | |
| 370 | |
| 371 | if (play_file) { |
Benny Prijono | 1595301 | 2006-04-27 22:37:08 +0000 | [diff] [blame] | 372 | unsigned wav_ptime; |
Benny Prijono | bf13fee | 2006-04-20 11:13:32 +0000 | [diff] [blame] | 373 | |
Benny Prijono | 1595301 | 2006-04-27 22:37:08 +0000 | [diff] [blame] | 374 | wav_ptime = stream_port->info.samples_per_frame * 1000 / |
| 375 | stream_port->info.clock_rate; |
| 376 | status = pjmedia_wav_player_port_create(pool, play_file, wav_ptime, |
Benny Prijono | 6fd4b8f | 2006-06-22 18:51:50 +0000 | [diff] [blame] | 377 | 0, -1, &play_file_port); |
Benny Prijono | bf13fee | 2006-04-20 11:13:32 +0000 | [diff] [blame] | 378 | if (status != PJ_SUCCESS) { |
| 379 | app_perror(THIS_FILE, "Unable to use file", status); |
| 380 | goto on_exit; |
| 381 | } |
| 382 | |
| 383 | status = pjmedia_master_port_create(pool, play_file_port, stream_port, |
| 384 | 0, &master_port); |
| 385 | if (status != PJ_SUCCESS) { |
| 386 | app_perror(THIS_FILE, "Unable to create master port", status); |
| 387 | goto on_exit; |
| 388 | } |
| 389 | |
| 390 | status = pjmedia_master_port_start(master_port); |
| 391 | if (status != PJ_SUCCESS) { |
| 392 | app_perror(THIS_FILE, "Error starting master port", status); |
| 393 | goto on_exit; |
| 394 | } |
| 395 | |
| 396 | |
| 397 | } else { |
| 398 | |
| 399 | /* Create sound device port. */ |
| 400 | if (dir == PJMEDIA_DIR_ENCODING_DECODING) |
| 401 | status = pjmedia_snd_port_create(pool, -1, -1, |
Benny Prijono | 1595301 | 2006-04-27 22:37:08 +0000 | [diff] [blame] | 402 | stream_port->info.clock_rate, |
Benny Prijono | bf13fee | 2006-04-20 11:13:32 +0000 | [diff] [blame] | 403 | stream_port->info.channel_count, |
| 404 | stream_port->info.samples_per_frame, |
| 405 | stream_port->info.bits_per_sample, |
| 406 | 0, &snd_port); |
| 407 | else if (dir == PJMEDIA_DIR_ENCODING) |
| 408 | status = pjmedia_snd_port_create_rec(pool, -1, |
Benny Prijono | 1595301 | 2006-04-27 22:37:08 +0000 | [diff] [blame] | 409 | stream_port->info.clock_rate, |
Benny Prijono | bf13fee | 2006-04-20 11:13:32 +0000 | [diff] [blame] | 410 | stream_port->info.channel_count, |
| 411 | stream_port->info.samples_per_frame, |
| 412 | stream_port->info.bits_per_sample, |
| 413 | 0, &snd_port); |
| 414 | else |
| 415 | status = pjmedia_snd_port_create_player(pool, -1, |
Benny Prijono | 1595301 | 2006-04-27 22:37:08 +0000 | [diff] [blame] | 416 | stream_port->info.clock_rate, |
Benny Prijono | bf13fee | 2006-04-20 11:13:32 +0000 | [diff] [blame] | 417 | stream_port->info.channel_count, |
| 418 | stream_port->info.samples_per_frame, |
| 419 | stream_port->info.bits_per_sample, |
| 420 | 0, &snd_port); |
| 421 | |
| 422 | |
| 423 | if (status != PJ_SUCCESS) { |
| 424 | app_perror(THIS_FILE, "Unable to create sound port", status); |
| 425 | goto on_exit; |
| 426 | } |
| 427 | |
| 428 | /* Connect sound port to stream */ |
| 429 | status = pjmedia_snd_port_connect( snd_port, stream_port ); |
| 430 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); |
| 431 | |
| 432 | } |
| 433 | |
Benny Prijono | 69d9d19 | 2006-05-21 19:00:28 +0000 | [diff] [blame] | 434 | /* Start streaming */ |
| 435 | pjmedia_stream_start(stream); |
| 436 | |
Benny Prijono | bf13fee | 2006-04-20 11:13:32 +0000 | [diff] [blame] | 437 | |
| 438 | /* Done */ |
| 439 | |
| 440 | if (dir == PJMEDIA_DIR_DECODING) |
| 441 | printf("Stream is active, dir is recv-only, local port is %d\n", |
| 442 | local_port); |
| 443 | else if (dir == PJMEDIA_DIR_ENCODING) |
| 444 | printf("Stream is active, dir is send-only, sending to %s:%d\n", |
| 445 | pj_inet_ntoa(remote_addr.sin_addr), |
| 446 | pj_ntohs(remote_addr.sin_port)); |
| 447 | else |
| 448 | printf("Stream is active, send/recv, local port is %d, " |
| 449 | "sending to %s:%d\n", |
| 450 | local_port, |
| 451 | pj_inet_ntoa(remote_addr.sin_addr), |
| 452 | pj_ntohs(remote_addr.sin_port)); |
| 453 | |
| 454 | |
| 455 | for (;;) { |
| 456 | |
| 457 | puts(""); |
| 458 | puts("Commands:"); |
| 459 | puts(" s Display media statistics"); |
| 460 | puts(" q Quit"); |
| 461 | puts(""); |
| 462 | |
| 463 | printf("Command: "); fflush(stdout); |
| 464 | |
| 465 | fgets(tmp, sizeof(tmp), stdin); |
| 466 | |
| 467 | if (tmp[0] == 's') |
| 468 | print_stream_stat(stream); |
| 469 | else if (tmp[0] == 'q') |
| 470 | break; |
| 471 | |
| 472 | } |
| 473 | |
| 474 | |
| 475 | |
| 476 | /* Start deinitialization: */ |
| 477 | on_exit: |
| 478 | |
| 479 | /* Destroy sound device */ |
| 480 | if (snd_port) { |
| 481 | pjmedia_snd_port_destroy( snd_port ); |
| 482 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); |
| 483 | } |
| 484 | |
| 485 | /* If there is master port, then we just need to destroy master port |
| 486 | * (it will recursively destroy upstream and downstream ports, which |
| 487 | * in this case are file_port and stream_port). |
| 488 | */ |
| 489 | if (master_port) { |
Benny Prijono | 22a300a | 2006-06-14 20:04:55 +0000 | [diff] [blame] | 490 | pjmedia_master_port_destroy(master_port, PJ_TRUE); |
Benny Prijono | bf13fee | 2006-04-20 11:13:32 +0000 | [diff] [blame] | 491 | play_file_port = NULL; |
| 492 | stream = NULL; |
| 493 | } |
| 494 | |
| 495 | /* Destroy stream */ |
| 496 | if (stream) { |
Benny Prijono | b04c9e0 | 2006-05-17 17:17:39 +0000 | [diff] [blame] | 497 | pjmedia_transport *tp; |
| 498 | |
| 499 | tp = pjmedia_stream_get_transport(stream); |
Benny Prijono | bf13fee | 2006-04-20 11:13:32 +0000 | [diff] [blame] | 500 | pjmedia_stream_destroy(stream); |
Benny Prijono | b04c9e0 | 2006-05-17 17:17:39 +0000 | [diff] [blame] | 501 | pjmedia_transport_udp_close(tp); |
Benny Prijono | bf13fee | 2006-04-20 11:13:32 +0000 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | /* Destroy file ports */ |
| 505 | if (play_file_port) |
| 506 | pjmedia_port_destroy( play_file_port ); |
| 507 | if (rec_file_port) |
| 508 | pjmedia_port_destroy( rec_file_port ); |
| 509 | |
| 510 | |
| 511 | /* Release application pool */ |
| 512 | pj_pool_release( pool ); |
| 513 | |
| 514 | /* Destroy media endpoint. */ |
| 515 | pjmedia_endpt_destroy( med_endpt ); |
| 516 | |
| 517 | /* Destroy pool factory */ |
| 518 | pj_caching_pool_destroy( &cp ); |
| 519 | |
| 520 | |
| 521 | return (status == PJ_SUCCESS) ? 0 : 1; |
| 522 | } |
| 523 | |
| 524 | |
| 525 | |
| 526 | |
| 527 | static const char *good_number(char *buf, pj_int32_t val) |
| 528 | { |
| 529 | if (val < 1000) { |
| 530 | pj_ansi_sprintf(buf, "%d", val); |
| 531 | } else if (val < 1000000) { |
| 532 | pj_ansi_sprintf(buf, "%d.%dK", |
| 533 | val / 1000, |
| 534 | (val % 1000) / 100); |
| 535 | } else { |
| 536 | pj_ansi_sprintf(buf, "%d.%02dM", |
| 537 | val / 1000000, |
| 538 | (val % 1000000) / 10000); |
| 539 | } |
| 540 | |
| 541 | return buf; |
| 542 | } |
| 543 | |
| 544 | |
| 545 | |
| 546 | /* |
| 547 | * Print stream statistics |
| 548 | */ |
| 549 | static void print_stream_stat(pjmedia_stream *stream) |
| 550 | { |
| 551 | char duration[80], last_update[80]; |
| 552 | char bps[16], ipbps[16], packets[16], bytes[16], ipbytes[16]; |
| 553 | pjmedia_port *port; |
| 554 | pjmedia_rtcp_stat stat; |
| 555 | pj_time_val now; |
| 556 | |
| 557 | |
| 558 | pj_gettimeofday(&now); |
| 559 | pjmedia_stream_get_stat(stream, &stat); |
| 560 | pjmedia_stream_get_port(stream, &port); |
| 561 | |
| 562 | puts("Stream statistics:"); |
| 563 | |
| 564 | /* Print duration */ |
| 565 | PJ_TIME_VAL_SUB(now, stat.start); |
| 566 | sprintf(duration, " Duration: %02ld:%02ld:%02ld.%03ld", |
| 567 | now.sec / 3600, |
| 568 | (now.sec % 3600) / 60, |
| 569 | (now.sec % 60), |
| 570 | now.msec); |
| 571 | |
| 572 | |
| 573 | printf(" Info: audio %.*s@%dHz, %dms/frame, %sB/s (%sB/s +IP hdr)\n", |
| 574 | (int)port->info.encoding_name.slen, |
| 575 | port->info.encoding_name.ptr, |
Benny Prijono | 1595301 | 2006-04-27 22:37:08 +0000 | [diff] [blame] | 576 | port->info.clock_rate, |
| 577 | port->info.samples_per_frame * 1000 / port->info.clock_rate, |
| 578 | good_number(bps, port->info.bytes_per_frame * port->info.clock_rate / |
| 579 | port->info.samples_per_frame), |
Benny Prijono | bf13fee | 2006-04-20 11:13:32 +0000 | [diff] [blame] | 580 | good_number(ipbps, (port->info.bytes_per_frame+32) * |
Benny Prijono | 1595301 | 2006-04-27 22:37:08 +0000 | [diff] [blame] | 581 | port->info.clock_rate / port->info.clock_rate)); |
Benny Prijono | bf13fee | 2006-04-20 11:13:32 +0000 | [diff] [blame] | 582 | |
| 583 | if (stat.rx.update_cnt == 0) |
| 584 | strcpy(last_update, "never"); |
| 585 | else { |
| 586 | pj_gettimeofday(&now); |
| 587 | PJ_TIME_VAL_SUB(now, stat.rx.update); |
| 588 | sprintf(last_update, "%02ldh:%02ldm:%02ld.%03lds ago", |
| 589 | now.sec / 3600, |
| 590 | (now.sec % 3600) / 60, |
| 591 | now.sec % 60, |
| 592 | now.msec); |
| 593 | } |
| 594 | |
| 595 | printf(" RX stat last update: %s\n" |
| 596 | " total %s packets %sB received (%sB +IP hdr)%s\n" |
| 597 | " pkt loss=%d (%3.1f%%), dup=%d (%3.1f%%), reorder=%d (%3.1f%%)%s\n" |
| 598 | " (msec) min avg max last\n" |
| 599 | " loss period: %7.3f %7.3f %7.3f %7.3f%s\n" |
| 600 | " jitter : %7.3f %7.3f %7.3f %7.3f%s\n", |
| 601 | last_update, |
| 602 | good_number(packets, stat.rx.pkt), |
| 603 | good_number(bytes, stat.rx.bytes), |
| 604 | good_number(ipbytes, stat.rx.bytes + stat.rx.pkt * 32), |
| 605 | "", |
| 606 | stat.rx.loss, |
Benny Prijono | f901651 | 2006-06-29 09:41:34 +0000 | [diff] [blame] | 607 | stat.rx.loss * 100.0 / (stat.rx.pkt + stat.rx.loss), |
Benny Prijono | bf13fee | 2006-04-20 11:13:32 +0000 | [diff] [blame] | 608 | stat.rx.dup, |
Benny Prijono | b12106f | 2006-06-29 14:45:17 +0000 | [diff] [blame^] | 609 | stat.rx.dup * 100.0 / (stat.rx.pkt + stat.rx.loss), |
Benny Prijono | bf13fee | 2006-04-20 11:13:32 +0000 | [diff] [blame] | 610 | stat.rx.reorder, |
Benny Prijono | b12106f | 2006-06-29 14:45:17 +0000 | [diff] [blame^] | 611 | stat.rx.reorder * 100.0 / (stat.rx.pkt + stat.rx.loss), |
Benny Prijono | bf13fee | 2006-04-20 11:13:32 +0000 | [diff] [blame] | 612 | "", |
| 613 | stat.rx.loss_period.min / 1000.0, |
| 614 | stat.rx.loss_period.avg / 1000.0, |
| 615 | stat.rx.loss_period.max / 1000.0, |
| 616 | stat.rx.loss_period.last / 1000.0, |
| 617 | "", |
| 618 | stat.rx.jitter.min / 1000.0, |
| 619 | stat.rx.jitter.avg / 1000.0, |
| 620 | stat.rx.jitter.max / 1000.0, |
| 621 | stat.rx.jitter.last / 1000.0, |
| 622 | "" |
| 623 | ); |
| 624 | |
| 625 | |
| 626 | if (stat.tx.update_cnt == 0) |
| 627 | strcpy(last_update, "never"); |
| 628 | else { |
| 629 | pj_gettimeofday(&now); |
| 630 | PJ_TIME_VAL_SUB(now, stat.tx.update); |
| 631 | sprintf(last_update, "%02ldh:%02ldm:%02ld.%03lds ago", |
| 632 | now.sec / 3600, |
| 633 | (now.sec % 3600) / 60, |
| 634 | now.sec % 60, |
| 635 | now.msec); |
| 636 | } |
| 637 | |
| 638 | printf(" TX stat last update: %s\n" |
| 639 | " total %s packets %sB sent (%sB +IP hdr)%s\n" |
| 640 | " pkt loss=%d (%3.1f%%), dup=%d (%3.1f%%), reorder=%d (%3.1f%%)%s\n" |
| 641 | " (msec) min avg max last\n" |
| 642 | " loss period: %7.3f %7.3f %7.3f %7.3f%s\n" |
| 643 | " jitter : %7.3f %7.3f %7.3f %7.3f%s\n", |
| 644 | last_update, |
| 645 | good_number(packets, stat.tx.pkt), |
| 646 | good_number(bytes, stat.tx.bytes), |
| 647 | good_number(ipbytes, stat.tx.bytes + stat.tx.pkt * 32), |
| 648 | "", |
| 649 | stat.tx.loss, |
Benny Prijono | f901651 | 2006-06-29 09:41:34 +0000 | [diff] [blame] | 650 | stat.tx.loss * 100.0 / (stat.tx.pkt + stat.tx.loss), |
Benny Prijono | bf13fee | 2006-04-20 11:13:32 +0000 | [diff] [blame] | 651 | stat.tx.dup, |
Benny Prijono | b12106f | 2006-06-29 14:45:17 +0000 | [diff] [blame^] | 652 | stat.tx.dup * 100.0 / (stat.tx.pkt + stat.tx.loss), |
Benny Prijono | bf13fee | 2006-04-20 11:13:32 +0000 | [diff] [blame] | 653 | stat.tx.reorder, |
Benny Prijono | b12106f | 2006-06-29 14:45:17 +0000 | [diff] [blame^] | 654 | stat.tx.reorder * 100.0 / (stat.tx.pkt + stat.tx.loss), |
Benny Prijono | bf13fee | 2006-04-20 11:13:32 +0000 | [diff] [blame] | 655 | "", |
| 656 | stat.tx.loss_period.min / 1000.0, |
| 657 | stat.tx.loss_period.avg / 1000.0, |
| 658 | stat.tx.loss_period.max / 1000.0, |
| 659 | stat.tx.loss_period.last / 1000.0, |
| 660 | "", |
| 661 | stat.tx.jitter.min / 1000.0, |
| 662 | stat.tx.jitter.avg / 1000.0, |
| 663 | stat.tx.jitter.max / 1000.0, |
| 664 | stat.tx.jitter.last / 1000.0, |
| 665 | "" |
| 666 | ); |
| 667 | |
| 668 | |
| 669 | printf(" RTT delay : %7.3f %7.3f %7.3f %7.3f%s\n", |
| 670 | stat.rtt.min / 1000.0, |
| 671 | stat.rtt.avg / 1000.0, |
| 672 | stat.rtt.max / 1000.0, |
| 673 | stat.rtt.last / 1000.0, |
| 674 | "" |
| 675 | ); |
| 676 | |
| 677 | } |
| 678 | |