You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

90 line
2.0KB

  1. // RFC-4566 SDP
  2. // 6. SDP Attributes (p25)
  3. // a=rtpmap:<payload type> <encoding name>/<clock rate>[/<encoding parameters>]
  4. //
  5. // a=rtpmap:98 L16/16000/2
  6. //
  7. // m=<media> <port>/<number of ports> <transport> <fmt list>
  8. // m=audio 49230 RTP/AVP 96 97 98
  9. // a=rtpmap:96 L8/8000
  10. // a=rtpmap:97 L16/8000
  11. // a=rtpmap:98 L16/11025/2
  12. #include "sdp-a-rtpmap.h"
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <assert.h>
  16. #include <stdio.h>
  17. int sdp_a_rtpmap(const char* rtpmap, int *payload, char encoding[16], int* rate, char parameters[64])
  18. {
  19. const char *p1;
  20. const char *p = rtpmap;
  21. // payload type
  22. p1 = strchr(p, ' ');
  23. if(!p1)
  24. return -1;
  25. if(payload)
  26. {
  27. *payload = atoi(p);
  28. }
  29. p = p1 + 1;
  30. // encoding name
  31. assert(' ' == *p1);
  32. p1 = strchr(p, '/');
  33. if(!p1)
  34. return -1;
  35. if(encoding)
  36. {
  37. if (p1 - p < 16)
  38. snprintf(encoding, 16, "%.*s", (int)(p1 - p), p);
  39. else
  40. *encoding = 0;
  41. }
  42. // clock rate
  43. assert('/' == *p1);
  44. if(rate)
  45. {
  46. *rate = atoi(p1+1);
  47. }
  48. // encoding parameters
  49. if(parameters)
  50. {
  51. p1 = strchr(p1 + 1, '/');
  52. if (p1 && strlen(p1) < 64)
  53. snprintf(parameters, 64, "%s", p1 + 1);
  54. else
  55. *parameters = 0;
  56. }
  57. return 0;
  58. }
  59. #if defined(DEBUG) || defined(_DEBUG)
  60. void sdp_a_rtpmap_test(void)
  61. {
  62. int payload = 0;
  63. int rate = 0;
  64. char encoding[32] = { 0 };
  65. char parameters[64] = { 0 };
  66. assert(0 == sdp_a_rtpmap("96 L8/8000", &payload, encoding, &rate, parameters));
  67. assert(96 == payload && 8000 == rate && 0 == strcmp(encoding, "L8") && '\0' == parameters[0]);
  68. assert(0 == sdp_a_rtpmap("97 L16/8000", &payload, encoding, &rate, parameters));
  69. assert(97 == payload && 8000 == rate && 0 == strcmp(encoding, "L16") && '\0' == parameters[0]);
  70. assert(0 == sdp_a_rtpmap("98 L16/11025/2", &payload, encoding, &rate, parameters));
  71. assert(98 == payload && 11025 == rate && 0 == strcmp(encoding, "L16") && 0==strcmp("2", parameters));
  72. assert(0 == sdp_a_rtpmap("102 G726-16/8000", &payload, encoding, &rate, parameters));
  73. assert(102 == payload && 8000 == rate && 0 == strcmp(encoding, "G726-16") && 0 == strcmp("", parameters));
  74. }
  75. #endif