AVPlayer HLS live stream falls behind - ios

I'm working on AVPlayer + HLS with live stream,
Sometimes, the video will fall behind and I need all clients to be in-sync with the stream.
How do we know when this happens? I'd want to re-sync it when it falls behind.
Thanks!

You may use your player API to analyze playback statistics and events like rebuffering.

To do so you will need to use the method seekToTime and pass the player current time with the value of the new buffered time.

Related

Adding an external audio track to HLS video

I am using AVPlayer to run a HLS video. The video has no sound. Also i have a audio track url of same format m3u8. Can i somehow change the AVPlayer item asset or something while running my video without sound to add my other audio track so that they are sort of played together.
Disappointingly, you can't create an AVComposition using non local video and audio tracks and play that.
But HLS is at its heart a sort of textual playlist consisting of media pieces that can be played either serially or concurrently. If you inspect both your video and audio m3u8 streams, you should be able to concoct a new single m3u8 stream that includes both video and audio.
HOWEVER, disappointingly, it seems you can't play this resulting stream as a local file (why!?!), so you'd set up an http server to serve it to you, either locally or from or afar, or maybe (!?) you could avoid all that with a clever use of AVAssetResourceLoaderDelegate.
It also seems synchronising two AVPlayers is unsupported too, although perhaps that situation has improved.

Is it possible to get a YouTube audio stream in Web Audio's MediaElementAudioSourceNode?

I want to be able to load only the audio stream of the youtube video and process it (EQ, Effects, etc.) through a graph of Web Audio nodes.
Is this doable? Any open-source work out there, doing that?
Thanks in advance to all and any responses.
No, because you can't get audio streams cross-domain. (that is, if your code could be hosted on YouTube.com, sure, but not from mydomain.com.)
The reason for this (you CAN do it if CORS is set up, but it's not on YouTube) is because if you can get the audio stream, you can do a bit-copy of the data. Just like images, they don't want to leak the raw data.

Does Looping remotely fetched video in AVPlayer cause redownload?

I'm using a github repo to playback video on an app, specifically Player. I'm trying to better understand the code and AVFoundation in general:
If I set a NSURL for the AVAssetURL with a remote server URL video and into the AVPlayer's AVPlayerItem, is it streaming the data from the remote URL? My guess is that this is true for the first play (and that it isn't downloaded all at once and then played, please correct me if I am wrong)
And then if I continuously loop the video that I started playing (by setting the seekToTime to kCMTimeZero once it has ended), am I causing the AVPlayer/Asset to continuously re-stream/re-download the file every time it loops? Or is it cached until the AVPlayer/Asset is released?
If anyone could help me answer or point me to the right Apple docs, I would appreciate it! Thanks!
Another similar (?) question said AVAssetResourceDownloader, but I'm not looking to download the file to local disk (if that's what it does).
You don't download the file but you fill the AVPlayer buffer (a sort of cache)
If you seek to zero you don't download the files since you have the buffer.
You can compare the AVPlayer buffer to the YouTube one.

How to download a LIVE HLS m3u8 stream on iOS

HLS streams can be "live" or "VOD". Downloading a VOD HLS stream is easy.
However, I want to download (or record) say 5 minutes of a LIVE HLS stream. Is this possible?
If I do so, I am sure I have to make significant changes to the m3u8 file... One reason is live streams do not have a "duration", but the stream I download has to be streamed as VOD so it must have a duration. There might be various other changes required that I am not aware of. Presumably URLs of ts segments would also need to be changed.
Any tips or advice (hopefully actual code!)?
Thanks!
PS. Note that this question is not about playing back the stream in offline mode - I know I need an HTTP server for that.
The Live playlist uses a sliding-window. You need to periodically reload it after target-duration time and download only the new segments as they appear in the list (they will be removed at a later time).
Save the #EXTINF for each segment and start writing them in a VOD playlist using the same target-duration and a media sequence starting at 0.
When you want to stop recording add the EXT-X-ENDLIST tag at the end.
It doesn't matter how you name your segments as long as you use the same name in the m3u8.

Does HTTP Live Streaming Support Rewind?

Is there a way to do rewind with an HLS implementation?
Here is a link of what HLS is: https://developer.apple.com/streaming/
Wikipedia says yes: "Later versions of the protocol also provide for trick mode fast-forward and rewind and integration of subtitle" http://en.wikipedia.org/wiki/HTTP_Live_Streaming
So how do I implement a rewind in HLS?
You can use AVPlayer to play an HTTP Live Stream video. It will allow you to seek in reverse just like you would seek forward. So in that sense, you can rewind. However, due to the compressed nature of the stream, rewind is quite slow, because it needs to rewind all the way back to the closest keyframe and then interpolate forward to your seek point.
If you're looking for a smooth rewind, it really isn't possible with any stream that is compressed (for the reasons stated above). But you can get decent "rewind" performance if your movie file lives on the device.
I would suggested creating an AVPlayerItem from your HLS and then play that item in an AVPlayer. Experiment a bit to see what the results are and go from there.

Resources