2013-05-07 7 views
24

मैं आईओएस 6.1.3 iPad2 और नए iPad पर एक एसआईपी ऑडियो स्ट्रीमिंग अनुप्रयोग चल रहा हूँ रहे हैं।iOS ऐप्लिकेशन क्रैश हेडफ़ोन या में खामियों को दूर अनप्लग

मैं अपने आईपैड पर मेरे ऐप शुरू (कुछ भी नहीं में खामियों को दूर)।
ऑडियो काम करता है।
मैं हेडफ़ोन प्लग करता हूं।
ऐप्लिकेशन क्रैश: malloc: वस्तु 0x के लिए त्रुटि ....: सूचक मुक्त कर दिया जा रहा है आवंटित नहीं किया गया था या EXC_BAD_ACCESS

वैकल्पिक रूप से:

मैं (मेरे iPad पर मेरे ऐप शुरू हेडफोन के साथ में खामियों को दूर)।
ऑडियो हेडफोन से बाहर आता है।
मैं हेडफ़ोन अनप्लग करता हूं।
ऐप्लिकेशन क्रैश: malloc: वस्तु 0x के लिए त्रुटि ....: सूचक मुक्त कर दिया जा रहा है आवंटित नहीं किया गया था या EXC_BAD_ACCESS

एप्लिकेशन कोड http://code.google.com/p/ios-coreaudio-example/ नमूना कोड (नीचे देखें) के आधार पर AudioUnit एपीआई कार्यरत हैं।

मैं परिवर्तन जागरूकता प्राप्त करने के लिए एक KAudioSessionProperty_AudioRoute बदलें कॉलबैक का उपयोग करता हूं। तो वहाँ ओएस ध्वनि प्रबंधक इधर-उधर तीन कॉलबैक हैं:
1) प्रक्रिया दर्ज की गई माइक नमूने
2) वक्ता
3) को सूचित करें ऑडियो HW उपस्थिति

परीक्षण के बहुत सारे के बाद के लिए नमूने प्रदान करें मेरी भावना यह है कि मुश्किल कोड वह है जो माइक कैप्चर करता है। प्लग/अनप्लग कार्रवाई के बाद, समय की सबसे रिकॉर्डिंग कॉलबैक बुलाया जा रहा है RouteChange से पहले कुछ समय के बाद 'विभाजन गलती' और RouteChange कॉलबैक कभी नहीं कहा जाता है के कारण कहा जाता है। अधिक विशिष्ट होने के नाते मुझे लगता है कि AudioUnitRender फ़ंक्शन 'स्मृति खराब पहुंच' का कारण बनता है जबकि अपवाद को बिल्कुल नहीं फेंक दिया जाता है।

मेरे भावना है कि उपकरणों ध्वनि से संबंधित संरचनाओं के ओएस अद्यतन करने के साथ एक गैर-परमाणु रिकॉर्डिंग कॉलबैक कोड दौड़। तो रिकॉर्डिंग कॉलबैक जितना अधिक गैर-परमाणु है ओएस एचडब्ल्यू अपडेट और रिकॉर्डिंग कॉलबैक की समेकन की संभावना अधिक है।

मैं रिकॉर्डिंग कॉलबैक संभव के रूप में पतली छोड़ने के लिए मेरे कोड को संशोधित किया लेकिन मेरे लग रहा है कि उच्च प्रसंस्करण भार मेरे ऐप्लिकेशन के अन्य थ्रेड द्वारा लाया संगामिति दौड़ से पहले वर्णित खिला रहा है। तो AudioUnitRender खराब पहुंच के कारण कोड के अन्य हिस्सों में मॉलोक/फ्री त्रुटि बढ़ती है।

मैं द्वारा रिकॉर्डिंग कॉलबैक प्रतीक्षा अवधि को कम करने की कोशिश की:

UInt32 numFrames = 256; 
UInt32 dataSize = sizeof(numFrames); 

AudioUnitSetProperty(audioUnit, 
    kAudioUnitProperty_MaximumFramesPerSlice, 
    kAudioUnitScope_Global, 
    0, 
    &numFrames, 
    dataSize); 

और मैं समस्याग्रस्त कोड को बढ़ावा देने की कोशिश की:

dispatch_async(dispatch_get_main_queue(), ^{ 

कोई भी व्यक्ति उस के लिए एक टिप या समाधान है करता है?

// 
// IosAudioController.m 
// Aruts 
// 
// Created by Simon Epskamp on 10/11/10. 
// Copyright 2010 __MyCompanyName__. All rights reserved. 
// 

#import "IosAudioController.h" 
#import <AudioToolbox/AudioToolbox.h> 

#define kOutputBus 0 
#define kInputBus 1 

IosAudioController* iosAudio; 

void checkStatus(int status) { 
    if (status) { 
     printf("Status not 0! %d\n", status); 
     // exit(1); 
    } 
} 

/** 
* This callback is called when new audio data from the microphone is available. 
*/ 
static OSStatus recordingCallback(void *inRefCon, 
    AudioUnitRenderActionFlags *ioActionFlags, 
    const AudioTimeStamp *inTimeStamp, 
    UInt32 inBusNumber, 
    UInt32 inNumberFrames, 
    AudioBufferList *ioData) { 

    // Because of the way our audio format (setup below) is chosen: 
    // we only need 1 buffer, since it is mono 
    // Samples are 16 bits = 2 bytes. 
    // 1 frame includes only 1 sample 

    AudioBuffer buffer; 

    buffer.mNumberChannels = 1; 
    buffer.mDataByteSize = inNumberFrames * 2; 
    buffer.mData = malloc(inNumberFrames * 2); 

    // Put buffer in a AudioBufferList 
    AudioBufferList bufferList; 
    bufferList.mNumberBuffers = 1; 
    bufferList.mBuffers[0] = buffer; 

    NSLog(@"Recording Callback 1 0x%x ? 0x%x",buffer.mData, 
     bufferList.mBuffers[0].mData); 

    // Then: 
    // Obtain recorded samples 

    OSStatus status; 
    status = AudioUnitRender([iosAudio audioUnit], 
     ioActionFlags, 
     inTimeStamp, 
     inBusNumber, 
     inNumberFrames, 
     &bufferList); 
     checkStatus(status); 

    // Now, we have the samples we just read sitting in buffers in bufferList 
    // Process the new data 
    [iosAudio processAudio:&bufferList]; 

    NSLog(@"Recording Callback 2 0x%x ? 0x%x",buffer.mData, 
     bufferList.mBuffers[0].mData); 

    // release the malloc'ed data in the buffer we created earlier 
    free(bufferList.mBuffers[0].mData); 

    return noErr; 
} 

/** 
* This callback is called when the audioUnit needs new data to play through the 
* speakers. If you don't have any, just don't write anything in the buffers 
*/ 
static OSStatus playbackCallback(void *inRefCon, 
    AudioUnitRenderActionFlags *ioActionFlags, 
    const AudioTimeStamp *inTimeStamp, 
    UInt32 inBusNumber, 
    UInt32 inNumberFrames, 
    AudioBufferList *ioData) { 
     // Notes: ioData contains buffers (may be more than one!) 
     // Fill them up as much as you can. 
     // Remember to set the size value in each 
     // buffer to match how much data is in the buffer. 

    for (int i=0; i < ioData->mNumberBuffers; i++) { 
     // in practice we will only ever have 1 buffer, since audio format is mono 
     AudioBuffer buffer = ioData->mBuffers[i]; 

     // NSLog(@" Buffer %d has %d channels and wants %d bytes of data.", i, 
      buffer.mNumberChannels, buffer.mDataByteSize); 

     // copy temporary buffer data to output buffer 
     UInt32 size = min(buffer.mDataByteSize, 
      [iosAudio tempBuffer].mDataByteSize); 

     // dont copy more data then we have, or then fits 
     memcpy(buffer.mData, [iosAudio tempBuffer].mData, size); 
     // indicate how much data we wrote in the buffer 
     buffer.mDataByteSize = size; 

     // uncomment to hear random noise 
     /* 
     * UInt16 *frameBuffer = buffer.mData; 
     * for (int j = 0; j < inNumberFrames; j++) { 
     *  frameBuffer[j] = rand(); 
     * } 
     */ 
    } 

    return noErr; 
} 

@implementation IosAudioController 
@synthesize audioUnit, tempBuffer; 

void propListener(void *inClientData, 
    AudioSessionPropertyID inID, 
    UInt32 inDataSize, 
    const void *inData) { 

    if (inID == kAudioSessionProperty_AudioRouteChange) { 

     UInt32 isAudioInputAvailable; 
     UInt32 size = sizeof(isAudioInputAvailable); 
     CFStringRef newRoute; 
     size = sizeof(CFStringRef); 

     AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &size, &newRoute); 

     if (newRoute) { 
      CFIndex length = CFStringGetLength(newRoute); 
      CFIndex maxSize = CFStringGetMaximumSizeForEncoding(length, 
       kCFStringEncodingUTF8); 

      char *buffer = (char *)malloc(maxSize); 
      CFStringGetCString(newRoute, buffer, maxSize, 
       kCFStringEncodingUTF8); 

      //CFShow(newRoute); 
      printf("New route is %s\n",buffer); 

      if (CFStringCompare(newRoute, CFSTR("HeadsetInOut"), NULL) == 
       kCFCompareEqualTo) // headset plugged in 
      { 
       printf("Headset\n"); 
      } else { 
       printf("Another device\n"); 

       UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker; 
       AudioSessionSetProperty(kAudioSessionProperty_OverrideAudioRoute, 
        sizeof (audioRouteOverride),&audioRouteOverride); 
      } 
      printf("New route is %s\n",buffer); 
      free(buffer); 
     } 
     newRoute = nil; 
    } 
} 

/** 
* Initialize the audioUnit and allocate our own temporary buffer. 
* The temporary buffer will hold the latest data coming in from the microphone, 
* and will be copied to the output when this is requested. 
*/ 
- (id) init { 
    self = [super init]; 
    OSStatus status; 

    // Initialize and configure the audio session 
    AudioSessionInitialize(NULL, NULL, NULL, self); 

    UInt32 audioCategory = kAudioSessionCategory_PlayAndRecord; 
    AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, 
     sizeof(audioCategory), &audioCategory); 
    AudioSessionAddPropertyListener(kAudioSessionProperty_AudioRouteChange, 
     propListener, self); 

    Float32 preferredBufferSize = .020; 
    AudioSessionSetProperty(kAudioSessionProperty_PreferredHardwareIOBufferDuration, 
     sizeof(preferredBufferSize), &preferredBufferSize); 

    AudioSessionSetActive(true); 

    // Describe audio component 
    AudioComponentDescription desc; 
    desc.componentType = kAudioUnitType_Output; 
    desc.componentSubType = 
     kAudioUnitSubType_VoiceProcessingIO/*kAudioUnitSubType_RemoteIO*/; 
    desc.componentFlags = 0; 
    desc.componentFlagsMask = 0; 
    desc.componentManufacturer = kAudioUnitManufacturer_Apple; 

    // Get component 
    AudioComponent inputComponent = AudioComponentFindNext(NULL, &desc); 

    // Get audio units 
    status = AudioComponentInstanceNew(inputComponent, &audioUnit); 
    checkStatus(status); 

    // Enable IO for recording 
    UInt32 flag = 1; 
    status = AudioUnitSetProperty(audioUnit, 
     kAudioOutputUnitProperty_EnableIO, 
     kAudioUnitScope_Input, 
     kInputBus, 
     &flag, 
     sizeof(flag)); 
     checkStatus(status); 

    // Enable IO for playback 
    flag = 1; 
    status = AudioUnitSetProperty(audioUnit, 
     kAudioOutputUnitProperty_EnableIO, 
     kAudioUnitScope_Output, 
     kOutputBus, 
     &flag, 
     sizeof(flag)); 

    checkStatus(status); 

    // Describe format 
    AudioStreamBasicDescription audioFormat; 
    audioFormat.mSampleRate = 8000.00; 
    //audioFormat.mSampleRate = 44100.00; 
    audioFormat.mFormatID = kAudioFormatLinearPCM; 
    audioFormat.mFormatFlags = 
     kAudioFormatFlagsCanonical/* kAudioFormatFlagIsSignedInteger | 
     kAudioFormatFlagIsPacked*/; 
    audioFormat.mFramesPerPacket = 1; 
    audioFormat.mChannelsPerFrame = 1; 
    audioFormat.mBitsPerChannel = 16; 
    audioFormat.mBytesPerPacket = 2; 
    audioFormat.mBytesPerFrame = 2; 

    // Apply format 
    status = AudioUnitSetProperty(audioUnit, 
     kAudioUnitProperty_StreamFormat, 
     kAudioUnitScope_Output, 
     kInputBus, 
     &audioFormat, 
     sizeof(audioFormat)); 

    checkStatus(status); 
    status = AudioUnitSetProperty(audioUnit, 
     kAudioUnitProperty_StreamFormat, 
     kAudioUnitScope_Input, 
     kOutputBus, 
     &audioFormat, 
     sizeof(audioFormat)); 

    checkStatus(status); 


    // Set input callback 
    AURenderCallbackStruct callbackStruct; 
    callbackStruct.inputProc = recordingCallback; 
    callbackStruct.inputProcRefCon = self; 
    status = AudioUnitSetProperty(audioUnit, 
     AudioOutputUnitProperty_SetInputCallback, 
     kAudioUnitScope_Global, 
     kInputBus, 
     &callbackStruct, 
     sizeof(callbackStruct)); 

    checkStatus(status); 
    // Set output callback 
    callbackStruct.inputProc = playbackCallback; 
    callbackStruct.inputProcRefCon = self; 
    status = AudioUnitSetProperty(audioUnit, 
     kAudioUnitProperty_SetRenderCallback, 
     kAudioUnitScope_Global, 
     kOutputBus, 
     &callbackStruct, 
     sizeof(callbackStruct)); 

    checkStatus(status); 

    // Disable buffer allocation for the recorder (optional - do this if we want to 
    // pass in our own) 

    flag = 0; 
    status = AudioUnitSetProperty(audioUnit, 
     kAudioUnitProperty_ShouldAllocateBuffer, 
     kAudioUnitScope_Output, 
     kInputBus, 
     &flag, 
     sizeof(flag)); 


    flag = 0; 
    status = AudioUnitSetProperty(audioUnit, 
    kAudioUnitProperty_ShouldAllocateBuffer, 
     kAudioUnitScope_Output, 
     kOutputBus, 
     &flag, 
     sizeof(flag)); 

    // Allocate our own buffers (1 channel, 16 bits per sample, thus 16 bits per 
    // frame, thus 2 bytes per frame). 
    // Practice learns the buffers used contain 512 frames, 
    // if this changes it will be fixed in processAudio. 
    tempBuffer.mNumberChannels = 1; 
    tempBuffer.mDataByteSize = 512 * 2; 
    tempBuffer.mData = malloc(512 * 2); 

    // Initialise 
    status = AudioUnitInitialize(audioUnit); 
    checkStatus(status); 

    return self; 
} 

/** 
* Start the audioUnit. This means data will be provided from 
* the microphone, and requested for feeding to the speakers, by 
* use of the provided callbacks. 
*/ 
- (void) start { 
    OSStatus status = AudioOutputUnitStart(audioUnit); 
    checkStatus(status); 
} 

/** 
* Stop the audioUnit 
*/ 
- (void) stop { 
    OSStatus status = AudioOutputUnitStop(audioUnit); 
    checkStatus(status); 
} 

/** 
* Change this function to decide what is done with incoming 
* audio data from the microphone. 
* Right now we copy it to our own temporary buffer. 
*/ 
- (void) processAudio: (AudioBufferList*) bufferList { 
    AudioBuffer sourceBuffer = bufferList->mBuffers[0]; 

    // fix tempBuffer size if it's the wrong size 
    if (tempBuffer.mDataByteSize != sourceBuffer.mDataByteSize) { 
     free(tempBuffer.mData); 
     tempBuffer.mDataByteSize = sourceBuffer.mDataByteSize; 
     tempBuffer.mData = malloc(sourceBuffer.mDataByteSize); 
    } 

    // copy incoming audio data to temporary buffer 
    memcpy(tempBuffer.mData, bufferList->mBuffers[0].mData, 
     bufferList->mBuffers[0].mDataByteSize); 
    usleep(1000000); // <- TO REPRODUCE THE ERROR, CONCURRENCY MORE LIKELY 

} 

/** 
* Clean up. 
*/ 
- (void) dealloc { 
    [super dealloc]; 
    AudioUnitUninitialize(audioUnit); 
    free(tempBuffer.mData); 
} 

@end 
+1

क्या आप 'malloc_error_break' पर एक ब्रेकपाइंट जोड़ने का प्रयास करें - यह आप सूचक है कि दो बार मुक्त किया जा रहा है देना चाहिए। – Mar0ux

+2

क्या आप 'char * buffer = (char *) malloc (maxSize) लीक कर रहे हैं;'? आपको उस कोड में से किसी भी कोड की आवश्यकता नहीं है - 'CFStringRef' 'NSString' के लिए एक टोल-फ्री पुल है, इसलिए आप' NSString * 'पर बस' newRoute' टाइप कर सकते हैं और' NSString' विधियों का उपयोग कर सकते हैं। – Mar0ux

+1

मैं परीक्षण कोड में शामिल करना भूल गया था, मैंने आपके द्वारा @ Mar0ux हाइलाइट किए गए मुद्दों पर फ़िक्स प्रदान किए हैं, लेकिन मेरे ऐप में वे पहले ही तय किए गए थे ('फ्री (बफर); 'और' newRoute = nil;')। हमेशा नहीं त्रुटि _malloc है: * ऑब्जेक्ट 0x के लिए त्रुटि ....: पॉइंटर को मुक्त किया जा रहा था आवंटित नहीं किया गया था, अन्य बार त्रुटि एक memcpy में EXC_BAD_ACCESS है। –

उत्तर

8

मेरी परीक्षण के अनुसार, लाइन है कि SEGV त्रुटि से चलाता है अंततः

AudioSessionSetProperty(kAudioSessionProperty_OverrideAudioRoute, 
            sizeof (audioRouteOverride),&audioRouteOverride); 

एक AudioUnit श्रृंखला के मध्य के गुणों बदल रहा है: आदेश यहाँ त्रुटि पुन: पेश करने में मेरे ऑडियो सत्र कोड है -फ्लैट हमेशा मुश्किल है, लेकिन अगर आप रीरउटिंग से पहले ऑडियोयूनीट को रोकते हैं, और फिर से शुरू करते हैं, तो यह संग्रहीत सभी बफर का उपयोग करके समाप्त होता है और फिर नए पैरामीटर के साथ चलता है।

क्या यह स्वीकार्य होगा, या क्या आपको मार्ग के परिवर्तन और रिकॉर्डिंग के पुनरारंभ के बीच एक अंतर की आवश्यकता है?

मैं क्या किया था:

void propListener(void *inClientData, 
       AudioSessionPropertyID inID, 
       UInt32 inDataSize, 
       const void *inData) { 

[iosAudio stop]; 
// ... 

[iosAudio start]; 
} 

अपने iPhone 5 पर कोई और अधिक दुर्घटना (अपने लाभ विभिन्न हार्डवेयर के साथ भिन्न हो सकते हैं)

सबसे तार्किक व्याख्या मुझे लगता है कि व्यवहार के लिए है, कुछ हद तक इन के द्वारा समर्थित परीक्षण, यह है कि रेंडर पाइप असीमित है। यदि आप अपने बफर में हेरफेर करने के लिए हमेशा के लिए लेते हैं, तो वे बस कतार में रहते हैं। लेकिन अगर आप AudioUnit की सेटिंग्स बदलते हैं, तो आप अज्ञात साइड इफेक्ट्स के साथ रेंडर कतार में एक बड़े पैमाने पर रीसेट ट्रिगर करते हैं। समस्या यह है कि, ये परिवर्तन तुल्यकालिक हैं, जो एक रेट्रोएक्टिव तरीके से प्रभावित होते हैं जो सभी अतुल्यकालिक कॉल उनकी बारी के लिए धैर्यपूर्वक प्रतीक्षा करते हैं।

अगर तुम्हें याद किया नमूने के बारे में परवाह नहीं है, आप कुछ की तरह कर सकते हैं:

static BOOL isStopped = NO; 
static OSStatus recordingCallback(void *inRefCon, //... 
{ 
    if(isStopped) { 
    NSLog(@"Stopped, ignoring"); 
    return noErr; 
    } 
    // ... 
} 

static OSStatus playbackCallback(void *inRefCon, //... 
{ 
    if(isStopped) { 
    NSLog(@"Stopped, ignoring"); 
    return noErr; 
    } 
    // ... 
} 

// ... 

/** 
* Start the audioUnit. This means data will be provided from 
* the microphone, and requested for feeding to the speakers, by 
* use of the provided callbacks. 
*/ 
- (void) start { 
    OSStatus status = AudioOutputUnitStart(_audioUnit); 
    checkStatus(status); 

    isStopped = NO; 
} 

/** 
* Stop the audioUnit 
*/ 
- (void) stop { 

    isStopped = YES; 

    OSStatus status = AudioOutputUnitStop(_audioUnit); 
    checkStatus(status); 
} 

// ... 
+0

धन्यवाद @krug, आपका प्रस्ताव इस कोड के मुद्दे को ठीक करता है। हालांकि, मेरे ऐप में रिकॉर्डिंग और प्लेबैक खेलने को प्लग/अनप्लग गतिविधि के बाद 3-4 बार कहा जाता है और 'प्रोपलिस्टर' कॉलबैक कहा जाता है। इन रिकॉर्डिंग के अंदर और कॉलबैक खेलना कुछ बाहरी संरचनाओं पॉइंटर्स को लगातार खराब मानों में संशोधित किया जाता है जब तक कि 'प्रोपलिस्टर' कॉलबैक नहीं कहा जाता है, जब उन पॉइंटर्स का संशोधन बंद हो जाता है, लेकिन क्षति पहले ही हो चुकी है। तो जब संचार बाहरी संरचनाओं को नष्ट कर देता है तो संचार के अंत में बाद में आतंक सूचक को मुक्त किया जाता है। –

+1

एक समाधान हो सकता है कि एक म्यूटेक्स बंद होने के बाद प्रक्रिया को लॉक कर दे, किसी भी नमूने को संसाधित करने के बाद अनलॉक किया जा सके? – krug

+0

हाय @ क्रग, मैंने पहले से ही रिकॉर्डिंग की रक्षा करने और '@ सिंक्रनाइज़ (iosAudio) {...} '(एक म्यूटेक्स की तरह) के साथ कॉलबैक खेलने की कोशिश की है, मैंने रिकॉर्डिंग में सिंक ब्लॉक रखा है और आपके पहले कॉलबैक खेलना है उत्तर परिवर्तन एक ही परिणाम के साथ शामिल हैं। –

संबंधित मुद्दे