音视频推流与拉流及播放视频实现

视频推流拉流概念,我认为这个哥们的图挺不错

音视频推流与拉流及播放视频实现_第1张图片

推流

推流,指的是把采集阶段封包好的内容传输到服务器的过程。

主流的推送协议和优缺点

  • RTMP
    RTMP是Real Time Messaging Protocol(实时消息传输协议)的缩写,是Adobe公司为Flash/AIR平台和服务器之间音、视频及数据传输开发的实时消息传送协议。RTMP协议基于TCP,包括RTMP基本协议及RTMPT/RTMPS/RTMPE等多种变种。
    RTMP协议中,视频必须是H264编码,音频必须是AAC或MP3编码,且多以flv格式封包。RTMP是目前最主流的流媒体传输协议,对CDN支持良好,实现难度较低,是大多数的直播平台的选择。
    不过RTMP有着一个最大的不足——不支持浏览器,且Adobe已不再更新。因此直播服务要支持浏览器的话,需要另外的推送协议支持。

  • HLS
    Http Live Streaming是由Apple公司定义的基于HTTP的流媒体实时传输协议。它的原理是将整个流分为多个小的文件来下载,每次只下载若干个。服务器端会将最新的直播数据生成新的小文件,客户端只要不停的按顺序播放从服务器获取到的文件,就实现了直播。基本上,HLS是以点播的技术实现了直播的体验。因为每个小文件的时长很短,客户端可以很快地切换码率,以适应不同带宽条件下的播放。
    分段推送的技术特点,决定了HLS的延迟一般会高于普通的流媒体直播协议。
    传输内容包括两部分:一是M3U8描述文件,二是TS媒体文件。TS媒体文件中的视频必须是H264编码,音频必须是AAC或MP3编码。
    由于数据通过HTTP协议传输,所以完全不用考虑防火墙或者代理的问题,而且分段文件的时长很短。

  • WebRTC
    WebRTC(Web Real-Time Communication),即“源自网页即时通信”。WebRTC是一个支持浏览器进行实时语音、视频对话的开源协议。WebRTC的支持者甚多,Google、Mozilla、Opera推动其成为W3C推荐标准。
    WebRTC支持目前的主流浏览器,并且基于SRTP和UDP,即便在网络信号一般的情况下也具备较好的稳定性。
    此外,WebRTC可以实现点对点通信,通信双方延时低,此外,WebRTC可以实现点对点通信,通信双方延时低,是实现“连麦”功能比较好的选择。

拉流

拉流是指服务器已有直播内容,用指定地址进行拉取的过程

音视频推流与拉流及播放视频实现_第2张图片

简单来讲,推流是直播段,拉流是客户端。

本文福利, 免费领取C++音视频学习资料包、技术视频,内容包括(音视频开发,面试题,FFmpeg ,webRTC ,rtmp ,hls ,rtsp ,ffplay ,编解码,推拉流,srs)↓↓↓↓↓↓见下面↓↓文章底部点击免费领取↓↓

从上述中我们知道了视频拉流就是播放从服务器拉取资源进行资源播放。

接下来讲解如何播放视频。大家普遍的都是用的IJKPlayer播放器进行播放。

这里我采用别人编译好的框架进行播放。

这里我采用的是CocoaPods,我使用pod search IJKPlayer,发现有好多人都包装了改框架,因此我就选了一个YWVideoPlayer

集成方法

  • xcode 创建一个新工程
  • cd 到工程目录下
  • pod init
  • vim Podfile
  • 在打开的Podfile文件中添加pod 'YWVideoPlayer', '~> 1.0.3'
  • esc ,回车+q,wq 保存
  • pod install(文件比较大,下载比较慢,耐心等待)
  • open .
  • 打开工程 ,将target->general->deployment target 的目标版本改成9.0

看下工程结构

音视频推流与拉流及播放视频实现_第3张图片

看工程结构,我们知道该作者这里只用了个sdwebimage库。并且IJKMediaFramework是编译好的文件可以直接使用。

这里我们ViewController.h 文件修改如下


#import "ViewController.h"
#import "YWMediaPlayerView.h"
@interface ViewController ()
@property (nonatomic, strong)YWMediaPlayerView  *mediaPlayerView;
@property (nonatomic, strong)UIView             *playerView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    _playerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, YWSCREEN_WIDTH, YWMinPlayerHeight)];
    _playerView.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:_playerView];
    _mediaPlayerView = [[YWMediaPlayerView alloc]init];
    
    // 测试链接 mp4、rtmp、m3u8
    // NSString *testUrl = @"http://flv2.bn.netease.com/videolib3/1604/28/fVobI0704/SD/fVobI0704-mobile.mp4";
    // NSString *testUrl = @"rtmp://live.hkstv.hk.lxdns.com/live/hks";
//    NSString *testUrl = @"http://dlhls.cdn.zhanqi.tv/zqlive/49427_jmACJ.m3u8";
    NSString * testUrl = @"rtmp://localhost:1935/rtmplive/room";
    [_mediaPlayerView playerViewWithUrl:testUrl WithTitle:@"视频的标题" WithView:_playerView  WithDelegate:self];
    
    // 这里如果是直播,则隐藏进度条以及时间等控件
    _mediaPlayerView.mediaControl.totalDurationLabel.hidden = YES;
    _mediaPlayerView.mediaControl.mediaProgressSlider.hidden = YES;
    _mediaPlayerView.mediaControl.currentTimeLabel.hidden = YES;
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        // 自动播放
        [_mediaPlayerView.mediaControl playControl];
    });
}
@end

运行程序,程序是现在的状态

音视频推流与拉流及播放视频实现_第4张图片

接着我们启动推流服务器推流数据

ffmpeg -re -i abc.mp4 -vcodec libx264 -acodec aac -strict -2 -f flv

rtmp://localhost:1935/rtmplive/room

音视频推流与拉流及播放视频实现_第5张图片

我们发现模拟器上在播放视频了。

我们成功播放了视频。这其实是抛砖引玉了。我们还是需要简单的分析下该作者的源码实现。

核心代码在这

- (void)preparePlayer {
    
#ifdef DEBUG
    [IJKFFMoviePlayerController setLogReport:YES];
    [IJKFFMoviePlayerController setLogLevel:k_IJK_LOG_DEBUG];
#else
    [IJKFFMoviePlayerController setLogReport:NO];
    [IJKFFMoviePlayerController setLogLevel:k_IJK_LOG_INFO];
#endif
    
    
    
    [IJKFFMoviePlayerController checkIfFFmpegVersionMatch:YES];
    
    
    IJKFFOptions *options = [IJKFFOptions optionsByDefault];
    
    if (self.player) {
        [self.player shutdown];
        [self.player.view removeFromSuperview];

        self.player=nil;
    }
    
    self.player = [[IJKFFMoviePlayerController alloc] initWithContentURL:self.url withOptions:options];
    self.player.view.frame = self.view.bounds;
    self.player.scalingMode = IJKMPMovieScalingModeAspectFit;
    self.player.shouldAutoplay = YES;
    
    [self.view  insertSubview:self.player.view belowSubview:self.mediaControl];
    
    self.mediaControl.delegatePlayer = self.player;
  
}

编译IJKPlayer

官方提供的编译

git clone https://github.com/Bilibili/ijkplayer.git ijkplayer-ios
cd ijkplayer-ios
git checkout -B latest k0.8.8

./init-ios.sh

cd ios
./compile-ffmpeg.sh clean
./compile-ffmpeg.sh all

# Demo
#     open ios/IJKMediaDemo/IJKMediaDemo.xcodeproj with Xcode
# 
# Import into Your own Application
#     Select your project in Xcode.
#     File -> Add Files to ... -> Select ios/IJKMediaPlayer/IJKMediaPlayer.xcodeproj
#     Select your Application's target.
#     Build Phases -> Target Dependencies -> Select IJKMediaFramework
#     Build Phases -> Link Binary with Libraries -> Add:
#         IJKMediaFramework.framework
#
#         AudioToolbox.framework
#         AVFoundation.framework
#         CoreGraphics.framework
#         CoreMedia.framework
#         CoreVideo.framework
#         libbz2.tbd
#         libz.tbd
#         MediaPlayer.framework
#         MobileCoreServices.framework
#         OpenGLES.framework
#         QuartzCore.framework
#         UIKit.framework
#         VideoToolbox.framework
#
#         ... (Maybe something else, if you get any link error)
# 

本文福利, 免费领取C++音视频学习资料包、技术视频,内容包括(音视频开发,面试题,FFmpeg ,webRTC ,rtmp ,hls ,rtsp ,ffplay ,编解码,推拉流,srs)↓↓↓↓↓↓见下面↓↓文章底部点击免费领取↓↓ 

# 

一看就是采用终端的方式进行

  • 1.打开终端
  • 2.cd ~/Desktop/
  • 3.mkdir ijk
  • 4.cd ijk/
  • 5.git clone https://github.com/Bilibili/ijkplayer.git ijkplayer-ios
  • 6.cd ijkplayer-ios
    +7.git checkout -B latest k0.8.8
    +8. ./init-ios.sh (要是下载速度几kb,那么ctrl+c 取消重新运行试试看)
    +9. cd ios
    +10. ./compile-ffmpeg.sh clean
    +11. ./compile-ffmpeg.sh all

第十一步骤出现下列错误 

解决如下

打开compile-ffmpeg.sh 文件,将FF_ALL_ARCHS_IOS8_SDK 改成arm64 i386 x86_64
即FF_ALL_ARCHS_IOS8_SDK="arm64 i386 x86_64"

如何将源文件导入到工程中引用编译不报错

我用的xcode版本是10.0

  • 1.创建一个新的工程

  • 2.File -> Add Files to ... -> Select ios/IJKMediaPlayer/IJKMediaPlayer.xcodeproj

音视频推流与拉流及播放视频实现_第6张图片

  • 3.选择工程的 target.

  • 4.选择Build Phases -> Target Dependencies -> Select IJKMediaFramework

  • 音视频推流与拉流及播放视频实现_第7张图片

5.选择 Build Phases -> Link Binary with Libraries -> Add:

音视频推流与拉流及播放视频实现_第8张图片

  • 6.将ViewController.m文件改成ViewController.mm 文件

  • 7修改viewControlloer.m文件如下

#import "ViewController.h"
#import 

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    id playback = [[IJKFFMoviePlayerController alloc] initWithContentURL:nil  withOptions:nil];
    
    [playback shutdown];
    // Do any additional setup after loading the view, typically from a nib.
}


@end
  • 8.command+R 编译不出错,说明编译成功。

使用IJKPlayer进行播放视频

将ViewController修改成如下代码



#import "ViewController.h"
#import 

@interface ViewController ()

@property (atomic, strong) NSURL *url;
@property (atomic, retain) id  player;
@property (weak, nonatomic) UIView *PlayerView;

- (IBAction)play_btn:(id)sender;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    //网络视频
//        self.url = [NSURL URLWithString:@"https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4"];
    //    _player = [[IJKAVMoviePlayerController alloc] initWithContentURL:self.url];
    
    //直播视频
//    self.url = [NSURL URLWithString:@"http://live.hkstv.hk.lxdns.com/live/hks/playlist.m3u8"];
    self.url = [NSURL URLWithString:@"rtmp://localhost:1935/rtmplive/room"];
    
    _player = [[IJKFFMoviePlayerController alloc] initWithContentURL:self.url withOptions:nil];
    
    UIView *playerView = [self.player view];
    
    UIView *displayView = [[UIView alloc] initWithFrame:CGRectMake(0, 50, self.view.bounds.size.width, 180)];
    self.PlayerView = displayView;
    self.PlayerView.backgroundColor = [UIColor blackColor];
    [self.view addSubview:self.PlayerView];
    
    playerView.frame = self.PlayerView.bounds;
    playerView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    
    [self.PlayerView insertSubview:playerView atIndex:1];
    [_player setScalingMode:IJKMPMovieScalingModeAspectFill];
    [self installMovieNotificationObservers];
    
}

-(void)viewWillAppear:(BOOL)animated{
    if (![self.player isPlaying]) {
        [self.player prepareToPlay];
    }
}

#pragma Selector func

- (void)loadStateDidChange:(NSNotification*)notification {
    IJKMPMovieLoadState loadState = _player.loadState;
    
    if ((loadState & IJKMPMovieLoadStatePlaythroughOK) != 0) {
        NSLog(@"LoadStateDidChange: IJKMovieLoadStatePlayThroughOK: %d\n",(int)loadState);
    }else if ((loadState & IJKMPMovieLoadStateStalled) != 0) {
        NSLog(@"loadStateDidChange: IJKMPMovieLoadStateStalled: %d\n", (int)loadState);
    } else {
        NSLog(@"loadStateDidChange: ???: %d\n", (int)loadState);
    }
}

- (void)moviePlayBackFinish:(NSNotification*)notification {
    int reason =[[[notification userInfo] valueForKey:IJKMPMoviePlayerPlaybackDidFinishReasonUserInfoKey] intValue];
    switch (reason) {
        case IJKMPMovieFinishReasonPlaybackEnded:
            NSLog(@"playbackStateDidChange: IJKMPMovieFinishReasonPlaybackEnded: %d\n", reason);
            break;
            
        case IJKMPMovieFinishReasonUserExited:
            NSLog(@"playbackStateDidChange: IJKMPMovieFinishReasonUserExited: %d\n", reason);
            break;
            
        case IJKMPMovieFinishReasonPlaybackError:
            NSLog(@"playbackStateDidChange: IJKMPMovieFinishReasonPlaybackError: %d\n", reason);
            break;
            
        default:
            NSLog(@"playbackPlayBackDidFinish: ???: %d\n", reason);
            break;
    }
}

- (void)mediaIsPreparedToPlayDidChange:(NSNotification*)notification {
    NSLog(@"mediaIsPrepareToPlayDidChange\n");
}

- (void)moviePlayBackStateDidChange:(NSNotification*)notification {
    switch (_player.playbackState) {
        case IJKMPMoviePlaybackStateStopped:
            NSLog(@"IJKMPMoviePlayBackStateDidChange %d: stoped", (int)_player.playbackState);
            break;
            
        case IJKMPMoviePlaybackStatePlaying:
            NSLog(@"IJKMPMoviePlayBackStateDidChange %d: playing", (int)_player.playbackState);
            break;
            
        case IJKMPMoviePlaybackStatePaused:
            NSLog(@"IJKMPMoviePlayBackStateDidChange %d: paused", (int)_player.playbackState);
            break;
            
        case IJKMPMoviePlaybackStateInterrupted:
            NSLog(@"IJKMPMoviePlayBackStateDidChange %d: interrupted", (int)_player.playbackState);
            break;
            
        case IJKMPMoviePlaybackStateSeekingForward:
        case IJKMPMoviePlaybackStateSeekingBackward: {
            NSLog(@"IJKMPMoviePlayBackStateDidChange %d: seeking", (int)_player.playbackState);
            break;
        }
            
        default: {
            NSLog(@"IJKMPMoviePlayBackStateDidChange %d: unknown", (int)_player.playbackState);
            break;
        }
    }
}

#pragma Install Notifiacation

- (void)installMovieNotificationObservers {
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(loadStateDidChange:)
                                                 name:IJKMPMoviePlayerLoadStateDidChangeNotification
                                               object:_player];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(moviePlayBackFinish:)
                                                 name:IJKMPMoviePlayerPlaybackDidFinishNotification
                                               object:_player];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(mediaIsPreparedToPlayDidChange:)
                                                 name:IJKMPMediaPlaybackIsPreparedToPlayDidChangeNotification
                                               object:_player];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(moviePlayBackStateDidChange:)
                                                 name:IJKMPMoviePlayerPlaybackStateDidChangeNotification
                                               object:_player];
    
}

- (void)removeMovieNotificationObservers {
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:IJKMPMoviePlayerLoadStateDidChangeNotification
                                                  object:_player];
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:IJKMPMoviePlayerPlaybackDidFinishNotification
                                                  object:_player];
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:IJKMPMediaPlaybackIsPreparedToPlayDidChangeNotification
                                                  object:_player];
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:IJKMPMoviePlayerPlaybackStateDidChangeNotification
                                                  object:_player];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)play_btn:(id)sender {
    if (![self.player isPlaying]) {
        [self.player play];
    }else{
        [self.player pause];
    }
}

@end

结果是

 音视频推流与拉流及播放视频实现_第9张图片

本文福利, 免费领取C++音视频学习资料包、技术视频,内容包括(音视频开发,面试题,FFmpeg ,webRTC ,rtmp ,hls ,rtsp ,ffplay ,编解码,推拉流,srs)↓↓↓↓↓↓见下面↓↓文章底部点击免费领取↓↓ 

你可能感兴趣的:(音视频开发进阶,音视频,视频编解码,实时音视频,webrtc,实时互动)