subject.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. package model
  2. import (
  3. "go-common/library/time"
  4. )
  5. var (
  6. _defaultSeg = &Segment{Start: 0, End: DefaultVideoEnd, Cnt: 1, Num: 1, Duration: 0}
  7. )
  8. const (
  9. // segmentLength 分段长度,根据视频时长做分段,单位:毫秒
  10. segmentLength = int64(6 * 60 * 1000)
  11. // DefaultVideoEnd 当视频时长不存在或者为0时的默认视频结尾时间点
  12. DefaultVideoEnd = int64(10 * 60 * 60 * 1000)
  13. // SubTypeVideo 主题类型
  14. SubTypeVideo = int32(1)
  15. // SubStateOpen 主题打开
  16. SubStateOpen = int32(0)
  17. // SubStateClosed 主题关闭
  18. SubStateClosed = int32(1)
  19. // AttrSubGuest 允许游客弹幕
  20. AttrSubGuest = uint(0)
  21. // AttrSubSpolier 允许剧透弹幕
  22. AttrSubSpolier = uint(1)
  23. // AttrSubMission 允许活动弹幕
  24. AttrSubMission = uint(2)
  25. // AttrSubAdvance 允许高级弹幕
  26. AttrSubAdvance = uint(3)
  27. // AttrSubMonitorBefore 弹幕先审后发
  28. AttrSubMonitorBefore = uint(4)
  29. // AttrSubMonitorAfter 弹幕先发后审
  30. AttrSubMonitorAfter = uint(5)
  31. )
  32. // Subject dm_subject.
  33. type Subject struct {
  34. ID int64 `json:"id"`
  35. Type int32 `json:"type"`
  36. Oid int64 `json:"oid"`
  37. Pid int64 `json:"pid"`
  38. Mid int64 `json:"mid"`
  39. State int32 `json:"state"`
  40. Attr int32 `json:"attr"`
  41. ACount int64 `json:"acount"`
  42. Count int64 `json:"count"`
  43. MCount int64 `json:"mcount"`
  44. MoveCnt int64 `json:"move_count"`
  45. Maxlimit int64 `json:"maxlimit"`
  46. Childpool int32 `json:"childpool"`
  47. CTime time.Time `json:"ctime"`
  48. MTime time.Time `json:"mtime"`
  49. }
  50. // AttrVal return val of subject'attr.
  51. func (s *Subject) AttrVal(bit uint) int32 {
  52. return (s.Attr >> bit) & int32(1)
  53. }
  54. // AttrSet set val of subject'attr.
  55. func (s *Subject) AttrSet(v int32, bit uint) {
  56. s.Attr = s.Attr&(^(1 << bit)) | (v << bit)
  57. }
  58. // Segment dm segment struct
  59. type Segment struct {
  60. Start int64 `json:"ps"` // 分段起始时间
  61. End int64 `json:"pe"` // 分段结束时间
  62. Cnt int64 `json:"cnt"` // 总分段数
  63. Num int64 `json:"num"` // 当前第几段
  64. Duration int64 `json:"duration"` // 视频总时长
  65. }
  66. // SegmentInfo get segment info by start time and video duration.
  67. func SegmentInfo(ps, duration int64) (s *Segment) {
  68. var cnt, num, pe int64
  69. if duration == 0 {
  70. s = _defaultSeg
  71. return
  72. }
  73. cnt = duration / segmentLength
  74. if duration%segmentLength > 0 {
  75. cnt++
  76. }
  77. for i := int64(0); i < cnt; i++ {
  78. if ps >= i*segmentLength && ps < (i+1)*segmentLength {
  79. ps = i * segmentLength
  80. pe = (i + 1) * segmentLength
  81. num = i + 1
  82. }
  83. }
  84. if pe > duration {
  85. pe = duration
  86. }
  87. if ps > duration {
  88. ps = duration
  89. pe = duration
  90. num = cnt
  91. }
  92. s = &Segment{
  93. Start: ps,
  94. End: pe,
  95. Cnt: cnt,
  96. Num: num,
  97. Duration: duration,
  98. }
  99. return
  100. }
  101. // SegmentPoint 根据当前段数和视频总时长计算分段的起始时间点
  102. func SegmentPoint(num, duration int64) (ps, pe int64) {
  103. if duration == 0 {
  104. ps = 0
  105. pe = DefaultVideoEnd
  106. return
  107. }
  108. pe = num * segmentLength
  109. ps = pe - segmentLength
  110. if pe > duration {
  111. pe = duration
  112. }
  113. if ps < 0 {
  114. ps = 0
  115. }
  116. return
  117. }