blob: 21ec62e35913f47e86d505a47f589d9052e553ed [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 Lisionef6333a2015-03-24 12:30:31 -0400127 [a setState:(idx.data(Qt::CheckStateRole) == Qt::Checked) ? NSOnState : NSOffState];
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 Lisionc5148052015-03-04 15:10:35 -0500141
Alexandre Lision2db8f472015-07-22 15:05:46 -0400142 auto contactmethod = qvariant_cast<Call*>(callIdx.data(static_cast<int>(Call::Role::Object)))->peerContactMethod();
143 BOOL shouldShow = (!contactmethod->contact() || contactmethod->contact()->isPlaceHolder());
144 [self.addContactButton setHidden:!shouldShow];
145
Alexandre Lision58cab672015-06-09 15:25:40 -0400146 Call::State state = callIdx.data((int)Call::Role::State).value<Call::State>();
Alexandre Lision266fca02015-09-28 14:47:05 -0400147
148 // Default values for this views
Alexandre Lisionf47a2562015-06-15 15:48:29 -0400149 [loadingIndicator setHidden:YES];
Alexandre Lision266fca02015-09-28 14:47:05 -0400150 [videoView setShouldAcceptInteractions:NO];
151
Alexandre Lision89edc6a2015-11-09 11:30:47 -0500152 [self.controlsPanel setHidden:current->hasParentCall()];
153 [self.joinPanel setHidden:!current->hasParentCall()];
Alexandre Lision266fca02015-09-28 14:47:05 -0400154
Alexandre Lisionf47a2562015-06-15 15:48:29 -0400155 [stateLabel setStringValue:callIdx.data((int)Call::Role::HumanStateName).toString().toNSString()];
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 [self animateOut];
249 return;
250 }
Alexandre Lisionbb5dbcd2015-07-09 16:36:47 -0400251
Alexandre Lision89edc6a2015-11-09 11:30:47 -0500252 CallModel::instance().selectCall(call);
253
Alexandre Lisionbb5dbcd2015-07-09 16:36:47 -0400254 if (call->state() == Call::State::HOLD) {
255 call << Call::Action::HOLD;
256 }
257
Alexandre Lision58cab672015-06-09 15:25:40 -0400258 [self collapseRightView];
Alexandre Lisionc5148052015-03-04 15:10:35 -0500259 [self updateCall];
Alexandre Lisionef6333a2015-03-24 12:30:31 -0400260 [self updateAllActions];
Alexandre Lisionc5148052015-03-04 15:10:35 -0500261 [self animateOut];
262 });
263
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400264 QObject::connect(CallModel::instance().userActionModel(),
Alexandre Lisionc5148052015-03-04 15:10:35 -0500265 &QAbstractItemModel::dataChanged,
266 [=](const QModelIndex &topLeft, const QModelIndex &bottomRight) {
Alexandre Lisionc5148052015-03-04 15:10:35 -0500267 const int first(topLeft.row()),last(bottomRight.row());
268 for(int i = first; i <= last;i++) {
Alexandre Lisionef6333a2015-03-24 12:30:31 -0400269 [self updateActionAtIndex:i];
Alexandre Lisionc5148052015-03-04 15:10:35 -0500270 }
271 });
272
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400273 QObject::connect(&CallModel::instance(),
Alexandre Lisionc5148052015-03-04 15:10:35 -0500274 &CallModel::callStateChanged,
275 [self](Call* c, Call::State state) {
Alexandre Lisionc5148052015-03-04 15:10:35 -0500276 [self updateCall];
277 });
278}
279
Alexandre Lisionb65c0272015-07-22 15:51:29 -0400280- (void) monitorIncomingTextMessages:(Media::Text*) media
281{
282 /* connect to incoming chat messages to open the chat view */
283 QObject::disconnect(self.messageConnection);
284 self.messageConnection = QObject::connect(media,
285 &Media::Text::messageReceived,
286 [self] (const QMap<QString,QString>& m) {
287 if([[self splitView] isSubviewCollapsed:[[[self splitView] subviews] objectAtIndex: 1]])
288 [self uncollapseRightView];
289 });
290}
291
Alexandre Lisionc5148052015-03-04 15:10:35 -0500292-(void) connectVideoSignals
293{
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400294 QModelIndex idx = CallModel::instance().selectionModel()->currentIndex();
295 Call* call = CallModel::instance().getCall(idx);
Alexandre Lision89edc6a2015-11-09 11:30:47 -0500296 QObject::disconnect(self.videoStarted);
Alexandre Lision58cab672015-06-09 15:25:40 -0400297 self.videoStarted = QObject::connect(call,
Alexandre Lisionc5148052015-03-04 15:10:35 -0500298 &Call::videoStarted,
299 [=](Video::Renderer* renderer) {
Alexandre Lisionef6333a2015-03-24 12:30:31 -0400300 NSLog(@"Video started!");
Alexandre Lisionef6333a2015-03-24 12:30:31 -0400301 [self connectVideoRenderer:renderer];
Alexandre Lisionc5148052015-03-04 15:10:35 -0500302 });
303
304 if(call->videoRenderer())
305 {
Alexandre Lisionc5148052015-03-04 15:10:35 -0500306 [self connectVideoRenderer:call->videoRenderer()];
307 }
308
309 [self connectPreviewRenderer];
310
311}
312
313-(void) connectPreviewRenderer
314{
Alexandre Lisionef6333a2015-03-24 12:30:31 -0400315 QObject::disconnect(previewHolder.frameUpdated);
316 QObject::disconnect(previewHolder.stopped);
317 QObject::disconnect(previewHolder.started);
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400318 previewHolder.started = QObject::connect(&Video::PreviewManager::instance(),
Alexandre Lisionc5148052015-03-04 15:10:35 -0500319 &Video::PreviewManager::previewStarted,
320 [=](Video::Renderer* renderer) {
Alexandre Lisionc5148052015-03-04 15:10:35 -0500321 QObject::disconnect(previewHolder.frameUpdated);
322 previewHolder.frameUpdated = QObject::connect(renderer,
323 &Video::Renderer::frameUpdated,
324 [=]() {
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400325 [self renderer:Video::PreviewManager::instance().previewRenderer()
Alexandre Lisionc5148052015-03-04 15:10:35 -0500326 renderFrameForView:previewView];
327 });
328 });
329
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400330 previewHolder.stopped = QObject::connect(&Video::PreviewManager::instance(),
Alexandre Lisionc5148052015-03-04 15:10:35 -0500331 &Video::PreviewManager::previewStopped,
332 [=](Video::Renderer* renderer) {
Alexandre Lisionc5148052015-03-04 15:10:35 -0500333 QObject::disconnect(previewHolder.frameUpdated);
334 [previewView.layer setContents:nil];
335 });
336
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400337 previewHolder.frameUpdated = QObject::connect(Video::PreviewManager::instance().previewRenderer(),
Alexandre Lisionc5148052015-03-04 15:10:35 -0500338 &Video::Renderer::frameUpdated,
339 [=]() {
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400340 [self renderer:Video::PreviewManager::instance().previewRenderer()
Alexandre Lisionc5148052015-03-04 15:10:35 -0500341 renderFrameForView:previewView];
342 });
343}
344
345-(void) connectVideoRenderer: (Video::Renderer*)renderer
346{
Alexandre Lisionef6333a2015-03-24 12:30:31 -0400347 QObject::disconnect(videoHolder.frameUpdated);
348 QObject::disconnect(videoHolder.started);
349 QObject::disconnect(videoHolder.stopped);
Alexandre Lisionc5148052015-03-04 15:10:35 -0500350 videoHolder.frameUpdated = QObject::connect(renderer,
351 &Video::Renderer::frameUpdated,
352 [=]() {
353 [self renderer:renderer renderFrameForView:videoView];
354 });
355
356 videoHolder.started = QObject::connect(renderer,
357 &Video::Renderer::started,
358 [=]() {
Alexandre Lisionc5148052015-03-04 15:10:35 -0500359 QObject::disconnect(videoHolder.frameUpdated);
360 videoHolder.frameUpdated = QObject::connect(renderer,
361 &Video::Renderer::frameUpdated,
362 [=]() {
363 [self renderer:renderer renderFrameForView:videoView];
364 });
365 });
366
367 videoHolder.stopped = QObject::connect(renderer,
368 &Video::Renderer::stopped,
369 [=]() {
Alexandre Lisionc5148052015-03-04 15:10:35 -0500370 QObject::disconnect(videoHolder.frameUpdated);
371 [videoView.layer setContents:nil];
372 });
373}
374
375-(void) renderer: (Video::Renderer*)renderer renderFrameForView:(NSView*) view
376{
Alexandre Lisionc5148052015-03-04 15:10:35 -0500377 QSize res = renderer->size();
378
Alexandre Lision6731e132015-10-14 14:29:06 -0400379 auto frame_ptr = renderer->currentFrame();
380 auto frame_data = frame_ptr.ptr;
381 if (!frame_data)
382 return;
383
Alexandre Lisionc5148052015-03-04 15:10:35 -0500384
385 CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
Alexandre Lision6731e132015-10-14 14:29:06 -0400386 CGContextRef newContext = CGBitmapContextCreate(frame_data,
Alexandre Lisionc5148052015-03-04 15:10:35 -0500387 res.width(),
388 res.height(),
389 8,
390 4*res.width(),
391 colorSpace,
392 kCGImageAlphaPremultipliedLast);
393
394
395 CGImageRef newImage = CGBitmapContextCreateImage(newContext);
396
397 /*We release some components*/
398 CGContextRelease(newContext);
399 CGColorSpaceRelease(colorSpace);
400
401 [CATransaction begin];
402 view.layer.contents = (__bridge id)newImage;
403 [CATransaction commit];
404
405 CFRelease(newImage);
406}
407
408- (void) initFrame
409{
410 [self.view setFrame:self.view.superview.bounds];
411 [self.view setHidden:YES];
412 self.view.layer.position = self.view.frame.origin;
Alexandre Lisionbb5dbcd2015-07-09 16:36:47 -0400413 [self collapseRightView];
Alexandre Lisionc5148052015-03-04 15:10:35 -0500414}
415
416# pragma private IN/OUT animations
417
418-(void) animateIn
419{
420 NSLog(@"animateIn");
421 CGRect frame = CGRectOffset(self.view.superview.bounds, -self.view.superview.bounds.size.width, 0);
422 [self.view setHidden:NO];
423
424 [CATransaction begin];
425 CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
426 [animation setFromValue:[NSValue valueWithPoint:frame.origin]];
427 [animation setToValue:[NSValue valueWithPoint:self.view.superview.bounds.origin]];
428 [animation setDuration:0.2f];
429 [animation setTimingFunction:[CAMediaTimingFunction functionWithControlPoints:.7 :0.9 :1 :1]];
430 [CATransaction setCompletionBlock:^{
Alexandre Lisiona1eee3c2015-08-10 13:44:51 -0400431
432 // when call comes in we want to show the controls/header
433 [self mouseIsMoving:YES];
434
Alexandre Lisionc5148052015-03-04 15:10:35 -0500435 [self connectVideoSignals];
Alexandre Lisionb65c0272015-07-22 15:51:29 -0400436 /* check if text media is already present */
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400437 if(!CallModel::instance().selectedCall())
Alexandre Lision4dfcafc2015-08-20 12:43:23 -0400438 return;
Alexandre Lision21666f32015-09-22 17:04:36 -0400439
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400440 QObject::connect(CallModel::instance().selectedCall(),
Alexandre Lision21666f32015-09-22 17:04:36 -0400441 &Call::changed,
442 [=]() {
443 [self updateCall];
444 });
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400445 if (CallModel::instance().selectedCall()->hasMedia(Media::Media::Type::TEXT, Media::Media::Direction::IN)) {
446 Media::Text *text = CallModel::instance().selectedCall()->firstMedia<Media::Text>(Media::Media::Direction::IN);
Alexandre Lisionb65c0272015-07-22 15:51:29 -0400447 [self monitorIncomingTextMessages:text];
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400448 } else if (CallModel::instance().selectedCall()->hasMedia(Media::Media::Type::TEXT, Media::Media::Direction::OUT)) {
449 Media::Text *text = CallModel::instance().selectedCall()->firstMedia<Media::Text>(Media::Media::Direction::OUT);
Alexandre Lisionb65c0272015-07-22 15:51:29 -0400450 [self monitorIncomingTextMessages:text];
451 } else {
452 /* monitor media for messaging text messaging */
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400453 self.mediaAddedConnection = QObject::connect(CallModel::instance().selectedCall(),
Alexandre Lisionb65c0272015-07-22 15:51:29 -0400454 &Call::mediaAdded,
455 [self] (Media::Media* media) {
456 if (media->type() == Media::Media::Type::TEXT) { [self monitorIncomingTextMessages:(Media::Text*)media];
457 QObject::disconnect(self.mediaAddedConnection);
458 }
459 });
460 }
Alexandre Lisionc5148052015-03-04 15:10:35 -0500461 }];
462 [self.view.layer addAnimation:animation forKey:animation.keyPath];
463
464 [CATransaction commit];
465}
466
467-(void) cleanUp
468{
Alexandre Lisionef6333a2015-03-24 12:30:31 -0400469 QObject::disconnect(videoHolder.frameUpdated);
470 QObject::disconnect(videoHolder.started);
471 QObject::disconnect(videoHolder.stopped);
472 QObject::disconnect(previewHolder.frameUpdated);
473 QObject::disconnect(previewHolder.stopped);
474 QObject::disconnect(previewHolder.started);
Alexandre Lisionc5148052015-03-04 15:10:35 -0500475 [videoView.layer setContents:nil];
476 [previewView.layer setContents:nil];
Alexandre Lision883719f2015-10-22 17:37:45 -0400477
Alexandre Lision89edc6a2015-11-09 11:30:47 -0500478 [_brokerPopoverVC performClose:self];
Alexandre Lision883719f2015-10-22 17:37:45 -0400479 [self.addToContactPopover performClose:self];
Alexandre Lision0e66aea2015-11-02 16:36:30 -0500480
Alexandre Lision89edc6a2015-11-09 11:30:47 -0500481 [self.chatButton setHidden:YES];
482 [self.addParticipantButton setHidden:YES];
483 [self.transferButton setHidden:YES];
484
Alexandre Lision883719f2015-10-22 17:37:45 -0400485 [self.chatButton setState:NSOffState];
Alexandre Lision89edc6a2015-11-09 11:30:47 -0500486 [self.mergeCallsButton setState:NSOffState];
Alexandre Lision883719f2015-10-22 17:37:45 -0400487 [self collapseRightView];
Alexandre Lisionc5148052015-03-04 15:10:35 -0500488}
489
490-(void) animateOut
491{
492 NSLog(@"animateOut");
493 if(self.view.frame.origin.x < 0) {
494 NSLog(@"Already hidden");
Alexandre Lisionc5148052015-03-04 15:10:35 -0500495 return;
496 }
497
498 CGRect frame = CGRectOffset(self.view.frame, -self.view.frame.size.width, 0);
499 [CATransaction begin];
500 CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
501 [animation setFromValue:[NSValue valueWithPoint:self.view.frame.origin]];
502 [animation setToValue:[NSValue valueWithPoint:frame.origin]];
503 [animation setDuration:0.2f];
504 [animation setTimingFunction:[CAMediaTimingFunction functionWithControlPoints:.7 :0.9 :1 :1]];
505
506 [CATransaction setCompletionBlock:^{
507 [self.view setHidden:YES];
Alexandre Lisionef6333a2015-03-24 12:30:31 -0400508 // first make sure everything is disconnected
Alexandre Lisionc5148052015-03-04 15:10:35 -0500509 [self cleanUp];
Alexandre Lision89edc6a2015-11-09 11:30:47 -0500510 if (RecentModel::instance().getActiveCall(RecentModel::instance().selectionModel()->currentIndex())) {
Alexandre Lisionc5148052015-03-04 15:10:35 -0500511 [self animateIn];
512 }
513 }];
514 [self.view.layer addAnimation:animation forKey:animation.keyPath];
Alexandre Lisiona1c6d752015-06-23 12:27:38 -0400515
516 [self.view.layer setPosition:frame.origin];
Alexandre Lisionc5148052015-03-04 15:10:35 -0500517 [CATransaction commit];
518}
519
520/**
521 * Debug purpose
522 */
523-(void) dumpFrame:(CGRect) frame WithName:(NSString*) name
524{
525 NSLog(@"frame %@ : %f %f %f %f \n\n",name ,frame.origin.x, frame.origin.y, frame.size.width, frame.size.height);
526}
527
Alexandre Lision58cab672015-06-09 15:25:40 -0400528-(void)collapseRightView
529{
530 NSView *right = [[splitView subviews] objectAtIndex:1];
531 NSView *left = [[splitView subviews] objectAtIndex:0];
532 NSRect leftFrame = [left frame];
533 [right setHidden:YES];
534 [splitView display];
535}
Alexandre Lisionc5148052015-03-04 15:10:35 -0500536
Alexandre Lision58cab672015-06-09 15:25:40 -0400537-(void)uncollapseRightView
538{
539 NSView *left = [[splitView subviews] objectAtIndex:0];
540 NSView *right = [[splitView subviews] objectAtIndex:1];
541 [right setHidden:NO];
542
543 CGFloat dividerThickness = [splitView dividerThickness];
544
545 // get the different frames
546 NSRect leftFrame = [left frame];
547 NSRect rightFrame = [right frame];
548
549 leftFrame.size.width = (leftFrame.size.width - rightFrame.size.width - dividerThickness);
550 rightFrame.origin.x = leftFrame.size.width + dividerThickness;
551 [left setFrameSize:leftFrame.size];
552 [right setFrame:rightFrame];
553 [splitView display];
Alexandre Lision16d9c0a2015-08-10 12:05:15 -0400554
555 [self.chatVC takeFocus];
Alexandre Lision58cab672015-06-09 15:25:40 -0400556}
557
558
559#pragma mark - Button methods
560
Alexandre Lision2db8f472015-07-22 15:05:46 -0400561- (IBAction)addToContact:(NSButton*) sender {
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400562 auto contactmethod = CallModel::instance().getCall(CallModel::instance().selectionModel()->currentIndex())->peerContactMethod();
Alexandre Lision2db8f472015-07-22 15:05:46 -0400563
564 if (self.addToContactPopover != nullptr) {
565 [self.addToContactPopover performClose:self];
566 self.addToContactPopover = NULL;
567 } else if (!contactmethod->contact() || contactmethod->contact()->isPlaceHolder()) {
568 auto* editorVC = [[PersonLinkerVC alloc] initWithNibName:@"PersonLinker" bundle:nil];
569 [editorVC setMethodToLink:contactmethod];
570 [editorVC setContactLinkedDelegate:self];
571 self.addToContactPopover = [[NSPopover alloc] init];
572 [self.addToContactPopover setContentSize:editorVC.view.frame.size];
573 [self.addToContactPopover setContentViewController:editorVC];
574 [self.addToContactPopover setAnimates:YES];
575 [self.addToContactPopover setBehavior:NSPopoverBehaviorTransient];
576 [self.addToContactPopover setDelegate:self];
577
578 [self.addToContactPopover showRelativeToRect:sender.bounds ofView:sender preferredEdge:NSMaxXEdge];
579 }
Alexandre Lision883719f2015-10-22 17:37:45 -0400580
581 [videoView setCallDelegate:nil];
Alexandre Lision2db8f472015-07-22 15:05:46 -0400582}
583
Alexandre Lisionc5148052015-03-04 15:10:35 -0500584- (IBAction)hangUp:(id)sender {
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400585 CallModel::instance().getCall(CallModel::instance().selectionModel()->currentIndex()) << Call::Action::REFUSE;
Alexandre Lisionc5148052015-03-04 15:10:35 -0500586}
587
588- (IBAction)accept:(id)sender {
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400589 CallModel::instance().getCall(CallModel::instance().selectionModel()->currentIndex()) << Call::Action::ACCEPT;
Alexandre Lisionc5148052015-03-04 15:10:35 -0500590}
591
592- (IBAction)toggleRecording:(id)sender {
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400593 CallModel::instance().getCall(CallModel::instance().selectionModel()->currentIndex()) << Call::Action::RECORD_AUDIO;
Alexandre Lisionc5148052015-03-04 15:10:35 -0500594}
595
596- (IBAction)toggleHold:(id)sender {
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400597 CallModel::instance().getCall(CallModel::instance().selectionModel()->currentIndex()) << Call::Action::HOLD;
Alexandre Lisionc5148052015-03-04 15:10:35 -0500598}
599
Alexandre Lision58cab672015-06-09 15:25:40 -0400600-(IBAction)toggleChat:(id)sender;
601{
602 BOOL rightViewCollapsed = [[self splitView] isSubviewCollapsed:[[[self splitView] subviews] objectAtIndex: 1]];
603 if (rightViewCollapsed) {
604 [self uncollapseRightView];
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400605 CallModel::instance().getCall(CallModel::instance().selectionModel()->currentIndex())->addOutgoingMedia<Media::Text>();
Alexandre Lision58cab672015-06-09 15:25:40 -0400606 } else {
607 [self collapseRightView];
608 }
609 [chatButton setState:rightViewCollapsed];
610}
611
Alexandre Lisiond18fa272015-06-15 11:18:03 -0400612- (IBAction)muteAudio:(id)sender
613{
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400614 UserActionModel* uam = CallModel::instance().userActionModel();
Alexandre Lisiond18fa272015-06-15 11:18:03 -0400615 uam << UserActionModel::Action::MUTE_AUDIO;
616}
617
618- (IBAction)muteVideo:(id)sender
619{
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400620 UserActionModel* uam = CallModel::instance().userActionModel();
Alexandre Lisiond18fa272015-06-15 11:18:03 -0400621 uam << UserActionModel::Action::MUTE_VIDEO;
622}
Alexandre Lision883719f2015-10-22 17:37:45 -0400623
624- (IBAction)toggleTransferView:(id)sender {
Alexandre Lision89edc6a2015-11-09 11:30:47 -0500625 if (_brokerPopoverVC != nullptr) {
626 [_brokerPopoverVC performClose:self];
627 _brokerPopoverVC = NULL;
Alexandre Lision883719f2015-10-22 17:37:45 -0400628 [self.transferButton setState:NSOffState];
629 } else {
Alexandre Lision89edc6a2015-11-09 11:30:47 -0500630 auto* brokerVC = [[BrokerVC alloc] initWithMode:BrokerMode::TRANSFER];
631 _brokerPopoverVC = [[NSPopover alloc] init];
632 [_brokerPopoverVC setContentSize:brokerVC.view.frame.size];
633 [_brokerPopoverVC setContentViewController:brokerVC];
634 [_brokerPopoverVC setAnimates:YES];
635 [_brokerPopoverVC setBehavior:NSPopoverBehaviorTransient];
636 [_brokerPopoverVC setDelegate:self];
637 [_brokerPopoverVC showRelativeToRect:[sender bounds] ofView:sender preferredEdge:NSMinYEdge];
Alexandre Lision883719f2015-10-22 17:37:45 -0400638 [videoView setCallDelegate:nil];
639 }
Alexandre Lisionf23ec5a2015-07-16 11:24:06 -0400640}
Alexandre Lisiond18fa272015-06-15 11:18:03 -0400641
Alexandre Lision89edc6a2015-11-09 11:30:47 -0500642- (IBAction)toggleAddParticipantView:(id)sender {
643 if (_brokerPopoverVC != nullptr) {
644 [_brokerPopoverVC performClose:self];
645 _brokerPopoverVC = NULL;
646 [self.addParticipantButton setState:NSOffState];
647 } else {
648 auto* brokerVC = [[BrokerVC alloc] initWithMode:BrokerMode::CONFERENCE];
649 _brokerPopoverVC = [[NSPopover alloc] init];
650 [_brokerPopoverVC setContentSize:brokerVC.view.frame.size];
651 [_brokerPopoverVC setContentViewController:brokerVC];
652 [_brokerPopoverVC setAnimates:YES];
653 [_brokerPopoverVC setBehavior:NSPopoverBehaviorTransient];
654 [_brokerPopoverVC setDelegate:self];
655 [_brokerPopoverVC showRelativeToRect:[sender bounds] ofView:sender preferredEdge:NSMinYEdge];
656 [videoView setCallDelegate:nil];
657 }
658}
659
660/**
661 * Merge current call with its parent call
662 */
663- (IBAction)mergeCalls:(id)sender
664{
665 auto current = CallModel::instance().selectedCall();
666 current->joinToParent();
667}
668
Alexandre Lision2db8f472015-07-22 15:05:46 -0400669#pragma mark - NSPopOverDelegate
670
Alexandre Lision266fca02015-09-28 14:47:05 -0400671- (void)popoverWillClose:(NSNotification *)notification
672{
Alexandre Lision89edc6a2015-11-09 11:30:47 -0500673 if (_brokerPopoverVC != nullptr) {
674 [_brokerPopoverVC performClose:self];
675 _brokerPopoverVC = NULL;
Alexandre Lision883719f2015-10-22 17:37:45 -0400676 }
Alexandre Lision266fca02015-09-28 14:47:05 -0400677
Alexandre Lision2db8f472015-07-22 15:05:46 -0400678 if (self.addToContactPopover != nullptr) {
679 [self.addToContactPopover performClose:self];
680 self.addToContactPopover = NULL;
681 }
Alexandre Lision883719f2015-10-22 17:37:45 -0400682
Alexandre Lision883719f2015-10-22 17:37:45 -0400683 [self.addContactButton setState:NSOffState];
684 [self.transferButton setState:NSOffState];
Alexandre Lision89edc6a2015-11-09 11:30:47 -0500685 [self.addParticipantButton setState:NSOffState];
Alexandre Lision883719f2015-10-22 17:37:45 -0400686}
687
688- (void)popoverDidClose:(NSNotification *)notification
689{
690 [videoView setCallDelegate:self];
Alexandre Lision2db8f472015-07-22 15:05:46 -0400691}
692
693#pragma mark - ContactLinkedDelegate
694
695- (void)contactLinked
696{
697 if (self.addToContactPopover != nullptr) {
698 [self.addToContactPopover performClose:self];
699 self.addToContactPopover = NULL;
700 }
701}
702
Alexandre Lision58cab672015-06-09 15:25:40 -0400703#pragma mark - NSSplitViewDelegate
704
705/* 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.
706 */
707- (BOOL)splitView:(NSSplitView *)splitView shouldCollapseSubview:(NSView *)subview forDoubleClickOnDividerAtIndex:(NSInteger)dividerIndex;
708{
709 NSView* rightView = [[splitView subviews] objectAtIndex:1];
710 return ([subview isEqual:rightView]);
711}
712
713
714- (BOOL)splitView:(NSSplitView *)splitView canCollapseSubview:(NSView *)subview;
715{
716 NSView* rightView = [[splitView subviews] objectAtIndex:1];
717 return ([subview isEqual:rightView]);
718}
719
720
Alexandre Lisiona1eee3c2015-08-10 13:44:51 -0400721# pragma mark - CallnDelegate
Alexandre Lision58cab672015-06-09 15:25:40 -0400722
723- (void) callShouldToggleFullScreen
724{
725 if(self.splitView.isInFullScreenMode)
726 [self.splitView exitFullScreenModeWithOptions:nil];
727 else {
728 NSApplicationPresentationOptions options = NSApplicationPresentationDefault +NSApplicationPresentationAutoHideDock +
729 NSApplicationPresentationAutoHideMenuBar + NSApplicationPresentationAutoHideToolbar;
730 NSDictionary *opts = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithUnsignedInt:options],
731 NSFullScreenModeApplicationPresentationOptions, nil];
732
733 [self.splitView enterFullScreenMode:[NSScreen mainScreen] withOptions:opts];
734 }
735}
736
Alexandre Lisiona1eee3c2015-08-10 13:44:51 -0400737-(void) mouseIsMoving:(BOOL) move
738{
739 [[controlsPanel animator] setAlphaValue:move]; // fade out
740 [[headerContainer animator] setAlphaValue:move];
741}
742
Alexandre Lisionc5148052015-03-04 15:10:35 -0500743@end