-
-
Notifications
You must be signed in to change notification settings - Fork 452
[SR] Add screenshot recorder #3203
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
Merged
romtsn
merged 15 commits into
rz/feat/session-replay
from
rz/feat/session-replay-sources
Mar 4, 2024
Merged
Changes from 9 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
d702876
Add screenshot recorder
romtsn 11f699c
Add TODOs and license headers
romtsn dd0e9a4
Api dump
romtsn d34ddee
Formatting
romtsn 0cca47c
Lint
romtsn 0a26e8d
Merge branch 'rz/feat/session-replay' into rz/feat/session-replay-sou…
romtsn 5ebdfed
Format code
getsentry-bot 04f43ed
More comments
romtsn b461847
Disable detekt plugin for now
romtsn 4e55ec0
Specify SHA for license headers
romtsn 9603672
Address review from Dhiogo
romtsn 1ce57cb
Address review from Markus
romtsn 1951891
api dump
romtsn b2940c4
Address review from Markus
romtsn 002a0f3
Api dump
romtsn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
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; | ||
} | ||
|
This file contains hidden or 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
148 changes: 148 additions & 0 deletions
148
sentry-android-replay/src/main/java/io/sentry/android/replay/ScreenshotRecorder.kt
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
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() { | ||
// TODO: replace with Debouncer from sentry-core | ||
val now = SystemClock.uptimeMillis() | ||
if (lastCapturedAtMs != null && (now - lastCapturedAtMs!!) < 1000L) { | ||
romtsn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return | ||
} | ||
lastCapturedAtMs = now | ||
|
||
val root = rootView.get() | ||
if (root == null || root.width <= 0 || root.height <= 0) { | ||
romtsn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return | ||
} | ||
|
||
val window = root.phoneWindow ?: return | ||
val bitmap = Bitmap.createBitmap( | ||
root.width, | ||
root.height, | ||
Bitmap.Config.ARGB_8888 | ||
) | ||
Log.e("BITMAP CREATED", bitmap.toString()) | ||
romtsn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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)) { | ||
romtsn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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( | ||
romtsn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 | ||
} | ||
} |
97 changes: 97 additions & 0 deletions
97
sentry-android-replay/src/main/java/io/sentry/android/replay/WindowRecorder.kt
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
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() | ||
romtsn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
private val onRootViewsChangedListener = OnRootViewsChangedListener { root, added -> | ||
if (added) { | ||
if (recorders.containsKey(root)) { | ||
// TODO: log | ||
return@OnRootViewsChangedListener | ||
} | ||
// 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() | ||
romtsn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
rootViewsSpy.listeners += onRootViewsChangedListener | ||
} | ||
|
||
fun stopRecording() { | ||
rootViewsSpy.listeners -= onRootViewsChangedListener | ||
recorders.entries.forEach { | ||
it.key.viewTreeObserver.removeOnDrawListener(it.value) | ||
} | ||
recorders.clear() | ||
encoder?.startRelease() | ||
encoder = null | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.