THIS IS NOT A BEGINERS TUTORIAL. This blog series will fall under the intermediate/ advanced tutorial. I say this not to discourage people from reading but simply to let people know that they may encounter some topics that might seem complicated
The Goal of this series
As the title states, this entire series will be about how to get the video from our Android device to stream on Twitch.
The steps you should take
1) get a preview working on your application
2) Allow your application to capture video
3) Create a secure Socket to connect to the Twitch injection servers (what this blog post is talking about )
4) Perform the RTMP handshake (slightly hard)
5) Encode the video from the device (very hard)
6) Send the encoded data to the Twitch injection server via the socket
What is a Socket?
Before we create a Socket, lets first talk about what a Socket actually is. Sockets allows us to treat a network connection as just another stream. Sockets shield us, the programmer from low-level details of
the network, such as error detection, packet sizes, packet splitting, packet retransmission, network addresses, and more.
What is a SSLSocket?
This is a Sockets and provides secure socket using protocols such as the "Secure Sockets Layer" (SSL) or IETF "Transport Layer Security" (TLS) protocols. We are going to use the TLS protocol
Creating the SSLSocket
Below is the code showing how we can create the secure socket connection in Kotlin:
classRtmpsClient(privatevalhost:String,privatevalport:Int,){privatevalTAG="RtmpsClient"privatelateinitvarsslSocket:SSLSocket// Perform connection on background thread using Coroutinesuspendfunconnect(){try{withContext(Dispatchers.IO){// Step 1: Create SSL context and factoryvalsslContext:SSLContext=SSLContext.getInstance("TLS")sslContext.init(null,null,null)valsslSocketFactory:SSLSocketFactory=sslContext.socketFactory// Step 2: Establish a secure connection to the RTMP serversslSocket=sslSocketFactory.createSocket(host,port)asSSLSocket// Add a HandshakeCompletedListenersslSocket.addHandshakeCompletedListener(object: HandshakeCompletedListener{overridefunhandshakeCompleted(event:HandshakeCompletedEvent){Log.i(TAG,"Handshake completed successfully!")Log.i(TAG,"Cipher Suite: ${event.cipherSuite}")Log.i(TAG,"Session: ${event.session}")Log.i(TAG,"Peer Principal: ${event.peerPrincipal}")}})sslSocket.startHandshake()// Perform SSL handshakeLog.i(TAG,"Connected to RTMPS server at $host:$port")}}catch(e:Exception){Log.e(TAG,"Failed to connect: ${e.message}",e)}}}
What port and host do we use?
According to the Twitch documentation, we need to get the Twitch injection servers. Which will return a bunch of servers. Which one to use? Well to be honest I copied what OBS was using. So for the host I used ingest.global-contribute.live-video.net and for the port I used 443. If it works for them, then it works for me.
The HandshakeCompletedListener
The Documentation states, You may register to receive event notification of handshake completion. This involves the use of two additional classes. HandshakeCompletedEvent objects are passed to HandshakeCompletedListener instances, which are registered by users of this API. This works perfectly and allows us to get a notification if the handshake fails and we could alert our user as such
startHandshake()
The startHandshake() method is what how we set up the security on the Socket. As the documentation statesStarts an SSL handshake on this connection. Common reasons include a need to use new encryption keys, to change cipher suites, or to initiate a new session..
Now what?
So you should be able to just create the class and call connect() and look at your logs to see a the message, Handshake completed successfully!
What is next?
The next blog post will be on creating the RTMP hand shake, which will be much more complicated
Conclusion
Thank you for taking the time out of your day to read this blog post of mine. If you have any questions or concerns please comment below or reach out to me on Twitter.
Top comments (0)
Subscribe
For further actions, you may consider blocking this person and/or reporting abuse
Top comments (0)