title: Read HLS Playlist information to dynamically change the preferredBitRate of an Item
body: <p>I'm working on a video app, we are changing form regular mp4 files to HLS, one of the many reasons we have to do the change is that we hace much more control over the bandwidth usage of videos (we load lots of other stuff in our player, so we need to optimize the experience the best way).</p>
  
  <p>So, <code>AVFoundation</code> introduced in iOS10 the ability to control the bandwidth using:</p>
  
  <pre><code>AVPlayerItem *playerItem = [AVPlayerItem playerItemWithAsset:self.urlAsset];
  playerItem.preferredForwardBufferDuration = 30.0;
  playerItem.preferredPeakBitRate = 200000.0; // Remember this line
  </code></pre>
  
  <p>There's also a configuration introduced on iOS11 to set the maximum resolution of the item with <code>preferredMaximumResolution</code>, So we're using it, but we still need a solution for iOS10 devices.</p>
  
  <p>Well, now we have control over the <code>preferredPeakBitRate</code> that's nice, but we have a problem, not all the HLS sources are generated by us, so, let's say we want to set a maximum resolution of 480p when you're not connected to a wifi network, today I don't have way to achieve that, not always I'm going to be able to know how much bandwidth needs the 480p source for the selected <code>HLS playlist</code>.</p>
  
  <p>One thing I was thinking about is to read the information inside the <code>m3u8</code> file, to at least know which are the different quality sources that my player can show and how much bandwidth needs everyone.</p>
  
  <p>One way to do this, would download the <code>m3u8</code> playlist as a plain text, use a regex to read the file and process this data, well, I'm trying to avoid that, I think that this should far less difficult.</p>
  
  <p>I cannot read this information from the tracks, because a) I can't find the information, b) the tracks are replaced dynamically when changing the quality, yeah 1 track for every quality level.</p>
  
  <p>So, I don't know how I can get this information, I've searched google, stackoverflow and I can't find this information, does any one can help me?</p>
  
  <p>Here's an example for what I want to do, I have this example playlist:</p>
  
  <pre><code>#EXTM3U
  
  #EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=314000,RESOLUTION=228x128,CODECS="mp4a.40.2"
  test-hls-1-16a709300abeb08713a5cada91ab864e_hls_duplex_192k.m3u8
  #EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=478000,RESOLUTION=400x224,CODECS="avc1.42001e,mp4a.40.2"
  test-hls-1-16a709300abeb08713a5cada91ab864e_hls_duplex_400k.m3u8
  #EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=691000,RESOLUTION=480x270,CODECS="avc1.42001e,mp4a.40.2"
  test-hls-1-16a709300abeb08713a5cada91ab864e_hls_duplex_600k.m3u8
  #EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=1120000,RESOLUTION=640x360,CODECS="avc1.4d001f,mp4a.40.2"
  test-hls-1-16a709300abeb08713a5cada91ab864e_hls_duplex_1000k.m3u8
  #EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=1661000,RESOLUTION=960x540,CODECS="avc1.4d001f,mp4a.40.2"
  test-hls-1-16a709300abeb08713a5cada91ab864e_hls_duplex_1500k.m3u8
  </code></pre>
  
  <p>And I just want to have that information available on an array inside my code, something like this:</p>
  
  <pre><code>NSArray&lt;ZZMetadata *&gt; *metadataArray = self.urlAsset.bandwidthMetadata;
  NSLog(@"Metadata info: %@", metadataArray);
  </code></pre>
  
  <p>And print something like this:</p>
  
  <pre><code>&lt;__NSArrayM 0x123456789&gt; (
  &lt;ZZMetadata 0x234567890&gt; {
    trackId: 1
    neededBandwidth: 314000
    resolution: 228x128
    codecs: ...
    ...
  }
  &lt;ZZMetadata 0x345678901&gt; {
    trackId: 2
    neededBandwidth: 478000
    resolution: 400x224
  }
  ...
  } 
  </code></pre>