]> 127.0.0.1 Git - bili_danmu/.git/commitdiff
Fix chrome上音画不同步、而firefox则不会 #89
authorqydysky <qydysky@foxmail.com>
Fri, 6 Oct 2023 18:40:22 +0000 (02:40 +0800)
committerqydysky <qydysky@foxmail.com>
Fri, 6 Oct 2023 18:40:22 +0000 (02:40 +0800)
Reply/F/comp.go
Reply/fmp4Decode.go

index 70a05e8c094cd0ad8de2fdee2554b8d6f5a406c7..39cf657ee3ac0a6718a168643c59365c61b5842b 100644 (file)
@@ -11,7 +11,7 @@ func init() {
        var linkMap = map[string][]string{
                "github.com/qydysky/bili_danmu/Reply.startRecDanmu.stop": {
                        comp.Sign[danmuXml.Sign](`toXml`),
-                       comp.Sign[reSetMp4TimeStamp.Sign](`resetTS`),
+                       comp.Sign[reSetMp4TimeStamp.Sign](`_resetTS`),
                        // comp.Sign[fmp4Tomp4.Sign](`conver`),
                },
                "github.com/qydysky/bili_danmu/Reply.SerF.player.ws": {
index ba055cd0643f20ccb7873e366a98695d6707b794..21e5620aad92f42225898fc9142e2d2fe6e0ff62 100644 (file)
@@ -4,6 +4,7 @@ import (
        "bytes"
        "errors"
        "io"
+       "math"
 
        "github.com/dustin/go-humanize"
        F "github.com/qydysky/bili_danmu/F"
@@ -41,24 +42,25 @@ type ie struct {
 }
 
 type trak struct {
-       timescale   int
-       trackID     int
-       handlerType byte
+       firstTimeStamp int
+       timescale      int
+       trackID        int
+       handlerType    byte
 }
 
 type timeStamp struct {
-       timeStamp   int
-       data        []byte
-       timescale   int
-       handlerType byte
+       timeStamp      int
+       data           []byte
+       firstTimeStamp int
+       handlerType    byte
 }
 
 func (t *timeStamp) getT() float64 {
-       return float64(t.timeStamp) / float64(t.timescale)
+       return float64(t.timeStamp) / float64(t.firstTimeStamp)
 }
 
 type Fmp4Decoder struct {
-       traks map[int]trak
+       traks map[int]*trak
        buf   *slice.Buf[byte]
 }
 
@@ -89,12 +91,13 @@ func (t *Fmp4Decoder) Init_fmp4(buf []byte) (b []byte, err error) {
                func(m []ie) bool {
                        tackId := int(F.Btoi(buf, m[0].i+20, 4))
                        if t.traks == nil {
-                               t.traks = make(map[int]trak)
+                               t.traks = make(map[int]*trak)
                        }
-                       t.traks[tackId] = trak{
-                               trackID:     tackId,
-                               timescale:   int(F.Btoi(buf, m[2].i+20, 4)),
-                               handlerType: buf[m[3].i+16],
+                       t.traks[tackId] = &trak{
+                               trackID:        tackId,
+                               firstTimeStamp: -1,
+                               timescale:      int(F.Btoi(buf, m[2].i+20, 4)),
+                               handlerType:    buf[m[3].i+16],
                        }
                        return false
                })
@@ -151,8 +154,11 @@ func (t *Fmp4Decoder) Search_stream_fmp4(buf []byte, keyframe *slice.Buf[byte])
                        track, ok := t.traks[int(F.Btoi(buf, tfhd+12, 4))]
                        if ok {
                                ts := get_timeStamp(tfdt)
+                               if track.firstTimeStamp == -1 {
+                                       track.firstTimeStamp = ts.timeStamp
+                               }
                                ts.handlerType = track.handlerType
-                               ts.timescale = track.timescale
+                               ts.firstTimeStamp = track.firstTimeStamp
                                return ts, track.handlerType
                        }
                        return
@@ -324,8 +330,8 @@ func (t *Fmp4Decoder) Search_stream_fmp4(buf []byte, keyframe *slice.Buf[byte])
                                }
 
                                //sync audio timeStamp
-                               if audio.getT() != video.getT() {
-                                       date := F.Itob64(int64(video.getT() * float64(audio.timescale)))
+                               if diff := math.Abs(audio.getT() - video.getT()); diff > 0.00001 {
+                                       date := F.Itob64(int64(video.getT() * float64(audio.firstTimeStamp)))
                                        copy(audio.data, date)
                                }