【问题标题】:Simultaneous AJAX calls during HTTP streaming在 HTTP 流期间同时调用 AJAX
【发布时间】:2012-08-21 13:47:38
【问题描述】:

我有一个运行 Web 应用程序的 Apache 服务器。在这个 web 应用程序中,我展示了一个使用 JWPlayer 的视频。 JWPlayer 使用 http 伪流从提供此视频的 PHP 脚本中获取视频。所有这些都运行良好,视频流式传输良好。

我遇到的问题是在视频流式传输时,我还使用 AJAX 调用来获取同一页面上的 Adob​​e Flash 文件使用的一些 XML 文件。在流式传输时,这些 XML 文件提取将保持“待处理”,直到加载整个视频。使用 Chrome,我可以看到视频被逐字节加载。视频完全加载后,将获取 XML 文件。此外,如果我在视频流式传输时在浏览器中打开另一个选项卡并尝试再次加载 Web 应用程序,则在视频完全加载之前它也不会显示。

这似乎是某种 Apache 设置。 apache 的 MPM 设置为:

ThreadsPerChild 150 MaxRequestsPerChild 0

这似乎是正确的。有什么想法可能是错的吗?

【问题讨论】:

  • 我的猜测:也许 Flash 使用浏览器的 XMLHTTPRequest 对象来处理 HTTP 伪流。这可能会阻止该对象可用于其他请求。
  • 也许Keep-alive 是问题所在? httpd.conf 中的值是多少?
  • 超时 300 KeepAlive On MaxKeepAliveRequests 100 KeepAliveTimeout 5

标签: ajax apache streaming jwplayer


【解决方案1】:

如果您使用的是 PHP 会话,那么这可能是导致 IO 阻塞的原因。

php blocking when calling the same file concurrently

【讨论】:

  • 谢谢!在输出视频内容之前添加 session_write_close() 就可以了。
【解决方案2】:

我正在制作一个带有私人视频流的系统。所以,我需要通过 php 进行流式传输,因为使用 php 编程我能够重新设置用户访问权限。

我在流式传输视频和在服务器上执行其他脚本时遇到问题。 使用 session_write_close() 解决了打开另一个脚本的问题,我发现网络上的脚本对我有很大帮助。

我想分享,因为该脚本会进行真正的流式传输。 我在http://www.tuxxin.com/php-mp4-streaming/ 网站上找到了它。 感谢这段代码的作者=D

尽情享受吧!

<?php
$file = 'video360p.mp4';
$fp = @fopen($file, 'rb');
$size   = filesize($file); // File size
$length = $size;           // Content length
$start  = 0;               // Start byte
$end    = $size - 1;       // End byte
header('Content-type: video/mp4');
//header("Accept-Ranges: 0-$length");
header("Accept-Ranges: bytes");
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 = ($c_end > $end) ? $end : $c_end;
    if ($c_start > $c_end || $c_start > $size - 1 || $c_end >= $size) {
        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);
$buffer = 1024 * 8;
while(!feof($fp) && ($p = ftell($fp)) <= $end) {
    if ($p + $buffer > $end) {
        $buffer = $end - $p + 1;
    }
    set_time_limit(0);
    echo fread($fp, $buffer);
    flush();
}
fclose($fp);
exit();
?>

【讨论】:

    猜你喜欢
    • 2012-01-31
    • 2021-03-03
    • 1970-01-01
    • 1970-01-01
    • 2017-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-10
    相关资源
    最近更新 更多