-
Notifications
You must be signed in to change notification settings - Fork 0
Feat: Implement GPU Generation Path #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| package com.visionrtc | ||
|
|
||
| import android.opengl.GLES20 | ||
| import android.os.SystemClock | ||
| import org.webrtc.EglBase | ||
| import org.webrtc.JavaI420Buffer | ||
| import org.webrtc.VideoFrame | ||
| import java.nio.ByteBuffer | ||
| import java.util.concurrent.Executors | ||
| import java.util.concurrent.ScheduledExecutorService | ||
| import java.util.concurrent.ScheduledFuture | ||
| import java.util.concurrent.TimeUnit | ||
| import kotlin.math.roundToInt | ||
|
|
||
| class GlNullGenerator( | ||
| private val eglBase: EglBase, | ||
| private val observer: org.webrtc.CapturerObserver, | ||
| width: Int, | ||
| height: Int, | ||
| fps: Int, | ||
| private val onFps: (Int) -> Unit, | ||
| ) { | ||
| @Volatile var width: Int = width; private set | ||
| @Volatile var height: Int = height; private set | ||
| @Volatile var fps: Int = fps; private set | ||
|
|
||
| private val executor: ScheduledExecutorService = Executors.newSingleThreadScheduledExecutor() | ||
| private var scheduled: ScheduledFuture<*>? = null | ||
| @Volatile private var running: Boolean = false | ||
| private var framesThisSecond = 0 | ||
| private var lastSecondTs = SystemClock.elapsedRealtime() | ||
|
|
||
| fun start() { | ||
| if (running) return | ||
| running = true | ||
| val periodNs = (1_000_000_000L / fps.toLong()) | ||
| scheduled = executor.scheduleAtFixedRate({ tick() }, 0L, periodNs, TimeUnit.NANOSECONDS) | ||
| } | ||
|
|
||
| fun pause() { running = false } | ||
| fun resume() { running = true } | ||
| fun stop() { | ||
| running = false | ||
| scheduled?.cancel(true) | ||
| executor.shutdownNow() | ||
| } | ||
|
|
||
| fun setResolution(w: Int, h: Int) { width = w; height = h } | ||
| fun setFps(next: Int) { | ||
| if (next <= 0 || next == fps) return | ||
| fps = next | ||
| if (running) { | ||
| scheduled?.cancel(false) | ||
| val periodNs = (1_000_000_000L / fps.toLong()) | ||
| scheduled = executor.scheduleAtFixedRate({ tick() }, 0L, periodNs, TimeUnit.NANOSECONDS) | ||
| } | ||
| } | ||
|
|
||
| private fun tick() { | ||
| if (!running) return | ||
| val now = SystemClock.elapsedRealtime() | ||
| if (now - lastSecondTs >= 1000) { | ||
| onFps(framesThisSecond) | ||
| framesThisSecond = 0 | ||
| lastSecondTs = now | ||
| } | ||
|
|
||
| val w = width | ||
| val h = height | ||
| val buffer = JavaI420Buffer.allocate(w, h) | ||
| val yPlane: ByteBuffer = buffer.dataY | ||
| val uPlane: ByteBuffer = buffer.dataU | ||
| val vPlane: ByteBuffer = buffer.dataV | ||
| val ts = (now % 5000).toInt() | ||
|
|
||
| for (y in 0 until h) { | ||
| for (x in 0 until w) { | ||
| val yVal = (((x + ts / 10) % w).toFloat() / w * 255f).roundToInt().coerceIn(0, 255) | ||
| yPlane.put(y * buffer.strideY + x, yVal.toByte()) | ||
| } | ||
| } | ||
| val chromaW = (w + 1) / 2 | ||
| val chromaH = (h + 1) / 2 | ||
| for (y in 0 until chromaH) { | ||
| for (x in 0 until chromaW) { | ||
| uPlane.put(y * buffer.strideU + x, 128.toByte()) | ||
| vPlane.put(y * buffer.strideV + x, 128.toByte()) | ||
| } | ||
| } | ||
|
|
||
| val timestampNs = org.webrtc.TimestampAligner.getRtcTimeNanos() | ||
| val frame = VideoFrame(buffer, 0, timestampNs) | ||
| observer.onFrameCaptured(frame) | ||
| frame.release() | ||
| framesThisSecond += 1 | ||
|
Comment on lines
+70
to
+95
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Release the I420 buffer after frame dispatch.
val frame = VideoFrame(buffer, 0, timestampNs)
observer.onFrameCaptured(frame)
frame.release()
+ buffer.release()🤖 Prompt for AI Agents |
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| package com.visionrtc | ||
|
|
||
| import android.content.Context | ||
| import android.view.View | ||
| import com.facebook.react.bridge.ReactApplicationContext | ||
| import com.facebook.react.module.annotations.ReactModule | ||
| import com.facebook.react.uimanager.SimpleViewManager | ||
| import com.facebook.react.uimanager.ThemedReactContext | ||
| import com.facebook.react.uimanager.annotations.ReactProp | ||
| import org.webrtc.SurfaceViewRenderer | ||
|
|
||
| @ReactModule(name = VisionRtcViewManager.NAME) | ||
| class VisionRtcViewManager(private val context: ReactApplicationContext) : SimpleViewManager<SurfaceViewRenderer>() { | ||
|
|
||
| companion object { const val NAME = "VisionRTCView" } | ||
|
|
||
| override fun getName(): String = NAME | ||
|
|
||
| override fun createViewInstance(reactContext: ThemedReactContext): SurfaceViewRenderer { | ||
| val view = SurfaceViewRenderer(reactContext) | ||
| val eglBase = (reactContext.getNativeModule(VisionRTCModule::class.java) as? VisionRTCModule)?.let { it }?.let { it } // will init later | ||
| // We use a lazy global egl in VisionRTCModule; init when attaching track | ||
| view.setEnableHardwareScaler(true) | ||
| view.setMirror(false) | ||
| return view | ||
| } | ||
|
|
||
| @ReactProp(name = "trackId") | ||
| fun setTrackId(view: SurfaceViewRenderer, trackId: String?) { | ||
| val module = view.context.applicationContext.let { (it as? Context)?.let { _ -> null } } | ||
| val mod = (context.getNativeModule(VisionRTCModule::class.java)) ?: return | ||
| val handle = mod.findTrack(trackId) | ||
| view.release() | ||
| view.init(mod.eglContext(), null) | ||
| handle?.track?.addSink(view) | ||
| } | ||
|
|
||
| override fun onDropViewInstance(view: SurfaceViewRenderer) { | ||
| super.onDropViewInstance(view) | ||
| view.release() | ||
| } | ||
|
Comment on lines
+28
to
+41
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Detach previous sinks when retargeting the view We keep adding the same - val handle = mod.findTrack(trackId)
- view.release()
- view.init(mod.eglContext(), null)
- handle?.track?.addSink(view)
+ val previous = attachedTracks[view]
+ if (previous != null && previous != trackId) {
+ mod.findTrack(previous)?.track?.removeSink(view)
+ attachedTracks.remove(view)
+ }
+ if (trackId.isNullOrBlank()) {
+ view.release()
+ return
+ }
+ val handle = mod.findTrack(trackId) ?: return
+ if (previous == trackId) return
+ view.release()
+ view.init(mod.eglContext(), null)
+ handle.track.addSink(view)
+ attachedTracks[view] = trackId override fun onDropViewInstance(view: SurfaceViewRenderer) {
super.onDropViewInstance(view)
- view.release()
+ attachedTracks.remove(view)?.let { previous ->
+ context.getNativeModule(VisionRTCModule::class.java)
+ ?.findTrack(previous)
+ ?.track
+ ?.removeSink(view)
+ }
+ view.release()
}
🤖 Prompt for AI Agents |
||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.