muaz-khan / RTCMultiConnection-Server

RTCMultiConnection socket.io server (npm install rtcmulticonnection-server)
https://muazkhan.com:9001/
MIT License
277 stars 147 forks source link

RecordRTC to PHP #30

Closed rayj00 closed 3 years ago

rayj00 commented 3 years ago

I am able to record a Scalable Broadcast. However I cannot send the webm to the server. None of the PHP (save.php) are working for me. I am currently trying the following:

index.html var fileType = 'video'; // or "audio" var fileName = 'ABCDEF.webm'; // or "wav"

var formData = new FormData(); formData.append(fileType + '-filename', fileName); formData.append(fileType + '-blob', blob);

xhr('save.php', formData, function (fName) { // window.open(location.href + fName); });

function xhr(url, data, callback) { var request = new XMLHttpRequest(); request.onreadystatechange = function () { if (request.readyState == 4 && request.status == 200) { callback(location.href + request.responseText); } }; request.open('POST', url, true); console.log(" POST url = ", url); request.send(data); }

save.php

<?php // Muaz Khan - www.MuazKhan.com // MIT License - https://www.webrtc-experiment.com/licence/ // Documentation - https://github.com/muaz-khan/RecordRTC

header("Access-Control-Allow-Origin: *"); error_reporting(E_ALL); ini_set('display_errors', 1);

set_error_handler("someFunction");

function someFunction($errno, $errstr) { echo '

Upload failed.


'; echo '

'.$errstr.'

'; }

function selfInvoker() { if (!isset($_POST['audio-filename']) && !isset($_POST['video-filename'])) { echo 'Empty file name.'; return; }

// do NOT allow empty file names
if (empty($_POST['audio-filename']) && empty($_POST['video-filename'])) {
    echo 'Empty file name.';
    return;
}

// do NOT allow third party audio uploads
if (false && isset($_POST['audio-filename']) && strrpos($_POST['audio-filename'], "RecordRTC-") !== 0) {
    echo 'File name must start with "RecordRTC-"';
    return;
}

// do NOT allow third party video uploads
if (false && isset($_POST['video-filename']) && strrpos($_POST['video-filename'], "RecordRTC-") !== 0) {
    echo 'File name must start with "RecordRTC-"';
    return;
}

$fileName = '';
$tempName = '';
$file_idx = '';

if (!empty($_FILES['audio-blob'])) {
    $file_idx = 'audio-blob';
    $fileName = $_POST['audio-filename'];
    $tempName = $_FILES[$file_idx]['tmp_name'];
} else {
    $file_idx = 'video-blob';
    $fileName = $_POST['video-filename'];
    $tempName = $_FILES[$file_idx]['tmp_name'];
}

if (empty($fileName) || empty($tempName)) {
    if(empty($tempName)) {
        echo 'Invalid temp_name: '.$tempName;
        return;
    }

    echo 'Invalid file name: '.$fileName;
    return;
}

/*
$upload_max_filesize = return_bytes(ini_get('upload_max_filesize'));
if ($_FILES[$file_idx]['size'] > $upload_max_filesize) {
   echo 'upload_max_filesize exceeded.';
   return;
}
$post_max_size = return_bytes(ini_get('post_max_size'));
if ($_FILES[$file_idx]['size'] > $post_max_size) {
   echo 'post_max_size exceeded.';
   return;
}
*/

$filePath = 'uploads/' . $fileName;

// make sure that one can upload only allowed audio/video files
$allowed = array(
    'webm',
    'wav',
    'mp4',
    'mkv',
    'mp3',
    'ogg'
);
$extension = pathinfo($filePath, PATHINFO_EXTENSION);
if (!$extension || empty($extension) || !in_array($extension, $allowed)) {
    echo 'Invalid file extension: '.$extension;
    return;
}

if (!move_uploaded_file($tempName, $filePath)) {
    if(!empty($_FILES["file"]["error"])) {
        $listOfErrors = array(
            '1' => 'The uploaded file exceeds the upload_max_filesize directive in php.ini.',
            '2' => 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.',
            '3' => 'The uploaded file was only partially uploaded.',
            '4' => 'No file was uploaded.',
            '6' => 'Missing a temporary folder. Introduced in PHP 5.0.3.',
            '7' => 'Failed to write file to disk. Introduced in PHP 5.1.0.',
            '8' => 'A PHP extension stopped the file upload. PHP does not provide a way to ascertain which extension caused the file upload to stop; examining the list of loaded extensions with phpinfo() may help.'
        );
        $error = $_FILES["file"]["error"];

        if(!empty($listOfErrors[$error])) {
            echo $listOfErrors[$error];
        }
        else {
            echo 'Not uploaded because of error #'.$_FILES["file"]["error"];
        }
    }
    else {
        echo 'Problem saving file: '.$tempName;
    }
    return;
}

echo 'success';

}

/ function return_bytes($val) { $val = trim($val); $last = strtolower($val[strlen($val)-1]); switch($last) { // The 'G' modifier is available since PHP 5.1.0 case 'g': $val = 1024; case 'm': $val = 1024; case 'k': $val = 1024; } return $val; } */

selfInvoker(); ?>

And my dev console I have: RecordRTC version: 5.5.9 17:38:51.966 RecordRTC.js:63 started recording video stream. 17:38:51.967 RecordRTC.js:1057 Using recorderType: MediaStreamRecorder 17:38:51.968 RecordRTC.js:2104 Passing following config over MediaRecorder API. {type: "video", mimeType: "video/webm", checkForInactiveTracks: false, initCallback: ƒ} 17:38:51.969 RecordRTC.js:713 Recorder state changed: recording 17:38:51.969 RecordRTC.js:103 Initialized recorderType: MediaStreamRecorder for output-type: video 17:38:53.519 RecordRTC.js:129 Stopped recording video stream. 17:38:53.520 RecordRTC.js:713 Recorder state changed: stopped 17:38:53.522 RecordRTC.js:170 video/x-matroska;codecs=avc1,opus -> 121 KB 17:38:53.523 (index):347 POST url = save.php

The uploads folder is empty and I am not seeing any entries in the apache log file for PHP error?

Ideas?

Ray