Alexandre Lision | 8521baa | 2015-03-13 11:08:00 -0400 | [diff] [blame] | 1 | /* |
Alexandre Lision | 9fe374b | 2016-01-06 10:17:31 -0500 | [diff] [blame] | 2 | * Copyright (C) 2015-2016 Savoir-faire Linux Inc. |
Alexandre Lision | 8521baa | 2015-03-13 11:08:00 -0400 | [diff] [blame] | 3 | * Author: Alexandre Lision <alexandre.lision@savoirfairelinux.com> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 3 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
Alexandre Lision | 8521baa | 2015-03-13 11:08:00 -0400 | [diff] [blame] | 18 | */ |
Alexandre Lision | 4a7b95e | 2015-02-20 10:06:43 -0500 | [diff] [blame] | 19 | #import "VideoPrefsVC.h" |
| 20 | |
Alexandre Lision | b8a8116 | 2015-03-20 18:08:01 -0400 | [diff] [blame] | 21 | #import <QuartzCore/QuartzCore.h> |
| 22 | |
| 23 | #import <QItemSelectionModel> |
| 24 | #import <QAbstractProxyModel> |
| 25 | |
| 26 | #import <video/configurationproxy.h> |
| 27 | #import <video/sourcemodel.h> |
| 28 | #import <video/previewmanager.h> |
| 29 | #import <video/renderer.h> |
| 30 | #import <video/device.h> |
| 31 | #import <video/devicemodel.h> |
| 32 | |
Alexandre Lision | 4a7b95e | 2015-02-20 10:06:43 -0500 | [diff] [blame] | 33 | @interface VideoPrefsVC () |
| 34 | |
Alexandre Lision | 861d010 | 2016-02-09 17:19:27 -0500 | [diff] [blame^] | 35 | @property (assign) IBOutlet NSView* previewView; |
| 36 | @property (assign) IBOutlet NSPopUpButton* videoDevicesList; |
| 37 | @property (assign) IBOutlet NSPopUpButton* sizesList; |
| 38 | @property (assign) IBOutlet NSPopUpButton* ratesList; |
Alexandre Lision | b8a8116 | 2015-03-20 18:08:01 -0400 | [diff] [blame] | 39 | |
Alexandre Lision | e0c7e33 | 2015-07-30 11:11:33 -0400 | [diff] [blame] | 40 | @property BOOL shouldHandlePreview; |
| 41 | |
Alexandre Lision | 4a7b95e | 2015-02-20 10:06:43 -0500 | [diff] [blame] | 42 | @end |
| 43 | |
| 44 | @implementation VideoPrefsVC |
Alexandre Lision | b8a8116 | 2015-03-20 18:08:01 -0400 | [diff] [blame] | 45 | @synthesize previewView; |
| 46 | @synthesize videoDevicesList; |
| 47 | @synthesize sizesList; |
| 48 | @synthesize ratesList; |
Alexandre Lision | 4a7b95e | 2015-02-20 10:06:43 -0500 | [diff] [blame] | 49 | |
Alexandre Lision | b8a8116 | 2015-03-20 18:08:01 -0400 | [diff] [blame] | 50 | QMetaObject::Connection frameUpdated; |
| 51 | QMetaObject::Connection previewStarted; |
| 52 | QMetaObject::Connection previewStopped; |
| 53 | |
| 54 | - (void)loadView |
| 55 | { |
| 56 | [super loadView]; |
| 57 | |
Alexandre Lision | 861d010 | 2016-02-09 17:19:27 -0500 | [diff] [blame^] | 58 | // Make sure models are loaded |
Alexandre Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 59 | Video::ConfigurationProxy::deviceModel().rowCount(); |
| 60 | Video::ConfigurationProxy::resolutionModel().rowCount(); |
| 61 | Video::ConfigurationProxy::rateModel().rowCount(); |
Alexandre Lision | b8a8116 | 2015-03-20 18:08:01 -0400 | [diff] [blame] | 62 | |
Alexandre Lision | 861d010 | 2016-02-09 17:19:27 -0500 | [diff] [blame^] | 63 | // Prepopulate values |
Alexandre Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 64 | QModelIndex qDeviceIdx = Video::ConfigurationProxy::deviceSelectionModel().currentIndex(); |
Alexandre Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 65 | [videoDevicesList addItemWithTitle:Video::ConfigurationProxy::deviceModel().data(qDeviceIdx, Qt::DisplayRole).toString().toNSString()]; |
Alexandre Lision | b8a8116 | 2015-03-20 18:08:01 -0400 | [diff] [blame] | 66 | |
Alexandre Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 67 | QModelIndex qSizeIdx = Video::ConfigurationProxy::resolutionSelectionModel().currentIndex(); |
| 68 | [sizesList addItemWithTitle:Video::ConfigurationProxy::resolutionModel().data(qSizeIdx, Qt::DisplayRole).toString().toNSString()]; |
Alexandre Lision | b8a8116 | 2015-03-20 18:08:01 -0400 | [diff] [blame] | 69 | |
Alexandre Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 70 | QModelIndex qRate = Video::ConfigurationProxy::rateSelectionModel().currentIndex(); |
| 71 | [ratesList addItemWithTitle:Video::ConfigurationProxy::rateModel().data(qDeviceIdx, Qt::DisplayRole).toString().toNSString()]; |
Alexandre Lision | b8a8116 | 2015-03-20 18:08:01 -0400 | [diff] [blame] | 72 | |
Alexandre Lision | 861d010 | 2016-02-09 17:19:27 -0500 | [diff] [blame^] | 73 | // connect to model reset (device may have changed) and selection changed signals |
| 74 | QObject::connect(qobject_cast<QAbstractProxyModel*>(&Video::ConfigurationProxy::resolutionModel()), |
| 75 | &QAbstractProxyModel::modelReset, |
| 76 | [=]() { |
| 77 | [sizesList removeAllItems]; |
| 78 | }); |
Alexandre Lision | b8a8116 | 2015-03-20 18:08:01 -0400 | [diff] [blame] | 79 | |
Alexandre Lision | 861d010 | 2016-02-09 17:19:27 -0500 | [diff] [blame^] | 80 | QObject::connect(&Video::ConfigurationProxy::resolutionSelectionModel(), |
| 81 | &QItemSelectionModel::currentChanged, |
| 82 | [=](const QModelIndex ¤t, const QModelIndex &previous) { |
| 83 | if (!current.isValid()) { |
| 84 | return; |
| 85 | } |
| 86 | [sizesList removeAllItems]; |
| 87 | [sizesList addItemWithTitle:Video::ConfigurationProxy::resolutionSelectionModel().currentIndex().data(Qt::DisplayRole).toString().toNSString()]; |
| 88 | |
| 89 | }); |
| 90 | |
| 91 | QObject::connect(qobject_cast<QAbstractProxyModel*>(&Video::ConfigurationProxy::rateModel()), |
| 92 | &QAbstractProxyModel::modelReset, |
| 93 | [=]() { |
| 94 | [ratesList removeAllItems]; |
| 95 | }); |
| 96 | |
| 97 | QObject::connect(&Video::ConfigurationProxy::rateSelectionModel(), |
| 98 | &QItemSelectionModel::currentChanged, |
| 99 | [=](const QModelIndex ¤t, const QModelIndex &previous) { |
| 100 | if (!current.isValid()) { |
| 101 | return; |
| 102 | } |
| 103 | [ratesList removeAllItems]; |
| 104 | [ratesList addItemWithTitle:Video::ConfigurationProxy::rateSelectionModel().currentIndex().data(Qt::DisplayRole).toString().toNSString()]; |
| 105 | |
| 106 | }); |
Alexandre Lision | b8a8116 | 2015-03-20 18:08:01 -0400 | [diff] [blame] | 107 | |
Alexandre Lision | b8a8116 | 2015-03-20 18:08:01 -0400 | [diff] [blame] | 108 | [previewView setWantsLayer:YES]; |
| 109 | [previewView setLayer:[CALayer layer]]; |
| 110 | [previewView.layer setBackgroundColor:[NSColor blackColor].CGColor]; |
| 111 | [previewView.layer setContentsGravity:kCAGravityResizeAspect]; |
| 112 | [previewView.layer setFrame:previewView.frame]; |
| 113 | [previewView.layer setBounds:previewView.frame]; |
Alexandre Lision | b8a8116 | 2015-03-20 18:08:01 -0400 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | - (IBAction)chooseDevice:(id)sender { |
| 117 | int index = [sender indexOfSelectedItem]; |
Alexandre Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 118 | QModelIndex qIdx = Video::ConfigurationProxy::deviceModel().index(index, 0); |
| 119 | Video::ConfigurationProxy::deviceSelectionModel().setCurrentIndex(qIdx, QItemSelectionModel::ClearAndSelect); |
Alexandre Lision | b8a8116 | 2015-03-20 18:08:01 -0400 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | - (IBAction)chooseSize:(id)sender { |
| 123 | int index = [sender indexOfSelectedItem]; |
Alexandre Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 124 | QModelIndex qIdx = Video::ConfigurationProxy::resolutionModel().index(index, 0); |
| 125 | Video::ConfigurationProxy::resolutionSelectionModel().setCurrentIndex(qIdx, QItemSelectionModel::ClearAndSelect); |
Alexandre Lision | b8a8116 | 2015-03-20 18:08:01 -0400 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | - (IBAction)chooseRate:(id)sender { |
| 129 | int index = [sender indexOfSelectedItem]; |
Alexandre Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 130 | QModelIndex qIdx = Video::ConfigurationProxy::rateModel().index(index, 0); |
| 131 | Video::ConfigurationProxy::rateSelectionModel().setCurrentIndex(qIdx, QItemSelectionModel::ClearAndSelect); |
Alexandre Lision | b8a8116 | 2015-03-20 18:08:01 -0400 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | - (void) connectPreviewSignals |
| 135 | { |
| 136 | QObject::disconnect(frameUpdated); |
| 137 | QObject::disconnect(previewStopped); |
| 138 | QObject::disconnect(previewStarted); |
Alexandre Lision | d7b8546 | 2015-11-10 17:14:29 -0500 | [diff] [blame] | 139 | |
Alexandre Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 140 | previewStarted = QObject::connect(&Video::PreviewManager::instance(), |
Alexandre Lision | d7b8546 | 2015-11-10 17:14:29 -0500 | [diff] [blame] | 141 | &Video::PreviewManager::previewStarted, |
| 142 | [=](Video::Renderer* renderer) { |
| 143 | NSLog(@"Preview started"); |
| 144 | QObject::disconnect(frameUpdated); |
| 145 | frameUpdated = QObject::connect(renderer, |
| 146 | &Video::Renderer::frameUpdated, |
| 147 | [=]() { |
| 148 | [self renderer:Video::PreviewManager::instance().previewRenderer() renderFrameForView:previewView]; |
| 149 | }); |
| 150 | }); |
Alexandre Lision | b8a8116 | 2015-03-20 18:08:01 -0400 | [diff] [blame] | 151 | |
Alexandre Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 152 | previewStopped = QObject::connect(&Video::PreviewManager::instance(), |
Alexandre Lision | d7b8546 | 2015-11-10 17:14:29 -0500 | [diff] [blame] | 153 | &Video::PreviewManager::previewStopped, |
| 154 | [=](Video::Renderer* renderer) { |
| 155 | NSLog(@"Preview stopped"); |
| 156 | QObject::disconnect(frameUpdated); |
| 157 | [previewView.layer setContents:nil]; |
| 158 | }); |
Alexandre Lision | b8a8116 | 2015-03-20 18:08:01 -0400 | [diff] [blame] | 159 | |
Alexandre Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 160 | frameUpdated = QObject::connect(Video::PreviewManager::instance().previewRenderer(), |
Alexandre Lision | d7b8546 | 2015-11-10 17:14:29 -0500 | [diff] [blame] | 161 | &Video::Renderer::frameUpdated, |
| 162 | [=]() { |
| 163 | [self renderer:Video::PreviewManager::instance().previewRenderer() |
Alexandre Lision | b8a8116 | 2015-03-20 18:08:01 -0400 | [diff] [blame] | 164 | renderFrameForView:previewView]; |
Alexandre Lision | d7b8546 | 2015-11-10 17:14:29 -0500 | [diff] [blame] | 165 | }); |
Alexandre Lision | b8a8116 | 2015-03-20 18:08:01 -0400 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | -(void) renderer: (Video::Renderer*)renderer renderFrameForView:(NSView*) view |
| 169 | { |
Alexandre Lision | b8a8116 | 2015-03-20 18:08:01 -0400 | [diff] [blame] | 170 | QSize res = renderer->size(); |
| 171 | |
Alexandre Lision | 6731e13 | 2015-10-14 14:29:06 -0400 | [diff] [blame] | 172 | auto frame_ptr = renderer->currentFrame(); |
| 173 | auto frame_data = frame_ptr.ptr; |
| 174 | if (!frame_data) |
| 175 | return; |
Alexandre Lision | b8a8116 | 2015-03-20 18:08:01 -0400 | [diff] [blame] | 176 | |
| 177 | CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); |
Alexandre Lision | 6731e13 | 2015-10-14 14:29:06 -0400 | [diff] [blame] | 178 | CGContextRef newContext = CGBitmapContextCreate(frame_data, |
Alexandre Lision | b8a8116 | 2015-03-20 18:08:01 -0400 | [diff] [blame] | 179 | res.width(), |
| 180 | res.height(), |
| 181 | 8, |
| 182 | 4*res.width(), |
| 183 | colorSpace, |
| 184 | kCGImageAlphaPremultipliedLast); |
| 185 | |
| 186 | |
| 187 | CGImageRef newImage = CGBitmapContextCreateImage(newContext); |
| 188 | |
| 189 | /*We release some components*/ |
| 190 | CGContextRelease(newContext); |
| 191 | CGColorSpaceRelease(colorSpace); |
| 192 | |
| 193 | [CATransaction begin]; |
| 194 | view.layer.contents = (__bridge id)newImage; |
| 195 | [CATransaction commit]; |
| 196 | |
| 197 | CFRelease(newImage); |
| 198 | } |
| 199 | |
Alexandre Lision | e78a09b | 2015-07-03 15:52:23 -0400 | [diff] [blame] | 200 | - (void) viewWillAppear |
| 201 | { |
Alexandre Lision | d7b8546 | 2015-11-10 17:14:29 -0500 | [diff] [blame] | 202 | // check if preview has to be started/stopped by this controller |
| 203 | self.shouldHandlePreview = !Video::PreviewManager::instance().previewRenderer()->isRendering(); |
| 204 | |
| 205 | [self connectPreviewSignals]; |
Alexandre Lision | e0c7e33 | 2015-07-30 11:11:33 -0400 | [diff] [blame] | 206 | if (self.shouldHandlePreview) { |
Alexandre Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 207 | Video::PreviewManager::instance().startPreview(); |
Alexandre Lision | e0c7e33 | 2015-07-30 11:11:33 -0400 | [diff] [blame] | 208 | } |
Alexandre Lision | b8a8116 | 2015-03-20 18:08:01 -0400 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | - (void)viewWillDisappear |
| 212 | { |
Alexandre Lision | d7b8546 | 2015-11-10 17:14:29 -0500 | [diff] [blame] | 213 | QObject::disconnect(frameUpdated); |
| 214 | QObject::disconnect(previewStopped); |
| 215 | QObject::disconnect(previewStarted); |
Alexandre Lision | e0c7e33 | 2015-07-30 11:11:33 -0400 | [diff] [blame] | 216 | if (self.shouldHandlePreview) { |
Alexandre Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 217 | Video::PreviewManager::instance().stopPreview(); |
Alexandre Lision | e0c7e33 | 2015-07-30 11:11:33 -0400 | [diff] [blame] | 218 | } |
Alexandre Lision | b8a8116 | 2015-03-20 18:08:01 -0400 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | #pragma mark - NSMenuDelegate methods |
| 222 | |
| 223 | - (BOOL)menu:(NSMenu *)menu updateItem:(NSMenuItem *)item atIndex:(NSInteger)index shouldCancel:(BOOL)shouldCancel |
| 224 | { |
| 225 | QModelIndex qIdx; |
Alexandre Lision | e1cf446 | 2015-10-08 15:44:38 -0400 | [diff] [blame] | 226 | if(self.videoDevicesList.menu == menu) { |
Alexandre Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 227 | qIdx = Video::ConfigurationProxy::deviceModel().index(index, 0); |
| 228 | [item setTitle:Video::ConfigurationProxy::deviceModel().data(qIdx, Qt::DisplayRole).toString().toNSString()]; |
Alexandre Lision | 861d010 | 2016-02-09 17:19:27 -0500 | [diff] [blame^] | 229 | if (qIdx == Video::ConfigurationProxy::deviceSelectionModel().currentIndex()) { |
| 230 | [videoDevicesList selectItem:item]; |
| 231 | } |
Alexandre Lision | e1cf446 | 2015-10-08 15:44:38 -0400 | [diff] [blame] | 232 | } else if(self.sizesList.menu == menu) { |
Alexandre Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 233 | qIdx = Video::ConfigurationProxy::resolutionModel().index(index, 0); |
| 234 | [item setTitle:Video::ConfigurationProxy::resolutionModel().data(qIdx, Qt::DisplayRole).toString().toNSString()]; |
Alexandre Lision | 861d010 | 2016-02-09 17:19:27 -0500 | [diff] [blame^] | 235 | if (qIdx == Video::ConfigurationProxy::resolutionSelectionModel().currentIndex()) { |
| 236 | [sizesList selectItem:item]; |
| 237 | } |
Alexandre Lision | b8a8116 | 2015-03-20 18:08:01 -0400 | [diff] [blame] | 238 | |
Alexandre Lision | e1cf446 | 2015-10-08 15:44:38 -0400 | [diff] [blame] | 239 | } else if(self.ratesList.menu == menu) { |
Alexandre Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 240 | qIdx = Video::ConfigurationProxy::rateModel().index(index, 0); |
| 241 | [item setTitle:Video::ConfigurationProxy::rateModel().data(qIdx, Qt::DisplayRole).toString().toNSString()]; |
Alexandre Lision | 861d010 | 2016-02-09 17:19:27 -0500 | [diff] [blame^] | 242 | if (qIdx == Video::ConfigurationProxy::rateSelectionModel().currentIndex()) { |
| 243 | [ratesList selectItem:item]; |
| 244 | } |
Alexandre Lision | b8a8116 | 2015-03-20 18:08:01 -0400 | [diff] [blame] | 245 | } |
| 246 | return YES; |
| 247 | } |
| 248 | |
| 249 | - (NSInteger)numberOfItemsInMenu:(NSMenu *)menu |
| 250 | { |
Alexandre Lision | e1cf446 | 2015-10-08 15:44:38 -0400 | [diff] [blame] | 251 | if(self.videoDevicesList.menu == menu) { |
Alexandre Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 252 | return Video::ConfigurationProxy::deviceModel().rowCount(); |
Alexandre Lision | e1cf446 | 2015-10-08 15:44:38 -0400 | [diff] [blame] | 253 | } else if(self.sizesList.menu == menu) { |
Alexandre Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 254 | return Video::ConfigurationProxy::resolutionModel().rowCount(); |
Alexandre Lision | e1cf446 | 2015-10-08 15:44:38 -0400 | [diff] [blame] | 255 | } else if(self.ratesList.menu == menu) { |
Alexandre Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 256 | return Video::ConfigurationProxy::rateModel().rowCount(); |
Alexandre Lision | b8a8116 | 2015-03-20 18:08:01 -0400 | [diff] [blame] | 257 | } |
Alexandre Lision | e1cf446 | 2015-10-08 15:44:38 -0400 | [diff] [blame] | 258 | return 0; |
Alexandre Lision | 4a7b95e | 2015-02-20 10:06:43 -0500 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | @end |