|
@@ -25,14 +25,19 @@ bool PcmPlayer::startPlay()
|
|
|
|
|
|
bool is_succeed = openDevice();
|
|
|
m_device_opened = is_succeed;
|
|
|
+ m_device_open_failed = !m_device_opened;
|
|
|
|
|
|
m_cache_size = 0.15 * m_sample_rate * m_channel; //计算150ms的缓存大小
|
|
|
|
|
|
+ m_is_stop = false;
|
|
|
+
|
|
|
return is_succeed;
|
|
|
}
|
|
|
|
|
|
bool PcmPlayer::stopPlay()
|
|
|
{
|
|
|
+ m_is_stop = true;
|
|
|
+ m_cond_audio.notify_all();
|
|
|
bool isSucceed = closeDevice();
|
|
|
m_device_opened = false;
|
|
|
return isSucceed;
|
|
@@ -44,10 +49,12 @@ int PcmPlayer::inputPCMFrame(PCMFramePtr frame)
|
|
|
|
|
|
if (m_device_opened) //音频设备打开的情况下才处理播放
|
|
|
{
|
|
|
- std::lock_guard<std::mutex> lck(m_mutex_audio);
|
|
|
- m_pcm_frame_list.push_back(frame);
|
|
|
- frame_size = m_pcm_frame_list.size();
|
|
|
- m_cond_audio.notify_all();
|
|
|
+ {
|
|
|
+ std::lock_guard<std::mutex> lck(m_mutex_audio);
|
|
|
+ m_pcm_frame_list.push_back(frame);
|
|
|
+ frame_size = m_pcm_frame_list.size();
|
|
|
+ m_cond_audio.notify_all();
|
|
|
+ }
|
|
|
|
|
|
// qDebug()<<m_sample_rate<<m_channel<<m_device_opened<<framePtr->sampleRate()<<framePtr->channels();
|
|
|
|
|
@@ -73,6 +80,7 @@ int PcmPlayer::inputPCMFrame(PCMFramePtr frame)
|
|
|
m_sample_rate = use_sample_rate;
|
|
|
m_channel = channels;
|
|
|
|
|
|
+ printf("resample to open audio device ... \n");
|
|
|
stopPlay();
|
|
|
startPlay();
|
|
|
}
|
|
@@ -85,7 +93,7 @@ int PcmPlayer::inputPCMFrame(PCMFramePtr frame)
|
|
|
if ((tick - m_last_try_open_device_time) > 3000)
|
|
|
{
|
|
|
//音频设备未打开,则每隔3秒尝试打开一次
|
|
|
- fprintf(stderr, "try to open audio device ... \n");
|
|
|
+ printf("try to open audio device ... \n");
|
|
|
stopPlay();
|
|
|
startPlay();
|
|
|
}
|
|
@@ -113,21 +121,20 @@ void PcmPlayer::clearFrame()
|
|
|
|
|
|
void PcmPlayer::playAudioBuffer(void *stream, int len)
|
|
|
{
|
|
|
-//fprintf(stderr, "%s %d %d \n", __FUNCTION__, len, mPcmFrameList.size());
|
|
|
+// printf("%s:%d %d %d m_is_stop=%d\n", __FILE__, __LINE__, len, m_pcm_frame_list.size(), m_is_stop);
|
|
|
|
|
|
while (m_last_frame_buffer_size < len)
|
|
|
{
|
|
|
std::unique_lock<std::mutex> lck(m_mutex_audio);
|
|
|
-
|
|
|
- while (m_pcm_frame_list.empty())
|
|
|
+ while (m_pcm_frame_list.empty() && !m_is_stop)
|
|
|
{
|
|
|
- if (m_cond_audio.wait_for(lck, std::chrono::milliseconds (500)) == std::cv_status::timeout)
|
|
|
+ if (m_cond_audio.wait_for(lck, std::chrono::milliseconds(500)) == std::cv_status::timeout)
|
|
|
{
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- if (m_pcm_frame_list.empty())
|
|
|
+ if (m_pcm_frame_list.empty() || m_is_stop)
|
|
|
{
|
|
|
break;
|
|
|
}
|
|
@@ -141,6 +148,7 @@ void PcmPlayer::playAudioBuffer(void *stream, int len)
|
|
|
m_current_pts = pcm_frame->pts();
|
|
|
}
|
|
|
|
|
|
+ // printf("%s:%d %d %d m_is_stop=%d\n", __FILE__, __LINE__, len, m_pcm_frame_list.size(), m_is_stop);
|
|
|
if (m_last_frame_buffer_size > 0)
|
|
|
{
|
|
|
// fprintf(stderr, "%s %d %d \n", __FUNCTION__, pcmFramePtr->getSize(), len);
|
|
@@ -161,5 +169,5 @@ void PcmPlayer::playAudioBuffer(void *stream, int len)
|
|
|
memmove(m_last_frame_buffer, m_last_frame_buffer+buffer_size, m_last_frame_buffer_size);
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
+// printf("%s:%d %d %d m_is_stop=%d\n", __FILE__, __LINE__, len, m_pcm_frame_list.size(), m_is_stop);
|
|
|
}
|