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.setUrl(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 if (media.endsWith(".wav"))
114 return QUrl(media);
115
116 using namespace Qt::StringLiterals;
117
118 QAudioOutput audioOutput;
119 QVideoSink videoOutput;
120 QMediaPlayer player;
121 player.setAudioOutput(&audioOutput);
122 player.setVideoOutput(&videoOutput);
123
124 player.setSource(QUrl{media});
125 player.play();
126
127 const auto waitingFinished = QTest::qWaitFor([&]() {
128 if (player.error() != QMediaPlayer::NoError)
129 return true;
130
131 switch (player.mediaStatus()) {
132 case QMediaPlayer::BufferingMedia:
133 case QMediaPlayer::BufferedMedia:
134 case QMediaPlayer::EndOfMedia:
135 case QMediaPlayer::InvalidMedia:
136 return true;
137
138 default:
139 return false;
140 }
141 });
142
143 auto enumValueToString = [](auto enumValue) {
144 return QString(QMetaEnum::fromType<decltype(enumValue)>().valueToKey(enumValue));
145 };
146
147 if (!waitingFinished)
148 return q23::unexpected{ "The media got stuck in the status "_L1
149 + enumValueToString(player.mediaStatus()) };
150
151 if (player.mediaStatus() == QMediaPlayer::InvalidMedia)
152 return q23::unexpected{ "Unable to load the media. Error ["_L1
153 + enumValueToString(player.error()) + " "_L1
154 + player.errorString() + "]"_L1 };
155
156 if (player.error() != QMediaPlayer::NoError)
157 return q23::unexpected{ "Unable to start playing the media, codecs issues. Error ["_L1
158 + enumValueToString(player.error()) + " "_L1
159 + player.errorString() + "]"_L1 };
160
161 return QUrl(media);
162 }
163
164 QString nativeFileName(const QString &media)
165 {
166#ifdef Q_OS_ANDROID
167 auto it = m_nativeFiles.find(media);
168 if (it != m_nativeFiles.end())
169 return it->second->fileName();
170
171 QFile file(media);
172 if (file.open(QIODevice::ReadOnly)) {
173 m_nativeFiles.insert({ media, std::unique_ptr<QTemporaryFile>(QTemporaryFile::createNativeFile(file))});
174 return m_nativeFiles[media]->fileName();
175 }
176 qWarning() << "Failed to create temporary file";
177#endif // Q_OS_ANDROID
178
179 return media;
180 }
181
182private:
183#ifdef Q_OS_ANDROID
185#endif
186 std::unordered_map<QString, QString> m_mediaToErrors;
187 int m_failedSelectionsCount = 0;
188};
189
190QT_END_NAMESPACE
191
192Q_DECLARE_METATYPE(MaybeUrl)
193
194#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.
Combined button and popup list for selecting options.