Can I use AVCaptureSession to encode an AAC stream to memory?

前端 未结 1 1712
感情败类
感情败类 2021-01-31 12:27

I\'m writing an iOS app that streams video and audio over the network.

I am using AVCaptureSession to grab raw video frames using AVCaptureVideo

相关标签:
1条回答
  • 2021-01-31 13:30

    I ended up asking Apple for advice (it turns out you can do that if you have a paid developer account).

    It seems that AVCaptureSession grabs a hold of the AAC hardware encoder but only lets you use it to write directly to file.

    You can use the software encoder but you have to ask for it specifically instead of using AudioConverterNew:

    AudioClassDescription *description = [self
            getAudioClassDescriptionWithType:kAudioFormatMPEG4AAC
                            fromManufacturer:kAppleSoftwareAudioCodecManufacturer];
    if (!description) {
        return false;
    }
    // see the question as for setting up pcmASBD and arc ASBD
    OSStatus st = AudioConverterNewSpecific(&pcmASBD, &aacASBD, 1, description, &_converter);
    if (st) {
        NSLog(@"error creating audio converter: %s", OSSTATUS(st));
        return false;
    }
    

    with

    - (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: %s", OSSTATUS(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: %s", OSSTATUS(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;
    }
    

    The software encoder will take up CPU resources, of course, but will get the job done.

    0 讨论(0)
提交回复
热议问题