Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to parse Server's "frame meta"? #3995

Closed
nnnpa31 opened this issue May 12, 2023 · 2 comments
Closed

How to parse Server's "frame meta"? #3995

nnnpa31 opened this issue May 12, 2023 · 2 comments

Comments

@nnnpa31
Copy link

nnnpa31 commented May 12, 2023

Hi, I'm trying to write a go implementation of the Scrcpy client. When I insert the obtained h264 stream data into the libavcodec's decoder, I sometimes get this error

[h264 @ 0x2e56a40] No start code is found.
[h264 @ 0x2e56a40] Error splitting the input into NAL units.
4 Invalid data found when processing input

I think this is because I am not splitting the NALU data correctly and wrapping it into the AVPacket.
To simplify the code, as you did in #646, I want to parse the frame meta data to read the complete NALU packet one by one.
I didn't understand the Server java code and I'm using version 1.25.
Can you give me some clues?

@rom1v
Copy link
Collaborator

rom1v commented May 12, 2023

Here are relevant pointers:

scrcpy/app/src/demuxer.c

Lines 77 to 96 in cb20bcb

// The video stream contains raw packets, without time information. When we
// record, we retrieve the timestamps separately, from a "meta" header
// added by the server before each raw packet.
//
// The "meta" header length is 12 bytes:
// [. . . . . . . .|. . . .]. . . . . . . . . . . . . . . ...
// <-------------> <-----> <-----------------------------...
// PTS packet raw packet
// size
//
// 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
// CK...... ........ ........ ........ ........ ........ ........ ........
// ^^<------------------------------------------------------------------->
// || PTS
// | `- key frame
// `-- config packet

private void writeFrameMeta(FileDescriptor fd, int packetSize, long pts, boolean config, boolean keyFrame) throws IOException {
headerBuffer.clear();
long ptsAndFlags;
if (config) {
ptsAndFlags = PACKET_FLAG_CONFIG; // non-media data packet
} else {
ptsAndFlags = pts;
if (keyFrame) {
ptsAndFlags |= PACKET_FLAG_KEY_FRAME;
}
}
headerBuffer.putLong(ptsAndFlags);
headerBuffer.putInt(packetSize);
headerBuffer.flip();
IO.writeFully(fd, headerBuffer);
}

@nnnpa31
Copy link
Author

nnnpa31 commented May 12, 2023

Thank you very much! I think this is enough to solve my needs:

 // The video stream contains raw packets, without time information. When we 
 // record, we retrieve the timestamps separately, from a "meta" header 
 // added by the server before each raw packet. 
 // 
 // The "meta" header length is 12 bytes: 
 // [. . . . . . . .|. . . .]. . . . . . . . . . . . . . . ... 
 //  <-------------> <-----> <-----------------------------... 
 //        PTS        packet        raw packet 
 //                    size 
 // 
 // 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 
 // CK...... ........ ........ ........ ........ ........ ........ ........ 
 // ^^<-------------------------------------------------------------------> 
 // ||                                PTS 
 // | `- key frame 
 //  `-- config packet 

I will close this issue.

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

No branches or pull requests

2 participants