瀏覽代碼

修复偶发的卡死问题

huihui 4 月之前
父節點
當前提交
c7eb58d8e0

+ 19 - 11
module/VideoPlayer/src/PcmPlayer/PcmPlayer.cpp

@@ -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);
 }

+ 4 - 1
module/VideoPlayer/src/PcmPlayer/PcmPlayer.h

@@ -32,11 +32,12 @@ public:
 
     uint32_t getCurrentPts(){return m_current_pts;}
 
-    void setMute(const bool is_mute){m_volume = is_mute;}
+    void setMute(const bool is_mute){m_is_mute = is_mute;}
     void setVolume(float value){m_volume = value;}
     float getVolume(){return m_volume;}
 
     bool deviceOpened(){return m_device_opened;}
+    bool deviceOpenFailed(){return m_device_open_failed;}
 
 protected:
     std::mutex m_mutex_audio;
@@ -49,10 +50,12 @@ protected:
 
     uint32_t m_current_pts = 0; //当前播放帧的时间戳
     bool m_device_opened = false; //设备是否已经打开了
+    bool m_device_open_failed = false; //设备打开失败
     uint64_t m_last_try_open_device_time = 0; //上一次尝试打开音频设备的时间
     int m_sample_rate = 0;
     int m_channel = 0;
     int m_cache_size = 81920; //缓存大小
+    bool m_is_stop = false;
 
     ///音量相关变量
     bool  m_is_mute = false;

+ 1 - 1
module/VideoPlayer/src/PcmPlayer/PcmPlayer_SDL.cpp

@@ -81,7 +81,7 @@ bool PcmPlayer_SDL::openDevice()
             break;
         }
     }
-// qDebug()<<__FUNCTION__<<"NUM="<<num<<" mAudioID="<<mAudioID;
+// printf("NUM=%d mAudioID=%d \n", num, mAudioID);
     /* 检查实际使用的配置(保存在spec,由SDL_OpenAudio()填充) */
 //    if (spec.format != AUDIO_S16SYS)
     if (mAudioID <= 0)