more testing code

This commit is contained in:
Harris
2021-08-20 09:14:54 +10:00
parent 9340c01af2
commit 74cd42d659
2 changed files with 110 additions and 6 deletions

View File

@@ -1,15 +1,43 @@
package org.thoughtcrime.securesms.calls
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_webrtc_tests.*
import network.loki.messenger.R
import org.session.libsignal.utilities.Log
import org.thoughtcrime.securesms.PassphraseRequiredActionBarActivity
import org.webrtc.*
import org.webrtc.RendererCommon.ScalingType
class WebRtcTestsActivity: PassphraseRequiredActionBarActivity(), PeerConnection.Observer,
SdpObserver {
SdpObserver, RendererCommon.RendererEvents {
companion object {
const val HD_VIDEO_WIDTH = 1280
const val HD_VIDEO_HEIGHT = 720
}
private class ProxyVideoSink : VideoSink {
private var target: VideoSink? = null
@Synchronized
override fun onFrame(frame: VideoFrame) {
if (target == null) {
Log.d("Loki-RTC", "Dropping frame in proxy because target is null.")
return
}
target!!.onFrame(frame)
}
@Synchronized
fun setTarget(target: VideoSink?) {
this.target = target
}
}
private val connectionFactory by lazy { PeerConnectionFactory.builder().createPeerConnectionFactory() }
private val remoteVideoSink = ProxyVideoSink()
private val localVideoSink = ProxyVideoSink()
override fun onCreate(savedInstanceState: Bundle?, ready: Boolean) {
super.onCreate(savedInstanceState, ready)
@@ -17,17 +45,82 @@ class WebRtcTestsActivity: PassphraseRequiredActionBarActivity(), PeerConnection
val server = PeerConnection.IceServer.builder("stun:stun.l.google.com:19302").createIceServer()
val peerConnection = connectionFactory.createPeerConnection(listOf(server), this) ?: return
val rtcConfig = PeerConnection.RTCConfiguration(listOf(server))
rtcConfig.keyType = PeerConnection.KeyType.ECDSA
rtcConfig.sdpSemantics = PeerConnection.SdpSemantics.UNIFIED_PLAN
val peerConnection = connectionFactory.createPeerConnection(rtcConfig, this) ?: return
Log.d("Loki-RTC", "peer connecting?")
peerConnection.connectionState()
val stream = connectionFactory.createLocalMediaStream("stream")
val audioSource = connectionFactory.createAudioSource(MediaConstraints())
val audioTrack = connectionFactory.createAudioTrack("audio", audioSource)
val videoSource = connectionFactory.createVideoSource(false)
val videoTrack = connectionFactory.createVideoTrack("video", videoSource)
stream.addTrack(audioTrack)
peerConnection.addStream(stream)
stream.addTrack(videoTrack)
val remoteTrack = getRemoteVideoTrack(peerConnection) ?: return
videoTrack.addSink(localVideoSink)
remoteTrack.addSink(remoteVideoSink)
remoteTrack.setEnabled(true)
videoTrack.setEnabled(true)
val eglBase = EglBase.create()
local_renderer.init(eglBase.eglBaseContext, this)
local_renderer.setScalingType(ScalingType.SCALE_ASPECT_FILL)
remote_renderer.init(eglBase.eglBaseContext, this)
val videoCapturer = createCameraCapturer(Camera2Enumerator(this)) ?: kotlin.run { finish(); return }
val surfaceHelper = SurfaceTextureHelper.create("video-thread", eglBase.eglBaseContext)
surfaceHelper.startListening(localVideoSink)
videoCapturer.initialize(surfaceHelper, this, null)
videoCapturer.startCapture(HD_VIDEO_WIDTH, HD_VIDEO_HEIGHT, 30)
peerConnection.createOffer(this, MediaConstraints())
}
private fun getRemoteVideoTrack(peerConnection: PeerConnection): VideoTrack? = peerConnection.transceivers.firstOrNull { it.receiver.track() is VideoTrack } as VideoTrack?
private fun createCameraCapturer(enumerator: CameraEnumerator): VideoCapturer? {
val deviceNames = enumerator.deviceNames
// First, try to find front facing camera
Log.d("Loki-RTC-vid", "Looking for front facing cameras.")
for (deviceName in deviceNames) {
if (enumerator.isFrontFacing(deviceName)) {
Log.d("Loki-RTC-vid", "Creating front facing camera capturer.")
val videoCapturer: VideoCapturer? = enumerator.createCapturer(deviceName, null)
if (videoCapturer != null) {
return videoCapturer
}
}
}
// Front facing camera not found, try something else
// Front facing camera not found, try something else
Log.d("Loki-RTC-vid", "Looking for other cameras.")
for (deviceName in deviceNames) {
if (!enumerator.isFrontFacing(deviceName)) {
Log.d("Loki-RTC-vid", "Creating other camera capturer.")
val videoCapturer: VideoCapturer? = enumerator.createCapturer(deviceName, null)
if (videoCapturer != null) {
return videoCapturer
}
}
}
return null
}
override fun onFirstFrameRendered() {
Log.d("Loki-RTC-vid", "first frame rendered")
}
override fun onFrameResolutionChanged(p0: Int, p1: Int, p2: Int) {
Log.d("Loki-RTC-vid", "frame resolution changed")
}
override fun onSignalingChange(p0: PeerConnection.SignalingState?) {
Log.d("Loki-RTC", "onSignalingChange: $p0")
}

View File

@@ -1,8 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<org.webrtc.SurfaceViewRenderer
android:id="@+id/remote_renderer"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<org.webrtc.SurfaceViewRenderer
android:id="@+id/local_renderer"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</RelativeLayout>
</LinearLayout>