'Receive video stream into Rails 7 via websocket
I have a very simple nodejs application that receives a video stream and shows it in a player.
Here´s the part of the code that listens, receives the video stream and sends it to the player.
app.set('port', 3000);
app.set('ipaddr', '127.0.0.1');
var http_server = app.listen(app.get('port'), app.get('ipaddr'), function() {
console.log('Express server listening on port ' + http_server.address().port);
});
var ws = new WebSocket.Server({server: http_server, perMessageDeflate: false});
app.post('/', function (req, res) {
req.on('data', function(data) {
ws.broadcast(data);
});
});
ws.broadcast = function(data) {
ws.clients.forEach(function each(client) {
if (client.readyState === WebSocket.OPEN) {
client.send(data);
}
});
};
I use JSMpeg to display the video.
var stream_url = 'ws://127.0.0.1:3000/'
var player = new JSMpeg.Player(stream_url, {canvas: $("#canvas_id"), videoBufferSize: 1024*1024});
Currently I´m sending the video from the camera using a Python script on my Mac like this:
output = Popen("ffmpeg -f avfoundation -framerate 20 -video_size 640x480 -pix_fmt uyvy422 -i '0' -f mpegts -codec:v mpeg1video -s 640x480 -b:v 1000k -bf 0 -", stdout=PIPE, stderr=sys.stdout, shell=True)
res = requests.post('127.0.0.1:3000', data=output.stdout, stream=True)
This is working, the video is posted to the nodejs app and the player shows it.
BUT I want to do the same with a Rails 7 application.
I have checked some tutorials to find out about action cable and so, which is new for me. And I have created a POST route to receive the stream from the Python script, but it is not receiving anything. Or it is not receiving the data correctly with:
requests.post('127.0.0.1:3000', data=output.stdout, stream=True)
Because if I try:
requests.post('127.0.0.1:3000', data="SOMETHING", stream=True)
I can see in the Rails app logs:
{"SOMETHING"=>nil, "controller"=>"home", "action"=>"input"}
Any hints to get what I have working with a simple nodejs app with a Rails 7 app?
UPDATE 1
Maybe my question should be simplified and reformulated...
With JSMpeg you can receive a video stream in this way:
var player = new JSMpeg.Player(stream_url, {canvas: $("#canvas_id"), videoBufferSize: 1024*1024});
So the question should be:
How can you create such endpoint, the stream_url, with Rails 7?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
