How To Code A LIVE Streaming Server: RTMP & Stream Key #1

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's up this is part one on how to build a live streaming server in this part we're gonna build a server that can take a stream from obs and then that stream can be watched over rtmp once that's done we're going to secure the server with another authentication server that checks the stream key so that only the owner can publish a stream here's what it looks like we're building a docker compose file with two services one is the nginx server which will take our stream from obs and deliver it to our audience and the other is a node server that will perform authentication as a note only in part one will you need to use vlc or a similar program to watch the stream in the next video we'll encode the stream to hls so that you can watch it from a website also as a reminder we now have a discord so if at any time you're stuck you can join our discord ask your question and hopefully me or someone else can answer it for you you can find an invite in the description below first we'll need to install docker we need this so the rest of the tutorial will be simple for any os whether it's windows mac or linux next let's install obs we need this to create our stream it's the go to program used by most twitch and youtube live streamers lastly we'll need to install vlc many of you might not know about vlc but it's probably the best free media player out there it can handle almost any media format and we'll be using it to play the live stream and for windows users you'll need wsl and when you're done setting that up go into docker and turn on the wsl integration with ubuntu okay so to start i've created the live stream part one folder so let's open up vs code and then since we'll be putting together a few docker images we'll start with docker compose that so this is a docker compose yaml where we can define our docker our various docker images and files so this would be version 3.9 services uh so our first service will be the rtmp and we're gonna build it off the rtmp folder it'll have ports 1935 this is where the nginx server will be right what port the nginx server will be running on and lastly we'll name it rtmp server save okay so since we've defined that we're going to build it off this folder let's create that folder tmp and then we need to define our docker file so we look back our file okay so this is the docker image we're going to be starting with nginx rtmp so it's a nginx server that already has the rtmp module installed so we can use this as a starting point the readme is super helpful i recommend you like go through this check it out test it yourself but we're going to be starting from this extending section so this is their starting docker docker file for us so let's copy that so all this does is it starts from their docker image and then lets us copy our own nginx configuration file into the docker image so we're going to copy on we need to create one so direct create nginx integration and they give us a good starting point so uh let's start there too let's copy that back cool so we've got our own uh nginx configuration file our docker file is taking that and then putting it into the image they gave us uh and our docker compose yaml file will build and run this docker image so in order to do that we run docker compose build now to run it docker compose up okay so the rtmp server is running now so now we can put everything down now we open up obs let me turn on my other camera okay so we've got obs open up here and i've added our i've already added my camera so we'll just go to settings and stream and then here i've already set it up but it's the server is set to uh rtmp clone slash localhost 1935 slash live it's slash live because in our configuration here um our application is called live and then the stream key is test um it doesn't really matter what this is uh it'll just be the name of the url from uh you use to watch the stream so that's all set up and then we'll hit start streaming so now this is streaming a live video from obs to our rtmp uh server as you can see here it's live so now in order to watch the stream we're going to open up vlc go to media open network and then here it's the url for the server we had in uh that we also put in obs and then the stream key is the last bit here so we'll hit play and so you should see the video of the stream i'm on vlc soon there it is i don't know i'm out of focus but just anyway um i can't miss deciding to focus for this matter uh so we've got a stream coming from uh obs going to our server and then we are watching it through uh vlc here so that is the basic setup so far here's a diagram of what we've built so far notice that the stream key test is needed to watch the stream if we were to deploy this project then anyone watching could hijack the stream using our stream key so we'll need to secure the stream and that's where our authentication server comes in we're going to change our stream key to be the username of the person streaming and then require that they give a key as a secret password we'll then configure nginx so that it checks with our auth server before allowing the stream to be published okay so for this next part we're going to be creating our authentication server and what that will be is a express server first we'll start with the docker compose so we're going to create a new service off it's going to build off of the slash boss folder we're not going to expose any ports because we only want the rtmp server to be using this authentication server um so we're not gonna expose the ports for anyone to use so we'll just set the container name to off number score server now since we're building off the off folder we'll be creating a folder and off and this is where we will build the express server so and there has to be off now let's have the mpm project so net save it off press enter all right uh we only need two different uh packages that's install save friendly express and nodemon pep now let's open the package.json here oops look at this go into scripts uh start and it'll be nodemon server.js yeah okay so uh now i'm gonna i'm gonna cheat a little bit here i'm not gonna write out the our server line by line i'm gonna copy it over from my reference here auth server full but i will go over what it says so here's our express server uh so first we express generate the app and what this does is when we get a request from nginx we will parse the host body well actually first i should explain how this authentication is actually gonna work so we go to our configuration here and in our live server here so how would the authentication server actually work so we're going to be using the on publish directive and we're gonna and it takes a url so that url is http uh off server one thousand slash off so i'm gonna explain in a moment why we use this little name here so we call it offserver because it's the networking in docker so in our docker compose here we named it off server so in order to access this there's no really localhost you access it through the other containers name because they're not on the same container they're in two different containers communicating to each other over docker compose so that's why this is auth server and not localhost and then 8000 is the port of our auth server and then slash off is the endpoint so the way this direct directive works is when a new stream is published to rtmp the nginx server will first post to this server and then if it returns a 200 status then it accepts the stream and if it is a anything else like a 400 or 500 that it will reject the stream so the way this will work is uh the rtmp server will give a stream key to this endpoint and then we'll check if that matches our real stream key and then we'll accept it if it matches and reject it if not it might be a little bit clear if i go over the server again so once it once they post to slash off we take the stream key from the body here so body dot key and then i've hard putted a password here to just make things simple you could actually make a like database request to a list of users and compare their stream key so i just made it super simple here uh to get started with so if it matches super secret then we return the 200 status otherwise we set 403 forbidden status back to it let's first run this locally to see if it works okay so npm start it's running cool all right so but in order to get this to work in our darker compose we need to create a docker image out of this authentication server so i'm going to cheat a little bit again and copy it from my reference here part one ref slash docker file also this new docker file here okay so it starts from the node image node version 12. we create a working directory in the image slash user source app this does this isn't really important just so much as you make a working directory we're going to copy over our packets json to it we're going to run npm install we're going to copy over our server.js file and then to when the docker image is starting we'll run npm start so you already get this running now first we go back do docker compose build and now it's building the off server as well so now we do docker compose okay so now what docker can up we've got the rtmp server running and we've got the authentication server running what we expect is when i publish to obs so the nginx server will first send a post request to our authentication server and check to see if the stream key that we get equals super secret and if it equals 200 accept the stream and if not then it'll return 403 and reject the stream so let's test this out got obs up turn up the camera again okay it's deciding to focus now that's good so settings so first let's get the wrong stream key here so first i'll name strip key test opti that'll be like my user and then question mark we'll set the and then the parameter where our key comes from is here so question mark key equals and then to first test to see if it rejects the wrong strip key we'll write wrong here to apply okay and we hit start streaming perfect so it failed to connect the server because uh we sent them the extreme key now if we set the stream key to have a key of super secret so i'm hoping this will work now so click ok start streaming okay so maybe we messed up here and so let's debug it ah okay so unexpected curly brace nginx i i know so i forgot a semicolon here so with that fixed cancel and then doctor compose build again and then docker compose up cool we don't see that error anymore so awesome so this down now let's let's try again with okay so now we'll try the correct key so start streaming okay we're live and then just to double check we'll play uh the stream here so remember back that i i set the stream key to obdi question mark key equals super secret so now to watch it all you need to go to is slash live slash op d di see opti play okay it's working now okay so we'll stop streaming and then now let's see if it rejects the stream if i give it the wrong stream key super secret just right wrong okay start streaming okay sweet could not access specified okay please double check your stream perfect so it's basically telling us we failed to connect because our stream key is wrong and so that's exactly what we wanted to happen that's it for now in the next video i'll be changing the encoding from rtmp to hls so that we can actually view the stream on a website instead of using the vlc program so if you've gotten this far thanks so much for watching uh be sure to leave a like comment what you thought if you need any help make sure to join the discord there's a link in the description and i will catch you in the next one peace you
Info
Channel: AbdisalanCodes
Views: 23,693
Rating: undefined out of 5
Keywords: nginx, vlc, stream key, rtmp, docker, live stream, obs, live streaming, streaming
Id: yKdPSXkaV5c
Channel Id: undefined
Length: 17min 52sec (1072 seconds)
Published: Sun Jan 31 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.