第六十篇、音视频采集硬编码(H264+ACC)
使用 AVCaptureSession进行实时采集音视频(YUV、),编码
通过AVCaptureVideoDataOutputSampleBufferDelegate获取到音视频buffer- 数据
分别对音视频原始数据进行编码
传输
ViewController
- //
- // ViewController.h
- // H264AACEncode
- //
- // Created by ZhangWen on 15/10/14.
- // Copyright © 2015年 Zhangwen. All rights reserved.
- //
- #import <UIKit/UIKit.h>
- #import <AVFoundation/AVFoundation.h>
- #import "AACEncoder.h"
- #import "H264Encoder.h"
- @interface ViewController : UIViewController <AVCaptureVideoDataOutputSampleBufferDelegate,AVCaptureAudioDataOutputSampleBufferDelegate,H264EncoderDelegate>
- @end
- //
- // ViewController.m
- // H264AACEncode
- //
- // Created by ZhangWen on 15/10/14.
- // Copyright © 2015年 Zhangwen. All rights reserved.
- //
- #import "ViewController.h"
- #define CAPTURE_FRAMES_PER_SECOND 20
- #define SAMPLE_RATE 44100
- #define VideoWidth 480
- #define VideoHeight 640
- @interface ViewController ()
- {
- UIButton *startBtn;
- bool startCalled;
- H264Encoder *h264Encoder;
- AACEncoder *aacEncoder;
- AVCaptureSession *captureSession;
- dispatch_queue_t _audioQueue;
- AVCaptureConnection* _audioConnection;
- AVCaptureConnection* _videoConnection;
- NSMutableData *_data;
- NSString *h264File;
- NSFileHandle *fileHandle;
- }
- @end
- @implementation ViewController
- - (void)viewDidLoad {
- [super viewDidLoad];
- // Do any additional setup after loading the view, typically from a nib.
- startCalled = true;
- _data = [[NSMutableData alloc] init];
- captureSession = [[AVCaptureSession alloc] init];
- [self initStartBtn];
- }
- #pragma mark
- #pragma mark - 设置音频 capture
- - (void) setupAudioCapture {
- aacEncoder = [[AACEncoder alloc] init];
- // create capture device with video input
- /*
- * Create audio connection
- */
- AVCaptureDevice *audioDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];
- NSError *error = nil;
- AVCaptureDeviceInput *audioInput = [[AVCaptureDeviceInput alloc] initWithDevice:audioDevice error:&error];
- if (error) {
- NSLog(@"Error getting audio input device: %@", error.description);
- }
- if ([captureSession canAddInput:audioInput]) {
- [captureSession addInput:audioInput];
- }
- _audioQueue = dispatch_queue_create("Audio Capture Queue", DISPATCH_QUEUE_SERIAL);
- AVCaptureAudioDataOutput* audioOutput = [[AVCaptureAudioDataOutput alloc] init];
- [audioOutput setSampleBufferDelegate:self queue:_audioQueue];
- if ([captureSession canAddOutput:audioOutput]) {
- [captureSession addOutput:audioOutput];
- }
- _audioConnection = [audioOutput connectionWithMediaType:AVMediaTypeAudio];
- }
- - (AVCaptureDevice *)cameraWithPosition:(AVCaptureDevicePosition)position
- {
- NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
- for ( AVCaptureDevice *device in devices )
- if ( device.position == position )
- return device;
- return nil;
- }
- #pragma mark
- #pragma mark - 设置视频 capture
- - (void) setupVideoCaprure
- {
- h264Encoder = [H264Encoder alloc];
- [h264Encoder initWithConfiguration];
- NSError *deviceError;
- AVCaptureDevice *cameraDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
- // cameraDevice = [self cameraWithPosition:AVCaptureDevicePositionBack];
- // cameraDevice.position = AVCaptureDevicePositionBack;
- AVCaptureDeviceInput *inputDevice = [AVCaptureDeviceInput deviceInputWithDevice:cameraDevice error:&deviceError];
- // make output device
- AVCaptureVideoDataOutput *outputDevice = [[AVCaptureVideoDataOutput alloc] init];
- NSString* key = (NSString*)kCVPixelBufferPixelFormatTypeKey;
- NSNumber* val = [NSNumber
- numberWithUnsignedInt:kCVPixelFormatType_420YpCbCr8BiPlanarFullRange];
- NSDictionary* videoSettings =
- [NSDictionary dictionaryWithObject:val forKey:key];
- NSError *error;
- [cameraDevice lockForConfiguration:&error];
- if (error == nil) {
- NSLog(@"cameraDevice.activeFormat.videoSupportedFrameRateRanges IS %@",[cameraDevice.activeFormat.videoSupportedFrameRateRanges objectAtIndex:]);
- if (cameraDevice.activeFormat.videoSupportedFrameRateRanges){
- [cameraDevice setActiveVideoMinFrameDuration:CMTimeMake(, CAPTURE_FRAMES_PER_SECOND)];
- [cameraDevice setActiveVideoMaxFrameDuration:CMTimeMake(, CAPTURE_FRAMES_PER_SECOND)];
- }
- }else{
- // handle error2
- }
- [cameraDevice unlockForConfiguration];
- // Start the session running to start the flow of data
- outputDevice.videoSettings = videoSettings;
- [outputDevice setSampleBufferDelegate:self queue:dispatch_get_main_queue()];
- // initialize capture session
- if ([captureSession canAddInput:inputDevice]) {
- [captureSession addInput:inputDevice];
- }
- if ([captureSession canAddOutput:outputDevice]) {
- [captureSession addOutput:outputDevice];
- }
- // begin configuration for the AVCaptureSession
- [captureSession beginConfiguration];
- // picture resolution
- [captureSession setSessionPreset:[NSString stringWithString:AVCaptureSessionPreset640x480]];
- _videoConnection = [outputDevice connectionWithMediaType:AVMediaTypeVideo];
- //Set landscape (if required)
- if ([_videoConnection isVideoOrientationSupported])
- {
- AVCaptureVideoOrientation orientation = AVCaptureVideoOrientationLandscapeRight; //<<<<<SET VIDEO ORIENTATION IF LANDSCAPE
- [_videoConnection setVideoOrientation:orientation];
- }
- // make preview layer and add so that camera's view is displayed on screen
- NSFileManager *fileManager = [NSFileManager defaultManager];
- NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
- NSString *documentsDirectory = [paths objectAtIndex:];
- h264File = [documentsDirectory stringByAppendingPathComponent:@"test.h264"];
- [fileManager removeItemAtPath:h264File error:nil];
- [fileManager createFileAtPath:h264File contents:nil attributes:nil];
- // Open the file using POSIX as this is anyway a test application
- //fd = open([h264File UTF8String], O_RDWR);
- fileHandle = [NSFileHandle fileHandleForWritingAtPath:h264File];
- [h264Encoder initEncode:VideoWidth height:VideoHeight];
- h264Encoder.delegate = self;
- }
- #pragma mark
- #pragma mark - sampleBuffer 数据
- -(void) captureOutput:(AVCaptureOutput*)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection*)connection
- {
- CMTime pts = CMSampleBufferGetPresentationTimeStamp(sampleBuffer);
- double dPTS = (double)(pts.value) / pts.timescale;
- // NSLog(@"DPTS is %f",dPTS);
- if (connection == _videoConnection) {
- [h264Encoder encode:sampleBuffer];
- } else if (connection == _audioConnection) {
- [aacEncoder encodeSampleBuffer:sampleBuffer completionBlock:^(NSData *encodedData, NSError *error) {
- if (encodedData) {
- NSLog(@"Audio data (%lu): %@", (unsigned long)encodedData.length, encodedData.description);
- #pragma mark
- #pragma mark - 音频数据(encodedData)
- [_data appendData:encodedData];
- } else {
- NSLog(@"Error encoding AAC: %@", error);
- }
- }];
- }
- }
- #pragma mark
- #pragma mark - 视频 sps 和 pps
- - (void)gotSpsPps:(NSData*)sps pps:(NSData*)pps
- {
- const char bytes[] = "\x00\x00\x00\x01";
- size_t length = (sizeof bytes) - ; //string literals have implicit trailing '\0'
- NSData *ByteHeader = [NSData dataWithBytes:bytes length:length];
- [fileHandle writeData:ByteHeader];
- [fileHandle writeData:sps];
- [fileHandle writeData:ByteHeader];
- [fileHandle writeData:pps];
- }
- #pragma mark
- #pragma mark - 视频数据回调
- - (void)gotEncodedData:(NSData*)data isKeyFrame:(BOOL)isKeyFrame
- {
- NSLog(@"Video data (%lu): %@", (unsigned long)data.length, data.description);
- if (fileHandle != NULL)
- {
- const char bytes[] = "\x00\x00\x00\x01";
- size_t length = (sizeof bytes) - ; //string literals have implicit trailing '\0'
- NSData *ByteHeader = [NSData dataWithBytes:bytes length:length];
- #pragma mark
- #pragma mark - 视频数据(data)
- [fileHandle writeData:ByteHeader];
- //[fileHandle writeData:UnitHeader];
- [fileHandle writeData:data];
- }
- }
- #pragma mark
- #pragma mark - 录制
- - (void)startBtnClicked
- {
- if (startCalled)
- {
- [self startCamera];
- startCalled = false;
- [startBtn setTitle:@"Stop" forState:UIControlStateNormal];
- }
- else
- {
- [startBtn setTitle:@"Start" forState:UIControlStateNormal];
- startCalled = true;
- [self stopCarmera];
- }
- }
- - (void) startCamera
- {
- [self setupAudioCapture];
- [self setupVideoCaprure];
- [captureSession commitConfiguration];
- [captureSession startRunning];
- }
- - (void) stopCarmera
- {
- [h264Encoder End];
- [captureSession stopRunning];
- //close(fd);
- [fileHandle closeFile];
- fileHandle = NULL;
- // 获取程序Documents目录路径
- NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
- NSString *documentsDirectory = [paths objectAtIndex:];
- NSMutableString * path = [[NSMutableString alloc]initWithString:documentsDirectory];
- [path appendString:@"/AACFile"];
- [_data writeToFile:path atomically:YES];
- }
- - (void)initStartBtn
- {
- startBtn = [UIButton buttonWithType:UIButtonTypeCustom];
- startBtn.frame = CGRectMake(, , , );
- startBtn.center = self.view.center;
- [startBtn addTarget:self action:@selector(startBtnClicked) forControlEvents:UIControlEventTouchUpInside];
- [startBtn setTitle:@"Start" forState:UIControlStateNormal];
- [startBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
- [self.view addSubview:startBtn];
- }
- - (void)didReceiveMemoryWarning {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- @end
AACEncoder
- //
- // AACEncoder.h
- // H264AACEncode
- //
- // Created by ZhangWen on 15/10/14.
- // Copyright © 2015年 Zhangwen. All rights reserved.
- //
- #import <Foundation/Foundation.h>
- #import <AVFoundation/AVFoundation.h>
- #import <AudioToolbox/AudioToolbox.h>
- @interface AACEncoder : NSObject
- @property (nonatomic) dispatch_queue_t encoderQueue;
- @property (nonatomic) dispatch_queue_t callbackQueue;
- - (void) encodeSampleBuffer:(CMSampleBufferRef)sampleBuffer completionBlock:(void (^)(NSData *encodedData, NSError* error))completionBlock;
- @end
- //
- // AACEncoder.m
- // H264AACEncode
- //
- // Created by ZhangWen on 15/10/14.
- // Copyright © 2015年 Zhangwen. All rights reserved.
- //
- #import "AACEncoder.h"
- @interface AACEncoder()
- @property (nonatomic) AudioConverterRef audioConverter;
- @property (nonatomic) uint8_t *aacBuffer;
- @property (nonatomic) NSUInteger aacBufferSize;
- @property (nonatomic) char *pcmBuffer;
- @property (nonatomic) size_t pcmBufferSize;
- @end
- @implementation AACEncoder
- - (void) dealloc {
- AudioConverterDispose(_audioConverter);
- free(_aacBuffer);
- }
- - (id) init {
- if (self = [super init]) {
- _encoderQueue = dispatch_queue_create("AAC Encoder Queue", DISPATCH_QUEUE_SERIAL);
- _callbackQueue = dispatch_queue_create("AAC Encoder Callback Queue", DISPATCH_QUEUE_SERIAL);
- _audioConverter = NULL;
- _pcmBufferSize = ;
- _pcmBuffer = NULL;
- _aacBufferSize = ;
- _aacBuffer = malloc(_aacBufferSize * sizeof(uint8_t));
- memset(_aacBuffer, , _aacBufferSize);
- }
- return self;
- }
- - (void) setupEncoderFromSampleBuffer:(CMSampleBufferRef)sampleBuffer {
- AudioStreamBasicDescription inAudioStreamBasicDescription = *CMAudioFormatDescriptionGetStreamBasicDescription((CMAudioFormatDescriptionRef)CMSampleBufferGetFormatDescription(sampleBuffer));
- AudioStreamBasicDescription outAudioStreamBasicDescription = {}; // Always initialize the fields of a new audio stream basic description structure to zero, as shown here: ...
- outAudioStreamBasicDescription.mSampleRate = inAudioStreamBasicDescription.mSampleRate; // The number of frames per second of the data in the stream, when the stream is played at normal speed. For compressed formats, this field indicates the number of frames per second of equivalent decompressed data. The mSampleRate field must be nonzero, except when this structure is used in a listing of supported formats (see “kAudioStreamAnyRate”).
- outAudioStreamBasicDescription.mFormatID = kAudioFormatMPEG4AAC; // kAudioFormatMPEG4AAC_HE does not work. Can't find `AudioClassDescription`. `mFormatFlags` is set to 0.
- outAudioStreamBasicDescription.mFormatFlags = kMPEG4Object_AAC_LC; // Format-specific flags to specify details of the format. Set to 0 to indicate no format flags. See “Audio Data Format Identifiers” for the flags that apply to each format.
- outAudioStreamBasicDescription.mBytesPerPacket = ; // The number of bytes in a packet of audio data. To indicate variable packet size, set this field to 0. For a format that uses variable packet size, specify the size of each packet using an AudioStreamPacketDescription structure.
- outAudioStreamBasicDescription.mFramesPerPacket = ; // The number of frames in a packet of audio data. For uncompressed audio, the value is 1. For variable bit-rate formats, the value is a larger fixed number, such as 1024 for AAC. For formats with a variable number of frames per packet, such as Ogg Vorbis, set this field to 0.
- outAudioStreamBasicDescription.mBytesPerFrame = ; // The number of bytes from the start of one frame to the start of the next frame in an audio buffer. Set this field to 0 for compressed formats. ...
- outAudioStreamBasicDescription.mChannelsPerFrame = ; // The number of channels in each frame of audio data. This value must be nonzero.
- outAudioStreamBasicDescription.mBitsPerChannel = ; // ... Set this field to 0 for compressed formats.
- outAudioStreamBasicDescription.mReserved = ; // Pads the structure out to force an even 8-byte alignment. Must be set to 0.
- AudioClassDescription *description = [self
- getAudioClassDescriptionWithType:kAudioFormatMPEG4AAC
- fromManufacturer:kAppleSoftwareAudioCodecManufacturer];
- OSStatus status = AudioConverterNewSpecific(&inAudioStreamBasicDescription, &outAudioStreamBasicDescription, , description, &_audioConverter);
- if (status != ) {
- NSLog(@"setup converter: %d", (int)status);
- }
- }
- - (AudioClassDescription *)getAudioClassDescriptionWithType:(UInt32)type
- fromManufacturer:(UInt32)manufacturer
- {
- static AudioClassDescription desc;
- UInt32 encoderSpecifier = type;
- OSStatus st;
- UInt32 size;
- st = AudioFormatGetPropertyInfo(kAudioFormatProperty_Encoders,
- sizeof(encoderSpecifier),
- &encoderSpecifier,
- &size);
- if (st) {
- NSLog(@"error getting audio format propery info: %d", (int)(st));
- return nil;
- }
- unsigned int count = size / sizeof(AudioClassDescription);
- AudioClassDescription descriptions[count];
- st = AudioFormatGetProperty(kAudioFormatProperty_Encoders,
- sizeof(encoderSpecifier),
- &encoderSpecifier,
- &size,
- descriptions);
- if (st) {
- NSLog(@"error getting audio format propery: %d", (int)(st));
- return nil;
- }
- for (unsigned int i = ; i < count; i++) {
- if ((type == descriptions[i].mSubType) &&
- (manufacturer == descriptions[i].mManufacturer)) {
- memcpy(&desc, &(descriptions[i]), sizeof(desc));
- return &desc;
- }
- }
- return nil;
- }
- static OSStatus inInputDataProc(AudioConverterRef inAudioConverter, UInt32 *ioNumberDataPackets, AudioBufferList *ioData, AudioStreamPacketDescription **outDataPacketDescription, void *inUserData)
- {
- AACEncoder *encoder = (__bridge AACEncoder *)(inUserData);
- UInt32 requestedPackets = *ioNumberDataPackets;
- //NSLog(@"Number of packets requested: %d", (unsigned int)requestedPackets);
- size_t copiedSamples = [encoder copyPCMSamplesIntoBuffer:ioData];
- if (copiedSamples < requestedPackets) {
- //NSLog(@"PCM buffer isn't full enough!");
- *ioNumberDataPackets = ;
- return -;
- }
- *ioNumberDataPackets = ;
- //NSLog(@"Copied %zu samples into ioData", copiedSamples);
- return noErr;
- }
- - (size_t) copyPCMSamplesIntoBuffer:(AudioBufferList*)ioData {
- size_t originalBufferSize = _pcmBufferSize;
- if (!originalBufferSize) {
- return ;
- }
- ioData->mBuffers[].mData = _pcmBuffer;
- ioData->mBuffers[].mDataByteSize = _pcmBufferSize;
- _pcmBuffer = NULL;
- _pcmBufferSize = ;
- return originalBufferSize;
- }
- - (void) encodeSampleBuffer:(CMSampleBufferRef)sampleBuffer completionBlock:(void (^)(NSData * encodedData, NSError* error))completionBlock {
- CFRetain(sampleBuffer);
- dispatch_async(_encoderQueue, ^{
- if (!_audioConverter) {
- [self setupEncoderFromSampleBuffer:sampleBuffer];
- }
- CMBlockBufferRef blockBuffer = CMSampleBufferGetDataBuffer(sampleBuffer);
- CFRetain(blockBuffer);
- OSStatus status = CMBlockBufferGetDataPointer(blockBuffer, , NULL, &_pcmBufferSize, &_pcmBuffer);
- NSError *error = nil;
- if (status != kCMBlockBufferNoErr) {
- error = [NSError errorWithDomain:NSOSStatusErrorDomain code:status userInfo:nil];
- }
- //NSLog(@"PCM Buffer Size: %zu", _pcmBufferSize);
- memset(_aacBuffer, , _aacBufferSize);
- AudioBufferList outAudioBufferList = {};
- outAudioBufferList.mNumberBuffers = ;
- outAudioBufferList.mBuffers[].mNumberChannels = ;
- outAudioBufferList.mBuffers[].mDataByteSize = _aacBufferSize;
- outAudioBufferList.mBuffers[].mData = _aacBuffer;
- AudioStreamPacketDescription *outPacketDescription = NULL;
- UInt32 ioOutputDataPacketSize = ;
- status = AudioConverterFillComplexBuffer(_audioConverter, inInputDataProc, (__bridge void *)(self), &ioOutputDataPacketSize, &outAudioBufferList, outPacketDescription);
- //NSLog(@"ioOutputDataPacketSize: %d", (unsigned int)ioOutputDataPacketSize);
- NSData *data = nil;
- if (status == ) {
- NSData *rawAAC = [NSData dataWithBytes:outAudioBufferList.mBuffers[].mData length:outAudioBufferList.mBuffers[].mDataByteSize];
- NSData *adtsHeader = [self adtsDataForPacketLength:rawAAC.length];
- NSMutableData *fullData = [NSMutableData dataWithData:adtsHeader];
- [fullData appendData:rawAAC];
- data = fullData;
- } else {
- error = [NSError errorWithDomain:NSOSStatusErrorDomain code:status userInfo:nil];
- }
- if (completionBlock) {
- dispatch_async(_callbackQueue, ^{
- completionBlock(data, error);
- });
- }
- CFRelease(sampleBuffer);
- CFRelease(blockBuffer);
- });
- }
- /**
- * Add ADTS header at the beginning of each and every AAC packet.
- * This is needed as MediaCodec encoder generates a packet of raw
- * AAC data.
- *
- * Note the packetLen must count in the ADTS header itself.
- * See: http://wiki.multimedia.cx/index.php?title=ADTS
- * Also: http://wiki.multimedia.cx/index.php?title=MPEG-4_Audio#Channel_Configurations
- **/
- - (NSData*) adtsDataForPacketLength:(NSUInteger)packetLength {
- int adtsLength = ;
- char *packet = malloc(sizeof(char) * adtsLength);
- // Variables Recycled by addADTStoPacket
- int profile = ; //AAC LC
- //39=MediaCodecInfo.CodecProfileLevel.AACObjectELD;
- int freqIdx = ; //44.1KHz
- int chanCfg = ; //MPEG-4 Audio Channel Configuration. 1 Channel front-center
- NSUInteger fullLength = adtsLength + packetLength;
- // fill in ADTS data
- packet[] = (char)0xFF; // 11111111 = syncword
- packet[] = (char)0xF9; // 1111 1 00 1 = syncword MPEG-2 Layer CRC
- packet[] = (char)(((profile-)<<) + (freqIdx<<) +(chanCfg>>));
- packet[] = (char)(((chanCfg&)<<) + (fullLength>>));
- packet[] = (char)((fullLength&0x7FF) >> );
- packet[] = (char)(((fullLength&)<<) + 0x1F);
- packet[] = (char)0xFC;
- NSData *data = [NSData dataWithBytesNoCopy:packet length:adtsLength freeWhenDone:YES];
- return data;
- }
- @end
H264Encoder
- //
- // H264Encoder.h
- // H264AACEncode
- //
- // Created by ZhangWen on 15/10/14.
- // Copyright © 2015年 Zhangwen. All rights reserved.
- //
- #import <Foundation/Foundation.h>
- #import <AVFoundation/AVFoundation.h>
- #import <VideoToolbox/VideoToolbox.h>
- @protocol H264EncoderDelegate <NSObject>
- - (void)gotSpsPps:(NSData*)sps pps:(NSData*)pps;
- - (void)gotEncodedData:(NSData*)data isKeyFrame:(BOOL)isKeyFrame;
- @end
- @interface H264Encoder : NSObject
- - (void) initWithConfiguration;
- - (void) start:(int)width height:(int)height;
- - (void) initEncode:(int)width height:(int)height;
- - (void) encode:(CMSampleBufferRef )sampleBuffer;
- - (void) End;
- @property (weak, nonatomic) NSString *error;
- @property (weak, nonatomic) id<H264EncoderDelegate> delegate;
- @end
- //
- // H264Encoder.m
- // H264AACEncode
- //
- // Created by ZhangWen on 15/10/14.
- // Copyright © 2015年 Zhangwen. All rights reserved.
- //
- #import "H264Encoder.h"
- @implementation H264Encoder
- {
- NSString * yuvFile;
- VTCompressionSessionRef EncodingSession;
- dispatch_queue_t aQueue;
- CMFormatDescriptionRef format;
- CMSampleTimingInfo * timingInfo;
- BOOL initialized;
- int frameCount;
- NSData *sps;
- NSData *pps;
- }
- @synthesize error;
- - (void) initWithConfiguration
- {
- EncodingSession = nil;
- initialized = true;
- aQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, );
- frameCount = ;
- sps = NULL;
- pps = NULL;
- }
- void didCompressH264(void *outputCallbackRefCon, void *sourceFrameRefCon, OSStatus status, VTEncodeInfoFlags infoFlags,
- CMSampleBufferRef sampleBuffer )
- {
- // NSLog(@"didCompressH264 called with status %d infoFlags %d", (int)status, (int)infoFlags);
- if (status != ) return;
- if (!CMSampleBufferDataIsReady(sampleBuffer))
- {
- NSLog(@"didCompressH264 data is not ready ");
- return;
- }
- H264Encoder* encoder = (__bridge H264Encoder*)outputCallbackRefCon;
- // Check if we have got a key frame first
- bool keyframe = !CFDictionaryContainsKey( (CFArrayGetValueAtIndex(CMSampleBufferGetSampleAttachmentsArray(sampleBuffer, true), )), kCMSampleAttachmentKey_NotSync);
- if (keyframe)
- {
- CMFormatDescriptionRef format = CMSampleBufferGetFormatDescription(sampleBuffer);
- // CFDictionaryRef extensionDict = CMFormatDescriptionGetExtensions(format);
- // Get the extensions
- // From the extensions get the dictionary with key "SampleDescriptionExtensionAtoms"
- // From the dict, get the value for the key "avcC"
- size_t sparameterSetSize, sparameterSetCount;
- const uint8_t *sparameterSet;
- OSStatus statusCode = CMVideoFormatDescriptionGetH264ParameterSetAtIndex(format, , &sparameterSet, &sparameterSetSize, &sparameterSetCount, );
- if (statusCode == noErr)
- {
- // Found sps and now check for pps
- size_t pparameterSetSize, pparameterSetCount;
- const uint8_t *pparameterSet;
- OSStatus statusCode = CMVideoFormatDescriptionGetH264ParameterSetAtIndex(format, , &pparameterSet, &pparameterSetSize, &pparameterSetCount, );
- if (statusCode == noErr)
- {
- // Found pps
- encoder->sps = [NSData dataWithBytes:sparameterSet length:sparameterSetSize];
- encoder->pps = [NSData dataWithBytes:pparameterSet length:pparameterSetSize];
- if (encoder->_delegate)
- {
- [encoder->_delegate gotSpsPps:encoder->sps pps:encoder->pps];
- }
- }
- }
- }
- CMBlockBufferRef dataBuffer = CMSampleBufferGetDataBuffer(sampleBuffer);
- size_t length, totalLength;
- char *dataPointer;
- OSStatus statusCodeRet = CMBlockBufferGetDataPointer(dataBuffer, , &length, &totalLength, &dataPointer);
- if (statusCodeRet == noErr) {
- size_t bufferOffset = ;
- static const int AVCCHeaderLength = ;
- while (bufferOffset < totalLength - AVCCHeaderLength) {
- // Read the NAL unit length
- uint32_t NALUnitLength = ;
- memcpy(&NALUnitLength, dataPointer + bufferOffset, AVCCHeaderLength);
- // Convert the length value from Big-endian to Little-endian
- NALUnitLength = CFSwapInt32BigToHost(NALUnitLength);
- NSData* data = [[NSData alloc] initWithBytes:(dataPointer + bufferOffset + AVCCHeaderLength) length:NALUnitLength];
- [encoder->_delegate gotEncodedData:data isKeyFrame:keyframe];
- // Move to the next NAL unit in the block buffer
- bufferOffset += AVCCHeaderLength + NALUnitLength;
- }
- }
- }
- - (void) start:(int)width height:(int)height
- {
- int frameSize = (width * height * 1.5);
- if (!initialized)
- {
- NSLog(@"H264: Not initialized");
- error = @"H264: Not initialized";
- return;
- }
- dispatch_sync(aQueue, ^{
- // For testing out the logic, lets read from a file and then send it to encoder to create h264 stream
- // Create the compression session
- OSStatus status = VTCompressionSessionCreate(NULL, width, height, kCMVideoCodecType_H264, NULL, NULL, NULL, didCompressH264, (__bridge void *)(self), &EncodingSession);
- NSLog(@"H264: VTCompressionSessionCreate %d", (int)status);
- if (status != )
- {
- NSLog(@"H264: Unable to create a H264 session");
- error = @"H264: Unable to create a H264 session";
- return ;
- }
- // Set the properties
- VTSessionSetProperty(EncodingSession, kVTCompressionPropertyKey_RealTime, kCFBooleanTrue);
- VTSessionSetProperty(EncodingSession, kVTCompressionPropertyKey_AllowFrameReordering, kCFBooleanFalse);
- VTSessionSetProperty(EncodingSession, kVTCompressionPropertyKey_MaxKeyFrameInterval, );
- VTSessionSetProperty(EncodingSession, kVTCompressionPropertyKey_ProfileLevel, kVTProfileLevel_H264_High_AutoLevel);
- // Tell the encoder to start encoding
- VTCompressionSessionPrepareToEncodeFrames(EncodingSession);
- // Start reading from the file and copy it to the buffer
- // Open the file using POSIX as this is anyway a test application
- int fd = open([yuvFile UTF8String], O_RDONLY);
- if (fd == -)
- {
- NSLog(@"H264: Unable to open the file");
- error = @"H264: Unable to open the file";
- return ;
- }
- NSMutableData* theData = [[NSMutableData alloc] initWithLength:frameSize] ;
- NSUInteger actualBytes = frameSize;
- while (actualBytes > )
- {
- void* buffer = [theData mutableBytes];
- NSUInteger bufferSize = [theData length];
- actualBytes = read(fd, buffer, bufferSize);
- if (actualBytes < frameSize)
- [theData setLength:actualBytes];
- frameCount++;
- // Create a CM Block buffer out of this data
- CMBlockBufferRef BlockBuffer = NULL;
- OSStatus status = CMBlockBufferCreateWithMemoryBlock(NULL, buffer, actualBytes,kCFAllocatorNull, NULL, , actualBytes, kCMBlockBufferAlwaysCopyDataFlag, &BlockBuffer);
- // Check for error
- if (status != noErr)
- {
- NSLog(@"H264: CMBlockBufferCreateWithMemoryBlock failed with %d", (int)status);
- error = @"H264: CMBlockBufferCreateWithMemoryBlock failed ";
- return ;
- }
- // Create a CM Sample Buffer
- CMSampleBufferRef sampleBuffer = NULL;
- CMFormatDescriptionRef formatDescription;
- CMFormatDescriptionCreate ( kCFAllocatorDefault, // Allocator
- kCMMediaType_Video,
- 'I420',
- NULL,
- &formatDescription );
- CMSampleTimingInfo sampleTimingInfo = {CMTimeMake(, )};
- OSStatus statusCode = CMSampleBufferCreate(kCFAllocatorDefault, BlockBuffer, YES, NULL, NULL, formatDescription, , , &sampleTimingInfo, , NULL, &sampleBuffer);
- // Check for error
- if (statusCode != noErr) {
- NSLog(@"H264: CMSampleBufferCreate failed with %d", (int)statusCode);
- error = @"H264: CMSampleBufferCreate failed ";
- return;
- }
- CFRelease(BlockBuffer);
- BlockBuffer = NULL;
- // Get the CV Image buffer
- CVImageBufferRef imageBuffer = (CVImageBufferRef)CMSampleBufferGetImageBuffer(sampleBuffer);
- // Create properties
- CMTime presentationTimeStamp = CMTimeMake(frameCount, );
- //CMTime duration = CMTimeMake(1, DURATION);
- VTEncodeInfoFlags flags;
- // Pass it to the encoder
- statusCode = VTCompressionSessionEncodeFrame(EncodingSession,
- imageBuffer,
- presentationTimeStamp,
- kCMTimeInvalid,
- NULL, NULL, &flags);
- // Check for error
- if (statusCode != noErr) {
- NSLog(@"H264: VTCompressionSessionEncodeFrame failed with %d", (int)statusCode);
- error = @"H264: VTCompressionSessionEncodeFrame failed ";
- // End the session
- VTCompressionSessionInvalidate(EncodingSession);
- CFRelease(EncodingSession);
- EncodingSession = NULL;
- error = NULL;
- return;
- }
- // NSLog(@"H264: VTCompressionSessionEncodeFrame Success");
- }
- // Mark the completion
- VTCompressionSessionCompleteFrames(EncodingSession, kCMTimeInvalid);
- // End the session
- VTCompressionSessionInvalidate(EncodingSession);
- CFRelease(EncodingSession);
- EncodingSession = NULL;
- error = NULL;
- close(fd);
- });
- }
- - (void) initEncode:(int)width height:(int)height
- {
- dispatch_sync(aQueue, ^{
- // For testing out the logic, lets read from a file and then send it to encoder to create h264 stream
- // Create the compression session
- OSStatus status = VTCompressionSessionCreate(NULL, width, height, kCMVideoCodecType_H264, NULL, NULL, NULL, didCompressH264, (__bridge void *)(self), &EncodingSession);
- NSLog(@"H264: VTCompressionSessionCreate %d", (int)status);
- if (status != )
- {
- NSLog(@"H264: Unable to create a H264 session");
- error = @"H264: Unable to create a H264 session";
- return ;
- }
- // Set the properties
- VTSessionSetProperty(EncodingSession, kVTCompressionPropertyKey_RealTime, kCFBooleanTrue);
- VTSessionSetProperty(EncodingSession, kVTCompressionPropertyKey_ProfileLevel, kVTProfileLevel_H264_Main_AutoLevel);
- // Tell the encoder to start encoding
- VTCompressionSessionPrepareToEncodeFrames(EncodingSession);
- });
- }
- - (void) encode:(CMSampleBufferRef )sampleBuffer
- {
- dispatch_sync(aQueue, ^{
- frameCount++;
- // Get the CV Image buffer
- CVImageBufferRef imageBuffer = (CVImageBufferRef)CMSampleBufferGetImageBuffer(sampleBuffer);
- // Create properties
- CMTime presentationTimeStamp = CMTimeMake(frameCount, );
- //CMTime duration = CMTimeMake(1, DURATION);
- VTEncodeInfoFlags flags;
- // Pass it to the encoder
- OSStatus statusCode = VTCompressionSessionEncodeFrame(EncodingSession,
- imageBuffer,
- presentationTimeStamp,
- kCMTimeInvalid,
- NULL, NULL, &flags);
- // Check for error
- if (statusCode != noErr) {
- NSLog(@"H264: VTCompressionSessionEncodeFrame failed with %d", (int)statusCode);
- error = @"H264: VTCompressionSessionEncodeFrame failed ";
- // End the session
- VTCompressionSessionInvalidate(EncodingSession);
- CFRelease(EncodingSession);
- EncodingSession = NULL;
- error = NULL;
- return;
- }
- // NSLog(@"H264: VTCompressionSessionEncodeFrame Success");
- });
- }
- - (void) End
- {
- // Mark the completion
- VTCompressionSessionCompleteFrames(EncodingSession, kCMTimeInvalid);
- // End the session
- VTCompressionSessionInvalidate(EncodingSession);
- CFRelease(EncodingSession);
- EncodingSession = NULL;
- error = NULL;
- }
- @end
第六十篇、音视频采集硬编码(H264+ACC)的更多相关文章
- 手机Android音视频采集与直播推送,实现单兵、移动监控类应用
从安卓智能手机.平板,到可穿戴的Android Ware.眼镜.手表.再到Android汽车.智能家居.电视,甚至最近看新闻,日本出的几款机器人都是Android系统的,再把目光放回监控行业,传统监控 ...
- C#专业的音视频采集录制类库SharpCapture介绍
SharpCapture是高性能.轻量级.接口清晰.使用简单的C#语言编写的.NET音视频采集.屏幕录制类库.本类库可以采集系统声卡.麦克风.摄像头.屏幕画面,支持声卡和话筒混音采集. 可以应用到直播 ...
- 一个基于JRTPLIB的轻量级RTSP客户端(myRTSPClient)——实现篇:(六)RTP音视频传输解析层之音视频数据传输格式
一.差异 本地音视频数据格式和用来传输的音视频数据格式存在些许差异,由于音视频数据流到达客户端时,需要考虑数据流的数据边界.分包.组包顺序等问题,所以传输中的音视频数据往往会多一些字节. 举个例子,有 ...
- 全志Tina_dolphin播放音视频裸流(h264,pcm)验证
最近在验证tina对裸流音视频的支持,主要指h264视频裸流及pcm音频裸流. 在原始sdk中有针对很多video和audio类型的parser,但就是没有找到pcm和h264的parser,所以需要 ...
- iOS-VideoToolbox硬编码H264
前言 VideoToolBox是iOS8之后,苹果开发的用于硬解码编码H264/H265(iOS11以后支持)的API. 对于H264还不了解的童鞋一定要先看下这边的H264的简介. 编码流程 我们实 ...
- 第六十篇:Vue的基本使用
好家伙,要来了,经典"hello world" 试用一下vue ① 导入 vue.js的 script 脚本文件 ② 在页面中声明一个将要被vue所控制的DOM区域 ③ 创建vm实 ...
- C#音视频网络流解码:H264视频和ACC音频
下面两种方式是直接翻译过来的,还有问题,比如指针的使用和值的传入.考虑C#和C++的差异,还是要抱着怀疑的态度去看待,不一定是对的. H264视频解码网络流: using FFmpeg.AutoGen ...
- freeswitch 音 视频 支持的编码
FreeSWITCH 支持很多的语音编解码:[13] PCMU – G.711 µ-law PCMA – G.711 A-law G.722 G.722.1 G.722.1c G.726 G.726 ...
- EasyPusher安卓Android手机直播推送之MediaCodec 硬编码H264格式
本文转自Holo的博客:http://blog.csdn.net/u013758734/article/details/50834770 最近在研究EasyDarwin的Push库EasyPusher ...
随机推荐
- 正则表达式(Regular Expression)
匹配中文字符的正则表达式: [\u4e00-\u9fa5] 评注:匹配中文还真是个头疼的事,有了这个表达式就好办了 匹配双字节字符(包括汉字在内):[^\x00-\xff] 评注:可以用来计算字符串的 ...
- 通过ulimit改善linux系统性能(摘自IBM)
本文介绍了 ulimit 内键指令的主要功能以及用于改善系统性能的 ulimit 用法.通过这篇文章,读者不仅能够了解 ulimit 所起的作用.而且能够学会怎样更好地通过 ulimit 限制资源的使 ...
- iOS开发——语法篇OC篇&高级语法精讲二
Objective高级语法精讲二 Objective-C是基于C语言加入了面向对象特性和消息转发机制的动态语言,这意味着它不仅需要一个编译器,还需要Runtime系统来动态创建类和对象,进行消息发送和 ...
- 交换a、b
有两个变量a和b,不使用任何中间变量交换a和b. 方法一: 采用如下方法: a=a+b; b=a-b; a=a-b; 这样做的缺点就是如果a.b都是比较大的数,则a=a+b时就会越界. 而采用: a= ...
- java_有返回值线程_提前加载例子
package com.demo.test3; import java.util.concurrent.Callable; import java.util.concurrent.ExecutionE ...
- C#_uploadify_mvc_version
jQuery Uploadify在ASP.NET MVC3中的使用 1.Uploadify简介 Uploadify是基于jQuery的一种上传插件,支持多文件.带进度条显示上传,在项目开发中常被使用. ...
- uboot_starts_analysis.pdf
Uboot中start.S源码的指令级的详尽解析 HTML版本的在线地址为:http://www.crifan.com/files/doc/docbook/uboot_starts_analysis/ ...
- UITableViewCell高度自适应探索--AutoLayout结合Frame
UITableViewCell高度自适应探索--UITableView+FDTemplateLayoutCell地址: http://www.jianshu.com/p/7839e3a273a6UIT ...
- SQlServer2008 之 定时执行sql语句作业的制定
1.打开[SQL Server Management Studio],在[对象资源管理器]列表中选择[SQL Server 代理]: 2.鼠标右击[SQL Server 代理],选择[启动(S)],如 ...
- AJAX XMLHttpRequest
<html> <head> <title>XMLHTTPRequest对象的说明DEMO</title> <script language=&qu ...