Browse Source

优化网络流读取失败时,重新执行打开流操作

huihui 3 months ago
parent
commit
5ef4cd4dc3

+ 5 - 0
module/VideoPlayer/src/VideoPlayer/VideoPlayer.cpp

@@ -625,6 +625,11 @@ fprintf(stderr, "%s mIsQuit=%d mIsPause=%d file_path=%s \n", __FUNCTION__, mIsQu
             break;
         }
 
+        if (m_is_live_mode && mIsReadError)
+        {
+            break; //网络流读取失败,需要重新打开流
+        }
+
         if (seek_req)
         {
             int stream_index = -1;

+ 1 - 1
module/VideoPlayer/src/VideoPlayer/VideoPlayer.h

@@ -30,7 +30,7 @@ extern "C"
 ///启用滤镜,用于旋转带角度的视频
 #define CONFIG_AVFILTER 1
 
-#include "../types.h"
+#include "util/util.h"
 #include "util/thread.h"
 #include "PcmPlayer/PcmPlayer.h"
 #include "frame/AudioFrame/AACFrame.h"

+ 3 - 0
module/VideoPlayer/src/frame/VideoFrame/VideoEncodedFrame.cpp

@@ -1,10 +1,13 @@
 #include "VideoEncodedFrame.h"
 
+#include "util/util.h"
+
 VideoEncodedFrame::VideoEncodedFrame()
 {
     mNalu = nullptr;
     mPts = 0;
     mIsKeyFrame = false;
+    m_timestamp_ms = Util::GetUtcTime();
 }
 
 VideoEncodedFrame::~VideoEncodedFrame()

+ 0 - 62
module/VideoPlayer/src/types.cpp

@@ -1,62 +0,0 @@
-#include "types.h"
-
-#include <time.h>
-
-#if defined(WIN32)
-    #include <WinSock2.h>
-    #include <Windows.h>
-    #include <direct.h>
-    #include <io.h> //C (Windows)    access
-#else
-    #include <sys/time.h>
-    #include <stdio.h>
-    #include <unistd.h>
-
-void Sleep(long mSeconds)
-{
-    usleep(mSeconds * 1000);
-}
-
-#endif
-
-void mSleep(int mSecond)
-{
-#if defined(WIN32)
-    Sleep(mSecond);
-#else
-    usleep(mSecond * 1000);
-#endif
-}
-
-
-int64_t getTimeStamp_MilliSecond()
-{
-
-    int mSecond = 0; //当前毫秒数
-
-#if defined(WIN32)
-
-    SYSTEMTIME sys;
-    GetLocalTime( &sys );
-
-    mSecond = sys.wMilliseconds;
-
-#else
-
-    struct timeval    tv;
-    struct timezone tz;
-
-    struct tm         *p;
-
-    gettimeofday(&tv, &tz);
-    p = localtime(&tv.tv_sec);
-
-    mSecond = tv.tv_usec / 1000;
-
-
-#endif
-
-    int64_t timeStamp = ((int64_t)time(NULL)) * 1000 + mSecond;
-
-    return timeStamp;
-}

+ 0 - 26
module/VideoPlayer/src/types.h

@@ -1,26 +0,0 @@
-#pragma once
-
-#include <stdio.h>
-#include <stdint.h>
-
-#if defined(WIN32)
-    #include <WinSock2.h>
-    #include <Windows.h>
-    #include <direct.h>
-    #include <io.h> //C (Windows)    access
-#else
-    #include <sys/time.h>
-    #include <stdio.h>
-    #include <unistd.h>
-#endif
-
-#if defined(WIN32)
-#else
-    void Sleep(long mSeconds);
-#endif
-
-void mSleep(int mSecond);
-
-int64_t getTimeStamp_MilliSecond(); //获取时间戳(毫秒)
-
-

+ 125 - 0
module/VideoPlayer/src/util/util.cpp

@@ -0,0 +1,125 @@
+#include "util.h"
+
+#include <time.h>
+
+#if defined(WIN32)
+    #include <WinSock2.h>
+    #include <Windows.h>
+    #include <direct.h>
+    #include <io.h> //C (Windows)    access
+#else
+    #include <sys/time.h>
+    #include <stdio.h>
+    #include <unistd.h>
+
+void Sleep(long mSeconds)
+{
+    usleep(mSeconds * 1000);
+}
+
+#endif
+
+void mSleep(int mSecond)
+{
+#if defined(WIN32)
+    Sleep(mSecond);
+#else
+    usleep(mSecond * 1000);
+#endif
+}
+
+namespace Util
+{
+    
+uint64_t GetUtcTime()
+{
+    /* 获取本地utc时间 */
+    struct timeval tv;
+    gettimeofday(&tv, NULL);
+
+//    /* utc时间 单位 秒*/
+//    long timestamp = tv.tv_sec;
+
+    /* utc时间 单位 毫秒 */
+    uint64_t timestamp = (uint64_t)tv.tv_sec*1000 + tv.tv_usec/1000;
+
+//    /* utc时间 单位 微秒 */
+//    long long timestamp = tv.tv_sec*1000*1000 + tv.tv_usec;
+
+    return timestamp;
+}
+
+uint64_t GetLocalTime() 
+{
+    struct timeval tv;
+    gettimeofday(&tv, NULL);
+    uint64_t current_time_ms = (uint64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000;
+    // printf("Current timestamp in milliseconds: %llu\n", current_time_ms);
+    return current_time_ms;
+}
+
+uint64_t GetTodayUtcTime()
+{
+//    auto getTimeStr = [=](struct tm* t)
+//    {
+//        char tStr[50] = {0};
+//        snprintf(tStr, sizeof(tStr), "%04d-%02d-%02d %02d:%02d:%02d", (1900 + t->tm_year), ( 1 + t->tm_mon), t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec);
+//        return std::string(tStr);
+//    };
+
+    time_t ts = time(nullptr);
+    struct tm* tUTC = gmtime(&ts);
+//    std::cout<<"UTC time:"<<getTimeStr(tUTC)<<std::endl;
+//    struct tm* tLocal = localtime(&ts);
+//    std::cout<<"Local time:"<<getTimeStr(tLocal)<<std::endl;
+
+#if 1
+    tUTC->tm_hour = 0;
+    tUTC->tm_min = 0;
+    tUTC->tm_sec = 0;
+    uint64_t timestamp = (uint64_t)mktime(tUTC) * 1000;
+#else
+    uint64_t timestamp = (AppConfig::GetUtcTime() - tUTC->tm_hour * 3600000 - tUTC->tm_min * 60000 - tUTC->tm_sec * 1000);
+#endif
+
+    return timestamp;
+}
+
+uint64_t GetUtcTimeInCurWeek()
+{
+    /* 获取本地utc时间 */
+    struct timeval tv;
+    gettimeofday(&tv, NULL);
+
+    /* utc时间 单位 秒*/
+    uint64_t timestamp = tv.tv_usec / 1000; //获取毫秒数
+
+    time_t ts = time(nullptr);
+    struct tm* tUTC = gmtime(&ts);
+    timestamp += (tUTC->tm_hour * 3600000 + tUTC->tm_min * 60000 + tUTC->tm_sec * 1000); //今天的时间戳
+    timestamp += tUTC->tm_wday * 86400000; //本周的时间戳
+
+    return timestamp;
+}
+
+std::string getLocalTimeInStringFormat()
+{
+    struct tm nowtime;
+    struct timeval tv;
+    char time_now[128] = {0};
+    gettimeofday(&tv, NULL);
+    localtime_r(&tv.tv_sec, &nowtime);
+
+    sprintf(time_now, "%4d%02d%02d%02d%02d%02d%03d",
+            nowtime.tm_year + 1900,
+            nowtime.tm_mon + 1,
+            nowtime.tm_mday,
+            nowtime.tm_hour,
+            nowtime.tm_min,
+            nowtime.tm_sec,
+            (int) (tv.tv_usec / 1000));
+
+    return std::string(time_now);
+}
+
+};

+ 34 - 0
module/VideoPlayer/src/util/util.h

@@ -0,0 +1,34 @@
+#pragma once
+
+#include <string>
+#include <stdio.h>
+#include <stdint.h>
+
+#if defined(WIN32)
+    #include <WinSock2.h>
+    #include <Windows.h>
+    #include <direct.h>
+    #include <io.h> //C (Windows)    access
+#else
+    #include <sys/time.h>
+    #include <stdio.h>
+    #include <unistd.h>
+#endif
+
+#if defined(WIN32)
+#else
+    void Sleep(long mSeconds);
+#endif
+
+void mSleep(int mSecond);
+namespace Util
+{
+    void mSleep(int mSecond);
+
+    uint64_t GetUtcTime(); //获取UTC时间戳(毫秒)
+    uint64_t GetLocalTime(); //获取本地时间戳(毫秒)
+    uint64_t GetTodayUtcTime(); //获取今天0点的UTC时间戳(毫秒)
+    uint64_t GetUtcTimeInCurWeek(); //获取当前时间相对于本周的UTC时间戳(毫秒)
+
+    std::string getLocalTimeInStringFormat();
+};