Skip to content
This repository has been archived by the owner on Aug 4, 2019. It is now read-only.

Commit

Permalink
Merge pull request #52 from Antidote-for-Tox/chuongv/audioTestsAndDemo
Browse files Browse the repository at this point in the history
Chuongv/audio tests and demo
  • Loading branch information
dvor committed Jun 30, 2015
2 parents ef28258 + 43fac74 commit 71d2564
Show file tree
Hide file tree
Showing 17 changed files with 531 additions and 237 deletions.
1 change: 1 addition & 0 deletions Classes/Private/Manager/Audio/OCTAudioEngine.m
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,7 @@ - (BOOL)startAudioSession:(NSError **)error

return ([session setCategory:AVAudioSessionCategoryPlayAndRecord error:error] &&
[session setPreferredSampleRate:kDefaultSampleRate error:error] &&
[session setMode:AVAudioSessionModeVideoChat error:error] &&
[session setActive:YES error:error]);
}

Expand Down
5 changes: 2 additions & 3 deletions Classes/Private/Manager/Database/OCTRealmManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@
sender:(OCTFriend *)sender
messageId:(OCTToxMessageId)messageId;

- (void)addMessageCall:(OCTMessageCallEvent)event
call:(OCTCall *)call
callDuration:(NSTimeInterval)duration;
- (void)addMessageCall:(OCTCall *)call;

@end
37 changes: 33 additions & 4 deletions Classes/Private/Manager/Database/OCTRealmManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ - (instancetype)initWithDatabasePath:(NSString *)path
_realm = [RLMRealm realmWithPath:path];
});

[self convertAllCallsToMessages];

return self;
}

Expand Down Expand Up @@ -258,6 +260,24 @@ - (void)removeChatWithAllMessages:(OCTChat *)chat
});
}

- (void)convertAllCallsToMessages
{
RLMResults *calls = [OCTCall objectsInRealm:self.realm where:nil];

DDLogInfo(@"OCTRealmManager: removing %lu calls", calls.count);

RBQRealmChangeLogger *logger = [self logger];

for (OCTCall *call in calls) {
[self addMessageCall:call];
}

[self.realm beginWriteTransaction];
[logger willDeleteObjects:calls];
[self.realm deleteObjects:calls];
[self.realm commitWriteTransaction];
}

- (OCTMessageAbstract *)addMessageWithText:(NSString *)text
type:(OCTToxMessageType)type
chat:(OCTChat *)chat
Expand Down Expand Up @@ -290,15 +310,24 @@ - (OCTMessageAbstract *)addMessageWithText:(NSString *)text
return messageAbstract;
}

- (void)addMessageCall:(OCTMessageCallEvent)event
call:(OCTCall *)call
callDuration:(NSTimeInterval)duration
- (void)addMessageCall:(OCTCall *)call
{
NSParameterAssert(call);
DDLogInfo(@"OCTRealmManager: adding messageCall to call %@", call);

OCTMessageCallEvent event;
switch (call.status) {
case OCTCallStatusDialing:
case OCTCallStatusRinging:
event = OCTMessageCallEventUnanswered;
break;
case OCTCallStatusActive:
event = OCTMessageCallEventAnswered;
break;
}

OCTMessageCall *messageCall = [OCTMessageCall new];
messageCall.callDuration = duration;
messageCall.callDuration = call.callDuration;
messageCall.callEvent = event;

OCTMessageAbstract *messageAbstract = [OCTMessageAbstract new];
Expand Down
1 change: 1 addition & 0 deletions Classes/Private/Manager/OCTManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ - (instancetype)initWithConfiguration:(OCTManagerConfiguration *)configuration
OCTSubmanagerCalls *calls = [[OCTSubmanagerCalls alloc] initWithTox:_tox];
calls.dataSource = self;
_calls = calls;
[_calls setupWithError:nil];

OCTSubmanagerObjects *objects = [OCTSubmanagerObjects new];
objects.dataSource = self;
Expand Down
76 changes: 46 additions & 30 deletions Classes/Private/Manager/Submanagers/OCTSubmanagerCalls.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#import "OCTSubmanagerCalls+Private.h"

const OCTToxAVAudioBitRate kDefaultAudioBitRate = 48;
const OCTToxAVAudioBitRate kDefaultAudioBitRate = OCTToxAVAudioBitRate48;
const OCTToxAVAudioBitRate kDefaultVideoBitRate = 400;

@interface OCTSubmanagerCalls () <OCTToxAVDelegate>
Expand Down Expand Up @@ -36,10 +36,6 @@ - (instancetype)initWithTox:(OCTTox *)tox
_toxAV.delegate = self;
[_toxAV start];

_audioEngine = [OCTAudioEngine new];
_audioEngine.toxav = self.toxAV;
[_audioEngine setupWithError:nil];

return self;
}

Expand All @@ -61,7 +57,7 @@ - (BOOL)setupWithError:(NSError **)error

- (OCTCall *)callToChat:(OCTChat *)chat enableAudio:(BOOL)enableAudio enableVideo:(BOOL)enableVideo error:(NSError **)error
{
OCTToxAVAudioBitRate audioBitRate = (enableAudio) ? kDefaultAudioBitRate : kOCTToxAVAudioBitRateDisable;
OCTToxAVAudioBitRate audioBitRate = (enableAudio) ? kDefaultAudioBitRate : OCTToxAVAudioBitRateDisabled;
OCTToxAVVideoBitRate videoBitRate = (enableVideo) ? kDefaultVideoBitRate : kOCTToxAVVideoBitRateDisable;

if (chat.friends.count == 1) {
Expand Down Expand Up @@ -89,7 +85,7 @@ - (OCTCall *)callToChat:(OCTChat *)chat enableAudio:(BOOL)enableAudio enableVide

- (BOOL)answerCall:(OCTCall *)call enableAudio:(BOOL)enableAudio enableVideo:(BOOL)enableVideo error:(NSError **)error
{
OCTToxAVAudioBitRate audioBitRate = (enableAudio) ? kDefaultAudioBitRate : kOCTToxAVAudioBitRateDisable;
OCTToxAVAudioBitRate audioBitRate = (enableAudio) ? kDefaultAudioBitRate : OCTToxAVAudioBitRateDisabled;
OCTToxAVVideoBitRate videoBitRate = (enableVideo) ? kDefaultVideoBitRate : kOCTToxAVVideoBitRateDisable;

if (call.chat.friends.count == 1) {
Expand Down Expand Up @@ -141,15 +137,13 @@ - (BOOL)sendCallControl:(OCTToxAVCallControl)control toCall:(OCTCall *)call erro
return NO;
}

OCTMessageCallEvent event = (call.status == OCTCallStatusActive) ? OCTMessageCallEventAnswered : OCTMessageCallEventUnanswered;

switch (control) {
case OCTToxAVCallControlResume:
[self updateCall:call withStatus:OCTCallStatusActive];
break;
case OCTToxAVCallControlCancel:
[self.timer stopTimer];
[self addMessageCall:event forCall:call withDuration:call.callDuration];
[self addMessageCall:call];
return [self.audioEngine stopAudioFlow:error];
case OCTToxAVCallControlPause:
break;
Expand Down Expand Up @@ -221,10 +215,10 @@ - (void)updateCall:(OCTCall *)call withStatus:(OCTCallStatus)status
}];
}

- (void)addMessageCall:(OCTMessageCallEvent)event forCall:(OCTCall *)call withDuration:(NSTimeInterval)duration
- (void)addMessageCall:(OCTCall *)call
{
OCTRealmManager *realmManager = [self.dataSource managerGetRealmManager];
[realmManager addMessageCall:event call:call callDuration:duration];
[realmManager addMessageCall:call];

[self.timer stopTimer];
[realmManager deleteObject:call];
Expand All @@ -234,28 +228,28 @@ - (void)updateCall:(OCTCall *)call withState:(OCTToxAVCallState)state
{
BOOL sendingAudio, sendingVideo, receivingAudio, receivingVideo;

if (state & OCTToxAVCallStateReceivingAudio) {
if (state & OCTToxAVFriendCallStateReceivingAudio) {
receivingAudio = YES;
}

if (state & OCTToxAVCallStateReceivingVideo) {
if (state & OCTToxAVFriendCallStateReceivingVideo) {
receivingVideo = YES;
}

if (state & OCTToxAVCallStateSendingAudio) {
if (state & OCTToxAVFriendCallStateSendingAudio) {
sendingAudio = YES;
}

if (state & OCTToxAVCallStateSendingVideo) {
if (state & OCTToxAVFriendCallStateSendingVideo) {
sendingVideo = YES;
}

OCTRealmManager *realmManager = [self.dataSource managerGetRealmManager];
[realmManager updateObject:call withBlock:^(OCTCall *callToUpdate) {
call.receivingAudio = receivingAudio;
call.receivingVideo = receivingVideo;
call.sendingAudio = sendingAudio;
call.sendingVideo = sendingVideo;
callToUpdate.receivingAudio = receivingAudio;
callToUpdate.receivingVideo = receivingVideo;
callToUpdate.sendingAudio = sendingAudio;
callToUpdate.sendingVideo = sendingVideo;
}];
}

Expand All @@ -275,13 +269,9 @@ - (void)toxAV:(OCTToxAV *)toxAV callStateChanged:(OCTToxAVCallState)state friend
{
OCTCall *call = [self getOrCreateCallWithFriendNumber:friendNumber];

OCTCallStatus status = call.status;

if ((state & OCTToxAVCallStateFinished) || (state & OCTToxAVCallStateError)) {
if ((state & OCTToxAVFriendCallStateFinished) || (state & OCTToxAVFriendCallStateError)) {

OCTMessageCallEvent event = (status = OCTCallStatusRinging) ? OCTMessageCallEventUnanswered : OCTMessageCallEventAnswered;

[self addMessageCall:event forCall:call withDuration:call.callDuration];
[self addMessageCall:call];

[self.audioEngine stopAudioFlow:nil];

Expand All @@ -290,6 +280,8 @@ - (void)toxAV:(OCTToxAV *)toxAV callStateChanged:(OCTToxAVCallState)state friend

if (call.status == OCTCallStatusDialing) {
[self updateCall:call withStatus:OCTCallStatusActive];
self.audioEngine.friendNumber = friendNumber;
[self.audioEngine startAudioFlow:nil];
[self.timer startTimerForCall:call];
}

Expand All @@ -298,13 +290,37 @@ - (void)toxAV:(OCTToxAV *)toxAV callStateChanged:(OCTToxAVCallState)state friend

- (void)toxAV:(OCTToxAV *)toxAV audioBitRateChanged:(OCTToxAVAudioBitRate)bitrate stable:(BOOL)stable friendNumber:(OCTToxFriendNumber)friendNumber
{
// Lower bitrate if unstable?
if (stable) {
return;
}

OCTToxAVAudioBitRate newBitrate;

switch (bitrate) {
case OCTToxAVAudioBitRate48:
newBitrate = OCTToxAVAudioBitRate32;
break;
case OCTToxAVAudioBitRate32:
newBitrate = OCTToxAVAudioBitRate24;
break;
case OCTToxAVAudioBitRate24:
newBitrate = OCTToxAVAudioBitRate16;
break;
case OCTToxAVAudioBitRate16:
newBitrate = OCTToxAVAudioBitRate8;
break;
case OCTToxAVAudioBitRate8:
return;
case OCTToxAVAudioBitRateDisabled:
NSAssert(NO, @"We shouldn't be here!");
break;
}

[self.toxAV setAudioBitRate:newBitrate force:NO forFriend:friendNumber error:nil];
}

- (void)toxAV:(OCTToxAV *)toxAV videoBitRateChanged:(OCTToxAVVideoBitRate)bitrate friendNumber:(OCTToxFriendNumber)friendNumber stable:(BOOL)stable
{
// Lower bitrate if unstable?
}
{}

- (void) toxAV:(OCTToxAV *)toxAV
receiveAudio:(OCTToxAVPCMData *)pcm
Expand Down
29 changes: 16 additions & 13 deletions Classes/Private/Wrapper/OCTToxAV.m
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,9 @@ - (void)fillError:(NSError **)error withCErrorSendFrame:(TOXAV_ERR_SEND_FRAME)cE
code = OCTToxAVErrorSendFrameInvalid;
failureReason = @"One of the frame parameters was invalid. E.g. the resolution may be too small or too large, or the audio sampling rate may be unsupported";
break;
case TOXAV_ERR_SEND_FRAME_PAYLOAD_TYPE_DISABLED:
code = OCTToxAVErrorSendFramePayloadTypeDisabled;
failureReason = @"Either friend turned off audio/video receiving or we turned off sending for the said payload.";
case TOXAV_ERR_SEND_FRAME_RTP_FAILED:
code = OCTToxAVErrorSendFrameRTPFailed;
failureReason = @"Failed to push frame through rtp interface";
Expand Down Expand Up @@ -542,7 +545,7 @@ void callIncomingCallback(ToxAV *cToxAV,

void callStateCallback(ToxAV *cToxAV,
uint32_t friendNumber,
enum TOXAV_CALL_STATE cState,
enum TOXAV_FRIEND_CALL_STATE cState,
void *userData)
{
OCTToxAV *toxAV = (__bridge OCTToxAV *)userData;
Expand All @@ -553,23 +556,23 @@ void callStateCallback(ToxAV *cToxAV,

OCTToxAVCallState state = 0;

if (cState & TOXAV_CALL_STATE_ERROR) {
state |= OCTToxAVCallStateError;
if (cState & TOXAV_FRIEND_CALL_STATE_ERROR) {
state |= OCTToxAVFriendCallStateError;
}
if (cState & TOXAV_CALL_STATE_FINISHED) {
state |= OCTToxAVCallStateFinished;
if (cState & TOXAV_FRIEND_CALL_STATE_FINISHED) {
state |= OCTToxAVFriendCallStateFinished;
}
if (cState & TOXAV_CALL_STATE_SENDING_A) {
state |= OCTToxAVCallStateSendingAudio;
if (cState & TOXAV_FRIEND_CALL_STATE_SENDING_A) {
state |= OCTToxAVFriendCallStateSendingAudio;
}
if (cState & TOXAV_CALL_STATE_SENDING_V) {
state |= OCTToxAVCallStateSendingVideo;
if (cState & TOXAV_FRIEND_CALL_STATE_SENDING_V) {
state |= OCTToxAVFriendCallStateSendingVideo;
}
if (cState & TOXAV_CALL_STATE_RECEIVING_A) {
state |= OCTToxAVCallStateReceivingAudio;
if (cState & TOXAV_FRIEND_CALL_STATE_RECEIVING_A) {
state |= OCTToxAVFriendCallStateReceivingAudio;
}
if (cState & TOXAV_CALL_STATE_RECEIVING_V) {
state |= OCTToxAVCallStateReceivingVideo;
if (cState & TOXAV_FRIEND_CALL_STATE_RECEIVING_V) {
state |= OCTToxAVFriendCallStateReceivingVideo;
}

if ([toxAV.delegate respondsToSelector:@selector(toxAV:callStateChanged:friendNumber:)]) {
Expand Down
Loading

0 comments on commit 71d2564

Please sign in to comment.