I am trying to stream mp4 videos using html5 on mobile browsers with PHP.
I am using the following class:
http://labs.divx.com/node/17030
The script works fine except for the reference to the mime_types array which I had to remove the $ to make it work.
Anyhow, my problem is this. When I run this script with a file.mp4 it runs fine on my phone.
But as soon as I put code in to validate if the file is allowed to be played, etc. (if the customer has purchased the video, if they have exceeded the number of views allowed, etc)
The file won't load at all.
It does load in desktop browsers though. It is only in mobile browsers that it doesn't load.
Here is the code I use to play the file:
When I run it like this, it works in desktop browsers, but not mobile:
<?php
apc_clear_cache();
session_start();
require(dirname(__FILE__) . "/../_includes/config.php");
require(dirname(__FILE__) . "/../_includes/validate_session.php");
$id = (int)$_GET['id'];
if (!$result = $mysqli->query('SELECT
orders_item_product_downloadable_videos.id,
orders_item_product_downloadable_videos.current_no_downloads,
DATE_ADD(IF(orders.id IS NOT NULL,orders.date_order,o.date_order),INTERVAL orders_item_product_downloadable_videos.no_days_expire DAY) AS date_expire,
product_downloadable_videos.embed_code,
orders_item_product_downloadable_videos.no_days_expire,
orders_item_product_downloadable_videos.no_downloads,
product_downloadable_videos.filename,
product_downloadable_videos.stream,
product_downloadable_videos.source
FROM
orders_item_product_downloadable_videos
LEFT JOIN
(orders_item_product CROSS JOIN orders_item CROSS JOIN orders)
ON
(orders_item_product_downloadable_videos.id_orders_item_product = orders_item_product.id AND orders_item_product.id_orders_item = orders_item.id AND orders_item.id_orders = orders.id)
LEFT JOIN
(orders_item_product AS oip CROSS JOIN orders_item_product AS oip_parent CROSS JOIN orders_item AS oi CROSS JOIN orders AS o)
ON
(orders_item_product_downloadable_videos.id_orders_item_product = oip.id AND oip.id_orders_item_product = oip_parent.id AND oip_parent.id_orders_item = oi.id AND oi.id_orders = o.id)
INNER JOIN
product_downloadable_videos
ON
(orders_item_product_downloadable_videos.id_product_downloadable_videos = product_downloadable_videos.id)
WHERE
orders_item_product_downloadable_videos.id = "'.$id.'"
AND
(
(orders.id IS NOT NULL AND orders.status IN (1,7) AND orders.id_customer = "'.(int)$_SESSION['customer']['id'].'")
OR
(o.id IS NOT NULL AND o.status IN (1,7) AND o.id_customer = "'.(int)$_SESSION['customer']['id'].'")
)
LIMIT 1')) throw new Exception('An error occured while trying to get video info.'."
".$mysqli->mysqli->error);
if (!$result->num_rows) {
//throw new Exception(language('account/order', 'ERROR_DOWNLOAD_EXPIRED'));
exit;
}
$row = $result->fetch_assoc();
$result->free();
if ($row['no_downloads'] > 0 && $row['current_no_downloads'] >= $row['no_downloads']) {
//throw new Exception(language('account/order', 'ERROR_NO_DOWNLOADS_REACHED'));
exit;
}
if ($row['no_days_expire'] > 0 && strtotime($row['date_expire']) <= time()) {
//throw new Exception(language('account/order', 'ERROR_DOWNLOAD_EXPIRED'));
exit;
}
$file = dirname(__FILE__).'/../admin/protected/streaming_videos/'.$row['source'];
require(dirname(__FILE__).'/../_includes/classes/stream.php');
$st = new VSTREAM();
$st->stream(dirname(__FILE__).'/../397d729225599bcda978ac4fd4aaeba2.mp4');
When I comment and run it like this, it works in mobile browsers, but why?
<?php
apc_clear_cache();
session_start();
require(dirname(__FILE__) . "/../_includes/config.php");
require(dirname(__FILE__).'/../_includes/classes/stream.php');
$st = new VSTREAM();
$st->stream(dirname(__FILE__).'/../397d729225599bcda978ac4fd4aaeba2.mp4');
If I add the validation_session.php code, it doesn't work.
<?php
apc_clear_cache();
session_start();
require(dirname(__FILE__) . "/../_includes/config.php");
require(dirname(__FILE__) . "/../_includes/validate_session.php");
require(dirname(__FILE__).'/../_includes/classes/stream.php');
$st = new VSTREAM();
$st->stream(dirname(__FILE__).'/../397d729225599bcda978ac4fd4aaeba2.mp4');
The validation_session.php code only has this inside:
<?php
if(!$_SESSION['customer']['id']){
header("Location: /account/login");
exit;
}
?>
I even tested the session by outputting it on the screen with the non working short code and i could see the $_SESSION['customer']['id'] did exist.
So I do not know what I am missing here.
If anybody has any idea why this is not working, I would really appreciate your input on this!
Thanks!
Unless the UID for the session is propagated in the URL, it is stored in a cookie. If the page works on a desktop without issue and only works on the mobile device when you aren't validating the session, the issue has to be linked to cookies somewhere. Most likely, your mobile browser isn't saving cookies and therefore is kicking you out when it tries to confirm the session.
Alternatively, you could pass the session ID in the URL: http://php.net/manual/en/session.idpassing.php
If you can get it to work with the session ID in the query string, then you know it is a cookies issue.
Ok so to conclude this issue, the main reason for the mobile playback was the maximum resolution per device.
And for the windows phone 8 device issue, I still have got no clue. It seems intermittent, I was able to play it off another server then back on mine by altering some code and removing it later, but now it seems to work and i don't know why so i am not touching it.