MediaSteram Class for PHP

My PHP Stuff

A lot of video service like YouTube uses a secret link

(like this:

)

to send video data to the flash player. We don’t know the video file actually from what part of the server.

Actually, it sends a header (It is: “Content-Type: video/x-flv”) to the flash player and let the flash player downloads that file which contains the video data. After finding out this, I made this class for this purpose.


<?php

class MediaStream

{

    private 
$resource;

    private 
$type;

    private 
$handle;

    private 
$base64;

    public 
$error;

    

    public function 
__construct($a,$type,$base64 false,$context NULL)

    {

        
//make sure $a is set

        
if(!isset($a))

        {

            
$this->error 'You missed argument 1 : $a';

        }

        

        
//the stream is a base64 encoded string, process it

        
if($base64)

        {

            
$this->getResourceFromBase64EncodedString($a);

            
$this->base64 $base64;

        }

        else if(
preg_match("/(.*)(\.(.*))$/i",$a) || preg_match("/^(http:|ftp:|https:)(.*)$/",$a))

        {

            
//$a is not a base64 encoded string but it's a file or a url, open it and save it into a variable

            
if(isset($context))

            {

                
//user had defined the context

                
$this->handle = @fopen($a,"rb",false,$context);

            }

            else

            {

                
$this->handle = @fopen($a,"rb");

            }

        }

        else

        {

            
//$a ia neither a base64 encoded string nor a filename

            
$this->error "Error occured! The source is neither a base64 encoded string nor a file.";

        }

        
$this->type = isset($type) ? $type : ($this->error "Error occured! You haven't set the MIME type of the resource.");

    }

    

    private function 
getResourceFromBase64EncodedString($a)

    {

        if(isset(
$a))

        {

            
set_time_limit(0); //prevent large data being cut off

            
$this->resource base64_decode($a);

        }

        else

        {

            return 
false;

        }

    }

    

    public function 
OutputStream()

    {

        
//if error occured, stop output the stream

        
if(isset($this->error))

        {

            die(
$this->error);

        }

        
//determine if the resource is decoded from base64 encoded string

        
if($this->base64)

        {

            
header("Content-type: ".$this->type);

            echo 
$this->resource;

        }

        else if(isset(
$this->handle))//the resource is a file handle, output all contents in the file.

        
{

            if(
$this->handle === false)

            {

                die(
"Error occured while open the stream.");

            }

            

            
header("Content-type: ".$this->type);

            
fpassthru($this->handle);

        }

        else

        {

            die(
"No contents to output.");

        }

    }

    

    public function 
__destruct()

    {

        unset(
$this->resource);

        unset(
$this->type);

        if(isset(
$this->handle) && $this->handle !== false)

        {

            
fclose($this->handle);    

        }

        unset(
$this->handle);

        unset(
$this->error);

    }

}

//examples

$s = new MediaStream("es.php.net.flv","video/x-flv");

$s->OutputStream();

$s->__destruct();

?>

Hope it’ll help you. This time the PHP source code has coloured so it’s easier to read. If you found bugs please email or leave a comment to me.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s