forked from c12121234/MyFFmpegPlayer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathffmpegaudio.cpp
More file actions
168 lines (157 loc) · 3.8 KB
/
ffmpegaudio.cpp
File metadata and controls
168 lines (157 loc) · 3.8 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
#include "ffmpegaudio.h"
#include "ffmpegdecode.h"
#include "ffmpegaudioplay.h"
#include "ffmpegresample.h"
#include <QDebug>
void FFmpegAudio::run()
{
unsigned char* pcm = new unsigned char[1024*1024*10];
while(!m_bIsExit)
{
m_AudioMutex.lock();
// if(m_PacketsList.empty() || !m_pDecode || !m_pAudioPlay || !m_pResample)
// {
// m_AudioMutex.unlock();
// msleep(1);
// continue;
// }
// AVPacket* pPkt = m_PacketsList.front();
// m_PacketsList.pop_front();
if(m_bIsPause)
{
m_AudioMutex.unlock();
msleep(5);
continue;
}
AVPacket* pPkt = this->Pop();
bool bRet = m_pDecode->Send(pPkt);
// if(!bRet)
// {
// m_AudioMutex.unlock();
// msleep(1);
// continue;
// }
//可能一次send 多個recv
while(!m_bIsExit)
{
AVFrame* pFrame = m_pDecode->Recv();
//減去緩衝中未播放的時間 單位ms
m_nPts = m_pDecode->GetPts() - m_pAudioPlay->GetNoPlayMs();
if(!pFrame)
break;
//resample
int nSize = m_pResample->Resample(pFrame,pcm);
//play audio
while(!m_bIsExit)
{
if(nSize<=0)
break;
int nFree = m_pAudioPlay->GetFree();
if(nFree<nSize || m_bIsPause)
{
msleep(1);
continue;
}
m_pAudioPlay->Write(pcm,nSize);
break;
}
}
m_AudioMutex.unlock();
msleep(1);
}
delete pcm;
}
FFmpegAudio::FFmpegAudio():
m_pAudioPlay(nullptr)
, m_pResample(nullptr)
, m_nPts(0)
, m_bIsPause(false)
{
if(!m_pResample)
m_pResample = new FFmpegResample();
if(!m_pAudioPlay)
m_pAudioPlay = FFmpegAudioPlay::Get();
}
FFmpegAudio::~FFmpegAudio()
{
m_bIsExit = true;
wait();
}
bool FFmpegAudio::Open(AVCodecParameters *pPara, int nSampleRate, int nChannels)
{
if(!pPara)
return false;
Clear();
m_AudioMutex.lock();
m_nPts = 0; //因為多次開啟open 每次開啟重置pts值 (若有close function 也可寫在close內)
bool bRet = true;
if(!m_pResample->Open(pPara,false))
{
qInfo()<<"FFmpegResample open failed.";
bRet = false;
}
m_pAudioPlay->SetSampleRate(nSampleRate);
m_pAudioPlay->SetChannels(nChannels);
if(!m_pAudioPlay->Open())
{
qInfo()<<"FFmpegAudio open failed.";
bRet = false;
}
if(!m_pDecode->Open(pPara))
{
qInfo()<<"audio FFmpegDecode open failed.";
bRet = false;
}
m_AudioMutex.unlock();
qInfo()<<"FFmpegAudio open:"<<bRet;
return bRet;
}
void FFmpegAudio::Close()
{
this->FFmpegDecodeThread::Close();
if(m_pResample)
{
m_pResample->Close();
m_AudioMutex.lock();
delete m_pResample;
m_pResample = nullptr;
m_AudioMutex.unlock();
}
if(m_pAudioPlay)
{
m_pAudioPlay->Close();
m_AudioMutex.lock();
m_pAudioPlay = nullptr;
m_AudioMutex.unlock();
}
}
void FFmpegAudio::Clear()
{
this->FFmpegDecodeThread::Clear();
m_AudioMutex.lock();
if(m_pAudioPlay)
m_pAudioPlay->Clear();
m_AudioMutex.unlock();
}
long long FFmpegAudio::GetPts()
{
return m_nPts;
}
void FFmpegAudio::SetPts(long long nPts)
{
m_AudioMutex.lock();
m_nPts = nPts;
m_AudioMutex.unlock();
}
void FFmpegAudio::SetPause(bool bIsPause)
{
m_AudioMutex.lock();
m_bIsPause = bIsPause;
if(m_pAudioPlay)
m_pAudioPlay->SetPause(bIsPause);
m_AudioMutex.unlock();
}
bool FFmpegAudio::GetPauseState()
{
return m_bIsPause;
}