dump.cc 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
  5. #include <cassert>
  6. #include <cinttypes>
  7. #include <cstdint>
  8. #include <cstdio>
  9. #include <vector>
  10. #include "mp4parse.h"
  11. void test_context()
  12. {
  13. mp4parse_state *context = mp4parse_new();
  14. assert(context != nullptr);
  15. mp4parse_free(context);
  16. }
  17. void test_arg_validation(mp4parse_state *context)
  18. {
  19. int32_t rv;
  20. rv = mp4parse_read(nullptr, nullptr, 0);
  21. assert(rv == MP4PARSE_ERROR_BADARG);
  22. rv = mp4parse_read(context, nullptr, 0);
  23. assert(rv == MP4PARSE_ERROR_BADARG);
  24. size_t len = 4097;
  25. rv = mp4parse_read(context, nullptr, len);
  26. assert(rv == MP4PARSE_ERROR_BADARG);
  27. std::vector<uint8_t> buf;
  28. rv = mp4parse_read(context, buf.data(), buf.size());
  29. assert(rv == MP4PARSE_ERROR_BADARG);
  30. buf.reserve(len);
  31. rv = mp4parse_read(context, buf.data(), buf.size());
  32. assert(rv == MP4PARSE_ERROR_BADARG);
  33. }
  34. void test_arg_validation()
  35. {
  36. test_arg_validation(nullptr);
  37. mp4parse_state *context = mp4parse_new();
  38. assert(context != nullptr);
  39. test_arg_validation(context);
  40. mp4parse_free(context);
  41. }
  42. const char * tracktype2mimetype(uint32_t type)
  43. {
  44. switch (type) {
  45. case MP4PARSE_TRACK_TYPE_H264: return "video/avc";
  46. case MP4PARSE_TRACK_TYPE_AAC: return "audio/mp4a-latm";
  47. }
  48. return "unknown";
  49. }
  50. const char * errorstring(int32_t error)
  51. {
  52. if (error >= MP4PARSE_OK) {
  53. return "Ok";
  54. }
  55. switch (error) {
  56. case MP4PARSE_ERROR_BADARG: return "Invalid argument";
  57. case MP4PARSE_ERROR_INVALID: return "Invalid data";
  58. case MP4PARSE_ERROR_UNSUPPORTED: return "Feature unsupported";
  59. case MP4PARSE_ERROR_EOF: return "Unexpected end-of-file";
  60. case MP4PARSE_ERROR_IO: return "I/O error";
  61. }
  62. return "Unknown error";
  63. }
  64. int32_t read_file(const char* filename)
  65. {
  66. FILE* f = fopen(filename, "rb");
  67. assert(f != nullptr);
  68. size_t len = 4096*16;
  69. std::vector<uint8_t> buf(len);
  70. size_t read = fread(buf.data(), sizeof(decltype(buf)::value_type), buf.size(), f);
  71. buf.resize(read);
  72. fclose(f);
  73. mp4parse_state *context = mp4parse_new();
  74. assert(context != nullptr);
  75. fprintf(stderr, "Parsing %lu byte buffer.\n", (unsigned long)read);
  76. int32_t rv = mp4parse_read(context, buf.data(), buf.size());
  77. if (rv != MP4PARSE_OK) {
  78. fprintf(stderr, "Parsing failed: %s\n", errorstring(rv));
  79. return rv;
  80. }
  81. uint32_t tracks = mp4parse_get_track_count(context);
  82. fprintf(stderr, "%u tracks returned to C code.\n", tracks);
  83. for (uint32_t i = 0; i < tracks; ++i) {
  84. mp4parse_track_info track_info;
  85. int32_t rv2 = mp4parse_get_track_info(context, i, &track_info);
  86. assert(rv2 == MP4PARSE_OK);
  87. fprintf(stderr, "Track %d: mime_type='%s' duration=%" PRId64 " media_time=%" PRId64 " track_id=%d\n",
  88. i, tracktype2mimetype(track_info.track_type), track_info.duration, track_info.media_time, track_info.track_id);
  89. }
  90. mp4parse_free(context);
  91. return MP4PARSE_OK;
  92. }
  93. int main(int argc, char* argv[])
  94. {
  95. test_context();
  96. test_arg_validation();
  97. for (auto i = 1; i < argc; ++i) {
  98. read_file(argv[i]);
  99. }
  100. return 0;
  101. }