video.go 2.8 KB

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