Index: pjproject/trunk/pjsip-apps/src/samples/siprtp.c
===================================================================
--- pjproject/trunk/pjsip-apps/src/samples/siprtp.c	(revision 429)
+++ pjproject/trunk/pjsip-apps/src/samples/siprtp.c	(revision 430)
@@ -35,4 +35,6 @@
 " Program options:\n"
 "   --count=N,        -c    Set number of calls to create (default:1) \n"
+"   --duration=SEC,   -d    Set maximum call duration (default:unlimited) \n"
+"   --auto-quit,      -q    Quit when calls have been completed (default:no)\n"
 "\n"
 " Address and ports options:\n"
@@ -129,4 +131,6 @@
     pj_time_val		 response_time;
     pj_time_val		 connect_time;
+
+    pj_timer_entry	 d_timer;	    /**< Disconnect timer.	*/
 };
 
@@ -135,4 +139,7 @@
 {
     unsigned		 max_calls;
+    unsigned		 uac_calls;
+    unsigned		 duration;
+    pj_bool_t		 auto_quit;
     unsigned		 thread_count;
     int			 sip_port;
@@ -188,4 +195,7 @@
 			       struct call *call,
 			       pjmedia_sdp_session **p_sdp);
+
+/* Hangup call */
+static void hangup_call(unsigned index);
 
 /* Destroy the call's media */
@@ -524,6 +534,8 @@
 				   dst_uri,		/* remote target    */
 				   &dlg);		/* dialog	    */
-    if (status != PJ_SUCCESS)
+    if (status != PJ_SUCCESS) {
+	++app.uac_calls;
 	return status;
+    }
 
     /* Create SDP */
@@ -534,4 +546,5 @@
     if (status != PJ_SUCCESS) {
 	pjsip_dlg_terminate(dlg);
+	++app.uac_calls;
 	return status;
     }
@@ -682,4 +695,17 @@
 
 
+/* Callback timer to disconnect call (limiting call duration) */
+static void timer_disconnect_call( pj_timer_heap_t *timer_heap,
+				   struct pj_timer_entry *entry)
+{
+    struct call *call = entry->user_data;
+
+    PJ_UNUSED_ARG(timer_heap);
+
+    entry->id = 0;
+    hangup_call(call->index);
+}
+
+
 /* Callback to be called when invite session's state has changed: */
 static void call_on_state_changed( pjsip_inv_session *inv, 
@@ -696,4 +722,9 @@
 	
 	pj_time_val null_time = {0, 0};
+
+	if (call->d_timer.id != 0) {
+	    pjsip_endpt_cancel_timer(app.sip_endpt, &call->d_timer);
+	    call->d_timer.id = 0;
+	}
 
 	PJ_LOG(3,(THIS_FILE, "Call #%d disconnected. Reason=%s",
@@ -713,4 +744,5 @@
 	call->connect_time = null_time;
 
+	++app.uac_calls;
 
     } else if (inv->state == PJSIP_INV_STATE_CONFIRMED) {
@@ -728,4 +760,15 @@
 		  PJ_TIME_VAL_MSEC(t)));
 
+	if (app.duration != 0) {
+	    call->d_timer.id = 1;
+	    call->d_timer.user_data = call;
+	    call->d_timer.cb = &timer_disconnect_call;
+
+	    t.sec = app.duration;
+	    t.msec = 0;
+
+	    pjsip_endpt_schedule_timer(app.sip_endpt, &call->d_timer, &t);
+	}
+
     } else if (	inv->state == PJSIP_INV_STATE_EARLY ||
 		inv->state == PJSIP_INV_STATE_CONNECTING) {
@@ -775,4 +818,6 @@
     struct pj_getopt_option long_options[] = {
 	{ "count",	    1, 0, 'c' },
+	{ "duration",	    1, 0, 'd' },
+	{ "auto-quit",	    0, 0, 'q' },
 	{ "local-port",	    1, 0, 'p' },
 	{ "rtp-port",	    1, 0, 'r' },
@@ -820,5 +865,5 @@
     /* Parse options */
     pj_optind = 0;
-    while((c=pj_getopt_long(argc,argv, "c:p:r:i:l:", 
+    while((c=pj_getopt_long(argc,argv, "c:d:p:r:i:l:q", 
 			    long_options, &option_index))!=-1) 
     {
@@ -831,4 +876,11 @@
 	    }
 	    break;
+	case 'd':
+	    app.duration = atoi(pj_optarg);
+	    break;
+	case 'q':
+	    app.auto_quit = 1;
+	    break;
+
 	case 'p':
 	    app.sip_port = atoi(pj_optarg);
@@ -1760,4 +1812,13 @@
 	return 1;
 
+    /* Verify options: */
+
+    /* Auto-quit can not be specified for UAS */
+    if (app.auto_quit && app.uri_to_call.slen == 0) {
+	printf("Error: --auto-quit option only valid for outgoing "
+	       "mode (UAC) only\n");
+	return 1;
+    }
+
     /* Init logging */
     status = app_logging_init();
@@ -1805,12 +1866,23 @@
 	}
 
+	if (app.auto_quit) {
+	    /* Wait for calls to complete */
+	    while (app.uac_calls < app.max_calls)
+		pj_thread_sleep(100);
+	    pj_thread_sleep(200);
+	} else {
+	    /* Start user interface loop */
+	    console_main();
+	}
+
     } else {
 
 	PJ_LOG(3,(THIS_FILE, "Ready for incoming calls (max=%d)", 
 		  app.max_calls));
-    }
-
-    /* Start user interface loop */
-    console_main();
+
+	/* Start user interface loop */
+	console_main();
+
+    }
 
     
