Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/103.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios AVAssetWriterInput(音频)和单核设备的性能问题_Ios_Audio_Avfoundation_Audio Recording_Avassetwriter - Fatal编程技术网

Ios AVAssetWriterInput(音频)和单核设备的性能问题

Ios AVAssetWriterInput(音频)和单核设备的性能问题,ios,audio,avfoundation,audio-recording,avassetwriter,Ios,Audio,Avfoundation,Audio Recording,Avassetwriter,我在iPhone 3GS和iPhone 4等单核设备上的AVAssetWriterInput出现了一个奇怪的性能问题。基本上,我有一个AVAssetWriter,有两个AvassetWriterInput,一个用于视频,一个用于音频。大致上,它看起来像: AVAssetWriter * assetWriter = [[AVAssetWriter alloc] initWithURL:videoURL

我在iPhone 3GS和iPhone 4等单核设备上的AVAssetWriterInput出现了一个奇怪的性能问题。基本上,我有一个AVAssetWriter,有两个AvassetWriterInput,一个用于视频,一个用于音频。大致上,它看起来像:

AVAssetWriter * assetWriter = [[AVAssetWriter alloc] initWithURL:videoURL
                                                        fileType:AVFileTypeMPEG4
                                                           error:&error];
videoWriterInput = [AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeVideo
                                                      outputSettings:videoSettings]; 
videoWriterInput.expectsMediaDataInRealTime = YES;

NSDictionary * audioSettings = [NSDictionary dictionaryWithObjectsAndKeys:
                                    [NSNumber numberWithInt:kAudioFormatMPEG4AAC],      AVFormatIDKey,
                                    [NSNumber numberWithInt:64000],                     AVEncoderBitRateKey,
                                    [NSNumber numberWithInt:2],                         AVNumberOfChannelsKey,
                                    [NSNumber numberWithInt:44100],                     AVSampleRateKey,
                                    currentChannelLayoutData,                           AVChannelLayoutKey,
                                    nil];
audioWriterInput = [AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeAudio
                                                      outputSettings:audioSettings];
audioWriterInput.expectsMediaDataInRealTime = YES; 

[assetWriter addInput:videoWriterInput];
[assetWriter addInput:audioWriterInput];
这里还有一个
avassetwriterinportpixelbufferadapter
,但为了简洁起见,我把它删掉了。我创建了一个串行调度队列,用于视频和音频写入。当我得到一个新的视频帧时,我将作业分派到该串行队列,当我得到新的音频字节时,我将作业分派到同一个串行队列。这样,大部分音频编写代码不会发生在音频回调线程上(否则会降低高优先级音频线程的速度)

我能够成功地编写带有音频的视频,并且在双核设备上表现良好。然而,在单核设备上,我发现大约每500-700毫秒,设备显示屏上就会出现明显的口吃。我已将罪犯追查到以下方面:

if ([audioWriterInput isReadyForMoreMediaData])
{
    if (![audioWriterInput appendSampleBuffer:sampleBuffer])
    {
       NSLog(@"Couldn't append audio sample buffer: %d", numAudioCallbacks_);
    }
}
如果我为
audioWriterInput
注释掉
appendSampleBuffer
,显然不会写入音频,但实际显示中也不会出现口吃。这很奇怪,因为所有这些都发生在主线程的下。每帧的CPU/GPU时间大约为4-5毫秒,因此CPU/GPU不像是瓶颈

另一个线索/好奇:当我写音频缓冲区时,我会得到一个锯齿状的内存图,如下所示:

if ([audioWriterInput isReadyForMoreMediaData])
{
    if (![audioWriterInput appendSampleBuffer:sampleBuffer])
    {
       NSLog(@"Couldn't append audio sample buffer: %d", numAudioCallbacks_);
    }
}

在我看来,audioWriterInput正在将一些缓冲区排队,然后写入/释放它们

最后一个提示是,当我录制单声道而不是立体声时,口吃似乎不那么严重。不幸的是,传入的声音是立体声的,所以当我这样做时,声音是错误的。不管怎样,口吃都不会完全消失,因此这不是一个有效的解决方案,但可能暗示立体声占用更多资源来处理


有人知道为什么用
AVAssetInputWriter
编写音频会导致整个应用程序口吃吗?这些设备显然只有一个内核,因此所有线程都将在该内核上运行,但奇怪的是,当CPU负载如此低时,主线程会断断续续。

嘿,你确定过这里的问题吗?我很想知道你是否这样做了。我也很想知道你是如何解决这个问题的。