Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keeping two AVPlayers in sync

I have a client who has a very specific request for the app that requires two AVPlayers to be in sync. One video is for some content and the other one is for a presenter speaking about the content. Using a AVMutableComposition to combine them into one video is not an option because the presenter video has to be able to respond to user generated events (e.g. they want to have a feature to show/hide the presenter) and I don't believe there is a way to have that kind of control over a specific AVMutableCompositionTrack.

So, I'm left with figuring out how to ensure that two AVPlayers stay in sync and I was wondering if anyone has had experience with this or suggestions for other tools to accomplish this.

Thanks

like image 802
moshe Avatar asked Apr 07 '12 19:04

moshe


1 Answers

The following methods are the ones to use

- (void)setRate:(float)rate 
           time:(CMTime)itemTime 
     atHostTime:(CMTime)hostClockTime;

- (void)prerollAtRate:(float)rate 
    completionHandler:(void (^)(BOOL finished))completionHandler;

Caveats

Important This method is not currently supported for HTTP Live Streaming or when automaticallyWaitsToMinimizeStalling is YES. For clients linked against iOS 10.0 and later or macOS 10.12 and later, invoking this method when automaticallyWaitsToMinimizeStalling is YES will raise an NSInvalidArgument exception.

This is an expected behavior since "live" is "present" and cannot be seek forward and setting the rate to less than 1.0 it will cause to extra buffering the stream (second point is a guess).

Documentation

https://developer.apple.com/documentation/avfoundation/avplayer/1386591-setrate?language=objc

https://developer.apple.com/documentation/avfoundation/avplayer/1389712-prerollatrate?language=objc

As a side note consider that HLS streams are not truly live streams, the "present moment" could vary several seconds among clientes consuming the stream, the opposite of WebRTC for example where the delay between publishers and consumers is kinda of warranted for 1 second max.

like image 64
zevarito Avatar answered Nov 19 '22 16:11

zevarito