inviteoption.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package gb28181
  2. import (
  3. "errors"
  4. "fmt"
  5. "math/rand"
  6. "strconv"
  7. )
  8. type InviteOptions struct {
  9. Start int
  10. End int
  11. dump string
  12. ssrc string
  13. SSRC uint32
  14. MediaPort uint16
  15. StreamPath string
  16. recyclePort func(p uint16) (err error)
  17. }
  18. func (o InviteOptions) IsLive() bool {
  19. return o.Start == 0 || o.End == 0
  20. }
  21. func (o InviteOptions) Record() bool {
  22. return !o.IsLive()
  23. }
  24. func (o *InviteOptions) Validate(start, end string) error {
  25. if start != "" {
  26. sint, err1 := strconv.ParseInt(start, 10, 0)
  27. if err1 != nil {
  28. return err1
  29. }
  30. o.Start = int(sint)
  31. }
  32. if end != "" {
  33. eint, err2 := strconv.ParseInt(end, 10, 0)
  34. if err2 != nil {
  35. return err2
  36. }
  37. o.End = int(eint)
  38. }
  39. if o.Start >= o.End {
  40. return errors.New("start < end")
  41. }
  42. return nil
  43. }
  44. func (o InviteOptions) String() string {
  45. return fmt.Sprintf("t=%d %d", o.Start, o.End)
  46. }
  47. func (o *InviteOptions) CreateSSRC() {
  48. ssrc := make([]byte, 10)
  49. if o.IsLive() {
  50. ssrc[0] = '0'
  51. } else {
  52. ssrc[0] = '1'
  53. }
  54. copy(ssrc[1:6], conf.Serial[3:8])
  55. randNum := 1000 + rand.Intn(8999)
  56. copy(ssrc[6:], strconv.Itoa(randNum))
  57. o.ssrc = string(ssrc)
  58. _ssrc, _ := strconv.ParseInt(o.ssrc, 10, 0)
  59. o.SSRC = uint32(_ssrc)
  60. }