-
Notifications
You must be signed in to change notification settings - Fork 0
/
demuxthreadutil.cpp
234 lines (225 loc) · 5.15 KB
/
demuxthreadutil.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
#include "demuxthreadutil.h"
DemuxThreadUtil::DemuxThreadUtil()
{
}
DemuxThreadUtil::~DemuxThreadUtil()
{
isExit = true;
wait();
}
// 打开媒体文件,初始化视频显示组件
bool DemuxThreadUtil::Open(const char* url, VideoPlayWidget* widget)
{
if (url == nullptr || url[0] == '\0')
{
return false;
}
lock.lock();
if (!demuxUtil)
{
demuxUtil = new DemuxUtil();
}
if (!videoThreadUtil)
{
videoThreadUtil = new VideoThreadUtil();
}
if (!audioThreadUtil)
{
audioThreadUtil = new AudioThreadUtil();
}
bool result = demuxUtil->Open(url);
if (!result)
{
lock.unlock();
qDebug() << QString::fromLocal8Bit("解封装Open失败");
return false;
}
if (!videoThreadUtil->Open(demuxUtil->getVideoParameters(), widget, demuxUtil->getVideoParameters()->width, demuxUtil->getVideoParameters()->height))
{
result = false;
qDebug() << QString::fromLocal8Bit("打开视频解码器和处理线程失败");
}
if (!audioThreadUtil->Open(demuxUtil->getAudioParameters()))
{
result = false;
qDebug() << QString::fromLocal8Bit("打开音频解码器和处理线程失败");
}
lock.unlock();
// 用来计算播放进度
totalMilliseconds = demuxUtil->totalMilliseconds;
return result;
}
// 启动音视频线程
void DemuxThreadUtil::StartThreads()
{
lock.lock();
if (!demuxUtil)
{
demuxUtil = new DemuxUtil();
}
if (!videoThreadUtil)
{
videoThreadUtil = new VideoThreadUtil();
}
if (!audioThreadUtil)
{
audioThreadUtil = new AudioThreadUtil();
}
QThread::start();
if (videoThreadUtil)
{
videoThreadUtil->start();
}
if (audioThreadUtil)
{
audioThreadUtil->start();
}
lock.unlock();
}
// 负责推送AVPacket* pkt
void DemuxThreadUtil::run()
{
while (!isExit)
{
lock.lock();
if(isPause)
{
lock.unlock();
msleep(5);
continue;
}
if (!demuxUtil)
{
lock.unlock();
msleep(5);
continue;
}
if (videoThreadUtil && audioThreadUtil)
{
// 用来计算播放进度
pts = audioThreadUtil->standard_pts;
// 用来音画同步
videoThreadUtil->sync_pts = audioThreadUtil->standard_pts;
}
AVPacket* pkt = demuxUtil->Read();
if (!pkt)
{
lock.unlock();
msleep(5);
continue;
}
if (demuxUtil->IsAudio(pkt))
{
if (audioThreadUtil)
{
audioThreadUtil->Push(pkt);
}
}
else
{
if (videoThreadUtil)
{
videoThreadUtil->Push(pkt);
}
}
lock.unlock();
msleep(1);
}
}
// 清理
void DemuxThreadUtil::Clear()
{
lock.lock();
if (demuxUtil)
{
demuxUtil->Clear();
qDebug() << QString::fromLocal8Bit("demuxUtil清理结束");
}
if (videoThreadUtil)
{
videoThreadUtil->Clear();
qDebug() << QString::fromLocal8Bit("videoThreadUtil清理结束");
}
if (audioThreadUtil)
{
audioThreadUtil->Clear();
qDebug() << QString::fromLocal8Bit("audioThreadUtil清理结束");
}
lock.unlock();
}
// 关闭
void DemuxThreadUtil::Close()
{
isExit = true;
wait();
if (videoThreadUtil)
{
videoThreadUtil->Close();
}
if (audioThreadUtil)
{
audioThreadUtil->Close();
}
lock.lock();
delete videoThreadUtil;
delete audioThreadUtil;
videoThreadUtil = nullptr;
audioThreadUtil = nullptr;
lock.unlock();
}
// 快进
void DemuxThreadUtil::Seek(double pos)
{
qDebug() << QString::fromLocal8Bit("进入快进");
Clear();
qDebug() << QString::fromLocal8Bit("设置暂停");
lock.lock();
bool pauseStatus = this->isPause;
qDebug() << QString::fromLocal8Bit("pauseStatus") << pauseStatus;
lock.unlock();
// 设置暂停
SetPause(true);
qDebug() << QString::fromLocal8Bit("设置暂停成功");
lock.lock();
if (demuxUtil)
{
qDebug() << QString::fromLocal8Bit("开始跳转");
demuxUtil->Seek(pos);
}
long long seekPts = pos * demuxUtil->totalMilliseconds;
qDebug() << QString::fromLocal8Bit("进度条目标位置pts") << seekPts;
while (!isExit)
{
AVPacket* pkt = demuxUtil->ReadOnlyVideo();
if (!pkt)
{
break;
}
if (videoThreadUtil->RepaintSeekPts(pkt, seekPts))
{
this->pts = seekPts;
break;
}
}
qDebug() << QString::fromLocal8Bit("进度条跳转完成");
lock.unlock();
if (!pauseStatus)
{
SetPause(false);
}
}
// 设置暂停
void DemuxThreadUtil::SetPause(bool isPause)
{
lock.lock();
this->isPause = isPause;
if (videoThreadUtil)
{
videoThreadUtil->SetPause(isPause);
}
if (audioThreadUtil)
{
audioThreadUtil->SetPause(isPause);
}
lock.unlock();
}