带有AVPlayer的rtsp://liveStream [英] rtsp:// liveStream with AVPlayer

查看:165
本文介绍了带有AVPlayer的rtsp://liveStream的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在带有AVPlayer的iPhoneDevice上播放liveStream.我也想从此流中获取CVPixelBufferRef以便下次使用.

I want to play liveStream on iPhoneDevice with AVPlayer. Also i want to get CVPixelBufferRef from this stream for next usage.

我使用 Apple指南用于创建播放器.当前使用本地存储的videoFiles时,此播放器工作正常,当我尝试播放此AppleSampleStremURL-http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8-其工作正弦时也是如此.

I use Apple guide for creating player. Currently with locally stored videoFiles this player works just fine, also when i try to play this AppleSampleStremURL - http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8 - its work sine too.

当我想像这样使用rtsp://播放流时出现问题:rtsp://192.192.168.1:8227/TTLS/Streaming/channels/2?videoCodecType=H.264

Problems appear when i want to play stream with rtsp:// like this one: rtsp://192.192.168.1:8227/TTLS/Streaming/channels/2?videoCodecType=H.264

代码-几乎所有操作都是使用Apple提供的guid完成的,但无论如何:

An code - almost all done using guid provided by Apple, but anyway:

准备播放资产

- (void)initialSetupWithURL:(NSURL *)url
{
    NSDictionary *assetOptions = @{ AVURLAssetPreferPreciseDurationAndTimingKey : @YES,
                                    AVURLAssetReferenceRestrictionsKey : @(AVAssetReferenceRestrictionForbidNone)};
    self.urlAsset = [AVURLAsset URLAssetWithURL:url options:assetOptions];
}

准备播放器

- (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:&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:&ItemStatusContext];
        [self.playerItem addObserver:self
                          forKeyPath:@"loadedTimeRanges"
                             options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld
                             context:&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);
    }
}

问题出现在这里-AVKeyValueStatus status = [self.urlAsset statusOfValueForKey:@"tracks" error:&error];-此行带有rtsp://URL返回AVKeyValueStatusFailed

Problems appear here - AVKeyValueStatus status = [self.urlAsset statusOfValueForKey:@"tracks" error:&error]; - this line with rtsp:// URL return AVKeyValueStatusFailed

有错误:

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.)"}

我还寻找问题:

  1. FirstOne -尝试用于此从苹果流媒体示例应用程序- SecondOne -尝试同时使用这两个建议-fileURLWithPath返回不正确的URL,例如:rtsp://192.168.1.168:8556/PSIA/Streaming/channels/2?videoCodecType=H.264 --file:///-所以是猜错了
  2. 根据AppleDev,尝试使用diff方法创建AvPlayer:例如[AVPlayer playerWithPlayerItem:playerItem][AVPlayer playerWithURL:url]-没什么变化.还要尝试为AVAsset设置其他设置-在initialSetupWithURL中(请参见上面的方法实现).
  1. FirstOne - try to use for this stream sample app from Apple - StitchedStreamPlayer - but get few different error for streams that i want to play there
  2. SecondOne - try to use both suggestion - fileURLWithPath return incorrect URL like : rtsp://192.168.1.168:8556/PSIA/Streaming/channels/2?videoCodecType=H.264 --file:/// - so it's i guess incorrect
  3. According to AppleDev try to create AvPlayer with diff approaches: like [AVPlayer playerWithPlayerItem:playerItem] and like [AVPlayer playerWithURL:url] - nothing changed. Also try to setup different setting for AVAsset - in initialSetupWithURL (see method implementation above).

那么,问题是AVPlayer支持播放rtsp://流吗? 如果是,有人可以提供正确用法的样本吗?我在代码中做错了什么? 如果AvPlayer不支持rtsp://,也许存在一些替代解决方案?

So, question 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?

推荐答案

我找不到如何通过AVURLAsset进行rtsp流的方法,但是可以找到一个很好的起点

I don't find a way how to do rtsp streaming via AVURLAsset but good start point can be found here . Maybe it will be useful for someOne

这篇关于带有AVPlayer的rtsp://liveStream的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆