Store packet flags in PTS most significant bits

A special PTS value was used to encode a config packet.

To prepare for adding more flags, use the most significant bits of the
PTS field to store flags.
This commit is contained in:
Romain Vimont 2022-02-11 19:31:01 +01:00
parent 29c163959c
commit e3c2398aa2
2 changed files with 20 additions and 6 deletions

View file

@ -13,7 +13,10 @@
#define BUFSIZE 0x10000 #define BUFSIZE 0x10000
#define HEADER_SIZE 12 #define HEADER_SIZE 12
#define NO_PTS UINT64_C(-1)
#define SC_PACKET_FLAG_CONFIG (UINT64_C(1) << 63)
#define SC_PACKET_PTS_MASK (SC_PACKET_FLAG_CONFIG - 1)
static bool static bool
sc_demuxer_recv_packet(struct sc_demuxer *demuxer, AVPacket *packet) { sc_demuxer_recv_packet(struct sc_demuxer *demuxer, AVPacket *packet) {
@ -28,6 +31,14 @@ sc_demuxer_recv_packet(struct sc_demuxer *demuxer, AVPacket *packet) {
// size // size
// //
// It is followed by <packet_size> bytes containing the packet/frame. // It is followed by <packet_size> bytes containing the packet/frame.
//
// The most significant bits of the PTS are used for packet flags:
//
// byte 7 byte 6 byte 5 byte 4 byte 3 byte 2 byte 1 byte 0
// C....... ........ ........ ........ ........ ........ ........ ........
// ^<-------------------------------------------------------------------->
// | PTS
// `- config packet
uint8_t header[HEADER_SIZE]; uint8_t header[HEADER_SIZE];
ssize_t r = net_recv_all(demuxer->socket, header, HEADER_SIZE); ssize_t r = net_recv_all(demuxer->socket, header, HEADER_SIZE);
@ -35,9 +46,8 @@ sc_demuxer_recv_packet(struct sc_demuxer *demuxer, AVPacket *packet) {
return false; return false;
} }
uint64_t pts = buffer_read64be(header); uint64_t pts_flags = buffer_read64be(header);
uint32_t len = buffer_read32be(&header[8]); uint32_t len = buffer_read32be(&header[8]);
assert(pts == NO_PTS || (pts & 0x8000000000000000) == 0);
assert(len); assert(len);
if (av_new_packet(packet, len)) { if (av_new_packet(packet, len)) {
@ -51,7 +61,11 @@ sc_demuxer_recv_packet(struct sc_demuxer *demuxer, AVPacket *packet) {
return false; return false;
} }
packet->pts = pts != NO_PTS ? (int64_t) pts : AV_NOPTS_VALUE; if (pts_flags & SC_PACKET_FLAG_CONFIG) {
packet->pts = AV_NOPTS_VALUE;
} else {
packet->pts = pts_flags & SC_PACKET_PTS_MASK;
}
return true; return true;
} }

View file

@ -28,7 +28,7 @@ public class ScreenEncoder implements Device.RotationListener {
// Keep the values in descending order // Keep the values in descending order
private static final int[] MAX_SIZE_FALLBACK = {2560, 1920, 1600, 1280, 1024, 800}; private static final int[] MAX_SIZE_FALLBACK = {2560, 1920, 1600, 1280, 1024, 800};
private static final int NO_PTS = -1; private static final long PACKET_FLAG_CONFIG = 1L << 63;
private final AtomicBoolean rotationChanged = new AtomicBoolean(); private final AtomicBoolean rotationChanged = new AtomicBoolean();
private final ByteBuffer headerBuffer = ByteBuffer.allocate(12); private final ByteBuffer headerBuffer = ByteBuffer.allocate(12);
@ -183,7 +183,7 @@ public class ScreenEncoder implements Device.RotationListener {
long pts; long pts;
if ((bufferInfo.flags & MediaCodec.BUFFER_FLAG_CODEC_CONFIG) != 0) { if ((bufferInfo.flags & MediaCodec.BUFFER_FLAG_CODEC_CONFIG) != 0) {
pts = NO_PTS; // non-media data packet pts = PACKET_FLAG_CONFIG; // non-media data packet
} else { } else {
if (ptsOrigin == 0) { if (ptsOrigin == 0) {
ptsOrigin = bufferInfo.presentationTimeUs; ptsOrigin = bufferInfo.presentationTimeUs;