From c5a01eed95f1837e93fee27bce4da78c79f4ed10 Mon Sep 17 00:00:00 2001
From: zhangmeng <775834166@qq.com>
Date: 星期二, 12 十一月 2019 17:41:43 +0800
Subject: [PATCH] update

---
 csrc/thirdparty/gb28181/include/PsToEs.hpp |   49 ++++++++++++++++++++++++++++++++++++++++---------
 1 files changed, 40 insertions(+), 9 deletions(-)

diff --git a/csrc/thirdparty/gb28181/include/PsToEs.hpp b/csrc/thirdparty/gb28181/include/PsToEs.hpp
index 51f613a..30a1903 100644
--- a/csrc/thirdparty/gb28181/include/PsToEs.hpp
+++ b/csrc/thirdparty/gb28181/include/PsToEs.hpp
@@ -5,6 +5,7 @@
 #include <pthread.h>
 
 #include "librtsp.h"
+#include <sys/time.h>
 
 
 using namespace std;
@@ -14,7 +15,7 @@
 public:
     MyQueue():mtx(PTHREAD_MUTEX_INITIALIZER), cond(PTHREAD_COND_INITIALIZER){
         t.tv_sec = 0;
-        t.tv_nsec = 20000000;
+        t.tv_nsec = 0;
     }
 
     ~MyQueue() {
@@ -39,9 +40,16 @@
 
     T pop() {
         pthread_mutex_lock(&mtx);
-        while (q.empty()) {
-            pthread_cond_wait(&cond, &mtx);
+        if (q.empty()) {
+            gettimeofday(&now, NULL);
+            t.tv_sec = now.tv_sec + 3;
+            t.tv_nsec = now.tv_usec * 1000;
+//            pthread_cond_wait(&cond, &mtx);
             pthread_cond_timedwait(&cond, &mtx, &t);
+        }
+        if (q.empty()) {
+            pthread_mutex_unlock(&mtx);
+            return 0;
         }
         T value = q.front();
         q.pop_front();
@@ -67,11 +75,22 @@
 		while (!q.empty()) q.pop_front();
 		pthread_mutex_unlock(&mtx);
     }
+	
+	void clearAll(std::function<void(T)> fn){
+		pthread_mutex_lock(&mtx);
+		while (!q.empty()){
+			T value = q.front();
+			fn(value);
+			q.pop_front();
+		} 
+		pthread_mutex_unlock(&mtx);
+	}
 private:
     deque<T> q;
     pthread_mutex_t mtx;
     pthread_cond_t cond;
     timespec t;
+    struct timeval now;
 };
 
 typedef struct _buffInfo {
@@ -87,7 +106,11 @@
 
 	~GB28181API(){
 		printf("GB28181API end!\n");
-		m_rtpQueue.clearAll();
+		// m_rtpQueue.clearAll();
+		m_rtpQueue.clearAll([](frameBuffInfo *info){
+			delete[] info->buff;
+			delete info;
+		});
 		deleteCamera();
 	}
 
@@ -115,7 +138,11 @@
 			//浠庣紦瀛樹腑鑾峰彇buffinfo
 			frameBuffInfo *buffinfo = _this->m_rtpQueue.pop();
 //			printf(" m_rtpQueue.pop after \n");
+            if(buffinfo != nullptr){
 			diff = len - buffinfo->buffLen;
+            }else{
+                return 0;
+            }
 
 			//甯ч暱澶т簬bufsize
 			if (diff < 0) {
@@ -174,20 +201,24 @@
 		}
 	}
 
-	void addCamera(string &rtsp){
+	long addCamera(string &rtsp){
 //		long userdata = 1001;//
-		printf("RTSPSTREAM_Open\n");
 		handle = RTSPSTREAM_Open(rtsp.c_str(), streamCallBack, (long)this);
-//		return handle;
+		printf("RTSPSTREAM_Open, handle:%ld \n", handle);
+		return handle;
 	}
 
 	void deleteCamera(){
 		printf("RTSPSTREAM_Close\n");
-		RTSPSTREAM_Close(handle);
+		if(handle != -1){
+			RTSPSTREAM_Close(handle);
+		}
+
+		handle = -1;
 	}	
 private:
 	MyQueue<frameBuffInfo *> m_rtpQueue;
-	long handle;
+	long handle = -1;
 };
 
 

--
Gitblit v1.8.0