flv.go 769 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package jessica
  2. import (
  3. "github.com/gobwas/ws"
  4. "github.com/gobwas/ws/wsutil"
  5. . "m7s.live/engine/v4"
  6. "m7s.live/engine/v4/codec"
  7. "m7s.live/engine/v4/util"
  8. )
  9. type JessicaFLV struct {
  10. JessicaBase
  11. }
  12. func (j *JessicaFLV) WriteFLVTag(tag FLVFrame) {
  13. if j.IsWebSocket {
  14. if err := ws.WriteHeader(j, ws.Header{
  15. Fin: true,
  16. OpCode: ws.OpBinary,
  17. Length: int64(util.SizeOfBuffers(tag)),
  18. }); err != nil {
  19. j.Stop()
  20. return
  21. }
  22. }
  23. if _, err := tag.WriteTo(j.Writer); err != nil {
  24. j.Stop()
  25. }
  26. }
  27. func (j *JessicaFLV) OnEvent(event any) {
  28. switch v := event.(type) {
  29. case ISubscriber:
  30. if err := wsutil.WriteServerBinary(j, codec.FLVHeader); err != nil {
  31. j.Stop()
  32. }
  33. case FLVFrame:
  34. j.WriteFLVTag(v)
  35. default:
  36. j.Subscriber.OnEvent(event)
  37. }
  38. }