实时使用AVAssetReader和timeRange读取样本

3
之前我使用CMSampleBufferGetAudioBufferListWithRetainedBlockBuffer从完整的音频文件中读取音频样本。现在我想使用时间范围(即我指定时间范围..按照时间读取小块音频,然后返回并再次读取)来做同样的事情。我想使用时间范围的原因是我想控制每个读取的大小(以适应最大大小的数据包)。
由于某种原因,在每次读取之间总会有一个颠簸。在我的代码中,您会注意到我每次设置时间范围时都会启动和结束AVAssetReader,这是因为在阅读器启动后我无法动态调整时间范围(有关更多详细信息,请参见此处)。
难道启动和关闭阅读器只是为了产生连续的实时体验而太昂贵了吗?还是有其他我不知道的方法可以做到这一点?
此外,请注意,无论我将时间间隔设置为什么点,都会出现这种抖动或滞后。这让我相信,以我目前的方式启动和结束读取器对于实时音频播放来说过于昂贵。
- (void) setupReader 
{
    NSURL *assetURL = [NSURL URLWithString:@"ipod-library://item/item.m4a?id=1053020204400037178"];   
    songAsset = [AVURLAsset URLAssetWithURL:assetURL options:nil];

    track = [songAsset.tracks objectAtIndex:0];     
    nativeTrackASBD = [self getTrackNativeSettings:track];

    // set CM time parameters
    assetCMTime = songAsset.duration;
    CMTimeReadDurationInSeconds = CMTimeMakeWithSeconds(1, assetCMTime.timescale);
    currentCMTime = CMTimeMake(0,assetCMTime.timescale); 
}

-(void)readVBRPackets
{
    // make sure assetCMTime is greater than currentCMTime
    while (CMTimeCompare(assetCMTime,currentCMTime) == 1 )
    {
        NSError * error = nil;
        reader = [[AVAssetReader alloc] initWithAsset:songAsset error:&error];
        readerOutput = [AVAssetReaderTrackOutput assetReaderTrackOutputWithTrack:track
                                                                  outputSettings:nil];

        [reader addOutput:readerOutput];
        reader.timeRange = CMTimeRangeMake(currentCMTime, CMTimeReadDurationInSeconds);

        [reader startReading];

        while ((sample = [readerOutput copyNextSampleBuffer])) {
            CMItemCount numSamples = CMSampleBufferGetNumSamples(sample);
            if (numSamples == 0) {
                continue;
            }

            NSLog(@"reading sample");               

            CMBlockBufferRef CMBuffer = CMSampleBufferGetDataBuffer( sample );                                                         
            AudioBufferList audioBufferList;  

            OSStatus err = CMSampleBufferGetAudioBufferListWithRetainedBlockBuffer(
                                                                               sample,
                                                                               NULL,
                                                                               &audioBufferList,
                                                                               sizeof(audioBufferList),
                                                                               NULL,
                                                                               NULL,
                                                                               kCMSampleBufferFlag_AudioBufferList_Assure16ByteAlignment,
                                                                               &CMBuffer
                                                                                   );



            const AudioStreamPacketDescription   * inPacketDescriptions;
            size_t                               packetDescriptionsSizeOut;
            size_t inNumberPackets;

            CheckError(CMSampleBufferGetAudioStreamPacketDescriptionsPtr(sample, 
                                                                         &inPacketDescriptions,
                                                                         &packetDescriptionsSizeOut),
                       "could not read sample packet descriptions");

            inNumberPackets = packetDescriptionsSizeOut/sizeof(AudioStreamPacketDescription);

            AudioBuffer audioBuffer = audioBufferList.mBuffers[0];


            for (int i = 0; i < inNumberPackets; ++i)
            {

                SInt64 dataOffset = inPacketDescriptions[i].mStartOffset;
                UInt32 packetSize   = inPacketDescriptions[i].mDataByteSize;            

                size_t packetSpaceRemaining;
                packetSpaceRemaining = bufferByteSize - bytesFilled;

                // if the space remaining in the buffer is not 
                // enough for the data contained in this packet
                // then just write it
                if (packetSpaceRemaining < packetSize)
                {
                    [self enqueueBuffer];           
                }

                // copy data to the audio queue buffer
                AudioQueueBufferRef fillBuf = audioQueueBuffers[fillBufferIndex];
                memcpy((char*)fillBuf->mAudioData + bytesFilled, 
                       (const char*)(audioBuffer.mData + dataOffset), packetSize);                                                                

                // fill out packet description
                packetDescs[packetsFilled] = inPacketDescriptions[i];
                packetDescs[packetsFilled].mStartOffset = bytesFilled;

                bytesFilled += packetSize;
                packetsFilled += 1;

                // if this is the last packet, then ship it
                size_t packetsDescsRemaining = kAQMaxPacketDescs - packetsFilled;
                if (packetsDescsRemaining == 0) {          
                    [self enqueueBuffer];              
                }                  
            }

            CFRelease(CMBuffer);
            CMSampleBufferInvalidate(sample);
            CFRelease(sample);
        }

        [reader cancelReading];
        reader = NULL;
        readerOutput = NULL;

        currentCMTime = CMTimeAdd(currentCMTime, CMTimeReadDurationInSeconds);
    }


}

1
感谢这段有用的代码!但是我发现其中一个 bug:如果 (numSamples == 0) 测试成功,那么 continue 将导致 "sample" 值永远不会被释放,造成内存泄漏和其他神秘问题。 :-) - Christopher Schardt
1个回答

5
我知道发生了什么 :-D 我花了将近一整天的时间才弄清楚。
实际上,AVAssetReader会在前1024个样本(可能略多一些)中淡出。这就是为什么会听到抖动效果。
我通过在真正想要读取的位置之前读取1024个样本,然后跳过那1024个样本来解决了这个问题。
希望这对你也有用。

你好,你能否详细说明一下你是如何克服这个错误的? - Hima

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接