-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXDemuxThread.cpp
More file actions
202 lines (166 loc) · 3.53 KB
/
XDemuxThread.cpp
File metadata and controls
202 lines (166 loc) · 3.53 KB
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
#include "XDemuxThread.h"
#include "XDemux.h"
#include "XVideoThread.h"
#include "XAudioThread.h"
#include "XDecode.h"
extern "C" {
#include <libavformat/avformat.h>
}
#include <iostream>
using namespace std;
XDemuxThread::XDemuxThread()
{
}
XDemuxThread::~XDemuxThread()
{
isExit = true;
wait();
}
bool XDemuxThread::Open(const char *url, IVideoCall *call)
{
// 容错处理
if(url == 0 || url[0] == '\0')
return false;
mux.lock();
// 打开解封装
bool ret = demux->Open(url);
if(!ret)
{
mux.unlock();
cout << "demux->Open(url) failed!" << endl;
return false;
}
// 打开视频解码器和处理线程
if(!vt->Open(demux->CopyVPara(), call, demux->width, demux->height))
{
ret = false;
cout << "vt->Open() failed!" << endl;
}
// 打开音频解码器和处理线程
if(!at->Open(demux->CopyAPara()))
{
ret = false;
cout << "at->Open() failed!" << endl;
}
totalMs = demux->totalMs;
mux.unlock();
cout << "XDemucThread::Open seccess!" << endl;
return ret;
}
void XDemuxThread::Start()
{
mux.lock();
if(!demux) demux = new XDemux();
if(!vt) vt = new XVideoThread();
if(!at) at = new XAudioThread();
QThread::start();
if(vt) vt->start();
if(at) at->start();
mux.unlock();
}
void XDemuxThread::Close()
{
isExit = true;
wait();
if(vt) vt->Close();
if(at) at->Close();
mux.lock();
delete vt;
delete at;
vt = nullptr;
at = nullptr;
mux.unlock();
}
void XDemuxThread::Clear()
{
mux.lock();
if(demux)
demux->Clear();
if(vt)
vt->Clear();
if(at)
at->Clear();
mux.unlock();
}
void XDemuxThread::SetPause(bool isPause)
{
mux.lock();
this->isPause = isPause;
if(at) at->SetPause(isPause);
if(vt) vt->SetPause(isPause);
mux.unlock();
}
void XDemuxThread::run()
{
while(!isExit)
{
mux.lock();
if(isPause)
{
mux.unlock();
msleep(5);
continue;
}
if(!demux)
{
mux.unlock();
msleep(5);
continue;
}
// 音视频同步
if(vt && at)
{
pts = at->pts;
vt->synpts = at->pts;
}
AVPacket *pkt = demux->Read();
if (!pkt)
{
mux.unlock();
msleep(5);
continue;
}
// 判断数据是音频
if(demux->IsAudio(pkt))
{
if(at) at->Push(pkt);
}
else // 视频
{
if(vt) vt->Push(pkt);
}
mux.unlock();
msleep(1);
}
}
void XDemuxThread::Seek(double pos)
{
// 清理缓冲
Clear();
mux.lock();
bool status = this->isPause;
mux.unlock();
// 暂停
SetPause(true);
mux.lock();
if(demux)
demux->Seek(pos);
// 实际要显示的位置pts
long long seekPts = pos * demux->totalMs;
while(!isExit)
{
AVPacket *pkt = demux->ReadVideo();
if(!pkt)
break;
// 如果解码到pts
if(vt->RepaintPts(pkt, seekPts))
{
this->pts = seekPts;
break;
}
}
mux.unlock();
// seek是非暂停状态
if(!status)
SetPause(false);
}