Skip to content

Commit cc15c67

Browse files
authored
fix bluetooth headset crash (#121)
1 parent 411bb9f commit cc15c67

5 files changed

Lines changed: 43 additions & 6 deletions

File tree

local_pod_repo/objcTox/Classes/Private/Manager/Audio/OCTAudioEngine+Private.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
extern int kBufferLength;
1111
extern int kNumberOfChannels;
1212
extern int kDefaultSampleRate;
13-
extern int kSampleCount;
13+
extern int kSampleCount_incoming_audio;
14+
extern int kSampleCount_outgoing_audio;
1415
extern int kBitsPerByte;
1516
extern int kFramesPerPacket;
1617
extern int kBytesPerSample;

local_pod_repo/objcTox/Classes/Private/Manager/Audio/OCTAudioEngine.m

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ - (BOOL)startAudioFlow:(NSError **)error
112112
OCTAudioEngine *aoi = welf;
113113

114114
if (aoi.enableMicrophone) {
115+
// TOXAUDIO: -outgoing-audio-
115116
[aoi.toxav sendAudioFrame:data
116117
sampleCount:samples
117118
channels:channelCount
@@ -120,6 +121,8 @@ - (BOOL)startAudioFlow:(NSError **)error
120121
error:nil];
121122
}
122123
};
124+
125+
// TOXAUDIO: -incoming-audio-
123126
[self.outputQueue updateSampleRate:self.outputSampleRate numberOfChannels:self.outputNumberOfChannels error:nil];
124127

125128
if (! [self.inputQueue begin:error] || ! [self.outputQueue begin:error]) {
@@ -149,6 +152,7 @@ - (BOOL)stopAudioFlow:(NSError **)error
149152

150153
- (void)provideAudioFrames:(OCTToxAVPCMData *)pcm sampleCount:(OCTToxAVSampleCount)sampleCount channels:(OCTToxAVChannels)channels sampleRate:(OCTToxAVSampleRate)sampleRate fromFriend:(OCTToxFriendNumber)friendNumber
151154
{
155+
// TOXAUDIO: -incoming-audio-
152156
int32_t len = (int32_t)(channels * sampleCount * sizeof(OCTToxAVPCMData));
153157
TPCircularBuffer *buf = [self.outputQueue getBufferPointer];
154158
if (buf) {
@@ -174,7 +178,9 @@ - (void)makeQueues:(NSError **)error
174178
// Note: OCTAudioQueue handles the case where the device ids are nil - in that case
175179
// we don't set the device explicitly, and the default is used.
176180
#if TARGET_OS_IPHONE
181+
// TOXAUDIO: -incoming-audio-
177182
self.outputQueue = [[OCTAudioQueue alloc] initWithOutputDeviceID:nil error:error];
183+
// TOXAUDIO: -outgoing-audio-
178184
self.inputQueue = [[OCTAudioQueue alloc] initWithInputDeviceID:nil error:error];
179185
#else
180186
self.outputQueue = [[OCTAudioQueue alloc] initWithOutputDeviceID:self.outputDeviceID error:error];

local_pod_repo/objcTox/Classes/Private/Manager/Audio/OCTAudioQueue.m

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@
1313
const int kBufferLength = 384000;
1414
const int kNumberOfChannels = 1;
1515
const int kDefaultSampleRate = 48000;
16-
const int kSampleCount = 1920;
16+
const int kSampleCount_incoming_audio = 1920;
17+
const int kSampleCount_outgoing_audio = (1920 / 2);
1718
const int kBitsPerByte = 8;
1819
const int kFramesPerPacket = 1;
1920
// if you make this too small, the output queue will silently not play,
2021
// but you will still get fill callbacks; it's really weird
21-
const int kFramesPerOutputBuffer = kSampleCount / 4;
22+
const int kFramesPerOutputBuffer_incoming_audio = kSampleCount_incoming_audio / 4;
23+
const int kFramesPerOutputBuffer_outgoing_audio = kSampleCount_outgoing_audio / 4;
2224
const int kBytesPerSample = sizeof(SInt16);
2325
const int kNumberOfAudioQueueBuffers = 8;
2426

@@ -157,11 +159,13 @@ - (instancetype)initWithDeviceID:(NSString *)devID isOutput:(BOOL)output error:(
157159

158160
- (instancetype)initWithInputDeviceID:(NSString *)devID error:(NSError **)error
159161
{
162+
// TOXAUDIO: -outgoing-audio-
160163
return [self initWithDeviceID:devID isOutput:NO error:error];
161164
}
162165

163166
- (instancetype)initWithOutputDeviceID:(NSString *)devID error:(NSError **)error
164167
{
168+
// TOXAUDIO: -incoming-audio-
165169
return [self initWithDeviceID:devID isOutput:YES error:error];
166170
}
167171

@@ -182,9 +186,11 @@ - (OSStatus)createAudioQueue
182186
{
183187
OSStatus err;
184188
if (self.isOutput) {
189+
// TOXAUDIO: -incoming-audio-
185190
err = _AudioQueueNewOutput(&_streamFmt, (void *)&FillOutputBuffer, (__bridge void *)self, NULL, kCFRunLoopCommonModes, 0, &_audioQueue);
186191
}
187192
else {
193+
// TOXAUDIO: -outgoing-audio-
188194
err = _AudioQueueNewInput(&_streamFmt, (void *)&InputAvailable, (__bridge void *)self, NULL, kCFRunLoopCommonModes, 0, &_audioQueue);
189195
}
190196

@@ -215,9 +221,16 @@ - (BOOL)begin:(NSError **)error
215221
}
216222

217223
for (int i = 0; i < kNumberOfAudioQueueBuffers; ++i) {
218-
_AudioQueueAllocateBuffer(self.audioQueue, kBytesPerSample * kNumberOfChannels * kFramesPerOutputBuffer, &(_AQBuffers[i]));
224+
if (self.isOutput) {
225+
// TOXAUDIO: -incoming-audio-
226+
_AudioQueueAllocateBuffer(self.audioQueue, kBytesPerSample * kNumberOfChannels * kFramesPerOutputBuffer_incoming_audio, &(_AQBuffers[i]));
227+
} else {
228+
// TOXAUDIO: -outgoing-audio-
229+
_AudioQueueAllocateBuffer(self.audioQueue, kBytesPerSample * kNumberOfChannels * kFramesPerOutputBuffer_outgoing_audio, &(_AQBuffers[i]));
230+
}
219231
_AudioQueueEnqueueBuffer(self.audioQueue, _AQBuffers[i], 0, NULL);
220232
if (self.isOutput) {
233+
// TOXAUDIO: -outgoing-audio-
221234
// For some reason we have to fill it with zero or the callback never gets called.
222235
FillOutputBuffer(self, self.audioQueue, _AQBuffers[i]);
223236
}
@@ -354,11 +367,25 @@ static void InputAvailable(OCTAudioQueue *__unsafe_unretained context,
354367

355368
int32_t availableBytesToConsume;
356369
void *tail = TPCircularBufferTail(&context->_buffer, &availableBytesToConsume);
357-
int32_t minimalBytesToConsume = kSampleCount * kNumberOfChannels * sizeof(SInt16);
370+
371+
// TOXAUDIO: -outgoing-audio-
372+
int32_t minimalBytesToConsume = kSampleCount_outgoing_audio * kNumberOfChannels * sizeof(SInt16);
373+
374+
if (context.isOutput) {
375+
// TOXAUDIO: -incoming-audio-
376+
minimalBytesToConsume = kSampleCount_incoming_audio * kNumberOfChannels * sizeof(SInt16);
377+
}
378+
358379
int32_t cyclesToConsume = availableBytesToConsume / minimalBytesToConsume;
359380

360381
for (int32_t i = 0; i < cyclesToConsume; i++) {
361-
context.sendDataBlock(tail, kSampleCount, context.streamFmt.mSampleRate, kNumberOfChannels);
382+
if (context.isOutput) {
383+
// TOXAUDIO: -incoming-audio-
384+
context.sendDataBlock(tail, kSampleCount_incoming_audio, context.streamFmt.mSampleRate, kNumberOfChannels);
385+
} else {
386+
// TOXAUDIO: -outgoing-audio-
387+
context.sendDataBlock(tail, kSampleCount_outgoing_audio, context.streamFmt.mSampleRate, kNumberOfChannels);
388+
}
362389
TPCircularBufferConsume(&context->_buffer, minimalBytesToConsume);
363390
tail = TPCircularBufferTail(&context->_buffer, &availableBytesToConsume);
364391
}

local_pod_repo/objcTox/Classes/Private/Manager/Submanagers/OCTSubmanagerCallsImpl.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,7 @@ - (void) toxAV:(OCTToxAV *)toxAV
528528
sampleRate:(OCTToxAVSampleRate)sampleRate
529529
friendNumber:(OCTToxFriendNumber)friendNumber
530530
{
531+
// TOXAUDIO: -incoming-audio-
531532
[self.audioEngine provideAudioFrames:pcm sampleCount:sampleCount channels:channels sampleRate:sampleRate fromFriend:friendNumber];
532533
}
533534

local_pod_repo/objcTox/Classes/Private/Wrapper/OCTToxAV.m

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ - (BOOL)sendAudioFrame:(OCTToxAVPCMData *)pcm sampleCount:(OCTToxAVSampleCount)s
203203
channels:(OCTToxAVChannels)channels sampleRate:(OCTToxAVSampleRate)sampleRate
204204
toFriend:(OCTToxFriendNumber)friendNumber error:(NSError **)error
205205
{
206+
// TOXAUDIO: -outgoing-audio-
206207
TOXAV_ERR_SEND_FRAME cError;
207208

208209
BOOL status = _toxav_audio_send_frame(self.toxAV, friendNumber,
@@ -620,6 +621,7 @@ void receiveAudioFrameCallback(ToxAV *cToxAV,
620621
{
621622
OCTToxAV *toxAV = (__bridge OCTToxAV *)userData;
622623

624+
// TOXAUDIO: -incoming-audio-
623625
if ([toxAV.delegate respondsToSelector:@selector(toxAV:receiveAudio:sampleCount:channels:sampleRate:friendNumber:)]) {
624626
[toxAV.delegate toxAV:toxAV receiveAudio:pcm sampleCount:sampleCount channels:channels sampleRate:sampleRate friendNumber:friendNumber];
625627
}

0 commit comments

Comments
 (0)