Jump to content
glenskie16

PHP stream mp4, detect when client stops streaming, or disconnects

Recommended Posts

I am currently streaming my mp4 files perfectly fine using php. My issue is, how do I detect when the video is either no longer being watched, or disconnected? I need to run a function after this so that I know the video is no longer being watched for analytics. Is there a way maybe with sockets or something? I'm currently using this.

 

if (file_exists($request)) {
        $fp = @fopen($request, 'rb');
        $size = filesize($request);
        $length = $size;
        $start = 0;
        $end = $size - 1;
        header("Content-Type: video/mp4");
        header('Accept-Ranges: 0-' . $length);

        if (isset($_SERVER['HTTP_RANGE'])) {
            $c_start = $start;
            $c_end = $end;
            list(, $range) = explode('=', $_SERVER['HTTP_RANGE'], 2);

            if (strpos($range, ',') !== false) {
                header('HTTP/1.1 416 Requested Range Not Satisfiable');
                header('Content-Range: bytes ' . $start . '-' . $end . '/' . $size);
                exit();
            }

            if ($range == '-') {
                $c_start = $size - substr($range, 1);
            }
            else {
                $range = explode('-', $range);
                $c_start = $range[0];
                $c_end = (isset($range[1]) && is_numeric($range[1]) ? $range[1] : $size);
            }

            $c_end = ($end < $c_end ? $end : $c_end);
            if (($c_end < $c_start) || (($size - 1) < $c_start) || ($size <= $c_end)) {
                header('HTTP/1.1 416 Requested Range Not Satisfiable');
                header('Content-Range: bytes ' . $start . '-' . $end . '/' . $size);
                exit();
            }

            $start = $c_start;
            $end = $c_end;
            $length = ($end - $start) + 1;
            fseek($fp, $start);
            header('HTTP/1.1 206 Partial Content');
        }

        header('Content-Range: bytes ' . $start . '-' . $end . '/' . $size);
        header('Content-Length: ' . $length);
        ob_end_flush();
        $buffer = 8192;
        $time_start = time();
        $bytes_read = 0;

        while (!feof($fp) && (($p = ftell($fp)) <= $end)) {
            $response = stream_get_line($fp, $buffer);
            echo $response;
            $bytes_read += strlen($response);

            if (30 <= time() - $time_start) {
                file_put_contents('/var/www/html/movieCon/'.$user.'_'.$token.'_'.$movie_id.'.con', time());
                $time_start = time();
                $bytes_read = 0;
            }
        }

        fclose($fp);
        exit();
    }

 

Share this post


Link to post
Share on other sites

You should have had to do that for your script in its current form anyways.

Share this post


Link to post
Share on other sites

Define "no longer being watched".  The video ending is pretty clear, but not being watched could be a number of things.

  • No longer in window scroll view
  • Video has been stopped
  • Video has been paused

How are you streaming this mp4?  Though an HTML5 player?  Or, do you have a specialized library for outputting the video to a specific third-party video player?  Or, perhaps a own roll-your-own version of a video player?  In any case, these events should be available from the video player itself.  PHP is just going to grab the mp4 and output it for the video player to use.

Share this post


Link to post
Share on other sites
8 minutes ago, Zane said:

but not being watched could be a number of things.

  • The viewer has fallen asleep
  • The viewer has gone to make a drink
  • etc

Share this post


Link to post
Share on other sites

The viewer make have taken off their glasses or been stabbed in the eyes.  Perhaps the cat walked in front of the computer screen. :P

Share this post


Link to post
Share on other sites
3 minutes ago, Zane said:

Perhaps the cat walked in front of the computer screen

My cat was always doing that - puked all over the keyboard once. Had to get a new one (keyboard, that is)

Share this post


Link to post
Share on other sites
1 minute ago, Barand said:

My cat [...] puked all over the keyboard

Yes another reason to stop watching the video.

Share this post


Link to post
Share on other sites

So like if they just disconnect from the stream. Also it can be in any player, like VLC, html5, so it would need to know if the person disconnected from the stream, or if they lost connection or any situation.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.