From 37f7ba99eab3b0338a571419faa6adeafa9b7d30 Mon Sep 17 00:00:00 2001 From: wangjian Date: Sun, 23 Oct 2022 13:33:16 +0800 Subject: [PATCH] =?UTF-8?q?1=E3=80=81add=20video=20stream=20demo=202?= =?UTF-8?q?=E3=80=81add=20config=20read=20string=20function?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- example/video_streaming/ap/main.go | 60 +++++++++++++++++++++++++------------- example/video_streaming/sf/main.go | 7 +++-- 2 files changed, 44 insertions(+), 23 deletions(-) diff --git a/example/video_streaming/ap/main.go b/example/video_streaming/ap/main.go index 6b7c4ea..34c59ad 100644 --- a/example/video_streaming/ap/main.go +++ b/example/video_streaming/ap/main.go @@ -7,13 +7,12 @@ import ( "fmt" "git.hpds.cc/Component/network/log" "git.hpds.cc/pavement/hpds_node" + "github.com/disintegration/imaging" + ffmpeg "github.com/u2takey/ffmpeg-go" "io" "os" "strconv" "time" - - "github.com/disintegration/imaging" - ffmpeg "github.com/u2takey/ffmpeg-go" ) func main() { @@ -37,7 +36,7 @@ func main() { if len(img) > 0 { //log.Printf("send img %s", HexToString(img)) str := fmt.Sprintf("%d", time.Now().UnixNano()/1e6) - tick := time.Now().Format("20060102150405") + str[10:] + tick := fmt.Sprintf("%d_%s", id, time.Now().Format("20060102150405")+str[10:]) go WriteFile(fmt.Sprintf("./%s.jpeg", tick), img) n, err := ap.Write(img) if err != nil { @@ -50,24 +49,45 @@ func main() { } video := VideoImage{} ffStream := ffmpeg.Input("rtmp://ns8.indexforce.com/home/mystream") - i := 0 - quit := make(chan int) - //tick := time.Tick(time.Second) - select { - case <-quit: - fmt.Println("quit") - return - case <-time.After(time.Millisecond * 42): - if i%24 == 0 { - img, err := video.ExtractImageBytes(ffStream, 1) - if err != nil { - fmt.Printf("ExtractImage64 error: %v\n", err) + //i := 0 + //quit := make(chan int) + tick := time.NewTicker(time.Millisecond * 42) + defer tick.Stop() + go func() { + i := 0 + for range tick.C { + if i%24 == 0 { + img, err := video.ExtractImageBytes(ffStream, i) + if err != nil { + fmt.Printf("ExtractImage64 error: %v\n", err) + } + fmt.Printf("i ========== %d \n", i) + send(i, img) + //log.Debugf("send img %#v", img) } - send(i, img) - //log.Debugf("send img %#v", img) + i++ } - i++ - } + }() + select {} + //ch := make(chan int, 1) + // + //select { + //case <-quit: + // fmt.Println("quit") + // close(ch) + // return + //case <-tick.C: + // if i%24 == 0 { + // img, err := video.ExtractImageBytes(ffStream, i) + // if err != nil { + // fmt.Printf("ExtractImage64 error: %v\n", err) + // } + // fmt.Printf("i ========== %d \n", i) + // send(i, img) + // //log.Debugf("send img %#v", img) + // } + // i++ + //} } func genSha1(buf []byte) string { diff --git a/example/video_streaming/sf/main.go b/example/video_streaming/sf/main.go index e3076ae..3231e70 100644 --- a/example/video_streaming/sf/main.go +++ b/example/video_streaming/sf/main.go @@ -19,7 +19,7 @@ var ( func main() { sf := hpds_node.NewStreamFunction( "echo-sf", - hpds_node.WithMqAddr("localhost:27188"), + hpds_node.WithMqAddr("localhost:27187"), hpds_node.WithObserveDataTags(ImageDataKey), hpds_node.WithCredential("token:z1"), ) @@ -52,13 +52,14 @@ func Handler(img []byte) (byte, []byte) { //} else { // fmt.Println("GO: Run bindgen -- infer FAILED") //} + id := atomic.AddUint64(&counter, 1) str := fmt.Sprintf("%d", time.Now().UnixNano()/1e6) tick := time.Now().Format("20060102150405") + str[10:] - go WriteFile(fmt.Sprintf("./%s.jpeg", tick), img) + go WriteFile(fmt.Sprintf("./%d_%s.jpeg", id, tick), img) // print logs hash := genSha1(img) - log.Printf("received image-%d hash %v, img_size=%d \n", atomic.AddUint64(&counter, 1), hash, len(img)) + log.Printf("received image-%d hash %v, img_size=%d \n", id, hash, len(img)) return 0x11, nil }