-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmusic_hub.go
182 lines (155 loc) · 3.19 KB
/
music_hub.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package main
import (
"io/ioutil"
"log"
"os"
"time"
"github.com/tcolgate/mp3"
"fmt"
)
// simple hub that manages all the socket connections as a clients
type Hub struct {
clients map[*Client]bool
broadcast chan []byte
register chan *Client
unregister chan *Client
track string
trackIndex int
tracks []string
}
// creates a hub
func newHub() Hub {
return Hub{
clients: make(map[*Client]bool),
broadcast: make(chan []byte),
register: make(chan *Client),
unregister: make(chan *Client),
trackIndex: 0,
tracks: readTracksAt("./music"),
}
}
func readTracksAt(dir string) []string {
files, err := ioutil.ReadDir(dir)
if err != nil {
panic(err)
}
songs := make([]string, 0)
for _, f := range files {
songs = append(songs, dir+"/"+f.Name())
fmt.Println(dir+"/"+f.Name())
}
return songs
}
// main hub of this app. To send something to a client use this hub
var MusicHub = newHub()
// function registers new client to a hub
func (hub *Hub) Register(client *Client) {
hub.register <- client
}
// function removes a client from a hub
func (hub *Hub) unregisterClient(c *Client) {
if _, ok := hub.clients[c]; ok {
delete(hub.clients, c)
close(c.send)
}
}
// function checks is hub empty
func (hub *Hub) isEmpty() bool {
return len(hub.clients) == 0
}
// function returns number of clients in a hub
func (hub *Hub) count() int {
return len(hub.clients)
}
// function makes preparations to broadcast message to all clients
// if there are clients in a hub
func (hub *Hub) SendMessage(message []byte) {
if hub.isEmpty() {
return
}
hub.broadcast <- message
}
func (hub *Hub) RunHttp(){
go hub.stream()
}
// main hub process
func (hub *Hub) Run() {
go hub.stream()
for {
select {
case c := <-hub.register:
hub.clients[c] = true
case c := <-hub.unregister:
hub.unregisterClient(c)
case m := <-hub.broadcast:
hub.broadcastMessage(m)
}
}
}
// function broadcasts prepared message to all the clients in the hub
func (hub *Hub) broadcastMessage(m []byte) {
for c := range hub.clients {
select {
case c.send <- m:
default:
close(c.send)
delete(hub.clients, c)
}
}
}
func (hub *Hub) broadcastMessageViaHTTP(m []byte){
}
func (hub *Hub) stream() {
for {
if err := hub.selectTrack(); err == nil {
hub.streamStep()
fmt.Println(hub.track)
}
return
}
}
func (hub *Hub) selectTrack() error {
if hub.trackIndex >= len(hub.tracks) {
hub.trackIndex=0
}
hub.track = hub.tracks[hub.trackIndex]
hub.trackIndex++
return nil
}
func (hub *Hub) streamStep() {
r, err := os.Open(hub.track)
defer r.Close()
if err != nil {
log.Println(err)
fmt.Println("here error")
return
}
d := mp3.NewDecoder(r)
var f mp3.Frame
skipped := 0
for {
if err := d.Decode(&f,&skipped); err != nil {
log.Println(err)
fmt.Println("here is problem")
return
}
b := make([]byte, f.Size())
f.Reader().Read(b)
hub.SendMessage(b)
time.Sleep(f.Duration())
}
}
func (hub *Hub) TrackInfo() (mp3.Frame, error) {
var f mp3.Frame
r, err := os.Open(hub.track)
if err != nil {
log.Println(err)
fmt.Println("on track info error")
return f, err
}
skipped := 0
d := mp3.NewDecoder(r)
d.Decode(&f,&skipped)
fmt.Println(&f)
return f, nil
}