HLSTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. /**
  3. * This file is part of the PHP-FFmpeg-video-streaming package.
  4. *
  5. * (c) Amin Yazdanpanah <contact@aminyazdanpanah.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Tests\FFMpegStreaming;
  11. use Streaming\Format\StreamFormat;
  12. use Streaming\HLS;
  13. use Streaming\Stream;
  14. use Streaming\Representation;
  15. class HLSTest extends TestCase
  16. {
  17. public function testHLSClass()
  18. {
  19. $this->assertInstanceOf(Stream::class, $this->getHLS());
  20. }
  21. public function testFormat()
  22. {
  23. $hls = $this->getHLS();
  24. $hls->X264();
  25. $this->assertInstanceOf(StreamFormat::class, $hls->getFormat());
  26. }
  27. public function testAutoRepresentations()
  28. {
  29. $hls = $this->getHLS();
  30. $hls->X264()
  31. ->autoGenerateRepresentations();
  32. $representations = $hls->getRepresentations()->all();
  33. $this->assertIsArray($representations);
  34. $this->assertInstanceOf(Representation::class, current($representations));
  35. $this->assertEquals('256x144', $representations[0]->size2string());
  36. $this->assertEquals('426x240', $representations[1]->size2string());
  37. $this->assertEquals('640x360', $representations[2]->size2string());
  38. $this->assertEquals(103, $representations[0]->getKiloBitrate());
  39. $this->assertEquals(138, $representations[1]->getKiloBitrate());
  40. $this->assertEquals(207, $representations[2]->getKiloBitrate());
  41. }
  42. public function testSetHlsTime()
  43. {
  44. $hls = $this->getHLS();
  45. $hls->setHlsTime(10);
  46. $this->assertEquals(10, $hls->getHlsTime());
  47. }
  48. public function testSetHlsAllowCache()
  49. {
  50. $hls = $this->getHLS();
  51. $hls->setHlsAllowCache(false);
  52. $this->assertFalse($hls->isHlsAllowCache());
  53. }
  54. public function testSave()
  55. {
  56. $rep_1 = (new Representation())->setKiloBitrate(200)->setResize(640, 360);
  57. $rep_2 = (new Representation())->setKiloBitrate(100)->setResize(480, 270);
  58. $hls = $this->getHLS();
  59. $hls->X264()
  60. ->addRepresentations([$rep_1, $rep_2])
  61. ->save($this->srcDir . '/hls/test.m3u8');
  62. $this->assertInstanceOf(Representation::class, $rep_1);
  63. $this->assertEquals($rep_1->getKiloBitrate(), 200);
  64. $this->assertEquals($rep_2->size2string(), "480x270");
  65. $this->assertFileExists($this->srcDir . '/hls/test.m3u8');
  66. }
  67. public function testEncryptedHLS()
  68. {
  69. $this->creatKeyInfoFile();
  70. $hls = $this->getHLS();
  71. $hls->X264()
  72. ->setHlsKeyInfoFile($this->srcDir . '/enc.keyinfo')
  73. ->autoGenerateRepresentations()
  74. ->save($this->srcDir . '/enc_hls/test.m3u8');
  75. $this->assertFileExists($this->srcDir . '/enc_hls/test.m3u8');
  76. }
  77. public function testRandomEncryptedHLS()
  78. {
  79. $url = "https://www.aminyazdanpanah.com/keys/test.key";
  80. $path = $this->srcDir . DIRECTORY_SEPARATOR . "test2.key";
  81. $hls = $this->getHLS();
  82. $hls->encryption($path, $url)
  83. ->X264()
  84. ->autoGenerateRepresentations()
  85. ->save($this->srcDir . '/enc_random_hls/test.m3u8');
  86. $this->assertFileExists($this->srcDir . '/enc_random_hls/test.m3u8');
  87. }
  88. private function getHLS()
  89. {
  90. return new HLS($this->getVideo());
  91. }
  92. private function creatKeyInfoFile()
  93. {
  94. $contents[] = 'http://www.aminyazdanpanah.com/key/enc.key';
  95. $contents[] = $this->srcDir . '/enc.key';
  96. $contents[] = '17e15e333e4347e31017c5415adde26f';
  97. file_put_contents($this->srcDir . '/enc.keyinfo', implode(PHP_EOL, $contents));
  98. }
  99. }