Skip to content
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

Serving pre-recorded mp4 on Raspberry Pi #3

Open
whypam opened this issue Oct 2, 2021 · 3 comments
Open

Serving pre-recorded mp4 on Raspberry Pi #3

whypam opened this issue Oct 2, 2021 · 3 comments

Comments

@whypam
Copy link

whypam commented Oct 2, 2021

Hi there,

Thanks for posting the solutions. I ran a server with tcpBridgeAutoPipeline.js on a Raspberry Pi. Since I didn't have a camera, I changed the CLI pipeline to gst-launch-1.0 -v filesrc location = bigbuck.mp4 ! decodebin ! x264enc ! rtph264pay ! udpsink host=localhost port=8081 and changed 'Content-Type': 'video/mp4'

Then I got an error - Error: listen EADDRINUSE: address already in use :::8081 I tried changing to other port and checked whether port was in use sudo lsof -i -P -n to make sure was not in use. But still getting the same error message. I think the issue is on the server itself not on the script you provided, but I can't figure out and am new to gstreamer cli pipeline and not sure how tcp server works with http server at the same time.

Could you please take a look and give me some insights? Appreciate your help.

index.js

var express = require('express');
var http = require('http');
var net = require('net');
var child = require('child_process');

var expressApp = express();
var httpServer = http.createServer(expressApp);

expressApp.get('/', function (req, res) {
    var date = new Date();

    res.writeHead(200, {
        'Date': date.toUTCString(),
        'Connection': 'close',
        'Cache-Control': 'private',
        'Content-Type': 'video/mp4',
    });

    var tcpServer = net.createServer(function (socket) {
        socket.on('data', function (data) {
            res.write(data);
        });
        socket.on('close', function (had_error) {
            res.end();
        });
    });

    tcpServer.maxConnections = 1;

    tcpServer.listen(8081, function () {
        var cmd = 'gst-launch-1.0';
        var args =
            ['-v', 'filesrc', 'location=bigbuck.mp4',
                '!', 'decodebin',
                '!', 'x264enc',
                '!', 'rtph264pay',
                '!', 'udpsink', 'host=localhost',
                'port=' + tcpServer.address().port];

        var gstMuxer = child.spawn(cmd, args);

        gstMuxer.stderr.on('data', onSpawnError);
        gstMuxer.on('exit', onSpawnExit);

        res.connection.on('close', function () {
            gstMuxer.kill();
        });

    });
});

httpServer.listen(8080);

function onSpawnError(data) {
    console.log(data.toString());
}

function onSpawnExit(code) {
    if (code != null) {
        console.log('GStreamer error, exit code ' + code);
    }
}

process.on('uncaughtException', function (err) {
    console.log(err);
});

Error message:

Error: listen EADDRINUSE: address already in use :::8081
    at Server.setupListenHandle [as _listen2] (net.js:1331:16)
    at listenInCluster (net.js:1379:12)
    at Server.listen (net.js:1465:7)
    at /home/pi/gstreamer-server/index.js:30:15
    at Layer.handle [as handle_request] (/home/pi/gstreamer-server/node_modules/express/lib/router/layer.js:95:5)
    at next (/home/pi/gstreamer-server/node_modules/express/lib/router/route.js:137:13)
    at Route.dispatch (/home/pi/gstreamer-server/node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] (/home/pi/gstreamer-server/node_modules/express/lib/router/layer.js:95:5)
    at /home/pi/gstreamer-server/node_modules/express/lib/router/index.js:281:22
    at Function.process_params (/home/pi/gstreamer-server/node_modules/express/lib/router/index.js:335:12) {
  code: 'EADDRINUSE',
  errno: -98,
  syscall: 'listen',
  address: '::',
  port: 8081
}

package.json

{
  "name": "gstreamer",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "nodemon index.js"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.17.1",
    "nodemon": "^2.0.5"
  }
}
pi@raspberrypi:~ $ lsof -i tcp:8081
COMMAND  PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
node    7827   pi   23u  IPv6 118623      0t0  TCP *:tproxy (LISTEN)
@janoglezcampos
Copy link
Owner

janoglezcampos commented Oct 2, 2021

Hello, I am quite bussy today, so tomorrow I will check a little bit more, but after a quick review of your code, the first thing I see is that you are using and udpsink on the pipeline. Also, you shouldnt need to decode the file, i think chrome accepts mp4.

Try:
var args =
['filesrc', 'location=bigbuck.mp4',
'!', 'queue',
'!', 'm.', 'oggmux', 'name=m',
'!', 'queue',
'!', 'tcpclientsink', 'host=localhost',
'port=' + tcpServer.address().port];

If mp4 is not accepted:
var args =
['filesrc', 'location=bigbuck.mp4',
'!', 'decodebin',
'!', 'video/x-raw,framerate=30/1,width=320,height=240',
'!', 'videoconvert',
'!', 'queue',
'!', 'theoraenc',
'!', 'queue',
'!', 'm.', 'oggmux', 'name=m',
'!', 'queue',
'!', 'tcpclientsink', 'host=localhost',
'port=' + tcpServer.address().port];

The idea behind this "project" is:
Open an http server on port 8080, a tcp server on port 8081, when its open, start a pipeline that sends the frames to nodejs on that port, then resend the tcp server writes the frame with the headers to the http server on port 8080.

Gstreamer (just the frames) -> tcp server (8081) (frames + headers)-> http server (8080)

As I said, let me a day and I will come with more info.

@whypam
Copy link
Author

whypam commented Oct 2, 2021

Hello, thanks for your advice and replying me in your tight schedule. I tried both args and got different error messages. Thanks again.

For the first one (shorter one):

pi@raspberrypi:~/gstreamer-server $ npm start

> gstreamer@1.0.0 start /home/pi/gstreamer-server
> nodemon index.js

[nodemon] 2.0.5
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node index.js`
consol log tcpServer port: 8081

** (gst-launch-1.0:2302): CRITICAL **: 11:12:08.654: gst_ogg_stream_setup_map_from_caps: assertion 'caps != NULL' failed

Error: listen EADDRINUSE: address already in use :::8081
    at Server.setupListenHandle [as _listen2] (net.js:1331:16)
    at listenInCluster (net.js:1379:12)
    at Server.listen (net.js:1465:7)
    at /home/pi/gstreamer-server/index.js:30:15
    at Layer.handle [as handle_request] (/home/pi/gstreamer-server/node_modules/express/lib/router/layer.js:95:5)
    at next (/home/pi/gstreamer-server/node_modules/express/lib/router/route.js:137:13)
    at Route.dispatch (/home/pi/gstreamer-server/node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] (/home/pi/gstreamer-server/node_modules/express/lib/router/layer.js:95:5)
    at /home/pi/gstreamer-server/node_modules/express/lib/router/index.js:281:22
    at Function.process_params (/home/pi/gstreamer-server/node_modules/express/lib/router/index.js:335:12) {
  code: 'EADDRINUSE',
  errno: -98,
  syscall: 'listen',
  address: '::',
  port: 8081
}

For the second one (longer one):

pi@raspberrypi:~/gstreamer-server $ npm start

> gstreamer@1.0.0 start /home/pi/gstreamer-server
> nodemon index.js

[nodemon] 2.0.5
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node index.js`
consol log tcpServer port: 8081
ERROR: from element /GstPipeline:pipeline0/GstDecodeBin:decodebin0/GstQTDemux:qtdemux0: Internal data stream error.
Additional debug info:
qtdemux.c(6073): gst_qtdemux_loop (): /GstPipeline:pipeline0/GstDecodeBin:decodebin0/GstQTDemux:qtdemux0:
streaming stopped, reason not-negotiated (-4)
ERROR: pipeline doesn't want to preroll.

Error: listen EADDRINUSE: address already in use :::8081
    at Server.setupListenHandle [as _listen2] (net.js:1331:16)
    at listenInCluster (net.js:1379:12)
    at Server.listen (net.js:1465:7)
    at /home/pi/gstreamer-server/index.js:30:15
    at Layer.handle [as handle_request] (/home/pi/gstreamer-server/node_modules/express/lib/router/layer.js:95:5)
    at next (/home/pi/gstreamer-server/node_modules/express/lib/router/route.js:137:13)
    at Route.dispatch (/home/pi/gstreamer-server/node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] (/home/pi/gstreamer-server/node_modules/express/lib/router/layer.js:95:5)
    at /home/pi/gstreamer-server/node_modules/express/lib/router/index.js:281:22
    at Function.process_params (/home/pi/gstreamer-server/node_modules/express/lib/router/index.js:335:12) {
  code: 'EADDRINUSE',
  errno: -98,
  syscall: 'listen',
  address: '::',
  port: 8081
}

@whypam
Copy link
Author

whypam commented Oct 16, 2021

Hello, hope you’re well. I figured the Error: listen EADDRINUSE: address already in use was due to the browser was somehow refreshed twice.

Now, I set up a Raspberry Pi camera and ran these CLI pipelines:
On RPi (server)
$ gst-launch-1.0 -v v4l2src device=/dev/video0 num-buffers=-1 ! video/x-raw,width=640,height=480, framerate=30/1 ! videoconvert ! jpegenc ! tcpserversink  host=192.168.xxx.xx port=5000

On Mac (client)
$ gst-launch-1.0 tcpclientsrc host=192.168.xxx.xx port=5000 ! jpegdec ! videoconvert ! autovideosink

These pipelines work well and start OpenGL renderer to display the camera stream.

I want to display the camera streaming on a browser. So I adapted your tcpBridgeAutoPipeline.js and replaced autovideosink to tcpclientsink and changed 'Content-Type': 'video/webm'. I got no error message but got a black blank video display, no camera streaming content.

Could you please take a look and advise anything I’m missing from the pipeline? Thanks again for your help.

On RPi, index.js

var express = require('express');
var http = require('http');
var net = require('net');
var child = require('child_process');

var expressApp = express();
var httpServer = http.createServer(expressApp);

expressApp.get('/', function (req, res) {
    var date = new Date();

    res.writeHead(200, {
        'Date': date.toUTCString(),
        'Connection': 'close',
        'Cache-Control': 'private',
        'Content-Type': 'video/webm',
    });

    var tcpServer = net.createServer(function (socket) {
        socket.on('data', function (data) {
            res.write(data);
        });
        socket.on('close', function (had_error) {
            res.end();
        });
    });

    tcpServer.maxConnections = 1;

    tcpServer.listen(8081, function () {
        var cmd = 'gst-launch-1.0';
        var args =
            ['tcpclientsrc', 'host=192.168.xxx.xx', 'port=5000'
                '!', 'jpegdec',
                '!', 'videoconvert',
                '!', 'tcpclientsink', 'host=192.168.xxx.xx',
                'port=8081'];

        var gstMuxer = child.spawn(cmd, args);

        gstMuxer.stderr.on('data', onSpawnError);
        gstMuxer.on('exit', onSpawnExit);

        res.connection.on('close', function () {
            gstMuxer.kill();
        });

    });
});

httpServer.listen(8080);

function onSpawnError(data) {
    console.log(data.toString());
}

function onSpawnExit(code) {
    if (code != null) {
        console.log('GStreamer error, exit code ' + code);
    }
}

process.on('uncaughtException', function (err) {
    console.log(err);
});

On Mac, index.html

<video width=”650” controls muted=”muted” autoplay>
	<source src=http://192.168.xxx.xx:8080 /> 
</video>

To start streaming, I did these steps:

  1. Ran this pipeline on RPi:
    $ gst-launch-1.0 -v v4l2src device=/dev/video0 num-buffers=-1 ! video/x-raw,width=640,height=480, framerate=30/1 ! videoconvert ! jpegenc ! tcpserversink  host=192.168.xxx.xx port=5000

  2. Started index.js on RPi

  3. Started index.html on Mac (client)

Could you please take a look? Appreciate your help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants