1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the Qt Mobility Components.
7**
8** $QT_BEGIN_LICENSE:BSD$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** BSD License Usage
18** Alternatively, you may use this file under the terms of the BSD license
19** as follows:
20**
21** "Redistribution and use in source and binary forms, with or without
22** modification, are permitted provided that the following conditions are
23** met:
24** * Redistributions of source code must retain the above copyright
25** notice, this list of conditions and the following disclaimer.
26** * Redistributions in binary form must reproduce the above copyright
27** notice, this list of conditions and the following disclaimer in
28** the documentation and/or other materials provided with the
29** distribution.
30** * Neither the name of The Qt Company Ltd nor the names of its
31** contributors may be used to endorse or promote products derived
32** from this software without specific prior written permission.
33**
34**
35** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46**
47** $QT_END_LICENSE$
48**
49****************************************************************************/
50
51#include <QtCore/QStandardPaths>
52#include <QtCore/QString>
53#include <QtCore/QStringList>
54#include <QtQml/QQmlContext>
55#include <QtQml/QQmlEngine>
56#include <QtGui/QGuiApplication>
57#include <QtQuick/QQuickItem>
58#include <QtQuick/QQuickView>
59#include "trace.h"
60
61#ifdef PERFORMANCEMONITOR_SUPPORT
62#include "performancemonitordeclarative.h"
63#endif
64
65#ifdef REQUEST_PERMISSIONS_ON_ANDROID
66#include <QtAndroid>
67
68bool requestStoragePermission() {
69 using namespace QtAndroid;
70
71 QString permission = QStringLiteral("android.permission.WRITE_EXTERNAL_STORAGE");
72 const QHash<QString, PermissionResult> results = requestPermissionsSync(QStringList({permission}));
73 if (!results.contains(permission) || results[permission] == PermissionResult::Denied) {
74 qWarning() << "Couldn't get permission: " << permission;
75 return false;
76 }
77
78 return true;
79}
80#endif
81
82static const QString DefaultFileName1 = "";
83static const QString DefaultFileName2 = "";
84
85int main(int argc, char *argv[])
86{
87 QGuiApplication app(argc, argv);
88
89#ifdef PERFORMANCEMONITOR_SUPPORT
90 PerformanceMonitor::qmlRegisterTypes();
91#endif
92#ifdef REQUEST_PERMISSIONS_ON_ANDROID
93 if (!requestStoragePermission())
94 return -1;
95#endif
96
97 QString source1, source2;
98 qreal volume = 0.5;
99 QStringList args = app.arguments();
100#ifdef PERFORMANCEMONITOR_SUPPORT
101 PerformanceMonitor::State performanceMonitorState;
102#endif
103 bool sourceIsUrl = false;
104 for (int i = 1; i < args.size(); ++i) {
105 const QByteArray arg = args.at(i).toUtf8();
106 if (arg.startsWith(c: '-')) {
107 if ("-volume" == arg) {
108 if (i+1 < args.count())
109 volume = 0.01 * args.at(i: ++i).toInt();
110 else
111 qtTrace() << "Option \"-volume\" takes a value";
112 }
113#ifdef PERFORMANCEMONITOR_SUPPORT
114 else if (performanceMonitorState.parseArgument(arg)) {
115 // Do nothing
116 }
117#endif
118 else if ("-url" == arg) {
119 sourceIsUrl = true;
120 } else {
121 qtTrace() << "Option" << arg << "ignored";
122 }
123 } else {
124 if (source1.isEmpty())
125 source1 = arg;
126 else if (source2.isEmpty())
127 source2 = arg;
128 else
129 qtTrace() << "Argument" << arg << "ignored";
130 }
131 }
132
133 QUrl url1, url2;
134 if (sourceIsUrl) {
135 url1 = source1;
136 url2 = source2;
137 } else {
138 if (!source1.isEmpty())
139 url1 = QUrl::fromLocalFile(localfile: source1);
140 if (!source2.isEmpty())
141 url2 = QUrl::fromLocalFile(localfile: source2);
142 }
143
144 QQuickView viewer;
145 viewer.setSource(QUrl("qrc:///qml/qmlvideo/main.qml"));
146 QObject::connect(sender: viewer.engine(), signal: &QQmlEngine::quit, receiver: &viewer, slot: &QQuickView::close);
147
148 QQuickItem *rootObject = viewer.rootObject();
149 rootObject->setProperty(name: "source1", value: url1);
150 rootObject->setProperty(name: "source2", value: url2);
151 rootObject->setProperty(name: "volume", value: volume);
152
153#ifdef PERFORMANCEMONITOR_SUPPORT
154 if (performanceMonitorState.valid) {
155 rootObject->setProperty(name: "perfMonitorsLogging", value: performanceMonitorState.logging);
156 rootObject->setProperty(name: "perfMonitorsVisible", value: performanceMonitorState.visible);
157 }
158 QObject::connect(sender: &viewer, SIGNAL(afterRendering()),
159 receiver: rootObject, SLOT(qmlFramePainted()));
160#endif
161
162 const QStringList moviesLocation = QStandardPaths::standardLocations(type: QStandardPaths::MoviesLocation);
163 const QUrl videoPath =
164 QUrl::fromLocalFile(localfile: moviesLocation.isEmpty() ?
165 app.applicationDirPath() :
166 moviesLocation.front());
167 viewer.rootContext()->setContextProperty("videoPath", videoPath);
168
169 QMetaObject::invokeMethod(obj: rootObject, member: "init");
170
171 viewer.setMinimumSize(QSize(640, 360));
172 viewer.show();
173
174 return app.exec();
175}
176
177

source code of qtmultimedia/examples/multimedia/video/qmlvideo/main.cpp