Response header from AVPlayer - ios

I have a special use case where I need to track a response header from AVPlayer while it is streaming an HLS playlist.
I searched in the internet and read the AVPlayer documentation and did not find any specific way to get this. I know that there is a way to get the HTTP response headers from the request made by the app but I could not find anything to get the response header for the requests made by the AVPlayer.

I spent weeks looking for a way to do this for both requests and responses for the playlist and chunk requests. The only way I was able to find that worked was by passing the playback request through a reverse proxy on the device. This allows you to intercept the request, add headers, send it to the real server, and then extract the headers from the response before returning it to the AVPlayer.
I made a simple example project (with lots of comments and documentation) here:
https://github.com/kevinjameshunt/AVPlayer-HTTP-Headers-Example

Related

How to programmatically check if a live YouTube channel is streaming?

My company runs a live web stream and has started duplicating this to YouTube. Unfortunately the staff won't check if it's live and internet issues cause our web encoder to stop encoding at times.
Is there a programmatic way I can tell if a channel is ACTUALLY streaming? i.e. if live video is coming out the channel and not just that "the channel is live"?
You may use Search: list.
Using this request returns a collection of search results that match the query parameters that you have specified in the API request. Add part=snippet in your request since this is a required parameter. Then, you may add the following optional parameters with their corresponding values in your HTTP request:
channelId=[channelId] - to search resources created by a particular channel.
type=video - to retrieve a particular type of resource
eventType=live - to return only active broadcasts. Please note that if you use eventType, also set the type parameter's value to video.
Combining all of these parameters, you may send HTTP request using the following format:
https://www.googleapis.com/youtube/v3/search?part=snippet&channelId=UCXswCcAMb5bvEUIDEzXFGYg&type=video&eventType=live
To better filter your search, you may also opt to add more parameters that are listed in supported parameters.
Lastly, solution in this related SO post - How to check if YouTube channel is streaming live might also help.

How to add custom HTTP header field to AVPlayer requests for HLS

We are using AVPlayer to play an HLS stream, but we have a new requirement that we need to supply a custom HTTP header field to identify the client from the backend team. Is it possible to add custom HTTP header field to AVPlayer requests without resorting to hacking HLS playlist file to use a custom protocol?
I found this answer somewhere from the web. I am using it in my test app, but since this is an undocumented feature, there is a risk that apple may reject.
But technically, this is an option.
NSMutableDictionary* headers = [NSMutableDictionary dictionary];
[headers setObject:#"SOF" forKey:#"X-REQ-HEADER-TEST"];
AVURLAsset* asset = [AVURLAsset URLAssetWithURL:myUrl options:#{#"AVURLAssetHTTPHeaderFieldsKey": headers}];
AVPlayerItem* item = [AVPlayerItem playerItemWithAsset:asset];
[avPlayerObj replaceCurrentItemWithPlayerItem:item];
Usage of AVURLAssetHTTPHeaderFieldsKey is the one in question.
I spent weeks looking for a way to do this officially. For anyone else looking for an approach that would work for both requests and responses for the playlist and chunk requests, the only way I was able to find that worked was by passing the playback request through a reverse proxy on the device itself, which allows you to intercept the request, add headers, send it to the real server, and then extract the headers from the response before returning it to the AVPlayer.
I made a simple example project (with lots of comments and documentation) here:
https://github.com/kevinjameshunt/AVPlayer-HTTP-Headers-Example

NSURLProtocol fails for video request

I'm having some issues with video requests handled through a special protocol scheme in a NSURLProtocol subclass. Every other resource (images/text) are getting handled correctly, however, when a video request is sent, I only get a call to 'canInitWithRequest' and no follow up. So, my video resource doesn't get resolved. Now, I've looked around and I found no definite solution for this. Some people use instead an embedded HTTP server, but that seems an overkill. Does anyone know if this is a bug or if not, why is this limitation, is there an workaround for it?
A similar issue: Custom NSURLProtocol class for WebView doesn't work when loading video in HTML5 document , but unfortunately without an answer.
#Meda, I was facing the similar issue. Here what I found and hope it is useful to you.
I assume that you are using NSUrlProtocol because you want to intercept the video requests.
I was using web view which makes request for video over HTTP. It goes to NSURLProtocol and makes the request. When it receives the data, webView loads the video rendering plugin (looking at the mime type in HTTP header). The plugin needs the data to come as Partial HTTP response (Response code 206). Further, the plugin does not use NSURLProtocol class but uses network layer below it. So requests that plugin makes, do not go thru NSURLProtocol. Considering this, there could be 2 problems in your case.
1. HTTP server you are using may not be supporting partial responses.
2. HTTP server is not reachable directly (Can you access the video from safari or any other
browser on your device?)
You can verify both the cases by taking network trace. use tcpdump (available on Mac) to take network trace and see what is happening there.

YouTube URL parameters issue. YouTube not using HTTP ranges

I want to understand all the parameters of a YouTube video as YouTube is now not using the HTTP range element but using its own range parameters inside its URL and thus I am not able to make a session using the Wireshark as i see so many HTTP 200 ok with video/x-flv and thus my player is not able to associate them as it reads the HTTP responses and its ranges. Here are the sample URLs what YouTube is sending for a single video. Is there any documetation available for this as well?
GET /videoplayback?algorithm=throttle-factor&burst=40&cp=U0hTTldOVF9FUUNOM19PSFhIOnBNNjJuUGVsZDZU&expire=1349736707&factor=1.25&fexp=922401%2C920704%2C912806%2C900711%2C913546%2C913556%2C925109%2C919003%2C920201%2C912706%2C900816&id=ee88421fc6a3f768&ip=90.84.144.49&ipbits=8&itag=34&keepalive=yes&key=yt1&ms=au&mt=1349713452&mv=m&newshard=yes&range=13-1781759&signature=84690C3B43F6FFBDD69E0E7009D0A6436946D642.904ADA59891696B5D1411665853784438D9E35D4&source=youtube&sparams=algorithm%2Cburst%2Ccp%2Cfactor%2Cid%2Cip%2Cipbits%2Citag%2Csource%2Cupn%2Cexpire&sver=3&upn=fc55lw1im0s HTTP/1.1
GET /videoplayback?algorithm=throttle-factor&burst=40&cp=U0hTTldOVF9FUUNOM19PSFhIOnBNNjJuUGVsZDZU&expire=1349736707&factor=1.25&fexp=922401%2C920704%2C912806%2C900711%2C913546%2C913556%2C925109%2C919003%2C920201%2C912706%2C900816&id=ee88421fc6a3f768&ip=90.84.144.49&ipbits=8&itag=34&keepalive=yes&key=yt1&ms=au&mt=1349713563&mv=m&newshard=yes&range=10690560-12472319&signature=84690C3B43F6FFBDD69E0E7009D0A6436946D642.904ADA59891696B5D1411665853784438D9E35D4&source=youtube&sparams=algorithm%2Cburst%2Ccp%2Cfactor%2Cid%2Cip%2Cipbits%2Citag%2Csource%2Cupn%2Cexpire&sver=3&upn=fc55lw1im0s&redirect_counter=1&cms_redirect=yes HTTP/1.1
GET /videoplayback?algorithm=throttle-factor&burst=40&cp=U0hTTldOVF9FUUNOM19PSFhIOnBNNjJuUGVsZDZU&expire=1349736707&factor=1.25&fexp=922401%2C920704%2C912806%2C900711%2C913546%2C913556%2C925109%2C919003%2C920201%2C912706%2C900816&id=ee88421fc6a3f768&ip=90.84.144.49&ipbits=8&itag=34&keepalive=yes&key=yt1&ms=au&mt=1349713452&mv=m&newshard=yes&range=12472320-14254079&signature=84690C3B43F6FFBDD69E0E7009D0A6436946D642.904ADA59891696B5D1411665853784438D9E35D4&source=youtube&sparams=algorithm%2Cburst%2Ccp%2Cfactor%2Cid%2Cip%2Cipbits%2Citag%2Csource%2Cupn%2Cexpire&sver=3&upn=fc55lw1im0s HTTP/1.1
There is no documentation for this sort of thing, and video streams are generally considered a "black box" from a developer's perspective.
For your question, see this thread.
Youtube streaming works providing 1.7MB chunk pieces controlled by range param, which indicate [start byte]-[end byte].
I'am also questioning similar issue here.

How to I access a SoundCloud public stream?

How do I play a track from a SoundCloud URL, which, for example, I got from the xml response from a query
<stream-url>https://api.soundcloud.com/tracks/31164607/stream</stream-url>
I should have thought that it would have been as easy as:
https://api.soundcloud.com/tracks/31164607/stream&client_id=my_client_id
yet I get
<error>401 - Unauthorized</error>
All I want to do is consume it in a Silverlight MediaElement, so all I need is set some url to the MediaElement's Source property.
I've checked an application that I wrote about 2 years ago, and THEN, accessing the stream url was as easy as this for a public track:
http://api.soundcloud.com/tracks/18163056/stream&consumer_key=MY_CONSUMER_KEY
however this no longer seems to work.
For example, all I had to do then in C# was:
MediaElement me = new MediaElement();
me.Source= new Url("http://api.soundcloud.com/tracks/18163056/stream&consumer_key=MY_CONSUMER_KEY");
me.Play();
Any hints would be appreciated.
I had a reply on a Microsoft forum that seems to imply that SoundCloud might not be possible to stream to Windows 8 Metro devices without consuming the whole stream before playback starts - which is quite worrying and would seem to imply that to make authentication possible, it would have to be done entirely in the url querystring insterad of using the header:
(The following reply is the answer to the following question: 'I am able to access an audio stream by http using the MediaElement, however I need to access it via https in which I need to add the oAuth info to the header of the initial request.
How is this done when using a MediaElement, and if it cannot be done, what is the workaround for consuming an audio feed in Metro 8 that requires header authentication to stream?')
"Direct access to the underlying network stream is not currently permitted by the MediaElement. Because of this there is currently no way to modify the header of the HTTP request to include any additional authentication information. That said, you do have control over the URL. You could theoretically setup an HTTP proxy service that translated the HTTP GET request parameters into the necessary oAuth credentials. Keep in mind that this is just a theoretical workaround. You may find different behavior in practice. Another theoretical workaround would be to handle the oAuth yourself via a raw stream socket and pass the retuned media data to the MediaElement via "Set Source" and a "Random Access Stream". Please keep in mind that this method has major limitations. in order to use a "Random Access Stream" with the ME you need to make sure all of the data is available before passing it to the ME."
The proxy service is not scalable for an application that is merely distributed for free as every stream would need to come via the proxy. And the raw stream socket, although getting around this, would mean that playback could not start until the whole file had downloaded - and this goes against all current UX (User Experience) guidelines.
So once again, if anyone has any tips, or info about how the whole authentication thing can be achieved in a querystring instead of using headers, I'd appreciate it!
I'm a little confused about whether you're referring to a public or a private track? If it's a public track, then you shouldn't need to send any authentication information, just your client id.
When I request https://api.soundcloud.com/tracks/31164607/stream?client_id=YOUR_CLIENT_ID then I get a 302 redirect to the proper mp3 stream.
Remember, adding parameters to a URL must start with a ? not &. This could (more than likely) be the reason why you are getting a 401 (SC is not picking up the client_id).
After authentication the link like this
http://api.soundcloud.com/tracks/103229681/stream?consumer_key=d61f17a08f86bfb1dea28539908bc9bf
is working fine. I am using Action Script.
I'm following up on Tom's reply because he calls attention to url character specificity. My HTTP requests randomly started failing today, and I was prefacing my client_Id with a ?. As soon as I changed that single ? to &, it started working. So in my case, SC wasn't picking up my client_Id because I used the wrong character. I think depending on where in the request we're talking about specifically, it's worth noting that differences between ? and & do make a difference.

Resources