-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSDLPlayer.cpp
More file actions
309 lines (264 loc) · 9.75 KB
/
SDLPlayer.cpp
File metadata and controls
309 lines (264 loc) · 9.75 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
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
#include "SDLPlayer.h"
#include <QDebug>
#define MAX_AUDIO_SIZE_PER_SECOND 192000//19200*10
SDLPlayer::SDLPlayer(QObject *parent) : QObject(parent)
{
mAudioSourceMap->clear();
mIsOpenSDL = false;
mThread = new QThread();
this->moveToThread(mThread);
mThread->start();
connect(this,SIGNAL(showWaveSignal(int,SdlDataModel)),this,SLOT(onAudioValueEnqueue(int,SdlDataModel)),Qt::QueuedConnection);
}
SDLPlayer *SDLPlayer::mSDLPlayer = NULL;
int SDLPlayer::mMaxIndex = 1;
QMap<int,MediaQueue<SdlDataModel> > *SDLPlayer::mAudioSourceMap = new QMap<int,MediaQueue<SdlDataModel> >();
QMap<int,SdlDataModel> *SDLPlayer::mPlayingModelMap = new QMap<int,SdlDataModel>();
QMap<int,MediaQueue<short> > *SDLPlayer::mAudioValueMap = new QMap<int,MediaQueue<short> >();
SDLPlayer *SDLPlayer::getSDLPlayer()
{
if(mSDLPlayer == NULL){
mSDLPlayer = new SDLPlayer();
}
return mSDLPlayer;
}
void SDLPlayer::stopSDL()
{
mIsAudioVideoSync = false;
mAudioSourceMap->clear();
mAudioValueMap->clear();
mPlayingModelMap->clear();
SDL_PauseAudio(1);
}
void SDLPlayer::resumeSDL()
{
mIsAudioVideoSync = true;
mAudioSourceMap->clear();
mAudioValueMap->clear();
mPlayingModelMap->clear();
SDL_PauseAudio(0);
}
void SDLPlayer::closeSDL()
{
stopSDL();
SDL_CloseAudio();
mIsOpenSDL = false;
}
MediaQueue<short>& SDLPlayer::getSDLShowQueue(int index)
{
return *(this->mAudioValueMap->find(index));
}
void SDLPlayer::setVideoStamp(long videoStamp)
{
mVideoStampMutex.lock();
mVideoStamp = videoStamp;
mVideoStampMutex.unlock();
}
long SDLPlayer::getVideoStamp()
{
return mVideoStamp;
}
bool SDLPlayer::isOpenSDL()
{
return mIsOpenSDL;
}
///////////////////////波形显示采样/////////////////////////////////
void SDLPlayer::onAudioValueEnqueue(int index,SdlDataModel model)
{
if(getSDLShowQueue(index).count() >= 400){
getSDLShowQueue(index).clear();
}
double total = 0;
int count = 0;
int interval = mIntervalSampleCount/2;
if(model.mSdlData != NULL){
unsigned char *data = model.mSdlData;
int dataSize = model.mSdlSize;
// qDebug()<<"dataSize:"<<dataSize;
for(int i=0;i < dataSize;i=i+2){
// qDebug()<<" i:"<<i<<" data+i:"<<*((short *)(data+i));
total = total + abs(*((short *)(data+i)));
if(count%interval == 0){
// qDebug()<<"total/interval:"<<total/interval<<"(short)(total/interval):"<<(short)(total/interval);
getSDLShowQueue(index).enqueue((short)(total/interval));
total = 0;
}
++count;
}
}
}
/////////////////////////音频播放//////////////////////////
long SDLPlayer::mAudioStamp = -1;
bool SDLPlayer::mIsAudioVideoSync = false;
void SDLPlayer::audioCallBack(void *,Uint8 *stream,int len)
{
memset(stream,0,len);
while(len > 0){
//有无播放内容,无则静音
bool isQuietFlag = true;
//遍历播放内容
foreach(int index,mAudioSourceMap->keys()){
if(mPlayingModelMap->contains(index)){//已在播放队列则取下一个播放包
//读取
SdlDataModel &playingModel = *(mPlayingModelMap->find(index));
if(playingModel.mPlaySize >= playingModel.mSdlSize){//播放完成,取下一个播放块
//1.释放空间,移到波形中释放
if(playingModel.mSdlData){
free(playingModel.mSdlData);
playingModel.mSdlData = NULL;
}
//2.取下一个播放数据
if(mAudioSourceMap->find(index)->count() > 0){
SDLPlayer::getSDLPlayer()->mAudioSourceMapMutex.lock();
playingModel = mAudioSourceMap->find(index)->dequeue();
SDLPlayer::getSDLPlayer()->mAudioSourceMapMutex.unlock();
isQuietFlag = false;
}
playingModel.mPlaySize = 0;
}else{//未播放完成
isQuietFlag = false;
}
}else{//不在播放队列则添加到播放队列
if(mAudioSourceMap->find(index)->count()>0){
SDLPlayer::getSDLPlayer()->mAudioSourceMapMutex.lock();
SdlDataModel playingModel = mAudioSourceMap->find(index)->dequeue();
SDLPlayer::getSDLPlayer()->mAudioSourceMapMutex.unlock();
mPlayingModelMap->insert(index,playingModel);
isQuietFlag = false;
}
}
}
//静音播放
if(isQuietFlag){
//播放静音数据,防止不再回调
SdlDataModel playingModel;
playingModel.mAudioPts = -1;
playingModel.mSdlSize = len;
playingModel.mSdlData = (unsigned char*)malloc(playingModel.mSdlSize);
memset(playingModel.mSdlData, 0, playingModel.mSdlSize);
QThread::usleep(10);
//播放长度
int leaveLen = playingModel.mSdlSize - playingModel.mPlaySize;//剩下播放长度
int playLen = leaveLen>len?len:leaveLen;//播放长度
//播放声音
SDL_MixAudioFormat(stream,playingModel.mSdlData+playingModel.mPlaySize,AUDIO_S16SYS,playLen,SDL_MIX_MAXVOLUME);//SDL_MIX_MAXVOLUME 0-128
return;
}
//播放
int maxLen = 0;
foreach(int index,mPlayingModelMap->keys()){
SdlDataModel &playingModel = *(mPlayingModelMap->find(index));
if(playingModel.mSdlData != NULL){
//播放长度
int leaveLen = playingModel.mSdlSize - playingModel.mPlaySize;//剩下播放长度
int playLen = leaveLen>len?len:leaveLen;//播放长度
if(maxLen <= playLen){
maxLen = playLen;
}
qDebug()<<"index:"<<index<<" dequeue---count():"<<mAudioSourceMap->find(index)->count()<<"playingModel.mSdlData:"<<playingModel.mSdlData;
//播放声音
SDL_MixAudioFormat(stream,playingModel.mSdlData+playingModel.mPlaySize,AUDIO_S16SYS,playLen,SDL_MIX_MAXVOLUME);//SDL_MIX_MAXVOLUME 0-128
//播放长度更新
playingModel.mPlaySize += playLen;
}
}
if(maxLen){
len -= maxLen;
stream += maxLen;
}
}
}
int SDLPlayer::addAudioSourceQueue(SdlDataModel& model,int index)
{
if(index <= 0){
index = mMaxIndex++;
MediaQueue<SdlDataModel> sdlDataModelQueue;
mAudioSourceMapMutex.lock();
mAudioSourceMap->insert(index,sdlDataModelQueue);
mAudioSourceMapMutex.unlock();
}
if(mAudioSourceMap->contains(index)){
mAudioSourceMapMutex.lock();
mAudioSourceMap->find(index)->enqueue(model);
mAudioSourceMapMutex.unlock();
qDebug()<<"index:"<<index<<" enqueue---count():"<<mAudioSourceMap->find(index)->count()<<" model.mSdlData:"<<model.mSdlData;
}else{
index = -1;
}
return index;
}
void SDLPlayer::delAudioSourceQueue(int index)
{
if(index){
mAudioSourceMapMutex.lock();
mAudioSourceMap->remove(index);
mPlayingModelMap->remove(index);
mAudioValueMap->remove(index);
mAudioSourceMapMutex.unlock();
}
}
void SDLPlayer::setSdlOpt(int channelCount,int audioSample,int sampleCountPerPacket,SDL_AudioFormat format)
{
mChannelCount = channelCount;
mAudioSample = audioSample;
mSampleCountPerPacket = sampleCountPerPacket;
mAudioFormat = format;
mIntervalMSecond = 8;//8ms
int audioSamplePointSize = 2;
switch(mAudioFormat){
case AUDIO_U8:
case AUDIO_S8:
audioSamplePointSize = 1;
break;
case AUDIO_U16LSB:
case AUDIO_S16LSB:
case AUDIO_U16MSB:
case AUDIO_S16MSB:
audioSamplePointSize = 2;
break;
case AUDIO_S32LSB:
case AUDIO_S32MSB:
case AUDIO_F32LSB:
case AUDIO_F32MSB:
audioSamplePointSize = 4;
break;
}
mIntervalSampleCount = mAudioSample/1000*mIntervalMSecond*audioSamplePointSize*mChannelCount; //每个64个字节 32000/1000*1*2*1=64
}
bool SDLPlayer::startSDL()
{
if(mAudioSample <= 0 || mChannelCount <= 0){
qDebug()<<"audio option error";
return false;
}
//已经打开时先关闭
if(mIsOpenSDL){
stopSDL();
}
//重新打开
mWantAudioSpec.freq = mAudioSample;//mAudioSample;//采样率 static const int next_sample_rates[] = {0, 44100, 48000, 96000, 192000};
mWantAudioSpec.channels = mChannelCount;//mChannelCount;//mAudioChannelCount;//通道数static const int next_nb_channels[] = {0, 0, 1, 6, 2, 6, 4, 6};
mWantAudioSpec.silence = 0;//是否禁音
mWantAudioSpec.samples = mSampleCountPerPacket;//FFMAX(SDL_AUDIO_MIN_BUFFER_SIZE, 2<<av_log2(mWantAudioSpec.freq/SDL_AUDIO_MAX_CALLBACKS_PER_SEC));//帧大小 AAC:1024 MP3:1152
mWantAudioSpec.format = mAudioFormat;//采样格式
mWantAudioSpec.callback = audioCallBack;//回调函数
if(SDL_OpenAudio(&mWantAudioSpec, NULL)<0){
qDebug("SDL_OpenAudio failure %s",SDL_GetError());
return false;
}
mIsOpenSDL = true;
SDL_PauseAudio(0);
return true;
}
int SDLPlayer::getAudioSample()
{
return mAudioSample;
}
int SDLPlayer::getChannelCount()
{
return mChannelCount;
}
int SDLPlayer::getAudioFormat()
{
return mAudioFormat;
}