blob: bd44a7f25f64fc9295e2aff8295ee48915b9de6e [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
Alexandre Lision210fe212016-01-27 11:15:13 -050023///Qt
24#import <QMimeData>
25#import <QtMacExtras/qmacfunctions.h>
26#import <QtCore/qabstractitemmodel.h>
27#import <QItemSelectionModel>
28#import <QItemSelection>
29#import <QPixmap>
30
31///LRC
Alexandre Lisionc5148052015-03-04 15:10:35 -050032#import <video/previewmanager.h>
33#import <video/renderer.h>
Olivier Soldano994971f2017-12-05 16:30:12 -050034#import <api/newcallmodel.h>
35#import <api/call.h>
36#import <api/conversationmodel.h>
Alexandre Lision210fe212016-01-27 11:15:13 -050037#import <globalinstances.h>
Alexandre Lisionc5148052015-03-04 15:10:35 -050038
Alexandre Lision62005312016-01-28 15:55:16 -050039#import "AppDelegate.h"
Alexandre Lisionf47a2562015-06-15 15:48:29 -040040#import "views/ITProgressIndicator.h"
Alexandre Lision74dd47f2015-04-14 13:47:42 -040041#import "views/CallView.h"
Alexandre Lision210fe212016-01-27 11:15:13 -050042#import "delegates/ImageManipulationDelegate.h"
Alexandre Lision2db8f472015-07-22 15:05:46 -040043#import "PersonLinkerVC.h"
Alexandre Lision16d9c0a2015-08-10 12:05:15 -040044#import "ChatVC.h"
Alexandre Lision883719f2015-10-22 17:37:45 -040045#import "BrokerVC.h"
Alexandre Lision4baba4c2016-02-11 13:00:57 -050046#import "views/IconButton.h"
Anthony Léonard14e7bf32017-06-08 08:13:16 -040047#import "views/CallLayer.h"
Anthony Léonard6f819752018-01-05 09:53:40 -050048#import "utils.h"
Alexandre Lision74dd47f2015-04-14 13:47:42 -040049
Alexandre Lisionc5148052015-03-04 15:10:35 -050050@interface RendererConnectionsHolder : NSObject
51
52@property QMetaObject::Connection frameUpdated;
53@property QMetaObject::Connection started;
54@property QMetaObject::Connection stopped;
55
56@end
57
58@implementation RendererConnectionsHolder
59
60@end
61
Olivier Soldano994971f2017-12-05 16:30:12 -050062@interface CurrentCallVC () <NSPopoverDelegate, ContactLinkedDelegate> {
63 std::string convUid_;
64 std::string callUid_;
65 const lrc::api::account::Info *accountInfo_;
Anthony Léonard66a050c2018-01-03 16:32:34 -050066 NSTimer* refreshDurationTimer;
Olivier Soldano994971f2017-12-05 16:30:12 -050067}
Alexandre Lisionc5148052015-03-04 15:10:35 -050068
Alexandre Lision210fe212016-01-27 11:15:13 -050069// Main container
70@property (unsafe_unretained) IBOutlet NSSplitView* splitView;
71
Alexandre Lision89edc6a2015-11-09 11:30:47 -050072// Header info
73@property (unsafe_unretained) IBOutlet NSView* headerContainer;
Alexandre Lision883719f2015-10-22 17:37:45 -040074@property (unsafe_unretained) IBOutlet NSTextField* personLabel;
75@property (unsafe_unretained) IBOutlet NSTextField* stateLabel;
Alexandre Lision89edc6a2015-11-09 11:30:47 -050076@property (unsafe_unretained) IBOutlet NSTextField* timeSpentLabel;
Alexandre Lision210fe212016-01-27 11:15:13 -050077@property (unsafe_unretained) IBOutlet NSImageView* personPhoto;
Alexandre Lision89edc6a2015-11-09 11:30:47 -050078
79// Call Controls
80@property (unsafe_unretained) IBOutlet NSView* controlsPanel;
Alexandre Lision62005312016-01-28 15:55:16 -050081
Alexandre Lision4baba4c2016-02-11 13:00:57 -050082@property (unsafe_unretained) IBOutlet IconButton* holdOnOffButton;
83@property (unsafe_unretained) IBOutlet IconButton* hangUpButton;
84@property (unsafe_unretained) IBOutlet IconButton* recordOnOffButton;
85@property (unsafe_unretained) IBOutlet IconButton* pickUpButton;
86@property (unsafe_unretained) IBOutlet IconButton* muteAudioButton;
87@property (unsafe_unretained) IBOutlet IconButton* muteVideoButton;
88@property (unsafe_unretained) IBOutlet IconButton* addContactButton;
89@property (unsafe_unretained) IBOutlet IconButton* transferButton;
90@property (unsafe_unretained) IBOutlet IconButton* addParticipantButton;
91@property (unsafe_unretained) IBOutlet IconButton* chatButton;
Alexandre Lisiond18fa272015-06-15 11:18:03 -040092
Alexandre Lision62005312016-01-28 15:55:16 -050093@property (unsafe_unretained) IBOutlet NSView* advancedPanel;
94@property (unsafe_unretained) IBOutlet IconButton* advancedButton;
95
Alexandre Lisiond18fa272015-06-15 11:18:03 -040096
Alexandre Lision89edc6a2015-11-09 11:30:47 -050097// Join call panel
98@property (unsafe_unretained) IBOutlet NSView* joinPanel;
99@property (unsafe_unretained) IBOutlet NSButton* mergeCallsButton;
100
Alexandre Lision2db8f472015-07-22 15:05:46 -0400101@property (strong) NSPopover* addToContactPopover;
Alexandre Lision89edc6a2015-11-09 11:30:47 -0500102@property (strong) NSPopover* brokerPopoverVC;
Alexandre Lision883719f2015-10-22 17:37:45 -0400103@property (strong) IBOutlet ChatVC* chatVC;
Alexandre Lisionf23ec5a2015-07-16 11:24:06 -0400104
Alexandre Lision210fe212016-01-27 11:15:13 -0500105// Ringing call panel
106@property (unsafe_unretained) IBOutlet NSView* ringingPanel;
107@property (unsafe_unretained) IBOutlet NSImageView* incomingPersonPhoto;
108@property (unsafe_unretained) IBOutlet NSTextField* incomingDisplayName;
109
110// Outgoing call panel
111@property (unsafe_unretained) IBOutlet NSView* outgoingPanel;
112@property (unsafe_unretained) IBOutlet ITProgressIndicator *loadingIndicator;
Alexandre Lisionc5148052015-03-04 15:10:35 -0500113
114// Video
Alexandre Lision58cab672015-06-09 15:25:40 -0400115@property (unsafe_unretained) IBOutlet CallView *videoView;
Alexandre Lision58cab672015-06-09 15:25:40 -0400116@property (unsafe_unretained) IBOutlet NSView *previewView;
Alexandre Lisionc5148052015-03-04 15:10:35 -0500117
118@property RendererConnectionsHolder* previewHolder;
119@property RendererConnectionsHolder* videoHolder;
Alexandre Lisionef6333a2015-03-24 12:30:31 -0400120@property QMetaObject::Connection videoStarted;
Alexandre Lision4d9905e2016-01-22 12:01:36 -0500121@property QMetaObject::Connection selectedCallChanged;
Alexandre Lisionb65c0272015-07-22 15:51:29 -0400122@property QMetaObject::Connection messageConnection;
123@property QMetaObject::Connection mediaAddedConnection;
Alexandre Lisionc5148052015-03-04 15:10:35 -0500124
125@end
126
127@implementation CurrentCallVC
Olivier Soldano994971f2017-12-05 16:30:12 -0500128@synthesize personLabel, personPhoto, stateLabel, holdOnOffButton, hangUpButton,
Alexandre Lision89edc6a2015-11-09 11:30:47 -0500129 recordOnOffButton, pickUpButton, chatButton, transferButton, addParticipantButton, timeSpentLabel,
Olivier Soldano994971f2017-12-05 16:30:12 -0500130 muteVideoButton, muteAudioButton, controlsPanel, advancedPanel, advancedButton, headerContainer, videoView,
131 incomingDisplayName, incomingPersonPhoto, previewView, splitView, loadingIndicator, ringingPanel, joinPanel,
132 outgoingPanel;
Alexandre Lisionc5148052015-03-04 15:10:35 -0500133
134@synthesize previewHolder;
135@synthesize videoHolder;
136
Olivier Soldano994971f2017-12-05 16:30:12 -0500137-(void) setCurrentCall:(const std::string&)callUid
138 conversation:(const std::string&)convUid
139 account:(const lrc::api::account::Info*)account;
Alexandre Lisionc5148052015-03-04 15:10:35 -0500140{
Olivier Soldano994971f2017-12-05 16:30:12 -0500141 if(account == nil)
142 return;
Alexandre Lisionef6333a2015-03-24 12:30:31 -0400143
Olivier Soldano994971f2017-12-05 16:30:12 -0500144 auto* callModel = account->callModel.get();
Alexandre Lisionc5148052015-03-04 15:10:35 -0500145
Olivier Soldano994971f2017-12-05 16:30:12 -0500146 if (not callModel->hasCall(callUid)){
147 callUid_.clear();
Alexandre Lision2db8f472015-07-22 15:05:46 -0400148 return;
149 }
Olivier Soldano994971f2017-12-05 16:30:12 -0500150 callUid_ = callUid;
151 convUid_ = convUid;
152 accountInfo_ = account;
Anthony Léonard4b0935f2017-12-26 12:05:22 -0500153 [self.chatVC setConversationUid:convUid model:account->conversationModel.get()];
Alexandre Lisionc5148052015-03-04 15:10:35 -0500154}
155
156- (void)awakeFromNib
157{
158 NSLog(@"INIT CurrentCall VC");
Alexandre Lisionef6333a2015-03-24 12:30:31 -0400159 [self.view setWantsLayer:YES];
Alexandre Lisionc5148052015-03-04 15:10:35 -0500160
Alexandre Lisionc5148052015-03-04 15:10:35 -0500161 [previewView setWantsLayer:YES];
Alexandre Lision22d615b2016-01-25 12:54:34 -0500162 [previewView.layer setBackgroundColor:[NSColor blackColor].CGColor];
163 [previewView.layer setContentsGravity:kCAGravityResizeAspectFill];
164 [previewView.layer setFrame:previewView.frame];
Alexandre Lisionc5148052015-03-04 15:10:35 -0500165
166 [controlsPanel setWantsLayer:YES];
Alexandre Lisionc5148052015-03-04 15:10:35 -0500167 [controlsPanel.layer setBackgroundColor:[NSColor clearColor].CGColor];
168 [controlsPanel.layer setFrame:controlsPanel.frame];
169
Alexandre Lisionef6333a2015-03-24 12:30:31 -0400170 previewHolder = [[RendererConnectionsHolder alloc] init];
171 videoHolder = [[RendererConnectionsHolder alloc] init];
172
Alexandre Lisionf47a2562015-06-15 15:48:29 -0400173 [loadingIndicator setColor:[NSColor whiteColor]];
174 [loadingIndicator setNumberOfLines:100];
175 [loadingIndicator setWidthOfLine:2];
176 [loadingIndicator setLengthOfLine:2];
177 [loadingIndicator setInnerMargin:30];
178
Alexandre Lisiona1eee3c2015-08-10 13:44:51 -0400179 [self.videoView setCallDelegate:self];
Alexandre Lisionc5148052015-03-04 15:10:35 -0500180}
181
Anthony Léonard66a050c2018-01-03 16:32:34 -0500182-(void) updateDurationLabel
183{
184 if (accountInfo_ != nil) {
185 auto* callModel = accountInfo_->callModel.get();
186 if (callModel->hasCall(callUid_)) {
187 auto& callStatus = callModel->getCall(callUid_).status;
188 if (callStatus != lrc::api::call::Status::ENDED &&
189 callStatus != lrc::api::call::Status::TERMINATING &&
190 callStatus != lrc::api::call::Status::INVALID) {
191 [timeSpentLabel setStringValue:@(callModel->getFormattedCallDuration(callUid_).c_str())];
192 return;
193 }
194 }
195 }
196
197 // If call is not running anymore or accountInfo_ is not set for any reason
198 // we stop the refresh loop
199 [refreshDurationTimer invalidate];
200 refreshDurationTimer = nil;
201 [timeSpentLabel setHidden:YES];
202}
203
Olivier Soldano994971f2017-12-05 16:30:12 -0500204-(void) updateCall:(BOOL) firstRun
Alexandre Lisionc5148052015-03-04 15:10:35 -0500205{
Olivier Soldano994971f2017-12-05 16:30:12 -0500206 if (accountInfo_ == nil)
207 return;
Alexandre Lisionbb5dbcd2015-07-09 16:36:47 -0400208
Olivier Soldano994971f2017-12-05 16:30:12 -0500209 auto* callModel = accountInfo_->callModel.get();
210 if (not callModel->hasCall(callUid_)) {
211 return;
212 }
Alexandre Lision89edc6a2015-11-09 11:30:47 -0500213
Olivier Soldano994971f2017-12-05 16:30:12 -0500214 auto currentCall = callModel->getCall(callUid_);
215 NSLog(@"\n status %@ \n",@(lrc::api::call::to_string(currentCall.status).c_str()));
216 [personLabel setStringValue:@(currentCall.peer.c_str())];
Anthony Léonard66a050c2018-01-03 16:32:34 -0500217 [timeSpentLabel setStringValue:@(callModel->getFormattedCallDuration(callUid_).c_str())];
218 [timeSpentLabel setHidden:NO];
219 if (refreshDurationTimer == nil)
220 refreshDurationTimer = [NSTimer scheduledTimerWithTimeInterval:1.0
221 target:self
222 selector:@selector(updateDurationLabel)
223 userInfo:nil
224 repeats:YES];
Olivier Soldano994971f2017-12-05 16:30:12 -0500225 [stateLabel setStringValue:@(to_string(currentCall.status).c_str())];
Alexandre Lisionbb5dbcd2015-07-09 16:36:47 -0400226
Olivier Soldano994971f2017-12-05 16:30:12 -0500227 if (firstRun) {
228 //QVariant photo = GlobalInstances::pixmapManipulator().callPhoto(current, QSize(100,100));
229 //[personPhoto setImage:QtMac::toNSImage(qvariant_cast<QPixmap>(photo))];
230 }
Alexandre Lisionc5148052015-03-04 15:10:35 -0500231
Olivier Soldano994971f2017-12-05 16:30:12 -0500232 // Default values for this views
233 [loadingIndicator setHidden:YES];
234 [videoView setShouldAcceptInteractions:NO];
235 [ringingPanel setHidden:YES];
236 [outgoingPanel setHidden:YES];
237 [controlsPanel setHidden:NO];
238 [headerContainer setHidden:NO];
Anthony Léonard4b0935f2017-12-26 12:05:22 -0500239 [chatButton setHidden:NO];
Alexandre Lisionc5148052015-03-04 15:10:35 -0500240
Olivier Soldano994971f2017-12-05 16:30:12 -0500241 using Status = lrc::api::call::Status;
242 switch (currentCall.status) {
243 case Status::SEARCHING:
244 case Status::OUTGOING_REQUESTED:
245 case Status::CONNECTING:
246 [loadingIndicator setHidden:NO];
247 break;
248 case Status::INCOMING_RINGING:
249 [controlsPanel setHidden:YES];
250 [outgoingPanel setHidden:YES];
251 [self setupIncoming:callUid_];
252 break;
253 case Status::OUTGOING_RINGING:
254 [controlsPanel setHidden:YES];
255 [outgoingPanel setHidden:NO];
256 break;
257// case Status::CONFERENCE:
258// [self setupConference:currentCall];
259// break;
260 case Status::PAUSED:
261 case Status::PEER_PAUSED:
262 case Status::INACTIVE:
263 case Status::IN_PROGRESS:
264 case Status::CONNECTED:
265 case Status::AUTO_ANSWERING:
266 break;
267 case Status::ENDED:
268 case Status::TERMINATING:
269 case Status::INVALID:
270 [controlsPanel setHidden:YES];
271 [outgoingPanel setHidden:NO];
272 if(self.splitView.isInFullScreenMode)
273 [self.splitView exitFullScreenModeWithOptions:nil];
274 [self animateOut];
275 break;
276 }
Alexandre Lision4d9905e2016-01-22 12:01:36 -0500277
Alexandre Lision4d9905e2016-01-22 12:01:36 -0500278}
279
Olivier Soldano994971f2017-12-05 16:30:12 -0500280-(void) setupIncoming:(const std::string&) callId
Alexandre Lision4d9905e2016-01-22 12:01:36 -0500281{
Olivier Soldano994971f2017-12-05 16:30:12 -0500282 if (accountInfo_ == nil)
283 return;
284
285 auto* callModel = accountInfo_->callModel.get();
286 if (not callModel->hasCall(callUid_)) {
287 return;
288 }
289
290 auto call = callModel->getCall(callUid_);
291 auto* convModel = accountInfo_->conversationModel.get();
292
293 [ringingPanel setHidden:NO];
294 [controlsPanel setHidden:YES];
295 [headerContainer setHidden:YES];
Anthony Léonard6f819752018-01-05 09:53:40 -0500296 auto it = getConversationFromUid(convUid_, *convModel);
Olivier Soldano994971f2017-12-05 16:30:12 -0500297 if (it != convModel->allFilteredConversations().end()) {
298 auto& imgManip = reinterpret_cast<Interfaces::ImageManipulationDelegate&>(GlobalInstances::pixmapManipulator());
299 QVariant photo = imgManip.conversationPhoto(*it, *accountInfo_);
300 [incomingPersonPhoto setImage:QtMac::toNSImage(qvariant_cast<QPixmap>(photo))];
301 [incomingDisplayName setStringValue:@(call.peer.c_str())];
302 }
303}
304
305
306-(void) setupConference:(Call*) c
307{
308 [videoView setShouldAcceptInteractions:YES];
309 [self.chatButton setHidden:NO];
310 [joinPanel setHidden:YES];
311 [self.addParticipantButton setHidden:NO];
312 [self.transferButton setHidden:YES];
313}
314
315-(void)collapseRightView
316{
317 NSView *right = [[splitView subviews] objectAtIndex:1];
318 NSView *left = [[splitView subviews] objectAtIndex:0];
319 NSRect leftFrame = [left frame];
320 [right setHidden:YES];
321 [splitView display];
322}
323
324- (void) changeCallSelection:(std::string&)callUid
325{
326 if (accountInfo_ == nil)
327 return;
328
329 auto* callModel = accountInfo_->callModel.get();
330 if (not callModel->hasCall(callUid)) {
331 return;
332 }
333
Alexandre Lision4d9905e2016-01-22 12:01:36 -0500334 QObject::disconnect(self.selectedCallChanged);
Olivier Soldano994971f2017-12-05 16:30:12 -0500335 self.selectedCallChanged = QObject::connect(callModel,
336 &lrc::api::NewCallModel::callStatusChanged,
337 [self](const std::string callId) {
Alexandre Lision210fe212016-01-27 11:15:13 -0500338 [self updateCall:NO];
Alexandre Lision4d9905e2016-01-22 12:01:36 -0500339 });
Alexandre Lisionc5148052015-03-04 15:10:35 -0500340}
341
342-(void) connectVideoSignals
343{
Olivier Soldano994971f2017-12-05 16:30:12 -0500344 if (accountInfo_ == nil)
345 return;
346
347 auto* callModel = accountInfo_->callModel.get();
Alexandre Lision89edc6a2015-11-09 11:30:47 -0500348 QObject::disconnect(self.videoStarted);
Olivier Soldano994971f2017-12-05 16:30:12 -0500349 self.videoStarted = QObject::connect(callModel,
350 &lrc::api::NewCallModel::remotePreviewStarted,
351 [self](const std::string& callId, Video::Renderer* renderer) {
352 NSLog(@"Video started!");
353 [self connectVideoRenderer:renderer];
354 });
Alexandre Lisionc5148052015-03-04 15:10:35 -0500355
Olivier Soldano994971f2017-12-05 16:30:12 -0500356 if (callModel->hasCall(callUid_)) {
357 if (auto renderer = callModel->getRenderer(callUid_)) {
358 QObject::disconnect(self.videoStarted);
359 [self connectVideoRenderer: renderer];
360 }
Alexandre Lisionc5148052015-03-04 15:10:35 -0500361 }
Alexandre Lisionc5148052015-03-04 15:10:35 -0500362 [self connectPreviewRenderer];
363
364}
365
366-(void) connectPreviewRenderer
367{
Alexandre Lisionef6333a2015-03-24 12:30:31 -0400368 QObject::disconnect(previewHolder.frameUpdated);
369 QObject::disconnect(previewHolder.stopped);
370 QObject::disconnect(previewHolder.started);
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400371 previewHolder.started = QObject::connect(&Video::PreviewManager::instance(),
Alexandre Lisionc5148052015-03-04 15:10:35 -0500372 &Video::PreviewManager::previewStarted,
373 [=](Video::Renderer* renderer) {
Alexandre Lisionc5148052015-03-04 15:10:35 -0500374 QObject::disconnect(previewHolder.frameUpdated);
375 previewHolder.frameUpdated = QObject::connect(renderer,
376 &Video::Renderer::frameUpdated,
377 [=]() {
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400378 [self renderer:Video::PreviewManager::instance().previewRenderer()
Anthony Léonard14e7bf32017-06-08 08:13:16 -0400379 renderFrameForPreviewView:previewView];
Alexandre Lisionc5148052015-03-04 15:10:35 -0500380 });
381 });
382
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400383 previewHolder.stopped = QObject::connect(&Video::PreviewManager::instance(),
Alexandre Lisionc5148052015-03-04 15:10:35 -0500384 &Video::PreviewManager::previewStopped,
385 [=](Video::Renderer* renderer) {
Alexandre Lisionc5148052015-03-04 15:10:35 -0500386 QObject::disconnect(previewHolder.frameUpdated);
387 [previewView.layer setContents:nil];
388 });
389
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400390 previewHolder.frameUpdated = QObject::connect(Video::PreviewManager::instance().previewRenderer(),
Alexandre Lisionc5148052015-03-04 15:10:35 -0500391 &Video::Renderer::frameUpdated,
392 [=]() {
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400393 [self renderer:Video::PreviewManager::instance().previewRenderer()
Anthony Léonard14e7bf32017-06-08 08:13:16 -0400394 renderFrameForPreviewView:previewView];
Alexandre Lisionc5148052015-03-04 15:10:35 -0500395 });
396}
397
398-(void) connectVideoRenderer: (Video::Renderer*)renderer
399{
Alexandre Lisionef6333a2015-03-24 12:30:31 -0400400 QObject::disconnect(videoHolder.frameUpdated);
401 QObject::disconnect(videoHolder.started);
402 QObject::disconnect(videoHolder.stopped);
Olivier Soldano994971f2017-12-05 16:30:12 -0500403
404 if(renderer == nil)
405 return;
406
Alexandre Lisionc5148052015-03-04 15:10:35 -0500407 videoHolder.frameUpdated = QObject::connect(renderer,
408 &Video::Renderer::frameUpdated,
409 [=]() {
Anthony Léonard14e7bf32017-06-08 08:13:16 -0400410 [self renderer:renderer renderFrameForDistantView:videoView];
Alexandre Lisionc5148052015-03-04 15:10:35 -0500411 });
412
413 videoHolder.started = QObject::connect(renderer,
414 &Video::Renderer::started,
415 [=]() {
Alexandre Lisionc5148052015-03-04 15:10:35 -0500416 QObject::disconnect(videoHolder.frameUpdated);
417 videoHolder.frameUpdated = QObject::connect(renderer,
418 &Video::Renderer::frameUpdated,
419 [=]() {
Anthony Léonard14e7bf32017-06-08 08:13:16 -0400420 [self renderer:renderer renderFrameForDistantView:videoView];
Alexandre Lisionc5148052015-03-04 15:10:35 -0500421 });
422 });
423
424 videoHolder.stopped = QObject::connect(renderer,
425 &Video::Renderer::stopped,
426 [=]() {
Alexandre Lisionc5148052015-03-04 15:10:35 -0500427 QObject::disconnect(videoHolder.frameUpdated);
Anthony Léonard14e7bf32017-06-08 08:13:16 -0400428 [(CallLayer*)videoView.layer setVideoRunning:NO];
Alexandre Lisionc5148052015-03-04 15:10:35 -0500429 });
430}
431
Anthony Léonard14e7bf32017-06-08 08:13:16 -0400432-(void) renderer: (Video::Renderer*)renderer renderFrameForPreviewView:(NSView*) view
Alexandre Lisionc5148052015-03-04 15:10:35 -0500433{
Alexandre Lisionc5148052015-03-04 15:10:35 -0500434 QSize res = renderer->size();
435
Alexandre Lision6731e132015-10-14 14:29:06 -0400436 auto frame_ptr = renderer->currentFrame();
437 auto frame_data = frame_ptr.ptr;
438 if (!frame_data)
439 return;
440
Alexandre Lisionc5148052015-03-04 15:10:35 -0500441 CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
Alexandre Lision6731e132015-10-14 14:29:06 -0400442 CGContextRef newContext = CGBitmapContextCreate(frame_data,
Alexandre Lisionc5148052015-03-04 15:10:35 -0500443 res.width(),
444 res.height(),
445 8,
446 4*res.width(),
447 colorSpace,
448 kCGImageAlphaPremultipliedLast);
449
450
451 CGImageRef newImage = CGBitmapContextCreateImage(newContext);
452
453 /*We release some components*/
454 CGContextRelease(newContext);
455 CGColorSpaceRelease(colorSpace);
456
457 [CATransaction begin];
458 view.layer.contents = (__bridge id)newImage;
459 [CATransaction commit];
460
461 CFRelease(newImage);
462}
463
Anthony Léonard14e7bf32017-06-08 08:13:16 -0400464-(void) renderer: (Video::Renderer*)renderer renderFrameForDistantView:(CallView*) view
465{
Anthony Léonard14e7bf32017-06-08 08:13:16 -0400466 auto frame_ptr = renderer->currentFrame();
467 if (!frame_ptr.ptr)
468 return;
469
470 CallLayer* callLayer = (CallLayer*) view.layer;
471
Anthony Léonarda9d7c8e2018-01-03 14:41:17 -0500472 [callLayer setCurrentFrame:std::move(frame_ptr)];
Anthony Léonard14e7bf32017-06-08 08:13:16 -0400473 [callLayer setVideoRunning:YES];
474}
475
Alexandre Lisionc5148052015-03-04 15:10:35 -0500476- (void) initFrame
477{
478 [self.view setFrame:self.view.superview.bounds];
479 [self.view setHidden:YES];
480 self.view.layer.position = self.view.frame.origin;
Alexandre Lisionbb5dbcd2015-07-09 16:36:47 -0400481 [self collapseRightView];
Alexandre Lisionc5148052015-03-04 15:10:35 -0500482}
483
484# pragma private IN/OUT animations
485
Olivier Soldano994971f2017-12-05 16:30:12 -0500486-(void)uncollapseRightView
487{
488 NSView *left = [[splitView subviews] objectAtIndex:0];
489 NSView *right = [[splitView subviews] objectAtIndex:1];
490 [right setHidden:NO];
491
492 CGFloat dividerThickness = [splitView dividerThickness];
493
494 // get the different frames
495 NSRect leftFrame = [left frame];
496 NSRect rightFrame = [right frame];
497
498 leftFrame.size.width = (leftFrame.size.width - rightFrame.size.width - dividerThickness);
499 rightFrame.origin.x = leftFrame.size.width + dividerThickness;
500 [left setFrameSize:leftFrame.size];
501 [right setFrame:rightFrame];
502 [splitView display];
503
504 [self.chatVC takeFocus];
505}
506
Alexandre Lisionc5148052015-03-04 15:10:35 -0500507-(void) animateIn
508{
Alexandre Lisionc5148052015-03-04 15:10:35 -0500509 CGRect frame = CGRectOffset(self.view.superview.bounds, -self.view.superview.bounds.size.width, 0);
510 [self.view setHidden:NO];
511
512 [CATransaction begin];
513 CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
514 [animation setFromValue:[NSValue valueWithPoint:frame.origin]];
515 [animation setToValue:[NSValue valueWithPoint:self.view.superview.bounds.origin]];
516 [animation setDuration:0.2f];
Alexandre Lision22d615b2016-01-25 12:54:34 -0500517 [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];
Alexandre Lisionc5148052015-03-04 15:10:35 -0500518 [CATransaction setCompletionBlock:^{
Olivier Soldano994971f2017-12-05 16:30:12 -0500519 if (accountInfo_ == nil)
520 return;
521
522 auto* callModel = accountInfo_->callModel.get();
523 auto* convModel = accountInfo_->conversationModel.get();
Alexandre Lisiona1eee3c2015-08-10 13:44:51 -0400524
525 // when call comes in we want to show the controls/header
526 [self mouseIsMoving:YES];
527
Alexandre Lisionc5148052015-03-04 15:10:35 -0500528 [self connectVideoSignals];
Alexandre Lisionb65c0272015-07-22 15:51:29 -0400529 /* check if text media is already present */
Olivier Soldano994971f2017-12-05 16:30:12 -0500530 if(not callModel->hasCall(callUid_))
Alexandre Lision4dfcafc2015-08-20 12:43:23 -0400531 return;
Alexandre Lision21666f32015-09-22 17:04:36 -0400532
Alexandre Lision22d615b2016-01-25 12:54:34 -0500533 [loadingIndicator setAnimates:YES];
Alexandre Lision210fe212016-01-27 11:15:13 -0500534 [self updateCall:YES];
Alexandre Lision4d9905e2016-01-22 12:01:36 -0500535
Olivier Soldano994971f2017-12-05 16:30:12 -0500536 /* monitor media for messaging text messaging */
537 QObject::disconnect(self.messageConnection);
538 self.messageConnection = QObject::connect(convModel,
539 &lrc::api::ConversationModel::interactionStatusUpdated,
540 [self] (std::string convUid,
541 uint64_t msgId,
542 lrc::api::interaction::Info msg) {
543 if (msg.type == lrc::api::interaction::Type::TEXT) {
544 if(not [[self splitView] isSubviewCollapsed:[[[self splitView] subviews] objectAtIndex: 1]]){
545 return;
546 }
547 [self uncollapseRightView];
548 }
549 });
Alexandre Lisionc5148052015-03-04 15:10:35 -0500550 }];
Alexandre Lisionc5148052015-03-04 15:10:35 -0500551
Olivier Soldano994971f2017-12-05 16:30:12 -0500552 [self.view.layer addAnimation:animation forKey:animation.keyPath];
Alexandre Lisionc5148052015-03-04 15:10:35 -0500553 [CATransaction commit];
554}
555
556-(void) cleanUp
557{
Alexandre Lisionef6333a2015-03-24 12:30:31 -0400558 QObject::disconnect(videoHolder.frameUpdated);
559 QObject::disconnect(videoHolder.started);
560 QObject::disconnect(videoHolder.stopped);
561 QObject::disconnect(previewHolder.frameUpdated);
562 QObject::disconnect(previewHolder.stopped);
563 QObject::disconnect(previewHolder.started);
Alexandre Lisionc5148052015-03-04 15:10:35 -0500564 [previewView.layer setContents:nil];
Alexandre Lision883719f2015-10-22 17:37:45 -0400565
Alexandre Lision89edc6a2015-11-09 11:30:47 -0500566 [_brokerPopoverVC performClose:self];
Alexandre Lision883719f2015-10-22 17:37:45 -0400567 [self.addToContactPopover performClose:self];
Alexandre Lision0e66aea2015-11-02 16:36:30 -0500568
Alexandre Lision89edc6a2015-11-09 11:30:47 -0500569 [self.chatButton setHidden:YES];
570 [self.addParticipantButton setHidden:YES];
571 [self.transferButton setHidden:YES];
572
Alexandre Lision4baba4c2016-02-11 13:00:57 -0500573 [self.chatButton setPressed:NO];
Alexandre Lision89edc6a2015-11-09 11:30:47 -0500574 [self.mergeCallsButton setState:NSOffState];
Alexandre Lision883719f2015-10-22 17:37:45 -0400575 [self collapseRightView];
Alexandre Lision57227772016-01-15 17:19:36 -0500576
577 [personLabel setStringValue:@""];
578 [timeSpentLabel setStringValue:@""];
579 [stateLabel setStringValue:@""];
580 [self.addContactButton setHidden:YES];
Alexandre Lision62005312016-01-28 15:55:16 -0500581
582 [advancedButton setPressed:NO];
583 [advancedPanel setHidden:YES];
Alexandre Lisionc5148052015-03-04 15:10:35 -0500584}
585
586-(void) animateOut
587{
Alexandre Lisionc5148052015-03-04 15:10:35 -0500588 if(self.view.frame.origin.x < 0) {
Alexandre Lisionc5148052015-03-04 15:10:35 -0500589 return;
590 }
591
592 CGRect frame = CGRectOffset(self.view.frame, -self.view.frame.size.width, 0);
593 [CATransaction begin];
594 CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
595 [animation setFromValue:[NSValue valueWithPoint:self.view.frame.origin]];
596 [animation setToValue:[NSValue valueWithPoint:frame.origin]];
597 [animation setDuration:0.2f];
Alexandre Lision22d615b2016-01-25 12:54:34 -0500598 [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
Alexandre Lisionc5148052015-03-04 15:10:35 -0500599
600 [CATransaction setCompletionBlock:^{
601 [self.view setHidden:YES];
Alexandre Lisionef6333a2015-03-24 12:30:31 -0400602 // first make sure everything is disconnected
Alexandre Lisionc5148052015-03-04 15:10:35 -0500603 [self cleanUp];
Olivier Soldano994971f2017-12-05 16:30:12 -0500604// if (currentCall_) {
605// [self animateIn];
606// }
Alexandre Lisionc5148052015-03-04 15:10:35 -0500607 }];
608 [self.view.layer addAnimation:animation forKey:animation.keyPath];
Alexandre Lisiona1c6d752015-06-23 12:27:38 -0400609
610 [self.view.layer setPosition:frame.origin];
Alexandre Lisionc5148052015-03-04 15:10:35 -0500611 [CATransaction commit];
612}
613
614/**
615 * Debug purpose
616 */
617-(void) dumpFrame:(CGRect) frame WithName:(NSString*) name
618{
619 NSLog(@"frame %@ : %f %f %f %f \n\n",name ,frame.origin.x, frame.origin.y, frame.size.width, frame.size.height);
620}
621
Alexandre Lision58cab672015-06-09 15:25:40 -0400622#pragma mark - Button methods
623
Alexandre Lisionc5148052015-03-04 15:10:35 -0500624- (IBAction)hangUp:(id)sender {
Olivier Soldano994971f2017-12-05 16:30:12 -0500625 if (accountInfo_ == nil)
626 return;
627
628 auto* callModel = accountInfo_->callModel.get();
629
630 callModel->hangUp(callUid_);
Alexandre Lisionc5148052015-03-04 15:10:35 -0500631}
632
633- (IBAction)accept:(id)sender {
Olivier Soldano994971f2017-12-05 16:30:12 -0500634 if (accountInfo_ == nil)
635 return;
636
637 auto* callModel = accountInfo_->callModel.get();
638
639 callModel->accept(callUid_);
Alexandre Lisionc5148052015-03-04 15:10:35 -0500640}
641
642- (IBAction)toggleRecording:(id)sender {
Olivier Soldano994971f2017-12-05 16:30:12 -0500643 if (accountInfo_ == nil)
644 return;
645
646 auto* callModel = accountInfo_->callModel.get();
647
648 callModel->toggleAudioRecord(callUid_);
Alexandre Lisionc5148052015-03-04 15:10:35 -0500649}
650
651- (IBAction)toggleHold:(id)sender {
Olivier Soldano994971f2017-12-05 16:30:12 -0500652 if (accountInfo_ == nil)
653 return;
654
655 auto* callModel = accountInfo_->callModel.get();
656
657 callModel->togglePause(callUid_);
Alexandre Lisionc5148052015-03-04 15:10:35 -0500658}
659
Alexandre Lision62005312016-01-28 15:55:16 -0500660- (IBAction)toggleAdvancedControls:(id)sender {
661 [advancedButton setPressed:!advancedButton.isPressed];
662 [advancedPanel setHidden:![advancedButton isPressed]];
663}
664
665- (IBAction)showDialpad:(id)sender {
666 AppDelegate* appDelegate = (AppDelegate *)[[NSApplication sharedApplication] delegate];
667 [appDelegate showDialpad];
668}
669
Alexandre Lision58cab672015-06-09 15:25:40 -0400670-(IBAction)toggleChat:(id)sender;
671{
672 BOOL rightViewCollapsed = [[self splitView] isSubviewCollapsed:[[[self splitView] subviews] objectAtIndex: 1]];
673 if (rightViewCollapsed) {
674 [self uncollapseRightView];
Alexandre Lision58cab672015-06-09 15:25:40 -0400675 } else {
676 [self collapseRightView];
677 }
Alexandre Lision62005312016-01-28 15:55:16 -0500678 [chatButton setPressed:rightViewCollapsed];
Alexandre Lision58cab672015-06-09 15:25:40 -0400679}
680
Alexandre Lisiond18fa272015-06-15 11:18:03 -0400681- (IBAction)muteAudio:(id)sender
682{
Olivier Soldano994971f2017-12-05 16:30:12 -0500683 if (accountInfo_ == nil)
684 return;
685
686 auto* callModel = accountInfo_->callModel.get();
687
688 callModel->toggleMedia(callUid_, lrc::api::NewCallModel::Media::AUDIO);
Alexandre Lisiond18fa272015-06-15 11:18:03 -0400689}
690
691- (IBAction)muteVideo:(id)sender
692{
Olivier Soldano994971f2017-12-05 16:30:12 -0500693 if (accountInfo_ == nil)
694 return;
695
696 auto* callModel = accountInfo_->callModel.get();
697
698 callModel->toggleMedia(callUid_, lrc::api::NewCallModel::Media::VIDEO);
Alexandre Lisiond18fa272015-06-15 11:18:03 -0400699}
Alexandre Lision883719f2015-10-22 17:37:45 -0400700
701- (IBAction)toggleTransferView:(id)sender {
Alexandre Lision89edc6a2015-11-09 11:30:47 -0500702 if (_brokerPopoverVC != nullptr) {
703 [_brokerPopoverVC performClose:self];
704 _brokerPopoverVC = NULL;
Alexandre Lision62005312016-01-28 15:55:16 -0500705 [self.transferButton setPressed:NO];
Alexandre Lision883719f2015-10-22 17:37:45 -0400706 } else {
Alexandre Lision89edc6a2015-11-09 11:30:47 -0500707 auto* brokerVC = [[BrokerVC alloc] initWithMode:BrokerMode::TRANSFER];
708 _brokerPopoverVC = [[NSPopover alloc] init];
709 [_brokerPopoverVC setContentSize:brokerVC.view.frame.size];
710 [_brokerPopoverVC setContentViewController:brokerVC];
711 [_brokerPopoverVC setAnimates:YES];
712 [_brokerPopoverVC setBehavior:NSPopoverBehaviorTransient];
713 [_brokerPopoverVC setDelegate:self];
714 [_brokerPopoverVC showRelativeToRect:[sender bounds] ofView:sender preferredEdge:NSMinYEdge];
Alexandre Lision883719f2015-10-22 17:37:45 -0400715 [videoView setCallDelegate:nil];
716 }
Alexandre Lisionf23ec5a2015-07-16 11:24:06 -0400717}
Alexandre Lisiond18fa272015-06-15 11:18:03 -0400718
Alexandre Lision89edc6a2015-11-09 11:30:47 -0500719- (IBAction)toggleAddParticipantView:(id)sender {
720 if (_brokerPopoverVC != nullptr) {
721 [_brokerPopoverVC performClose:self];
722 _brokerPopoverVC = NULL;
Alexandre Lision62005312016-01-28 15:55:16 -0500723 [self.addParticipantButton setPressed:NO];
Alexandre Lision89edc6a2015-11-09 11:30:47 -0500724 } else {
725 auto* brokerVC = [[BrokerVC alloc] initWithMode:BrokerMode::CONFERENCE];
726 _brokerPopoverVC = [[NSPopover alloc] init];
727 [_brokerPopoverVC setContentSize:brokerVC.view.frame.size];
728 [_brokerPopoverVC setContentViewController:brokerVC];
729 [_brokerPopoverVC setAnimates:YES];
730 [_brokerPopoverVC setBehavior:NSPopoverBehaviorTransient];
731 [_brokerPopoverVC setDelegate:self];
732 [_brokerPopoverVC showRelativeToRect:[sender bounds] ofView:sender preferredEdge:NSMinYEdge];
733 [videoView setCallDelegate:nil];
734 }
735}
736
737/**
738 * Merge current call with its parent call
739 */
740- (IBAction)mergeCalls:(id)sender
741{
Olivier Soldano994971f2017-12-05 16:30:12 -0500742// auto current = CallModel::instance().selectedCall();
743// current->joinToParent();
Alexandre Lision89edc6a2015-11-09 11:30:47 -0500744}
745
Alexandre Lision2db8f472015-07-22 15:05:46 -0400746#pragma mark - NSPopOverDelegate
747
Alexandre Lision266fca02015-09-28 14:47:05 -0400748- (void)popoverWillClose:(NSNotification *)notification
749{
Alexandre Lision89edc6a2015-11-09 11:30:47 -0500750 if (_brokerPopoverVC != nullptr) {
751 [_brokerPopoverVC performClose:self];
752 _brokerPopoverVC = NULL;
Alexandre Lision883719f2015-10-22 17:37:45 -0400753 }
Alexandre Lision266fca02015-09-28 14:47:05 -0400754
Alexandre Lision2db8f472015-07-22 15:05:46 -0400755 if (self.addToContactPopover != nullptr) {
756 [self.addToContactPopover performClose:self];
757 self.addToContactPopover = NULL;
758 }
Alexandre Lision883719f2015-10-22 17:37:45 -0400759
Alexandre Lision62005312016-01-28 15:55:16 -0500760 [self.addContactButton setPressed:NO];
761 [self.transferButton setPressed:NO];
Alexandre Lision89edc6a2015-11-09 11:30:47 -0500762 [self.addParticipantButton setState:NSOffState];
Alexandre Lision883719f2015-10-22 17:37:45 -0400763}
764
765- (void)popoverDidClose:(NSNotification *)notification
766{
767 [videoView setCallDelegate:self];
Alexandre Lision2db8f472015-07-22 15:05:46 -0400768}
769
770#pragma mark - ContactLinkedDelegate
771
772- (void)contactLinked
773{
774 if (self.addToContactPopover != nullptr) {
775 [self.addToContactPopover performClose:self];
776 self.addToContactPopover = NULL;
777 }
778}
779
Alexandre Lision58cab672015-06-09 15:25:40 -0400780#pragma mark - NSSplitViewDelegate
781
782/* 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.
783 */
784- (BOOL)splitView:(NSSplitView *)splitView shouldCollapseSubview:(NSView *)subview forDoubleClickOnDividerAtIndex:(NSInteger)dividerIndex;
785{
786 NSView* rightView = [[splitView subviews] objectAtIndex:1];
787 return ([subview isEqual:rightView]);
788}
789
790
791- (BOOL)splitView:(NSSplitView *)splitView canCollapseSubview:(NSView *)subview;
792{
793 NSView* rightView = [[splitView subviews] objectAtIndex:1];
794 return ([subview isEqual:rightView]);
795}
796
797
Alexandre Lisiona1eee3c2015-08-10 13:44:51 -0400798# pragma mark - CallnDelegate
Alexandre Lision58cab672015-06-09 15:25:40 -0400799
800- (void) callShouldToggleFullScreen
801{
802 if(self.splitView.isInFullScreenMode)
803 [self.splitView exitFullScreenModeWithOptions:nil];
804 else {
805 NSApplicationPresentationOptions options = NSApplicationPresentationDefault +NSApplicationPresentationAutoHideDock +
806 NSApplicationPresentationAutoHideMenuBar + NSApplicationPresentationAutoHideToolbar;
807 NSDictionary *opts = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithUnsignedInt:options],
808 NSFullScreenModeApplicationPresentationOptions, nil];
809
810 [self.splitView enterFullScreenMode:[NSScreen mainScreen] withOptions:opts];
811 }
812}
813
Alexandre Lisiona1eee3c2015-08-10 13:44:51 -0400814-(void) mouseIsMoving:(BOOL) move
815{
816 [[controlsPanel animator] setAlphaValue:move]; // fade out
817 [[headerContainer animator] setAlphaValue:move];
818}
819
Alexandre Lisionc5148052015-03-04 15:10:35 -0500820@end