1、iOS直播技术的流程

直播技术的流程大致可以分为几个步骤:数据采集、图像处理(实时滤镜)、视频编码、封包、上传、云端(转码、录制、分发)、直播播放器。

  • 数据采集:通过摄像头和麦克风获得实时的音视频数据;
  • 图像处理:将数据采集的输入流进行实时滤镜,得到我们美化之后的视频帧;
  • 视频编码:编码分为软编码和硬编码。现在一般的编码方式都是H.264,比较新的H.265据说压缩率比较高,但算法也相当要复杂一些,使用还不够广泛。软编码是利用CPU进行编码,硬编码就是使用GPU进行编码,软编码支持现在所有的系统版本,由于苹果在iOS8才开放硬编码的API,故硬编码只支持iOS8以上的系统;
  • 封包:现在直播推流中,一般采用的格式是FLV;
  • 上传:常用的协议是利用RTMP协议进行推流;
  • 云端:进行流的转码、分发和录制;
  • 直播播放器:负责拉流、解码、播放。

用一张腾讯云的图来说明上面的流程:

直播技术流程

2、获取系统的授权

直播的第一步就是采集数据,包含视频和音频数据,由于iOS权限的要求,需要先获取访问摄像头和麦克风的权限:

请求获取访问摄像头权限

__weak typeof(self) _self = self;AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];switch (status) {case AVAuthorizationStatusNotDetermined:{// 许可对话没有出现,发起授权许可[AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {if (granted) {dispatch_async(dispatch_get_main_queue(), ^{[_self.session setRunning:YES];});}}];break;}case AVAuthorizationStatusAuthorized:{// 已经开启授权,可继续[_self.session setRunning:YES];break;}case AVAuthorizationStatusDenied:case AVAuthorizationStatusRestricted:// 用户明确地拒绝授权,或者相机设备无法访问break;default:break;}

请求获取访问麦克风权限

AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio];switch (status) {case AVAuthorizationStatusNotDetermined:{[AVCaptureDevice requestAccessForMediaType:AVMediaTypeAudio completionHandler:^(BOOL granted) {}];break;}case AVAuthorizationStatusAuthorized:{break;}case AVAuthorizationStatusDenied:case AVAuthorizationStatusRestricted:break;default:break;}

3、配置采样参数

音频:需要配置码率、采样率;
视频:需要配置视频分辨率、视频的帧率、视频的码率。

4、音视频的录制

音频的录制

 self.taskQueue = dispatch_queue_create("com.1905.live.audioCapture.Queue", NULL);AVAudioSession *session = [AVAudioSession sharedInstance];[session setActive:YES withOptions:kAudioSessionSetActiveFlag_NotifyOthersOnDeactivation error:nil];[[NSNotificationCenter defaultCenter] addObserver: selfselector: @selector(handleRouteChange:)name: AVAudioSessionRouteChangeNotificationobject: session];[[NSNotificationCenter defaultCenter] addObserver: selfselector: @selector(handleInterruption:)name: AVAudioSessionInterruptionNotificationobject: session];NSError *error = nil;[session setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker | AVAudioSessionCategoryOptionMixWithOthers error:nil];[session setMode:AVAudioSessionModeVideoRecording error:&error];if (![session setActive:YES error:&error]) {[self handleAudioComponentCreationFailure];}AudioComponentDescription acd;acd.componentType = kAudioUnitType_Output;acd.componentSubType = kAudioUnitSubType_RemoteIO;acd.componentManufacturer = kAudioUnitManufacturer_Apple;acd.componentFlags = 0;acd.componentFlagsMask = 0;self.component = AudioComponentFindNext(NULL, &acd);OSStatus status = noErr;status = AudioComponentInstanceNew(self.component, &_componetInstance);if (noErr != status) {[self handleAudioComponentCreationFailure];}UInt32 flagOne = 1;AudioUnitSetProperty(self.componetInstance, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Input, 1, &flagOne, sizeof(flagOne));AudioStreamBasicDescription desc = {0};desc.mSampleRate = _configuration.audioSampleRate;desc.mFormatID = kAudioFormatLinearPCM;desc.mFormatFlags = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagsNativeEndian | kAudioFormatFlagIsPacked;desc.mChannelsPerFrame = (UInt32)_configuration.numberOfChannels;desc.mFramesPerPacket = 1;desc.mBitsPerChannel = 16;desc.mBytesPerFrame = desc.mBitsPerChannel / 8 * desc.mChannelsPerFrame;desc.mBytesPerPacket = desc.mBytesPerFrame * desc.mFramesPerPacket;AURenderCallbackStruct cb;cb.inputProcRefCon = (__bridge void *)(self);cb.inputProc = handleInputBuffer;status = AudioUnitSetProperty(self.componetInstance, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, 1, &desc, sizeof(desc));status = AudioUnitSetProperty(self.componetInstance, kAudioOutputUnitProperty_SetInputCallback, kAudioUnitScope_Global, 1, &cb, sizeof(cb));status = AudioUnitInitialize(self.componetInstance);if (noErr != status) {[self handleAudioComponentCreationFailure];}[session setPreferredSampleRate:_configuration.audioSampleRate error:nil];[session setActive:YES error:nil];

视频的录制:调用GPUImage中的GPUImageVideoCamera

_videoCamera = [[GPUImageVideoCamera alloc] initWithSessionPreset:_configuration.avSessionPreset cameraPosition:AVCaptureDevicePositionFront];
_videoCamera.outputImageOrientation = _configuration.orientation;
_videoCamera.horizontallyMirrorFrontFacingCamera = NO;
_videoCamera.horizontallyMirrorRearFacingCamera = NO;
_videoCamera.frameRate = (int32_t)_configuration.videoFrameRate;_gpuImageView = [[GPUImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];
[_gpuImageView setFillMode:kGPUImageFillModePreserveAspectRatioAndFill];
[_gpuImageView setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];[_gpuImageView setInputRotation:kGPUImageFlipHorizonal atIndex:0];

转载请注明原地址,Clang的博客:https://chenhu1001.github.io 谢谢!

iOS直播技术分享-音视频采集(一)相关推荐

  1. 技术分享| 音视频与微信小程序互通实践

    随着网络架构的变迁.媒体技术发展.音视频场景迭代,基于流媒体的技术也是推陈出新.WebRTC渐渐的成为了音视频互动场景的主流,而微信在6.5.21版本通过小程序开放了实时音视频能力,开发者们可以使用组 ...

  2. iOS直播(二)GPUImage音视频采集

    上文中介绍了用AVFoundation实现音视频采集(https://blog.csdn.net/dolacmeng/article/details/81268622) ,开源的基于GPU的第三方图像 ...

  3. “小程序+直播”怎样搅动音视频技术生态?

    ​ 责编 / 王宇豪 策划 / LiveVideoStack 12月26日晚间,微信小程序开放了直播能力,并首先向社交.教育.医疗.政务民生.金融等五大应用场景开放.与原生App应用和基于浏览器的H5 ...

  4. 直播软件搭建音视频开发中的视频采集

    直播软件搭建音视频开发中的视频采集 前言 在直播和短视频行业日益火热的发展形势下,音视频开发(采集.编解码.传输.播放.美颜)等技术也随之成为开发者们关注的重点,本系列文章就音视频开发过程中所运用到的 ...

  5. iOS直播技术分析与实现

    不经意间发现,两个月没写博客了 , 把最近的一些技术成果,总结成了文章,与大家分享. 视频直播技术要点分析 HTTP Live Streaming(HLS)是苹果公司(Apple Inc.)实现的基于 ...

  6. iOS 直播技术及Demo

    要过年了,新年快乐,今天写一些关于iOS直播技术相关知识,及详细Demo介绍,首先请下载Demo Demo下载地址(点击跳转下载) 一.直播介绍 1.1.直播现状 近年来,直播越来越火,但直播技术却对 ...

  7. HTTP Live Streaming直播源代码软件开发(iOS直播)技术分析与实现

    HLS技术要点分析 HTTP Live Streaming(HLS)是苹果公司(Apple Inc.)实现的基于HTTP的流媒体传输协议,可实现流媒体的直播和点播,主要应用在iOS系统,为iOS设备( ...

  8. iOS直播技术学习笔记 直播总体概览(一)

    ####概述 直播的现状 2016年,是一个直播年.直播行业快速发展,同时也滋生了大大小小上千家相关的公司. 中国互联网络信息中心发布的报告显示,截至今年6月,我国网络直播用户规模达到3.25亿,占网 ...

  9. iOS 直播技术文档

    iOS 直播 个人项目可以参考+lflivekit+ljkplayer 第三方推荐使用金山云 im推荐使用容云 网络层(socket或st)负责传输,协议层(rtmp或hls)负责网络打包,封装层(f ...

最新文章

  1. 安装es怎么在后台运行_ES备份索引数据到阿里云OSS
  2. 【bzoj3033】太鼓达人 DFS欧拉图
  3. 前端学习(3184):ant-design的button介绍按钮属性
  4. 经济专业为什么学python_既然有了会计学专业,为什么还要有税收学专业?
  5. 威马董事长沈晖隔空喊话王兴:威马一定会是Top3之一
  6. linux php7 替换,linux-shell-命令替换和变量替换
  7. python工程师简历项目经验怎么写_班长项目经验简历范文
  8. sdut 1299 最长上升子序列
  9. 环比和同比的定义和应用
  10. 安全渗透测试工具--Burpsuite的爬虫功能
  11. 邻接表(Adjacency List)
  12. arcgis注记详解
  13. 惠普HP Officejet K7103 打印机驱动
  14. 【数学基础】简单易懂的张量求导和计算图讲解
  15. CSS中的十二种结构伪类选择器
  16. 计算机房精密空调术语,精密空调8大制冷形式,40个专业术语
  17. 高通linux平台(mdm9x07,sdx12)连接qact
  18. osgEarth的Rex引擎原理分析(四十三)osgEarth的Geographic、Geodetic、Geocentric和Project的关系
  19. 用POP3获取邮箱邮件内容,支持SSL验证登陆(完整C#源码)
  20. 林大OJ习题 2020年1月7日

热门文章

  1. 【python 爬虫】全国失信被执行人名单爬虫
  2. P1279 字串距离【动规】
  3. 2021中山濠头中学高考成绩查询,2021中山市普通高中排名一览表
  4. OpenGL使用精灵图集
  5. pytorh实现全局平均(最大)池化的两种方式
  6. Ubuntu自用配置(Ubuntu 22.04LTS + 拯救者R9000P 2021)
  7. 计算机网络id显示灰色的,教你还原win10系统网络id按钮是灰色点击不了的步骤
  8. MySQL数据库入门-新手常见问题答疑
  9. 什么是openstack的 metadata
  10. C++菱形继承与虚继承