Welcome to mirror list, hosted at ThFree Co, Russian Federation.

MKAudioOutput.m « src - github.com/mumble-voip/mumblekit.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 60ef9d8f2d15b848b18d6885954494845f8586b2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
// Copyright 2005-2012 The MumbleKit Developers. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

#import "MKUtils.h"
#import "MKAudioOutput.h"
#import "MKAudioOutputSpeech.h"
#import "MKAudioOutputUser.h"
#import "MKReadWriteLock.h"

#import <AudioUnit/AudioUnit.h>
#import <AudioUnit/AUComponent.h>
#import <AudioToolbox/AudioToolbox.h>

/*
 * Output callback.
 */
static OSStatus outputCallback(void *udata, AudioUnitRenderActionFlags *flags, const AudioTimeStamp *ts,
                               UInt32 busnum, UInt32 nframes, AudioBufferList *buflist) {
    MKAudioOutput *ao = (MKAudioOutput *) udata;
    AudioBuffer *buf = buflist->mBuffers;
    MK_UNUSED OSStatus err;
    BOOL done;

    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    done = [ao mixFrames:buf->mData amount:nframes];
    if (! done) {
        /*
         * Not very obvious from the documentation, but CoreAudio simply wants you to set your buffer
         * size to 0, and return a non-zero value when you don't have anything to feed it. It will call
         * you back later.
         */
        buf->mDataByteSize = 0;
        [pool release];
        return -1;
    }

    [pool release];
    return noErr;
}

@interface MKAudioOutput () {
    MKAudioSettings _settings;
    
    AudioUnit audioUnit;
    int sampleSize;
    int frameSize;
    int mixerFrequency;
    int numChannels;
    float *speakerVolume;
    
    MKReadWriteLock *outputLock;
    NSMutableDictionary *outputs;
}
@end

@implementation MKAudioOutput

- (id) initWithSettings:(MKAudioSettings *)settings {
    self = [super init];
    if (self == nil)
        return nil;

    // Copy settings
    memcpy(&_settings, settings, sizeof(MKAudioSettings));

    sampleSize = 0;
    frameSize = SAMPLE_RATE / 100;
    mixerFrequency = 0;

    outputLock = [[MKReadWriteLock alloc] init];
    outputs = [[NSMutableDictionary alloc] init];

    return self;
}

- (void) dealloc {
    // fixme(mkrautz): Return value?
    [self teardownDevice];

    [outputLock release];
    [outputs release];

    [super dealloc];
}

- (BOOL) setupDevice {
    UInt32 len;
    OSStatus err;
    AudioComponent comp;
    AudioComponentDescription desc;
    AudioStreamBasicDescription fmt;

    desc.componentType = kAudioUnitType_Output;
#if TARGET_OS_IPHONE == 1
    desc.componentSubType = kAudioUnitSubType_RemoteIO;
#elif TARGET_OS_MAC == 1
    desc.componentSubType = kAudioUnitSubType_HALOutput;
#endif
    desc.componentManufacturer = kAudioUnitManufacturer_Apple;
    desc.componentFlags = 0;
    desc.componentFlagsMask = 0;

    comp = AudioComponentFindNext(NULL, &desc);
    if (! comp) {
        NSLog(@"MKAudioOutput: Unable to find AudioUnit.");
        return NO;
    }

    err = AudioComponentInstanceNew(comp, (AudioComponentInstance *) &audioUnit);
    if (err != noErr) {
        NSLog(@"MKAudioOutput: Unable to instantiate new AudioUnit.");
        return NO;
    }

    err = AudioUnitInitialize(audioUnit);
    if (err != noErr) {
        NSLog(@"MKAudioOutput: Unable to initialize AudioUnit.");
        return NO;
    }

    len = sizeof(AudioStreamBasicDescription);
    err = AudioUnitGetProperty(audioUnit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, 0, &fmt, &len);
    if (err != noErr) {
        NSLog(@"MKAudioOuptut: Unable to get output stream format from AudioUnit.");
        return NO;
    }

    mixerFrequency = (int) 48000;
    numChannels = (int) fmt.mChannelsPerFrame;
    sampleSize = numChannels * sizeof(short);

    if (speakerVolume) {
        free(speakerVolume);
    }
    speakerVolume = malloc(sizeof(float)*numChannels);

    int i;
    for (i = 0; i < numChannels; ++i) {
        speakerVolume[i] = 1.0f;
    }

    fmt.mFormatFlags = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked;
    fmt.mBitsPerChannel = sizeof(short) * 8;

    NSLog(@"MKAudioOutput: Output device currently configured as %iHz sample rate, %i channels, %i sample size", mixerFrequency, numChannels, sampleSize);

    fmt.mFormatID = kAudioFormatLinearPCM;
    fmt.mSampleRate = mixerFrequency;
    fmt.mBytesPerFrame = sampleSize;
    fmt.mBytesPerPacket = sampleSize;
    fmt.mFramesPerPacket = 1;

    len = sizeof(AudioStreamBasicDescription);
    err = AudioUnitSetProperty(audioUnit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, 0, &fmt, len);
    if (err != noErr) {
        NSLog(@"MKAudioOutput: Unable to set stream format for output device.");
        return NO;
    }

    AURenderCallbackStruct cb;
    cb.inputProc = outputCallback;
    cb.inputProcRefCon = self;
    len = sizeof(AURenderCallbackStruct);
    err = AudioUnitSetProperty(audioUnit, kAudioUnitProperty_SetRenderCallback, kAudioUnitScope_Global, 0, &cb, len);
    if (err != noErr) {
        NSLog(@"MKAudioOutput: Could not set render callback.");
        return NO;
    }

    /* On desktop we call AudioDeviceSetProperty() with kAudioDevicePropertyBufferFrameSize to set our frame size up. */

    err = AudioOutputUnitStart(audioUnit);
    if (err != noErr) {
        NSLog(@"MKAudioOutput: Unable to start AudioUnit");
        return NO;
    }

    return YES;
}

- (BOOL) teardownDevice {
    OSStatus err = AudioOutputUnitStop(audioUnit);
    if (err != noErr) {
        NSLog(@"MKAudioOutput: Unable to stop AudioUnit.");
        return NO;
    }

    NSLog(@"MKAudioOuptut: Teardown finished.");
    return YES;
}

- (BOOL) mixFrames:(void *)frames amount:(unsigned int)nsamp {
    unsigned int i, s;
    BOOL retVal = NO;

    /*
     * The real mixer:
     */
    NSMutableArray *mix = [[NSMutableArray alloc] init];
    NSMutableArray *del = [[NSMutableArray alloc] init];
    unsigned int nchan = numChannels;

    /* if volume is too low, skip. */

    [outputLock readLock];
    for (NSNumber *sessionKey in outputs) {
        MKAudioOutputUser *ou = [outputs objectForKey:sessionKey];
        if (! [ou needSamples:nsamp]) {
            [del addObject:ou];
        } else {
            [mix addObject:ou];
        }
    }

    float *mixBuffer = alloca(sizeof(float)*numChannels*nsamp);
    memset(mixBuffer, 0, sizeof(float)*numChannels*nsamp);

    if ([mix count] > 0) {
        for (MKAudioOutputUser *ou in mix) {
            const float * restrict userBuffer = [ou buffer];
            for (s = 0; s < nchan; ++s) {
                const float str = speakerVolume[s];
                float * restrict o = (float *)mixBuffer + s;
                for (i = 0; i < nsamp; ++i) {
                    o[i*nchan] += userBuffer[i] * str;
                }
            }
        }

        short *outputBuffer = (short *)frames;
        for (i = 0; i < nsamp * numChannels; ++i) {
            if (mixBuffer[i] > 1.0f) {
                outputBuffer[i] = 32768;
            } else if (mixBuffer[i] < -1.0f) {
                outputBuffer[i] = -32768;
            } else {
                outputBuffer[i] = mixBuffer[i] * 32768.0f;
            }
        }
    } else {
        memset((short *)frames, 0, nsamp * numChannels);
    }

    [outputLock unlock];

    for (MKAudioOutputUser *ou in del) {
        [self removeBuffer:ou];
    }

    retVal = [mix count] > 0;

    [mix release];
    [del release];

    return retVal;
}

- (void) removeBuffer:(MKAudioOutputUser *)u {
    // hack(mkrautz): output sources should be a subclass, but should implement a protocol instead.
    if ([u respondsToSelector:@selector(userSession)]) {
        [outputLock writeLock];
        [outputs removeObjectForKey:[NSNumber numberWithUnsignedInt:[(id)u userSession]]];
        [outputLock unlock];
    }
}

- (void) addFrameToBufferWithSession:(NSUInteger)session data:(NSData *)data sequence:(NSUInteger)seq type:(MKUDPMessageType)msgType {
    if (numChannels == 0)
        return;

    [outputLock readLock];
    MKAudioOutputSpeech *outputUser = [outputs objectForKey:[NSNumber numberWithUnsignedInt:session]];
    [outputLock unlock];

    if (outputUser == nil || [outputUser messageType] != msgType) {
        if (outputUser != nil) {
            [self removeBuffer:outputUser];
        }
        outputUser = [[MKAudioOutputSpeech alloc] initWithSession:session sampleRate:mixerFrequency messageType:msgType];
        [outputLock writeLock];
        [outputs setObject:outputUser forKey:[NSNumber numberWithUnsignedInt:session]];
        [outputLock unlock];
        [outputUser release];
    }

    [outputUser addFrame:data forSequence:seq];
}

@end