Use VideoStreamer directly from ScreenEncoder

The Callbacks interface notifies new packets. But in addition, the
screen encoder will need to write headers on start.

We could add a function onStart(), but for simplicity, just remove the
interface, which brings no value, and call the streamer directly.

Refs 87972e2022
This commit is contained in:
Romain Vimont 2023-02-06 11:44:18 +01:00
parent 5b2ec66222
commit ae29e5b562
2 changed files with 6 additions and 11 deletions

View file

@ -21,10 +21,6 @@ import java.util.concurrent.atomic.AtomicBoolean;
public class ScreenEncoder implements Device.RotationListener {
public interface Callbacks {
void onPacket(ByteBuffer codecBuffer, MediaCodec.BufferInfo bufferInfo) throws IOException;
}
private static final int DEFAULT_I_FRAME_INTERVAL = 10; // seconds
private static final int REPEAT_FRAME_DELAY_US = 100_000; // repeat after 100ms
private static final String KEY_MAX_FPS_TO_ENCODER = "max-fps-to-encoder";
@ -63,7 +59,7 @@ public class ScreenEncoder implements Device.RotationListener {
return rotationChanged.getAndSet(false);
}
public void streamScreen(Device device, Callbacks callbacks) throws IOException, ConfigurationException {
public void streamScreen(Device device, VideoStreamer streamer) throws IOException, ConfigurationException {
MediaCodec codec = createCodec(videoMimeType, encoderName);
MediaFormat format = createFormat(videoMimeType, bitRate, maxFps, codecOptions);
IBinder display = createDisplay();
@ -92,7 +88,7 @@ public class ScreenEncoder implements Device.RotationListener {
codec.start();
alive = encode(codec, callbacks);
alive = encode(codec, streamer);
// do not call stop() on exception, it would trigger an IllegalStateException
codec.stop();
} catch (IllegalStateException | IllegalArgumentException e) {
@ -161,7 +157,7 @@ public class ScreenEncoder implements Device.RotationListener {
return 0;
}
private boolean encode(MediaCodec codec, Callbacks callbacks) throws IOException {
private boolean encode(MediaCodec codec, VideoStreamer streamer) throws IOException {
boolean eof = false;
MediaCodec.BufferInfo bufferInfo = new MediaCodec.BufferInfo();
@ -184,7 +180,7 @@ public class ScreenEncoder implements Device.RotationListener {
consecutiveErrors = 0;
}
callbacks.onPacket(codecBuffer, bufferInfo);
streamer.writePacket(codecBuffer, bufferInfo);
}
} finally {
if (outputBufferId >= 0) {

View file

@ -6,7 +6,7 @@ import java.io.FileDescriptor;
import java.io.IOException;
import java.nio.ByteBuffer;
public final class VideoStreamer implements ScreenEncoder.Callbacks {
public final class VideoStreamer {
private static final long PACKET_FLAG_CONFIG = 1L << 63;
private static final long PACKET_FLAG_KEY_FRAME = 1L << 62;
@ -28,8 +28,7 @@ public final class VideoStreamer implements ScreenEncoder.Callbacks {
IO.writeFully(fd, buffer);
}
@Override
public void onPacket(ByteBuffer codecBuffer, MediaCodec.BufferInfo bufferInfo) throws IOException {
public void writePacket(ByteBuffer codecBuffer, MediaCodec.BufferInfo bufferInfo) throws IOException {
if (sendFrameMeta) {
writeFrameMeta(fd, bufferInfo, codecBuffer.remaining());
}