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.

101 lines
1.9KB

  1. // https://wiki.wireshark.org/rtpdump
  2. // https://code.wireshark.org/review/gitweb?p=wireshark.git;a=blob;f=ui/tap-rtp-common.c
  3. #include "rtp-dump.h"
  4. #include <stdio.h>
  5. #include <assert.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include "byte-order.h"
  9. struct rtpdump_t
  10. {
  11. FILE* fp;
  12. uint32_t start_sec;
  13. uint32_t start_usec;
  14. uint32_t source;
  15. uint16_t port;
  16. };
  17. static int rtmp_dump_read_file_header(struct rtpdump_t* ctx)
  18. {
  19. int i;
  20. uint8_t buf[64];
  21. // #!rtpplay[[version]] [[addr]]/[[port]]\n
  22. for (i = 0; i < sizeof(buf); i++)
  23. {
  24. fread(buf + i, 1, 1, ctx->fp);
  25. if (buf[i] == '\n')
  26. break;
  27. }
  28. if (i >= sizeof(buf) || 1 != fread(buf, 16, 1, ctx->fp))
  29. return -1;
  30. be_read_uint32(buf, &ctx->start_sec);
  31. be_read_uint32(buf + 4, &ctx->start_usec);
  32. be_read_uint32(buf + 8, &ctx->source);
  33. be_read_uint16(buf + 12, &ctx->port);
  34. return 0;
  35. }
  36. struct rtpdump_t* rtpdump_open(const char* file, int flags)
  37. {
  38. FILE* fp;
  39. struct rtpdump_t* ctx;
  40. fp = fopen(file, "rb");
  41. if (!fp)
  42. return NULL;
  43. ctx = (struct rtpdump_t*)calloc(1, sizeof(*ctx));
  44. if (!ctx)
  45. {
  46. fclose(fp);
  47. return NULL;
  48. }
  49. ctx->fp = fp;
  50. if (0 != rtmp_dump_read_file_header(ctx))
  51. {
  52. rtpdump_close(ctx);
  53. return NULL;
  54. }
  55. return ctx;
  56. }
  57. int rtpdump_close(struct rtpdump_t* ctx)
  58. {
  59. if (ctx->fp)
  60. {
  61. fclose(ctx->fp);
  62. ctx->fp = NULL;
  63. }
  64. free(ctx);
  65. return 0;
  66. }
  67. int rtpdump_read(struct rtpdump_t* ctx, uint32_t* clock, void* data, int bytes)
  68. {
  69. uint8_t buf[8];
  70. uint16_t len; /* length of packet, including this header (may be smaller than plen if not whole packet recorded) */
  71. uint16_t payload; /* actual header+payload length for RTP, 0 for RTCP */
  72. if (1 != fread(buf, 8, 1, ctx->fp))
  73. return -1;
  74. be_read_uint16(buf, &len);
  75. be_read_uint16(buf + 2, &payload);
  76. be_read_uint32(buf + 4, clock); /* milliseconds since the start of recording */
  77. if (bytes < len - 8)
  78. return -1;
  79. if (1 != fread(data, len - 8, 1, ctx->fp))
  80. return -1;
  81. return len - 8;
  82. }