Live video on the AT Protocol
1package media
2
3import (
4 "bytes"
5 "context"
6 "fmt"
7 "os"
8 "testing"
9 "time"
10
11 "github.com/go-gst/go-gst/gst"
12 "github.com/go-gst/go-gst/gst/pbutils"
13 "github.com/stretchr/testify/require"
14 "golang.org/x/sync/errgroup"
15)
16
17func TestAudioSmear(t *testing.T) {
18 withNoGSTLeaks(t, func() {
19
20 g, _ := errgroup.WithContext(context.Background())
21 for i := 0; i < streamplaceTestCount; i++ {
22 g.Go(func() error {
23 return testAudioSmearInner(t)
24 })
25 }
26 err := g.Wait()
27 require.NoError(t, err)
28 })
29}
30
31func testAudioSmearInner(t *testing.T) error {
32 uri := getFixture("duration-mismatch.mp4")
33
34 // info, err := discoverer.DiscoverURI(fmt.Sprintf("file://%s", uri))
35 // if err != nil {
36 // return err
37 // }
38
39 f, err := os.Open(uri)
40 if err != nil {
41 return err
42 }
43 defer f.Close()
44
45 // audioBs := bytes.Buffer{}
46 // videoBs := bytes.Buffer{}
47
48 seg, err := ToBuffers(context.Background(), f)
49 if err != nil {
50 return err
51 }
52
53 err = seg.Normalize(context.Background())
54 if err != nil {
55 return err
56 }
57
58 buf := bytes.Buffer{}
59 err = JoinAudioVideo(context.Background(), seg, &buf)
60 if err != nil {
61 return err
62 }
63
64 require.Equal(t, 1191255, buf.Len())
65
66 // // Write audio and video buffers to temporary files for further analysis
67 // tempDir := t.TempDir()
68
69 // audioFilePath := fmt.Sprintf("%s/audio.mp4", tempDir)
70 // videoFilePath := fmt.Sprintf("%s/video.mp4", tempDir)
71
72 // // Write audio buffer to file
73 // audioFile, err := os.Create(audioFilePath)
74 // if err != nil {
75 // return err
76 // }
77 // _, err = io.Copy(audioFile, bytes.NewReader(audioBs.Bytes()))
78 // if err != nil {
79 // return err
80 // }
81 // err = audioFile.Close()
82 // if err != nil {
83 // return err
84 // }
85
86 // // Write video buffer to file
87 // videoFile, err := os.Create(videoFilePath)
88 // if err != nil {
89 // return err
90 // }
91 // _, err = io.Copy(videoFile, bytes.NewReader(videoBs.Bytes()))
92 // if err != nil {
93 // return err
94 // }
95 // err = videoFile.Close()
96 // if err != nil {
97 // return err
98 // }
99
100 // SmearAudioTimestamps(context.Background(), bytes.NewReader(audioBs.Bytes()), &bytes.Buffer{})
101
102 // checkSame(t, videoFile.Name(), getFixture("duration-mismatch-video.mp4"))
103 // checkSame(t, audioFile.Name(), getFixture("duration-mismatch-audio.mp4"))
104 // printDiscovererInfo(info)
105 return nil
106
107}
108
109func checkSame(t *testing.T, v1, v2 string) {
110 discoverer, err := pbutils.NewDiscoverer(gst.ClockTime(time.Second * 15))
111 if err != nil {
112 panic(err)
113 }
114
115 info, err := discoverer.DiscoverURI(fmt.Sprintf("file://%s", v1))
116 require.NoError(t, err)
117 dur1 := info.GetDuration().AsDuration()
118
119 info, err = discoverer.DiscoverURI(fmt.Sprintf("file://%s", v2))
120 require.NoError(t, err)
121 dur2 := info.GetDuration().AsDuration()
122
123 require.Equal(t, *dur2, *dur1)
124}
125
126func printDiscovererInfo(info *pbutils.DiscovererInfo) {
127 fmt.Println("URI:", info.GetURI())
128 fmt.Println("Duration:", info.GetDuration())
129
130 printTags(info)
131 printStreamInfo(info.GetStreamInfo())
132
133 children := info.GetStreamList()
134 fmt.Println("Children streams:")
135 for _, child := range children {
136 printStreamInfo(child)
137 }
138}
139
140func printTags(info *pbutils.DiscovererInfo) {
141 fmt.Println("Tags:")
142 tags := info.GetTags()
143 if tags != nil {
144 fmt.Println(" ", tags)
145 return
146 }
147 fmt.Println(" no tags")
148}
149
150func printStreamInfo(info *pbutils.DiscovererStreamInfo) {
151 if info == nil {
152 return
153 }
154 fmt.Println("Stream: ")
155 fmt.Println(" Stream id:", info.GetStreamID())
156 if caps := info.GetCaps(); caps != nil {
157 fmt.Println(" Format:", caps)
158 }
159}