forked from wang-bin/QtAV
-
Notifications
You must be signed in to change notification settings - Fork 0
/
QuickSubtitle.cpp
176 lines (154 loc) · 4.84 KB
/
QuickSubtitle.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
/******************************************************************************
QtAV: Multimedia framework based on Qt and FFmpeg
Copyright (C) 2012-2016 Wang Bin <[email protected]>
* This file is part of QtAV (from 2014)
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 "QmlAV/QuickSubtitle.h"
#include "QmlAV/QmlAVPlayer.h"
#include <QtAV/Filter.h>
#include <QtAV/VideoFrame.h>
#include <QtAV/private/PlayerSubtitle.h>
using namespace QtAV;
class QuickSubtitle::Filter : public QtAV::VideoFilter
{
public:
Filter(Subtitle *sub, QuickSubtitle *parent) :
VideoFilter(parent)
, m_empty_image(false)
, m_sub(sub)
, m_subject(parent)
{}
protected:
virtual void process(Statistics* statistics, VideoFrame* frame) {
Q_UNUSED(statistics);
if (!m_sub)
return;
if (frame && frame->timestamp() > 0.0) {
m_sub->setTimestamp(frame->timestamp()); //TODO: set to current display video frame's timestamp
QRect r;
QImage image(m_sub->getImage(frame->width(), frame->height(), &r));
if (image.isNull()) {
if (m_empty_image)
return;
m_empty_image = true;
} else {
m_empty_image = false;
}
m_subject->notifyObservers(image, r, frame->width(), frame->height());
}
}
private:
bool m_empty_image;
Subtitle *m_sub;
QuickSubtitle *m_subject;
};
QuickSubtitle::QuickSubtitle(QObject *parent) :
QObject(parent)
, SubtitleAPIProxy(this)
, m_enable(true)
, m_player(0)
, m_player_sub(new PlayerSubtitle(this))
, m_filter(0)
{
QmlAVPlayer *p = qobject_cast<QmlAVPlayer*>(parent);
if (p)
setPlayer(p);
m_filter = new Filter(m_player_sub->subtitle(), this);
setSubtitle(m_player_sub->subtitle()); //for proxy
connect(this, SIGNAL(enabledChanged(bool)), m_player_sub, SLOT(onEnabledChanged(bool))); //////
connect(m_player_sub, SIGNAL(autoLoadChanged(bool)), this, SIGNAL(autoLoadChanged()));
connect(m_player_sub, SIGNAL(fileChanged()), this, SIGNAL(fileChanged()));
}
QString QuickSubtitle::getText() const
{
return m_player_sub->subtitle()->getText();
}
void QuickSubtitle::addObserver(QuickSubtitleObserver *ob)
{
if (!m_observers.contains(ob)) {
QMutexLocker lock(&m_mutex);
Q_UNUSED(lock);
m_observers.append(ob);
}
}
void QuickSubtitle::removeObserver(QuickSubtitleObserver *ob)
{
QMutexLocker lock(&m_mutex);
Q_UNUSED(lock);
m_observers.removeAll(ob);
}
void QuickSubtitle::notifyObservers(const QImage &image, const QRect &r, int width, int height, QuickSubtitleObserver *ob)
{
if (ob) {
ob->update(image, r, width, height);
return;
}
QMutexLocker lock(&m_mutex);
Q_UNUSED(lock);
if (m_observers.isEmpty())
return;
foreach (QuickSubtitleObserver* o, m_observers) {
o->update(image, r, width, height);
}
}
void QuickSubtitle::setPlayer(QObject *player)
{
QmlAVPlayer *p = qobject_cast<QmlAVPlayer*>(player);
if (m_player == p)
return;
if (m_player)
m_filter->uninstall();
m_player = p;
if (!p)
return;
m_filter->installTo(p->player());
// ~Filter() can not call uninstall() unless player is still exists
// TODO: check AVPlayer null?
m_player_sub->setPlayer(p->player());
}
QObject* QuickSubtitle::player()
{
return m_player;
}
void QuickSubtitle::setEnabled(bool value)
{
if (m_enable == value)
return;
m_enable = value;
Q_EMIT enabledChanged(value);
m_filter->setEnabled(m_enable);
if (!m_enable) { //display nothing
notifyObservers(QImage(), QRect(), 0, 0);
}
}
bool QuickSubtitle::isEnabled() const
{
return m_enable;
}
void QuickSubtitle::setFile(const QString &file)
{
m_player_sub->setFile(file);
}
QString QuickSubtitle::file() const
{
return m_player_sub->file();
}
void QuickSubtitle::setAutoLoad(bool value)
{
m_player_sub->setAutoLoad(value);
}
bool QuickSubtitle::autoLoad() const
{
return m_player_sub->autoLoad();
}