Skip to content

Commit

Permalink
Add network type to websocket query (livekit#140)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidliu authored Sep 11, 2022
1 parent f5ee158 commit 2c67b7c
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package io.livekit.android.dagger

import android.content.Context
import androidx.annotation.Nullable
import dagger.Module
import dagger.Provides
import dagger.Reusable
import io.livekit.android.stats.AndroidNetworkInfo
import io.livekit.android.stats.NetworkInfo
import okhttp3.OkHttpClient
import okhttp3.WebSocket
import javax.inject.Named
Expand All @@ -24,4 +28,10 @@ object WebModule {
fun websocketFactory(okHttpClient: OkHttpClient): WebSocket.Factory {
return okHttpClient
}

@Provides
@Reusable
fun networkInfo(context: Context): NetworkInfo {
return AndroidNetworkInfo(context)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import io.livekit.android.RoomOptions
import io.livekit.android.dagger.InjectionNames
import io.livekit.android.room.participant.ParticipantTrackPermission
import io.livekit.android.room.track.Track
import io.livekit.android.stats.NetworkInfo
import io.livekit.android.stats.getClientInfo
import io.livekit.android.util.CloseableCoroutineScope
import io.livekit.android.util.Either
Expand Down Expand Up @@ -42,6 +43,7 @@ constructor(
private val okHttpClient: OkHttpClient,
@Named(InjectionNames.DISPATCHER_IO)
private val ioDispatcher: CoroutineDispatcher,
private val networkInfo: NetworkInfo,
) : WebSocketListener() {
var isConnected = false
private set
Expand Down Expand Up @@ -147,6 +149,7 @@ constructor(
queryParams.add(CONNECT_QUERY_DEVICE_MODEL to clientInfo.deviceModel)
queryParams.add(CONNECT_QUERY_OS to clientInfo.os)
queryParams.add(CONNECT_QUERY_OS_VERSION to clientInfo.osVersion)
queryParams.add(CONNECT_QUERY_NETWORK_TYPE to networkInfo.getNetworkType().protoName)

return queryParams.foldIndexed("") { index, acc, pair ->
val separator = if(index == 0) "?" else "&"
Expand Down Expand Up @@ -604,6 +607,7 @@ constructor(
const val CONNECT_QUERY_DEVICE_MODEL = "device_model"
const val CONNECT_QUERY_OS = "os"
const val CONNECT_QUERY_OS_VERSION = "os_version"
const val CONNECT_QUERY_NETWORK_TYPE = "network"

const val SD_TYPE_ANSWER = "answer"
const val SD_TYPE_OFFER = "offer"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package io.livekit.android.stats

import android.content.Context
import android.net.ConnectivityManager
import android.net.NetworkCapabilities
import android.os.Build

interface NetworkInfo {
fun getNetworkType(): NetworkType
}

class AndroidNetworkInfo(private val context: Context) : NetworkInfo {
override fun getNetworkType(): NetworkType {
val connectivityManager =
context.getSystemService(Context.CONNECTIVITY_SERVICE) as? ConnectivityManager ?: return NetworkType.UNKNOWN

@Suppress("DEPRECATION")
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
val nw = connectivityManager.activeNetwork ?: return NetworkType.UNKNOWN
val actNw = connectivityManager.getNetworkCapabilities(nw) ?: return NetworkType.UNKNOWN
return when {
actNw.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) -> NetworkType.WIFI
actNw.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET) -> NetworkType.ETHERNET
actNw.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) -> NetworkType.CELLULAR
actNw.hasTransport(NetworkCapabilities.TRANSPORT_VPN) -> NetworkType.VPN
actNw.hasTransport(NetworkCapabilities.TRANSPORT_BLUETOOTH) -> NetworkType.BLUETOOTH
actNw.hasTransport(NetworkCapabilities.TRANSPORT_WIFI_AWARE) -> NetworkType.OTHER
actNw.hasTransport(NetworkCapabilities.TRANSPORT_LOWPAN) -> NetworkType.OTHER
actNw.hasTransport(NetworkCapabilities.TRANSPORT_USB) -> NetworkType.OTHER
else -> NetworkType.UNKNOWN
}
} else {
val info = connectivityManager.activeNetworkInfo
if (info == null || !info.isConnected) return NetworkType.UNKNOWN
return when (info.type) {
ConnectivityManager.TYPE_BLUETOOTH -> NetworkType.BLUETOOTH
ConnectivityManager.TYPE_DUMMY -> NetworkType.UNKNOWN
ConnectivityManager.TYPE_ETHERNET -> NetworkType.ETHERNET
ConnectivityManager.TYPE_MOBILE -> NetworkType.CELLULAR
ConnectivityManager.TYPE_MOBILE_DUN -> NetworkType.CELLULAR
ConnectivityManager.TYPE_MOBILE_HIPRI -> NetworkType.CELLULAR
ConnectivityManager.TYPE_MOBILE_MMS -> NetworkType.CELLULAR
ConnectivityManager.TYPE_MOBILE_SUPL -> NetworkType.CELLULAR
ConnectivityManager.TYPE_VPN -> NetworkType.VPN
ConnectivityManager.TYPE_WIFI -> NetworkType.WIFI
ConnectivityManager.TYPE_WIMAX -> NetworkType.CELLULAR
else -> NetworkType.UNKNOWN
}
}
}
}

enum class NetworkType(val protoName: String) {
WIFI("wifi"),
ETHERNET("ethernet"),
CELLULAR("cellular"),
VPN("vpn"),
BLUETOOTH("bluetooth"),
OTHER("other"),
UNKNOWN(""),
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ package io.livekit.android.mock.dagger

import dagger.Module
import dagger.Provides
import dagger.Reusable
import io.livekit.android.mock.MockWebSocketFactory
import io.livekit.android.stats.NetworkInfo
import io.livekit.android.stats.NetworkType
import okhttp3.OkHttpClient
import okhttp3.Response
import okhttp3.WebSocket
Expand Down Expand Up @@ -35,4 +38,12 @@ object TestWebModule {
fun mockWebsocketFactory(): MockWebSocketFactory {
return MockWebSocketFactory()
}

@Provides
@Reusable
fun networkInfo(): NetworkInfo {
return object : NetworkInfo {
override fun getNetworkType() = NetworkType.WIFI
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package io.livekit.android.room
import io.livekit.android.BaseTest
import io.livekit.android.mock.MockWebSocketFactory
import io.livekit.android.mock.TestData
import io.livekit.android.stats.NetworkInfo
import io.livekit.android.stats.NetworkType
import io.livekit.android.util.toOkioByteString
import io.livekit.android.util.toPBByteString
import kotlinx.coroutines.ExperimentalCoroutinesApi
Expand Down Expand Up @@ -39,7 +41,10 @@ class SignalClientTest : BaseTest() {
wsFactory,
Json,
okHttpClient = okHttpClient,
ioDispatcher = coroutineRule.dispatcher
ioDispatcher = coroutineRule.dispatcher,
networkInfo = object : NetworkInfo {
override fun getNetworkType() = NetworkType.WIFI
}
)
client.listener = listener
}
Expand Down

0 comments on commit 2c67b7c

Please sign in to comment.