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.

109 lines
2.9 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #include "test/rtp_file_writer.h"
  11. #include <stdint.h>
  12. #include <stdio.h>
  13. #include <string>
  14. #include "rtc_base/checks.h"
  15. #include "rtc_base/constructor_magic.h"
  16. namespace webrtc {
  17. namespace test {
  18. static const uint16_t kPacketHeaderSize = 8;
  19. static const char kFirstLine[] = "#!rtpplay1.0 0.0.0.0/0\n";
  20. // Write RTP packets to file in rtpdump format, as documented at:
  21. // http://www.cs.columbia.edu/irt/software/rtptools/
  22. class RtpDumpWriter : public RtpFileWriter {
  23. public:
  24. explicit RtpDumpWriter(FILE* file) : file_(file) {
  25. RTC_CHECK(file_ != NULL);
  26. Init();
  27. }
  28. ~RtpDumpWriter() override {
  29. if (file_ != NULL) {
  30. fclose(file_);
  31. file_ = NULL;
  32. }
  33. }
  34. bool WritePacket(const RtpPacket* packet) override {
  35. uint16_t len = static_cast<uint16_t>(packet->length + kPacketHeaderSize);
  36. uint16_t plen = static_cast<uint16_t>(packet->original_length);
  37. uint32_t offset = packet->time_ms;
  38. RTC_CHECK(WriteUint16(len));
  39. RTC_CHECK(WriteUint16(plen));
  40. RTC_CHECK(WriteUint32(offset));
  41. return fwrite(packet->data, sizeof(uint8_t), packet->length, file_) ==
  42. packet->length;
  43. }
  44. private:
  45. bool Init() {
  46. fprintf(file_, "%s", kFirstLine);
  47. RTC_CHECK(WriteUint32(0));
  48. RTC_CHECK(WriteUint32(0));
  49. RTC_CHECK(WriteUint32(0));
  50. RTC_CHECK(WriteUint16(0));
  51. RTC_CHECK(WriteUint16(0));
  52. return true;
  53. }
  54. bool WriteUint32(uint32_t in) {
  55. // Loop through shifts = {24, 16, 8, 0}.
  56. for (int shifts = 24; shifts >= 0; shifts -= 8) {
  57. uint8_t tmp = static_cast<uint8_t>((in >> shifts) & 0xFF);
  58. if (fwrite(&tmp, sizeof(uint8_t), 1, file_) != 1)
  59. return false;
  60. }
  61. return true;
  62. }
  63. bool WriteUint16(uint16_t in) {
  64. // Write 8 MSBs.
  65. uint8_t tmp = static_cast<uint8_t>((in >> 8) & 0xFF);
  66. if (fwrite(&tmp, sizeof(uint8_t), 1, file_) != 1)
  67. return false;
  68. // Write 8 LSBs.
  69. tmp = static_cast<uint8_t>(in & 0xFF);
  70. if (fwrite(&tmp, sizeof(uint8_t), 1, file_) != 1)
  71. return false;
  72. return true;
  73. }
  74. FILE* file_;
  75. RTC_DISALLOW_COPY_AND_ASSIGN(RtpDumpWriter);
  76. };
  77. RtpFileWriter* RtpFileWriter::Create(FileFormat format,
  78. const std::string& filename) {
  79. FILE* file = fopen(filename.c_str(), "wb");
  80. if (file == NULL) {
  81. printf("ERROR: Can't open file: %s\n", filename.c_str());
  82. return NULL;
  83. }
  84. switch (format) {
  85. case kRtpDump:
  86. return new RtpDumpWriter(file);
  87. }
  88. fclose(file);
  89. return NULL;
  90. }
  91. } // namespace test
  92. } // namespace webrtc