static void destroy_stack(void) { enum { WAIT_CLEAR = 5000, WAIT_INTERVAL = 500 }; unsigned i; PJ_LOG(3,(THIS_FILE, "Shutting down..")); /* Wait until all clear */ hangup_all(); for (i=0; i<WAIT_CLEAR/WAIT_INTERVAL; ++i) { unsigned j; for (j=0; j<MAX_CALLS; ++j) { call_t *call = &app.call[j]; if (call->inv && call->inv->state <= PJSIP_INV_STATE_CONFIRMED) break; } if (j==MAX_CALLS) return; pj_thread_sleep(WAIT_INTERVAL); } app.quit = PJ_TRUE; if (app.worker_thread) { pj_thread_join(app.worker_thread); app.worker_thread = NULL; } //if (app.med_endpt) //pjmedia_endpt_destroy(app.med_endpt); if (app.sip_endpt) pjsip_endpt_destroy(app.sip_endpt); if (app.pool) pj_pool_release(app.pool); dump_pool_usage(THIS_FILE, &app.cp); pj_caching_pool_destroy(&app.cp); }
/* * main() * * If called with argument, treat argument as SIP URL to be called. * Otherwise wait for incoming calls. */ int main(int argc, char *argv[]) { pj_pool_t *pool = NULL; pj_status_t status; unsigned i; /* Must init PJLIB first: */ status = pj_init(); PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); pj_log_set_level(5); /* Then init PJLIB-UTIL: */ status = pjlib_util_init(); PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); /* Must create a pool factory before we can allocate any memory. */ pj_caching_pool_init(&cp, &pj_pool_factory_default_policy, 0); /* Create global endpoint: */ { const pj_str_t *hostname; const char *endpt_name; /* Endpoint MUST be assigned a globally unique name. * The name will be used as the hostname in Warning header. */ /* For this implementation, we'll use hostname for simplicity */ hostname = pj_gethostname(); endpt_name = hostname->ptr; /* Create the endpoint: */ status = pjsip_endpt_create(&cp.factory, endpt_name, &g_endpt); PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); } /* * Add UDP transport, with hard-coded port * Alternatively, application can use pjsip_udp_transport_attach() to * start UDP transport, if it already has an UDP socket (e.g. after it * resolves the address with STUN). */ { pj_sockaddr addr; pj_sockaddr_init(AF, &addr, NULL, (pj_uint16_t)SIP_PORT); if (AF == pj_AF_INET()) { status = pjsip_udp_transport_start( g_endpt, &addr.ipv4, NULL, 1, NULL); } else if (AF == pj_AF_INET6()) { status = pjsip_udp_transport_start6(g_endpt, &addr.ipv6, NULL, 1, NULL); } else { status = PJ_EAFNOTSUP; } if (status != PJ_SUCCESS) { app_perror(THIS_FILE, "Unable to start UDP transport", status); return 1; } } /* * Init transaction layer. * This will create/initialize transaction hash tables etc. */ status = pjsip_tsx_layer_init_module(g_endpt); PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); /* * Initialize UA layer module. * This will create/initialize dialog hash tables etc. */ status = pjsip_ua_init_module( g_endpt, NULL ); PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); /* * Init invite session module. * The invite session module initialization takes additional argument, * i.e. a structure containing callbacks to be called on specific * occurence of events. * * The on_state_changed and on_new_session callbacks are mandatory. * Application must supply the callback function. * * We use on_media_update() callback in this application to start * media transmission. */ { pjsip_inv_callback inv_cb; /* Init the callback for INVITE session: */ pj_bzero(&inv_cb, sizeof(inv_cb)); inv_cb.on_state_changed = &call_on_state_changed; inv_cb.on_new_session = &call_on_forked; inv_cb.on_media_update = &call_on_media_update; /* Initialize invite session module: */ status = pjsip_inv_usage_init(g_endpt, &inv_cb); PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); } /* Initialize 100rel support */ status = pjsip_100rel_init_module(g_endpt); PJ_ASSERT_RETURN(status == PJ_SUCCESS, status); /* * Register our module to receive incoming requests. */ status = pjsip_endpt_register_module( g_endpt, &mod_simpleua); PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); /* * Register message logger module. */ status = pjsip_endpt_register_module( g_endpt, &msg_logger); PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); /* * Initialize media endpoint. * This will implicitly initialize PJMEDIA too. */ #if PJ_HAS_THREADS status = pjmedia_endpt_create(&cp.factory, NULL, 1, &g_med_endpt); #else status = pjmedia_endpt_create(&cp.factory, pjsip_endpt_get_ioqueue(g_endpt), 0, &g_med_endpt); #endif PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); /* * Add PCMA/PCMU codec to the media endpoint. */ #if defined(PJMEDIA_HAS_G711_CODEC) && PJMEDIA_HAS_G711_CODEC!=0 status = pjmedia_codec_g711_init(g_med_endpt); PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); #endif #if defined(PJMEDIA_HAS_VIDEO) && (PJMEDIA_HAS_VIDEO != 0) /* Init video subsystem */ pool = pjmedia_endpt_create_pool(g_med_endpt, "Video subsystem", 512, 512); status = pjmedia_video_format_mgr_create(pool, 64, 0, NULL); PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); status = pjmedia_converter_mgr_create(pool, NULL); PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); status = pjmedia_vid_codec_mgr_create(pool, NULL); PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); status = pjmedia_vid_dev_subsys_init(&cp.factory); PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); # if defined(PJMEDIA_HAS_FFMPEG_VID_CODEC) && PJMEDIA_HAS_FFMPEG_VID_CODEC!=0 /* Init ffmpeg video codecs */ status = pjmedia_codec_ffmpeg_vid_init(NULL, &cp.factory); PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); # endif /* PJMEDIA_HAS_FFMPEG_VID_CODEC */ #endif /* PJMEDIA_HAS_VIDEO */ /* * Create media transport used to send/receive RTP/RTCP socket. * One media transport is needed for each call. Application may * opt to re-use the same media transport for subsequent calls. */ for (i = 0; i < PJ_ARRAY_SIZE(g_med_transport); ++i) { status = pjmedia_transport_udp_create3(g_med_endpt, AF, NULL, NULL, RTP_PORT + i*2, 0, &g_med_transport[i]); if (status != PJ_SUCCESS) { app_perror(THIS_FILE, "Unable to create media transport", status); return 1; } /* * Get socket info (address, port) of the media transport. We will * need this info to create SDP (i.e. the address and port info in * the SDP). */ pjmedia_transport_info_init(&g_med_tpinfo[i]); pjmedia_transport_get_info(g_med_transport[i], &g_med_tpinfo[i]); pj_memcpy(&g_sock_info[i], &g_med_tpinfo[i].sock_info, sizeof(pjmedia_sock_info)); } /* * If URL is specified, then make call immediately. */ if (argc > 1) { pj_sockaddr hostaddr; char hostip[PJ_INET6_ADDRSTRLEN+2]; char temp[80]; pj_str_t dst_uri = pj_str(argv[1]); pj_str_t local_uri; pjsip_dialog *dlg; pjmedia_sdp_session *local_sdp; pjsip_tx_data *tdata; if (pj_gethostip(AF, &hostaddr) != PJ_SUCCESS) { app_perror(THIS_FILE, "Unable to retrieve local host IP", status); return 1; } pj_sockaddr_print(&hostaddr, hostip, sizeof(hostip), 2); pj_ansi_sprintf(temp, "<sip:simpleuac@%s:%d>", hostip, SIP_PORT); local_uri = pj_str(temp); /* Create UAC dialog */ status = pjsip_dlg_create_uac( pjsip_ua_instance(), &local_uri, /* local URI */ &local_uri, /* local Contact */ &dst_uri, /* remote URI */ &dst_uri, /* remote target */ &dlg); /* dialog */ if (status != PJ_SUCCESS) { app_perror(THIS_FILE, "Unable to create UAC dialog", status); return 1; } /* If we expect the outgoing INVITE to be challenged, then we should * put the credentials in the dialog here, with something like this: * { pjsip_cred_info cred[1]; cred[0].realm = pj_str("sip.server.realm"); cred[0].scheme = pj_str("digest"); cred[0].username = pj_str("theuser"); cred[0].data_type = PJSIP_CRED_DATA_PLAIN_PASSWD; cred[0].data = pj_str("thepassword"); pjsip_auth_clt_set_credentials( &dlg->auth_sess, 1, cred); } * */ /* Get the SDP body to be put in the outgoing INVITE, by asking * media endpoint to create one for us. */ status = pjmedia_endpt_create_sdp( g_med_endpt, /* the media endpt */ dlg->pool, /* pool. */ MAX_MEDIA_CNT, /* # of streams */ g_sock_info, /* RTP sock info */ &local_sdp); /* the SDP result */ PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); /* Create the INVITE session, and pass the SDP returned earlier * as the session's initial capability. */ status = pjsip_inv_create_uac( dlg, local_sdp, 0, &g_inv); PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); /* If we want the initial INVITE to travel to specific SIP proxies, * then we should put the initial dialog's route set here. The final * route set will be updated once a dialog has been established. * To set the dialog's initial route set, we do it with something * like this: * { pjsip_route_hdr route_set; pjsip_route_hdr *route; const pj_str_t hname = { "Route", 5 }; char *uri = "sip:proxy.server;lr"; pj_list_init(&route_set); route = pjsip_parse_hdr( dlg->pool, &hname, uri, strlen(uri), NULL); PJ_ASSERT_RETURN(route != NULL, 1); pj_list_push_back(&route_set, route); pjsip_dlg_set_route_set(dlg, &route_set); } * * Note that Route URI SHOULD have an ";lr" parameter! */ /* Create initial INVITE request. * This INVITE request will contain a perfectly good request and * an SDP body as well. */ status = pjsip_inv_invite(g_inv, &tdata); PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); /* Send initial INVITE request. * From now on, the invite session's state will be reported to us * via the invite session callbacks. */ status = pjsip_inv_send_msg(g_inv, tdata); PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); } else { /* No URL to make call to */ PJ_LOG(3,(THIS_FILE, "Ready to accept incoming calls...")); } /* Loop until one call is completed */ for (;!g_complete;) { pj_time_val timeout = {0, 10}; pjsip_endpt_handle_events(g_endpt, &timeout); } /* On exit, dump current memory usage: */ dump_pool_usage(THIS_FILE, &cp); /* Destroy audio ports. Destroy the audio port first * before the stream since the audio port has threads * that get/put frames to the stream. */ if (g_snd_port) pjmedia_snd_port_destroy(g_snd_port); #if defined(PJMEDIA_HAS_VIDEO) && (PJMEDIA_HAS_VIDEO != 0) /* Destroy video ports */ if (g_vid_capturer) pjmedia_vid_port_destroy(g_vid_capturer); if (g_vid_renderer) pjmedia_vid_port_destroy(g_vid_renderer); #endif /* Destroy streams */ if (g_med_stream) pjmedia_stream_destroy(g_med_stream); #if defined(PJMEDIA_HAS_VIDEO) && (PJMEDIA_HAS_VIDEO != 0) if (g_med_vstream) pjmedia_vid_stream_destroy(g_med_vstream); /* Deinit ffmpeg codec */ # if defined(PJMEDIA_HAS_FFMPEG_VID_CODEC) && PJMEDIA_HAS_FFMPEG_VID_CODEC!=0 pjmedia_codec_ffmpeg_vid_deinit(); # endif #endif /* Destroy media transports */ for (i = 0; i < MAX_MEDIA_CNT; ++i) { if (g_med_transport[i]) pjmedia_transport_close(g_med_transport[i]); } /* Deinit pjmedia endpoint */ if (g_med_endpt) pjmedia_endpt_destroy(g_med_endpt); /* Deinit pjsip endpoint */ if (g_endpt) pjsip_endpt_destroy(g_endpt); /* Release pool */ if (pool) pj_pool_release(pool); return 0; }
/***************************************************************************** * main() */ int main(int argc, char *argv[]) { int dev_id = -1; int clock_rate = CLOCK_RATE; int channel_count = NCHANNELS; int samples_per_frame = NSAMPLES; int bits_per_sample = NBITS; pj_caching_pool cp; pjmedia_endpt *med_endpt; pj_pool_t *pool; pjmedia_conf *conf; int i, port_count, file_count; pjmedia_port **file_port; /* Array of file ports */ pjmedia_port *rec_port = NULL; /* Wav writer port */ char tmp[10]; pj_status_t status; /* Must init PJLIB first: */ status = pj_init(); PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); /* Get command line options. */ if (get_snd_options(THIS_FILE, argc, argv, &dev_id, &clock_rate, &channel_count, &samples_per_frame, &bits_per_sample)) { usage(); return 1; } /* Must create a pool factory before we can allocate any memory. */ pj_caching_pool_init(&cp, &pj_pool_factory_default_policy, 0); /* * Initialize media endpoint. * This will implicitly initialize PJMEDIA too. */ status = pjmedia_endpt_create(&cp.factory, NULL, 1, &med_endpt); PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); /* Create memory pool to allocate memory */ pool = pj_pool_create( &cp.factory, /* pool factory */ "wav", /* pool name. */ 4000, /* init size */ 4000, /* increment size */ NULL /* callback on error */ ); file_count = argc - pj_optind; port_count = file_count + 1 + RECORDER; /* Create the conference bridge. * With default options (zero), the bridge will create an instance of * sound capture and playback device and connect them to slot zero. */ status = pjmedia_conf_create( pool, /* pool to use */ port_count,/* number of ports */ clock_rate, channel_count, samples_per_frame, bits_per_sample, 0, /* options */ &conf /* result */ ); if (status != PJ_SUCCESS) { app_perror(THIS_FILE, "Unable to create conference bridge", status); return 1; } #if RECORDER status = pjmedia_wav_writer_port_create( pool, "confrecord.wav", clock_rate, channel_count, samples_per_frame, bits_per_sample, 0, 0, &rec_port); if (status != PJ_SUCCESS) { app_perror(THIS_FILE, "Unable to create WAV writer", status); return 1; } pjmedia_conf_add_port(conf, pool, rec_port, NULL, NULL); #endif /* Create file ports. */ file_port = pj_pool_alloc(pool, file_count * sizeof(pjmedia_port*)); for (i=0; i<file_count; ++i) { /* Load the WAV file to file port. */ status = pjmedia_wav_player_port_create( pool, /* pool. */ argv[i+pj_optind], /* filename */ 0, /* use default ptime */ 0, /* flags */ 0, /* buf size */ &file_port[i] /* result */ ); if (status != PJ_SUCCESS) { char title[80]; pj_ansi_sprintf(title, "Unable to use %s", argv[i+pj_optind]); app_perror(THIS_FILE, title, status); usage(); return 1; } /* Add the file port to conference bridge */ status = pjmedia_conf_add_port( conf, /* The bridge */ pool, /* pool */ file_port[i], /* port to connect */ NULL, /* Use port's name */ NULL /* ptr for slot # */ ); if (status != PJ_SUCCESS) { app_perror(THIS_FILE, "Unable to add conference port", status); return 1; } } /* * All ports are set up in the conference bridge. * But at this point, no media will be flowing since no ports are * "connected". User must connect the port manually. */ /* Dump memory usage */ dump_pool_usage(THIS_FILE, &cp); /* Sleep to allow log messages to flush */ pj_thread_sleep(100); /* * UI Menu: */ for (;;) { char tmp1[10]; char tmp2[10]; char *err; int src, dst, level, dur; puts(""); conf_list(conf, 0); puts(""); puts("Menu:"); puts(" s Show ports details"); puts(" c Connect one port to another"); puts(" d Disconnect port connection"); puts(" t Adjust signal level transmitted (tx) to a port"); puts(" r Adjust signal level received (rx) from a port"); puts(" v Display VU meter for a particular port"); puts(" q Quit"); puts(""); printf("Enter selection: "); fflush(stdout); if (fgets(tmp, sizeof(tmp), stdin) == NULL) break; switch (tmp[0]) { case 's': puts(""); conf_list(conf, 1); break; case 'c': puts(""); puts("Connect source port to destination port"); if (!input("Enter source port number", tmp1, sizeof(tmp1)) ) continue; src = strtol(tmp1, &err, 10); if (*err || src < 0 || src >= port_count) { puts("Invalid slot number"); continue; } if (!input("Enter destination port number", tmp2, sizeof(tmp2)) ) continue; dst = strtol(tmp2, &err, 10); if (*err || dst < 0 || dst >= port_count) { puts("Invalid slot number"); continue; } status = pjmedia_conf_connect_port(conf, src, dst, 0); if (status != PJ_SUCCESS) app_perror(THIS_FILE, "Error connecting port", status); break; case 'd': puts(""); puts("Disconnect port connection"); if (!input("Enter source port number", tmp1, sizeof(tmp1)) ) continue; src = strtol(tmp1, &err, 10); if (*err || src < 0 || src >= port_count) { puts("Invalid slot number"); continue; } if (!input("Enter destination port number", tmp2, sizeof(tmp2)) ) continue; dst = strtol(tmp2, &err, 10); if (*err || dst < 0 || dst >= port_count) { puts("Invalid slot number"); continue; } status = pjmedia_conf_disconnect_port(conf, src, dst); if (status != PJ_SUCCESS) app_perror(THIS_FILE, "Error connecting port", status); break; case 't': puts(""); puts("Adjust transmit level of a port"); if (!input("Enter port number", tmp1, sizeof(tmp1)) ) continue; src = strtol(tmp1, &err, 10); if (*err || src < 0 || src >= port_count) { puts("Invalid slot number"); continue; } if (!input("Enter level (-128 to >127, 0 for normal)", tmp2, sizeof(tmp2)) ) continue; level = strtol(tmp2, &err, 10); if (*err || level < -128) { puts("Invalid level"); continue; } status = pjmedia_conf_adjust_tx_level( conf, src, level); if (status != PJ_SUCCESS) app_perror(THIS_FILE, "Error adjusting level", status); break; case 'r': puts(""); puts("Adjust receive level of a port"); if (!input("Enter port number", tmp1, sizeof(tmp1)) ) continue; src = strtol(tmp1, &err, 10); if (*err || src < 0 || src >= port_count) { puts("Invalid slot number"); continue; } if (!input("Enter level (-128 to >127, 0 for normal)", tmp2, sizeof(tmp2)) ) continue; level = strtol(tmp2, &err, 10); if (*err || level < -128) { puts("Invalid level"); continue; } status = pjmedia_conf_adjust_rx_level( conf, src, level); if (status != PJ_SUCCESS) app_perror(THIS_FILE, "Error adjusting level", status); break; case 'v': puts(""); puts("Display VU meter"); if (!input("Enter port number to monitor", tmp1, sizeof(tmp1)) ) continue; src = strtol(tmp1, &err, 10); if (*err || src < 0 || src >= port_count) { puts("Invalid slot number"); continue; } if (!input("Enter r for rx level or t for tx level", tmp2, sizeof(tmp2))) continue; if (tmp2[0] != 'r' && tmp2[0] != 't') { puts("Invalid option"); continue; } if (!input("Duration to monitor (in seconds)", tmp1, sizeof(tmp1)) ) continue; dur = strtol(tmp1, &err, 10); if (*err) { puts("Invalid duration number"); continue; } monitor_level(conf, src, tmp2[0], dur); break; case 'q': goto on_quit; default: printf("Invalid input character '%c'\n", tmp[0]); break; } } on_quit: /* Start deinitialization: */ /* Destroy conference bridge */ status = pjmedia_conf_destroy( conf ); PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); /* Destroy file ports */ for (i=0; i<file_count; ++i) { status = pjmedia_port_destroy( file_port[i]); PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); } /* Destroy recorder port */ if (rec_port) pjmedia_port_destroy(rec_port); /* Release application pool */ pj_pool_release( pool ); /* Destroy media endpoint. */ pjmedia_endpt_destroy( med_endpt ); /* Destroy pool factory */ pj_caching_pool_destroy( &cp ); /* Shutdown PJLIB */ pj_shutdown(); /* Done. */ return 0; }
int main(int argc, char *argv[]) { pj_caching_pool cp; pjmedia_endpt *med_endpt; pj_pool_t *pool; pjmedia_port *file_port; pjmedia_port *resample_port; pjmedia_snd_port *snd_port; char tmp[10]; pj_status_t status; int dev_id = -1; int sampling_rate = CLOCK_RATE; int channel_count = NCHANNELS; int samples_per_frame = NSAMPLES; int bits_per_sample = NBITS; //int ptime; //int down_samples; /* Must init PJLIB first: */ status = pj_init(); PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); /* Get options */ if (get_snd_options(THIS_FILE, argc, argv, &dev_id, &sampling_rate, &channel_count, &samples_per_frame, &bits_per_sample)) { puts(""); puts(desc); return 1; } if (!argv[pj_optind]) { puts("Error: no file is specified"); puts(desc); return 1; } /* Must create a pool factory before we can allocate any memory. */ pj_caching_pool_init(&cp, &pj_pool_factory_default_policy, 0); /* * Initialize media endpoint. * This will implicitly initialize PJMEDIA too. */ status = pjmedia_endpt_create(&cp.factory, NULL, 1, &med_endpt); PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); /* Create memory pool for our file player */ pool = pj_pool_create( &cp.factory, /* pool factory */ "app", /* pool name. */ 4000, /* init size */ 4000, /* increment size */ NULL /* callback on error */ ); /* Create the file port. */ status = pjmedia_wav_player_port_create( pool, argv[pj_optind], 0, 0, 0, &file_port); if (status != PJ_SUCCESS) { app_perror(THIS_FILE, "Unable to open file", status); return 1; } /* File must have same number of channels. */ if (PJMEDIA_PIA_CCNT(&file_port->info) != (unsigned)channel_count) { PJ_LOG(3,(THIS_FILE, "Error: file has different number of channels. " "Perhaps you'd need -c option?")); pjmedia_port_destroy(file_port); return 1; } /* Calculate number of samples per frame to be taken from file port */ //ptime = samples_per_frame * 1000 / sampling_rate; /* Create the resample port. */ status = pjmedia_resample_port_create( pool, file_port, sampling_rate, 0, &resample_port); if (status != PJ_SUCCESS) { app_perror(THIS_FILE, "Unable to create resample port", status); return 1; } /* Create sound player port. */ status = pjmedia_snd_port_create( pool, /* pool */ dev_id, /* device */ dev_id, /* device */ sampling_rate, /* clock rate. */ channel_count, /* # of channels. */ samples_per_frame, /* samples per frame. */ bits_per_sample, /* bits per sample. */ 0, /* options */ &snd_port /* returned port */ ); if (status != PJ_SUCCESS) { app_perror(THIS_FILE, "Unable to open sound device", status); return 1; } /* Connect resample port to sound device */ status = pjmedia_snd_port_connect( snd_port, resample_port); if (status != PJ_SUCCESS) { app_perror(THIS_FILE, "Error connecting sound ports", status); return 1; } /* Dump memory usage */ dump_pool_usage(THIS_FILE, &cp); /* * File should be playing and looping now, using sound device's thread. */ /* Sleep to allow log messages to flush */ pj_thread_sleep(100); printf("Playing %s at sampling rate %d (original file sampling rate=%d)\n", argv[pj_optind], sampling_rate, PJMEDIA_PIA_SRATE(&file_port->info)); puts(""); puts("Press <ENTER> to stop playing and quit"); if (fgets(tmp, sizeof(tmp), stdin) == NULL) { puts("EOF while reading stdin, will quit now.."); } /* Start deinitialization: */ /* Destroy sound device */ status = pjmedia_snd_port_destroy( snd_port ); PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); /* Destroy resample port. * This will destroy all downstream ports (e.g. the file port) */ status = pjmedia_port_destroy( resample_port ); PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); /* Release application pool */ pj_pool_release( pool ); /* Destroy media endpoint. */ pjmedia_endpt_destroy( med_endpt ); /* Destroy pool factory */ pj_caching_pool_destroy( &cp ); /* Shutdown PJLIB */ pj_shutdown(); /* Done. */ return 0; }
int main(int argc, char *argv[]) { pj_caching_pool cp; pjmedia_endpt *med_endpt; pj_pool_t *pool; pjmedia_port *file_port = NULL; pjmedia_port *stereo_port = NULL; pjmedia_snd_port *snd_port = NULL; int dev_id = -1; char tmp[10]; pj_status_t status; char *wav_file = NULL; unsigned mode = 0; unsigned rec_ch_cnt = 1; unsigned snd_ch_cnt = 2; enum { OPT_MODE = 'm', OPT_REC_CHANNEL = 'C', OPT_SND_CHANNEL = 'c', }; struct pj_getopt_option long_options[] = { { "mode", 1, 0, OPT_MODE }, { "rec-ch-cnt", 1, 0, OPT_REC_CHANNEL }, { "snd-ch-cnt", 1, 0, OPT_SND_CHANNEL }, { NULL, 0, 0, 0 }, }; int c; int option_index; /* Must init PJLIB first: */ status = pj_init(); PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); /* Parse arguments */ pj_optind = 0; while((c=pj_getopt_long(argc,argv, "m:C:c:", long_options, &option_index))!=-1) { switch (c) { case OPT_MODE: if (mode) { app_perror(THIS_FILE, "Cannot record and play at once!", PJ_EINVAL); return 1; } mode = atoi(pj_optarg); break; case OPT_REC_CHANNEL: rec_ch_cnt = atoi(pj_optarg); break; case OPT_SND_CHANNEL: snd_ch_cnt = atoi(pj_optarg); break; default: printf("Invalid options %s\n", argv[pj_optind]); puts(desc); return 1; } } wav_file = argv[pj_optind]; /* Verify arguments. */ if (!wav_file) { app_perror(THIS_FILE, "WAV file not specified!", PJ_EINVAL); puts(desc); return 1; } if (!snd_ch_cnt || !rec_ch_cnt || rec_ch_cnt > 6) { app_perror(THIS_FILE, "Invalid or too many channel count!", PJ_EINVAL); puts(desc); return 1; } if (mode != MODE_RECORD && mode != MODE_PLAY) { app_perror(THIS_FILE, "Invalid operation mode!", PJ_EINVAL); puts(desc); return 1; } /* Must create a pool factory before we can allocate any memory. */ pj_caching_pool_init(&cp, &pj_pool_factory_default_policy, 0); /* * Initialize media endpoint. * This will implicitly initialize PJMEDIA too. */ status = pjmedia_endpt_create(&cp.factory, NULL, 1, &med_endpt); PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); /* Create memory pool for our file player */ pool = pj_pool_create( &cp.factory, /* pool factory */ "app", /* pool name. */ 4000, /* init size */ 4000, /* increment size */ NULL /* callback on error */ ); if (mode == MODE_PLAY) { /* Create WAVE file player port. */ status = pjmedia_wav_player_port_create( pool, wav_file, PTIME, 0, 0, &file_port); if (status != PJ_SUCCESS) { app_perror(THIS_FILE, "Unable to open file", status); return 1; } /* Create sound player port. */ status = pjmedia_snd_port_create_player( pool, /* pool */ dev_id, /* device id. */ PJMEDIA_PIA_SRATE(&file_port->info),/* clock rate. */ snd_ch_cnt, /* # of channels. */ snd_ch_cnt * PTIME * /* samples per frame. */ PJMEDIA_PIA_SRATE(&file_port->info) / 1000, PJMEDIA_PIA_BITS(&file_port->info),/* bits per sample. */ 0, /* options */ 15, 5, &snd_port /* returned port */ ); if (status != PJ_SUCCESS) { app_perror(THIS_FILE, "Unable to open sound device", status); return 1; } if (snd_ch_cnt != PJMEDIA_PIA_CCNT(&file_port->info)) { status = pjmedia_stereo_port_create( pool, file_port, snd_ch_cnt, 0, &stereo_port); if (status != PJ_SUCCESS) { app_perror(THIS_FILE, "Unable to create stereo port", status); return 1; } status = pjmedia_snd_port_connect(snd_port, stereo_port); } else { status = pjmedia_snd_port_connect(snd_port, file_port); } if (status != PJ_SUCCESS) { app_perror(THIS_FILE, "Unable to connect sound port", status); return 1; } } else { /* Create WAVE file writer port. */ status = pjmedia_wav_writer_port_create(pool, wav_file, REC_CLOCK_RATE, rec_ch_cnt, rec_ch_cnt * PTIME * REC_CLOCK_RATE / 1000, NBITS, 0, 0, &file_port); if (status != PJ_SUCCESS) { app_perror(THIS_FILE, "Unable to open file", status); return 1; } /* Create sound player port. */ status = pjmedia_snd_port_create_rec( pool, /* pool */ dev_id, /* device id. */ REC_CLOCK_RATE, /* clock rate. */ snd_ch_cnt, /* # of channels. */ snd_ch_cnt * PTIME * REC_CLOCK_RATE / 1000, /* samples per frame. */ NBITS, /* bits per sample. */ 0, /* options */ 15, 5, &snd_port /* returned port */ ); if (status != PJ_SUCCESS) { app_perror(THIS_FILE, "Unable to open sound device", status); return 1; } if (rec_ch_cnt != snd_ch_cnt) { status = pjmedia_stereo_port_create( pool, file_port, snd_ch_cnt, 0, &stereo_port); if (status != PJ_SUCCESS) { app_perror(THIS_FILE, "Unable to create stereo port", status); return 1; } status = pjmedia_snd_port_connect(snd_port, stereo_port); } else { status = pjmedia_snd_port_connect(snd_port, file_port); } if (status != PJ_SUCCESS) { app_perror(THIS_FILE, "Unable to connect sound port", status); return 1; } } /* Dump memory usage */ dump_pool_usage(THIS_FILE, &cp); /* * File should be playing and looping now, using sound device's thread. */ /* Sleep to allow log messages to flush */ pj_thread_sleep(100); printf("Mode = %s\n", (mode == MODE_PLAY? "playing" : "recording") ); printf("File port channel count = %d\n", PJMEDIA_PIA_CCNT(&file_port->info)); printf("Sound port channel count = %d\n", PJMEDIA_PIA_CCNT(&pjmedia_snd_port_get_port(snd_port)->info)); puts(""); puts("Press <ENTER> to stop and quit"); if (fgets(tmp, sizeof(tmp), stdin) == NULL) { puts("EOF while reading stdin, will quit now.."); } /* Start deinitialization: */ /* Destroy sound device */ status = pjmedia_snd_port_destroy( snd_port ); PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); /* Destroy stereo port and file_port. * Stereo port will destroy all downstream ports (e.g. the file port) */ status = pjmedia_port_destroy( stereo_port? stereo_port : file_port); PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); /* Release application pool */ pj_pool_release( pool ); /* Destroy media endpoint. */ pjmedia_endpt_destroy( med_endpt ); /* Destroy pool factory */ pj_caching_pool_destroy( &cp ); /* Shutdown PJLIB */ pj_shutdown(); /* Done. */ return 0; }