title: Decode audio samples from hls stream on ios?
body: <p>I am trying to decode audio samples from a remote HLS (m3u8) stream on an iOS device for further processing of the data, e.g. record to a file.
  As reference stream <a href="http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8" rel="noreferrer">http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8</a> is used.</p>
  
  <p>By using an AVURLAsset in combination with the AVPlayer I am able to show the video as preview on a CALayer.
  I can also get the raw video data (CVPixelBuffer) by using AVPlayerItemVideoOutput. The audio is hearable over the speaker of the iOS device as well.</p>
  
  <p>This is the code I am using at the moment for the AVURLAsset and AVPlayer:</p>
  
  <pre><code>NSURL* url = [NSURL URLWithString:@"http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8"];
  AVURLAsset *asset = [AVURLAsset URLAssetWithURL:url options:nil];
  NSString *tracksKey = @"tracks";
  
  [asset loadValuesAsynchronouslyForKeys:@[tracksKey] completionHandler: ^{
  
      dispatch_async(dispatch_get_main_queue(), ^{
  
          NSError* error = nil;
          AVKeyValueStatus status = [asset statusOfValueForKey:tracksKey error:&amp;error];
  
          if (status == AVKeyValueStatusLoaded) {
              NSDictionary *settings = @
              {
                  (id)kCVPixelBufferPixelFormatTypeKey: @(kCVPixelFormatType_32BGRA),
                  @"IOSurfaceOpenGLESTextureCompatibility": @YES,
                  @"IOSurfaceOpenGLESFBOCompatibility": @YES,
              };
              AVPlayerItemVideoOutput* output = [[AVPlayerItemVideoOutput alloc] initWithPixelBufferAttributes:settings];
              AVPlayerItem* playerItem = [AVPlayerItem playerItemWithAsset:asset];
              [playerItem addOutput:output];
              AVPlayer* player = [AVPlayer playerWithPlayerItem:playerItem];
              [player setVolume: 0.0];    // no preview audio
              self.playerItem = playerItem;
              self.player = player;
              self.playerItemVideoOutput = output;
  
              AVPlayerLayer* playerLayer = [AVPlayerLayer playerLayerWithPlayer: player];
              [self.preview.layer addSublayer: playerLayer];
              [playerLayer setFrame: self.preview.bounds];
              [playerLayer setVideoGravity: AVLayerVideoGravityResizeAspectFill];
  
              [self setPlayerLayer: playerLayer];
  
              [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playerItemNewAccessLogEntry:) name:AVPlayerItemNewAccessLogEntryNotification object:self.playerItem];
  
              [_player addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew context:&amp;PlayerStatusContext];
              [_playerItem addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew context:&amp;PlayerItemStatusContext];
              [_playerItem addObserver:self forKeyPath:@"tracks" options:0 context:nil];
          }
      });
  }];
  
  -(void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
  {
      if (self.player.status == AVPlayerStatusReadyToPlay &amp;&amp; context == &amp;PlayerStatusContext) {
          [self.player play];
      }
  }
  </code></pre>
  
  <p>To get the raw video data of the HLS stream I use:</p>
  
  <pre><code>CVPixelBufferRef buffer = [self.playerItemVideoOutput copyPixelBufferForItemTime:self.playerItem.currentTime itemTimeForDisplay:nil];
  
  if (!buffer) {
      return;
  }
  CMSampleBufferRef newSampleBuffer = NULL;
  CMSampleTimingInfo timingInfo = kCMTimingInfoInvalid;
  timingInfo.duration = CMTimeMake(33, 1000);
  int64_t ts = timestamp * 1000.0;
  timingInfo.decodeTimeStamp = CMTimeMake(ts, 1000);
  timingInfo.presentationTimeStamp = timingInfo.decodeTimeStamp;
  
  CMVideoFormatDescriptionRef videoInfo = NULL;
  CMVideoFormatDescriptionCreateForImageBuffer(
                                                   NULL, buffer, &amp;videoInfo);
  CMSampleBufferCreateForImageBuffer(kCFAllocatorDefault,
                                         buffer,
                                         true,
                                         NULL,
                                         NULL,
                                         videoInfo,
                                         &amp;timingInfo,
                                         &amp;newSampleBuffer);
  
  // do something here with sample buffer...
  
  CFRelease(buffer);
  CFRelease(newSampleBuffer);
  </code></pre>
  
  <p>Now I would like to get access to the raw audio data as well, but had no luck so far.
  I tried to use MTAudioProcessingTap as described here:<br>
  <a href="http://venodesigns.net/2014/01/08/recording-live-audio-streams-on-ios/" rel="noreferrer">http://venodesigns.net/2014/01/08/recording-live-audio-streams-on-ios/</a><br>
  Unfortunately I could not get this to work properly. I succeeded in getting access to the underlying assetTrack of the AVPlayerItem but the callback mehtods "prepare" and "process" of the MTAudioProcessingTap is never getting called. I am not sure if I am on the right track here.</p>
  
  <p>AVPlayer is playing the audio of the stream via the speaker, so internally the audio seems to be available as raw audio data. Is it possible to get access to the raw audio data? If it is not possible with AVPlayer, are there any other approaches?</p>
  
  <p>If possible, I would not like to use ffmpeg, because the hardware decoder of the iOS device should be used for the decoding of the stream.</p>