Skip to content

Commit

Permalink
Move workarounds to a separate class
Browse files Browse the repository at this point in the history
Extract workarounds (currently only one) to a separate class to avoid
polluting the main code.
  • Loading branch information
rom1v committed Nov 19, 2019
1 parent 18f2e33 commit 213c468
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
11 changes: 1 addition & 10 deletions server/src/main/java/com/genymobile/scrcpy/ScreenEncoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import android.media.MediaFormat;
import android.os.Build;
import android.os.IBinder;
import android.os.Looper;
import android.view.Surface;

import java.io.FileDescriptor;
Expand Down Expand Up @@ -53,15 +52,7 @@ public boolean consumeRotationChange() {
}

public void streamScreen(Device device, FileDescriptor fd) throws IOException {
// Some devices internally create a Handler when creating an input Surface, causing an exception:
// "Can't create handler inside thread that has not called Looper.prepare()"
// <https://github.com/Genymobile/scrcpy/issues/240>
//
// Use Looper.prepareMainLooper() instead of Looper.prepare() to avoid a NullPointerException:
// "Attempt to read from field 'android.os.MessageQueue android.os.Looper.mQueue'
// on a null object reference"
// <https://github.com/Genymobile/scrcpy/issues/921>
Looper.prepareMainLooper();
Workarounds.prepareMainLooper();

MediaFormat format = createFormat(bitRate, maxFps, iFrameInterval);
device.setRotationListener(this);
Expand Down
21 changes: 21 additions & 0 deletions server/src/main/java/com/genymobile/scrcpy/Workarounds.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.genymobile.scrcpy;

import android.os.Looper;

public final class Workarounds {
private Workarounds() {
// not instantiable
}

public static void prepareMainLooper() {
// Some devices internally create a Handler when creating an input Surface, causing an exception:
// "Can't create handler inside thread that has not called Looper.prepare()"
// <https://github.com/Genymobile/scrcpy/issues/240>
//
// Use Looper.prepareMainLooper() instead of Looper.prepare() to avoid a NullPointerException:
// "Attempt to read from field 'android.os.MessageQueue android.os.Looper.mQueue'
// on a null object reference"
// <https://github.com/Genymobile/scrcpy/issues/921>
Looper.prepareMainLooper();
}
}

0 comments on commit 213c468

Please sign in to comment.