Expand the metadata to 16 bytes, 13-16 is the screen direction

This commit is contained in:
gz0119 2025-03-03 15:16:44 +08:00
parent d892a9aac5
commit b7c588a988
6 changed files with 24 additions and 13 deletions

BIN
.DS_Store vendored Normal file

Binary file not shown.

BIN
app/.DS_Store vendored Normal file

Binary file not shown.

View file

@ -9,7 +9,7 @@
#include "util/binary.h"
#include "util/log.h"
#define SC_PACKET_HEADER_SIZE 12
#define SC_PACKET_HEADER_SIZE 16
#define SC_PACKET_FLAG_CONFIG (UINT64_C(1) << 63)
#define SC_PACKET_FLAG_KEY_FRAME (UINT64_C(1) << 62)
@ -82,11 +82,11 @@ sc_demuxer_recv_packet(struct sc_demuxer *demuxer, AVPacket *packet) {
// The video and audio streams contain a sequence of raw packets (as
// provided by MediaCodec), each prefixed with a "meta" header.
//
// The "meta" header length is 12 bytes:
// [. . . . . . . .|. . . .]. . . . . . . . . . . . . . . ...
// <-------------> <-----> <-----------------------------...
// PTS packet raw packet
// size
// The "meta" header length is 16 bytes:
// [. . . . . . . .|. . . .][. . . .]. . . . . . . . . . . . . . . ...
// <-------------> <------> <------> <-----------------------------...
// PTS packet screen raw packet
// size orientation
//
// It is followed by <packet_size> bytes containing the packet/frame.
//
@ -107,6 +107,8 @@ sc_demuxer_recv_packet(struct sc_demuxer *demuxer, AVPacket *packet) {
uint64_t pts_flags = sc_read64be(header);
uint32_t len = sc_read32be(&header[8]);
uint32_t screen_orientation = sc_read32be(&header[12]);
LOGD("Screen Orientation: %u", screen_orientation);
assert(len);
if (av_new_packet(packet, len)) {
@ -131,6 +133,7 @@ sc_demuxer_recv_packet(struct sc_demuxer *demuxer, AVPacket *packet) {
}
packet->dts = packet->pts;
return true;
}

View file

@ -132,7 +132,7 @@ public final class Server {
audioCapture = new AudioPlaybackCapture(options.getAudioDup());
}
Streamer audioStreamer = new Streamer(connection.getAudioFd(), audioCodec, options.getSendCodecMeta(), options.getSendFrameMeta());
Streamer audioStreamer = new Streamer(connection.getAudioFd(), audioCodec, options.getSendCodecMeta(), options.getSendFrameMeta(), options.getDisplayId());
AsyncProcessor audioRecorder;
if (audioCodec == AudioCodec.RAW) {
audioRecorder = new AudioRawRecorder(audioCapture, audioStreamer);
@ -144,7 +144,7 @@ public final class Server {
if (video) {
Streamer videoStreamer = new Streamer(connection.getVideoFd(), options.getVideoCodec(), options.getSendCodecMeta(),
options.getSendFrameMeta());
options.getSendFrameMeta(), options.getDisplayId());
SurfaceCapture surfaceCapture;
if (options.getVideoSource() == VideoSource.DISPLAY) {
NewDisplay newDisplay = options.getNewDisplay();

View file

@ -210,7 +210,7 @@ public final class Device {
}
}
private static int getCurrentRotation(int displayId) {
public static int getCurrentRotation(int displayId) {
assert displayId != DISPLAY_ID_NONE;
if (displayId == 0) {

View file

@ -11,6 +11,7 @@ import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Arrays;
import com.genymobile.scrcpy.util.Ln;
public final class Streamer {
@ -21,14 +22,16 @@ public final class Streamer {
private final Codec codec;
private final boolean sendCodecMeta;
private final boolean sendFrameMeta;
private final int displayId;
private final ByteBuffer headerBuffer = ByteBuffer.allocate(12);
private final ByteBuffer headerBuffer = ByteBuffer.allocate(16);
public Streamer(FileDescriptor fd, Codec codec, boolean sendCodecMeta, boolean sendFrameMeta) {
public Streamer(FileDescriptor fd, Codec codec, boolean sendCodecMeta, boolean sendFrameMeta, int displayId) {
this.fd = fd;
this.codec = codec;
this.sendCodecMeta = sendCodecMeta;
this.sendFrameMeta = sendFrameMeta;
this.displayId = displayId;
}
public Codec getCodec() {
@ -76,11 +79,15 @@ public final class Streamer {
}
if (sendFrameMeta) {
writeFrameMeta(fd, buffer.remaining(), pts, config, keyFrame);
int screenOrientation = getScreenOrientation();
writeFrameMeta(fd, buffer.remaining(), pts, config, keyFrame, screenOrientation);
}
IO.writeFully(fd, buffer);
}
private int getScreenOrientation() {
return Device.getCurrentRotation(this.displayId);
}
public void writePacket(ByteBuffer codecBuffer, MediaCodec.BufferInfo bufferInfo) throws IOException {
long pts = bufferInfo.presentationTimeUs;
@ -89,7 +96,7 @@ public final class Streamer {
writePacket(codecBuffer, pts, config, keyFrame);
}
private void writeFrameMeta(FileDescriptor fd, int packetSize, long pts, boolean config, boolean keyFrame) throws IOException {
private void writeFrameMeta(FileDescriptor fd, int packetSize, long pts, boolean config, boolean keyFrame, int screenOrientation) throws IOException {
headerBuffer.clear();
long ptsAndFlags;
@ -104,6 +111,7 @@ public final class Streamer {
headerBuffer.putLong(ptsAndFlags);
headerBuffer.putInt(packetSize);
headerBuffer.putInt(screenOrientation);
headerBuffer.flip();
IO.writeFully(fd, headerBuffer);
}