session_test.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
  3. * Copyright (C) 2003-2008 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. #include <pjmedia/mediamgr.h>
  20. #include <pjmedia/session.h>
  21. #include <pj/sock.h>
  22. #include <pj/pool.h>
  23. #include <stdio.h>
  24. #include <pj/string.h>
  25. pj_status_t session_test (pj_pool_factory *pf)
  26. {
  27. pj_med_mgr_t *mm;
  28. pj_media_session_t *s1, *s2;
  29. pj_pool_t *pool;
  30. pjsdp_session_desc *sdp;
  31. pj_media_stream_info sd_info;
  32. char buf[1024];
  33. int len;
  34. pj_media_stream_stat tx_stat, rx_stat;
  35. pool = pj_pool_create(pf, "test", 4096, 1024, NULL);
  36. // Init media manager.
  37. mm = pj_med_mgr_create ( pf );
  38. // Create caller session.
  39. // THIS WILL DEFINITELY CRASH (NULL as argument)!
  40. s1 = pj_media_session_create (mm, NULL);
  41. // Set caller's media to send-only.
  42. sd_info.dir = PJMEDIA_DIR_ENCODING;
  43. pj_media_session_modify_stream (s1, 0, PJMEDIA_STREAM_MODIFY_DIR, &sd_info);
  44. // Create caller SDP.
  45. sdp = pj_media_session_create_sdp (s1, pool, 0);
  46. len = pjsdp_print (sdp, buf, sizeof(buf));
  47. buf[len] = '\0';
  48. printf("Caller's initial SDP:\n<BEGIN>\n%s\n<END>\n", buf);
  49. // Parse SDP from caller.
  50. sdp = pjsdp_parse (buf, len, pool);
  51. // Create callee session based on caller's SDP.
  52. // THIS WILL DEFINITELY CRASH (NULL as argument)!
  53. s2 = pj_media_session_create_from_sdp (mm, sdp, NULL);
  54. // Create callee SDP
  55. sdp = pj_media_session_create_sdp (s2, pool, 0);
  56. len = pjsdp_print (sdp, buf, sizeof(buf));
  57. buf[len] = '\0';
  58. printf("Callee's SDP:\n<BEGIN>\n%s\n<END>\n", buf);
  59. // Parse SDP from callee.
  60. sdp = pjsdp_parse (buf, len, pool);
  61. // Update caller
  62. pj_media_session_update (s1, sdp);
  63. sdp = pj_media_session_create_sdp (s1, pool, 0);
  64. pjsdp_print (sdp, buf, sizeof(buf));
  65. printf("Caller's SDP after update:\n<BEGIN>\n%s\n<END>\n", buf);
  66. // Now start media.
  67. pj_media_session_activate (s2);
  68. pj_media_session_activate (s1);
  69. // Wait
  70. for (;;) {
  71. int has_stat;
  72. printf("Enter q to exit, 1 or 2 to print statistics.\n");
  73. fgets (buf, 10, stdin);
  74. has_stat = 0;
  75. switch (buf[0]) {
  76. case 'q':
  77. case 'Q':
  78. goto done;
  79. break;
  80. case '1':
  81. pj_media_session_get_stat (s1, 0, &tx_stat, &rx_stat);
  82. has_stat = 1;
  83. break;
  84. case '2':
  85. pj_media_session_get_stat (s2, 0, &tx_stat, &rx_stat);
  86. has_stat = 1;
  87. break;
  88. }
  89. if (has_stat) {
  90. pj_media_stream_stat *stat[2] = { &tx_stat, &rx_stat };
  91. const char *statname[2] = { "TX", "RX" };
  92. int i;
  93. for (i=0; i<2; ++i) {
  94. printf("%s statistics:\n", statname[i]);
  95. printf(" Pkt TX=%d RX=%d\n", stat[i]->pkt_tx, stat[i]->pkt_rx);
  96. printf(" Octets TX=%d RX=%d\n", stat[i]->oct_tx, stat[i]->oct_rx);
  97. printf(" Jitter %d ms\n", stat[i]->jitter);
  98. printf(" Pkt lost %d\n", stat[i]->pkt_lost);
  99. }
  100. printf("\n");
  101. }
  102. }
  103. done:
  104. // Done.
  105. pj_pool_release (pool);
  106. pj_media_session_destroy (s2);
  107. pj_media_session_destroy (s1);
  108. pj_med_mgr_destroy (mm);
  109. return 0;
  110. }