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

search for sync token #131

Closed
wants to merge 2 commits into from
Closed

search for sync token #131

wants to merge 2 commits into from

Conversation

faljse
Copy link

@faljse faljse commented Feb 16, 2017

Modified from #85 discussion
Works for me :)

@faljse
Copy link
Author

faljse commented Feb 16, 2017

Sometimes locks up at: https://github.com/phoboslab/jsmpeg/blob/master/src/mpeg1.js#L749
Edit:
Problem seems to be:

  • 0x47 is detected somewhere in data stream, not being a sync token
  • loop mentioned above is unlikely to hit exit condition in random data

Is there a more reliable way to detect sync tokens?
(Couldn`t find checksum in mpeg-ts header)

@phoboslab
Copy link
Owner

phoboslab commented Feb 16, 2017

After a bit of digging, MPEG-TS seems to dictate that to find synchronization, you have to detect 5 consecutive 0x47 sync tokens with a distance of 188 bytes each. I.e. if you find a sync token, repeat to check byteIndex+187 for 0x47 4 times.

So if the streams looses sync, it's best to wait until the TS parser has at least 6 * 188 bytes buffered to scan through. Then scan through the first 188 bytes for the first token and jump +187 bytes each to find the next 4.

See for example: https://linuxtv.org/wiki/index.php/MPEG-2_Frame_Aligner

resync on lost connection
resync if parsePacket() cant find sync token
make sure buffer is byte aligned on resync
@faljse
Copy link
Author

faljse commented Feb 17, 2017

Done :)
I did quite a bit of testing today, and it works pretty reliable.

It might be a good idea to add some error recovery for things like the while(true) in decodeBlock()
(Maybe throw an exception and resync if we read() beyond the buffer in BitBuffer).
But for now this works well enough for me.

@phoboslab phoboslab closed this in 7bdf107 Feb 18, 2017
@phoboslab
Copy link
Owner

Thanks!

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