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

Chuongv/audio tests and demo #52

Merged
merged 15 commits into from
Jun 30, 2015
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
36 changes: 32 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,23 @@ - (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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add break; here. This may save us from error in future.

}

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 OCTToxAVAudioBitRateDisabled:
NSAssert(NO, @"We shouldn't be here!");
break;
case OCTToxAVAudioBitRate8:
return;
case OCTToxAVAudioBitRate48:
newBitrate = OCTToxAVAudioBitRate32;
break;
case OCTToxAVAudioBitRate32:
newBitrate = OCTToxAVAudioBitRate24;
break;
case OCTToxAVAudioBitRate24:
newBitrate = OCTToxAVAudioBitRate16;
break;
case OCTToxAVAudioBitRate16:
newBitrate = OCTToxAVAudioBitRate8;
break;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, if bitrate is unstable we will lower it. Any ideas of possibilities of increasing bitrate, e.g. if network quality increased?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea I'm not sure what the best way to do that is? We can submit a new non-forceful bitrate change request every few minutes in the call

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can leave it for now. Currently it is more important just to make things work. I've created an issue so we won't forget about that #53

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks okay, you can just change the ordering to be more consistent. From lowest to highest.


[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