VideoPlayer165Tests.inc 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. // --- Fruity Video Player (core/VideoPlayer.h) - round 165g
  2. #include "core/VideoPlayer.h"
  3. TEST (video165_fl_state)
  4. {
  5. using namespace lynx::video;
  6. // FL 2026's Default.fst (0xD5 payload, 37 bytes): version 3, flags 0x92, time display 0, 3 params (100, 0, 0),
  7. // no path, the window size 375 x 0
  8. const std::vector<std::uint8_t> fl = { 0x03, 0, 0, 0, 0x92, 0, 0, 0, 0, 0, 0, 0, 0x03, 0, 0, 0, 0x64, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  9. 0x00, 0x77, 0x01, 0, 0, 0, 0, 0, 0 };
  10. const auto s = read (fl);
  11. CHECK (s.has_value());
  12. if (! s)
  13. return;
  14. CHECK (s->keepAspect && s->enableAudio && s->slave && ! s->loop && ! s->ignoreTempo && ! s->dock && s->transport == 0);
  15. CHECK (s->volume == 100 && ! s->mute && ! s->play && s->path.empty() && s->width == 375 && s->height == 0);
  16. CHECK (s->timeDisplay == hms && s->delay == 0);
  17. // written back: FL's bytes, then LYNX's tail
  18. const auto back = write (*s);
  19. CHECK (back.size() == fl.size() + 16 && std::equal (fl.begin(), fl.end(), back.begin()));
  20. CHECK (read (back) == s);
  21. // a defaulted Settings is FL's default
  22. CHECK (Settings {} == *s);
  23. // a path (UTF-16, with a non-ASCII letter), times, every flag
  24. Settings v;
  25. v.path = "C:\\Users\\kauan\\V\xc3\xad" "deos\\clip.mp4";
  26. v.loop = true;
  27. v.keepAspect = false;
  28. v.transport = 2;
  29. v.enableAudio = false;
  30. v.ignoreTempo = true;
  31. v.slave = false;
  32. v.dock = true;
  33. v.timeDisplay = smpte5994drop;
  34. v.volume = 127;
  35. v.mute = true;
  36. v.play = true;
  37. v.width = 640;
  38. v.height = 360;
  39. v.start = 12345678;
  40. v.end = 98765432;
  41. v.delay = unitsPerSecond * 3;
  42. const auto bytes = write (v);
  43. CHECK (read (bytes) == v);
  44. // the path's length byte and characters as FL writes them (FUN_0071b530)
  45. CHECK (bytes[28] == 30 && bytes[29] == 'C' && bytes[30] == 0 && bytes[29 + 2 * 16] == 0xed && bytes[30 + 2 * 16] == 0);
  46. std::uint32_t flags = 0;
  47. std::memcpy (&flags, bytes.data() + 4, 4);
  48. CHECK (flags == (1u | 8u | 0x40u | 0x100u));
  49. // older states: the audio switch in bits 4|5, slaved by default before version 3
  50. auto old = fl;
  51. old[0] = 2;
  52. old[4] = 0x20;
  53. CHECK (! read (old).has_value() || read (old)->enableAudio);
  54. CHECK (! read ({ 9, 0, 0, 0 }).has_value() && ! read ({}).has_value());
  55. }
  56. TEST (video165_time_display)
  57. {
  58. using namespace lynx::video;
  59. CHECK (formatTime (0.0, hms, 0.0) == "0:00:00:000");
  60. CHECK (formatTime (3723.456, hms, 0.0) == "1:02:03:456");
  61. CHECK (formatTime (-1.25, hms, 0.0) == "-0:00:01:250");
  62. CHECK (formatTime (1.52, smpte25, 0.0) == "0:00:01:13");
  63. CHECK (formatTime (59.99, smpte30, 0.0) == "0:00:59:29");
  64. // drop frame: 29.97 skips frames 0 and 1 at each minute but the tenth
  65. CHECK (formatTime (1800.0 / 29.97, smpte2997drop, 0.0) == "0:01:00;02");
  66. CHECK (formatTime (17982.0 / 29.97, smpte2997drop, 0.0) == "0:10:00;00");
  67. CHECK (formatTime (1800.0 / 29.97, smpte2997, 0.0) == "0:01:00:00");
  68. CHECK (formatTime (2.0, smpteAuto, 24.0) == "0:00:02:00" && formatTime (2.5, smpteAuto, 24.0) == "0:00:02:12");
  69. CHECK (timeDisplayFps (smpte23976, 0.0) == 23.976 && timeDisplayDrops (smpte5994drop) && ! timeDisplayDrops (smpte5994));
  70. }
  71. TEST (video165_clock_and_report)
  72. {
  73. using namespace lynx::video;
  74. Settings s;
  75. s.delay = unitsPerSecond; // starts when the song reaches 1 s
  76. s.start = 2 * unitsPerSecond; // from the video's 2 s
  77. s.end = 5 * unitsPerSecond; // to 5 s
  78. CHECK (Clock::videoTime (0.5, s, 10.0) < 0.0);
  79. CHECK (std::abs (Clock::videoTime (1.0, s, 10.0) - 2.0) < 1e-9 && std::abs (Clock::videoTime (3.5, s, 10.0) - 4.5) < 1e-9);
  80. CHECK (std::abs (Clock::videoTime (9.0, s, 10.0) - 5.0) < 1e-9); // holds the end
  81. s.loop = true;
  82. CHECK (std::abs (Clock::videoTime (5.0, s, 10.0) - 3.0) < 1e-9); // 4 s in = one 3-s pass + 1 s
  83. s.end = 0; // to the video's end
  84. s.loop = false;
  85. CHECK (std::abs (Clock::videoTime (20.0, s, 10.0) - 10.0) < 1e-9);
  86. const std::string report =
  87. "Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'clip.mp4':\n"
  88. " Duration: 00:01:02.50, start: 0.000000, bitrate: 1205 kb/s\n"
  89. " Stream #0:0[0x1](und): Video: h264 (High) (avc1 / 0x31637661), yuv420p(progressive), 1280x720 [SAR 1:1 DAR 16:9], 1072 kb/s, 29.97 fps, 29.97 tbr, 30k tbn (default)\n"
  90. " Stream #0:1[0x2](und): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 128 kb/s (default)\n";
  91. const auto info = parseFfmpegReport (report);
  92. CHECK (info.hasVideo && info.hasAudio && info.width == 1280 && info.height == 720);
  93. CHECK (std::abs (info.fps - 29.97) < 1e-9 && std::abs (info.duration - 62.5) < 1e-9);
  94. const auto none = parseFfmpegReport ("Input #0, wav, from 'a.wav':\n Duration: 00:00:03.00, bitrate: 1411 kb/s\n Stream #0:0: Audio: pcm_s16le, 44100 Hz\n");
  95. CHECK (! none.hasVideo && none.hasAudio);
  96. }
  97. TEST (video165_instrument_plays_the_track)
  98. {
  99. using namespace lynx;
  100. // a 2-s ramp-like sound track at 48 kHz (left = sample index, right = minus it)
  101. auto track = std::make_shared<video::AudioTrack>();
  102. track->rate = 48000.0;
  103. for (int i = 0; i < 96000; ++i)
  104. {
  105. track->samples.push_back ((std::int16_t) (i % 30000));
  106. track->samples.push_back ((std::int16_t) -(i % 30000));
  107. }
  108. video::setAudioProvider ([track] (const std::string& path, bool) -> std::shared_ptr<const video::AudioTrack> {
  109. return path == "clip.mp4" ? track : nullptr;
  110. });
  111. auto inst = video::makeInstrument();
  112. CHECK (video::playerOf (inst.get()) != nullptr);
  113. ChannelDef def;
  114. def.instrument = InstrumentKind::videoplayer;
  115. video::Settings s;
  116. s.path = "clip.mp4";
  117. s.delay = video::unitsPerSecond / 2; // the video starts at 0.5 s of the song
  118. def.pluginState = video::write (s);
  119. const double bpm = 120.0, rate = 48000.0;
  120. inst->prepare (rate);
  121. inst->configure (def, bpm);
  122. // stopped: silent
  123. std::vector<float> l (480, 0.0f), r (480, 0.0f);
  124. inst->setSongPosition (0.0);
  125. inst->render (l.data(), r.data(), 480, 1.0f, 1.0f);
  126. CHECK (std::all_of (l.begin(), l.end(), [] (float x) { return x == 0.0f; }));
  127. // play from the song's 1.0 s: the video is at 0.5 s → frame 24000
  128. const auto ticksPerSample = bpm * ppq / (60.0 * rate);
  129. auto ticks = 1.0 * bpm / 60.0 * ppq;
  130. double at = ticks;
  131. inst->setSongPosition (at - 480 * ticksPerSample); // (the block before: it moves)
  132. inst->render (l.data(), r.data(), 480, 1.0f, 1.0f);
  133. std::fill (l.begin(), l.end(), 0.0f);
  134. std::fill (r.begin(), r.end(), 0.0f);
  135. inst->setSongPosition (at);
  136. inst->render (l.data(), r.data(), 480, 1.0f, 1.0f);
  137. CHECK (std::abs (l[0] * 32768.0f - 24000.0f) < 1.5f && std::abs (l[100] * 32768.0f - 24100.0f) < 1.5f);
  138. CHECK (std::abs (r[100] + l[100]) < 1e-6f);
  139. // muted / volume 50 %
  140. s.volume = 50;
  141. def.pluginState = video::write (s);
  142. inst->configure (def, bpm);
  143. std::fill (l.begin(), l.end(), 0.0f);
  144. at += 480 * ticksPerSample;
  145. inst->setSongPosition (at);
  146. inst->render (l.data(), r.data(), 480, 1.0f, 1.0f);
  147. CHECK (std::abs (l[0] * 32768.0f - 24480.0f * 0.5f) < 1.5f);
  148. s.mute = true;
  149. def.pluginState = video::write (s);
  150. inst->configure (def, bpm);
  151. CHECK (inst->idle());
  152. // before the delay: silence
  153. s.mute = false;
  154. def.pluginState = video::write (s);
  155. inst->configure (def, bpm);
  156. std::fill (l.begin(), l.end(), 0.0f);
  157. inst->setSongPosition (0.0);
  158. inst->setSongPosition (480 * ticksPerSample);
  159. inst->render (l.data(), r.data(), 480, 1.0f, 1.0f);
  160. CHECK (std::all_of (l.begin(), l.end(), [] (float x) { return x == 0.0f; }));
  161. video::setAudioProvider (nullptr);
  162. }