正在显示
5 个修改的文件
包含
49 行增加
和
9 行删除
| @@ -2,6 +2,8 @@ package io.livekit.android | @@ -2,6 +2,8 @@ package io.livekit.android | ||
| 2 | 2 | ||
| 3 | import android.content.Context | 3 | import android.content.Context |
| 4 | import io.livekit.android.dagger.DaggerLiveKitComponent | 4 | import io.livekit.android.dagger.DaggerLiveKitComponent |
| 5 | +import io.livekit.android.dagger.LiveKitOverrides | ||
| 6 | +import io.livekit.android.dagger.create | ||
| 5 | import io.livekit.android.room.Room | 7 | import io.livekit.android.room.Room |
| 6 | import io.livekit.android.room.RoomListener | 8 | import io.livekit.android.room.RoomListener |
| 7 | import io.livekit.android.util.LKLog | 9 | import io.livekit.android.util.LKLog |
| @@ -29,11 +31,12 @@ class LiveKit { | @@ -29,11 +31,12 @@ class LiveKit { | ||
| 29 | fun create( | 31 | fun create( |
| 30 | appContext: Context, | 32 | appContext: Context, |
| 31 | options: RoomOptions = RoomOptions(), | 33 | options: RoomOptions = RoomOptions(), |
| 34 | + overrides: LiveKitOverrides = LiveKitOverrides(), | ||
| 32 | ): Room { | 35 | ): Room { |
| 33 | val ctx = appContext.applicationContext | 36 | val ctx = appContext.applicationContext |
| 34 | val component = DaggerLiveKitComponent | 37 | val component = DaggerLiveKitComponent |
| 35 | .factory() | 38 | .factory() |
| 36 | - .create(ctx) | 39 | + .create(ctx, overrides) |
| 37 | 40 | ||
| 38 | val room = component.roomFactory().create(ctx) | 41 | val room = component.roomFactory().create(ctx) |
| 39 | 42 | ||
| @@ -66,9 +69,10 @@ class LiveKit { | @@ -66,9 +69,10 @@ class LiveKit { | ||
| 66 | token: String, | 69 | token: String, |
| 67 | options: ConnectOptions = ConnectOptions(), | 70 | options: ConnectOptions = ConnectOptions(), |
| 68 | roomOptions: RoomOptions = RoomOptions(), | 71 | roomOptions: RoomOptions = RoomOptions(), |
| 69 | - listener: RoomListener? = null | 72 | + listener: RoomListener? = null, |
| 73 | + overrides: LiveKitOverrides = LiveKitOverrides() | ||
| 70 | ): Room { | 74 | ): Room { |
| 71 | - val room = create(appContext, roomOptions) | 75 | + val room = create(appContext, roomOptions, overrides) |
| 72 | 76 | ||
| 73 | room.listener = listener | 77 | room.listener = listener |
| 74 | room.connect(url, token, options) | 78 | room.connect(url, token, options) |
| @@ -25,4 +25,7 @@ object InjectionNames { | @@ -25,4 +25,7 @@ object InjectionNames { | ||
| 25 | internal const val SIGNAL_JSON_ENABLED = "signal_json_enabled" | 25 | internal const val SIGNAL_JSON_ENABLED = "signal_json_enabled" |
| 26 | 26 | ||
| 27 | internal const val OPTIONS_VIDEO_HW_ACCEL = "options_video_hw_accel" | 27 | internal const val OPTIONS_VIDEO_HW_ACCEL = "options_video_hw_accel" |
| 28 | + | ||
| 29 | + // Overrides | ||
| 30 | + internal const val OVERRIDE_OKHTTP = "override_okhttp" | ||
| 28 | } | 31 | } |
| @@ -4,8 +4,11 @@ import android.content.Context | @@ -4,8 +4,11 @@ import android.content.Context | ||
| 4 | import dagger.BindsInstance | 4 | import dagger.BindsInstance |
| 5 | import dagger.Component | 5 | import dagger.Component |
| 6 | import io.livekit.android.room.Room | 6 | import io.livekit.android.room.Room |
| 7 | +import okhttp3.OkHttpClient | ||
| 7 | import org.webrtc.EglBase | 8 | import org.webrtc.EglBase |
| 8 | import org.webrtc.PeerConnectionFactory | 9 | import org.webrtc.PeerConnectionFactory |
| 10 | +import javax.annotation.Nullable | ||
| 11 | +import javax.inject.Named | ||
| 9 | import javax.inject.Singleton | 12 | import javax.inject.Singleton |
| 10 | 13 | ||
| 11 | @Singleton | 14 | @Singleton |
| @@ -17,7 +20,7 @@ import javax.inject.Singleton | @@ -17,7 +20,7 @@ import javax.inject.Singleton | ||
| 17 | JsonFormatModule::class, | 20 | JsonFormatModule::class, |
| 18 | ] | 21 | ] |
| 19 | ) | 22 | ) |
| 20 | -interface LiveKitComponent { | 23 | +internal interface LiveKitComponent { |
| 21 | 24 | ||
| 22 | fun roomFactory(): Room.Factory | 25 | fun roomFactory(): Room.Factory |
| 23 | 26 | ||
| @@ -27,6 +30,30 @@ interface LiveKitComponent { | @@ -27,6 +30,30 @@ interface LiveKitComponent { | ||
| 27 | 30 | ||
| 28 | @Component.Factory | 31 | @Component.Factory |
| 29 | interface Factory { | 32 | interface Factory { |
| 30 | - fun create(@BindsInstance appContext: Context): LiveKitComponent | 33 | + fun create( |
| 34 | + @BindsInstance appContext: Context, | ||
| 35 | + | ||
| 36 | + @BindsInstance | ||
| 37 | + @Named(InjectionNames.OVERRIDE_OKHTTP) | ||
| 38 | + @Nullable | ||
| 39 | + okHttpClientOverride: OkHttpClient? | ||
| 40 | + ): LiveKitComponent | ||
| 31 | } | 41 | } |
| 32 | -} | ||
| 42 | +} | ||
| 43 | + | ||
| 44 | +internal fun LiveKitComponent.Factory.create( | ||
| 45 | + context: Context, | ||
| 46 | + overrides: LiveKitOverrides, | ||
| 47 | +): LiveKitComponent { | ||
| 48 | + return create( | ||
| 49 | + appContext = context, | ||
| 50 | + okHttpClientOverride = overrides.okHttpClient | ||
| 51 | + ) | ||
| 52 | +} | ||
| 53 | + | ||
| 54 | +/** | ||
| 55 | + * Overrides to replace LiveKit internally used component with custom implementations. | ||
| 56 | + */ | ||
| 57 | +data class LiveKitOverrides( | ||
| 58 | + val okHttpClient: OkHttpClient? = null | ||
| 59 | +) |
| @@ -4,14 +4,20 @@ import dagger.Module | @@ -4,14 +4,20 @@ import dagger.Module | ||
| 4 | import dagger.Provides | 4 | import dagger.Provides |
| 5 | import okhttp3.OkHttpClient | 5 | import okhttp3.OkHttpClient |
| 6 | import okhttp3.WebSocket | 6 | import okhttp3.WebSocket |
| 7 | +import javax.annotation.Nullable | ||
| 8 | +import javax.inject.Named | ||
| 7 | import javax.inject.Singleton | 9 | import javax.inject.Singleton |
| 8 | 10 | ||
| 9 | @Module | 11 | @Module |
| 10 | object WebModule { | 12 | object WebModule { |
| 11 | @Provides | 13 | @Provides |
| 12 | @Singleton | 14 | @Singleton |
| 13 | - fun okHttpClient(): OkHttpClient { | ||
| 14 | - return OkHttpClient() | 15 | + fun okHttpClient( |
| 16 | + @Named(InjectionNames.OVERRIDE_OKHTTP) | ||
| 17 | + @Nullable | ||
| 18 | + okHttpClientOverride: OkHttpClient? | ||
| 19 | + ): OkHttpClient { | ||
| 20 | + return okHttpClientOverride ?: OkHttpClient() | ||
| 15 | } | 21 | } |
| 16 | 22 | ||
| 17 | @Provides | 23 | @Provides |
| @@ -17,7 +17,7 @@ import javax.inject.Singleton | @@ -17,7 +17,7 @@ import javax.inject.Singleton | ||
| 17 | JsonFormatModule::class, | 17 | JsonFormatModule::class, |
| 18 | ] | 18 | ] |
| 19 | ) | 19 | ) |
| 20 | -interface TestLiveKitComponent : LiveKitComponent { | 20 | +internal interface TestLiveKitComponent : LiveKitComponent { |
| 21 | 21 | ||
| 22 | fun websocketFactory(): MockWebsocketFactory | 22 | fun websocketFactory(): MockWebsocketFactory |
| 23 | 23 |
-
请 注册 或 登录 后发表评论