OSDN Git Service

[BUILD] Set SOVERSION and GIT hash automatically.
[csp-qt/common_source_project-fm7.git] / source / src / qt / avio / movie_saver.cpp
index ffa388a..d50d8a7 100644 (file)
@@ -8,9 +8,10 @@
 #include <QDateTime>
 #include "movie_saver.h"
 #include "../osd.h"
-#include "agar_logger.h"
+#include "common.h"
+#include "csp_logger.h"
 
-MOVIE_SAVER::MOVIE_SAVER(int width, int height, int fps, OSD *osd) : QThread(0)
+MOVIE_SAVER::MOVIE_SAVER(int width, int height, int fps, OSD *osd, config_t *cfg) : QThread(0)
 {
        buffer_size=8 * 1024 * 224;
        max_rate=4000 * 1000;
@@ -18,7 +19,11 @@ MOVIE_SAVER::MOVIE_SAVER(int width, int height, int fps, OSD *osd) : QThread(0)
        _width = width;
        _height = height;
        rec_fps = fps;
+       
        p_osd = osd;
+       p_config = cfg;
+       p_logger = osd->get_logger();
+       
        recording = false;
        audio_sample_rate = 48000;
 #if defined(USE_MOVIE_SAVER)
@@ -39,6 +44,8 @@ MOVIE_SAVER::MOVIE_SAVER(int width, int height, int fps, OSD *osd) : QThread(0)
        video_data_queue.clear();
        
        do_reset_encoder_options();
+
+       //video_queue_mutex = new QMutex(QMutex::Recursive);
        totalSrcFrame = 0;
        totalDstFrame = 0;
        totalAudioFrame = 0;
@@ -47,16 +54,29 @@ MOVIE_SAVER::MOVIE_SAVER(int width, int height, int fps, OSD *osd) : QThread(0)
        audio_bit_rate = 160 * 1000;
        video_geometry = QSize(640, 480);
        video_encode_threads = 4;
-
+//     audio_enqueue_count = 0;
+       left_frames = 0;
        req_close = false;
+       req_stop = false;
        bRunThread = false;
+       
 }
 
 MOVIE_SAVER::~MOVIE_SAVER()
 {
+       req_close = true;
        if(recording) do_close_main();
 }
 
+QString MOVIE_SAVER::get_avio_version()
+{
+#if defined(__LIBAVIO_VERSION)
+       return QString::fromUtf8(__LIBAVIO_VERSION);
+#else
+       return QString::fromUtf8("\0");
+#endif
+}
+
 QString MOVIE_SAVER::ts2str(int64_t ts)
 {
 #if defined(USE_LIBAV) 
@@ -99,7 +119,7 @@ void MOVIE_SAVER::log_packet(const void *_fmt_ctx, const void *_pkt)
        const AVPacket *pkt = (const AVPacket *)_pkt;
        AVRational *time_base = &fmt_ctx->streams[pkt->stream_index]->time_base;
 
-       AGAR_DebugLog(AGAR_LOG_DEBUG, "pts:%s pts_time:%s dts:%s dts_time:%s duration:%s duration_time:%s stream_index:%d\n",
+       p_logger->debug_log(CSP_LOG_DEBUG, CSP_LOG_TYPE_MOVIE_SAVER, "pts:%s pts_time:%s dts:%s dts_time:%s duration:%s duration_time:%s stream_index:%d\n",
                   ts2str(pkt->pts).toLocal8Bit().constData(),
                   ts2timestr(pkt->pts, (void *)time_base).toLocal8Bit().constData(),
                   ts2str(pkt->dts).toLocal8Bit().constData(),
@@ -111,15 +131,19 @@ void MOVIE_SAVER::log_packet(const void *_fmt_ctx, const void *_pkt)
 }
 
 
-void MOVIE_SAVER::enqueue_video(QImage *p)
+void MOVIE_SAVER::enqueue_video(int frames, int width, int height, QImage *p)
 {
 #if defined(USE_MOVIE_SAVER)
        if(!recording) return;
        if(p == NULL) return;
-       uint32_t *pq;
-       QImage *pp = new QImage(*p);
-       //AGAR_DebugLog(AGAR_LOG_DEBUG, "Movie: Enqueue video data %d bytes %dx%d", pp->byteCount(), pp->width(), pp->height());
-       video_data_queue.enqueue(pp);
+       if(req_stop) return;
+       if((width  <= 0) || (height <= 0) || (frames <= 0)) return;
+       if((p->width() <= 0) || (p->height() <= 0)) return;
+       VIDEO_DATA *px = new VIDEO_DATA(frames, width, height, p);
+       //p_logger->debug_log(CSP_LOG_DEBUG, CSP_LOG_TYPE_MOVIE_SAVER, "Movie: Enqueue video data %dx%d %d bytes %dx%d %d frames", width, height, p->byteCount(), p->width(), p->height(), frames);
+       //video_queue_mutex->lock();
+       video_data_queue.enqueue(px);
+       //video_queue_mutex->unlock();
 #endif   
 }
 
@@ -128,24 +152,28 @@ bool MOVIE_SAVER::dequeue_video(uint32_t *p)
        //if(!recording) return false;
        if(p == NULL) return false;
 
-       QImage *pp = video_data_queue.dequeue();
+       VIDEO_DATA *pp = video_data_queue.dequeue();
 #if defined(USE_MOVIE_SAVER)
        if(pp == NULL) return false;
        int y;
-       int x;
        uint32_t *pq;
-       for(y = 0; y < _height; y++) {
-               if(y >= pp->height()) break;
-               pq = (uint32_t *)(pp->constScanLine(y));
-               memcpy(&(p[_width * y]), pq, ((_width * sizeof(uint32_t)) > pp->bytesPerLine()) ? pp->bytesPerLine() : _width * sizeof(uint32_t));
+       QImage *pp_i = &(pp->frame_data);
+       left_frames = pp->frames;
+       _width = pp->_width;
+       _height = pp->_height;
+       if((left_frames > 0) && (pp_i != NULL)){ 
+               for(y = 0; y < _height; y++) {
+                       if(y >= pp_i->height()) break;
+                       pq = (uint32_t *)(pp_i->constScanLine(y));
+                       my_memcpy(&(p[_width * y]), pq, (((uint)_width * sizeof(uint32_t)) > pp_i->bytesPerLine()) ? pp_i->bytesPerLine() : _width * sizeof(uint32_t));
+               }
+               video_size = _width * y;
+               //p_logger->debug_log(CSP_LOG_DEBUG, CSP_LOG_TYPE_MOVIE_SAVER, "Movie: Dequeue video data %d bytes", video_size);
        }
-       video_size = _width * y;
-       //AGAR_DebugLog(AGAR_LOG_DEBUG, "Movie: Dequeue video data %d bytes", pp->byteCount());
 #else
        video_size = 0;
 #endif   
        if(pp != NULL) delete pp;
-       
        return true;
 }
 
@@ -154,9 +182,11 @@ void MOVIE_SAVER::enqueue_audio(int16_t *p, int size)
 #if defined(USE_MOVIE_SAVER)
        if(!recording) return;
        if(p == NULL) return;
+       if(req_stop) return;
        QByteArray *pp = new QByteArray((const char *)p, size);
-       //AGAR_DebugLog(AGAR_LOG_DEBUG, "Movie: Enqueue audio data %d bytes", size);
+       //p_logger->debug_log(CSP_LOG_DEBUG, CSP_LOG_TYPE_MOVIE_SAVER, "Movie: Enqueue audio data %d bytes", size);
        audio_data_queue.enqueue(pp);
+//     audio_enqueue_count++;
 #endif   
 }
 
@@ -165,14 +195,24 @@ bool MOVIE_SAVER::dequeue_audio(int16_t *p)
        //if(!recording) return false;
        if(audio_data_queue.isEmpty()) return false;
        if(p == NULL) return false;
+       
        QByteArray *pp = audio_data_queue.dequeue();
+       int16_t *q = (int16_t *)pp->constData();
+       int32_t tmpd;
 #if defined(USE_MOVIE_SAVER)
        if(pp == NULL) return false;
        audio_size = pp->size();
        if(audio_size <= 0) return false;
-       memcpy(p, pp->constData(), audio_size);
+       // I expect to use SIMD.
+       for(int i = 0; i < (int)(audio_size / sizeof(int16_t)); i++) {
+               tmpd = q[i];
+               tmpd <<= 4;
+               tmpd = tmpd / 3;
+               tmpd >>= 3;
+               p[i] = tmpd;
+       }
+       //memcpy(p, pp->constData(), audio_size);
        audio_count++;
-       //AGAR_DebugLog(AGAR_LOG_DEBUG, "Movie: Dequeue audio data %d bytes", pp->size());
 #else
        audio_size = 0;
 #endif   
@@ -184,15 +224,17 @@ bool MOVIE_SAVER::dequeue_audio(int16_t *p)
 void MOVIE_SAVER::run()
 {
        bRunThread = true;
-       //AGAR_DebugLog(AGAR_LOG_DEBUG, "MOVIE THREAD: Start");
+       //p_logger->debug_log(CSP_LOG_DEBUG, CSP_LOG_TYPE_MOVIE_SAVER, "MOVIE THREAD: Start");
        int ret;
-       int fps_wait = (int)((1000.0 / (double)rec_fps) / 2.0);
+       int fps_wait = (int)((1000.0 / (double)rec_fps) / 4.0);
        int tmp_wait = fps_wait;
        bool need_audio_transcode = false;
        bool need_video_transcode = false;
        int i;
        int64_t total_packets_written = 0;
-       bool old_recording = false;
+       bool a_f, v_f;
+       a_f = v_f = false;
+       volatile bool old_recording = false;
        audio_remain = 0;
        video_remain = 0;
        audio_offset = 0;
@@ -201,13 +243,13 @@ void MOVIE_SAVER::run()
        n_encode_audio = 0;
        n_encode_video = 0;
        req_close = false;
-       
+       req_stop = false;
+       left_frames = 0;
        while(bRunThread) {
                if(recording) {
                        if(!bRunThread) break;
-                       if(old_recording != recording) {
-                               AGAR_DebugLog(AGAR_LOG_DEBUG, "MOVIE/Saver: Start to recording.");
-                               n_encode_video = n_encode_audio = 1;
+                       if(!old_recording) {
+                               //n_encode_video = n_encode_audio = 1;
                                audio_remain = 0;
                                video_remain = 0;
                                audio_offset = 0;
@@ -216,9 +258,19 @@ void MOVIE_SAVER::run()
                                video_count = 0;
                                audio_count = 0;
                                req_close = false;
+                               req_stop = false;
+                               left_frames = 0;
+                               if(!do_open_main()) {
+                                       recording = false;
+                                       goto _next_turn;
+                               }
+                               p_logger->debug_log(CSP_LOG_DEBUG, CSP_LOG_TYPE_MOVIE_SAVER, "MOVIE/Saver: Start to recording.");
+                               old_recording = true;
+                               a_f = v_f = false;
                        }
                        if(audio_remain <= 0) {
-                               if(audio_data_queue.isEmpty()) goto _video;
+                               a_f = audio_data_queue.isEmpty();
+                               if(a_f) goto _video;
                                dequeue_audio(audio_frame_buf);
                                audio_remain = audio_size;
                                audio_offset = 0;
@@ -226,9 +278,11 @@ void MOVIE_SAVER::run()
                        }
                _video:
                        {
-                               if(video_data_queue.isEmpty())
+                               v_f = video_data_queue.isEmpty();
+                               if(v_f)
                                        goto _write_frame;
-                               dequeue_video(video_frame_buf);
+                               if(left_frames <= 0) dequeue_video(video_frame_buf);
+                               left_frames--;
                                video_remain = video_size;
                                video_offset = 0;
                                need_video_transcode = true;
@@ -239,20 +293,20 @@ void MOVIE_SAVER::run()
                                                                                                                audio_st.next_pts, audio_st.st->codec->time_base) <= 0)) {
                                n_encode_video = write_video_frame();
                                if(n_encode_video < 0) {
-                                       AGAR_DebugLog(AGAR_LOG_DEBUG, "MOVIE/Saver: Something wrong with encoding video.");
+                                       p_logger->debug_log(CSP_LOG_DEBUG, CSP_LOG_TYPE_MOVIE_SAVER, "MOVIE/Saver: Something wrong with encoding video.");
                                        goto _final;
                                }
                        } else {
                                n_encode_audio = write_audio_frame();
                                if(n_encode_audio < 0) {
-                                       AGAR_DebugLog(AGAR_LOG_DEBUG, "MOVIE/Saver: Something wrong with encoding audio.");
+                                       p_logger->debug_log(CSP_LOG_DEBUG, CSP_LOG_TYPE_MOVIE_SAVER, "MOVIE/Saver: Something wrong with encoding audio.");
                                        goto _final;
                                }
                        }
                        if (ret < 0 && ret != AVERROR_EOF) {
                                char errbuf[128];
                                av_strerror(ret, errbuf, sizeof(errbuf));
-                               AGAR_DebugLog(AGAR_LOG_INFO, "Error while filtering: %s\n", (const char *)errbuf);
+                               p_logger->debug_log(CSP_LOG_INFO, CSP_LOG_TYPE_MOVIE_SAVER, "Error while filtering: %s\n", (const char *)errbuf);
                                goto _final;
                        }
                        
@@ -260,11 +314,12 @@ void MOVIE_SAVER::run()
                        //print_report(0, timer_start, cur_time);
                        if(req_close) {
                                do_close_main();
+                               need_video_transcode = need_audio_transcode = false;
+                               old_recording = false;
                        }
                }
        _next_turn:
-               //printf("%d\n", req_close);
-               old_recording = recording;
+               //if(req_stop && a_f && v_f) req_close = true;
                if(!bRunThread) break;
                if(need_video_transcode || need_audio_transcode) {
                        need_video_transcode = need_audio_transcode = false;
@@ -278,22 +333,23 @@ void MOVIE_SAVER::run()
                        tmp_wait -= fps_wait;
                }
                if(tmp_wait <= 0) {
-                       fps_wait = (int)((1000.0 / (double)rec_fps) / 2.0);
+                       fps_wait = (int)((1000.0 / (double)rec_fps) / 4.0);
                        //fps_wait = 10;
                        tmp_wait = fps_wait;
                }
-               old_recording = recording;
                if(req_close) {
                        do_close_main();
+                       old_recording = false;
                }
                continue;
        _final:
-               old_recording = recording;
+               req_close = true;
                do_close_main();
-               req_close = false;
+               old_recording = false;
        }
-       AGAR_DebugLog(AGAR_LOG_DEBUG, "MOVIE: Exit thread.");
+       p_logger->debug_log(CSP_LOG_DEBUG, CSP_LOG_TYPE_MOVIE_SAVER, "MOVIE: Exit thread.");
        if(recording) {
+               req_close = true;
                do_close_main();
        }
 }
@@ -308,6 +364,7 @@ void MOVIE_SAVER::do_exit()
 {
        bRunThread = false;
        req_close = true;
+       req_stop = true;
 }
 
 void MOVIE_SAVER::do_set_record_fps(int fps)
@@ -317,13 +374,11 @@ void MOVIE_SAVER::do_set_record_fps(int fps)
 
 void MOVIE_SAVER::do_set_width(int width)
 {
-       //printf("width = %d -> %d\n", _width, width);
        _width = width;
 }
 
 void MOVIE_SAVER::do_set_height(int height)
 {
-       //printf("height = %d -> %d\n", _height, height);
        _height = height;
 }