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; |
Kateryna Kostiuk | 0a54fa8 | 2017-06-14 15:48:37 -0400 | [diff] [blame] | 39 | @property (assign) IBOutlet NSButton *enableHardwareAccelerationButton; |
Alexandre Lision | b8a8116 | 2015-03-20 18:08:01 -0400 | [diff] [blame] | 40 | |
Alexandre Lision | e0c7e33 | 2015-07-30 11:11:33 -0400 | [diff] [blame] | 41 | @property BOOL shouldHandlePreview; |
| 42 | |
Alexandre Lision | 4a7b95e | 2015-02-20 10:06:43 -0500 | [diff] [blame] | 43 | @end |
| 44 | |
| 45 | @implementation VideoPrefsVC |
Alexandre Lision | b8a8116 | 2015-03-20 18:08:01 -0400 | [diff] [blame] | 46 | @synthesize previewView; |
| 47 | @synthesize videoDevicesList; |
| 48 | @synthesize sizesList; |
| 49 | @synthesize ratesList; |
Alexandre Lision | 4a7b95e | 2015-02-20 10:06:43 -0500 | [diff] [blame] | 50 | |
Alexandre Lision | b8a8116 | 2015-03-20 18:08:01 -0400 | [diff] [blame] | 51 | QMetaObject::Connection frameUpdated; |
| 52 | QMetaObject::Connection previewStarted; |
| 53 | QMetaObject::Connection previewStopped; |
| 54 | |
| 55 | - (void)loadView |
| 56 | { |
| 57 | [super loadView]; |
| 58 | |
Alexandre Lision | 861d010 | 2016-02-09 17:19:27 -0500 | [diff] [blame] | 59 | // Make sure models are loaded |
Alexandre Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 60 | Video::ConfigurationProxy::deviceModel().rowCount(); |
| 61 | Video::ConfigurationProxy::resolutionModel().rowCount(); |
| 62 | Video::ConfigurationProxy::rateModel().rowCount(); |
Alexandre Lision | b8a8116 | 2015-03-20 18:08:01 -0400 | [diff] [blame] | 63 | |
Alexandre Lision | 861d010 | 2016-02-09 17:19:27 -0500 | [diff] [blame] | 64 | // Prepopulate values |
Alexandre Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 65 | QModelIndex qDeviceIdx = Video::ConfigurationProxy::deviceSelectionModel().currentIndex(); |
Alexandre Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 66 | [videoDevicesList addItemWithTitle:Video::ConfigurationProxy::deviceModel().data(qDeviceIdx, Qt::DisplayRole).toString().toNSString()]; |
Alexandre Lision | b8a8116 | 2015-03-20 18:08:01 -0400 | [diff] [blame] | 67 | |
Alexandre Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 68 | QModelIndex qSizeIdx = Video::ConfigurationProxy::resolutionSelectionModel().currentIndex(); |
| 69 | [sizesList addItemWithTitle:Video::ConfigurationProxy::resolutionModel().data(qSizeIdx, Qt::DisplayRole).toString().toNSString()]; |
Alexandre Lision | b8a8116 | 2015-03-20 18:08:01 -0400 | [diff] [blame] | 70 | |
Alexandre Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 71 | QModelIndex qRate = Video::ConfigurationProxy::rateSelectionModel().currentIndex(); |
| 72 | [ratesList addItemWithTitle:Video::ConfigurationProxy::rateModel().data(qDeviceIdx, Qt::DisplayRole).toString().toNSString()]; |
Alexandre Lision | b8a8116 | 2015-03-20 18:08:01 -0400 | [diff] [blame] | 73 | |
Alexandre Lision | 861d010 | 2016-02-09 17:19:27 -0500 | [diff] [blame] | 74 | // connect to model reset (device may have changed) and selection changed signals |
| 75 | QObject::connect(qobject_cast<QAbstractProxyModel*>(&Video::ConfigurationProxy::resolutionModel()), |
| 76 | &QAbstractProxyModel::modelReset, |
| 77 | [=]() { |
| 78 | [sizesList removeAllItems]; |
| 79 | }); |
Alexandre Lision | b8a8116 | 2015-03-20 18:08:01 -0400 | [diff] [blame] | 80 | |
Alexandre Lision | 861d010 | 2016-02-09 17:19:27 -0500 | [diff] [blame] | 81 | QObject::connect(&Video::ConfigurationProxy::resolutionSelectionModel(), |
| 82 | &QItemSelectionModel::currentChanged, |
| 83 | [=](const QModelIndex ¤t, const QModelIndex &previous) { |
| 84 | if (!current.isValid()) { |
| 85 | return; |
| 86 | } |
| 87 | [sizesList removeAllItems]; |
| 88 | [sizesList addItemWithTitle:Video::ConfigurationProxy::resolutionSelectionModel().currentIndex().data(Qt::DisplayRole).toString().toNSString()]; |
| 89 | |
| 90 | }); |
| 91 | |
| 92 | QObject::connect(qobject_cast<QAbstractProxyModel*>(&Video::ConfigurationProxy::rateModel()), |
| 93 | &QAbstractProxyModel::modelReset, |
| 94 | [=]() { |
| 95 | [ratesList removeAllItems]; |
| 96 | }); |
| 97 | |
| 98 | QObject::connect(&Video::ConfigurationProxy::rateSelectionModel(), |
| 99 | &QItemSelectionModel::currentChanged, |
| 100 | [=](const QModelIndex ¤t, const QModelIndex &previous) { |
| 101 | if (!current.isValid()) { |
| 102 | return; |
| 103 | } |
| 104 | [ratesList removeAllItems]; |
| 105 | [ratesList addItemWithTitle:Video::ConfigurationProxy::rateSelectionModel().currentIndex().data(Qt::DisplayRole).toString().toNSString()]; |
| 106 | |
| 107 | }); |
Alexandre Lision | b8a8116 | 2015-03-20 18:08:01 -0400 | [diff] [blame] | 108 | |
Alexandre Lision | b8a8116 | 2015-03-20 18:08:01 -0400 | [diff] [blame] | 109 | [previewView setWantsLayer:YES]; |
| 110 | [previewView setLayer:[CALayer layer]]; |
| 111 | [previewView.layer setBackgroundColor:[NSColor blackColor].CGColor]; |
| 112 | [previewView.layer setContentsGravity:kCAGravityResizeAspect]; |
| 113 | [previewView.layer setFrame:previewView.frame]; |
| 114 | [previewView.layer setBounds:previewView.frame]; |
Kateryna Kostiuk | 0a54fa8 | 2017-06-14 15:48:37 -0400 | [diff] [blame] | 115 | |
| 116 | [self.enableHardwareAccelerationButton setState:Video::ConfigurationProxy::getDecodingAccelerated()]; |
| 117 | |
Alexandre Lision | b8a8116 | 2015-03-20 18:08:01 -0400 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | - (IBAction)chooseDevice:(id)sender { |
| 121 | int index = [sender indexOfSelectedItem]; |
Alexandre Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 122 | QModelIndex qIdx = Video::ConfigurationProxy::deviceModel().index(index, 0); |
| 123 | Video::ConfigurationProxy::deviceSelectionModel().setCurrentIndex(qIdx, QItemSelectionModel::ClearAndSelect); |
Alexandre Lision | b8a8116 | 2015-03-20 18:08:01 -0400 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | - (IBAction)chooseSize:(id)sender { |
| 127 | int index = [sender indexOfSelectedItem]; |
Alexandre Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 128 | QModelIndex qIdx = Video::ConfigurationProxy::resolutionModel().index(index, 0); |
| 129 | Video::ConfigurationProxy::resolutionSelectionModel().setCurrentIndex(qIdx, QItemSelectionModel::ClearAndSelect); |
Alexandre Lision | b8a8116 | 2015-03-20 18:08:01 -0400 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | - (IBAction)chooseRate:(id)sender { |
| 133 | int index = [sender indexOfSelectedItem]; |
Alexandre Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 134 | QModelIndex qIdx = Video::ConfigurationProxy::rateModel().index(index, 0); |
| 135 | Video::ConfigurationProxy::rateSelectionModel().setCurrentIndex(qIdx, QItemSelectionModel::ClearAndSelect); |
Alexandre Lision | b8a8116 | 2015-03-20 18:08:01 -0400 | [diff] [blame] | 136 | } |
| 137 | |
Kateryna Kostiuk | 0a54fa8 | 2017-06-14 15:48:37 -0400 | [diff] [blame] | 138 | - (IBAction)toggleHardwareAcceleration:(NSButton *)sender { |
| 139 | bool enabled = [sender state]==NSOnState; |
| 140 | Video::ConfigurationProxy::setDecodingAccelerated(enabled); |
| 141 | } |
| 142 | |
Alexandre Lision | b8a8116 | 2015-03-20 18:08:01 -0400 | [diff] [blame] | 143 | - (void) connectPreviewSignals |
| 144 | { |
| 145 | QObject::disconnect(frameUpdated); |
| 146 | QObject::disconnect(previewStopped); |
| 147 | QObject::disconnect(previewStarted); |
Alexandre Lision | d7b8546 | 2015-11-10 17:14:29 -0500 | [diff] [blame] | 148 | |
Alexandre Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 149 | previewStarted = QObject::connect(&Video::PreviewManager::instance(), |
Alexandre Lision | d7b8546 | 2015-11-10 17:14:29 -0500 | [diff] [blame] | 150 | &Video::PreviewManager::previewStarted, |
| 151 | [=](Video::Renderer* renderer) { |
| 152 | NSLog(@"Preview started"); |
| 153 | QObject::disconnect(frameUpdated); |
| 154 | frameUpdated = QObject::connect(renderer, |
| 155 | &Video::Renderer::frameUpdated, |
| 156 | [=]() { |
| 157 | [self renderer:Video::PreviewManager::instance().previewRenderer() renderFrameForView:previewView]; |
| 158 | }); |
| 159 | }); |
Alexandre Lision | b8a8116 | 2015-03-20 18:08:01 -0400 | [diff] [blame] | 160 | |
Alexandre Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 161 | previewStopped = QObject::connect(&Video::PreviewManager::instance(), |
Alexandre Lision | d7b8546 | 2015-11-10 17:14:29 -0500 | [diff] [blame] | 162 | &Video::PreviewManager::previewStopped, |
| 163 | [=](Video::Renderer* renderer) { |
| 164 | NSLog(@"Preview stopped"); |
| 165 | QObject::disconnect(frameUpdated); |
| 166 | [previewView.layer setContents:nil]; |
| 167 | }); |
Alexandre Lision | b8a8116 | 2015-03-20 18:08:01 -0400 | [diff] [blame] | 168 | |
Alexandre Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 169 | frameUpdated = QObject::connect(Video::PreviewManager::instance().previewRenderer(), |
Alexandre Lision | d7b8546 | 2015-11-10 17:14:29 -0500 | [diff] [blame] | 170 | &Video::Renderer::frameUpdated, |
| 171 | [=]() { |
| 172 | [self renderer:Video::PreviewManager::instance().previewRenderer() |
Alexandre Lision | b8a8116 | 2015-03-20 18:08:01 -0400 | [diff] [blame] | 173 | renderFrameForView:previewView]; |
Alexandre Lision | d7b8546 | 2015-11-10 17:14:29 -0500 | [diff] [blame] | 174 | }); |
Alexandre Lision | b8a8116 | 2015-03-20 18:08:01 -0400 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | -(void) renderer: (Video::Renderer*)renderer renderFrameForView:(NSView*) view |
| 178 | { |
Alexandre Lision | b8a8116 | 2015-03-20 18:08:01 -0400 | [diff] [blame] | 179 | QSize res = renderer->size(); |
| 180 | |
Alexandre Lision | 6731e13 | 2015-10-14 14:29:06 -0400 | [diff] [blame] | 181 | auto frame_ptr = renderer->currentFrame(); |
| 182 | auto frame_data = frame_ptr.ptr; |
| 183 | if (!frame_data) |
| 184 | return; |
Alexandre Lision | b8a8116 | 2015-03-20 18:08:01 -0400 | [diff] [blame] | 185 | |
| 186 | CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); |
Alexandre Lision | 6731e13 | 2015-10-14 14:29:06 -0400 | [diff] [blame] | 187 | CGContextRef newContext = CGBitmapContextCreate(frame_data, |
Alexandre Lision | b8a8116 | 2015-03-20 18:08:01 -0400 | [diff] [blame] | 188 | res.width(), |
| 189 | res.height(), |
| 190 | 8, |
| 191 | 4*res.width(), |
| 192 | colorSpace, |
| 193 | kCGImageAlphaPremultipliedLast); |
| 194 | |
| 195 | |
| 196 | CGImageRef newImage = CGBitmapContextCreateImage(newContext); |
| 197 | |
| 198 | /*We release some components*/ |
| 199 | CGContextRelease(newContext); |
| 200 | CGColorSpaceRelease(colorSpace); |
| 201 | |
| 202 | [CATransaction begin]; |
| 203 | view.layer.contents = (__bridge id)newImage; |
| 204 | [CATransaction commit]; |
| 205 | |
| 206 | CFRelease(newImage); |
| 207 | } |
| 208 | |
Alexandre Lision | e78a09b | 2015-07-03 15:52:23 -0400 | [diff] [blame] | 209 | - (void) viewWillAppear |
| 210 | { |
Alexandre Lision | d7b8546 | 2015-11-10 17:14:29 -0500 | [diff] [blame] | 211 | // check if preview has to be started/stopped by this controller |
| 212 | self.shouldHandlePreview = !Video::PreviewManager::instance().previewRenderer()->isRendering(); |
| 213 | |
| 214 | [self connectPreviewSignals]; |
Alexandre Lision | e0c7e33 | 2015-07-30 11:11:33 -0400 | [diff] [blame] | 215 | if (self.shouldHandlePreview) { |
Alexandre Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 216 | Video::PreviewManager::instance().startPreview(); |
Alexandre Lision | e0c7e33 | 2015-07-30 11:11:33 -0400 | [diff] [blame] | 217 | } |
Alexandre Lision | b8a8116 | 2015-03-20 18:08:01 -0400 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | - (void)viewWillDisappear |
| 221 | { |
Alexandre Lision | d7b8546 | 2015-11-10 17:14:29 -0500 | [diff] [blame] | 222 | QObject::disconnect(frameUpdated); |
| 223 | QObject::disconnect(previewStopped); |
| 224 | QObject::disconnect(previewStarted); |
Alexandre Lision | e0c7e33 | 2015-07-30 11:11:33 -0400 | [diff] [blame] | 225 | if (self.shouldHandlePreview) { |
Alexandre Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 226 | Video::PreviewManager::instance().stopPreview(); |
Alexandre Lision | e0c7e33 | 2015-07-30 11:11:33 -0400 | [diff] [blame] | 227 | } |
Alexandre Lision | b8a8116 | 2015-03-20 18:08:01 -0400 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | #pragma mark - NSMenuDelegate methods |
| 231 | |
| 232 | - (BOOL)menu:(NSMenu *)menu updateItem:(NSMenuItem *)item atIndex:(NSInteger)index shouldCancel:(BOOL)shouldCancel |
| 233 | { |
| 234 | QModelIndex qIdx; |
Alexandre Lision | e1cf446 | 2015-10-08 15:44:38 -0400 | [diff] [blame] | 235 | if(self.videoDevicesList.menu == menu) { |
Alexandre Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 236 | qIdx = Video::ConfigurationProxy::deviceModel().index(index, 0); |
| 237 | [item setTitle:Video::ConfigurationProxy::deviceModel().data(qIdx, Qt::DisplayRole).toString().toNSString()]; |
Alexandre Lision | 861d010 | 2016-02-09 17:19:27 -0500 | [diff] [blame] | 238 | if (qIdx == Video::ConfigurationProxy::deviceSelectionModel().currentIndex()) { |
| 239 | [videoDevicesList selectItem:item]; |
| 240 | } |
Alexandre Lision | e1cf446 | 2015-10-08 15:44:38 -0400 | [diff] [blame] | 241 | } else if(self.sizesList.menu == menu) { |
Alexandre Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 242 | qIdx = Video::ConfigurationProxy::resolutionModel().index(index, 0); |
| 243 | [item setTitle:Video::ConfigurationProxy::resolutionModel().data(qIdx, Qt::DisplayRole).toString().toNSString()]; |
Alexandre Lision | 861d010 | 2016-02-09 17:19:27 -0500 | [diff] [blame] | 244 | if (qIdx == Video::ConfigurationProxy::resolutionSelectionModel().currentIndex()) { |
| 245 | [sizesList selectItem:item]; |
| 246 | } |
Alexandre Lision | b8a8116 | 2015-03-20 18:08:01 -0400 | [diff] [blame] | 247 | |
Alexandre Lision | e1cf446 | 2015-10-08 15:44:38 -0400 | [diff] [blame] | 248 | } else if(self.ratesList.menu == menu) { |
Alexandre Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 249 | qIdx = Video::ConfigurationProxy::rateModel().index(index, 0); |
| 250 | [item setTitle:Video::ConfigurationProxy::rateModel().data(qIdx, Qt::DisplayRole).toString().toNSString()]; |
Alexandre Lision | 861d010 | 2016-02-09 17:19:27 -0500 | [diff] [blame] | 251 | if (qIdx == Video::ConfigurationProxy::rateSelectionModel().currentIndex()) { |
| 252 | [ratesList selectItem:item]; |
| 253 | } |
Alexandre Lision | b8a8116 | 2015-03-20 18:08:01 -0400 | [diff] [blame] | 254 | } |
| 255 | return YES; |
| 256 | } |
| 257 | |
| 258 | - (NSInteger)numberOfItemsInMenu:(NSMenu *)menu |
| 259 | { |
Alexandre Lision | e1cf446 | 2015-10-08 15:44:38 -0400 | [diff] [blame] | 260 | if(self.videoDevicesList.menu == menu) { |
Alexandre Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 261 | return Video::ConfigurationProxy::deviceModel().rowCount(); |
Alexandre Lision | e1cf446 | 2015-10-08 15:44:38 -0400 | [diff] [blame] | 262 | } else if(self.sizesList.menu == menu) { |
Alexandre Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 263 | return Video::ConfigurationProxy::resolutionModel().rowCount(); |
Alexandre Lision | e1cf446 | 2015-10-08 15:44:38 -0400 | [diff] [blame] | 264 | } else if(self.ratesList.menu == menu) { |
Alexandre Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 265 | return Video::ConfigurationProxy::rateModel().rowCount(); |
Alexandre Lision | b8a8116 | 2015-03-20 18:08:01 -0400 | [diff] [blame] | 266 | } |
Alexandre Lision | e1cf446 | 2015-10-08 15:44:38 -0400 | [diff] [blame] | 267 | return 0; |
Alexandre Lision | 4a7b95e | 2015-02-20 10:06:43 -0500 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | @end |