Skip to content

Commit b9fad54

Browse files
committed
Browser Fix
1 parent 19e7fed commit b9fad54

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

mcav-common/src/main/java/me/brandonli/mcav/media/player/driver/ChromeDriverPlayer.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.openqa.selenium.chrome.ChromeOptions;
3636
import org.openqa.selenium.devtools.DevTools;
3737
import org.openqa.selenium.devtools.v136.page.Page;
38+
import org.openqa.selenium.devtools.v136.page.model.ScreencastFrameMetadata;
3839
import org.openqa.selenium.interactions.Action;
3940
import org.openqa.selenium.interactions.Actions;
4041

@@ -57,6 +58,9 @@ public final class ChromeDriverPlayer implements BrowserPlayer {
5758
private final DevTools tools;
5859

5960
private volatile byte[] frameBuffer;
61+
private volatile long frameWidth;
62+
private volatile long frameHeight;
63+
6064
private volatile CompletableFuture<Void> captureTask;
6165
private volatile CompletableFuture<Void> processingTask;
6266

@@ -112,6 +116,9 @@ private void startScreenCapture() {
112116
final Base64.Decoder decoder = Base64.getDecoder();
113117
this.tools.addListener(Page.screencastFrame(), frame -> {
114118
if (this.running.get()) {
119+
final ScreencastFrameMetadata frameMetadata = frame.getMetadata();
120+
this.frameWidth = (long) frameMetadata.getDeviceWidth();
121+
this.frameHeight = (long) frameMetadata.getDeviceHeight();
115122
this.frameBuffer = decoder.decode(frame.getData());
116123
this.tools.send(Page.screencastFrameAck(frame.getSessionId()));
117124
}
@@ -131,6 +138,7 @@ private void processFrames() {
131138
while (this.running.get()) {
132139
if (this.frameBuffer != null) {
133140
final StaticImage staticImage = StaticImage.bytes(this.frameBuffer);
141+
staticImage.resize(metadata.getVideoWidth(), metadata.getVideoHeight());
134142
VideoPipelineStep current = this.videoPipeline;
135143
while (current != null) {
136144
current.process(staticImage, metadata);
@@ -152,13 +160,25 @@ public void sendMouseEvent(final int x, final int y, final MouseClick type) {
152160
if (!this.running.get()) {
153161
return;
154162
}
163+
final int[] translated = this.translateCoordinates(x, y);
164+
final int newX = translated[0];
165+
final int newY = translated[1];
155166
final Actions actions = new Actions(this.driver);
156-
final Actions move = actions.moveToLocation(x, y);
167+
final Actions move = actions.moveToLocation(newX, newY);
157168
final Actions modified = this.getAction(type, move);
158169
final Action action = modified.build();
159170
action.perform();
160171
}
161172

173+
private int[] translateCoordinates(final int x, final int y) {
174+
final VideoMetadata videoMetadata = this.source.getMetadata();
175+
final int targetWidth = videoMetadata.getVideoWidth();
176+
final int targetHeight = videoMetadata.getVideoHeight();
177+
final int newX = (int) (((float) x / this.frameWidth) * targetWidth);
178+
final int newY = (int) (((float) y / this.frameHeight) * targetHeight);
179+
return new int[] { newX, newY };
180+
}
181+
162182
/**
163183
* {@inheritDoc}
164184
*/

0 commit comments

Comments
 (0)