| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * This file is part of the PHP-FFmpeg-video-streaming package. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * (c) Amin Yazdanpanah <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * For the full copyright and license information, please view the LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * file that was distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | namespace Streaming; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | class HLSPlaylist | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      * @param string $filename | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |      * @param array $reps | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      * @param string $manifests | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      * @param array $stream_info | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     public static function save(string $filename, array $reps, string $manifests, array $stream_info = []): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         file_put_contents($filename, static::contents($reps, $manifests, $stream_info)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * @param array $reps | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * @param string $manifests | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * @param array $stream_info | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     private static function contents(array $reps, string $manifests, array $stream_info = []): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         $content = ["#EXTM3U", "#EXT-X-VERSION:3"]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         foreach ($reps as $rep) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |             $content[] = static::streamInfo($rep, $stream_info); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |             $content[] = $manifests . "_" . $rep->getHeight() . "p.m3u8"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         return implode(PHP_EOL, $content); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * @param Representation $rep | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * @param array $extra | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      * @return string | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 49 |  |  |      */ | 
            
                                                        
            
                                    
            
            
                | 50 |  |  |     private static function streamInfo(Representation $rep, array $extra = []): string | 
            
                                                        
            
                                    
            
            
                | 51 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 52 |  |  |         $ext_stream = '#EXT-X-STREAM-INF:'; | 
            
                                                        
            
                                    
            
            
                | 53 |  |  |         $params = [ | 
            
                                                        
            
                                    
            
            
                | 54 |  |  |             "BANDWIDTH=" . $rep->getKiloBitrate() * 1024, | 
            
                                                        
            
                                    
            
            
                | 55 |  |  |             "RESOLUTION=" . $rep->getResize(), | 
            
                                                        
            
                                    
            
            
                | 56 |  |  |             "NAME=\"" . $rep->getHeight() . "\"" | 
            
                                                        
            
                                    
            
            
                | 57 |  |  |         ]; | 
            
                                                        
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 59 |  |  |         return $ext_stream . implode(",", array_merge($params, $extra)); | 
            
                                                        
            
                                    
            
            
                | 60 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 61 |  |  | } |