Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
mediafileselector_p.h
Go to the documentation of this file.
1// Copyright (C) 2017 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4#ifndef MEDIAFILESELECTOR_H
5#define MEDIAFILESELECTOR_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists purely as an
12// implementation detail. This header file may change from version to
13// version without notice, or even be removed.
14//
15// We mean it.
16//
17
18#include <QtCore/qurl.h>
19#include <QtCore/private/qexpected_p.h>
20#include <qmediaplayer.h>
21#include <qaudiooutput.h>
22#include <qvideosink.h>
23#include <qsignalspy.h>
24#include <qfileinfo.h>
25#include <qtest.h>
26#include <private/qmultimediautils_p.h>
27#ifdef Q_OS_ANDROID
28#include <QtCore/qtemporaryfile.h>
29#endif
30
31#include <unordered_map>
32
34
35using MaybeUrl = q23::expected<QUrl, QString>;
36
37#define CHECK_SELECTED_URL(maybeUrl)
38 if (!maybeUrl)
39 QSKIP((QLatin1String("\nUnable to select none of the media candidates:\n") + maybeUrl.error())
40 .toLocal8Bit()
41 .data())
42
44{
45public:
46 int failedSelectionsCount() const { return m_failedSelectionsCount; }
47
49 {
50 QStringList failedMedias;
51 for (const auto &mediaToError : m_mediaToErrors)
52 if (!mediaToError.second.isEmpty())
53 failedMedias.emplace_back(mediaToError.first);
54
55 failedMedias.sort();
56 return dumpErrors(failedMedias);
57 }
58
59 template <typename... Media>
60 MaybeUrl select(Media... media)
61 {
62 return select({ std::move(nativeFileName(media))... });
63 }
64
65 MaybeUrl select(const QStringList &candidates)
66 {
67 QUrl foundUrl;
68 for (const auto &media : candidates) {
69 auto emplaceRes = m_mediaToErrors.try_emplace(media, QString());
70 if (emplaceRes.second) {
71 auto maybeUrl = selectMediaFile(media);
72 if (!maybeUrl) {
73 Q_ASSERT(!maybeUrl.error().isEmpty());
74 emplaceRes.first->second = maybeUrl.error();
75 }
76 }
77
78 if (foundUrl.isEmpty() && emplaceRes.first->second.isEmpty())
79 foundUrl = media;
80 }
81
82 if (!foundUrl.isEmpty())
83 return foundUrl;
84
85 ++m_failedSelectionsCount;
86 return q23::unexpected{ dumpErrors(candidates) };
87 }
88
89private:
90 QString dumpErrors(const QStringList &medias) const
91 {
92 using namespace Qt::StringLiterals;
93 QString result;
94
95 for (const auto &media : medias) {
96 auto it = m_mediaToErrors.find(media);
97 if (it != m_mediaToErrors.end() && !it->second.isEmpty())
98 result.append("\t"_L1)
99 .append(it->first)
100 .append(": "_L1)
101 .append(it->second)
102 .append("\n"_L1);
103 }
104
105 return result;
106 }
107
108 static MaybeUrl selectMediaFile(QString media)
109 {
110 if (qEnvironmentVariableIsSet("QTEST_SKIP_MEDIA_VALIDATION"))
111 return QUrl(media);
112
113 using namespace Qt::StringLiterals;
114
115 QAudioOutput audioOutput;
116 QVideoSink videoOutput;
117 QMediaPlayer player;
118 player.setAudioOutput(&audioOutput);
119 player.setVideoOutput(&videoOutput);
120
121 player.setSource(media);
122 player.play();
123
124 const auto waitingFinished = QTest::qWaitFor([&]() {
125 if (player.error() != QMediaPlayer::NoError)
126 return true;
127
128 switch (player.mediaStatus()) {
129 case QMediaPlayer::BufferingMedia:
130 case QMediaPlayer::BufferedMedia:
131 case QMediaPlayer::EndOfMedia:
132 case QMediaPlayer::InvalidMedia:
133 return true;
134
135 default:
136 return false;
137 }
138 });
139
140 auto enumValueToString = [](auto enumValue) {
141 return QString(QMetaEnum::fromType<decltype(enumValue)>().valueToKey(enumValue));
142 };
143
144 if (!waitingFinished)
145 return q23::unexpected{ "The media got stuck in the status "_L1
146 + enumValueToString(player.mediaStatus()) };
147
148 if (player.mediaStatus() == QMediaPlayer::InvalidMedia)
149 return q23::unexpected{ "Unable to load the media. Error ["_L1
150 + enumValueToString(player.error()) + " "_L1
151 + player.errorString() + "]"_L1 };
152
153 if (player.error() != QMediaPlayer::NoError)
154 return q23::unexpected{ "Unable to start playing the media, codecs issues. Error ["_L1
155 + enumValueToString(player.error()) + " "_L1
156 + player.errorString() + "]"_L1 };
157
158 return QUrl(media);
159 }
160
161 QString nativeFileName(const QString &media)
162 {
163#ifdef Q_OS_ANDROID
164 auto it = m_nativeFiles.find(media);
165 if (it != m_nativeFiles.end())
166 return it->second->fileName();
167
168 QFile file(media);
169 if (file.open(QIODevice::ReadOnly)) {
170 m_nativeFiles.insert({ media, std::unique_ptr<QTemporaryFile>(QTemporaryFile::createNativeFile(file))});
171 return m_nativeFiles[media]->fileName();
172 }
173 qWarning() << "Failed to create temporary file";
174#endif // Q_OS_ANDROID
175
176 return media;
177 }
178
179private:
180#ifdef Q_OS_ANDROID
182#endif
183 std::unordered_map<QString, QString> m_mediaToErrors;
184 int m_failedSelectionsCount = 0;
185};
186
187QT_END_NAMESPACE
188
189Q_DECLARE_METATYPE(MaybeUrl)
190
191#endif
QString dumpErrors() const
int failedSelectionsCount() const
MaybeUrl select(const QStringList &candidates)
MaybeUrl select(Media... media)
The QMediaPlayer class allows the playing of a media files.