blob: 1c3efdda3ab23cfeccd6142b82b463f592a9190d [file] [log] [blame]
Alexandre Lisionc5148052015-03-04 15:10:35 -05001/*
Alexandre Lision9fe374b2016-01-06 10:17:31 -05002 * Copyright (C) 2015-2016 Savoir-faire Linux Inc.
Alexandre Lisionc5148052015-03-04 15:10:35 -05003 * 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.
Alexandre Lisionc5148052015-03-04 15:10:35 -050018 */
19#import "CurrentCallVC.h"
20
21#import <QuartzCore/QuartzCore.h>
22
23#import <call.h>
24#import <callmodel.h>
Alexandre Lision89edc6a2015-11-09 11:30:47 -050025#import <recentmodel.h>
Alexandre Lisionc5148052015-03-04 15:10:35 -050026#import <useractionmodel.h>
Alexandre Lision89edc6a2015-11-09 11:30:47 -050027#import <QMimeData>
Alexandre Lisionc5148052015-03-04 15:10:35 -050028#import <contactmethod.h>
29#import <qabstractitemmodel.h>
30#import <QItemSelectionModel>
31#import <QItemSelection>
Alexandre Lisionc5148052015-03-04 15:10:35 -050032#import <video/previewmanager.h>
33#import <video/renderer.h>
Alexandre Lision58cab672015-06-09 15:25:40 -040034#import <media/text.h>
Alexandre Lision2db8f472015-07-22 15:05:46 -040035#import <person.h>
Alexandre Lisionc5148052015-03-04 15:10:35 -050036
Alexandre Lisionf47a2562015-06-15 15:48:29 -040037#import "views/ITProgressIndicator.h"
Alexandre Lision74dd47f2015-04-14 13:47:42 -040038#import "views/CallView.h"
Alexandre Lision2db8f472015-07-22 15:05:46 -040039#import "PersonLinkerVC.h"
Alexandre Lision16d9c0a2015-08-10 12:05:15 -040040#import "ChatVC.h"
Alexandre Lision883719f2015-10-22 17:37:45 -040041#import "BrokerVC.h"
Alexandre Lision74dd47f2015-04-14 13:47:42 -040042
Alexandre Lisionc5148052015-03-04 15:10:35 -050043@interface RendererConnectionsHolder : NSObject
44
45@property QMetaObject::Connection frameUpdated;
46@property QMetaObject::Connection started;
47@property QMetaObject::Connection stopped;
48
49@end
50
51@implementation RendererConnectionsHolder
52
53@end
54
Alexandre Lision2db8f472015-07-22 15:05:46 -040055@interface CurrentCallVC () <NSPopoverDelegate, ContactLinkedDelegate>
Alexandre Lisionc5148052015-03-04 15:10:35 -050056
Alexandre Lision89edc6a2015-11-09 11:30:47 -050057// Header info
58@property (unsafe_unretained) IBOutlet NSView* headerContainer;
Alexandre Lision883719f2015-10-22 17:37:45 -040059@property (unsafe_unretained) IBOutlet NSTextField* personLabel;
60@property (unsafe_unretained) IBOutlet NSTextField* stateLabel;
Alexandre Lision89edc6a2015-11-09 11:30:47 -050061@property (unsafe_unretained) IBOutlet NSTextField* timeSpentLabel;
62
63// Call Controls
64@property (unsafe_unretained) IBOutlet NSView* controlsPanel;
Alexandre Lision883719f2015-10-22 17:37:45 -040065@property (unsafe_unretained) IBOutlet NSButton* holdOnOffButton;
66@property (unsafe_unretained) IBOutlet NSButton* hangUpButton;
67@property (unsafe_unretained) IBOutlet NSButton* recordOnOffButton;
68@property (unsafe_unretained) IBOutlet NSButton* pickUpButton;
69@property (unsafe_unretained) IBOutlet NSButton* muteAudioButton;
70@property (unsafe_unretained) IBOutlet NSButton* muteVideoButton;
71@property (unsafe_unretained) IBOutlet NSButton* addContactButton;
Alexandre Lision883719f2015-10-22 17:37:45 -040072@property (unsafe_unretained) IBOutlet NSButton* transferButton;
Alexandre Lision89edc6a2015-11-09 11:30:47 -050073@property (unsafe_unretained) IBOutlet NSButton* addParticipantButton;
74@property (unsafe_unretained) IBOutlet NSButton* chatButton;
Alexandre Lisiond18fa272015-06-15 11:18:03 -040075
Alexandre Lisionf47a2562015-06-15 15:48:29 -040076@property (unsafe_unretained) IBOutlet ITProgressIndicator *loadingIndicator;
Alexandre Lisiond18fa272015-06-15 11:18:03 -040077
Alexandre Lision89edc6a2015-11-09 11:30:47 -050078// Join call panel
79@property (unsafe_unretained) IBOutlet NSView* joinPanel;
80@property (unsafe_unretained) IBOutlet NSButton* mergeCallsButton;
81
Alexandre Lision883719f2015-10-22 17:37:45 -040082@property (unsafe_unretained) IBOutlet NSSplitView* splitView;
Alexandre Lisionc5148052015-03-04 15:10:35 -050083
Alexandre Lision2db8f472015-07-22 15:05:46 -040084@property (strong) NSPopover* addToContactPopover;
Alexandre Lision89edc6a2015-11-09 11:30:47 -050085@property (strong) NSPopover* brokerPopoverVC;
Alexandre Lision883719f2015-10-22 17:37:45 -040086@property (strong) IBOutlet ChatVC* chatVC;
Alexandre Lisionf23ec5a2015-07-16 11:24:06 -040087
Alexandre Lisionc5148052015-03-04 15:10:35 -050088@property QHash<int, NSButton*> actionHash;
89
90// Video
Alexandre Lision58cab672015-06-09 15:25:40 -040091@property (unsafe_unretained) IBOutlet CallView *videoView;
Alexandre Lisionc5148052015-03-04 15:10:35 -050092@property CALayer* videoLayer;
Alexandre Lision58cab672015-06-09 15:25:40 -040093@property (unsafe_unretained) IBOutlet NSView *previewView;
Alexandre Lisionc5148052015-03-04 15:10:35 -050094@property CALayer* previewLayer;
95
96@property RendererConnectionsHolder* previewHolder;
97@property RendererConnectionsHolder* videoHolder;
Alexandre Lisionef6333a2015-03-24 12:30:31 -040098@property QMetaObject::Connection videoStarted;
Alexandre Lisionb65c0272015-07-22 15:51:29 -040099@property QMetaObject::Connection messageConnection;
100@property QMetaObject::Connection mediaAddedConnection;
Alexandre Lisionc5148052015-03-04 15:10:35 -0500101
102@end
103
104@implementation CurrentCallVC
Alexandre Lisiond18fa272015-06-15 11:18:03 -0400105@synthesize personLabel, actionHash, stateLabel, holdOnOffButton, hangUpButton,
Alexandre Lision89edc6a2015-11-09 11:30:47 -0500106 recordOnOffButton, pickUpButton, chatButton, transferButton, addParticipantButton, timeSpentLabel,
Alexandre Lision883719f2015-10-22 17:37:45 -0400107 muteVideoButton, muteAudioButton, controlsPanel, headerContainer, videoView,
108 videoLayer, previewLayer, previewView, splitView, loadingIndicator;
Alexandre Lisionc5148052015-03-04 15:10:35 -0500109
110@synthesize previewHolder;
111@synthesize videoHolder;
112
Alexandre Lisionef6333a2015-03-24 12:30:31 -0400113- (void) updateAllActions
Alexandre Lisionc5148052015-03-04 15:10:35 -0500114{
Alexandre Lisionf7e11142015-12-15 10:58:11 -0500115 for (int i = 0 ; i < CallModel::instance().userActionModel()->rowCount() ; i++) {
Alexandre Lisionef6333a2015-03-24 12:30:31 -0400116 [self updateActionAtIndex:i];
117 }
118}
119
120- (void) updateActionAtIndex:(int) row
121{
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400122 const QModelIndex& idx = CallModel::instance().userActionModel()->index(row,0);
Alexandre Lisionef6333a2015-03-24 12:30:31 -0400123 UserActionModel::Action action = qvariant_cast<UserActionModel::Action>(idx.data(UserActionModel::Role::ACTION));
124 NSButton* a = actionHash[(int) action];
Alexandre Lision266fca02015-09-28 14:47:05 -0400125 if (a) {
126 [a setHidden:!(idx.flags() & Qt::ItemIsEnabled)];
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500127 [a setHighlighted:(idx.data(Qt::CheckStateRole) == Qt::Checked) ? YES : NO];
Alexandre Lisionc5148052015-03-04 15:10:35 -0500128 }
129}
130
131-(void) updateCall
132{
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400133 QModelIndex callIdx = CallModel::instance().selectionModel()->currentIndex();
Alexandre Lision2db8f472015-07-22 15:05:46 -0400134 if (!callIdx.isValid()) {
135 return;
136 }
Alexandre Lision89edc6a2015-11-09 11:30:47 -0500137 auto current = CallModel::instance().getCall(callIdx);
138
Alexandre Lision58cab672015-06-09 15:25:40 -0400139 [personLabel setStringValue:callIdx.data(Qt::DisplayRole).toString().toNSString()];
140 [timeSpentLabel setStringValue:callIdx.data((int)Call::Role::Length).toString().toNSString()];
Alexandre Lision57227772016-01-15 17:19:36 -0500141 [stateLabel setStringValue:callIdx.data((int)Call::Role::HumanStateName).toString().toNSString()];
Alexandre Lisionc5148052015-03-04 15:10:35 -0500142
Alexandre Lision2db8f472015-07-22 15:05:46 -0400143 auto contactmethod = qvariant_cast<Call*>(callIdx.data(static_cast<int>(Call::Role::Object)))->peerContactMethod();
144 BOOL shouldShow = (!contactmethod->contact() || contactmethod->contact()->isPlaceHolder());
145 [self.addContactButton setHidden:!shouldShow];
146
Alexandre Lision58cab672015-06-09 15:25:40 -0400147 Call::State state = callIdx.data((int)Call::Role::State).value<Call::State>();
Alexandre Lision266fca02015-09-28 14:47:05 -0400148
149 // Default values for this views
Alexandre Lisionf47a2562015-06-15 15:48:29 -0400150 [loadingIndicator setHidden:YES];
Alexandre Lision266fca02015-09-28 14:47:05 -0400151 [videoView setShouldAcceptInteractions:NO];
152
Alexandre Lision89edc6a2015-11-09 11:30:47 -0500153 [self.controlsPanel setHidden:current->hasParentCall()];
154 [self.joinPanel setHidden:!current->hasParentCall()];
Alexandre Lision266fca02015-09-28 14:47:05 -0400155
Alexandre Lisionc5148052015-03-04 15:10:35 -0500156 switch (state) {
Alexandre Lisione6dbf092015-04-11 17:19:35 -0400157 case Call::State::DIALING:
Alexandre Lisionf47a2562015-06-15 15:48:29 -0400158 [loadingIndicator setHidden:NO];
Alexandre Lisione6dbf092015-04-11 17:19:35 -0400159 break;
160 case Call::State::NEW:
Alexandre Lisione6dbf092015-04-11 17:19:35 -0400161 break;
Alexandre Lisionc5148052015-03-04 15:10:35 -0500162 case Call::State::INITIALIZATION:
Alexandre Lisionf47a2562015-06-15 15:48:29 -0400163 [loadingIndicator setHidden:NO];
164 break;
165 case Call::State::CONNECTED:
Alexandre Lisionf47a2562015-06-15 15:48:29 -0400166 [loadingIndicator setHidden:NO];
Alexandre Lisionc5148052015-03-04 15:10:35 -0500167 break;
168 case Call::State::RINGING:
Alexandre Lisionc5148052015-03-04 15:10:35 -0500169 break;
Alexandre Lision89edc6a2015-11-09 11:30:47 -0500170 case Call::State::CONFERENCE:
171 [videoView setShouldAcceptInteractions:YES];
172 [self.chatButton setHidden:NO];
173 [self.addParticipantButton setHidden:NO];
174 [self.transferButton setHidden:YES];
175 break;
Alexandre Lisionc5148052015-03-04 15:10:35 -0500176 case Call::State::CURRENT:
Alexandre Lision74dd47f2015-04-14 13:47:42 -0400177 [videoView setShouldAcceptInteractions:YES];
Alexandre Lision266fca02015-09-28 14:47:05 -0400178 [self.chatButton setHidden:NO];
Alexandre Lision89edc6a2015-11-09 11:30:47 -0500179 [self.addParticipantButton setHidden:NO];
180 [self.transferButton setHidden:NO];
Alexandre Lisionc5148052015-03-04 15:10:35 -0500181 break;
182 case Call::State::HOLD:
Alexandre Lisionc5148052015-03-04 15:10:35 -0500183 break;
184 case Call::State::BUSY:
Alexandre Lisionc5148052015-03-04 15:10:35 -0500185 break;
186 case Call::State::OVER:
Alexandre Lision266fca02015-09-28 14:47:05 -0400187 case Call::State::FAILURE:
Alexandre Lisiona1eee3c2015-08-10 13:44:51 -0400188 if(self.splitView.isInFullScreenMode)
189 [self.splitView exitFullScreenModeWithOptions:nil];
Alexandre Lisionc5148052015-03-04 15:10:35 -0500190 break;
Alexandre Lisionc5148052015-03-04 15:10:35 -0500191 }
192
193}
194
195- (void)awakeFromNib
196{
197 NSLog(@"INIT CurrentCall VC");
Alexandre Lisionef6333a2015-03-24 12:30:31 -0400198 [self.view setWantsLayer:YES];
Alexandre Lisionc5148052015-03-04 15:10:35 -0500199 [self.view setLayer:[CALayer layer]];
Alexandre Lisionc5148052015-03-04 15:10:35 -0500200
Alexandre Lisionef6333a2015-03-24 12:30:31 -0400201 actionHash[ (int)UserActionModel::Action::ACCEPT] = pickUpButton;
202 actionHash[ (int)UserActionModel::Action::HOLD ] = holdOnOffButton;
203 actionHash[ (int)UserActionModel::Action::RECORD] = recordOnOffButton;
204 actionHash[ (int)UserActionModel::Action::HANGUP] = hangUpButton;
Alexandre Lisiond18fa272015-06-15 11:18:03 -0400205 actionHash[ (int)UserActionModel::Action::MUTE_AUDIO] = muteAudioButton;
206 actionHash[ (int)UserActionModel::Action::MUTE_VIDEO] = muteVideoButton;
Alexandre Lisionc5148052015-03-04 15:10:35 -0500207
Alexandre Lisionc5148052015-03-04 15:10:35 -0500208 videoLayer = [CALayer layer];
209 [videoView setWantsLayer:YES];
210 [videoView setLayer:videoLayer];
211 [videoView.layer setBackgroundColor:[NSColor blackColor].CGColor];
212 [videoView.layer setFrame:videoView.frame];
213 [videoView.layer setContentsGravity:kCAGravityResizeAspect];
Alexandre Lisionc5148052015-03-04 15:10:35 -0500214
215 previewLayer = [CALayer layer];
216 [previewView setWantsLayer:YES];
217 [previewView setLayer:previewLayer];
218 [previewLayer setBackgroundColor:[NSColor blackColor].CGColor];
Alexandre Lisionef6333a2015-03-24 12:30:31 -0400219 [previewLayer setContentsGravity:kCAGravityResizeAspectFill];
Alexandre Lisionc5148052015-03-04 15:10:35 -0500220 [previewLayer setFrame:previewView.frame];
221
222 [controlsPanel setWantsLayer:YES];
223 [controlsPanel setLayer:[CALayer layer]];
224 [controlsPanel.layer setBackgroundColor:[NSColor clearColor].CGColor];
225 [controlsPanel.layer setFrame:controlsPanel.frame];
226
Alexandre Lisionef6333a2015-03-24 12:30:31 -0400227 previewHolder = [[RendererConnectionsHolder alloc] init];
228 videoHolder = [[RendererConnectionsHolder alloc] init];
229
Alexandre Lisionf47a2562015-06-15 15:48:29 -0400230 [loadingIndicator setColor:[NSColor whiteColor]];
231 [loadingIndicator setNumberOfLines:100];
232 [loadingIndicator setWidthOfLine:2];
233 [loadingIndicator setLengthOfLine:2];
234 [loadingIndicator setInnerMargin:30];
235
Alexandre Lisiona1eee3c2015-08-10 13:44:51 -0400236 [self.videoView setCallDelegate:self];
Alexandre Lision58cab672015-06-09 15:25:40 -0400237
Alexandre Lisionc5148052015-03-04 15:10:35 -0500238 [self connect];
239}
240
241- (void) connect
242{
Alexandre Lision89edc6a2015-11-09 11:30:47 -0500243 QObject::connect(RecentModel::instance().selectionModel(),
Alexandre Lisionc5148052015-03-04 15:10:35 -0500244 &QItemSelectionModel::currentChanged,
245 [=](const QModelIndex &current, const QModelIndex &previous) {
Alexandre Lision89edc6a2015-11-09 11:30:47 -0500246 auto call = RecentModel::instance().getActiveCall(current);
247 if(!current.isValid() || !call) {
Alexandre Lisionc5148052015-03-04 15:10:35 -0500248 return;
249 }
Alexandre Lisionbb5dbcd2015-07-09 16:36:47 -0400250
Alexandre Lision89edc6a2015-11-09 11:30:47 -0500251 CallModel::instance().selectCall(call);
252
Alexandre Lisionbb5dbcd2015-07-09 16:36:47 -0400253 if (call->state() == Call::State::HOLD) {
254 call << Call::Action::HOLD;
255 }
256
Alexandre Lision58cab672015-06-09 15:25:40 -0400257 [self collapseRightView];
Alexandre Lisionc5148052015-03-04 15:10:35 -0500258 [self updateCall];
Alexandre Lisionef6333a2015-03-24 12:30:31 -0400259 [self updateAllActions];
Alexandre Lisionc5148052015-03-04 15:10:35 -0500260 });
261
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400262 QObject::connect(CallModel::instance().userActionModel(),
Alexandre Lisionc5148052015-03-04 15:10:35 -0500263 &QAbstractItemModel::dataChanged,
264 [=](const QModelIndex &topLeft, const QModelIndex &bottomRight) {
Alexandre Lisionc5148052015-03-04 15:10:35 -0500265 const int first(topLeft.row()),last(bottomRight.row());
266 for(int i = first; i <= last;i++) {
Alexandre Lisionef6333a2015-03-24 12:30:31 -0400267 [self updateActionAtIndex:i];
Alexandre Lisionc5148052015-03-04 15:10:35 -0500268 }
269 });
270
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400271 QObject::connect(&CallModel::instance(),
Alexandre Lisionc5148052015-03-04 15:10:35 -0500272 &CallModel::callStateChanged,
273 [self](Call* c, Call::State state) {
Alexandre Lision57227772016-01-15 17:19:36 -0500274 auto current = CallModel::instance().selectionModel()->currentIndex();
275 if (!current.isValid())
276 [self animateOut];
277 else if (CallModel::instance().getIndex(c) == current) {
278 if (c->state() == Call::State::OVER) {
279 [self animateOut];
280 } else {
281 [self updateCall];
282 }
283 }
Alexandre Lisionc5148052015-03-04 15:10:35 -0500284 });
285}
286
Alexandre Lisionb65c0272015-07-22 15:51:29 -0400287- (void) monitorIncomingTextMessages:(Media::Text*) media
288{
289 /* connect to incoming chat messages to open the chat view */
290 QObject::disconnect(self.messageConnection);
291 self.messageConnection = QObject::connect(media,
292 &Media::Text::messageReceived,
293 [self] (const QMap<QString,QString>& m) {
294 if([[self splitView] isSubviewCollapsed:[[[self splitView] subviews] objectAtIndex: 1]])
295 [self uncollapseRightView];
296 });
297}
298
Alexandre Lisionc5148052015-03-04 15:10:35 -0500299-(void) connectVideoSignals
300{
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400301 QModelIndex idx = CallModel::instance().selectionModel()->currentIndex();
302 Call* call = CallModel::instance().getCall(idx);
Alexandre Lision89edc6a2015-11-09 11:30:47 -0500303 QObject::disconnect(self.videoStarted);
Alexandre Lision58cab672015-06-09 15:25:40 -0400304 self.videoStarted = QObject::connect(call,
Alexandre Lisionc5148052015-03-04 15:10:35 -0500305 &Call::videoStarted,
306 [=](Video::Renderer* renderer) {
Alexandre Lisionef6333a2015-03-24 12:30:31 -0400307 NSLog(@"Video started!");
Alexandre Lisionef6333a2015-03-24 12:30:31 -0400308 [self connectVideoRenderer:renderer];
Alexandre Lisionc5148052015-03-04 15:10:35 -0500309 });
310
311 if(call->videoRenderer())
312 {
Alexandre Lisionc5148052015-03-04 15:10:35 -0500313 [self connectVideoRenderer:call->videoRenderer()];
314 }
315
316 [self connectPreviewRenderer];
317
318}
319
320-(void) connectPreviewRenderer
321{
Alexandre Lisionef6333a2015-03-24 12:30:31 -0400322 QObject::disconnect(previewHolder.frameUpdated);
323 QObject::disconnect(previewHolder.stopped);
324 QObject::disconnect(previewHolder.started);
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400325 previewHolder.started = QObject::connect(&Video::PreviewManager::instance(),
Alexandre Lisionc5148052015-03-04 15:10:35 -0500326 &Video::PreviewManager::previewStarted,
327 [=](Video::Renderer* renderer) {
Alexandre Lisionc5148052015-03-04 15:10:35 -0500328 QObject::disconnect(previewHolder.frameUpdated);
329 previewHolder.frameUpdated = QObject::connect(renderer,
330 &Video::Renderer::frameUpdated,
331 [=]() {
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400332 [self renderer:Video::PreviewManager::instance().previewRenderer()
Alexandre Lisionc5148052015-03-04 15:10:35 -0500333 renderFrameForView:previewView];
334 });
335 });
336
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400337 previewHolder.stopped = QObject::connect(&Video::PreviewManager::instance(),
Alexandre Lisionc5148052015-03-04 15:10:35 -0500338 &Video::PreviewManager::previewStopped,
339 [=](Video::Renderer* renderer) {
Alexandre Lisionc5148052015-03-04 15:10:35 -0500340 QObject::disconnect(previewHolder.frameUpdated);
341 [previewView.layer setContents:nil];
342 });
343
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400344 previewHolder.frameUpdated = QObject::connect(Video::PreviewManager::instance().previewRenderer(),
Alexandre Lisionc5148052015-03-04 15:10:35 -0500345 &Video::Renderer::frameUpdated,
346 [=]() {
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400347 [self renderer:Video::PreviewManager::instance().previewRenderer()
Alexandre Lisionc5148052015-03-04 15:10:35 -0500348 renderFrameForView:previewView];
349 });
350}
351
352-(void) connectVideoRenderer: (Video::Renderer*)renderer
353{
Alexandre Lisionef6333a2015-03-24 12:30:31 -0400354 QObject::disconnect(videoHolder.frameUpdated);
355 QObject::disconnect(videoHolder.started);
356 QObject::disconnect(videoHolder.stopped);
Alexandre Lisionc5148052015-03-04 15:10:35 -0500357 videoHolder.frameUpdated = QObject::connect(renderer,
358 &Video::Renderer::frameUpdated,
359 [=]() {
360 [self renderer:renderer renderFrameForView:videoView];
361 });
362
363 videoHolder.started = QObject::connect(renderer,
364 &Video::Renderer::started,
365 [=]() {
Alexandre Lisionc5148052015-03-04 15:10:35 -0500366 QObject::disconnect(videoHolder.frameUpdated);
367 videoHolder.frameUpdated = QObject::connect(renderer,
368 &Video::Renderer::frameUpdated,
369 [=]() {
370 [self renderer:renderer renderFrameForView:videoView];
371 });
372 });
373
374 videoHolder.stopped = QObject::connect(renderer,
375 &Video::Renderer::stopped,
376 [=]() {
Alexandre Lisionc5148052015-03-04 15:10:35 -0500377 QObject::disconnect(videoHolder.frameUpdated);
378 [videoView.layer setContents:nil];
379 });
380}
381
382-(void) renderer: (Video::Renderer*)renderer renderFrameForView:(NSView*) view
383{
Alexandre Lisionc5148052015-03-04 15:10:35 -0500384 QSize res = renderer->size();
385
Alexandre Lision6731e132015-10-14 14:29:06 -0400386 auto frame_ptr = renderer->currentFrame();
387 auto frame_data = frame_ptr.ptr;
388 if (!frame_data)
389 return;
390
Alexandre Lisionc5148052015-03-04 15:10:35 -0500391
392 CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
Alexandre Lision6731e132015-10-14 14:29:06 -0400393 CGContextRef newContext = CGBitmapContextCreate(frame_data,
Alexandre Lisionc5148052015-03-04 15:10:35 -0500394 res.width(),
395 res.height(),
396 8,
397 4*res.width(),
398 colorSpace,
399 kCGImageAlphaPremultipliedLast);
400
401
402 CGImageRef newImage = CGBitmapContextCreateImage(newContext);
403
404 /*We release some components*/
405 CGContextRelease(newContext);
406 CGColorSpaceRelease(colorSpace);
407
408 [CATransaction begin];
409 view.layer.contents = (__bridge id)newImage;
410 [CATransaction commit];
411
412 CFRelease(newImage);
413}
414
415- (void) initFrame
416{
417 [self.view setFrame:self.view.superview.bounds];
418 [self.view setHidden:YES];
419 self.view.layer.position = self.view.frame.origin;
Alexandre Lisionbb5dbcd2015-07-09 16:36:47 -0400420 [self collapseRightView];
Alexandre Lisionc5148052015-03-04 15:10:35 -0500421}
422
423# pragma private IN/OUT animations
424
425-(void) animateIn
426{
427 NSLog(@"animateIn");
428 CGRect frame = CGRectOffset(self.view.superview.bounds, -self.view.superview.bounds.size.width, 0);
429 [self.view setHidden:NO];
430
431 [CATransaction begin];
432 CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
433 [animation setFromValue:[NSValue valueWithPoint:frame.origin]];
434 [animation setToValue:[NSValue valueWithPoint:self.view.superview.bounds.origin]];
435 [animation setDuration:0.2f];
436 [animation setTimingFunction:[CAMediaTimingFunction functionWithControlPoints:.7 :0.9 :1 :1]];
437 [CATransaction setCompletionBlock:^{
Alexandre Lisiona1eee3c2015-08-10 13:44:51 -0400438
439 // when call comes in we want to show the controls/header
440 [self mouseIsMoving:YES];
441
Alexandre Lisionc5148052015-03-04 15:10:35 -0500442 [self connectVideoSignals];
Alexandre Lisionb65c0272015-07-22 15:51:29 -0400443 /* check if text media is already present */
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400444 if(!CallModel::instance().selectedCall())
Alexandre Lision4dfcafc2015-08-20 12:43:23 -0400445 return;
Alexandre Lision21666f32015-09-22 17:04:36 -0400446
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400447 QObject::connect(CallModel::instance().selectedCall(),
Alexandre Lision21666f32015-09-22 17:04:36 -0400448 &Call::changed,
449 [=]() {
450 [self updateCall];
451 });
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400452 if (CallModel::instance().selectedCall()->hasMedia(Media::Media::Type::TEXT, Media::Media::Direction::IN)) {
453 Media::Text *text = CallModel::instance().selectedCall()->firstMedia<Media::Text>(Media::Media::Direction::IN);
Alexandre Lisionb65c0272015-07-22 15:51:29 -0400454 [self monitorIncomingTextMessages:text];
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400455 } else if (CallModel::instance().selectedCall()->hasMedia(Media::Media::Type::TEXT, Media::Media::Direction::OUT)) {
456 Media::Text *text = CallModel::instance().selectedCall()->firstMedia<Media::Text>(Media::Media::Direction::OUT);
Alexandre Lisionb65c0272015-07-22 15:51:29 -0400457 [self monitorIncomingTextMessages:text];
458 } else {
459 /* monitor media for messaging text messaging */
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400460 self.mediaAddedConnection = QObject::connect(CallModel::instance().selectedCall(),
Alexandre Lisionb65c0272015-07-22 15:51:29 -0400461 &Call::mediaAdded,
462 [self] (Media::Media* media) {
463 if (media->type() == Media::Media::Type::TEXT) { [self monitorIncomingTextMessages:(Media::Text*)media];
464 QObject::disconnect(self.mediaAddedConnection);
465 }
466 });
467 }
Alexandre Lisionc5148052015-03-04 15:10:35 -0500468 }];
469 [self.view.layer addAnimation:animation forKey:animation.keyPath];
470
471 [CATransaction commit];
472}
473
474-(void) cleanUp
475{
Alexandre Lisionef6333a2015-03-24 12:30:31 -0400476 QObject::disconnect(videoHolder.frameUpdated);
477 QObject::disconnect(videoHolder.started);
478 QObject::disconnect(videoHolder.stopped);
479 QObject::disconnect(previewHolder.frameUpdated);
480 QObject::disconnect(previewHolder.stopped);
481 QObject::disconnect(previewHolder.started);
Alexandre Lisionc5148052015-03-04 15:10:35 -0500482 [videoView.layer setContents:nil];
483 [previewView.layer setContents:nil];
Alexandre Lision883719f2015-10-22 17:37:45 -0400484
Alexandre Lision89edc6a2015-11-09 11:30:47 -0500485 [_brokerPopoverVC performClose:self];
Alexandre Lision883719f2015-10-22 17:37:45 -0400486 [self.addToContactPopover performClose:self];
Alexandre Lision0e66aea2015-11-02 16:36:30 -0500487
Alexandre Lision89edc6a2015-11-09 11:30:47 -0500488 [self.chatButton setHidden:YES];
489 [self.addParticipantButton setHidden:YES];
490 [self.transferButton setHidden:YES];
491
Alexandre Lision883719f2015-10-22 17:37:45 -0400492 [self.chatButton setState:NSOffState];
Alexandre Lision89edc6a2015-11-09 11:30:47 -0500493 [self.mergeCallsButton setState:NSOffState];
Alexandre Lision883719f2015-10-22 17:37:45 -0400494 [self collapseRightView];
Alexandre Lision57227772016-01-15 17:19:36 -0500495
496 [personLabel setStringValue:@""];
497 [timeSpentLabel setStringValue:@""];
498 [stateLabel setStringValue:@""];
499 [self.addContactButton setHidden:YES];
Alexandre Lisionc5148052015-03-04 15:10:35 -0500500}
501
502-(void) animateOut
503{
504 NSLog(@"animateOut");
505 if(self.view.frame.origin.x < 0) {
506 NSLog(@"Already hidden");
Alexandre Lisionc5148052015-03-04 15:10:35 -0500507 return;
508 }
509
510 CGRect frame = CGRectOffset(self.view.frame, -self.view.frame.size.width, 0);
511 [CATransaction begin];
512 CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
513 [animation setFromValue:[NSValue valueWithPoint:self.view.frame.origin]];
514 [animation setToValue:[NSValue valueWithPoint:frame.origin]];
515 [animation setDuration:0.2f];
516 [animation setTimingFunction:[CAMediaTimingFunction functionWithControlPoints:.7 :0.9 :1 :1]];
517
518 [CATransaction setCompletionBlock:^{
519 [self.view setHidden:YES];
Alexandre Lisionef6333a2015-03-24 12:30:31 -0400520 // first make sure everything is disconnected
Alexandre Lisionc5148052015-03-04 15:10:35 -0500521 [self cleanUp];
Alexandre Lision89edc6a2015-11-09 11:30:47 -0500522 if (RecentModel::instance().getActiveCall(RecentModel::instance().selectionModel()->currentIndex())) {
Alexandre Lisionc5148052015-03-04 15:10:35 -0500523 [self animateIn];
524 }
525 }];
526 [self.view.layer addAnimation:animation forKey:animation.keyPath];
Alexandre Lisiona1c6d752015-06-23 12:27:38 -0400527
528 [self.view.layer setPosition:frame.origin];
Alexandre Lisionc5148052015-03-04 15:10:35 -0500529 [CATransaction commit];
530}
531
532/**
533 * Debug purpose
534 */
535-(void) dumpFrame:(CGRect) frame WithName:(NSString*) name
536{
537 NSLog(@"frame %@ : %f %f %f %f \n\n",name ,frame.origin.x, frame.origin.y, frame.size.width, frame.size.height);
538}
539
Alexandre Lision58cab672015-06-09 15:25:40 -0400540-(void)collapseRightView
541{
542 NSView *right = [[splitView subviews] objectAtIndex:1];
543 NSView *left = [[splitView subviews] objectAtIndex:0];
544 NSRect leftFrame = [left frame];
545 [right setHidden:YES];
546 [splitView display];
547}
Alexandre Lisionc5148052015-03-04 15:10:35 -0500548
Alexandre Lision58cab672015-06-09 15:25:40 -0400549-(void)uncollapseRightView
550{
551 NSView *left = [[splitView subviews] objectAtIndex:0];
552 NSView *right = [[splitView subviews] objectAtIndex:1];
553 [right setHidden:NO];
554
555 CGFloat dividerThickness = [splitView dividerThickness];
556
557 // get the different frames
558 NSRect leftFrame = [left frame];
559 NSRect rightFrame = [right frame];
560
561 leftFrame.size.width = (leftFrame.size.width - rightFrame.size.width - dividerThickness);
562 rightFrame.origin.x = leftFrame.size.width + dividerThickness;
563 [left setFrameSize:leftFrame.size];
564 [right setFrame:rightFrame];
565 [splitView display];
Alexandre Lision16d9c0a2015-08-10 12:05:15 -0400566
567 [self.chatVC takeFocus];
Alexandre Lision58cab672015-06-09 15:25:40 -0400568}
569
570
571#pragma mark - Button methods
572
Alexandre Lision2db8f472015-07-22 15:05:46 -0400573- (IBAction)addToContact:(NSButton*) sender {
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400574 auto contactmethod = CallModel::instance().getCall(CallModel::instance().selectionModel()->currentIndex())->peerContactMethod();
Alexandre Lision2db8f472015-07-22 15:05:46 -0400575
576 if (self.addToContactPopover != nullptr) {
577 [self.addToContactPopover performClose:self];
578 self.addToContactPopover = NULL;
579 } else if (!contactmethod->contact() || contactmethod->contact()->isPlaceHolder()) {
580 auto* editorVC = [[PersonLinkerVC alloc] initWithNibName:@"PersonLinker" bundle:nil];
581 [editorVC setMethodToLink:contactmethod];
582 [editorVC setContactLinkedDelegate:self];
583 self.addToContactPopover = [[NSPopover alloc] init];
584 [self.addToContactPopover setContentSize:editorVC.view.frame.size];
585 [self.addToContactPopover setContentViewController:editorVC];
586 [self.addToContactPopover setAnimates:YES];
587 [self.addToContactPopover setBehavior:NSPopoverBehaviorTransient];
588 [self.addToContactPopover setDelegate:self];
589
590 [self.addToContactPopover showRelativeToRect:sender.bounds ofView:sender preferredEdge:NSMaxXEdge];
591 }
Alexandre Lision883719f2015-10-22 17:37:45 -0400592
593 [videoView setCallDelegate:nil];
Alexandre Lision2db8f472015-07-22 15:05:46 -0400594}
595
Alexandre Lisionc5148052015-03-04 15:10:35 -0500596- (IBAction)hangUp:(id)sender {
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400597 CallModel::instance().getCall(CallModel::instance().selectionModel()->currentIndex()) << Call::Action::REFUSE;
Alexandre Lisionc5148052015-03-04 15:10:35 -0500598}
599
600- (IBAction)accept:(id)sender {
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400601 CallModel::instance().getCall(CallModel::instance().selectionModel()->currentIndex()) << Call::Action::ACCEPT;
Alexandre Lisionc5148052015-03-04 15:10:35 -0500602}
603
604- (IBAction)toggleRecording:(id)sender {
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400605 CallModel::instance().getCall(CallModel::instance().selectionModel()->currentIndex()) << Call::Action::RECORD_AUDIO;
Alexandre Lisionc5148052015-03-04 15:10:35 -0500606}
607
608- (IBAction)toggleHold:(id)sender {
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400609 CallModel::instance().getCall(CallModel::instance().selectionModel()->currentIndex()) << Call::Action::HOLD;
Alexandre Lisionc5148052015-03-04 15:10:35 -0500610}
611
Alexandre Lision58cab672015-06-09 15:25:40 -0400612-(IBAction)toggleChat:(id)sender;
613{
614 BOOL rightViewCollapsed = [[self splitView] isSubviewCollapsed:[[[self splitView] subviews] objectAtIndex: 1]];
615 if (rightViewCollapsed) {
616 [self uncollapseRightView];
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400617 CallModel::instance().getCall(CallModel::instance().selectionModel()->currentIndex())->addOutgoingMedia<Media::Text>();
Alexandre Lision58cab672015-06-09 15:25:40 -0400618 } else {
619 [self collapseRightView];
620 }
621 [chatButton setState:rightViewCollapsed];
622}
623
Alexandre Lisiond18fa272015-06-15 11:18:03 -0400624- (IBAction)muteAudio:(id)sender
625{
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400626 UserActionModel* uam = CallModel::instance().userActionModel();
Alexandre Lisiond18fa272015-06-15 11:18:03 -0400627 uam << UserActionModel::Action::MUTE_AUDIO;
628}
629
630- (IBAction)muteVideo:(id)sender
631{
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400632 UserActionModel* uam = CallModel::instance().userActionModel();
Alexandre Lisiond18fa272015-06-15 11:18:03 -0400633 uam << UserActionModel::Action::MUTE_VIDEO;
634}
Alexandre Lision883719f2015-10-22 17:37:45 -0400635
636- (IBAction)toggleTransferView:(id)sender {
Alexandre Lision89edc6a2015-11-09 11:30:47 -0500637 if (_brokerPopoverVC != nullptr) {
638 [_brokerPopoverVC performClose:self];
639 _brokerPopoverVC = NULL;
Alexandre Lision883719f2015-10-22 17:37:45 -0400640 [self.transferButton setState:NSOffState];
641 } else {
Alexandre Lision89edc6a2015-11-09 11:30:47 -0500642 auto* brokerVC = [[BrokerVC alloc] initWithMode:BrokerMode::TRANSFER];
643 _brokerPopoverVC = [[NSPopover alloc] init];
644 [_brokerPopoverVC setContentSize:brokerVC.view.frame.size];
645 [_brokerPopoverVC setContentViewController:brokerVC];
646 [_brokerPopoverVC setAnimates:YES];
647 [_brokerPopoverVC setBehavior:NSPopoverBehaviorTransient];
648 [_brokerPopoverVC setDelegate:self];
649 [_brokerPopoverVC showRelativeToRect:[sender bounds] ofView:sender preferredEdge:NSMinYEdge];
Alexandre Lision883719f2015-10-22 17:37:45 -0400650 [videoView setCallDelegate:nil];
651 }
Alexandre Lisionf23ec5a2015-07-16 11:24:06 -0400652}
Alexandre Lisiond18fa272015-06-15 11:18:03 -0400653
Alexandre Lision89edc6a2015-11-09 11:30:47 -0500654- (IBAction)toggleAddParticipantView:(id)sender {
655 if (_brokerPopoverVC != nullptr) {
656 [_brokerPopoverVC performClose:self];
657 _brokerPopoverVC = NULL;
658 [self.addParticipantButton setState:NSOffState];
659 } else {
660 auto* brokerVC = [[BrokerVC alloc] initWithMode:BrokerMode::CONFERENCE];
661 _brokerPopoverVC = [[NSPopover alloc] init];
662 [_brokerPopoverVC setContentSize:brokerVC.view.frame.size];
663 [_brokerPopoverVC setContentViewController:brokerVC];
664 [_brokerPopoverVC setAnimates:YES];
665 [_brokerPopoverVC setBehavior:NSPopoverBehaviorTransient];
666 [_brokerPopoverVC setDelegate:self];
667 [_brokerPopoverVC showRelativeToRect:[sender bounds] ofView:sender preferredEdge:NSMinYEdge];
668 [videoView setCallDelegate:nil];
669 }
670}
671
672/**
673 * Merge current call with its parent call
674 */
675- (IBAction)mergeCalls:(id)sender
676{
677 auto current = CallModel::instance().selectedCall();
678 current->joinToParent();
679}
680
Alexandre Lision2db8f472015-07-22 15:05:46 -0400681#pragma mark - NSPopOverDelegate
682
Alexandre Lision266fca02015-09-28 14:47:05 -0400683- (void)popoverWillClose:(NSNotification *)notification
684{
Alexandre Lision89edc6a2015-11-09 11:30:47 -0500685 if (_brokerPopoverVC != nullptr) {
686 [_brokerPopoverVC performClose:self];
687 _brokerPopoverVC = NULL;
Alexandre Lision883719f2015-10-22 17:37:45 -0400688 }
Alexandre Lision266fca02015-09-28 14:47:05 -0400689
Alexandre Lision2db8f472015-07-22 15:05:46 -0400690 if (self.addToContactPopover != nullptr) {
691 [self.addToContactPopover performClose:self];
692 self.addToContactPopover = NULL;
693 }
Alexandre Lision883719f2015-10-22 17:37:45 -0400694
Alexandre Lision883719f2015-10-22 17:37:45 -0400695 [self.addContactButton setState:NSOffState];
696 [self.transferButton setState:NSOffState];
Alexandre Lision89edc6a2015-11-09 11:30:47 -0500697 [self.addParticipantButton setState:NSOffState];
Alexandre Lision883719f2015-10-22 17:37:45 -0400698}
699
700- (void)popoverDidClose:(NSNotification *)notification
701{
702 [videoView setCallDelegate:self];
Alexandre Lision2db8f472015-07-22 15:05:46 -0400703}
704
705#pragma mark - ContactLinkedDelegate
706
707- (void)contactLinked
708{
709 if (self.addToContactPopover != nullptr) {
710 [self.addToContactPopover performClose:self];
711 self.addToContactPopover = NULL;
712 }
713}
714
Alexandre Lision58cab672015-06-09 15:25:40 -0400715#pragma mark - NSSplitViewDelegate
716
717/* Return YES if the subview should be collapsed because the user has double-clicked on an adjacent divider. If a split view has a delegate, and the delegate responds to this message, it will be sent once for the subview before a divider when the user double-clicks on that divider, and again for the subview after the divider, but only if the delegate returned YES when sent -splitView:canCollapseSubview: for the subview in question. When the delegate indicates that both subviews should be collapsed NSSplitView's behavior is undefined.
718 */
719- (BOOL)splitView:(NSSplitView *)splitView shouldCollapseSubview:(NSView *)subview forDoubleClickOnDividerAtIndex:(NSInteger)dividerIndex;
720{
721 NSView* rightView = [[splitView subviews] objectAtIndex:1];
722 return ([subview isEqual:rightView]);
723}
724
725
726- (BOOL)splitView:(NSSplitView *)splitView canCollapseSubview:(NSView *)subview;
727{
728 NSView* rightView = [[splitView subviews] objectAtIndex:1];
729 return ([subview isEqual:rightView]);
730}
731
732
Alexandre Lisiona1eee3c2015-08-10 13:44:51 -0400733# pragma mark - CallnDelegate
Alexandre Lision58cab672015-06-09 15:25:40 -0400734
735- (void) callShouldToggleFullScreen
736{
737 if(self.splitView.isInFullScreenMode)
738 [self.splitView exitFullScreenModeWithOptions:nil];
739 else {
740 NSApplicationPresentationOptions options = NSApplicationPresentationDefault +NSApplicationPresentationAutoHideDock +
741 NSApplicationPresentationAutoHideMenuBar + NSApplicationPresentationAutoHideToolbar;
742 NSDictionary *opts = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithUnsignedInt:options],
743 NSFullScreenModeApplicationPresentationOptions, nil];
744
745 [self.splitView enterFullScreenMode:[NSScreen mainScreen] withOptions:opts];
746 }
747}
748
Alexandre Lisiona1eee3c2015-08-10 13:44:51 -0400749-(void) mouseIsMoving:(BOOL) move
750{
751 [[controlsPanel animator] setAlphaValue:move]; // fade out
752 [[headerContainer animator] setAlphaValue:move];
753}
754
Alexandre Lisionc5148052015-03-04 15:10:35 -0500755@end