Tristan Matthews | 0a329cc | 2013-07-17 13:20:14 -0400 | [diff] [blame] | 1 | /* $Id$ */ |
| 2 | /* |
| 3 | * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com) |
| 4 | * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 19 | */ |
| 20 | #include <pjmedia/mediamgr.h> |
| 21 | #include <pjmedia/session.h> |
| 22 | #include <pj/sock.h> |
| 23 | #include <pj/pool.h> |
| 24 | #include <stdio.h> |
| 25 | #include <pj/string.h> |
| 26 | |
| 27 | pj_status_t session_test (pj_pool_factory *pf) |
| 28 | { |
| 29 | pj_med_mgr_t *mm; |
| 30 | pj_media_session_t *s1, *s2; |
| 31 | pj_pool_t *pool; |
| 32 | pjsdp_session_desc *sdp; |
| 33 | pj_media_stream_info sd_info; |
| 34 | char buf[1024]; |
| 35 | int len; |
| 36 | pj_media_stream_stat tx_stat, rx_stat; |
| 37 | |
| 38 | pool = pj_pool_create(pf, "test", 4096, 1024, NULL); |
| 39 | |
| 40 | // Init media manager. |
| 41 | mm = pj_med_mgr_create ( pf ); |
| 42 | |
| 43 | // Create caller session. |
| 44 | // THIS WILL DEFINITELY CRASH (NULL as argument)! |
| 45 | s1 = pj_media_session_create (mm, NULL); |
| 46 | |
| 47 | // Set caller's media to send-only. |
| 48 | sd_info.dir = PJMEDIA_DIR_ENCODING; |
| 49 | pj_media_session_modify_stream (s1, 0, PJMEDIA_STREAM_MODIFY_DIR, &sd_info); |
| 50 | |
| 51 | // Create caller SDP. |
| 52 | sdp = pj_media_session_create_sdp (s1, pool, 0); |
| 53 | len = pjsdp_print (sdp, buf, sizeof(buf)); |
| 54 | buf[len] = '\0'; |
| 55 | printf("Caller's initial SDP:\n<BEGIN>\n%s\n<END>\n", buf); |
| 56 | |
| 57 | // Parse SDP from caller. |
| 58 | sdp = pjsdp_parse (buf, len, pool); |
| 59 | |
| 60 | // Create callee session based on caller's SDP. |
| 61 | // THIS WILL DEFINITELY CRASH (NULL as argument)! |
| 62 | s2 = pj_media_session_create_from_sdp (mm, sdp, NULL); |
| 63 | |
| 64 | // Create callee SDP |
| 65 | sdp = pj_media_session_create_sdp (s2, pool, 0); |
| 66 | len = pjsdp_print (sdp, buf, sizeof(buf)); |
| 67 | buf[len] = '\0'; |
| 68 | printf("Callee's SDP:\n<BEGIN>\n%s\n<END>\n", buf); |
| 69 | |
| 70 | // Parse SDP from callee. |
| 71 | sdp = pjsdp_parse (buf, len, pool); |
| 72 | |
| 73 | // Update caller |
| 74 | pj_media_session_update (s1, sdp); |
| 75 | sdp = pj_media_session_create_sdp (s1, pool, 0); |
| 76 | pjsdp_print (sdp, buf, sizeof(buf)); |
| 77 | printf("Caller's SDP after update:\n<BEGIN>\n%s\n<END>\n", buf); |
| 78 | |
| 79 | // Now start media. |
| 80 | pj_media_session_activate (s2); |
| 81 | pj_media_session_activate (s1); |
| 82 | |
| 83 | // Wait |
| 84 | for (;;) { |
| 85 | int has_stat; |
| 86 | |
| 87 | printf("Enter q to exit, 1 or 2 to print statistics.\n"); |
| 88 | fgets (buf, 10, stdin); |
| 89 | has_stat = 0; |
| 90 | |
| 91 | switch (buf[0]) { |
| 92 | case 'q': |
| 93 | case 'Q': |
| 94 | goto done; |
| 95 | break; |
| 96 | case '1': |
| 97 | pj_media_session_get_stat (s1, 0, &tx_stat, &rx_stat); |
| 98 | has_stat = 1; |
| 99 | break; |
| 100 | case '2': |
| 101 | pj_media_session_get_stat (s2, 0, &tx_stat, &rx_stat); |
| 102 | has_stat = 1; |
| 103 | break; |
| 104 | } |
| 105 | |
| 106 | if (has_stat) { |
| 107 | pj_media_stream_stat *stat[2] = { &tx_stat, &rx_stat }; |
| 108 | const char *statname[2] = { "TX", "RX" }; |
| 109 | int i; |
| 110 | |
| 111 | for (i=0; i<2; ++i) { |
| 112 | printf("%s statistics:\n", statname[i]); |
| 113 | printf(" Pkt TX=%d RX=%d\n", stat[i]->pkt_tx, stat[i]->pkt_rx); |
| 114 | printf(" Octets TX=%d RX=%d\n", stat[i]->oct_tx, stat[i]->oct_rx); |
| 115 | printf(" Jitter %d ms\n", stat[i]->jitter); |
| 116 | printf(" Pkt lost %d\n", stat[i]->pkt_lost); |
| 117 | } |
| 118 | printf("\n"); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | done: |
| 123 | |
| 124 | // Done. |
| 125 | pj_pool_release (pool); |
| 126 | pj_media_session_destroy (s2); |
| 127 | pj_media_session_destroy (s1); |
| 128 | pj_med_mgr_destroy (mm); |
| 129 | |
| 130 | return 0; |
| 131 | } |