-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXAudioThread.cpp
More file actions
163 lines (137 loc) · 3.25 KB
/
XAudioThread.cpp
File metadata and controls
163 lines (137 loc) · 3.25 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
#include "XAudioThread.h"
#include "XDecode.h"
#include "XResample.h"
#include "XAudioPlay.h"
#include <iostream>
using namespace std;
XAudioThread::XAudioThread()
{
if(!res) res = new XResample();
if(!ap) ap = XAudioPlay::Get();
}
XAudioThread::~XAudioThread()
{
}
void XAudioThread::run()
{
unsigned char *pcm = new unsigned char[1024 * 1024 * 10];
while(!isExit)
{
amux.lock();
if(isPause)
{
amux.unlock();
msleep(5);
continue;
}
// 没有数据
// if(packs.empty() || !decode || !res || !ap)
// {
// amux.unlock();
// msleep(1);
// continue;
// }
// AVPacket *pkt = packs.front();
// packs.pop_front();
AVPacket *pkt = Pop();
bool ret = decode->Send(pkt);
if(!ret)
{
amux.unlock();
msleep(1);
continue;
}
// 一次send 多次recv
while(!isExit)
{
AVFrame *frame = decode->Recv();
if(!frame)
break;
// 减去缓冲中未播放的时间
pts = decode->pts - ap->GetNoPlayMs();
cout << "audio thread pts = " << pts << endl;
// 重采样
int size = res->Resample(frame, pcm);
// 播放音频
while(!isExit)
{
if(size <= 0)
break;
// 缓冲未播完,空间不够
if(ap->GetFree() < size || isPause)
{
msleep(1);
continue;
}
ap->Write(pcm, size);
break;
}
}
amux.unlock();
}
delete[] pcm;
}
bool XAudioThread::Open(AVCodecParameters *para)
{
if(!para)
return false;
Clear();
amux.lock();
pts = 0;
bool ret = true;
if(!res->Open(para, false))
{
ret = false;
cout << "XResample open failed !" << endl;
// amux.unlock();
// return false;
}
if(!ap->Open())
{
ret = false;
cout << "XAudioPlay open failed !" << endl;
// amux.unlock();
// return false;
}
if(!decode->Open(para))
{
cout << "audio XDecode open failed !" << endl;
ret = false;
}
cout << "XAudioThread::Open = " << ret << endl;
amux.unlock();
return ret;
}
void XAudioThread::Close()
{
XDecodeThread::Close();
if(res)
{
res->Close();
amux.lock();
res = nullptr;
amux.unlock();
}
if(ap)
{
ap->Close();
amux.lock();
ap = nullptr;
amux.unlock();
}
}
void XAudioThread::Clear()
{
XDecodeThread::Clear();
mux.lock();
if(ap) ap->Clear();
mux.unlock();
}
void XAudioThread::SetPause(bool isPause)
{
// amux.lock();
this->isPause = isPause;
if(ap)
ap->SetPause(isPause);
// amux.unlock();
}