Jump to content

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


glenskie16

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();
    }

 

Link to comment
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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.