DEV Community

Cover image for Using Android to stream to Twitch. Part 1. Securing a Socket
Tristan Elliott
Tristan Elliott

Posted on

Using Android to stream to Twitch. Part 1. Securing a Socket

Table of contents

  1. Warning
  2. Goal of this series
  3. Steps in this series
  4. What is a Socket?
  5. What is a SSLSocket
  6. Creating the SSLSocket (THE ACTUAL CODE)
  7. What port and host do we use?
  8. Adding a handshake listener
  9. Starting the handshake
  10. What is next?

My app on the Google play store

My app's GitHub code

Resources

Warning

  • 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:

class RtmpsClient(
    private val host: String,
    private val port: Int,
) {
        private val TAG = "RtmpsClient"


    private lateinit var sslSocket: SSLSocket

    // Perform connection on background thread using Coroutine
    suspend fun connect() {
        try {
            withContext(Dispatchers.IO) {
                // Step 1: Create SSL context and factory
                val sslContext: SSLContext = SSLContext.getInstance("TLS")
                sslContext.init(null, null, null)
                val sslSocketFactory: SSLSocketFactory = sslContext.socketFactory

                // Step 2: Establish a secure connection to the RTMP server
                sslSocket = sslSocketFactory.createSocket(host, port) as SSLSocket
                // Add a HandshakeCompletedListener
                sslSocket.addHandshakeCompletedListener(object : HandshakeCompletedListener {
                    override fun handshakeCompleted(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 handshake
                Log.i(TAG, "Connected to RTMPS server at $host:$port")


            }
        } catch (e: Exception) {
            Log.e(TAG, "Failed to connect: ${e.message}", e)
        }
    }
}

Enter fullscreen mode Exit fullscreen mode

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 states Starts 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)