video_bvc.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package dao
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "go-common/app/service/bbq/common/db/bbq"
  7. "go-common/app/service/bbq/video/model/grpc"
  8. "go-common/library/database/sql"
  9. "go-common/library/log"
  10. "go-common/library/net/metadata"
  11. "strconv"
  12. "strings"
  13. )
  14. const (
  15. _querySvPlay = "select `svid`,`path`,`resolution_retio`,`code_rate`,`video_code`,`file_size`,`duration` from %s where svid in (%s) and is_deleted = 0 order by code_rate desc"
  16. )
  17. const (
  18. _defaultPlatform = "html5"
  19. _playBcNum = 1
  20. )
  21. //RawSVBvcKey 批量获取playurl相对地址
  22. func (d *Dao) RawSVBvcKey(c context.Context, svids []int64) (res map[int64][]*bbq.VideoBvc, err error) {
  23. var (
  24. tb map[string][]string
  25. rows *sql.Rows
  26. )
  27. res = make(map[int64][]*bbq.VideoBvc)
  28. tb = make(map[string][]string)
  29. tName := "video_bvc_%02d"
  30. for _, v := range svids {
  31. if v <= 0 {
  32. continue
  33. }
  34. tbName := fmt.Sprintf(tName, v%100)
  35. tb[tbName] = append(tb[tbName], strconv.FormatInt(v, 10))
  36. }
  37. for k, v := range tb {
  38. query := fmt.Sprintf(_querySvPlay, k, strings.Join(v, ","))
  39. if rows, err = d.db.Query(c, query); err != nil {
  40. log.Errorv(c, log.KV("log", "RawSVBvcKey query sql"), log.KV("err", err))
  41. continue
  42. }
  43. for rows.Next() {
  44. tmp := bbq.VideoBvc{}
  45. if err = rows.Scan(&tmp.SVID, &tmp.Path, &tmp.ResolutionRetio, &tmp.CodeRate, &tmp.VideoCode, &tmp.FileSize, &tmp.Duration); err != nil {
  46. log.Errorv(c, log.KV("log", "RawSVBvcKey scan"), log.KV("err", err))
  47. continue
  48. }
  49. res[tmp.SVID] = append(res[tmp.SVID], &tmp)
  50. }
  51. }
  52. return
  53. }
  54. // RelPlayURLs 相对地址批量获取playurl
  55. func (d *Dao) RelPlayURLs(c context.Context, addrs []string) (res map[string]*grpc.VideoKeyItem, err error) {
  56. res = make(map[string]*grpc.VideoKeyItem)
  57. req := &grpc.RequestMsg{
  58. Keys: addrs,
  59. Backup: uint32(_playBcNum),
  60. Platform: _defaultPlatform,
  61. UIP: metadata.String(c, metadata.RemoteIP),
  62. }
  63. _str, _ := json.Marshal(req)
  64. log.V(5).Infov(c, log.KV("log", fmt.Sprintf("bvc play req (%s)", string(_str))))
  65. r, err := d.bvcPlayClient.ProtobufPlayurl(c, req)
  66. _str, _ = json.Marshal(r)
  67. if err != nil {
  68. log.Error("bvc play err[%v] ret[%s]", err, string(_str))
  69. return
  70. }
  71. log.V(5).Infov(c, log.KV("log", fmt.Sprintf("bvc play ret (%s)", string(_str))))
  72. res = r.Data
  73. return
  74. }