1
/* $Id: ipjsuaAppDelegate.m 3550 2011-05-05 05:33:27Z nanang $ */
3
* Copyright (C) 2010-2011 Teluu Inc. (http://www.teluu.com)
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation; either version 2 of the License, or
8
* (at your option) any later version.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
#import "ipjsuaAppDelegate.h"
23
extern pj_log_func *log_cb;
25
@implementation ipjsuaAppDelegate
27
@synthesize tabBarController;
31
/* Sleep interval duration */
32
#define SLEEP_INTERVAL 0.5
33
/* Determine whether we should print the messages in the debugger
36
#define DEBUGGER_PRINT 1
37
/* Whether we should show pj log messages in the text area */
39
#define PATH_LENGTH PJ_MAXPATH
40
#define KEEP_ALIVE_INTERVAL 600
42
extern pj_bool_t app_restart;
44
char argv_buf[PATH_LENGTH];
45
char *argv[] = {"", "--config-file", argv_buf};
47
ipjsuaAppDelegate *app;
51
NSMutableString *mstr;
52
pj_thread_desc a_thread_desc;
53
pj_thread_t *a_thread;
54
pjsua_call_id ccall_id;
56
pj_status_t app_init(int argc, char *argv[]);
57
pj_status_t app_main(void);
58
pj_status_t app_destroy(void);
59
void keepAliveFunction(int timeout);
61
void showMsg(const char *format, ...)
63
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
66
va_start(arg, format);
67
NSString *str = [[NSString alloc] initWithFormat:[NSString stringWithFormat:@"%s", format] arguments: arg];
73
[mstr appendString:str];
77
char * getInput(char *s, int n, FILE *stream)
79
if (stream != stdin) {
80
return fgets(s, n, stream);
83
app.mainView.hasInput = false;
84
[app.mainView.textField setEnabled: true];
85
[app performSelectorOnMainThread:@selector(displayMsg:) withObject:mstr waitUntilDone:YES];
88
while (!thread_quit && !app.mainView.hasInput) {
90
[NSThread sleepForTimeInterval:SLEEP_INTERVAL];
92
[app performSelectorOnMainThread:@selector(displayMsg:) withObject:mstr waitUntilDone:YES];
99
[app.mainView.text getCString:s maxLength:n encoding:NSASCIIStringEncoding];
100
[app.mainView.textField setEnabled: false];
101
[app performSelectorOnMainThread:@selector(displayMsg:) withObject:app.mainView.text waitUntilDone:NO];
106
void showLog(int level, const char *data, int len)
111
pj_bool_t showNotification(pjsua_call_id call_id)
116
// Create a new notification
117
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
118
UILocalNotification* alert = [[[UILocalNotification alloc] init] autorelease];
121
alert.repeatInterval = 0;
122
alert.alertBody = @"Incoming call received...";
123
alert.alertAction = @"Answer";
125
[[UIApplication sharedApplication] presentLocalNotificationNow:alert];
136
- (void)answer_call {
137
if (!pj_thread_is_registered())
139
pj_thread_register("ipjsua", a_thread_desc, &a_thread);
141
pjsua_call_answer(ccall_id, 200, NULL, NULL);
145
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
146
[app performSelectorOnMainThread:@selector(answer_call) withObject:nil waitUntilDone:YES];
150
if (!pj_thread_is_registered())
152
pj_thread_register("ipjsua", a_thread_desc, &a_thread);
154
keepAliveFunction(KEEP_ALIVE_INTERVAL);
157
- (void)applicationDidEnterBackground:(UIApplication *)application
159
[app performSelectorOnMainThread:@selector(keepAlive) withObject:nil waitUntilDone:YES];
160
[application setKeepAliveTimeout:KEEP_ALIVE_INTERVAL handler: ^{
161
[app performSelectorOnMainThread:@selector(keepAlive) withObject:nil waitUntilDone:YES];
168
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
169
/* Wait until the view is ready */
170
while (self.mainView == nil) {
171
[NSThread sleepForTimeInterval:SLEEP_INTERVAL];
174
[NSThread setThreadPriority:1.0];
175
mstr = [NSMutableString stringWithCapacity:4196];
177
pj_log_set_log_func(&showLog);
182
app_restart = PJ_FALSE;
183
if (app_init(3, argv) != PJ_SUCCESS) {
184
NSString *str = @"Failed to initialize pjsua\n";
185
[app performSelectorOnMainThread:@selector(displayMsg:) withObject:str waitUntilDone:YES];
191
/* This is on purpose */
195
[app performSelectorOnMainThread:@selector(displayMsg:) withObject:mstr waitUntilDone:YES];
196
[mstr setString:@""];
197
} while (app_restart);
202
- (void)displayMsg:(NSString *)str {
203
self.mainView.textView.text = [self.mainView.textView.text stringByAppendingString:str];
204
[self.mainView.textView scrollRangeToVisible:NSMakeRange([self.mainView.textView.text length] - 1, 1)];
207
- (void)applicationDidFinishLaunching:(UIApplication *)application {
208
/* If there is no config file in the document dir, copy the default config file into the directory */
209
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
210
NSString *cfgPath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"/config.cfg"];
211
if (![[NSFileManager defaultManager] fileExistsAtPath:cfgPath]) {
212
NSString *resPath = [[NSBundle mainBundle] pathForResource:@"config" ofType:@"cfg"];
213
NSString *cfg = [NSString stringWithContentsOfFile:resPath encoding:NSASCIIStringEncoding error:NULL];
214
[cfg writeToFile:cfgPath atomically:NO encoding:NSASCIIStringEncoding error:NULL];
216
[cfgPath getCString:argv[2] maxLength:PATH_LENGTH encoding:NSASCIIStringEncoding];
218
// Add the tab bar controller's current view as a subview of the window
219
[window addSubview:tabBarController.view];
220
[window makeKeyAndVisible];
225
/* Start pjsua thread */
226
[NSThread detachNewThreadSelector:@selector(start_app) toTarget:self withObject:nil];
230
// Optional UITabBarControllerDelegate method
231
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
236
// Optional UITabBarControllerDelegate method
237
- (void)tabBarController:(UITabBarController *)tabBarController didEndCustomizingViewControllers:(NSArray *)viewControllers changed:(BOOL)changed {
244
[NSThread sleepForTimeInterval:SLEEP_INTERVAL];
246
[tabBarController release];