dump.cc 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 <cstdint>
  7. #include <cstdio>
  8. #include <vector>
  9. #include "mp4parse.h"
  10. void test_context()
  11. {
  12. mp4parse_state *context = mp4parse_new();
  13. assert(context != nullptr);
  14. mp4parse_free(context);
  15. }
  16. void test_arg_validation(mp4parse_state *context)
  17. {
  18. int32_t rv;
  19. rv = mp4parse_read(nullptr, nullptr, 0);
  20. assert(rv < 0);
  21. rv = mp4parse_read(context, nullptr, 0);
  22. assert(rv < 0);
  23. size_t len = 4097;
  24. rv = mp4parse_read(context, nullptr, len);
  25. assert(rv < 0);
  26. std::vector<uint8_t> buf;
  27. rv = mp4parse_read(context, buf.data(), buf.size());
  28. assert(rv < 0);
  29. buf.reserve(len);
  30. rv = mp4parse_read(context, buf.data(), buf.size());
  31. assert(rv < 0);
  32. }
  33. void test_arg_validation()
  34. {
  35. test_arg_validation(nullptr);
  36. mp4parse_state *context = mp4parse_new();
  37. assert(context != nullptr);
  38. test_arg_validation(context);
  39. mp4parse_free(context);
  40. }
  41. void read_file(const char* filename)
  42. {
  43. FILE* f = fopen(filename, "rb");
  44. assert(f != nullptr);
  45. size_t len = 4096;
  46. std::vector<uint8_t> buf(len);
  47. size_t read = fread(buf.data(), sizeof(decltype(buf)::value_type), buf.size(), f);
  48. buf.resize(read);
  49. fclose(f);
  50. mp4parse_state *context = mp4parse_new();
  51. assert(context != nullptr);
  52. fprintf(stderr, "Parsing %lu byte buffer.\n", (unsigned long)read);
  53. int32_t rv = mp4parse_read(context, buf.data(), buf.size());
  54. assert(rv >= 0);
  55. fprintf(stderr, "%d tracks returned to C code.\n", rv);
  56. mp4parse_free(context);
  57. }
  58. int main(int argc, char* argv[])
  59. {
  60. test_context();
  61. test_arg_validation();
  62. for (auto i = 1; i < argc; ++i) {
  63. read_file(argv[i]);
  64. }
  65. return 0;
  66. }