-
Notifications
You must be signed in to change notification settings - Fork 271
diyCamera
pengbin edited this page Sep 13, 2016
·
9 revisions
由于金山云SDK将直播推流的各个环节都拆分为独立的类, 大部分数据都可以用回调的方式进行串联, 用户可以自由选择其中的环节进行使用. 比如 用户可以自己采集音视频数据,再使用金山云SDK推流模块 KSYStreamerBase,完成推流功能.
假设用户使用AVFoundation提供的接口来完成音视频的采集,得到的CMSampleBufferRef 可以直接送入KSYStreamerBase, 完成压缩和推流. 如果通过其他方式获取到NSDATA类型的音频数据, 则需要通过如下代码转为CMSampleBufferRef类型后, 送入KSYStreamerBase.
- 获得音频格式
-(AudioStreamBasicDescription) getAudioFormat{
AudioStreamBasicDescription format;
format.mSampleRate = 44100;
format.mFormatID = kAudioFormatLinearPCM;
format.mFormatFlags = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked;
format.mBytesPerPacket = 2;
format.mFramesPerPacket = 1;
format.mBytesPerFrame = 2;
format.mChannelsPerFrame = 1;//目前只支持单声道
format.mBitsPerChannel = 16;
format.mReserved = 0;
return format;
}
- 获得音频格式
- (CMSampleBufferRef)createAudioBuffer:(NSData *)sample_data
sampleCount:(size_t)sample_position_
channelCount:(size_t)nchans {
OSStatus status;
CMBlockBufferRef bbuf = NULL;
CMSampleBufferRef sbuf = NULL;
short *samples = (short *)sample_data.bytes;
size_t buflen = sample_data.length;
status = CMBlockBufferCreateWithMemoryBlock(kCFAllocatorDefault, samples, buflen, kCFAllocatorNull, NULL, 0, buflen,
0, &bbuf);
if (status != noErr) {
NSLog(@"CMBlockBufferCreateWithMemoryBlock error");
return NULL;
}
CMTime timestamp = [self getCurrentCMTime];
status = CMAudioSampleBufferCreateWithPacketDescriptions(kCFAllocatorDefault, bbuf, TRUE, 0, NULL, _audio_fmt_desc_,
sample_data.length / nchans / sizeof(short), timestamp,
NULL, &sbuf);
if (status != noErr)
{
NSLog(@"CMSampleBufferCreate error");
return NULL;
}
CMTimeShow(timestamp);
CFRelease(bbuf);
_audioFrames = _audioFrames + buflen/nchans/sizeof(short);
return sbuf;
}
推流模块基本配置:
- 构造推流实例, 并按照需求配置参数
KSYStreamerBase* _streamer = [[KSYStreamerBase alloc] initWithDefaultCfg];
_streamer.videoCodec = KSYVideoCodec_X264;//视频编码方式
_streamer.videoFPS = 15;//视频帧率
_streamer.audiokBPS = 48; // 音频码率
_streamer.videoInitBitrate = 600; // k bit ps//初始码率
_streamer.videoMaxBitrate = 1000; // k bit ps//最大码率
_streamer.videoMinBitrate = 0; // k bit ps//最小码率
- 开始推流
[_streamer startStream: _hostURL];//_hostURL为推流地址
[_streamer stopStream];
- 送入音视频数据
// 送入音频的数据
[_streamer processAudioSampleBuffer:buf];
// 送入视频数据 (CVPixelBufferRef + timeinfo)
[_streamer processVideoPixelBuffer:pixelBuffer
timeInfo:timeInfo];
// 或者送入视频数据 (CMSampleBufferRef)
[_streamer processVideoSampleBuffer:buf]
- 结束推流
[_streamer stopStream];