25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

dash-static-test.cpp 2.7KB

8 달 전
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #include "dash-mpd.h"
  2. #include "dash-proto.h"
  3. #include "mov-format.h"
  4. #include "mov-reader.h"
  5. #include <assert.h>
  6. #include <string.h>
  7. #include <stdio.h>
  8. extern "C" const struct mov_buffer_t* mov_file_buffer(void);
  9. static char s_packet[2 * 1024 * 1024];
  10. static uint32_t s_track_video;
  11. static uint32_t s_track_audio;
  12. static int s_adapation_video;
  13. static int s_adapation_audio;
  14. static void mp4_onvideo(void* mpd, uint32_t track, uint8_t object, int width, int height, const void* extra, size_t bytes)
  15. {
  16. s_track_video = track;
  17. s_adapation_video = dash_mpd_add_video_adaptation_set((dash_mpd_t*)mpd, "dash-static-video", object, width, height, extra, bytes);
  18. }
  19. static void mp4_onaudio(void* mpd, uint32_t track, uint8_t object, int channel_count, int bit_per_sample, int sample_rate, const void* extra, size_t bytes)
  20. {
  21. s_track_audio = track;
  22. s_adapation_audio = dash_mpd_add_audio_adaptation_set((dash_mpd_t*)mpd, "dash-static-audio", object, channel_count, bit_per_sample, sample_rate, extra, bytes);
  23. }
  24. static void mp4_onread(void* mpd, uint32_t track, const void* buffer, size_t bytes, int64_t pts, int64_t dts, int flags)
  25. {
  26. if (s_track_video == track)
  27. {
  28. bool keyframe = 5 == (0x1f & ((uint8_t*)buffer)[4]);
  29. dash_mpd_input((dash_mpd_t*)mpd, s_adapation_video, buffer, bytes, pts, dts, keyframe ? MOV_AV_FLAG_KEYFREAME : 0);
  30. }
  31. else if (s_track_audio == track)
  32. {
  33. dash_mpd_input((dash_mpd_t*)mpd, s_adapation_audio, buffer, bytes, pts, dts, 0);
  34. }
  35. else
  36. {
  37. assert(0);
  38. }
  39. }
  40. static int dash_mpd_onsegment(void* /*param*/, int /*track*/, const void* data, size_t bytes, int64_t /*pts*/, int64_t /*dts*/, int64_t /*duration*/, const char* name)
  41. {
  42. FILE* fp = fopen(name, "wb");
  43. fwrite(data, 1, bytes, fp);
  44. fclose(fp);
  45. return 0;
  46. }
  47. static void dash_save_playlist(const char* name, const char* playlist)
  48. {
  49. char filename[256];
  50. snprintf(filename, sizeof(filename), "%s.mpd", name);
  51. FILE* fp = fopen(filename, "wb");
  52. fwrite(playlist, 1, strlen(playlist), fp);
  53. fclose(fp);
  54. }
  55. void dash_static_test(const char* mp4, const char* name)
  56. {
  57. FILE* fp = fopen(mp4, "rb");
  58. mov_reader_t* mov = mov_reader_create(mov_file_buffer(), fp);
  59. dash_mpd_t* mpd = dash_mpd_create(DASH_STATIC, dash_mpd_onsegment, NULL);
  60. struct mov_reader_trackinfo_t info = { mp4_onvideo, mp4_onaudio };
  61. mov_reader_getinfo(mov, &info, mpd);
  62. int r = mov_reader_read(mov, s_packet, sizeof(s_packet), mp4_onread, mpd);
  63. while (1 == r)
  64. {
  65. r = mov_reader_read(mov, s_packet, sizeof(s_packet), mp4_onread, mpd);
  66. }
  67. //flush
  68. dash_mpd_input(mpd, s_adapation_video, NULL, 0, 0, 0, 0);
  69. dash_mpd_playlist(mpd, s_packet, sizeof(s_packet));
  70. dash_save_playlist(name, s_packet);
  71. dash_mpd_destroy(mpd);
  72. mov_reader_destroy(mov);
  73. fclose(fp);
  74. }