-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
170 lines (145 loc) · 3.86 KB
/
Copy pathmain.go
File metadata and controls
170 lines (145 loc) · 3.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package main
import (
"flag"
"fmt"
"log"
"os"
"os/signal"
"sync"
"syscall"
"time"
"github.com/cloudwebrtc/go-protoo/logger"
"github.com/pion/ion-load-tool/ion"
"github.com/pion/ion-load-tool/producer"
)
var (
waitGroup sync.WaitGroup
clientNameTmpl = "client_%v"
)
func init() {
logger.SetLevel(logger.InfoLevel)
}
type testRun struct {
client ion.RoomClient
consume bool
produce bool
mediaSource producer.IFileProducer
doneCh chan interface{}
index int
}
func (t *testRun) runClient() {
defer waitGroup.Done()
t.client.Init()
t.client.Join()
// Start producer
if t.produce {
t.client.Publish(t.mediaSource.VideoCodec())
}
// Wire consumers
// Wait for the end of the test then shutdown
done := false
for !done {
select {
case msg := <-t.client.OnStreamAdd:
if t.consume {
t.client.Subscribe(msg.MediaInfo)
}
case msg := <-t.client.OnStreamRemove:
if t.consume {
t.client.UnSubscribe(msg.MediaInfo)
}
case <-t.client.OnBroadcast:
case <-t.doneCh:
done = true
break
}
}
log.Printf("Begin client %v shutdown", t.index)
// Close producer and sender
if t.produce {
t.mediaSource.Stop()
t.client.UnPublish()
}
// Close client
t.client.Leave()
t.client.Close()
}
func (t *testRun) setupClient(room, path, vidFile, fileType string, audio bool) {
name := fmt.Sprintf(clientNameTmpl, t.index)
t.client = ion.NewClient(name, room, path)
t.doneCh = make(chan interface{})
if t.produce {
// Configure sender tracks
offset := t.index * 5
if fileType == "webm" {
t.mediaSource = producer.NewMFileProducer(vidFile, offset, producer.TrackSelect{
Audio: audio,
Video: true,
})
} else if fileType == "ivf" {
audio = false
t.mediaSource = producer.NewIVFProducer(vidFile, offset)
}
t.client.VideoTrack = t.mediaSource.VideoTrack()
if audio {
t.client.AudioTrack = t.mediaSource.AudioTrack()
}
t.mediaSource.Start()
}
go t.runClient()
}
func main() {
var containerPath, containerType string
var ionPath, roomName string
var numClients, runSeconds int
var consume, produce bool
var staggerSeconds float64
var audio bool
flag.StringVar(&containerPath, "produce", "", "path to the media file you want to playback")
flag.StringVar(&ionPath, "ion-url", "ws://localhost:8443/ws", "websocket url for ion biz system")
flag.StringVar(&roomName, "room", "video-demo", "Room name for Ion")
flag.IntVar(&numClients, "clients", 1, "Number of clients to start")
flag.Float64Var(&staggerSeconds, "stagger", 1.0, "Number of seconds to stagger client start and stop")
flag.IntVar(&runSeconds, "seconds", 60, "Number of seconds to run test for")
flag.BoolVar(&consume, "consume", false, "Run subscribe to all streams and consume data")
flag.BoolVar(&audio, "audio", false, "Publish audio stream from webm file")
flag.Parse()
produce = containerPath != ""
// Validate type
if produce {
ext, ok := producer.ValidateVPFile(containerPath)
log.Println(ext)
if !ok {
panic("Only IVF and WEBM containers are supported.")
}
containerType = ext
}
clients := make([]*testRun, numClients)
staggerDur := time.Duration(staggerSeconds) * time.Second
waitGroup.Add(numClients)
for i := 0; i < numClients; i++ {
cfg := &testRun{consume: consume, produce: produce, index: i}
cfg.setupClient(roomName, ionPath, containerPath, containerType, audio)
clients[i] = cfg
time.Sleep(staggerDur)
}
// Setup shutdown
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
timer := time.NewTimer(time.Duration(runSeconds) * time.Second)
select {
case <-sigs:
case <-timer.C:
}
for i, a := range clients {
// Signal shutdown
close(a.doneCh)
// Staggered shutdown.
if len(clients) > 1 && i < len(clients)-1 {
time.Sleep(staggerDur)
}
}
log.Println("Wait for client shutdown")
waitGroup.Wait()
log.Println("All clients shut down")
}