首页 文章 精选 留言 我的

精选列表

搜索[硬件兼容性],共10002篇文章
优秀的个人博客,低调大师

向后兼容性增强和新的宏

以下由中文社区翻译,能力有限,如有翻译错误,欢迎指正! 在持续六个月的开发后,我们骄傲地宣布,Nim 1.2 版本发布了! 本发行版包含了超过 600 次的尚未被合并到 1.0.x 版本的提交。 与 1.0 相比,添加了几个新功能和标准库。 我们努力将断崖式的改动降至最低, 但如果不进行这些必要的更改,某些错误是无法被修复的, 而且我们认为我们的用户将从中受益。 我们建议所有用户升级和使用 1.2 版本。 使用多个版本的 Nim useVersion 如果你想要更新到 v1.2 版本,但依赖与 v1.0 版本的一些行为, 你可以使用命令行参数 --useVersion 来切换。 在最新的 Nim 中,你可以用这个参数来模拟之前版本的 Nim 。 举个例子,v1.2 版本的 Nim 对 proc 类型的类型转换变得更加严格。 如果这个变更无法兼容你的代码,你可以使用 --useVersion:1.0 来模拟之前版本的行为。 上面的例子只是个示例。 如果你依赖于一些边缘案例,或现已修复的错误行为,试试 --useVersion:1.0 ,他可能返回你想看到的结果。 如果没有得到你想要的之前版本的行为,请打开在 我们的 bug 追踪器 上提个 issue , 我们会努力在未来的 bug 修复版本(1.2.x)中进行修复。 when declared 如果你开发了 Nim 的库,而且想让不管是用 v1.0 的还是 v1.2 版本的用户都能使用你的库, 但却依赖了一些在 v1.0 版本没有,而在 v1.2 版本中才加入的新特性, 你可以使用 when declared 作为你的优势。 举个例子, sequtils 模块现在加入了 unzip 过程。 你就可以把你的代码写成下面这样: import sequtils when not declared(unzip): proc unzip*[S, T](s: openArray[(S, T)]): (seq[S], seq[T]) = result[0] = newSeq[S](s.len) result[1] = newSeq[T](s.len) for i in 0..<s.len: result[0][i] = s[i][0] result[1][i] = s[i][1] let a = @[('a', 1), ('b', 2), ('c', 3)] let b = unzip(a) # version 1.0 将会使用上方声明的过程 # version 1.2 会直接使用 sequtils 模块的过程 assert b == (@['a', 'b', 'c'], @[1, 2, 3]) 安装 Nim 1.2 Nim 的新用户 检查你系统的包管理器是否已经装载了 1.2 版本, 或者按照 这里 的说明进行安装。 老用户 如果你之前使用 choosenim 安装过老版本的 Nim , 升级到 1.2 版本就这么简单: $ choosenim update stable 为 v1.2 贡献力量 我们的贡献者都是超人, 我们列举了很多到 这里。 给你们个大大的么么哒,没有你们就没有这个版本! 新特性 GC: ARC 我们这次版本主要的新特性是 --gc:arc ! 为了避免陷入本文中大量的细节, 我们建议你查看下 FOSDEM上的这个视频(YouTuBe), Araq 在视频中解释了 ARC 背后的详细信息,并展示了一些显示其优点的基准。 之后可能会更新一篇关于 ARC 详细说明的文章。 新的宏语法糖 1.2.0 版本介绍了一些新的宏,它们应该可以帮你完成一些通用的任务。 在接下来的一些章节中,我们会给你展示他们和他们的用例。 所有的示例都意味着 import sugar。 Collect collect 是一个用以解析序列/集合/表解析的宏。 它替代了 lc (list comprehension,列表解析), 而且比 lc 更人性化和强大。 使用 collect 没有开销,比如说:你可以把 newSeqOfCap 和它配合使用。 所有集合的语法都是类似的,区别在于 init 函数和最后一个表达式。 举个例子: let numbers = collect(newSeq): # 或者: collect(newSeqOfCap(10)) for i in 1..10: i assert numbers == @[1, 2, 3, 4, 5, 6, 7, 8, 9, 10] import tables let oddSquares = collect(initTable): for i in numbers: if i mod 2 == 1: {i: i * i} assert len(oddSquares) == 5 assert oddSquares[7] == 49 import sets let oddOrNegative = collect(initHashSet): for i in numbers: {if i mod 2 == 1: i else: -i} assert 5 in oddOrNegative assert 2 notin oddOrNegative assert -2 in oddOrNegative Dup dup 是一个宏,它让原地函数(in-place functions,译者注:引用传递参数的函数) 可以在不影响和修改函数输入的情况下返回结果。 我们是这么考虑的,Nim的未来版本将只提供函数的原地版本 (即不再提供 sort 和 sorted、rotateLeft 和 rotatedLeft 等), 他们可以被 dup 转换为返回拷贝的函数。 这些版本可以通过 dup 转换成返回副本的函数。 这个宏也允许了(其他方式的)函数链(function chaining)。 还是看示例吧: proc makePalindrome(s: var string) = for i in countdown(s.len-2, 0): s.add(s[i]) var a = "abc" makePalindrome(a) assert a == "abcba" var b = "xyz" var c = b.dup(makePalindrome) assert b == "xyz" assert c == "xyzyx" 下划线(_)可用于表示传递参数的位置: import algorithm # b = "xyz" var d = dup b: makePalindrome # xyzyx sort(_, SortOrder.Descending) # zyyxx makePalindrome # zyyxxxyyz assert d == "zyyxxxyyz" Capture ‎capture 是一个宏,在循环中创建闭包时非常有用, 以捕获一些局部循环变量(按其当前迭代值)。 您可能希望将其用于事件处理和 {int: callBack} 查找表。‎ 举例如下: import strformat, sugar var myClosure: proc(): string for i in 5..7: for j in 7..9: if i * j == 42: capture i, j: myClosure = proc (): string = fmt"{i} * {j} = 42" let a = myClosure() assert a == "6 * 7 = 42"

优秀的个人博客,低调大师

Tangdao Design 发布 1.0.1,修复兼容性,新增组件,添加后台模板页

本次更新内容如下: 修复 1、修复样式sass变量问题。 新增模板 1、新增模板、模板布局、导航 查看模板 新增组件 1、新增模式窗口 2、整理工具类 3、新增气泡卡片 4、新增进度条 5、新增步骤组件 6、新增滚动插件 7、新增加载组件 8、新增Tab标签 9、新增Toast提示组件 10、新增文字提示组件 11、新增表格组件 12、新增表单 13、新增表单布局 14、新增Select2组件 15、新增Search组件 16、新增各类图表组件 Tangdao Design 是一个优质的现代干净的Bootstrap 4中台模板,使用简单和最小的设计。它使用SASS预处理器、HTML5、CSS3和jQuery插件完全响应。它使用了大多数helper/utilities类,这些类是可重用的类,可以加快页面的速度并提高页面时间加载的速度。它可以用于大多数类型的应用程序模板,如分析、管理等程序,也可以用于您的桌面或移动应用程序。惊人的灵活性和可重用性。

优秀的个人博客,低调大师

MySQL Driver 5.1.X与6.X日期兼容性问题

1.引言 最近在一个新项目里尝试使用mysql-connector-java-6.0.5.jar,但是从MaxCompute(原名ODPS)中导入MySQL的数据在控制台中看到是正常的,从Java应用里读取的却是相差13或14小时的。甚至SQL里限定了数据的时间在某一天,应用查出来的数据还能是不在那天的。这就很奇怪了,本着求根问底,踩坑识坑的精神,好好地研究了一把。 首先看从http://search.maven.org中可以大概的看到MySQL Driver的发布历史:5.1.X和6.X版本现在正在双线演进。 2.复现问题 那么首先写一个小的JDBC程序来验证一下两个版本Driver行为的不一致,MySQL中的表如下: CREATE TABLE `demo_table` ( `id` INT(11) NULL DEFA

优秀的个人博客,低调大师

转载: iOS音视频实时采集硬件编码

原文地址:https://blog.csdn.net/u011518723/article/details/49248467 我的demo地址:https://github.com/weiman152/LiveRecordDemo。 其中的swift版的viewcontroller是视频采集部分,供参考。 OC部分包括了音视频的采集以及编码。 感谢作者的分享,下面是原文内容: 使用 AVCaptureSession进行实时采集音视频 通过AVCaptureVideoDataOutputSampleBufferDelegate获取到音视频buffer数据 分别对音视频原始数据进行编码 传输 // // 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:0]); if (cameraDevice.activeFormat.videoSupportedFrameRateRanges){ [cameraDevice setActiveVideoMinFrameDuration:CMTimeMake(1, CAPTURE_FRAMES_PER_SECOND)]; [cameraDevice setActiveVideoMaxFrameDuration:CMTimeMake(1, 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 is view is displayed on screen NSFileManager *fileManager = [NSFileManager defaultManager]; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; 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) - 1; //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) - 1; //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:0]; NSMutableString * path = [[NSMutableString alloc]initWithString:documentsDirectory]; [path appendString:@"/AACFile"]; [_data writeToFile:path atomically:YES]; } - (void)initStartBtn { startBtn = [UIButton buttonWithType:UIButtonTypeCustom]; startBtn.frame = CGRectMake(0, 0, 100, 30); 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.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 = 0; _pcmBuffer = NULL; _aacBufferSize = 1024; _aacBuffer = malloc(_aacBufferSize * sizeof(uint8_t)); memset(_aacBuffer, 0, _aacBufferSize); } return self; } - (void) setupEncoderFromSampleBuffer:(CMSampleBufferRef)sampleBuffer { AudioStreamBasicDescription inAudioStreamBasicDescription = *CMAudioFormatDescriptionGetStreamBasicDescription((CMAudioFormatDescriptionRef)CMSampleBufferGetFormatDescription(sampleBuffer)); AudioStreamBasicDescription outAudioStreamBasicDescription = {0}; // 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 = 0; // 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 = 1024; // 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 = 0; // 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 = 1; // The number of channels in each frame of audio data. This value must be nonzero. outAudioStreamBasicDescription.mBitsPerChannel = 0; // ... Set this field to 0 for compressed formats. outAudioStreamBasicDescription.mReserved = 0; // 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, 1, description, &_audioConverter); if (status != 0) { 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 = 0; 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 = 0; return -1; } *ioNumberDataPackets = 1; //NSLog(@"Copied %zu samples into ioData", copiedSamples); return noErr; } - (size_t) copyPCMSamplesIntoBuffer:(AudioBufferList*)ioData { size_t originalBufferSize = _pcmBufferSize; if (!originalBufferSize) { return 0; } ioData->mBuffers[0].mData = _pcmBuffer; ioData->mBuffers[0].mDataByteSize = _pcmBufferSize; _pcmBuffer = NULL; _pcmBufferSize = 0; 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, 0, 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, 0, _aacBufferSize); AudioBufferList outAudioBufferList = {0}; outAudioBufferList.mNumberBuffers = 1; outAudioBufferList.mBuffers[0].mNumberChannels = 1; outAudioBufferList.mBuffers[0].mDataByteSize = _aacBufferSize; outAudioBufferList.mBuffers[0].mData = _aacBuffer; AudioStreamPacketDescription *outPacketDescription = NULL; UInt32 ioOutputDataPacketSize = 1; status = AudioConverterFillComplexBuffer(_audioConverter, inInputDataProc, (__bridge void *)(self), &ioOutputDataPacketSize, &outAudioBufferList, outPacketDescription); //NSLog(@"ioOutputDataPacketSize: %d", (unsigned int)ioOutputDataPacketSize); NSData *data = nil; if (status == 0) { NSData *rawAAC = [NSData dataWithBytes:outAudioBufferList.mBuffers[0].mData length:outAudioBufferList.mBuffers[0].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 = 7; char *packet = malloc(sizeof(char) * adtsLength); // Variables Recycled by addADTStoPacket int profile = 2; //AAC LC //39=MediaCodecInfo.CodecProfileLevel.AACObjectELD; int freqIdx = 4; //44.1KHz int chanCfg = 1; //MPEG-4 Audio Channel Configuration. 1 Channel front-center NSUInteger fullLength = adtsLength + packetLength; // fill in ADTS data packet[0] = (char)0xFF; // 11111111 = syncword packet[1] = (char)0xF9; // 1111 1 00 1 = syncword MPEG-2 Layer CRC packet[2] = (char)(((profile-1)<<6) + (freqIdx<<2) +(chanCfg>>2)); packet[3] = (char)(((chanCfg&3)<<6) + (fullLength>>11)); packet[4] = (char)((fullLength&0x7FF) >> 3); packet[5] = (char)(((fullLength&7)<<5) + 0x1F); packet[6] = (char)0xFC; NSData *data = [NSData dataWithBytesNoCopy:packet length:adtsLength freeWhenDone:YES]; return data; } @end // // 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, 0); frameCount = 0; 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 != 0) 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), 0)), 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, 0, &sparameterSet, &sparameterSetSize, &sparameterSetCount, 0 ); if (statusCode == noErr) { // Found sps and now check for pps size_t pparameterSetSize, pparameterSetCount; const uint8_t *pparameterSet; OSStatus statusCode = CMVideoFormatDescriptionGetH264ParameterSetAtIndex(format, 1, &pparameterSet, &pparameterSetSize, &pparameterSetCount, 0 ); 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, 0, &length, &totalLength, &dataPointer); if (statusCodeRet == noErr) { size_t bufferOffset = 0; static const int AVCCHeaderLength = 4; while (bufferOffset < totalLength - AVCCHeaderLength) { // Read the NAL unit length uint32_t NALUnitLength = 0; 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 != 0) { 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, 240); 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 == -1) { 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 > 0) { 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, 0, 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(1, 300)}; OSStatus statusCode = CMSampleBufferCreate(kCFAllocatorDefault, BlockBuffer, YES, NULL, NULL, formatDescription, 1, 1, &sampleTimingInfo, 0, 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, 300); //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 != 0) { 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, 1000); //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

资源下载

更多资源
腾讯云软件源

腾讯云软件源

为解决软件依赖安装时官方源访问速度慢的问题,腾讯云为一些软件搭建了缓存服务。您可以通过使用腾讯云软件源站来提升依赖包的安装速度。为了方便用户自由搭建服务架构,目前腾讯云软件源站支持公网访问和内网访问。

Nacos

Nacos

Nacos /nɑ:kəʊs/ 是 Dynamic Naming and Configuration Service 的首字母简称,一个易于构建 AI Agent 应用的动态服务发现、配置管理和AI智能体管理平台。Nacos 致力于帮助您发现、配置和管理微服务及AI智能体应用。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据、流量管理。Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。

Sublime Text

Sublime Text

Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python的插件,代码段等。还可自定义键绑定,菜单和工具栏。Sublime Text 的主要功能包括:拼写检查,书签,完整的 Python API , Goto 功能,即时项目切换,多选择,多窗口等等。Sublime Text 是一个跨平台的编辑器,同时支持Windows、Linux、Mac OS X等操作系统。

WebStorm

WebStorm

WebStorm 是jetbrains公司旗下一款JavaScript 开发工具。目前已经被广大中国JS开发者誉为“Web前端开发神器”、“最强大的HTML5编辑器”、“最智能的JavaScript IDE”等。与IntelliJ IDEA同源,继承了IntelliJ IDEA强大的JS部分的功能。

用户登录
用户注册