~ubuntu-branches/ubuntu/trusty/sflphone/trusty

« back to all changes in this revision

Viewing changes to daemon/libs/pjproject-2.0.1/pjsip-apps/src/ipjsua/Classes/ipjsuaAppDelegate.m

  • Committer: Package Import Robot
  • Author(s): Mark Purcell
  • Date: 2014-01-28 18:23:36 UTC
  • mfrom: (4.3.4 sid)
  • Revision ID: package-import@ubuntu.com-20140128182336-jrsv0k9u6cawc068
Tags: 1.3.0-1
* New upstream release 
  - Fixes "New Upstream Release" (Closes: #735846)
  - Fixes "Ringtone does not stop" (Closes: #727164)
  - Fixes "[sflphone-kde] crash on startup" (Closes: #718178)
  - Fixes "sflphone GUI crashes when call is hung up" (Closes: #736583)
* Build-Depends: ensure GnuTLS 2.6
  - libucommon-dev (>= 6.0.7-1.1), libccrtp-dev (>= 2.0.6-3)
  - Fixes "FTBFS Build-Depends libgnutls{26,28}-dev" (Closes: #722040)
* Fix "boost 1.49 is going away" unversioned Build-Depends: (Closes: #736746)
* Add Build-Depends: libsndfile-dev, nepomuk-core-dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* $Id: ipjsuaAppDelegate.m 3550 2011-05-05 05:33:27Z nanang $ */
2
 
/*
3
 
 * Copyright (C) 2010-2011 Teluu Inc. (http://www.teluu.com)
4
 
 *
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.
9
 
 *
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.
14
 
 *
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
18
 
 */
19
 
#import <pjlib.h>
20
 
#import <pjsua.h>
21
 
#import "ipjsuaAppDelegate.h"
22
 
 
23
 
extern pj_log_func *log_cb;
24
 
 
25
 
@implementation ipjsuaAppDelegate
26
 
@synthesize window;
27
 
@synthesize tabBarController;
28
 
@synthesize mainView;
29
 
@synthesize cfgView;
30
 
 
31
 
/* Sleep interval duration */
32
 
#define SLEEP_INTERVAL      0.5
33
 
/* Determine whether we should print the messages in the debugger
34
 
 * console as well
35
 
 */
36
 
#define DEBUGGER_PRINT      1
37
 
/* Whether we should show pj log messages in the text area */
38
 
#define SHOW_LOG            1
39
 
#define PATH_LENGTH         PJ_MAXPATH
40
 
#define KEEP_ALIVE_INTERVAL 600
41
 
 
42
 
extern pj_bool_t app_restart;
43
 
 
44
 
char argv_buf[PATH_LENGTH];
45
 
char *argv[] = {"", "--config-file", argv_buf};
46
 
 
47
 
ipjsuaAppDelegate       *app;
48
 
 
49
 
bool                     app_running;
50
 
bool                     thread_quit;
51
 
NSMutableString         *mstr;
52
 
pj_thread_desc           a_thread_desc;
53
 
pj_thread_t             *a_thread;
54
 
pjsua_call_id            ccall_id;
55
 
 
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);
60
 
 
61
 
void showMsg(const char *format, ...)
62
 
{
63
 
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
64
 
    va_list arg;
65
 
 
66
 
    va_start(arg, format);
67
 
    NSString *str = [[NSString alloc] initWithFormat:[NSString stringWithFormat:@"%s", format] arguments: arg];
68
 
#if DEBUGGER_PRINT
69
 
    NSLog(@"%@", str);
70
 
#endif
71
 
    va_end(arg);
72
 
 
73
 
    [mstr appendString:str];
74
 
    [pool release];
75
 
}
76
 
 
77
 
char * getInput(char *s, int n, FILE *stream)
78
 
{
79
 
    if (stream != stdin) {
80
 
        return fgets(s, n, stream);
81
 
    }
82
 
 
83
 
    app.mainView.hasInput = false;
84
 
    [app.mainView.textField setEnabled: true];
85
 
    [app performSelectorOnMainThread:@selector(displayMsg:) withObject:mstr waitUntilDone:YES];
86
 
    [mstr setString:@""];
87
 
 
88
 
    while (!thread_quit && !app.mainView.hasInput) {
89
 
        int ctr = 0;
90
 
        [NSThread sleepForTimeInterval:SLEEP_INTERVAL];
91
 
        if (ctr == 4) {
92
 
            [app performSelectorOnMainThread:@selector(displayMsg:) withObject:mstr waitUntilDone:YES];
93
 
            [mstr setString:@""];
94
 
            ctr = 0;
95
 
        }
96
 
        ctr++;
97
 
    }
98
 
 
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];
102
 
 
103
 
    return s;
104
 
}
105
 
 
106
 
void showLog(int level, const char *data, int len)
107
 
{
108
 
    showMsg("%s", data);
109
 
}
110
 
 
111
 
pj_bool_t showNotification(pjsua_call_id call_id)
112
 
{
113
 
#ifdef __IPHONE_4_0
114
 
    ccall_id = call_id;
115
 
 
116
 
    // Create a new notification
117
 
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
118
 
    UILocalNotification* alert = [[[UILocalNotification alloc] init] autorelease];
119
 
    if (alert)
120
 
    {
121
 
        alert.repeatInterval = 0;
122
 
        alert.alertBody = @"Incoming call received...";
123
 
        alert.alertAction = @"Answer";
124
 
 
125
 
        [[UIApplication sharedApplication] presentLocalNotificationNow:alert];
126
 
    }
127
 
 
128
 
    [pool release];
129
 
 
130
 
    return PJ_FALSE;
131
 
#else
132
 
    return PJ_TRUE;
133
 
#endif
134
 
}
135
 
 
136
 
- (void)answer_call {
137
 
    if (!pj_thread_is_registered())
138
 
    {
139
 
        pj_thread_register("ipjsua", a_thread_desc, &a_thread);
140
 
    }
141
 
    pjsua_call_answer(ccall_id, 200, NULL, NULL);
142
 
}
143
 
 
144
 
#ifdef __IPHONE_4_0
145
 
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
146
 
    [app performSelectorOnMainThread:@selector(answer_call) withObject:nil waitUntilDone:YES];
147
 
}
148
 
 
149
 
- (void)keepAlive {
150
 
    if (!pj_thread_is_registered())
151
 
    {
152
 
        pj_thread_register("ipjsua", a_thread_desc, &a_thread);
153
 
    }
154
 
    keepAliveFunction(KEEP_ALIVE_INTERVAL);
155
 
}
156
 
 
157
 
- (void)applicationDidEnterBackground:(UIApplication *)application
158
 
{
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];
162
 
    }];
163
 
}
164
 
 
165
 
#endif
166
 
 
167
 
- (void)start_app {
168
 
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
169
 
    /* Wait until the view is ready */
170
 
    while (self.mainView == nil) {
171
 
        [NSThread sleepForTimeInterval:SLEEP_INTERVAL];
172
 
    }
173
 
 
174
 
    [NSThread setThreadPriority:1.0];
175
 
    mstr = [NSMutableString stringWithCapacity:4196];
176
 
#if SHOW_LOG
177
 
    pj_log_set_log_func(&showLog);
178
 
    log_cb = &showLog;
179
 
#endif
180
 
 
181
 
    do {
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];
186
 
        } else {
187
 
            app_running = true;
188
 
            app_main();
189
 
 
190
 
            app_destroy();
191
 
            /* This is on purpose */
192
 
            app_destroy();
193
 
        }
194
 
 
195
 
        [app performSelectorOnMainThread:@selector(displayMsg:) withObject:mstr waitUntilDone:YES];
196
 
        [mstr setString:@""];
197
 
    } while (app_restart);
198
 
 
199
 
    [pool release];
200
 
}
201
 
 
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)];
205
 
}
206
 
 
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];
215
 
    }
216
 
    [cfgPath getCString:argv[2] maxLength:PATH_LENGTH encoding:NSASCIIStringEncoding];
217
 
 
218
 
    // Add the tab bar controller's current view as a subview of the window
219
 
    [window addSubview:tabBarController.view];
220
 
    [window makeKeyAndVisible];
221
 
 
222
 
    app = self;
223
 
    app_running = false;
224
 
    thread_quit = false;
225
 
    /* Start pjsua thread */
226
 
    [NSThread detachNewThreadSelector:@selector(start_app) toTarget:self withObject:nil];
227
 
}
228
 
 
229
 
/*
230
 
// Optional UITabBarControllerDelegate method
231
 
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
232
 
}
233
 
*/
234
 
 
235
 
/*
236
 
// Optional UITabBarControllerDelegate method
237
 
- (void)tabBarController:(UITabBarController *)tabBarController didEndCustomizingViewControllers:(NSArray *)viewControllers changed:(BOOL)changed {
238
 
}
239
 
*/
240
 
 
241
 
 
242
 
- (void)dealloc {
243
 
    thread_quit = true;
244
 
    [NSThread sleepForTimeInterval:SLEEP_INTERVAL];
245
 
 
246
 
    [tabBarController release];
247
 
    [window release];
248
 
    [super dealloc];
249
 
}
250
 
 
251
 
@end