flv.go 830 B

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