Skip to content

Commit

Permalink
Merge 0a26e8d into fdd4363
Browse files Browse the repository at this point in the history
  • Loading branch information
romtsn committed Feb 13, 2024
2 parents fdd4363 + 0a26e8d commit 268db42
Show file tree
Hide file tree
Showing 8 changed files with 973 additions and 0 deletions.
63 changes: 63 additions & 0 deletions sentry-android-replay/api/sentry-android-replay.api
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
public final class io/sentry/android/replay/BuildConfig {
public static final field BUILD_TYPE Ljava/lang/String;
public static final field DEBUG Z
public static final field LIBRARY_PACKAGE_NAME Ljava/lang/String;
public static final field VERSION_NAME Ljava/lang/String;
public fun <init> ()V
}

public final class io/sentry/android/replay/WindowRecorder {
public fun <init> ()V
public final fun startRecording (Landroid/content/Context;)V
public final fun stopRecording ()V
}

public abstract interface class io/sentry/android/replay/video/SimpleFrameMuxer {
public abstract fun getVideoTime ()J
public abstract fun isStarted ()Z
public abstract fun muxVideoFrame (Ljava/nio/ByteBuffer;Landroid/media/MediaCodec$BufferInfo;)V
public abstract fun release ()V
public abstract fun start (Landroid/media/MediaFormat;)V
}

public final class io/sentry/android/replay/video/SimpleMp4FrameMuxer : io/sentry/android/replay/video/SimpleFrameMuxer {
public fun <init> (Ljava/lang/String;F)V
public fun getVideoTime ()J
public fun isStarted ()Z
public fun muxVideoFrame (Ljava/nio/ByteBuffer;Landroid/media/MediaCodec$BufferInfo;)V
public fun release ()V
public fun start (Landroid/media/MediaFormat;)V
}

public final class io/sentry/android/replay/viewhierarchy/ViewHierarchyNode {
public static final field Companion Lio/sentry/android/replay/viewhierarchy/ViewHierarchyNode$Companion;
public fun <init> (FFIIZLjava/lang/Integer;Landroid/graphics/Rect;)V
public synthetic fun <init> (FFIIZLjava/lang/Integer;Landroid/graphics/Rect;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public final fun component1 ()F
public final fun component2 ()F
public final fun component3 ()I
public final fun component4 ()I
public final fun component5 ()Z
public final fun component6 ()Ljava/lang/Integer;
public final fun component7 ()Landroid/graphics/Rect;
public final fun copy (FFIIZLjava/lang/Integer;Landroid/graphics/Rect;)Lio/sentry/android/replay/viewhierarchy/ViewHierarchyNode;
public static synthetic fun copy$default (Lio/sentry/android/replay/viewhierarchy/ViewHierarchyNode;FFIIZLjava/lang/Integer;Landroid/graphics/Rect;ILjava/lang/Object;)Lio/sentry/android/replay/viewhierarchy/ViewHierarchyNode;
public fun equals (Ljava/lang/Object;)Z
public final fun getChildren ()Ljava/util/List;
public final fun getDominantColor ()Ljava/lang/Integer;
public final fun getHeight ()I
public final fun getShouldRedact ()Z
public final fun getVisibleRect ()Landroid/graphics/Rect;
public final fun getWidth ()I
public final fun getX ()F
public final fun getY ()F
public fun hashCode ()I
public final fun setChildren (Ljava/util/List;)V
public fun toString ()Ljava/lang/String;
}

public final class io/sentry/android/replay/viewhierarchy/ViewHierarchyNode$Companion {
public final fun adjustAlpha (I)I
public final fun fromView (Landroid/view/View;)Lio/sentry/android/replay/viewhierarchy/ViewHierarchyNode;
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
package io.sentry.android.replay

import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.RectF
import android.os.Handler
import android.os.HandlerThread
import android.os.Looper
import android.os.SystemClock
import android.util.Log
import android.view.PixelCopy
import android.view.View
import android.view.ViewGroup
import android.view.ViewTreeObserver
import io.sentry.android.replay.video.SimpleVideoEncoder
import io.sentry.android.replay.viewhierarchy.ViewHierarchyNode
import java.lang.ref.WeakReference
import java.util.WeakHashMap
import kotlin.system.measureTimeMillis

internal class ScreenshotRecorder(
val rootView: WeakReference<View>,
val encoder: SimpleVideoEncoder
) : ViewTreeObserver.OnDrawListener {

private val thread = HandlerThread("SentryReplay").also { it.start() }
private val handler = Handler(thread.looper)
private val bitmapToVH = WeakHashMap<Bitmap, ViewHierarchyNode>()

companion object {
const val TAG = "ScreenshotRecorder"
}

private var lastCapturedAtMs: Long? = null
override fun onDraw() {
val now = SystemClock.uptimeMillis()
if (lastCapturedAtMs != null && (now - lastCapturedAtMs!!) < 1000L) {
return
}
lastCapturedAtMs = now

val root = rootView.get()
if (root == null || root.width <= 0 || root.height <= 0) {
return
}

val window = root.phoneWindow ?: return
val bitmap = Bitmap.createBitmap(
root.width,
root.height,
Bitmap.Config.ARGB_8888
)
Log.e("BITMAP CREATED", bitmap.toString())

val time = measureTimeMillis {
val rootNode = ViewHierarchyNode.fromView(root)
root.traverse(rootNode)
bitmapToVH[bitmap] = rootNode
}
Log.e("TIME", time.toString())

// val latch = CountDownLatch(1)

Handler(Looper.getMainLooper()).postAtFrontOfQueue {
PixelCopy.request(
window,
bitmap,
{ copyResult: Int ->
Log.d(TAG, "PixelCopy result: $copyResult")
if (copyResult != PixelCopy.SUCCESS) {
Log.e(TAG, "Failed to capture screenshot")
return@request
}

Log.e("BITMAP CAPTURED", bitmap.toString())
val viewHierarchy = bitmapToVH[bitmap]

if (viewHierarchy != null) {
val canvas = Canvas(bitmap)
viewHierarchy.traverse {
if (it.shouldRedact && (it.width > 0 && it.height > 0)) {
it.visibleRect ?: return@traverse

val paint = Paint().apply {
color = it.dominantColor ?: Color.BLACK
}
canvas.drawRoundRect(RectF(it.visibleRect), 10f, 10f, paint)
}
}
}

val scaledBitmap = Bitmap.createScaledBitmap(
bitmap,
encoder.muxerConfig.videoWidth,
encoder.muxerConfig.videoHeight,
true
)
// val baos = ByteArrayOutputStream()
// scaledBitmap.compress(Bitmap.CompressFormat.JPEG, 75, baos)
// val bmp = BitmapFactory.decodeByteArray(baos.toByteArray(), 0, baos.size())
encoder.encode(scaledBitmap)
// bmp.recycle()
scaledBitmap.recycle()
bitmap.recycle()
Log.i(TAG, "Captured a screenshot")
// latch.countDown()
},
handler
)
}

// val success = latch.await(200, MILLISECONDS)
// Log.i(TAG, "Captured a screenshot: $success")
}

private fun ViewHierarchyNode.traverse(callback: (ViewHierarchyNode) -> Unit) {
callback(this)
if (this.children != null) {
this.children!!.forEach {
it.traverse(callback)
}
}
}

private fun View.traverse(parentNode: ViewHierarchyNode) {
if (this !is ViewGroup) {
return
}

if (this.childCount == 0) {
return
}

val childNodes = ArrayList<ViewHierarchyNode>(this.childCount)
for (i in 0 until childCount) {
val child = getChildAt(i)
if (child != null) {
val childNode = ViewHierarchyNode.fromView(child)
childNodes.add(childNode)
child.traverse(childNode)
}
}
parentNode.children = childNodes
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package io.sentry.android.replay

import android.content.Context
import android.graphics.Point
import android.os.Build.VERSION
import android.os.Build.VERSION_CODES
import android.view.View
import android.view.ViewTreeObserver
import android.view.WindowManager
import io.sentry.android.replay.video.MuxerConfig
import io.sentry.android.replay.video.SimpleVideoEncoder
import java.io.File
import java.lang.ref.WeakReference
import java.util.WeakHashMap
import java.util.concurrent.atomic.AtomicBoolean
import kotlin.LazyThreadSafetyMode.NONE
import kotlin.math.roundToInt

class WindowRecorder {

private val rootViewsSpy by lazy(NONE) {
RootViewsSpy.install()
}

private var encoder: SimpleVideoEncoder? = null
private val isRecording = AtomicBoolean(false)
private val recorders: WeakHashMap<View, ViewTreeObserver.OnDrawListener> = WeakHashMap()

private val onRootViewsChangedListener = OnRootViewsChangedListener { root, added ->
if (added) {
// stop tracking other windows so they don't interfere in the recording like a 25th frame effect
recorders.entries.forEach {
it.key.viewTreeObserver.removeOnDrawListener(it.value)
}

val recorder = ScreenshotRecorder(WeakReference(root), encoder!!)
recorders[root] = recorder
root.viewTreeObserver?.addOnDrawListener(recorder)
} else {
root.viewTreeObserver?.removeOnDrawListener(recorders[root])
recorders.remove(root)

recorders.entries.forEach {
it.key.viewTreeObserver.addOnDrawListener(it.value)
}
}
}

fun startRecording(context: Context) {
if (isRecording.getAndSet(true)) {
return
}

val wm = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
// val (height, width) = (wm.currentWindowMetrics.bounds.bottom /
// context.resources.displayMetrics.density).roundToInt() to
// (wm.currentWindowMetrics.bounds.right /
// context.resources.displayMetrics.density).roundToInt()
// TODO: API level check
// PixelCopy takes screenshots including system bars, so we have to get the real size here
val aspectRatio = if (VERSION.SDK_INT >= VERSION_CODES.R) {
wm.currentWindowMetrics.bounds.bottom.toFloat() / wm.currentWindowMetrics.bounds.right.toFloat()
} else {
val screenResolution = Point()
@Suppress("DEPRECATION")
wm.defaultDisplay.getRealSize(screenResolution)
screenResolution.y.toFloat() / screenResolution.x.toFloat()
}

val videoFile = File(context.cacheDir, "sentry-sr.mp4")
encoder = SimpleVideoEncoder(
MuxerConfig(
videoFile,
videoWidth = (720 / aspectRatio).roundToInt(),
videoHeight = 720,
frameRate = 1f,
bitrate = 500 * 1000
)
)
encoder?.start()
rootViewsSpy.listeners += onRootViewsChangedListener
}

fun stopRecording() {
rootViewsSpy.listeners -= onRootViewsChangedListener
recorders.entries.forEach {
it.key.viewTreeObserver.removeOnDrawListener(it.value)
}
recorders.clear()
encoder?.startRelease()
encoder = null
}
}
Loading

0 comments on commit 268db42

Please sign in to comment.