zhangmeng
2021-05-25 eb747decf2ddb1f8045a95c31146e1e3fae2640f
capture.go
@@ -5,8 +5,9 @@
   "fmt"
   "time"
   "basic.com/valib/godraw.git"
   "basic.com/valib/goffmpeg.git"
   "basic.com/valib/godraw.git"
   "github.com/disintegration/imaging"
)
@@ -38,8 +39,8 @@
   InvalidFileExt PicExt = PicExt("")
)
// Capture pic
func CaptureGB28181(soFile string, url string, maxTry int) ([]byte, error) {
// capture pic
func captureGB28181(soFile string, url string, maxTry int) ([]byte, error) {
   var ret []byte
   var err error
@@ -49,14 +50,14 @@
      return nil, err
   }
   reterr := errors.New(fmt.Sprintf("try %d times to capture image, is url correct?", maxTry))
   for i := 0; i < maxTry; i++ {
      ret = goffmpeg.GetGBJpg(url)
      if len(ret) > 0 {
         reterr = nil
         break
      }
   reterr := errors.New(fmt.Sprintf("gb28181 try %d times to capture image, is url correct?", maxTry))
   // for i := 0; i < maxTry; i++ {
   ret = goffmpeg.GetGBJpg(url, maxTry)
   if len(ret) > 0 {
      reterr = nil
      // break
   }
   // }
   goffmpeg.FreeFFmpeg()
@@ -67,6 +68,10 @@
func Capture(soFile string, m StreamMode, url string, ext PicExt, w, h, maxTry int) ([]byte, error) {
   if m != GB28181 && m != Rtsp {
      return nil, errors.New("there is no this mode, try capture.Rtsp/capture.GB28181")
   }
   if m == GB28181 {
      return captureGB28181(soFile, url, maxTry)
   }
   if ext == InvalidFileExt {
@@ -101,11 +106,9 @@
   gf.BuildDecoder()
   tryTime := 0
   interval := 40 // ms
   // 保证有一个关键帧
   maxTry += 50
   if maxTry > 60 {
      maxTry = 60
   }
   maxTry *= 1000
   var bgrData, jpgData []byte
   var err error
@@ -127,11 +130,11 @@
         }
         break
      } else {
         tryTime++
         tryTime += interval
         if tryTime > maxTry {
            break
         }
         time.Sleep(800 * time.Millisecond)
         time.Sleep(time.Duration(interval) * time.Millisecond)
      }
   }