This repository has been archived by the owner on Oct 22, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathlibshout.go
205 lines (169 loc) · 3.91 KB
/
libshout.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
* Copyright (c) $2013, Ömer Yildiz. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*/
package shout
import (
"fmt"
"unsafe"
)
/*
#cgo LDFLAGS: -lshout
#include <stdlib.h>
#include <shout/shout.h>
*/
import "C"
const (
BUFFER_SIZE = 8192
)
const (
// See shout.h
SHOUTERR_SUCCESS = 0
SHOUTERR_INSANE = -1
SHOUTERR_NOCORRECT = -2
SHOUTERR_NOLOGIN = -3
SHOUTERR_SOCKET = -4
SHOUTERR_MALLOC = -5
SHOUTERR_METADATA = -6
SHOUTERR_CONNECTED = -7
SHOUTERR_UNCONNECTED = -8
SHOUTERR_UNSUPPORTED = -9
SHOUTERR_BUSY = -10
)
const (
FORMAT_OGG = 0
FORMAT_MP3 = 1
FORMAT_WEBM = 2
)
const (
PROTOCOL_HTTP = 0
PROTOCOL_XAUDIOCAST = 1
PROTOCOL_ICY = 2
)
type ShoutError struct {
Message string
Code int
}
func (e ShoutError) Error() string {
return fmt.Sprintf("%s (%d)", e.Message, e.Code)
}
type Shout struct {
Host string
Port uint
User string
Password string
Mount string
Format int
Protocol int
// wrap the native C struct
struc *C.struct_shout
metadata *C.struct_shout_metadata_t
stream chan []byte
}
func init() {
C.shout_init()
}
func Shutdown() {
C.shout_shutdown()
}
func Free(s *Shout) {
C.shout_free(s.struc)
}
func (s *Shout) lazyInit() {
if s.struc != nil {
return
}
s.struc = C.shout_new()
s.updateParameters()
s.stream = make(chan []byte)
}
func (s *Shout) updateParameters() {
// set hostname
p := C.CString(s.Host)
C.shout_set_host(s.struc, p)
C.free(unsafe.Pointer(p))
// set port
C.shout_set_port(s.struc, C.ushort(s.Port))
// set username
p = C.CString(s.User)
C.shout_set_user(s.struc, p)
C.free(unsafe.Pointer(p))
// set password
p = C.CString(s.Password)
C.shout_set_password(s.struc, p)
C.free(unsafe.Pointer(p))
// set mount point
p = C.CString(s.Mount)
C.shout_set_mount(s.struc, p)
C.free(unsafe.Pointer(p))
// set format
C.shout_set_format(s.struc, C.uint(s.Format))
// set protocol
C.shout_set_protocol(s.struc, C.uint(s.Protocol))
}
func (s *Shout) GetError() string {
s.lazyInit()
err := C.shout_get_error(s.struc)
return C.GoString(err)
}
func (s *Shout) Open() (chan<- []byte, error) {
s.lazyInit()
errcode := int(C.shout_open(s.struc))
if errcode != C.SHOUTERR_SUCCESS {
return nil, ShoutError{
Code: errcode,
Message: s.GetError(),
}
}
go s.handleStream()
return s.stream, nil
}
func (s *Shout) Close() error {
errcode := int(C.shout_close(s.struc))
if errcode != C.SHOUTERR_SUCCESS {
return ShoutError{
Code: errcode,
Message: s.GetError(),
}
}
return nil
}
func (s *Shout) send(buffer []byte) error {
ptr := (*C.uchar)(&buffer[0])
C.shout_send(s.struc, ptr, C.size_t(len(buffer)))
errno := int(C.shout_get_errno(s.struc))
if errno != C.SHOUTERR_SUCCESS {
fmt.Println("something went wrong: %d", errno)
}
C.shout_sync(s.struc)
return nil
}
func (s *Shout) handleStream() {
for buf := range s.stream {
s.send(buf)
}
}
func (s *Shout) UpdateMetadata( mname string, mvalue string ) {
md := C.shout_metadata_new()
ptr1 := C.CString(mname)
ptr2 := C.CString(mvalue)
C.shout_metadata_add( md, ptr1, ptr2 )
C.free(unsafe.Pointer(ptr1))
C.free(unsafe.Pointer(ptr2))
C.shout_set_metadata( s.struc, md )
C.shout_metadata_free(md)
}