-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
164 lines (103 loc) · 2.66 KB
/
main.go
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
package main
import (
"bytes"
"flag"
"io"
"log"
"net/http"
"os"
"sync"
"time"
)
const (
BUFFERSIZE = 8192
//formula for delay = track_duration * buffer_size / aac_file_size
DELAY = 150
)
type Connection struct {
bufferChannel chan []byte
buffer []byte
}
type ConnectionPool struct {
ConnectionMap map[*Connection]struct{}
mu sync.Mutex
}
func (cp *ConnectionPool) AddConnection(connection *Connection) {
defer cp.mu.Unlock()
cp.mu.Lock()
cp.ConnectionMap[connection] = struct{}{}
}
func (cp *ConnectionPool) DeleteConnection(connection *Connection) {
defer cp.mu.Unlock()
cp.mu.Lock()
delete(cp.ConnectionMap, connection)
}
func (cp *ConnectionPool) Broadcast(buffer []byte) {
defer cp.mu.Unlock()
cp.mu.Lock()
for connection := range cp.ConnectionMap {
copy(connection.buffer, buffer)
select {
case connection.bufferChannel <- connection.buffer:
default:
}
}
}
func NewConnectionPool() *ConnectionPool {
connectionMap := make(map[*Connection]struct{})
return &ConnectionPool{ConnectionMap: connectionMap}
}
func stream(connectionPool *ConnectionPool, content []byte) {
buffer := make([]byte, BUFFERSIZE)
for {
// clear() is a new builtin function introduced in go 1.21. Just reinitialize the buffer if on a lower version.
clear(buffer)
tempfile := bytes.NewReader(content)
ticker := time.NewTicker(time.Millisecond * DELAY)
for range ticker.C {
_, err := tempfile.Read(buffer)
if err == io.EOF {
ticker.Stop()
break
}
connectionPool.Broadcast(buffer)
}
}
}
func main() {
fname := flag.String("filename", "file.aac", "path of the audio file")
flag.Parse()
file, err := os.Open(*fname)
if err != nil {
log.Fatal(err)
}
ctn, err := io.ReadAll(file)
if err != nil {
log.Fatal(err)
}
connPool := NewConnectionPool()
go stream(connPool, ctn)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "audio/aac")
w.Header().Add("Connection", "keep-alive")
flusher, ok := w.(http.Flusher)
if !ok {
log.Println("Could not create flusher")
}
connection := &Connection{bufferChannel: make(chan []byte), buffer: make([]byte, BUFFERSIZE)}
connPool.AddConnection(connection)
log.Printf("%s has connected to the audio stream\n", r.Host)
for {
buf := <-connection.bufferChannel
if _, err := w.Write(buf); err != nil {
connPool.DeleteConnection(connection)
log.Printf("%s's connection to the audio stream has been closed\n", r.Host)
return
}
flusher.Flush()
clear(connection.buffer)
}
})
log.Println("Listening on port 8080...")
log.Fatal(http.ListenAndServe(":8080", nil))
}