Commit b070c0fde045ac35b70e8e247273ed1f654b0c4f

Authored by Hu Chunming
1 parent 0bb99bec

修正m_recoderinfo_list的size为1时pop后会导致崩溃的问题

src/decoder/dvpp/FFRecoderTaskManager.cpp
@@ -180,72 +180,80 @@ void FFRecoderTaskManager::recode_thread() { @@ -180,72 +180,80 @@ void FFRecoderTaskManager::recode_thread() {
180 180
181 auto it_param = m_recoderinfo_list.begin(); 181 auto it_param = m_recoderinfo_list.begin();
182 RecoderInfo recoderinfo = *it_param; 182 RecoderInfo recoderinfo = *it_param;
183 - m_recoderinfo_list.pop_front();  
184 m_recoderinfo_list_mtx.unlock(); 183 m_recoderinfo_list_mtx.unlock();
185 184
186 - auto it_data = getStartIterator(recoderinfo.frame_nb);  
187 - if(it_data == m_pkt_list.end()) {  
188 - std::this_thread::sleep_for(std::chrono::milliseconds(3));  
189 - continue;  
190 - } 185 + do
  186 + {
  187 + auto it_data = getStartIterator(recoderinfo.frame_nb);
  188 + if(it_data == m_pkt_list.end()) {
  189 + std::this_thread::sleep_for(std::chrono::milliseconds(3));
  190 + break;
  191 + }
191 192
192 - LOG_INFO("start frame_nb: {}", (*it_data)->frame_nb); 193 + LOG_INFO("start frame_nb: {}", (*it_data)->frame_nb);
193 194
194 - m_pkt_list_mtx.lock();  
195 - auto it = m_pkt_list.begin();  
196 - while (it != it_data) {  
197 - DataPacket* dataPkt = m_pkt_list.front();  
198 - delete dataPkt;  
199 - dataPkt = nullptr;  
200 - m_pkt_list.pop_front();  
201 - it = m_pkt_list.begin();  
202 - }  
203 - m_pkt_list_mtx.unlock(); 195 + m_pkt_list_mtx.lock();
  196 + auto it = m_pkt_list.begin();
  197 + while (it != it_data) {
  198 + DataPacket* dataPkt = m_pkt_list.front();
  199 + delete dataPkt;
  200 + dataPkt = nullptr;
  201 + m_pkt_list.pop_front();
  202 + it = m_pkt_list.begin();
  203 + }
  204 + m_pkt_list_mtx.unlock();
204 205
205 - string file_name = recoderinfo.recoderPath;  
206 -  
207 - FFRecoder ffrecoder;  
208 - bool bInit = ffrecoder.init(m_inStream, m_avctx, file_name.c_str());  
209 - if (!bInit) {  
210 - LOG_ERROR("ffrecoder init error : {} {} {}", recoderinfo.task_id, recoderinfo.object_id, recoderinfo.frame_nb);  
211 - ffrecoder.uninit();  
212 - continue;  
213 - }  
214 - LOG_DEBUG("record start, pkt_list size: {} task_id: {} object_id:{} frame_nb: {}", m_pkt_list.size(), recoderinfo.task_id, recoderinfo.object_id, recoderinfo.frame_nb);  
215 -  
216 - int count = 0;  
217 - auto it_save = it_data;  
218 - unsigned long long start_frame_nb = (*it_data)->frame_nb;  
219 - unsigned long long end_frame_nb = (*it_data)->frame_nb;  
220 - for (; it_save != m_pkt_list.end() && count < 500; ++it_save) {  
221 - DataPacket* dataPkt = *it_save;  
222 - if(dataPkt->frame_nb > recoderinfo.frame_nb) { 206 + string file_name = recoderinfo.recoderPath;
  207 +
  208 + FFRecoder ffrecoder;
  209 + bool bInit = ffrecoder.init(m_inStream, m_avctx, file_name.c_str());
  210 + if (!bInit) {
  211 + LOG_ERROR("ffrecoder init error : {} {} {}", recoderinfo.task_id, recoderinfo.object_id, recoderinfo.frame_nb);
  212 + ffrecoder.uninit();
223 break; 213 break;
224 } 214 }
225 - AVPacket* pkt = dataPkt->pkt;  
226 - if(pkt == nullptr) {  
227 - LOG_ERROR("{} pkt is nullptr", recoderinfo.task_id);  
228 - continue;  
229 - } else if (pkt->data == nullptr || pkt->size <= 0){  
230 - LOG_ERROR("{} pkt data is nullptr", recoderinfo.task_id);  
231 - continue; 215 + LOG_DEBUG("record start, pkt_list size: {} task_id: {} object_id:{} frame_nb: {}", m_pkt_list.size(), recoderinfo.task_id, recoderinfo.object_id, recoderinfo.frame_nb);
  216 +
  217 + int count = 0;
  218 + auto it_save = it_data;
  219 + unsigned long long start_frame_nb = (*it_data)->frame_nb;
  220 + unsigned long long end_frame_nb = (*it_data)->frame_nb;
  221 + for (; it_save != m_pkt_list.end() && count < 500; ++it_save) {
  222 + DataPacket* dataPkt = *it_save;
  223 + if(dataPkt->frame_nb > recoderinfo.frame_nb) {
  224 + break;
  225 + }
  226 + AVPacket* pkt = dataPkt->pkt;
  227 + if(pkt == nullptr) {
  228 + LOG_ERROR("{} pkt is nullptr", recoderinfo.task_id);
  229 + continue;
  230 + } else if (pkt->data == nullptr || pkt->size <= 0){
  231 + LOG_ERROR("{} pkt data is nullptr", recoderinfo.task_id);
  232 + continue;
  233 + }
  234 +
  235 + ffrecoder.write_pkt(pkt);
  236 + count++;
  237 + end_frame_nb = (*it_save)->frame_nb;
232 } 238 }
233 -  
234 - ffrecoder.write_pkt(pkt);  
235 - count++;  
236 - end_frame_nb = (*it_save)->frame_nb;  
237 - }  
238 239
239 - ffrecoder.flush_pkt();  
240 - ffrecoder.uninit(); 240 + ffrecoder.flush_pkt();
  241 + ffrecoder.uninit();
241 242
242 - // 发送mq消息  
243 - if(mq_publish_func && recoderinfo.mq_info.length() > 0) {  
244 - mq_publish_func(recoderinfo.mq_info.c_str());  
245 - // LOG_INFO("record save: {}", recoderinfo.mq_info.c_str());  
246 - } 243 + // 发送mq消息
  244 + if(mq_publish_func && recoderinfo.mq_info.length() > 0) {
  245 + mq_publish_func(recoderinfo.mq_info.c_str());
  246 + // LOG_INFO("record save: {}", recoderinfo.mq_info.c_str());
  247 + }
247 248
248 - LOG_INFO("record end, total save: {} start_frame_nb: {} end_frame_nb: {} file_path: {}", count, start_frame_nb, end_frame_nb, file_name); 249 + LOG_INFO("record end, total save: {} start_frame_nb: {} end_frame_nb: {} file_path: {}", count, start_frame_nb, end_frame_nb, file_name);
  250 + } while (0);
  251 +
  252 + // m_recoderinfo_list 为空会触发 m_pkt_list size 大于1000时的删除操作,
  253 + // 因此应当在本次m_pkt_list操作都完成之后再pop,避免这边在用m_pkt_list, 另一边在删除,从而导致崩溃
  254 + m_recoderinfo_list_mtx.lock();
  255 + m_recoderinfo_list.pop_front();
  256 + m_recoderinfo_list_mtx.unlock();
249 } 257 }
250 258
251 LOG_INFO("recode_thread end."); 259 LOG_INFO("recode_thread end.");