video.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // Copyright 2022-2023 EMQ Technologies Co., Ltd.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package main
  15. import (
  16. "bytes"
  17. "fmt"
  18. "os"
  19. "time"
  20. ffmpeg "github.com/u2takey/ffmpeg-go"
  21. "github.com/lf-edge/ekuiper/internal/conf"
  22. "github.com/lf-edge/ekuiper/pkg/api"
  23. "github.com/lf-edge/ekuiper/pkg/cast"
  24. )
  25. const (
  26. RTSP_DEFAULT_INTERVAL = 10000
  27. FRAMENUMBER = 5
  28. )
  29. type VideoPullSource struct {
  30. url string
  31. interval int
  32. }
  33. func (rps *VideoPullSource) Configure(_ string, props map[string]interface{}) error {
  34. if u, ok := props["url"]; ok {
  35. if p, ok := u.(string); ok {
  36. rps.url = p
  37. }
  38. }
  39. rps.interval = RTSP_DEFAULT_INTERVAL
  40. if i, ok := props["interval"]; ok {
  41. i1, err := cast.ToInt(i, cast.CONVERT_SAMEKIND)
  42. if err != nil {
  43. return fmt.Errorf("not valid interval value %v", i1)
  44. } else {
  45. rps.interval = i1
  46. }
  47. }
  48. return nil
  49. }
  50. func (rps *VideoPullSource) Open(ctx api.StreamContext, consumer chan<- api.SourceTuple, errCh chan<- error) {
  51. rps.initTimerPull(ctx, consumer, errCh)
  52. }
  53. func (rps *VideoPullSource) Close(ctx api.StreamContext) error {
  54. logger := ctx.GetLogger()
  55. logger.Infof("Closing video pull source")
  56. return nil
  57. }
  58. func (rps *VideoPullSource) initTimerPull(ctx api.StreamContext, consumer chan<- api.SourceTuple, _ chan<- error) {
  59. ticker := time.NewTicker(time.Millisecond * time.Duration(rps.interval))
  60. logger := ctx.GetLogger()
  61. defer ticker.Stop()
  62. for {
  63. select {
  64. case <-ticker.C:
  65. rcvTime := conf.GetNow()
  66. buf := rps.readFrameAsJpeg(ctx)
  67. results, e := ctx.DecodeIntoList(buf.Bytes())
  68. meta := make(map[string]interface{})
  69. if e != nil {
  70. logger.Errorf("Invalid data format, cannot decode %s with error %s", string(buf.Bytes()), e)
  71. return
  72. }
  73. for _, result := range results {
  74. select {
  75. case consumer <- api.NewDefaultSourceTupleWithTime(result, meta, rcvTime):
  76. logger.Debugf("send data to device node")
  77. case <-ctx.Done():
  78. return
  79. }
  80. }
  81. case <-ctx.Done():
  82. return
  83. }
  84. }
  85. }
  86. func (rps *VideoPullSource) readFrameAsJpeg(ctx api.StreamContext) *bytes.Buffer {
  87. logger := ctx.GetLogger()
  88. buf := bytes.NewBuffer(nil)
  89. err := ffmpeg.Input(rps.url).
  90. Filter("select", ffmpeg.Args{fmt.Sprintf("gte(n,%d)", FRAMENUMBER)}).
  91. Output("pipe:", ffmpeg.KwArgs{"vframes": 1, "format": "image2", "vcodec": "mjpeg"}).
  92. WithOutput(buf, os.Stdout).
  93. Run()
  94. if err != nil {
  95. logger.Errorf("ffmpeg exec error %v", err)
  96. return buf
  97. }
  98. return buf
  99. }
  100. func Video() api.Source {
  101. return &VideoPullSource{}
  102. }