Skip to content

Add session metadata for video stream#6159

Merged
rom1v merged 4 commits intodevfrom
video_session
Jan 10, 2026
Merged

Add session metadata for video stream#6159
rom1v merged 4 commits intodevfrom
video_session

Conversation

@rom1v
Copy link
Copy Markdown
Collaborator

@rom1v rom1v commented Jun 15, 2025

Introduce a new packet type, a "session" packet, containing metadata about the encoding session. It is used only for the video stream, and currently includes the video resolution.

For illustration, here is a sequence of packets on the video stream:

                                    device rotation
                                    v
CODEC | SESSION | MEDIA | MEDIA | … | SESSION | MEDIA | MEDIA | …
       1920x1080 <-----------------> 1080x1920 <------------------
                  encoding session 1            encoding session 2

This metadata is not strictly necessary, since the video resolution can be determined after decoding. However, it allows detection of cases where the encoder does not respect the requested size (and logs a warning), even without decoding (e.g., when there is no video playback).

Additional metadata could be added later if necessary, for example the actual device rotation.

Refs #5918
Refs #5894

@sbfkcel
Copy link
Copy Markdown

sbfkcel commented Oct 9, 2025

To make the session data more compact, additional metadata is reserved. Would it be better to put the flag and width together in the first 4 bytes?

    public void writeSessionMeta(int width, int height) throws IOException {
        writeSessionMeta(width, height, 0);
    }

    public void writeSessionMeta(int width, int height, int extra) throws IOException {
        if (sendStreamMeta) {
            headerBuffer.clear();

            int flagAndWidth = (int)(PACKET_FLAG_SESSION | (width & ((1 << 31) - 1)));
            headerBuffer.putInt(flagAndWidth);
            headerBuffer.putInt(height);
            headerBuffer.putInt(extra);
            headerBuffer.flip();
            IO.writeFully(fd, headerBuffer);
        }
    }

public void writeSessionMeta(int width, int height) throws IOException {
if (sendStreamMeta) {
headerBuffer.clear();
headerBuffer.putInt((int) (PACKET_FLAG_SESSION >> 32)); // Set the first bit to 1
headerBuffer.putInt(width);
headerBuffer.putInt(height);
headerBuffer.flip();
IO.writeFully(fd, headerBuffer);
}
}

@rom1v
Copy link
Copy Markdown
Collaborator Author

rom1v commented Oct 9, 2025

That way, the packet header is always 12 bytes:

ssize_t r = net_recv_all(demuxer->socket, header, SC_PACKET_HEADER_SIZE);

If it was variable, it would require the client to buffer the data (more copies) and "packetize" (more complexity). As a tradeoff, it uses 2 syscalls (one to read the header, one to read the full payload).

@sbfkcel
Copy link
Copy Markdown

sbfkcel commented Oct 13, 2025

Sorry, I may not have explained it clearly enough. The Session Meta data is indeed always 12 bytes. However, since the flag, width, and height each occupy 4 bytes, there’s no remaining space for expansion.

Therefore, my proposal is to combine the flag and width into 4 bytes, keep height as 4 bytes, and reserve the remaining 4 bytes for future expansion.

@rom1v
Copy link
Copy Markdown
Collaborator Author

rom1v commented Oct 13, 2025

Note that there is no backward or forward compatibility (the protocol is always used between matching client and server), so there is nothing to "reserve", the protocol can be changed completely for any version.

If we want to add something taking 31 bits or less, you can store it just after the flag. If we need a lot more data, we can add a payload. We can also move the width/height fields as needed.

@rom1v
Copy link
Copy Markdown
Collaborator Author

rom1v commented Jan 8, 2026

Now that #6216 is merged, the next scrcpy version will be 4.0.

This is a good time to merge this protocol change.

rom1v and others added 4 commits January 10, 2026 11:53
The stream metadata will contain both:
 - the codec ID at the start of the stream
 - the session metadata (video width and height) at the start of each
   "session" (typically on rotation)

PR #6159 <#6159>
Introduce a new packet type, a "session" packet, containing metadata
about the encoding session. It is used only for the video stream and
currently includes the video resolution.

For illustration, here is a sequence of packets on the video stream:

                                        device rotation
                                        v
    CODEC | SESSION | MEDIA | MEDIA | … | SESSION | MEDIA | MEDIA | …
           1920x1080 <-----------------> 1080x1920 <------------------
                      encoding session 1            encoding session 2

This metadata is not strictly necessary, since the video resolution can
be determined after decoding. However, it allows detection of cases
where the encoder does not respect the requested size (and logs a
warning), even without decoding (e.g., when there is no video playback).

Additional metadata could be added later if necessary, for example the
actual device rotation.

Refs #5918 <#5918>
Refs #5894 <#5894>
PR #6159 <#6159>

Co-authored-by: gz0119 <liyong2@4399.com>
The delay buffer must forward the session packets while preserving
their order relative to media packets.

PR #6159 <#6159>
Warn if the size of a decoded video frame does not match the session
metadata.

PR #6159 <#6159>
@rom1v rom1v merged commit 826076f into dev Jan 10, 2026
rom1v added a commit that referenced this pull request Apr 17, 2026
The protocol changed in commit 78cba1b.

Update the documentation accordingly.

Refs #6159 <#6159>
rom1v added a commit that referenced this pull request Apr 17, 2026
The protocol changed in commit 78cba1b.

Update the documentation accordingly.

Refs #6159 <#6159>
rom1v added a commit that referenced this pull request Apr 17, 2026
The protocol changed in commit 78cba1b.

Update the documentation accordingly.

Refs #6159 <#6159>
PR #6776 <#6776>
rom1v added a commit that referenced this pull request Apr 19, 2026
The protocol changed in commit 78cba1b.

Update the documentation accordingly.

Refs #6159 <#6159>
PR #6776 <#6776>
rom1v added a commit that referenced this pull request Apr 19, 2026
The protocol changed in commit 78cba1b.

Update the documentation accordingly.

Refs #6159 <#6159>
PR #6776 <#6776>
rom1v added a commit that referenced this pull request Apr 24, 2026
The protocol changed in commit 78cba1b.

Update the documentation accordingly.

Refs #6159 <#6159>
PR #6776 <#6776>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants