Thursday, February 28, 2019

Headless Raspberry Pi Stream Youtube Streamer

There are many reasons you'd want to stream to Youtube from a Raspberry Pi. With the camera attachment, you can watch over anything with great clarity. My friend wanted to set up a camera to watch over his hedgehog - mostly for fun - and chose this system to do so. In many of the solutions I've seen to now, it simply offers a command line interface from which you can start streaming. What I intend to do is far more simple to use.

I don't intend to make this a very complicated project. Rather, I intend to make it as simple as possible using things that already exist and are proven to work. It starts with Alex Ellis's Docker image for streaming to Youtube. Simple in its design, it literally pipes raw camera data to FFMPEG which is responsible for everything else. I followed every step on his blog post to make sure it works. When setting this up, you should at least pull the image so it doesn't have to when you click the start streaming button.

If we stopped here, we'd have to SSH in every time and give it a new key and, to be honest, I don't want to do that. I'd like an easier way to solve this problem. So, I decided to write a very simple web application in order to facilitate this. It manages the lifecycle of the docker container so we don't have to.

The web app will consist of a button and a text box. The button will allow you to toggle streaming and the text box will have the current stream key. Because the container is constructed with the stream key, when we stop the stream, we're actually removing the container completely. Whenever the state of the app changes, we save it to a JSON file. That's just a small usability thing.

The frontend is powered by an API that has three endpoints: start, stop, and status. Start and Stop do exactly what you'd expect. They start and stop the streamer container. Status simply returns the status of the web application. All three end points return the status of the streamer (started or stopped) and the key currently in use. The start and stop end points will return if the action requested was successful or not as well.

To top it all off, I installed PM2 to manage the whole thing. To set it up, I ran the following commands.
npm install -g pm2
pm2 startup
# pm2 will provide you with a command that you'd run here
pm2 start index.js # start the application
pm2 save # freeze the process state so it starts up
Now when we start the pi, the application should run. And if it was running before, it should start the streamer with the old key. There. Completely headless with no SSH.

This simple application is powered by NodeJS. I ran it in NodeJS 11, but I suspect it will run in older versions too. To install NodeJS on the Raspberry Pi, I followed this instructable adjusting the paths for the latest version of Node. You can get the code from the Github Repo. A simple project that shows that leveraging existing systems to make our system simple and somewhat reliable.

No comments:

Post a Comment