title: rtsp:// liveStream with AVPlayer
body: <p>I want to play liveStream on iPhoneDevice with AVPlayer. Also i want to get <code>CVPixelBufferRef</code> from this stream for next usage.</p>
  
  <p>I use <a href="https://developer.apple.com/library/ios/documentation/AudioVideo/Conceptual/AVFoundationPG/Articles/02_Playback.html" rel="nofollow noreferrer">Apple guide</a> for creating player. Currently with locally stored videoFiles this player works just fine, also when i try to play this AppleSampleStremURL - <code>http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8</code> - its work sine too. </p>
  
  <p>Problems appear when i want to play stream with rtsp:// like this one: <code>rtsp://192.192.168.1:8227/TTLS/Streaming/channels/2?videoCodecType=H.264</code></p>
  
  <p>An code - almost all done using guid provided by Apple, but anyway:</p>
  
  <p>Prepare asset for playing</p>
  
  <pre><code>- (void)initialSetupWithURL:(NSURL *)url
  {
      NSDictionary *assetOptions = @{ AVURLAssetPreferPreciseDurationAndTimingKey : @YES,
                                      AVURLAssetReferenceRestrictionsKey : @(AVAssetReferenceRestrictionForbidNone)};
      self.urlAsset = [AVURLAsset URLAssetWithURL:url options:assetOptions];
  }
  </code></pre>
  
  <p>Prepare player </p>
  
  <pre><code>- (void)prepareToPlay
  {
      NSArray *keys = @[@"tracks"];
      __weak SPHVideoPlayer *weakSelf = self;
      [weakSelf.urlAsset loadValuesAsynchronouslyForKeys:keys completionHandler:^{
          dispatch_async(dispatch_get_main_queue(), ^{
              [weakSelf startLoading];
          });
      }];
  }
  
  - (void)startLoading
  {
      NSError *error;
      AVKeyValueStatus status = [self.urlAsset statusOfValueForKey:@"tracks" error:&amp;error];
      if (status == AVKeyValueStatusLoaded) {
          self.assetDuration = CMTimeGetSeconds(self.urlAsset.duration);
          NSDictionary* videoOutputOptions = @{ (id)kCVPixelBufferPixelFormatTypeKey : @(kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange)};
          self.videoOutput = [[AVPlayerItemVideoOutput alloc] initWithPixelBufferAttributes:videoOutputOptions];
          self.playerItem = [AVPlayerItem playerItemWithAsset: self.urlAsset];
  
          [self.playerItem addObserver:self
                            forKeyPath:@"status"
                               options:NSKeyValueObservingOptionInitial
                               context:&amp;ItemStatusContext];
          [self.playerItem addObserver:self
                            forKeyPath:@"loadedTimeRanges"
                               options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld
                               context:&amp;ItemStatusContext];
          [[NSNotificationCenter defaultCenter] addObserver:self
                                                   selector:@selector(playerItemDidReachEnd:)
                                                       name:AVPlayerItemDidPlayToEndTimeNotification
                                                     object:self.playerItem];
          [[NSNotificationCenter defaultCenter] addObserver:self
                                                   selector:@selector(didFailedToPlayToEnd)
                                                       name:AVPlayerItemFailedToPlayToEndTimeNotification
                                                     object:nil];
  
          [self.playerItem addOutput:self.videoOutput];
  
          self.assetPlayer = [AVPlayer playerWithPlayerItem:self.playerItem];
  
          [self addPeriodicalObserver];
          NSLog(@"Player created");
      } else {
          NSLog(@"The asset's tracks were not loaded:\n%@", error.localizedDescription);
      }
  }
  </code></pre>
  
  <p>Problems appear here - <code>AVKeyValueStatus status = [self.urlAsset statusOfValueForKey:@"tracks" error:&amp;error];</code> - this line with rtsp:// URL return <code>AVKeyValueStatusFailed</code> </p>
  
  <p>with Error:</p>
  
  <pre><code>Printing description of error:
  Error Domain=AVFoundationErrorDomain Code=-11800 
  "The operation could not be completed" 
  UserInfo=0x7fd1ea5a8a10 {NSLocalizedFailureReason=An unknown error occurred (-12936), 
  NSLocalizedDescription=The operation could not be completed,
  NSURL=rtsp://192.168.1.168:8556/PSIA/Streaming/channels/2?videoCodecType=H.264,
  NSUnderlyingError=0x7fd1ea53f830 "The operation couldn’t be completed.
  (OSStatus error -12936.)"}
  </code></pre>
  
  <p>I also looked for questions:</p>
  
  <ol>
  <li><a href="https://stackoverflow.com/questions/6431517/http-live-streaming-with-avplayer-in-ios-4-0">FirstOne</a> - try to use for this stream sample app from Apple - <strong><a href="https://developer.apple.com/library/ios/samplecode/StitchedStreamPlayer/Listings/Classes_MyStreamingMovieViewController_m.html#//apple_ref/doc/uid/DTS40010092-Classes_MyStreamingMovieViewController_m-DontLinkElementID_8" rel="nofollow noreferrer">StitchedStreamPlayer</a></strong> - but get few different error for streams that i want to play there</li>
  <li><a href="https://stackoverflow.com/a/4201419/2012219">SecondOne</a> - try to use both suggestion - <code>fileURLWithPath</code> return incorrect URL like : <code>rtsp://192.168.1.168:8556/PSIA/Streaming/channels/2?videoCodecType=H.264    --file:///</code> - so it's i guess incorrect</li>
  <li>According to AppleDev try to create AvPlayer with diff approaches: like <code>[AVPlayer playerWithPlayerItem:playerItem]</code> and like <code>[AVPlayer playerWithURL:url]</code> - nothing changed. Also try to setup different setting for AVAsset - in <code>initialSetupWithURL</code> (see method implementation above).</li>
  </ol>
  
  <p>So, <strong>question</strong> is AVPlayer support playing rtsp:// stream?
  If yes, can someone provide sample of correct usage? Nad what i'm doing wrong in code?
  If AvPlayer not support rtsp:// maybe exist some alternative solution?</p>