diff --git a/classes/VideoDownload.php b/classes/VideoDownload.php index 345d2eb..6543a85 100644 --- a/classes/VideoDownload.php +++ b/classes/VideoDownload.php @@ -324,4 +324,16 @@ class VideoDownload return popen($procBuilder->getProcess()->getCommandLine(), 'r'); } + + /** + * Get video stream from an RTMP video. + * + * @param \stdClass $video Video object returned by getJSON + * + * @return resource popen stream + */ + public function getRtmpStream(\stdClass $video) + { + return popen($this->getRtmpProcess($video)->getCommandLine(), 'r'); + } } diff --git a/controllers/FrontController.php b/controllers/FrontController.php index 3b45ece..55011be 100644 --- a/controllers/FrontController.php +++ b/controllers/FrontController.php @@ -305,7 +305,13 @@ class FrontController private function getStream($url, $format, $response, $request, $password = null) { $video = $this->download->getJSON($url, $format, $password); - if ($video->protocol == 'm3u8') { + if ($video->protocol == 'rtmp') { + $stream = $this->download->getRtmpStream($video); + $response = $response->withHeader('Content-Type', 'video/'.$video->ext); + if ($request->isGet()) { + $response = $response->withBody(new Stream($stream)); + } + } elseif ($video->protocol == 'm3u8') { $stream = $this->download->getM3uStream($video); $response = $response->withHeader('Content-Type', 'video/'.$video->ext); if ($request->isGet()) { diff --git a/tests/FrontControllerTest.php b/tests/FrontControllerTest.php index c1b64ce..eb99c2c 100644 --- a/tests/FrontControllerTest.php +++ b/tests/FrontControllerTest.php @@ -344,6 +344,21 @@ class FrontControllerTest extends \PHPUnit_Framework_TestCase $this->assertTrue($result->isOk()); } + /** + * Test the redirect() function with an RTMP stream. + * + * @return void + */ + public function testRedirectWithRtmpStream() + { + $controller = new FrontController($this->container, new Config(['stream'=>true])); + $result = $controller->redirect( + $this->request->withQueryParams(['url'=>'http://www.rtl2.de/sendung/grip-das-motormagazin/folge/folge-203-0']), + $this->response + ); + $this->assertTrue($result->isOk()); + } + /** * Test the redirect() function with a missing password. * diff --git a/tests/VideoDownloadTest.php b/tests/VideoDownloadTest.php index e207ef5..ce0426e 100644 --- a/tests/VideoDownloadTest.php +++ b/tests/VideoDownloadTest.php @@ -196,6 +196,23 @@ class VideoDownloadTest extends \PHPUnit_Framework_TestCase ]; } + /** + * Provides RTMP URLs for tests. + * + * @return array[] + */ + public function rtmpUrlProvider() + { + return [ + [ + 'http://www.rtl2.de/sendung/grip-das-motormagazin/folge/folge-203-0', 'bestaudio/best', + 'GRIP sucht den Sommerkönig-folge-203-0', + 'f4v', + 'edgefcs.net', + ], + ]; + } + /** * Provides incorrect URLs for tests. * @@ -373,6 +390,23 @@ class VideoDownloadTest extends \PHPUnit_Framework_TestCase $this->assertFalse(feof($stream)); } + /** + * Test getRtmpStream function. + * + * @param string $url URL + * @param string $format Format + * + * @return void + * @dataProvider rtmpUrlProvider + */ + public function testGetRtmpStream($url, $format) + { + $video = $this->download->getJSON($url, $format); + $stream = $this->download->getRtmpStream($video); + $this->assertInternalType('resource', $stream); + $this->assertFalse(feof($stream)); + } + /** * Test getM3uStream function without avconv. *