当音视频采集和预处理(即美颜、滤镜这些)开发者已经全部实现,只需要使用 SDK 来编码和推流,那么可以通过 TXLiteAVSDK 提供的自定义采集数据接口来满足该场景。
简单示例部分代码如下(以AVCaptureSession采集为例):
#pragma mark - AVCaptureVideoDataOutputSampleBufferDelegate、AVCaptureAudioDataOutputSampleBufferDelegate
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection {
if (captureOutput == _audioOutput) {
//向SDK填充Audio数据
[_txLivePush sendAudioSampleBuffer:sampleBuffer withType:RPSampleBufferTypeAudioMic];
} else {
//向SDK填充Video数据
[_txLivePush sendVideoSampleBuffer:sampleBuffer];
}
}
//自定义采集参数设置以及启动推流
- (void)startRtmp {
if (_txLivePublisher != nil) {
TXLivePushConfig* config = [[TXLivePushConfig alloc] init];
//【示例代码1】设置自定义视频采集逻辑(自定义视频采集逻辑不要调用startPreview)
_config.customModeType |= CUSTOM_MODE_VIDEO_CAPTURE;
_config.autoSampleBufferSize = YES;
//【示例代码2】设置自定义音频采集逻辑(音频采样位宽必须是16)
//_config.customModeType |= CUSTOM_MODE_AUDIO_CAPTURE;
//_config.audioSampleRate = AUDIO_SAMPLE_RATE_48000;
//开始推流
[_txLivePublisher setConfig:_config];
_txLivePublisher.delegate = self;
[_txLivePublisher startPush:rtmpUrl];
}
}
//YUV数据转CVPixelBuffer(不是必须)
- (void)didReceivedYUV420pPacket:(APYUV420pPacket)packet {
int sYLineSize = packet.yLineSize;
int sULineSize = packet.uLineSize;
int sVLineSize = packet.vLineSize;
int sYSize = sYLineSize * packet.height;
int sUSize = sULineSize * packet.height/2;
int sVSize = sVLineSize * packet.height/2;
int dWidth = packet.width;
int dHeight = packet.height;
CVPixelBufferRef pxbuffer;
CVReturn rc;
rc = CVPixelBufferCreate(NULL, dWidth, dHeight, kCVPixelFormatType_420YpCbCr8PlanarFullRange, NULL, &pxbuffer);
if (rc != 0) {
NSLog(@"CVPixelBufferCreate failed %d", rc);
if (pxbuffer) { CFRelease(pxbuffer); }
return;
}
rc = CVPixelBufferLockBaseAddress(pxbuffer, 0);
if (rc != 0) {
NSLog(@"CVPixelBufferLockBaseAddress falied %d", rc);
if (pxbuffer) { CFRelease(pxbuffer); }
return;
} else {
uint8_t *y_copyBaseAddress = (uint8_t*)CVPixelBufferGetBaseAddressOfPlane(pxbuffer, 0);
uint8_t *u_copyBaseAddress= (uint8_t*)CVPixelBufferGetBaseAddressOfPlane(pxbuffer, 1);
uint8_t *v_copyBaseAddress= (uint8_t*)CVPixelBufferGetBaseAddressOfPlane(pxbuffer, 2);
int dYLineSize = (int)CVPixelBufferGetBytesPerRowOfPlane(pxbuffer, 0);
int dULineSize = (int)CVPixelBufferGetBytesPerRowOfPlane(pxbuffer, 1);
int dVLineSize = (int)CVPixelBufferGetBytesPerRowOfPlane(pxbuffer, 2);
memcpy(y_copyBaseAddress, packet.dataBuffer, sYSize);
memcpy(u_copyBaseAddress, packet.dataBuffer + sYSize, sUSize);
memcpy(v_copyBaseAddress, packet.dataBuffer + sYSize + sUSize, sVSize);
rc = CVPixelBufferUnlockBaseAddress(pxbuffer, 0);
if (rc != 0) {
NSLog(@"CVPixelBufferUnlockBaseAddress falied %d", rc);
}
}
CMVideoFormatDescriptionRef videoInfo = NULL;
CMVideoFormatDescriptionCreateForImageBuffer(NULL, pxbuffer, &videoInfo);
CMSampleTimingInfo timing = {kCMTimeInvalid, kCMTimeInvalid, kCMTimeInvalid};
CMSampleBufferRef dstSampleBuffer = NULL;
rc = CMSampleBufferCreateForImageBuffer(kCFAllocatorDefault, pxbuffer, YES, NULL, NULL, videoInfo, &timing, &dstSampleBuffer);
if (rc) {
NSLog(@"CMSampleBufferCreateForImageBuffer error: %d", rc);
} else {
[self.txLivePublisher sendVideoSampleBuffer:dstSampleBuffer];
}
if (pxbuffer) { CFRelease(pxbuffer); }
if (videoInfo) { CFRelease(videoInfo); }
if (dstSampleBuffer) { CFRelease(dstSampleBuffer); }
}
/**
* 发送自定义的SampleBuffer,代替sendCustomVideoData
* 内部有简单的帧率控制,发太快会自动丢帧;超时则会重发最后一帧
* @note 相关属性设置请参考TXLivePushConfig,autoSampleBufferSize优先级高于sampleBufferSize
* @property sampleBufferSize,设置输出分辨率,如果此分辨率不等于sampleBuffer中数据分辨率则会对视频数据做缩放
* @property autoSampleBufferSize,输出分辨率等于输入分辨率,即sampleBuffer中数据的实际分辨率
*/
- (void)sendVideoSampleBuffer:(CMSampleBufferRef)sampleBuffer;
/**
* Replaykit发送自定义音频包
* @prama sampleBuffer 声音
* @prama sampleBufferType RPSampleBufferTypeAudioApp or RPSampleBufferTypeAudioMic,
*
* 当两种声音都发送时,内部做混音;否则只发送一路声音
*/
- (void)sendAudioSampleBuffer:(CMSampleBufferRef)sampleBuffer withType:(RPSampleBufferType)sampleBufferType;
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。