-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Changes from 1 commit
878718d
0a94b9b
b639383
5f8bad1
a77fa21
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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(() => { | ||
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. change |
||
res.write(buffer); | ||
totalbyteswritten += buffer.length; | ||
bufferIdx += range; | ||
bufferAvail -= range; | ||
} | ||
} | ||
if(totalbyteswritten >= totalbyteswanted) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: space between |
||
// 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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} | ||
} | ||
|
||
export default GridStoreAdapter; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe rename to |
||
if (this.adapter.constructor.name == 'GridStoreAdapter') { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. shouldn't we infer the streaming based upon the request headers like 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) { | ||
|
There was a problem hiding this comment.
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