video.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. "github.com/lf-edge/ekuiper/internal/conf"
  21. "github.com/lf-edge/ekuiper/pkg/api"
  22. "github.com/lf-edge/ekuiper/pkg/cast"
  23. ffmpeg "github.com/u2takey/ffmpeg-go"
  24. )
  25. const RTSP_DEFAULT_INTERVAL = 10000
  26. const FRAMENUMBER = 5
  27. type VideoPullSource struct {
  28. url string
  29. interval int
  30. }
  31. func (rps *VideoPullSource) Configure(_ string, props map[string]interface{}) error {
  32. if u, ok := props["url"]; ok {
  33. if p, ok := u.(string); ok {
  34. rps.url = p
  35. }
  36. }
  37. rps.interval = RTSP_DEFAULT_INTERVAL
  38. if i, ok := props["interval"]; ok {
  39. i1, err := cast.ToInt(i, cast.CONVERT_SAMEKIND)
  40. if err != nil {
  41. return fmt.Errorf("not valid interval value %v", i1)
  42. } else {
  43. rps.interval = i1
  44. }
  45. }
  46. return nil
  47. }
  48. func (rps *VideoPullSource) Open(ctx api.StreamContext, consumer chan<- api.SourceTuple, errCh chan<- error) {
  49. rps.initTimerPull(ctx, consumer, errCh)
  50. }
  51. func (rps *VideoPullSource) Close(ctx api.StreamContext) error {
  52. logger := ctx.GetLogger()
  53. logger.Infof("Closing video pull source")
  54. return nil
  55. }
  56. func (rps *VideoPullSource) initTimerPull(ctx api.StreamContext, consumer chan<- api.SourceTuple, _ chan<- error) {
  57. ticker := time.NewTicker(time.Millisecond * time.Duration(rps.interval))
  58. logger := ctx.GetLogger()
  59. defer ticker.Stop()
  60. for {
  61. select {
  62. case <-ticker.C:
  63. rcvTime := conf.GetNow()
  64. buf := rps.readFrameAsJpeg(ctx)
  65. result, e := ctx.Decode(buf.Bytes())
  66. meta := make(map[string]interface{})
  67. if e != nil {
  68. logger.Errorf("Invalid data format, cannot decode %s with error %s", string(buf.Bytes()), e)
  69. return
  70. }
  71. select {
  72. case consumer <- api.NewDefaultSourceTupleWithTime(result, meta, rcvTime):
  73. logger.Debugf("send data to device node")
  74. case <-ctx.Done():
  75. return
  76. }
  77. case <-ctx.Done():
  78. return
  79. }
  80. }
  81. }
  82. func (rps *VideoPullSource) readFrameAsJpeg(ctx api.StreamContext) *bytes.Buffer {
  83. logger := ctx.GetLogger()
  84. buf := bytes.NewBuffer(nil)
  85. err := ffmpeg.Input(rps.url).
  86. Filter("select", ffmpeg.Args{fmt.Sprintf("gte(n,%d)", FRAMENUMBER)}).
  87. Output("pipe:", ffmpeg.KwArgs{"vframes": 1, "format": "image2", "vcodec": "mjpeg"}).
  88. WithOutput(buf, os.Stdout).
  89. Run()
  90. if err != nil {
  91. logger.Errorf("ffmpeg exec error %v", err)
  92. return buf
  93. }
  94. return buf
  95. }
  96. func Video() api.Source {
  97. return &VideoPullSource{}
  98. }