From c60d61c48c7a1e7b693d4c3f6427e3b616d1f471 Mon Sep 17 00:00:00 2001
From: zhangmeng <775834166@qq.com>
Date: 星期一, 21 十月 2019 12:29:33 +0800
Subject: [PATCH] update ffmpeg

---
 csrc/buz/recorder.cpp |   58 +++++++++++++++++++++++++++++++++++++++++-----------------
 1 files changed, 41 insertions(+), 17 deletions(-)

diff --git a/csrc/buz/recorder.cpp b/csrc/buz/recorder.cpp
index cf2fa1f..d3d9a98 100644
--- a/csrc/buz/recorder.cpp
+++ b/csrc/buz/recorder.cpp
@@ -78,7 +78,8 @@
             out_ = new FormatOut(in_->getFPS(), "mp4");
             
             int pid = getpid();
-            file_path_ = dir_ + "/" + sole::uuid4().base62() + "-" + std::to_string(pid) + ".mp4";
+            std::string filename(sole::uuid4().base62() + "-" + std::to_string(pid) + ".mp4");
+            file_path_ = dir_ + "/" + filename;
 
             auto v = in_->getStream(AVMEDIA_TYPE_VIDEO);
             if (!v){
@@ -88,10 +89,18 @@
             if (!audio){
                 a = NULL;
             }
-            auto ret = out_->JustWriter(v, a, file_path_.c_str());
+            bool ret = out_->JustWriter(v, a, file_path_.c_str());
             if (ret){
-                logIt("start record file: %s", file_path_.c_str());
+                logIt("start record file: %s", file_path_.c_str());                
                 return 0;
+            }else{
+                file_path_ = "./" + filename;
+                ret = out_->JustWriter(v, a, file_path_.c_str());
+                logIt("failed in dir %s, try file %s to start record file", dir_.c_str(), file_path_.c_str());                
+                if (ret){
+                    logIt("start record file: %s", file_path_.c_str());                
+                    return 0;
+                }
             }
 
             logIt("failed to start record: %s", file_path_.c_str());
@@ -105,12 +114,18 @@
             }
 
             int pid = getpid();
-            file_path_ = dir_ + "/" + sole::uuid4().base62() + "-" + std::to_string(pid) + ".hevc";
+            std::string filename(sole::uuid4().base62() + "-" + std::to_string(pid) + ".mp4");
+            file_path_ = dir_ + "/" + filename;
 
             fp_ = fopen(file_path_.c_str(), "wb");
             if (!fp_){
-                logIt("write hevc open file error: %s", file_path_.c_str());
-                return -1;
+                file_path_ = "./" + filename;
+                logIt("failed in dir %s, try file %s to start record hevc file", dir_.c_str(), file_path_.c_str());                
+                fp_ = fopen(file_path_.c_str(), "wb");
+                if (!fp_){
+                    logIt("failed start record hevc file: %s", file_path_.c_str());
+                    return -1;
+                }
             }
             logIt("start record file: %s", file_path_.c_str());
 
@@ -225,15 +240,22 @@
 
             int ret = mux_hevc(fp, file_path_.c_str());
             fclose(fp);
-            if (remove(hevc_file.c_str()) != 0){
-                logIt("mux hevc remove file %s failed", hevc_file.c_str());
+            if (ret == 0){
+                if (remove(hevc_file.c_str()) != 0){
+                    logIt("mux hevc remove file %s failed", hevc_file.c_str());
+                }
+            }else{
+                logIt("mux hevc to mp4 error, use raw hevc");
+                file_path_ = hevc_file;
             }
 
-            return ret;
+            return 0;
         }
 
         static int read_buffer(void *opaque, uint8_t *buf, int buf_size){
             FILE *fp_open = (FILE*)opaque;
+            if (!fp_open) logIt("mux hevc open file error");
+
         	if(!feof(fp_open)){
         		int true_size=fread(buf,1,buf_size,fp_open);
         		return true_size;
@@ -250,8 +272,14 @@
                 return -1;
             }
 
-            if (in->openWithCustomIO(fp, read_buffer) < 0) {
-                logIt("mux hevc open custom io error");
+            int tryTime = 0;
+            while (in->openWithCustomIO(fp, read_buffer) < 0) {
+                usleep(10000);
+                if (tryTime++ < 100){
+                    logIt("mux hevc mux: %d failed open custom io %s, try again", tryTime, outfile);
+                    continue;
+                }
+                logIt("mux hevc try %d time to open custom io, failed", tryTime);
                 return -2;
             }
             if (in->open(NULL, NULL) < 0){
@@ -293,14 +321,10 @@
 
         void Recorder::end_writer(){
 
-            int ret = -1;
             if (in_->IsHEVC()){
-                ret = end_write_hevc();
+                end_write_hevc();
             }else{
-                ret = end_write_h264();
-            }
-            if (ret < 0){
-                logIt("end write file : %s, h265: %d, failed", file_path_.c_str(), in_->IsHEVC());
+                end_write_h264();
             }
 
             logIt("finished record : %s frames: %d", file_path_.c_str(), cur_frame);

--
Gitblit v1.8.0