forked from wang-bin/QtAV
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AVOutput.cpp
189 lines (162 loc) · 4.77 KB
/
AVOutput.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
/******************************************************************************
QtAV: Multimedia framework based on Qt and FFmpeg
Copyright (C) 2012-2016 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/AVOutput.h"
#include "QtAV/private/AVOutput_p.h"
#include "QtAV/Filter.h"
#include "QtAV/FilterContext.h"
#include "filter/FilterManager.h"
#include "output/OutputSet.h"
#include "utils/Logger.h"
namespace QtAV {
AVOutputPrivate::~AVOutputPrivate() {
cond.wakeAll(); //WHY: failed to wake up
}
AVOutput::AVOutput()
{
}
AVOutput::AVOutput(AVOutputPrivate &d)
:DPTR_INIT(&d)
{
}
AVOutput::~AVOutput()
{
pause(false); //Does not work. cond may still waiting when destroyed
detach();
DPTR_D(AVOutput);
if (d.filter_context) {
delete d.filter_context;
d.filter_context = 0;
}
foreach (Filter *f, d.pending_uninstall_filters) {
d.filters.removeAll(f);
}
QList<Filter*>::iterator it = d.filters.begin();
while (it != d.filters.end()) {
// if not uninstall here, if AVOutput is also an QObject (for example, widget based renderers)
// then qobject children filters will be deleted when parent is destroying and call FilterManager::uninstallFilter()
// and FilterManager::instance().unregisterFilter(filter, this) too late that AVOutput is almost be destroyed
uninstallFilter(*it);
// 1 filter has 1 target. so if has output filter in manager, the output is this output
/*FilterManager::instance().hasOutputFilter(*it) && */
if ((*it)->isOwnedByTarget() && !(*it)->parent())
delete *it;
++it;
}
d.filters.clear();
}
bool AVOutput::isAvailable() const
{
return d_func().available;
}
void AVOutput::pause(bool p)
{
DPTR_D(AVOutput);
if (d.paused == p)
return;
d.paused = p;
}
bool AVOutput::isPaused() const
{
return d_func().paused;
}
//TODO: how to call this automatically before write()?
bool AVOutput::tryPause()
{
DPTR_D(AVOutput);
if (!d.paused)
return false;
QMutexLocker lock(&d.mutex);
Q_UNUSED(lock);
d.cond.wait(&d.mutex);
return true;
}
void AVOutput::addOutputSet(OutputSet *set)
{
d_func().output_sets.append(set);
}
void AVOutput::removeOutputSet(OutputSet *set)
{
d_func().output_sets.removeAll(set);
}
void AVOutput::attach(OutputSet *set)
{
set->addOutput(this);
}
void AVOutput::detach(OutputSet *set)
{
DPTR_D(AVOutput);
if (set) {
set->removeOutput(this);
return;
}
foreach(OutputSet *set, d.output_sets) {
set->removeOutput(this);
}
}
QList<Filter*>& AVOutput::filters()
{
return d_func().filters;
}
void AVOutput::setStatistics(Statistics *statistics)
{
DPTR_D(AVOutput);
d.statistics = statistics;
}
bool AVOutput::installFilter(Filter *filter, int index)
{
return onInstallFilter(filter, index);
}
bool AVOutput::onInstallFilter(Filter *filter, int index)
{
if (!FilterManager::instance().registerFilter(filter, this, index)) {
return false;
}
DPTR_D(AVOutput);
d.filters = FilterManager::instance().outputFilters(this);
return true;
}
/*
* FIXME: how to ensure thread safe using mutex etc? for a video filter, both are in main thread.
* an audio filter on audio output may be in audio thread
*/
bool AVOutput::uninstallFilter(Filter *filter)
{
return onUninstallFilter(filter);
}
bool AVOutput::onUninstallFilter(Filter *filter)
{
DPTR_D(AVOutput);
FilterManager::instance().unregisterFilter(filter, this);
d.pending_uninstall_filters.push_back(filter);
return true;
}
void AVOutput::hanlePendingTasks()
{
onHanlePendingTasks();
}
bool AVOutput::onHanlePendingTasks()
{
DPTR_D(AVOutput);
if (d.pending_uninstall_filters.isEmpty())
return false;
foreach (Filter *filter, d.pending_uninstall_filters) {
d.filters.removeAll(filter);
}
d.pending_uninstall_filters.clear();
return true;
}
} //namespace QtAV