Skip to content

Stream video with GridStoreAdapter (implements byte-range requests) #2437

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions spec/ParseFile.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,27 @@ describe('Parse.File testing', () => {
});
});

it('supports byte-range requests when requesting a video', done => {
var headers = {
'Content-Type': 'video/mp4',
'X-Parse-Application-Id': 'test',
'X-Parse-REST-API-Key': 'rest'
};
request.post({
headers: headers,
url: 'http://localhost:8378/1/files/file',
body: '101010101001010101010101010101010010110101010101010101010'
}, (error, response, body) => {
expect(error).toBe(null);
var b = JSON.parse(body);
request.get(b.url, (error, response, body) =>{
expect(response.headers['content-type']).toMatch(/^video\/mp4/);
expect(response.headers['accept-ranges']).toMatch(/^bytes/);
done();
});
});
});

it_exclude_dbs(['postgres'])('creates correct url for old files hosted on files.parsetfss.com', done => {
var file = {
__type: 'File',
Expand Down
110 changes: 110 additions & 0 deletions src/Adapters/Files/GridStoreAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,116 @@ export class GridStoreAdapter extends FilesAdapter {
getFileLocation(config, filename) {
return (config.mount + '/files/' + config.applicationId + '/' + encodeURIComponent(filename));
}

handleVideoStream(filename, range, res, contentType) {
return this._connect().then(database => {
return GridStore.exist(database, filename)
.then(() => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please put on the same line as previous one

let gridStore = new GridStore(database, filename, 'r');
gridStore.open((err, gridFile) => {
if(!gridFile) {
res.status(404);
res.set('Content-Type', 'text/plain');
res.end('File not found.');
return;
}
streamVideo(gridFile,range, res, contentType);
});
});
});
}
}

/**
* streamVideo is licensed under Creative Commons Attribution 4.0 International License (https://creativecommons.org/licenses/by/4.0/).
* Author: LEROIB at weightingformypizza.(https://weightingformypizza.wordpress.com/2015/06/24/stream-html5-media-content-like-video-audio-from-mongodb-using-express-and-gridstore/)
*/
function streamVideo(gridFile, range, res, contentType) {
var buffer_size = 1024 * 1024;//1024Kb
if (range != null) {
// Range request, partiall stream the file
var parts = range.replace(/bytes=/, "").split("-");
var partialstart = parts[0];
var partialend = parts[1];
var start = partialstart ? parseInt(partialstart, 10) : 0;
var end = partialend ? parseInt(partialend, 10) : gridFile.length - 1;
var chunksize = (end - start) + 1;

if(chunksize == 1){
start = 0;
partialend = false;
}

if(!partialend){
if(((gridFile.length-1) - start) < (buffer_size)){
end = gridFile.length - 1;
}else{
end = start + (buffer_size);
}
chunksize = (end - start) + 1;
}

if(start == 0 && end == 2){
chunksize = 1;
}

res.writeHead(206, {
'Content-Range': 'bytes ' + start + '-' + end + '/' + gridFile.length,
'Accept-Ranges': 'bytes',
'Content-Length': chunksize,
'Content-Type': contentType,
});

gridFile.seek(start, function () {
// get gridFile stream
var stream = gridFile.stream(true);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fix indent.

var ended = false;
var bufferIdx = 0;
var bufferAvail = 0;
var range = (end - start) + 1;
var totalbyteswanted = (end - start) + 1;
var totalbyteswritten = 0;
// write to response
stream.on('data', function (buff) {
bufferAvail += buff.length;
//Ok check if we have enough to cover our range
if(bufferAvail < range) {
//Not enough bytes to satisfy our full range
if(bufferAvail > 0)
{
//Write full buffer
res.write(buff);
totalbyteswritten += buff.length;
range -= buff.length;
bufferIdx += buff.length;
bufferAvail -= buff.length;
}
}
else{
//Enough bytes to satisfy our full range!
if(bufferAvail > 0) {
var buffer = buff.slice(0,range);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

change var to const

res.write(buffer);
totalbyteswritten += buffer.length;
bufferIdx += range;
bufferAvail -= range;
}
}
if(totalbyteswritten >= totalbyteswanted) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: space between if an (

// totalbytes = 0;
gridFile.close();
res.end();
this.destroy();
}
});
});
}else{
// stream back whole file
res.header("Accept-Ranges", "bytes");
res.header('Content-Type', contentType);
res.header('Content-Length', gridFile.length);
var stream = gridFile.stream(true).pipe(res);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stream is never used, please remove

}
}

export default GridStoreAdapter;
13 changes: 13 additions & 0 deletions src/Controllers/FilesController.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,19 @@ export class FilesController extends AdaptableController {
expectedAdapterType() {
return FilesAdapter;
}


/**
* Stream video file by serving data in chunks if FilesAdapter is GridStoreAdapter.
* If not; handle the request as usual with "getFileData".
*/
handleVideoStream(filename, range, res, contentType) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe rename to getFileRange(config, filename, range, res) to be more like getFileData() or add a range attribute to getFileData(config, filename, range, res)

if (this.adapter.constructor.name == 'GridStoreAdapter') {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you expose just check if the function exists on the adapter for forward compatibility?

return this.adapter.handleVideoStream(filename,range,res,contentType);
}else{
return this.adapter.getFileData(filename);
}
}
}

export default FilesController;
28 changes: 18 additions & 10 deletions src/Routers/FilesRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,24 @@ export class FilesRouter {
const config = new Config(req.params.appId);
const filesController = config.filesController;
const filename = req.params.filename;
filesController.getFileData(config, filename).then((data) => {
res.status(200);
var contentType = mime.lookup(filename);
res.set('Content-Type', contentType);
res.end(data);
}).catch((err) => {
res.status(404);
res.set('Content-Type', 'text/plain');
res.end('File not found.');
});
const contentType = mime.lookup(filename);
if (contentType == 'video/mp4' || contentType == 'video/quicktime') {
filesController.handleVideoStream(filename, req.get("Range"), res, contentType).catch((err) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't we infer the streaming based upon the request headers like Range.
Also the header seem to be Content-Range not Range.

http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html

Can you clarify please?

res.status(404);
res.set('Content-Type', 'text/plain');
res.end('File not found.');
});
}else{
filesController.getFileData(config, filename).then((data) => {
res.status(200);
res.set('Content-Type', contentType);
res.end(data);
}).catch((err) => {
res.status(404);
res.set('Content-Type', 'text/plain');
res.end('File not found.');
});
}
}

createHandler(req, res, next) {
Expand Down