RecordRTC将视频上传到节点js服务器

时间:2016-07-13 11:42:08

标签: node.js socket.io webrtc fs recordrtc

我在录制webrtc会议时使用RecordRTC。实施录制后,当我测试此应用程序时,如果两个客户端都在同一系统上,那么它的工作正常。当我在不同的系统上测试此应用程序时,它不能正常工作,并且不会记录会议。

这是我停止录制客户端的代码。

recordRTC.stopRecording(function (videoURL) {

            console.log('recordRTC.stopRecording Function inside');

            SelectedFile = recordRTC.getBlob();
            $('#uploadForm').append('#attachmentFileId', recordRTC.getBlob());
            StartUpload();
        });


    var FReader;
    var Name = "Meeting" + "_" + Date.now() + ".webm";

    function StartUpload()
    {
        FReader = new FileReader();
        FReader.onload = function (evnt)
        {
            socket.emit('Upload', { 'Name': Name, Data: evnt.target.result });
        }
        socket.emit('Start', { 'Name': Name, 'Size': SelectedFile.size });
    }

    socket.on('MoreData', function (data)
    {
        var Place = data['Place'] * 524288; //The Next Blocks Starting Position
        var NewFile; //The Variable that will hold the new Block of Data

        if (SelectedFile.webkitSlice)
            NewFile = SelectedFile.webkitSlice(Place, Place + Math.min(524288, (SelectedFile.size - Place)));
        else
            NewFile = SelectedFile.slice(Place, Place + Math.min(524288, (SelectedFile.size - Place)));
        FReader.readAsBinaryString(NewFile);
    });

服务器端代码 我是从here得到的。

socket.on('Start', function (data) { //data contains the variables that we passed through in the html file
        var Name = data['Name'];
        Files[Name] = {  //Create a new Entry in The Files Variable
            FileSize : data['Size'],
            Data     : "",
            Downloaded : 0
        }
        var Place = 0;
        try{
            var Stat = fs.statSync('Temp/' +  Name);
            if(Stat.isFile())
            {
                Files[Name]['Downloaded'] = Stat.size;
                Place = Stat.size / 524288;
            }
        }
        catch(er){} //It's a New File
        fs.open("Temp/" + Name, 'a', 0755, function(err, fd){
            if(err)
            {
                console.log(err);
            }
            else
            {
                Files[Name]['Handler'] = fd; //We store the file handler so we can write to it later
                socket.emit('MoreData', { 'Place' : Place, Percent : 0 });
            }
        });
});

socket.on('Upload', function (data){
        var Name = data['Name'];
        Files[Name]['Downloaded'] += data['Data'].length;
        Files[Name]['Data'] += data['Data'];
        if(Files[Name]['Downloaded'] == Files[Name]['FileSize']) //If File is Fully Uploaded
        {
            fs.write(Files[Name]['Handler'], Files[Name]['Data'], null, 'Binary', function(err, Writen){
            var input = fs.createReadStream("Temp/" + Name);
            var output = fs.createWriteStream("Video/" + Name);

            //util.pump(readableStream, writableStream, [callback])
            //Deprecated: Use readableStream.pipe(writableStream)
            input.pipe(output);
            input.on("end", function() {
                console.log("end");
                fs.unlink("Temp/" + Name, function ()
                { //This Deletes The Temporary File
                    console.log("unlink this file:",Name );
                    //socket.emit('Done', {'Image' : 'Video/' + Name + '.jpg'});
                });
            });
        });
        }
        else if(Files[Name]['Data'].length > 10485760){ //If the Data Buffer reaches 10MB
            fs.write(Files[Name]['Handler'], Files[Name]['Data'], null, 'Binary', function(err, Writen){
                Files[Name]['Data'] = ""; //Reset The Buffer
                var Place = Files[Name]['Downloaded'] / 524288;
                var Percent = (Files[Name]['Downloaded'] / Files[Name]['FileSize']) * 100;
                socket.emit('MoreData', { 'Place' : Place, 'Percent' :  Percent});
            });
        }
        else
        {
            var Place = Files[Name]['Downloaded'] / 524288;
            var Percent = (Files[Name]['Downloaded'] / Files[Name]['FileSize']) * 100;
            socket.emit('MoreData', { 'Place' : Place, 'Percent' :  Percent});
        }
    });

如果两个客户端在同一台机器/系统上工作正常,但如果两个客户端都在不同的系统上,则不会记录会议。

0 个答案:

没有答案