Created
January 7, 2020 04:24
-
-
Save jamesmoore255/03e3e1ae1701fe4eaf45cbb99c5f9509 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.Context | |
import android.util.Log | |
import android.view.View | |
import android.widget.FrameLayout | |
import com.twilio.video.* | |
import io.flutter.plugin.common.BinaryMessenger | |
import io.flutter.plugin.common.MethodCall | |
import io.flutter.plugin.common.MethodChannel | |
import io.flutter.plugin.common.MethodChannel.MethodCallHandler | |
import io.flutter.plugin.common.MethodChannel.Result | |
import io.flutter.plugin.platform.PlatformView | |
/** | |
* A *Twilio Video View*, used to connect to a twilio video call and render the view to the flutter interface | |
* @property context the context of the view | |
* @param twilioVideoTutorialPlugin the instance of the Twilio Video Plugin used to communicate through the event channel. | |
* @param messenger the registrar messenger used to register the method channel. | |
*/ | |
class TwilioVideoTutorialView internal constructor(private var context: Context, twilioVideoTutorialPlugin: TwilioVideoTutorialPlugin, messenger: BinaryMessenger) : PlatformView, MethodCallHandler { | |
private val methodChannel: MethodChannel = MethodChannel(messenger, "twilioVideoPlugin") | |
// Initialize the cameraCapturer and default it to the front camera | |
private val cameraCapturer: CameraCapturer = CameraCapturer(context, CameraCapturer.CameraSource.FRONT_CAMERA) | |
// Create a local video track with the camera capturer | |
private val localVideoTrack: LocalVideoTrack = LocalVideoTrack.create(context, true, cameraCapturer)!! | |
// The local participant connebangcted to the room | |
var localParticipant: LocalParticipant? = null | |
// The twilio room set up for the call | |
private var room: Room? = null | |
var roomName: String? = null | |
// The twilio token passed through the method channel | |
private var token: String? = null | |
private val primaryVideoView: VideoView = VideoView(context) | |
// Create the parent view, this will be used for the primary and future thumbnail video views | |
private val view: FrameLayout = FrameLayout(context) | |
// The tag for any logging | |
val TAG = "TwilioVideoTutorial" | |
// Override the platform view getView function and return the parent view | |
override fun getView(): View { | |
return view | |
} | |
init { | |
// Initialize the method channel | |
methodChannel.setMethodCallHandler(this) | |
} | |
/** | |
* This listens to all the events that happen within the room and makes appropriate responses. | |
*/ | |
private val roomListener = object : Room.Listener { | |
override fun onConnected(room: Room) { | |
localParticipant = room.localParticipant | |
roomName = room.name | |
} | |
override fun onReconnected(room: Room) { | |
Log.i("Reconnected", "Participant: $localParticipant") | |
} | |
override fun onReconnecting(room: Room, twilioException: TwilioException) { | |
// Send a message to the flutter ui to be displayed regarding this action | |
Log.i("Reconnecting", "Participant: $localParticipant") | |
} | |
override fun onConnectFailure(room: Room, twilioException: TwilioException) { | |
Log.e("Connection Failure Room", room.name) | |
// Retry initializing the call | |
init(token!!) | |
} | |
override fun onDisconnected(room: Room, twilioException: TwilioException?) { | |
if (twilioException != null) { | |
throw error("Twilio error on disconnect for room $roomName: ${twilioException.message}") | |
} | |
localParticipant = null | |
Log.i("Disconnected", "room: $roomName") | |
// Re init ui if not destroyed | |
} | |
override fun onParticipantConnected(room: Room, remoteParticipant: RemoteParticipant) { | |
Log.i( | |
TAG, "Participant connected") | |
// Send a message to the flutter ui to be displayed regarding this action | |
Log.i("Participant connected", "Participant: $remoteParticipant") | |
} | |
override fun onParticipantDisconnected(room: Room, remoteParticipant: RemoteParticipant) { | |
// Create function to remove the remote participant properly | |
Log.i("Participant disconnect", remoteParticipant.identity) | |
} | |
override fun onRecordingStarted(room: Room) { | |
/** Will not be being implemented */ | |
} | |
override fun onRecordingStopped(room: Room) { | |
/** This will not be being implemented */ | |
} | |
} | |
/** | |
* This is where the calls from flutter will be intercepted | |
*/ | |
override fun onMethodCall(methodCall: MethodCall, result: Result) { | |
when (methodCall.method) { | |
"init" -> { | |
try { | |
val callOptions: Map<*, *>? = methodCall.arguments as? Map<*, *> | |
token = callOptions?.get("token") as String | |
init(token!!) | |
} catch (exception: Exception) { | |
result.error("Twilio Initiation Error: ", "${exception.message}", exception.stackTrace) | |
} | |
} | |
"hangup" -> hangup(result) | |
else -> result.notImplemented() | |
} | |
} | |
/** | |
* Init the necessary video rendering | |
*/ | |
private fun init(token: String) { | |
try { | |
val connectOptions = ConnectOptions.Builder(token) | |
localVideoTrack.let { connectOptions.videoTracks(listOf(it)) } | |
room = Video.connect(context, connectOptions.build(), roomListener) | |
localVideoTrack.addRenderer(primaryVideoView) | |
primaryVideoView.mirror = true | |
view.addView(primaryVideoView) | |
} catch (exception: Exception) { | |
Log.e("Initiation exception", "${exception.message}") | |
} | |
} | |
/** | |
* Disconnect from room | |
*/ | |
private fun hangup(result: Result) { | |
room?.disconnect() | |
localVideoTrack.release() | |
result.success(true) | |
} | |
override fun dispose() {} | |
} |
Where we have to add this file in android?
Bro have you got the answer
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Where we have to add this file in android?