| 1 |
/* |
| 2 |
* Copyright (C) 2013 Canonical, Ltd. |
| 3 |
* |
| 4 |
* Authors: |
| 5 |
* Ugo Riboni <[email protected]> |
| 6 |
* MichaĆ Sawicz <[email protected]> |
| 7 |
* Renato Araujo Oliveira Filho <[email protected]> |
| 8 |
* |
| 9 |
* This program is free software; you can redistribute it and/or modify |
| 10 |
* it under the terms of the GNU General Public License as published by |
| 11 |
* the Free Software Foundation; version 3. |
| 12 |
* |
| 13 |
* This program is distributed in the hope that it will be useful, |
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
* GNU General Public License for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU General Public License |
| 19 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 20 |
*/ |
| 21 |
import QtQuick 2.4 |
| 22 |
import QtMultimedia 5.0 |
| 23 |
import Lomiri.Components 1.3 |
| 24 |
import Lomiri.Components.Popups 1.0 as Popups |
| 25 |
import "../common" |
| 26 |
import "../sdk" |
| 27 |
|
| 28 |
AbstractPlayer { |
| 29 |
id: player |
| 30 |
|
| 31 |
property variant info |
| 32 |
property int pressCount: 0 |
| 33 |
property bool wasPlaying: false |
| 34 |
property bool rotating: false |
| 35 |
property alias controlsActive: _controls.active |
| 36 |
property bool componentLoaded: false |
| 37 |
readonly property int seekStep: 1000 |
| 38 |
readonly property bool isEmpty: source == "" |
| 39 |
property bool isAudio: mpApplication.isAudio(source) |
| 40 |
property var errorDialog: null |
| 41 |
|
| 42 |
signal timeClicked |
| 43 |
signal playEmptyFile |
| 44 |
|
| 45 |
objectName: "player" |
| 46 |
info: VideoInfo { |
| 47 |
uri: source |
| 48 |
} |
| 49 |
|
| 50 |
function playUri(uri) { |
| 51 |
source = uri |
| 52 |
mpApplication.playUri = uri |
| 53 |
var storedPosition = mpApplication.getStoredPosition(); |
| 54 |
if (componentLoaded) { |
| 55 |
if(storedPosition > 0) { |
| 56 |
PopupUtils.open(dialogPlayerHaveStoredPosition, null) |
| 57 |
} else { |
| 58 |
play() |
| 59 |
} |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
Component.onCompleted: { |
| 64 |
componentLoaded = true |
| 65 |
} |
| 66 |
|
| 67 |
function edgeEvent(event) { |
| 68 |
event.accepted = true |
| 69 |
} |
| 70 |
|
| 71 |
function playPause() { |
| 72 |
if (["paused", "playing"].indexOf(player.state) != -1) { |
| 73 |
player.togglePause(); |
| 74 |
} else { |
| 75 |
player.play(); |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
//TODO: blur effect does not work fine without multiple thread rendering |
| 80 |
// ControlsMask { |
| 81 |
// anchors.fill: parent |
| 82 |
// controls: _controls |
| 83 |
// videoOutput: player.videoOutput |
| 84 |
// } |
| 85 |
|
| 86 |
|
| 87 |
ToolBar { |
| 88 |
id: _controls |
| 89 |
|
| 90 |
objectName: "toolbar" |
| 91 |
anchors { |
| 92 |
left: parent.left |
| 93 |
right: parent.right |
| 94 |
bottom: parent.bottom |
| 95 |
} |
| 96 |
|
| 97 |
height: _controlsContents.height |
| 98 |
active: player.isAudio |
| 99 |
Controls { |
| 100 |
id: _controlsContents |
| 101 |
|
| 102 |
property bool wasPausedBeforeSeek: false |
| 103 |
property bool wasVisibleBeforeSeek: false |
| 104 |
property int seekPosition: 0 |
| 105 |
|
| 106 |
function aboutToSeek() |
| 107 |
{ |
| 108 |
wasPausedBeforeSeek = (state == "paused") |
| 109 |
wasVisibleBeforeSeek = _controls.active && !_controls.aboutToDismiss |
| 110 |
_controls.abortDismiss() |
| 111 |
player.pause() |
| 112 |
_controls.active = true |
| 113 |
_controlsContents.seekPosition = video.position |
| 114 |
} |
| 115 |
|
| 116 |
function seekDone() |
| 117 |
{ |
| 118 |
_controlsContents.finalSeekPosition = _controlsContents.seekPosition |
| 119 |
// Only automatically resume playing after a seek that is not to the |
| 120 |
// end of stream (i.e. position == duration) |
| 121 |
if (player.status !== MediaPlayer.EndOfMedia && !_controlsContents.wasPausedBeforeSeek) { |
| 122 |
player.play() |
| 123 |
} |
| 124 |
|
| 125 |
if (!_controlsContents.wasVisibleBeforeSeek) { |
| 126 |
_controls.dismiss() |
| 127 |
} |
| 128 |
|
| 129 |
_controlsContents.seekPosition = -1 |
| 130 |
_controlsContents.wasPausedBeforeSeek = false |
| 131 |
_controlsContents.wasVisibleBeforeSeek = false |
| 132 |
} |
| 133 |
|
| 134 |
function seek(time) |
| 135 |
{ |
| 136 |
//keep trak of last seek position in case of the last seek does not complete in time |
| 137 |
//sometimes the seek is too fast and we can not rely on the video position to calculate |
| 138 |
//the next seek position. |
| 139 |
_controlsContents.seekPosition = time |
| 140 |
player.video.seek(time) |
| 141 |
} |
| 142 |
|
| 143 |
openFileEnabled: true |
| 144 |
objectName: "controls" |
| 145 |
state: player.state |
| 146 |
video: player.video |
| 147 |
anchors { |
| 148 |
left: parent.left |
| 149 |
right: parent.right |
| 150 |
bottom: parent.bottom |
| 151 |
} |
| 152 |
|
| 153 |
maximumHeight: units.gu(27) |
| 154 |
sceneSelectorHeight: units.gu(18) |
| 155 |
playerStatus: player.status |
| 156 |
|
| 157 |
onPlaybackClicked: { |
| 158 |
if (player.source == "") { |
| 159 |
player.playEmptyFile() |
| 160 |
} else { |
| 161 |
player.playPause() |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
onFullscreenClicked: mpApplication.toggleFullscreen() |
| 166 |
onOpenFileClicked: player.playEmptyFile() |
| 167 |
onStartSeek: aboutToSeek() |
| 168 |
onEndSeek: seekDone() |
| 169 |
onSeekRequested: seek(time) |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
MouseArea { |
| 174 |
id: _mouseArea |
| 175 |
|
| 176 |
objectName: "videoMouseArea" |
| 177 |
anchors { |
| 178 |
left: parent.left |
| 179 |
right: parent.right |
| 180 |
top: parent.top |
| 181 |
bottom: _controls.top |
| 182 |
} |
| 183 |
onClicked: { |
| 184 |
if (!player.isAudio) { |
| 185 |
_controls.active = !_controls.active |
| 186 |
} else { |
| 187 |
player.playPause() |
| 188 |
} |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
Item { |
| 193 |
id: emptyState |
| 194 |
|
| 195 |
anchors.fill: parent |
| 196 |
visible: false |
| 197 |
Icon { |
| 198 |
id: emptyStateIcon |
| 199 |
|
| 200 |
source: "image://theme/document-open" |
| 201 |
color: "white" |
| 202 |
anchors.centerIn: parent |
| 203 |
width: units.gu(4) |
| 204 |
} |
| 205 |
Label { |
| 206 |
text: i18n.tr("Please choose a file to open") |
| 207 |
color: "white" |
| 208 |
textSize: Label.Large |
| 209 |
anchors { |
| 210 |
horizontalCenter: parent.horizontalCenter |
| 211 |
top: emptyStateIcon.bottom |
| 212 |
topMargin: units.gu(2) |
| 213 |
} |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
Item { |
| 218 |
id: audioFileState |
| 219 |
|
| 220 |
anchors.fill: parent |
| 221 |
visible: player.isAudio |
| 222 |
Icon { |
| 223 |
id: audioFileStateIcon |
| 224 |
|
| 225 |
source: "image://theme/stock_music" |
| 226 |
color: "white" |
| 227 |
anchors.centerIn: parent |
| 228 |
width: units.gu(12) |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
state: player.isEmpty ? "empty" : "" |
| 233 |
states: [ |
| 234 |
State { |
| 235 |
name: "empty" |
| 236 |
PropertyChanges { |
| 237 |
target: _controls |
| 238 |
active: true |
| 239 |
} |
| 240 |
PropertyChanges { |
| 241 |
target: emptyState |
| 242 |
visible: true |
| 243 |
} |
| 244 |
PropertyChanges { |
| 245 |
target: _mouseArea |
| 246 |
enabled: false |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
] |
| 251 |
|
| 252 |
Keys.onReleased: |
| 253 |
{ |
| 254 |
if (event.isAutoRepeat) { |
| 255 |
return |
| 256 |
} |
| 257 |
|
| 258 |
switch(event.key) { |
| 259 |
case Qt.Key_Right: |
| 260 |
case Qt.Key_Left: |
| 261 |
_controlsContents.seekDone() |
| 262 |
break; |
| 263 |
default: |
| 264 |
break |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
Keys.onPressed: { |
| 269 |
switch(event.key) { |
| 270 |
case Qt.Key_Space: |
| 271 |
player.playPause() |
| 272 |
break; |
| 273 |
case Qt.Key_Right: |
| 274 |
case Qt.Key_Left: |
| 275 |
{ |
| 276 |
if (!event.isAutoRepeat) { |
| 277 |
_controlsContents.aboutToSeek() |
| 278 |
} |
| 279 |
// wait controls be fully visbile |
| 280 |
if (!_controls.fullVisible) |
| 281 |
return |
| 282 |
|
| 283 |
var nextPos = _controlsContents.seekPosition >= 0 ? |
| 284 |
_controlsContents.seekPosition : 0 |
| 285 |
|
| 286 |
if (event.key === Qt.Key_Right) { |
| 287 |
var maxPos = (video ? video.duration : 0) |
| 288 |
nextPos += player.seekStep |
| 289 |
if (nextPos > maxPos) { |
| 290 |
nextPos = -1; |
| 291 |
} |
| 292 |
} else { |
| 293 |
nextPos -= player.seekStep |
| 294 |
if (nextPos < 0) { |
| 295 |
nextPos = -1 |
| 296 |
} |
| 297 |
} |
| 298 |
|
| 299 |
if (nextPos !== -1) { |
| 300 |
_controlsContents.seek(nextPos) |
| 301 |
} |
| 302 |
break; |
| 303 |
} |
| 304 |
case Qt.Key_F11: |
| 305 |
mpApplication.toggleFullscreen() |
| 306 |
break; |
| 307 |
case Qt.Key_Escape: |
| 308 |
mpApplication.leaveFullScreen() |
| 309 |
break; |
| 310 |
default: |
| 311 |
break; |
| 312 |
} |
| 313 |
} |
| 314 |
|
| 315 |
Component { |
| 316 |
id: dialogPlayerError |
| 317 |
|
| 318 |
Popups.Dialog { |
| 319 |
id: dialogue |
| 320 |
objectName: "playError" |
| 321 |
|
| 322 |
property string errorString: "" |
| 323 |
|
| 324 |
title: i18n.tr("Error playing video") |
| 325 |
text: errorString |
| 326 |
|
| 327 |
Button { |
| 328 |
text: i18n.tr("Close") |
| 329 |
onClicked: { |
| 330 |
PopupUtils.close(dialogue) |
| 331 |
if (player.state != MediaPlayer.PlayingState) |
| 332 |
{ |
| 333 |
console.debug("Warning: Quitting app due to fatal playback error.") |
| 334 |
Qt.quit() |
| 335 |
} |
| 336 |
} |
| 337 |
} |
| 338 |
|
| 339 |
Component.onDestruction: player.errorDialog = null |
| 340 |
} |
| 341 |
} |
| 342 |
|
| 343 |
Component{ |
| 344 |
id: dialogPlayerHaveStoredPosition |
| 345 |
|
| 346 |
Popups.Dialog { |
| 347 |
id: dialogPlayerHaveStoredPositionDialog |
| 348 |
objectName: "playError" |
| 349 |
|
| 350 |
title: i18n.tr("Continue playback?") |
| 351 |
text: i18n.tr("Or start over") |
| 352 |
|
| 353 |
Row { |
| 354 |
id: row |
| 355 |
width: parent.width |
| 356 |
spacing: units.gu(1) |
| 357 |
Button { |
| 358 |
width: parent.width/2 - row.spacing/2 |
| 359 |
text: i18n.tr("From start") |
| 360 |
onClicked: { |
| 361 |
mpApplication.storePosition(-1); |
| 362 |
player.play() |
| 363 |
PopupUtils.close(dialogPlayerHaveStoredPositionDialog) |
| 364 |
} |
| 365 |
} |
| 366 |
Button { |
| 367 |
width: parent.width/2 - row.spacing/2 |
| 368 |
text: i18n.tr("Continue") |
| 369 |
color: LomiriColors.green |
| 370 |
onClicked: { |
| 371 |
player.seek(mpApplication.getStoredPosition()) |
| 372 |
player.play() |
| 373 |
PopupUtils.close(dialogPlayerHaveStoredPositionDialog) |
| 374 |
} |
| 375 |
} |
| 376 |
} |
| 377 |
} |
| 378 |
} |
| 379 |
|
| 380 |
onError: { |
| 381 |
if (player.errorDialog !== null) |
| 382 |
|
| 383 |
return |
| 384 |
|
| 385 |
player.errorDialog = PopupUtils.open(dialogPlayerError, null) |
| 386 |
switch(errorCode) { |
| 387 |
case 1: |
| 388 |
player.errorDialog.errorString = i18n.tr("Fail to open the source video.") |
| 389 |
break; |
| 390 |
case 2: |
| 391 |
player.errorDialog.errorString = i18n.tr("Video format not supported.") |
| 392 |
break; |
| 393 |
case 3: |
| 394 |
player.errorDialog.errorString = i18n.tr("A network error occurred.") |
| 395 |
break; |
| 396 |
case 4: |
| 397 |
player.errorDialog.errorString = i18n.tr("You don't have the appropriate permissions to play a media resource.") |
| 398 |
break; |
| 399 |
case 5: |
| 400 |
player.errorDialog.errorString = i18n.tr("Fail to connect with playback backend.") |
| 401 |
break; |
| 402 |
} |
| 403 |
} |
| 404 |
|
| 405 |
onPositionChanged: mpApplication.storePosition(position >= duration ? -1 : position) |
| 406 |
} |