|
| 1 | +package logreceiver |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "io" |
| 6 | + "log" |
| 7 | + "net/http" |
| 8 | + "net/url" |
| 9 | + "path/filepath" |
| 10 | + "sync" |
| 11 | + "time" |
| 12 | + |
| 13 | + "github.com/zencoder/go-dash/mpd" |
| 14 | +) |
| 15 | + |
| 16 | +type Streams struct { |
| 17 | + cache sync.Map |
| 18 | +} |
| 19 | + |
| 20 | +type DashManifest struct { |
| 21 | + LastRefresh time.Time |
| 22 | + Qualities map[string]string |
| 23 | +} |
| 24 | + |
| 25 | +func (s *Streams) getManifest(segmentPath string) DashManifest { |
| 26 | + base := filepath.Dir(segmentPath) |
| 27 | + val, ok := s.cache.LoadOrStore(base, DashManifest{Qualities: make(map[string]string)}) |
| 28 | + if !ok { |
| 29 | + // not present in Cache getInBackground |
| 30 | + go func(b string) { |
| 31 | + err := s.getManifestInBackground(b) |
| 32 | + if err == nil { |
| 33 | + return |
| 34 | + } |
| 35 | + |
| 36 | + log.Println(err.Error()) |
| 37 | + s.cache.Delete(b) |
| 38 | + }(base) |
| 39 | + } |
| 40 | + |
| 41 | + return val.(DashManifest) |
| 42 | +} |
| 43 | + |
| 44 | +func (s *Streams) getManifestInBackground(base string) error { |
| 45 | + path, err := url.JoinPath("http://127.0.0.1/", base, "manifest.mpd") |
| 46 | + |
| 47 | + log.Printf("try to get %s\n", path) |
| 48 | + |
| 49 | + if err != nil { |
| 50 | + return fmt.Errorf("failed to get manifest path: %s", err) |
| 51 | + } |
| 52 | + |
| 53 | + resp, err := http.Get(path) |
| 54 | + if err != nil { |
| 55 | + return fmt.Errorf("failed to get manifest: %s", err.Error()) |
| 56 | + } |
| 57 | + |
| 58 | + if resp.StatusCode != http.StatusOK { |
| 59 | + return fmt.Errorf("failed to get manifest: return status is %d", resp.StatusCode) |
| 60 | + } |
| 61 | + |
| 62 | + content, err := io.ReadAll(resp.Body) |
| 63 | + if err != nil { |
| 64 | + return fmt.Errorf("failed to get manifest body: %s", err.Error()) |
| 65 | + } |
| 66 | + |
| 67 | + manifest, err := mpd.ReadFromString(string(content)) |
| 68 | + if err != nil { |
| 69 | + return fmt.Errorf("failed to decode manifest: %s", err.Error()) |
| 70 | + } |
| 71 | + |
| 72 | + mD := DashManifest{} |
| 73 | + mD.Qualities = make(map[string]string) |
| 74 | + if len(manifest.Periods) != 1 { |
| 75 | + return fmt.Errorf("not exactly one period") |
| 76 | + } |
| 77 | + |
| 78 | + for _, a := range manifest.Periods[0].AdaptationSets { |
| 79 | + if a == nil { |
| 80 | + continue |
| 81 | + } |
| 82 | + |
| 83 | + if a.ContentType == nil || *a.ContentType != "video" { |
| 84 | + continue |
| 85 | + } |
| 86 | + |
| 87 | + for _, r := range a.Representations { |
| 88 | + if r == nil || r.Height == nil || r.ID == nil { |
| 89 | + continue |
| 90 | + } |
| 91 | + |
| 92 | + mD.Qualities[*r.ID] = fmt.Sprintf("%d", *r.Height) |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + mD.LastRefresh = time.Now() |
| 97 | + s.cache.Store(base, mD) |
| 98 | + return nil |
| 99 | +} |
| 100 | + |
| 101 | +func (s *Streams) RefreshRoutine() { |
| 102 | + ticker := time.NewTicker(time.Minute * 30) |
| 103 | + for { |
| 104 | + <-ticker.C |
| 105 | + |
| 106 | + s.cache.Range(func(key, value any) bool { |
| 107 | + m := value.(DashManifest) |
| 108 | + base := key.(string) |
| 109 | + if time.Since(m.LastRefresh) < time.Hour { |
| 110 | + return true |
| 111 | + } |
| 112 | + |
| 113 | + err := s.getManifestInBackground(base) |
| 114 | + if err != nil { |
| 115 | + log.Println(err.Error()) |
| 116 | + s.cache.Delete(base) |
| 117 | + } |
| 118 | + |
| 119 | + return true |
| 120 | + }) |
| 121 | + } |
| 122 | +} |
0 commit comments