【如何快速的开发一个完整的iOS直播app】(采集篇)

前言

在看这篇之前,如果您还不了解直播原理,请查看这篇文章如何快速的开发一个完整的iOS直播app(原理篇)

开发一款直播app,首先需要采集主播的视频和音频,然后传入流媒体服务器,本篇主要讲解如何采集主播的视频和音频,当前可以切换前置后置摄像头和焦点光标,但是美颜功能还没做,可以看见素颜的你,后续还会有直播的其他功能文章陆续发布。

如果喜欢我的文章,可以关注我微博:袁峥Seemygo,也可以来小码哥,了解下我们的iOS培训课程。后续还会更新更多内容,有任何问题,欢迎简书留言袁峥Seemygo。。。

效果

为了采集效果图,我也是豁出去了,请忽略人物,关注技术。

【如何快速的开发一个完整的iOS直播app】(采集篇)
忽略本人.png

基本知识介绍

  • AVFoundation: 音视频数据采集需要用AVFoundation框架.

  • AVCaptureDevice:硬件设备,包括麦克风、摄像头,通过该对象可以设置物理设备的一些属性(例如相机聚焦、白平衡等)

  • AVCaptureDeviceInput:硬件输入对象,可以根据AVCaptureDevice创建对应的AVCaptureDeviceInput对象,用于管理硬件输入数据。
  • AVCaptureOutput:硬件输出对象,用于接收各类输出数据,通常使用对应的子类AVCaptureAudioDataOutput(声音数据输出对象)、AVCaptureVideoDataOutput(视频数据输出对象)
  • AVCaptionConnection:当把一个输入和输出添加到AVCaptureSession之后,AVCaptureSession就会在输入、输出设备之间建立连接,而且通过AVCaptureOutput可以获取这个连接对象。
  • AVCaptureVideoPreviewLayer:相机拍摄预览图层,能实时查看拍照或视频录制效果,创建该对象需要指定对应的AVCaptureSession对象,因为AVCaptureSession包含视频输入数据,有视频数据才能展示。
  • AVCaptureSession: 协调输入与输出之间传输数据
    • 系统作用:可以操作硬件设备
    • 工作原理:让App与系统之间产生一个捕获会话,相当于App与硬件设备有联系了, 我们只需要把硬件输入对象和输出对象添加到会话中,会话就会自动把硬件输入对象和输出产生连接,这样硬件输入与输出设备就能传输音视频数据。
    • 现实生活场景:租客(输入钱),中介(会话),房东(输出房),租客和房东都在中介登记,中介就会让租客与房东之间产生联系,以后租客就能直接和房东联系了。

捕获音视频步骤:官方文档

  • 1.创建AVCaptureSession对象
  • 2.获取AVCaptureDevicel录像设备(摄像头),录音设备(麦克风),注意不具备输入数据功能,只是用来调节硬件设备的配置。
  • 3.根据音频/视频硬件设备(AVCaptureDevice)创建音频/视频硬件输入数据对象(AVCaptureDeviceInput),专门管理数据输入。
  • 4.创建视频输出数据管理对象(AVCaptureVideoDataOutput),并且设置样品缓存代理(setSampleBufferDelegate)就可以通过它拿到采集到的视频数据
  • 5.创建音频输出数据管理对象(AVCaptureAudioDataOutput),并且设置样品缓存代理(setSampleBufferDelegate)就可以通过它拿到采集到的音频数据
  • 6.将数据输入对象AVCaptureDeviceInput、数据输出对象AVCaptureOutput添加到媒体会话管理对象AVCaptureSession中,就会自动让音频输入与输出和视频输入与输出产生连接.
  • 7.创建视频预览图层AVCaptureVideoPreviewLayer并指定媒体会话,添加图层到显示容器layer中
  • 8.启动AVCaptureSession,只有开启,才会开始输入到输出数据流传输。
 1 // 捕获音视频
 2 - (void)setupCaputureVideo
 3 {
 4     // 1.创建捕获会话,必须要强引用,否则会被释放
 5     AVCaptureSession *captureSession = [[AVCaptureSession alloc] init];
 6     _captureSession = captureSession;
 7 
 8     // 2.获取摄像头设备,默认是后置摄像头
 9     AVCaptureDevice *videoDevice = [self getVideoDevice:AVCaptureDevicePositionFront];
10 
11     // 3.获取声音设备
12     AVCaptureDevice *audioDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];
13 
14     // 4.创建对应视频设备输入对象
15     AVCaptureDeviceInput *videoDeviceInput = [AVCaptureDeviceInput deviceInputWithDevice:videoDevice error:nil];
16     _currentVideoDeviceInput = videoDeviceInput;
17 
18     // 5.创建对应音频设备输入对象
19     AVCaptureDeviceInput *audioDeviceInput = [AVCaptureDeviceInput deviceInputWithDevice:audioDevice error:nil];
20 
21     // 6.添加到会话中
22     // 注意“最好要判断是否能添加输入,会话不能添加空的
23     // 6.1 添加视频
24     if ([captureSession canAddInput:videoDeviceInput]) {
25         [captureSession addInput:videoDeviceInput];
26     }
27     // 6.2 添加音频
28     if ([captureSession canAddInput:audioDeviceInput]) {
29         [captureSession addInput:audioDeviceInput];
30     }
31 
32     // 7.获取视频数据输出设备
33     AVCaptureVideoDataOutput *videoOutput = [[AVCaptureVideoDataOutput alloc] init];
34     // 7.1 设置代理,捕获视频样品数据
35     // 注意:队列必须是串行队列,才能获取到数据,而且不能为空
36     dispatch_queue_t videoQueue = dispatch_queue_create("Video Capture Queue", DISPATCH_QUEUE_SERIAL);
37     [videoOutput setSampleBufferDelegate:self queue:videoQueue];
38     if ([captureSession canAddOutput:videoOutput]) {
39         [captureSession addOutput:videoOutput];
40     }
41 
42     // 8.获取音频数据输出设备
43     AVCaptureAudioDataOutput *audioOutput = [[AVCaptureAudioDataOutput alloc] init];
44     // 8.2 设置代理,捕获视频样品数据
45     // 注意:队列必须是串行队列,才能获取到数据,而且不能为空
46     dispatch_queue_t audioQueue = dispatch_queue_create("Audio Capture Queue", DISPATCH_QUEUE_SERIAL);
47     [audioOutput setSampleBufferDelegate:self queue:audioQueue];
48     if ([captureSession canAddOutput:audioOutput]) {
49         [captureSession addOutput:audioOutput];
50     }
51 
52     // 9.获取视频输入与输出连接,用于分辨音视频数据
53     _videoConnection = [videoOutput connectionWithMediaType:AVMediaTypeVideo];
54 
55     // 10.添加视频预览图层
56     AVCaptureVideoPreviewLayer *previedLayer = [AVCaptureVideoPreviewLayer layerWithSession:captureSession];
57     previedLayer.frame = [UIScreen mainScreen].bounds;
58     [self.view.layer insertSublayer:previedLayer atIndex:0];
59     _previedLayer = previedLayer;
60 
61     // 11.启动会话
62     [captureSession startRunning];
63 }
64 
65 // 指定摄像头方向获取摄像头
66 - (AVCaptureDevice *)getVideoDevice:(AVCaptureDevicePosition)position
67 {
68     NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
69     for (AVCaptureDevice *device in devices) {
70         if (device.position == position) {
71             return device;
72         }
73     }
74     return nil;
75 }
76 
77 #pragma mark - AVCaptureVideoDataOutputSampleBufferDelegate
78 // 获取输入设备数据,有可能是音频有可能是视频
79 - (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection
80 {
81     if (_videoConnection == connection) {
82         NSLog(@"采集到视频数据");
83     } else {
84         NSLog(@"采集到音频数据");
85     }
86 }

视频采集额外功能一(切换摄像头)

  • 切换摄像头步骤
    • 1.获取当前视频设备输入对象
    • 2.判断当前视频设备是前置还是后置
    • 3.确定切换摄像头的方向
    • 4.根据摄像头方向获取对应的摄像头设备
    • 5.创建对应的摄像头输入对象
    • 6.从会话中移除之前的视频输入对象
    • 7.添加新的视频输入对象到会话中
 1 // 切换摄像头
 2 - (IBAction)toggleCapture:(id)sender {
 3 
 4     // 获取当前设备方向
 5     AVCaptureDevicePosition curPosition = _currentVideoDeviceInput.device.position;
 6 
 7     // 获取需要改变的方向
 8     AVCaptureDevicePosition togglePosition = curPosition == AVCaptureDevicePositionFront?AVCaptureDevicePositionBack:AVCaptureDevicePositionFront;
 9 
10     // 获取改变的摄像头设备
11     AVCaptureDevice *toggleDevice = [self getVideoDevice:togglePosition];
12 
13     // 获取改变的摄像头输入设备
14     AVCaptureDeviceInput *toggleDeviceInput = [AVCaptureDeviceInput deviceInputWithDevice:toggleDevice error:nil];
15 
16     // 移除之前摄像头输入设备
17     [_captureSession removeInput:_currentVideoDeviceInput];
18 
19     // 添加新的摄像头输入设备
20     [_captureSession addInput:toggleDeviceInput];
21 
22     // 记录当前摄像头输入设备
23     _currentVideoDeviceInput = toggleDeviceInput;
24 
25 }

视频采集额外功能二(聚焦光标)

  • 聚焦光标步骤
    • 1.监听屏幕的点击
    • 2.获取点击的点位置,转换为摄像头上的点,必须通过视频预览图层(AVCaptureVideoPreviewLayer)转
    • 3.设置聚焦光标图片的位置,并做动画
    • 4.设置摄像头设备聚焦模式和曝光模式(注意:这里设置一定要锁定配置lockForConfiguration,否则报错)
 1 // 点击屏幕,出现聚焦视图
 2 - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
 3 {
 4     // 获取点击位置
 5     UITouch *touch = [touches anyObject];
 6     CGPoint point = [touch locationInView:self.view];
 7 
 8     // 把当前位置转换为摄像头点上的位置
 9     CGPoint cameraPoint = [_previedLayer captureDevicePointOfInterestForPoint:point];
10 
11     // 设置聚焦点光标位置
12     [self setFocusCursorWithPoint:point];
13 
14     // 设置聚焦
15     [self focusWithMode:AVCaptureFocusModeAutoFocus exposureMode:AVCaptureExposureModeAutoExpose atPoint:cameraPoint];
16 }
17 
18 /**
19  *  设置聚焦光标位置
20  *
21  *  @param point 光标位置
22  */
23 -(void)setFocusCursorWithPoint:(CGPoint)point{
24     self.focusCursorImageView.center=point;
25     self.focusCursorImageView.transform=CGAffineTransformMakeScale(1.5, 1.5);
26     self.focusCursorImageView.alpha=1.0;
27     [UIView animateWithDuration:1.0 animations:^{
28         self.focusCursorImageView.transform=CGAffineTransformIdentity;
29     } completion:^(BOOL finished) {
30         self.focusCursorImageView.alpha=0;
31 
32     }];
33 }
34 
35 /**
36  *  设置聚焦
37  */
38 -(void)focusWithMode:(AVCaptureFocusMode)focusMode exposureMode:(AVCaptureExposureMode)exposureMode atPoint:(CGPoint)point{
39 
40     AVCaptureDevice *captureDevice = _currentVideoDeviceInput.device;
41     // 锁定配置
42     [captureDevice lockForConfiguration:nil];
43 
44     // 设置聚焦
45     if ([captureDevice isFocusModeSupported:AVCaptureFocusModeAutoFocus]) {
46         [captureDevice setFocusMode:AVCaptureFocusModeAutoFocus];
47     }
48     if ([captureDevice isFocusPointOfInterestSupported]) {
49         [captureDevice setFocusPointOfInterest:point];
50     }
51 
52     // 设置曝光
53     if ([captureDevice isExposureModeSupported:AVCaptureExposureModeAutoExpose]) {
54         [captureDevice setExposureMode:AVCaptureExposureModeAutoExpose];
55     }
56     if ([captureDevice isExposurePointOfInterestSupported]) {
57         [captureDevice setExposurePointOfInterest:point];
58     }
59 
60     // 解锁配置
61     [captureDevice unlockForConfiguration];
62 }
文/袁峥Seemygo(简书作者)
原文链接:http://www.jianshu.com/p/c71bfda055fa
著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。