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 11 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
6 changes: 3 additions & 3 deletions Classes/Private/Manager/Database/OCTRealmManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
sender:(OCTFriend *)sender
messageId:(OCTToxMessageId)messageId;

- (void)addMessageCall:(OCTMessageCallEvent)event
call:(OCTCall *)call
callDuration:(NSTimeInterval)duration;
- (void)addMessageCallEvent:(OCTMessageCallEvent)event
call:(OCTCall *)call
callDuration:(NSTimeInterval)duration;
@end
27 changes: 24 additions & 3 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 removeAllCalls];

return self;
}

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

- (void)removeAllCalls
Copy link
Member

Choose a reason for hiding this comment

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

It would be better to rename method to something more explicit like 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) {
OCTMessageCallEvent event = (call.status != OCTCallStatusActive) ? OCTMessageCallEventUnanswered : OCTMessageCallEventAnswered;
Copy link
Member

Choose a reason for hiding this comment

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

It would look nicer if it would be positive statement

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

Copy link
Member

Choose a reason for hiding this comment

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

Even better it would be to use switch here. This way in case if new value will be added to enum compiler will give you an error.

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

Copy link
Member

Choose a reason for hiding this comment

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

Also as event and callDuration depends on call, there is no need to path them into addMessageCall... method. You can move all logic inside and simply create method like

addMessageFromCall:(OCTCall *)call;

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 this cleans up the code quite a bit. I like 👍 Thanks

[self addMessageCallEvent:event call:call callDuration:call.callDuration];
}

[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,9 +311,9 @@ - (OCTMessageAbstract *)addMessageWithText:(NSString *)text
return messageAbstract;
}

- (void)addMessageCall:(OCTMessageCallEvent)event
call:(OCTCall *)call
callDuration:(NSTimeInterval)duration
- (void)addMessageCallEvent:(OCTMessageCallEvent)event
call:(OCTCall *)call
callDuration:(NSTimeInterval)duration
{
NSParameterAssert(call);
DDLogInfo(@"OCTRealmManager: adding messageCall to call %@", call);
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
44 changes: 27 additions & 17 deletions Classes/Private/Manager/Submanagers/OCTSubmanagerCalls.m
Original file line number Diff line number Diff line change
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 Down Expand Up @@ -224,7 +220,7 @@ - (void)updateCall:(OCTCall *)call withStatus:(OCTCallStatus)status
- (void)addMessageCall:(OCTMessageCallEvent)event forCall:(OCTCall *)call withDuration:(NSTimeInterval)duration
{
OCTRealmManager *realmManager = [self.dataSource managerGetRealmManager];
[realmManager addMessageCall:event call:call callDuration:duration];
[realmManager addMessageCallEvent:event call:call callDuration:duration];

[self.timer stopTimer];
[realmManager deleteObject:call];
Expand All @@ -234,28 +230,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 @@ -277,9 +273,9 @@ - (void)toxAV:(OCTToxAV *)toxAV callStateChanged:(OCTToxAVCallState)state friend

OCTCallStatus status = call.status;

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

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

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

Expand All @@ -290,6 +286,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,12 +296,24 @@ - (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?
NSArray *validBitrates = @[@8, @16, @24, @32, @48];
Copy link
Member

Choose a reason for hiding this comment

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

Why are you sure that these are valid bitrates and that toxav will return them?

Copy link
Member Author

Choose a reason for hiding this comment

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

They were working when I was testing them, well at least for 24/32/48. I never got down lower than 24. These are just numbers that mannol suggested me to try.

Copy link
Member

Choose a reason for hiding this comment

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

Hm, can you maybe make them constants? Enum with these values will be fine.


if (! stable) {
Copy link
Member

Choose a reason for hiding this comment

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

Early return would look nicer (at the start of method, before validBitrates array)

if (stable) {
    return;
}

NSUInteger currentIndexBitRate = [validBitrates indexOfObject:[NSNumber numberWithInt:bitrate]];

if (currentIndexBitRate == NSNotFound) {
return;
}

NSNumber *newBitrate = [validBitrates objectAtIndex:currentIndexBitRate - 1];
Copy link
Member

Choose a reason for hiding this comment

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

Crash on currentIndexBitRate == 0


[self.toxAV setAudioBitRate:newBitrate.intValue force:NO forFriend:friendNumber error:nil];
}
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

}

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

Choose a reason for hiding this comment

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

Early return. Or remove this at all until video implementation.

}

- (void) toxAV:(OCTToxAV *)toxAV
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 or video receiving or we turned off sending for the said payload.";
Copy link
Member

Choose a reason for hiding this comment

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

audio or video -> audio/video looks more readable to me.

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
17 changes: 11 additions & 6 deletions Classes/Public/Wrapper/OCTToxAVConstants.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,34 +38,34 @@ typedef NS_OPTIONS(NSInteger, OCTToxAVCallState) {
* transitions can occur for the call. This call state will never be triggered
* in combination with other call states.
*/
OCTToxAVCallStateError = 1 << 0,
OCTToxAVFriendCallStateError = 1 << 0,

/**
* The call has finished. This is the final state after which no more state
* transitions can occur for the call. This call state will never be
* triggered in combination with other call states.
*/
OCTToxAVCallStateFinished = 1 << 1,
OCTToxAVFriendCallStateFinished = 1 << 1,

/**
* The flag that marks that friend is sending audio.
*/
OCTToxAVCallStateSendingAudio = 1 << 2,
OCTToxAVFriendCallStateSendingAudio = 1 << 2,

/**
* The flag that marks that friend is sending video.
*/
OCTToxAVCallStateSendingVideo = 1 << 3,
OCTToxAVFriendCallStateSendingVideo = 1 << 3,

/**
* The flag that marks that friend is receiving audio.
*/
OCTToxAVCallStateReceivingAudio = 1 << 4,
OCTToxAVFriendCallStateReceivingAudio = 1 << 4,

/**
* The flag that marks that friend is receiving video.
*/
OCTToxAVCallStateReceivingVideo = 1 << 5,
OCTToxAVFriendCallStateReceivingVideo = 1 << 5,
};

/*******************************************************************************
Expand Down Expand Up @@ -235,6 +235,11 @@ typedef NS_ENUM(NSInteger, OCTToxAVErrorSendFrame) {
*/
OCTToxAVErrorSendFrameInvalid,

/**
* Bit rate for this payload type was not set up.
*/
OCTToxAVErrorSendFramePayloadTypeDisabled,

/**
* Failed to push frame through rtp interface.
*/
Expand Down
2 changes: 1 addition & 1 deletion Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ platform :ios, '7.0'
# ignore all warnings from all pods
inhibit_all_warnings!

pod 'toxcore-ios', :podspec => 'https://raw.githubusercontent.com/Chuongv/toxcore-ios/0.1.10-new-av/toxcore-ios.podspec'
pod 'toxcore-ios', :podspec => 'https://raw.githubusercontent.com/Chuongv/toxcore-ios/0.1.12-new-av/toxcore-ios.podspec'
pod 'CocoaLumberjack', '~> 1.9.2'
pod 'TPCircularBuffer', '~> 0.0.1'
pod 'Realm', '0.93.2'
Expand Down
6 changes: 6 additions & 0 deletions objcTox.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
EE9A84F0183CA4D0C3342BD50E2F44E5 /* OCTMessageAbstract.m in Sources */ = {isa = PBXBuildFile; fileRef = 5309FB4CA1007014A575D75CB9387C5C /* OCTMessageAbstract.m */; };
EF084F94290314128486E23E3CE9B22F /* OCTToxAV.m in Sources */ = {isa = PBXBuildFile; fileRef = 9DA03E67FB19C7268351D5CDA3ABCAD3 /* OCTToxAV.m */; };
F02C04D11B20FB98001025D1 /* OCTSubmanagerCallsTests.m in Sources */ = {isa = PBXBuildFile; fileRef = F02C04D01B20FB98001025D1 /* OCTSubmanagerCallsTests.m */; };
F02DA7D11B3DEFF800B89491 /* OCTCallsViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = F02DA7D01B3DEFF800B89491 /* OCTCallsViewController.m */; };
F041A4491B3B1BC7007A5AE8 /* OCTCallTimer.m in Sources */ = {isa = PBXBuildFile; fileRef = F041A4481B3B1BC7007A5AE8 /* OCTCallTimer.m */; };
F041A44A1B3B1BC7007A5AE8 /* OCTCallTimer.m in Sources */ = {isa = PBXBuildFile; fileRef = F041A4481B3B1BC7007A5AE8 /* OCTCallTimer.m */; };
F05BE2681B3A6DF200C5795D /* OCTToxAVConstants.m in Sources */ = {isa = PBXBuildFile; fileRef = F07C8F981B1E37D300E399F5 /* OCTToxAVConstants.m */; };
Expand Down Expand Up @@ -206,6 +207,8 @@
EEE0165339DE12FD2EE1821ACE3E19B8 /* OCTToxOptions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = OCTToxOptions.h; sourceTree = "<group>"; };
F02C04CF1B20F7E0001025D1 /* OCTSubmanagerCalls+Private.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "OCTSubmanagerCalls+Private.h"; sourceTree = "<group>"; };
F02C04D01B20FB98001025D1 /* OCTSubmanagerCallsTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = OCTSubmanagerCallsTests.m; sourceTree = "<group>"; };
F02DA7CF1B3DEFF800B89491 /* OCTCallsViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = OCTCallsViewController.h; sourceTree = "<group>"; };
F02DA7D01B3DEFF800B89491 /* OCTCallsViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = OCTCallsViewController.m; sourceTree = "<group>"; };
F041A4471B3B1BC7007A5AE8 /* OCTCallTimer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = OCTCallTimer.h; sourceTree = "<group>"; };
F041A4481B3B1BC7007A5AE8 /* OCTCallTimer.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = OCTCallTimer.m; sourceTree = "<group>"; };
F056BC181B251AC2008D60B8 /* OCTAudioEngine+Private.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "OCTAudioEngine+Private.h"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -353,6 +356,8 @@
7218668C8B51B22E4E473DA9AF6979D1 /* OCTUserViewController.m */,
F0B36B531B2CFE2200892E05 /* OCTTabBarControllerViewController.h */,
F0B36B541B2CFE2200892E05 /* OCTTabBarControllerViewController.m */,
F02DA7CF1B3DEFF800B89491 /* OCTCallsViewController.h */,
F02DA7D01B3DEFF800B89491 /* OCTCallsViewController.m */,
);
path = objcToxDemo;
sourceTree = "<group>";
Expand Down Expand Up @@ -752,6 +757,7 @@
11B4BAF21B11E2850001A96A /* OCTSubmanagerFiles.m in Sources */,
EC8B1CF4E9445717B8E61A26FD52E36B /* OCTMessageFile.m in Sources */,
9CB71FC31B399EE800E3C1EF /* OCTObject.m in Sources */,
F02DA7D11B3DEFF800B89491 /* OCTCallsViewController.m in Sources */,
9CB71FC21B399EE400E3C1EF /* OCTRealmManager.m in Sources */,
25E5DD48EE60A2A6203731909C2F310E /* OCTMessageText.m in Sources */,
F9A7007A32C59757AC4132DF656C5793 /* OCTStartDemoViewController.m in Sources */,
Expand Down
13 changes: 13 additions & 0 deletions objcToxDemo/OCTCallsViewController.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// OCTCallsViewController.h
// objcTox
//
// Created by Chuong Vu on 6/26/15.
// Copyright (c) 2015 dvor. All rights reserved.
//

#import "OCTTableViewController.h"

@interface OCTCallsViewController : OCTTableViewController

@end
Loading