blob: 404e75a86cf3abf9dab3d7ad47eaf0a3e905c769 [file] [log] [blame]
Alexandre Lisionc5148052015-03-04 15:10:35 -05001/*
2 * Copyright (C) 2004-2015 Savoir-Faire Linux Inc.
3 * Author: Alexandre Lision <alexandre.lision@savoirfairelinux.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 3 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Additional permission under GNU GPL version 3 section 7:
20 *
21 * If you modify this program, or any covered work, by linking or
22 * combining it with the OpenSSL project's OpenSSL library (or a
23 * modified version of that library), containing parts covered by the
24 * terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc.
25 * grants you additional permission to convey the resulting work.
26 * Corresponding Source for a non-source form of such a combination
27 * shall include the source code for the parts of OpenSSL used as well
28 * as that of the covered work.
29 */
30#import "CurrentCallVC.h"
31
32#import <QuartzCore/QuartzCore.h>
33
34#import <call.h>
35#import <callmodel.h>
36#import <useractionmodel.h>
37#import <contactmethod.h>
38#import <qabstractitemmodel.h>
39#import <QItemSelectionModel>
40#import <QItemSelection>
41
42#import <video/previewmanager.h>
43#import <video/renderer.h>
44
45/** FrameReceiver class - delegate for AVCaptureSession
46 */
47@interface RendererConnectionsHolder : NSObject
48
49@property QMetaObject::Connection frameUpdated;
50@property QMetaObject::Connection started;
51@property QMetaObject::Connection stopped;
52
53@end
54
55@implementation RendererConnectionsHolder
56
57@end
58
59@interface CurrentCallVC ()
60
61@property (assign) IBOutlet NSTextField *personLabel;
62@property (assign) IBOutlet NSTextField *stateLabel;
63@property (assign) IBOutlet NSButton *holdOnOffButton;
64@property (assign) IBOutlet NSButton *hangUpButton;
65@property (assign) IBOutlet NSButton *recordOnOffButton;
66@property (assign) IBOutlet NSButton *pickUpButton;
67@property (assign) IBOutlet NSTextField *timeSpentLabel;
68@property (assign) IBOutlet NSView *controlsPanel;
69
70@property QHash<int, NSButton*> actionHash;
71
72// Video
73@property (assign) IBOutlet NSView *videoView;
74@property CALayer* videoLayer;
75@property (assign) IBOutlet NSView *previewView;
76@property CALayer* previewLayer;
77
78@property RendererConnectionsHolder* previewHolder;
79@property RendererConnectionsHolder* videoHolder;
80
81@end
82
83@implementation CurrentCallVC
84@synthesize personLabel;
85@synthesize actionHash;
86@synthesize stateLabel;
87@synthesize holdOnOffButton;
88@synthesize hangUpButton;
89@synthesize recordOnOffButton;
90@synthesize pickUpButton;
91@synthesize timeSpentLabel;
92@synthesize controlsPanel;
93@synthesize videoView;
94@synthesize videoLayer;
95@synthesize previewLayer;
96@synthesize previewView;
97
98@synthesize previewHolder;
99@synthesize videoHolder;
100
101
102
103
104- (void) updateActions
105{
106 for(int i = 0 ; i <= CallModel::instance()->userActionModel()->rowCount() ; i++) {
107 const QModelIndex& idx = CallModel::instance()->userActionModel()->index(i,0);
108 NSButton* a = actionHash[(int)qvariant_cast<UserActionModel::Action>(idx.data(UserActionModel::Role::ACTION))];
109 if (a != nil) {
110 [a setEnabled:(idx.flags() & Qt::ItemIsEnabled)];
111 [a setState:(idx.data(Qt::CheckStateRole) == Qt::Checked) ? NSOnState : NSOffState];
112 }
113 }
114}
115
116-(void) updateCall
117{
118 QModelIndex callIdx = CallModel::instance()->selectionModel()->currentIndex();
119 [personLabel setStringValue:CallModel::instance()->data(callIdx, Qt::DisplayRole).toString().toNSString()];
120 [timeSpentLabel setStringValue:CallModel::instance()->data(callIdx, (int)Call::Role::Length).toString().toNSString()];
121
122 Call::State state = CallModel::instance()->data(callIdx, (int)Call::Role::State).value<Call::State>();
123
124 switch (state) {
125 case Call::State::INITIALIZATION:
126 [stateLabel setStringValue:@"Initializing"];
127 break;
128 case Call::State::RINGING:
129 [stateLabel setStringValue:@"Ringing"];
130 break;
131 case Call::State::CURRENT:
132 [stateLabel setStringValue:@"Current"];
133 break;
134 case Call::State::HOLD:
135 [stateLabel setStringValue:@"On Hold"];
136 break;
137 case Call::State::BUSY:
138 [stateLabel setStringValue:@"Busy"];
139 break;
140 case Call::State::OVER:
141 [stateLabel setStringValue:@"Finished"];
142 break;
143 case Call::State::FAILURE:
144 [stateLabel setStringValue:@"Failure"];
145 break;
146 default:
147 break;
148 }
149
150}
151
152- (void)awakeFromNib
153{
154 NSLog(@"INIT CurrentCall VC");
155 [self.view setWantsLayer:YES]; // view's backing store is using a Core Animation Layer
156 [self.view setLayer:[CALayer layer]];
157 //self.view.layerContentsRedrawPolicy = NSViewLayerContentsRedrawOnSetNeedsDisplay;
158
159
160 [controlsPanel setWantsLayer:YES];
161 [controlsPanel setLayer:[CALayer layer]];
162 [controlsPanel.layer setZPosition:2.0];
163 [controlsPanel.layer setBackgroundColor:[NSColor whiteColor].CGColor];
164
165 actionHash[ (int)UserActionModel::Action::ACCEPT ] = pickUpButton;
166 actionHash[ (int)UserActionModel::Action::HOLD ] = holdOnOffButton;
167 actionHash[ (int)UserActionModel::Action::RECORD ] = recordOnOffButton;
168 actionHash[ (int)UserActionModel::Action::HANGUP ] = hangUpButton;
169 //actionHash[ (int)UserActionModel::Action::MUTE_AUDIO ] = action_mute_capture;
170 //actionHash[ (int)UserActionModel::Action::SERVER_TRANSFER ] = action_transfer;
171
172
173
174 videoLayer = [CALayer layer];
175 [videoView setWantsLayer:YES];
176 [videoView setLayer:videoLayer];
177 [videoView.layer setBackgroundColor:[NSColor blackColor].CGColor];
178 [videoView.layer setFrame:videoView.frame];
179 [videoView.layer setContentsGravity:kCAGravityResizeAspect];
180 //[videoView.layer setBounds:CGRectMake(0, 0, videoView.frame.size.width, videoView.frame.size.height)];
181
182 previewLayer = [CALayer layer];
183 [previewView setWantsLayer:YES];
184 [previewView setLayer:previewLayer];
185 [previewLayer setBackgroundColor:[NSColor blackColor].CGColor];
186 [previewLayer setContentsGravity:kCAGravityResizeAspect];
187 [previewLayer setFrame:previewView.frame];
188
189 [controlsPanel setWantsLayer:YES];
190 [controlsPanel setLayer:[CALayer layer]];
191 [controlsPanel.layer setBackgroundColor:[NSColor clearColor].CGColor];
192 [controlsPanel.layer setFrame:controlsPanel.frame];
193
194 [self connect];
195}
196
197- (void) connect
198{
199 QObject::connect(CallModel::instance()->selectionModel(),
200 &QItemSelectionModel::currentChanged,
201 [=](const QModelIndex &current, const QModelIndex &previous) {
202 NSLog(@"selection changed!");
203 if(!current.isValid()) {
204 [self animateOut];
205 return;
206 }
207 [self updateCall];
208 [self updateActions];
209 [self animateOut];
210 });
211
212 QObject::connect(CallModel::instance(),
213 &QAbstractItemModel::dataChanged,
214 [=](const QModelIndex &topLeft, const QModelIndex &bottomRight) {
215 NSLog(@"data changed!");
216 [self updateCall];
217 });
218
219 QObject::connect(CallModel::instance()->userActionModel(),
220 &QAbstractItemModel::dataChanged,
221 [=](const QModelIndex &topLeft, const QModelIndex &bottomRight) {
222 NSLog(@"useraction changed");
223 const int first(topLeft.row()),last(bottomRight.row());
224 for(int i = first; i <= last;i++) {
225 const QModelIndex& idx = CallModel::instance()->userActionModel()->index(i,0);
226 NSButton* a = actionHash[(int)qvariant_cast<UserActionModel::Action>(idx.data(UserActionModel::Role::ACTION))];
227 if (a) {
228 [a setEnabled:(idx.flags() & Qt::ItemIsEnabled)];
229 [a setState:(idx.data(Qt::CheckStateRole) == Qt::Checked) ? NSOnState : NSOffState];
230 }
231 }
232 });
233
234 QObject::connect(CallModel::instance(),
235 &CallModel::callStateChanged,
236 [self](Call* c, Call::State state) {
237 NSLog(@"callStateChanged");
238 [self updateCall];
239 });
240}
241
242-(void) connectVideoSignals
243{
244 QModelIndex idx = CallModel::instance()->selectionModel()->currentIndex();
245 Call* call = CallModel::instance()->getCall(idx);
246
247 QObject::connect(call,
248 &Call::videoStarted,
249 [=](Video::Renderer* renderer) {
250 NSLog(@"Video started!");
251 [self connectVideoRenderer:renderer];
252 });
253
254 if(call->videoRenderer())
255 {
256 NSLog(@"GONNA CONNECT TO FRAMES");
257 [self connectVideoRenderer:call->videoRenderer()];
258 }
259
260 [self connectPreviewRenderer];
261
262}
263
264-(void) connectPreviewRenderer
265{
266 previewHolder.started = QObject::connect(Video::PreviewManager::instance(),
267 &Video::PreviewManager::previewStarted,
268 [=](Video::Renderer* renderer) {
269 NSLog(@"Preview started");
270 QObject::disconnect(previewHolder.frameUpdated);
271 previewHolder.frameUpdated = QObject::connect(renderer,
272 &Video::Renderer::frameUpdated,
273 [=]() {
274 [self renderer:Video::PreviewManager::instance()->previewRenderer()
275 renderFrameForView:previewView];
276 });
277 });
278
279 previewHolder.stopped = QObject::connect(Video::PreviewManager::instance(),
280 &Video::PreviewManager::previewStopped,
281 [=](Video::Renderer* renderer) {
282 NSLog(@"Preview stopped");
283 QObject::disconnect(previewHolder.frameUpdated);
284 [previewView.layer setContents:nil];
285 });
286
287 previewHolder.frameUpdated = QObject::connect(Video::PreviewManager::instance()->previewRenderer(),
288 &Video::Renderer::frameUpdated,
289 [=]() {
290 [self renderer:Video::PreviewManager::instance()->previewRenderer()
291 renderFrameForView:previewView];
292 });
293}
294
295-(void) connectVideoRenderer: (Video::Renderer*)renderer
296{
297 videoHolder.frameUpdated = QObject::connect(renderer,
298 &Video::Renderer::frameUpdated,
299 [=]() {
300 [self renderer:renderer renderFrameForView:videoView];
301 });
302
303 videoHolder.started = QObject::connect(renderer,
304 &Video::Renderer::started,
305 [=]() {
306 NSLog(@"Renderer started");
307 QObject::disconnect(videoHolder.frameUpdated);
308 videoHolder.frameUpdated = QObject::connect(renderer,
309 &Video::Renderer::frameUpdated,
310 [=]() {
311 [self renderer:renderer renderFrameForView:videoView];
312 });
313 });
314
315 videoHolder.stopped = QObject::connect(renderer,
316 &Video::Renderer::stopped,
317 [=]() {
318 NSLog(@"Renderer stopped");
319 QObject::disconnect(videoHolder.frameUpdated);
320 [videoView.layer setContents:nil];
321 });
322}
323
324-(void) renderer: (Video::Renderer*)renderer renderFrameForView:(NSView*) view
325{
326 const QByteArray& data = renderer->currentFrame();
327 QSize res = renderer->size();
328
329 auto buf = reinterpret_cast<const unsigned char*>(data.data());
330
331 CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
332 CGContextRef newContext = CGBitmapContextCreate((void *)buf,
333 res.width(),
334 res.height(),
335 8,
336 4*res.width(),
337 colorSpace,
338 kCGImageAlphaPremultipliedLast);
339
340
341 CGImageRef newImage = CGBitmapContextCreateImage(newContext);
342
343 /*We release some components*/
344 CGContextRelease(newContext);
345 CGColorSpaceRelease(colorSpace);
346
347 [CATransaction begin];
348 view.layer.contents = (__bridge id)newImage;
349 [CATransaction commit];
350
351 CFRelease(newImage);
352}
353
354- (void) initFrame
355{
356 [self.view setFrame:self.view.superview.bounds];
357 [self.view setHidden:YES];
358 self.view.layer.position = self.view.frame.origin;
359}
360
361# pragma private IN/OUT animations
362
363-(void) animateIn
364{
365 NSLog(@"animateIn");
366 CGRect frame = CGRectOffset(self.view.superview.bounds, -self.view.superview.bounds.size.width, 0);
367 [self.view setHidden:NO];
368
369 [CATransaction begin];
370 CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
371 [animation setFromValue:[NSValue valueWithPoint:frame.origin]];
372 [animation setToValue:[NSValue valueWithPoint:self.view.superview.bounds.origin]];
373 [animation setDuration:0.2f];
374 [animation setTimingFunction:[CAMediaTimingFunction functionWithControlPoints:.7 :0.9 :1 :1]];
375 [CATransaction setCompletionBlock:^{
376 NSLog(@"COMPLETION IN");
377
378 [self connectVideoSignals];
379
380 }];
381 [self.view.layer addAnimation:animation forKey:animation.keyPath];
382
383 [CATransaction commit];
384}
385
386-(void) cleanUp
387{
388 [videoView.layer setContents:nil];
389 [previewView.layer setContents:nil];
390}
391
392-(void) animateOut
393{
394 NSLog(@"animateOut");
395 if(self.view.frame.origin.x < 0) {
396 NSLog(@"Already hidden");
397 if (CallModel::instance()->selectionModel()->currentIndex().isValid()) {
398 [self animateIn];
399 }
400 return;
401 }
402
403 CGRect frame = CGRectOffset(self.view.frame, -self.view.frame.size.width, 0);
404 [CATransaction begin];
405 CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
406 [animation setFromValue:[NSValue valueWithPoint:self.view.frame.origin]];
407 [animation setToValue:[NSValue valueWithPoint:frame.origin]];
408 [animation setDuration:0.2f];
409 [animation setTimingFunction:[CAMediaTimingFunction functionWithControlPoints:.7 :0.9 :1 :1]];
410
411 [CATransaction setCompletionBlock:^{
412 [self.view setHidden:YES];
413
414 [self cleanUp];
415
416 if (CallModel::instance()->selectionModel()->currentIndex().isValid()) {
417 [self animateIn];
418 }
419 }];
420 [self.view.layer addAnimation:animation forKey:animation.keyPath];
421 [CATransaction commit];
422}
423
424/**
425 * Debug purpose
426 */
427-(void) dumpFrame:(CGRect) frame WithName:(NSString*) name
428{
429 NSLog(@"frame %@ : %f %f %f %f \n\n",name ,frame.origin.x, frame.origin.y, frame.size.width, frame.size.height);
430}
431
432
433#pragma button methods
434- (IBAction)hangUp:(id)sender {
435 CallModel::instance()->getCall(CallModel::instance()->selectionModel()->currentIndex()) << Call::Action::REFUSE;
436}
437
438- (IBAction)accept:(id)sender {
439 CallModel::instance()->getCall(CallModel::instance()->selectionModel()->currentIndex()) << Call::Action::ACCEPT;
440}
441
442- (IBAction)toggleRecording:(id)sender {
443 CallModel::instance()->getCall(CallModel::instance()->selectionModel()->currentIndex()) << Call::Action::RECORD;
444}
445
446- (IBAction)toggleHold:(id)sender {
447 CallModel::instance()->getCall(CallModel::instance()->selectionModel()->currentIndex()) << Call::Action::HOLD;
448}
449
450@end