Przeglądaj źródła

V2.0.0

1.修复音频队列重复分配数据导致的内存泄露
叶海辉 6 lat temu
rodzic
commit
65dfb031f1

BIN
bin32/VideoRecorder.exe


+ 1 - 1
src/video/getvideothread.cpp

@@ -370,8 +370,8 @@ void GetVideoThread::run()
                     memcpy(picture_buf,pFrameYUV->data[0],y_size);
                     memcpy(picture_buf+y_size,pFrameYUV->data[1],y_size/4);
                     memcpy(picture_buf+y_size+y_size/4,pFrameYUV->data[2],y_size/4);
-                    uint8_t * yuv_buf = (uint8_t *)malloc(size);
 
+                    uint8_t * yuv_buf = (uint8_t *)malloc(size);
                     ///˝ŤYUVÍźĎń˛ĂźôłÉÄżąę´óĐĄ
                     Yuv420Cut(pic_x,pic_y,pic_w,pic_h,pCodecCtx->width,pCodecCtx->height,picture_buf,yuv_buf);
                     m_saveVideoFileThread->videoDataQuene_Input(yuv_buf,yuvSize*3/2,time);

+ 33 - 51
src/video/savevideofile.cpp

@@ -53,11 +53,7 @@ SaveVideoFileThread::SaveVideoFileThread()
     videoDataQueneHead = NULL;
     videoDataQueneTail = NULL;
 
-    AudioDataQueneHead = NULL;
-    AudioDataQueneTail = NULL;
-
     videoBufferCount = 0;
-    audioBufferCount = 0;
 
     m_videoFrameRate = 15;
 
@@ -206,60 +202,44 @@ BufferDataNode *SaveVideoFileThread::videoDataQuene_get(int64_t time)
     return node;
 }
 
-void SaveVideoFileThread::audioDataQuene_Input(uint8_t * buffer,int size)
+void SaveVideoFileThread::audioDataQuene_Input(const uint8_t *buffer, const int &size)
 {
-    BufferDataNode * node = (BufferDataNode*)malloc(sizeof(BufferDataNode));
+    BufferDataNode  node;
 //    node->buffer = buffer;
-    node->bufferSize = size;
-    node->next = NULL;
+    node.bufferSize = size;
+    node.next = NULL;
 
-    node->buffer = (uint8_t *)malloc(size);
-    memcpy(node->buffer,buffer,size);
+    node.buffer = (uint8_t*)buffer;
+//    node->buffer = (uint8_t *)malloc(size);
+//    memcpy(node->buffer,buffer,size);
 
     mAudioMutex.lock();
 
-    if (AudioDataQueneHead == NULL)
-    {
-        AudioDataQueneHead = node;
-    }
-    else
-    {
-        AudioDataQueneTail->next = node;
-    }
-
-    AudioDataQueneTail = node;
+    mAudioDataList.append(node);
 
-    audioBufferCount++;
 //qDebug()<<__FUNCTION__<<audioBufferCount<<size;
     mAudioMutex.unlock();
 
 }
 
-BufferDataNode *SaveVideoFileThread::audioDataQuene_get()
+bool SaveVideoFileThread::audioDataQuene_get(BufferDataNode &node)
 {
-    BufferDataNode * node = NULL;
+    bool isSucceed = false;
 
     mAudioMutex.lock();
 
-    if (AudioDataQueneHead != NULL)
+    if (!mAudioDataList.isEmpty())
     {
-        node = AudioDataQueneHead;
+        node = mAudioDataList.takeFirst();
 
-        if (AudioDataQueneTail == AudioDataQueneHead)
-        {
-            AudioDataQueneTail = NULL;
-        }
-
-        AudioDataQueneHead = AudioDataQueneHead->next;
-
-        audioBufferCount--;
+        isSucceed = true;
 
+//    qDebug()<<__FUNCTION__<<mAudioDataList.size();
     }
-//qDebug()<<__FUNCTION__<<audioBufferCount;
 
     mAudioMutex.unlock();
 
-    return node;
+    return isSucceed;
 }
 
 
@@ -475,23 +455,25 @@ bool SaveVideoFileThread::write_audio_frame(AVFormatContext *oc, OutputStream *o
     c = ost->enc;
 
 #if 1
-    BufferDataNode *node = audioDataQuene_get();
 
-    if (node == NULL)
+    BufferDataNode node;
+
+    if (audioDataQuene_get(node))
+    {
+        frame = ost->frame;
+    //        memset(frame->data[0], 0x0, frame->nb_samples);
+        memcpy(frame->data[0], node.buffer, frame->nb_samples);
+    //            memcpy(frame->data[0], node.buffer, node.bufferSize);
+        free(node.buffer);
+
+        frame->pts = ost->next_pts;
+        ost->next_pts  += frame->nb_samples;
+    }
+    else
     {
         return false;
     }
 
-    frame = ost->frame;
-//        memset(frame->data[0], 0x0, frame->nb_samples);
-    memcpy(frame->data[0], node->buffer, frame->nb_samples);
-//            memcpy(frame->data[0], node->buffer, node->bufferSize);
-    free(node->buffer);
-    free(node);
-
-    frame->pts = ost->next_pts;
-    ost->next_pts  += frame->nb_samples;
-
 #else
     frame = get_audio_frame(ost); //×Ô¶¯Éú³ÉÒôƵÊý¾Ý
 #endif
@@ -499,10 +481,10 @@ bool SaveVideoFileThread::write_audio_frame(AVFormatContext *oc, OutputStream *o
     if (frame)
     {
         /* convert samples from native format to destination codec format, using the resampler */
-            /* compute destination number of samples */
-            dst_nb_samples = av_rescale_rnd(swr_get_delay(ost->swr_ctx, c->sample_rate) + frame->nb_samples,
-                                            c->sample_rate, c->sample_rate, AV_ROUND_UP);
-            av_assert0(dst_nb_samples == frame->nb_samples);
+        /* compute destination number of samples */
+        dst_nb_samples = av_rescale_rnd(swr_get_delay(ost->swr_ctx, c->sample_rate) + frame->nb_samples,
+                                        c->sample_rate, c->sample_rate, AV_ROUND_UP);
+        av_assert0(dst_nb_samples == frame->nb_samples);
 
 //        /* when we pass a frame to the encoder, it may keep a reference to it
 //         * internally;

+ 3 - 5
src/video/savevideofile.h

@@ -78,8 +78,8 @@ public:
     void videoDataQuene_Input(uint8_t * buffer, int size, int64_t time);
     BufferDataNode *videoDataQuene_get(int64_t time);
 
-    void audioDataQuene_Input(uint8_t * buffer,int size);
-    BufferDataNode *audioDataQuene_get();
+    void audioDataQuene_Input(const uint8_t * buffer,const int &size);
+    bool audioDataQuene_get(BufferDataNode &node);
 
     int audio_input_frame_size;
 
@@ -122,12 +122,10 @@ private:
     BufferDataNode * videoDataQueneTail;
 
     QMutex mAudioMutex;
-    BufferDataNode * AudioDataQueneHead;
-    BufferDataNode * AudioDataQueneTail;
+    QList<BufferDataNode> mAudioDataList;
 
     BufferDataNode * lastVideoNode; //上一次的帧(帧不足的时候用上一次的帧来补全)
     int videoBufferCount;
-    int audioBufferCount;
 
     void open_audio(AVFormatContext *oc, AVCodec *codec, OutputStream *ost);
     void close_audio(AVFormatContext *oc, OutputStream *ost);