forked from wang-bin/QtAV
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayerSubtitle.cpp
297 lines (269 loc) · 9.6 KB
/
PlayerSubtitle.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/******************************************************************************
QtAV: Media play library based on Qt and FFmpeg
Copyright (C) 2014-2015 Wang Bin <[email protected]>
* This file is part of QtAV
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
******************************************************************************/
#include "QtAV/private/PlayerSubtitle.h"
#include <QtCore/QDir>
#include <QtCore/QFileInfo>
#include "QtAV/AVPlayer.h"
#include "QtAV/Subtitle.h"
#include "utils/Logger.h"
namespace QtAV {
extern QString getLocalPath(const QString& fullPath);
// /xx/oo/a.01.mov => /xx/oo/a.01. native dir separator => /
/*!
* \brief getSubtitleBasePath
* \param fullPath path of video or without extension
* \return absolute path without extension and file://
*/
static QString getSubtitleBasePath(const QString fullPath)
{
QString path(QDir::fromNativeSeparators(fullPath));
//path.remove(p->source().scheme() + "://");
// QString name = QFileInfo(path).completeBaseName();
// why QFileInfo(path).dir() starts with qml app dir?
QString name(path);
int lastSep = path.lastIndexOf(QLatin1Char('/'));
if (lastSep >= 0) {
name = name.mid(lastSep + 1);
path = path.left(lastSep + 1); // endsWidth "/"
}
int lastDot = name.lastIndexOf(QLatin1Char('.')); // not path.lastIndexof("."): xxx.oo/xx
if (lastDot > 0)
name = name.left(lastDot);
if (path.startsWith(QLatin1String("file:"))) // can skip convertion here. Subtitle class also convert the path
path = getLocalPath(path);
path.append(name);
return path;
}
PlayerSubtitle::PlayerSubtitle(QObject *parent)
: QObject(parent)
, m_auto(true)
, m_enabled(true)
, m_player(0)
, m_sub(new Subtitle(this))
{
}
Subtitle* PlayerSubtitle::subtitle()
{
return m_sub;
}
void PlayerSubtitle::setPlayer(AVPlayer *player)
{
if (m_player == player)
return;
if (m_player) {
disconnectSignals();
}
m_player = player;
if (!m_player)
return;
connectSignals();
}
void PlayerSubtitle::setFile(const QString &file)
{
if (m_file != file)
Q_EMIT fileChanged();
// always load
// file was set but now playing with fuzzy match. if file is set again to the same value, subtitle must load that file
m_file = file;
if (!m_enabled)
return;
m_sub->setFileName(file);
m_sub->setFuzzyMatch(false);
m_sub->loadAsync();
}
QString PlayerSubtitle::file() const
{
return m_file;
}
void PlayerSubtitle::setAutoLoad(bool value)
{
if (m_auto == value)
return;
m_auto = value;
emit autoLoadChanged(value);
}
bool PlayerSubtitle::autoLoad() const
{
return m_auto;
}
void PlayerSubtitle::onPlayerSourceChanged()
{
m_file = QString();
if (!m_auto || !m_enabled) {
return;
}
AVPlayer *p = qobject_cast<AVPlayer*>(sender());
if (!p)
return;
m_sub->setFileName(getSubtitleBasePath(p->file()));
m_sub->setFuzzyMatch(true);
m_sub->loadAsync();
}
void PlayerSubtitle::onPlayerPositionChanged()
{
AVPlayer *p = qobject_cast<AVPlayer*>(sender());
if (!p)
return;
m_sub->setTimestamp(qreal(p->position())/1000.0);
}
void PlayerSubtitle::onPlayerStart()
{
if (!m_enabled)
return;
if (!autoLoad()) {
if (m_file == m_sub->fileName())
return;
m_sub->setFileName(m_file);
m_sub->setFuzzyMatch(false);
if (m_file.isEmpty()) {
const int n = m_player->currentSubtitleStream();
if (n < 0 || m_tracks.isEmpty() || m_tracks.size() <= n) {
m_sub->processHeader(QByteArray(), QByteArray()); // reset
return;
}
QVariantMap track = m_tracks[n].toMap();
QByteArray codec(track.value(QStringLiteral("codec")).toByteArray());
QByteArray data(track.value(QStringLiteral("extra")).toByteArray());
m_sub->processHeader(codec, data);
} else {
m_sub->loadAsync();
}
return;
}
if (m_file != m_sub->fileName())
return;
if (!m_player)
return;
// autoLoad was false then reload then true then reload
// previous loaded is user selected subtitle
m_sub->setFileName(getSubtitleBasePath(m_player->file()));
m_sub->setFuzzyMatch(true);
m_sub->loadAsync();
return;
}
void PlayerSubtitle::onEnabledChanged(bool value)
{
m_enabled = value;
if (value) {
if (m_player) {
connectSignals();
}
if (autoLoad()) {
if (!m_player)
return;
m_sub->setFileName(getSubtitleBasePath(m_player->file()));
m_sub->setFuzzyMatch(true);
m_sub->loadAsync();
} else {
m_sub->setFileName(m_file);
m_sub->setFuzzyMatch(false);
m_sub->loadAsync();
}
} else {
if (m_player) {
disconnectSignals();
}
}
}
void PlayerSubtitle::tryReload()
{
tryReload(3);
}
void PlayerSubtitle::tryReloadInternalSub()
{
tryReload(1);
}
void PlayerSubtitle::tryReload(int flag)
{
if (!m_player)
return;
if (!m_player->isPlaying())
return;
const int kReloadInternal = 1;
const int kReloadExternal = 1<<1;
if (flag & kReloadExternal) {
if (!m_file.isEmpty() && m_enabled) { //engine changed
m_sub->processHeader(QByteArray(), QByteArray()); // reset
m_sub->loadAsync();
return;
}
}
if (!(flag & kReloadInternal)) { // if internal flag is set, try internal first, then fallback to external if internal is failed
if (!m_enabled)
return;
if (flag & kReloadExternal) {
m_sub->processHeader(QByteArray(), QByteArray()); // reset
m_sub->loadAsync();
}
return;
}
const int n = m_player->currentSubtitleStream();
if (n < 0 || m_tracks.isEmpty() || m_tracks.size() <= n) {
m_sub->processHeader(QByteArray(), QByteArray()); // reset, null processor
//try to fallback to external sub if an invalid internal sub track is set
if ((flag & kReloadExternal) && m_enabled)
m_sub->loadAsync();
return;
}
QVariantMap track = m_tracks[n].toMap();
QByteArray codec(track.value(QStringLiteral("codec")).toByteArray());
QByteArray data(track.value(QStringLiteral("extra")).toByteArray());
m_sub->processHeader(codec, data);
Packet pkt(m_current_pkt[n]);
if (pkt.isValid()) {
processInternalSubtitlePacket(n, pkt);
}
}
void PlayerSubtitle::updateInternalSubtitleTracks(const QVariantList &tracks)
{
m_tracks = tracks;
m_current_pkt.resize(tracks.size());
}
void PlayerSubtitle::processInternalSubtitlePacket(int track, const QtAV::Packet &packet)
{
m_sub->processLine(packet.data, packet.pts, packet.duration);
m_current_pkt[track] = packet;
}
void PlayerSubtitle::processInternalSubtitleHeader(const QByteArray& codec, const QByteArray &data)
{
m_sub->processHeader(codec, data);
}
void PlayerSubtitle::connectSignals()
{
connect(m_player, SIGNAL(sourceChanged()), this, SLOT(onPlayerSourceChanged()));
connect(m_player, SIGNAL(positionChanged(qint64)), this, SLOT(onPlayerPositionChanged()));
connect(m_player, SIGNAL(started()), this, SLOT(onPlayerStart()));
connect(m_player, SIGNAL(internalSubtitlePacketRead(int,QtAV::Packet)), this, SLOT(processInternalSubtitlePacket(int,QtAV::Packet)));
connect(m_player, SIGNAL(internalSubtitleHeaderRead(QByteArray,QByteArray)), this, SLOT(processInternalSubtitleHeader(QByteArray,QByteArray)));
connect(m_player, SIGNAL(internalSubtitleTracksChanged(QVariantList)), this, SLOT(updateInternalSubtitleTracks(QVariantList)));
// try to reload internal subtitle track. if failed and external subtitle is enabled, fallback to external
connect(m_player, SIGNAL(subtitleStreamChanged(int)), this, SLOT(tryReloadInternalSub()));
connect(m_sub, SIGNAL(codecChanged()), this, SLOT(tryReload()));
connect(m_sub, SIGNAL(enginesChanged()), this, SLOT(tryReload()));
}
void PlayerSubtitle::disconnectSignals()
{
disconnect(m_player, SIGNAL(sourceChanged()), this, SLOT(onPlayerSourceChanged()));
disconnect(m_player, SIGNAL(positionChanged(qint64)), this, SLOT(onPlayerPositionChanged()));
disconnect(m_player, SIGNAL(started()), this, SLOT(onPlayerStart()));
disconnect(m_player, SIGNAL(internalSubtitlePacketRead(int,QtAV::Packet)), this, SLOT(processInternalSubtitlePacket(int,QtAV::Packet)));
disconnect(m_player, SIGNAL(internalSubtitleHeaderRead(QByteArray,QByteArray)), this, SLOT(processInternalSubtitleHeader(QByteArray,QByteArray)));
disconnect(m_player, SIGNAL(internalSubtitleTracksChanged(QVariantList)), this, SLOT(updateInternalSubtitleTracks(QVariantList)));
disconnect(m_sub, SIGNAL(codecChanged()), this, SLOT(tryReload()));
disconnect(m_sub, SIGNAL(enginesChanged()), this, SLOT(tryReload()));
}
} //namespace QtAV