-
Notifications
You must be signed in to change notification settings - Fork 34
/
main.go
271 lines (219 loc) · 6.98 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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
package main
import (
"bytes"
"crypto/rand"
"encoding/binary"
"encoding/hex"
"flag"
"fmt"
"io"
"log"
"net"
"os"
"os/exec"
"syscall"
"time"
"github.com/ljfranklin/pcap"
"github.com/mdlayher/raw"
)
type Frame struct {
FrameID uint16
LastChunk uint16
Data []byte
}
func main() {
interf := flag.String("interface", "eth0", "What interface the device is attached to")
debug := flag.Bool("debug", false, "Print loads of debug info")
output_mkv := flag.Bool("mkv", true, "Spit out Audio + Video contained in MKV, else spit out raw MJPEG")
audio := flag.Bool("audio", true, "Output audio into MKV as well")
wakeup := flag.Bool("wakeups", true, "Send packets needed to start/keep the sender transmitting")
sendermac := flag.String("sender-mac", "000b78006001", "The macaddress of the sender unit")
flag.Parse()
var videowriter *os.File
pipename := randString(5)
audiodis := make(chan []byte, 100)
videodis := make(chan []byte, 100)
if *wakeup {
go BroadcastWakeups(*interf, *sendermac)
}
if *output_mkv {
go WrapinMKV(fmt.Sprintf("/tmp/hdmi-Vfifo-%s", pipename), audiodis, *audio)
err := syscall.Mkfifo(fmt.Sprintf("/tmp/hdmi-Vfifo-%s", pipename), 0664)
if err != nil {
log.Fatalf("Could not make a fifo in /tmp/hdmi-Vfifo-%s, %s", pipename, err.Error())
}
videowriter, err = os.OpenFile(fmt.Sprintf("/tmp/hdmi-Vfifo-%s", pipename), os.O_WRONLY, 0664)
if err != nil {
log.Fatalf("Could not open newly made fifo in /tmp/hdmi-Vfifo-%s, %s", pipename, err.Error())
return
}
} else {
videowriter = os.Stdout
}
go DumpChanToFile(videodis, videowriter)
MULTICAST_MAC := []byte{0x01, 0x00, 0x5e, 0x02, 0x02, 0x02}
h, err := pcap.OpenLive(*interf, 1500, true, 500)
if h == nil {
fmt.Fprintf(os.Stderr, "de hdmi: %s\n", err)
return
}
defer h.Close()
droppedframes := 0
desyncframes := 0
totalframes := 0
CurrentPacket := Frame{}
CurrentPacket.Data = make([]byte, 0)
videodis <- []byte("--myboundary\nContent-Type: image/jpeg\n\n")
for pkt, r := h.NextEx(); r >= 0; pkt, r = h.NextEx() {
if r == 0 {
// Timeout, continue
continue
}
MACADDR := pkt.Data[0:6]
if bytes.Compare(MACADDR, MULTICAST_MAC) != 0 {
// This isnt the multicast packet we are looking for
continue
}
// Ethernet + IP + UDP = 50, Min packet size is 5 bytes, thus 55
if len(pkt.Data) < 100 {
continue
}
ApplicationData := pkt.Data[42:]
// Maybe there is some audio data on port 2066?
if pkt.Data[36] == 0x08 && pkt.Data[37] == 0x12 && *output_mkv && *audio {
select {
case audiodis <- ApplicationData[16:]:
default:
}
continue
}
// Check that the port is 2068
if pkt.Data[36] != 0x08 || pkt.Data[37] != 0x14 {
continue
}
FrameNumber := uint16(0)
CurrentChunk := uint16(0)
buf := bytes.NewBuffer(ApplicationData[:2])
buf2 := bytes.NewBuffer(ApplicationData[2:4])
binary.Read(buf, binary.BigEndian, &FrameNumber)
binary.Read(buf2, binary.BigEndian, &CurrentChunk)
if CurrentPacket.FrameID != FrameNumber && CurrentPacket.FrameID != 0 {
// Did we drop a packet ?
droppedframes++
if CurrentPacket.FrameID < FrameNumber {
CurrentPacket = Frame{}
CurrentPacket.Data = make([]byte, 0)
CurrentPacket.LastChunk = 0
log.Printf("Dropped packet because of non sane frame number (%d dropped so far)", droppedframes)
}
continue
}
if *debug {
log.Printf("%d/%d - %d/%d - %d", FrameNumber, CurrentChunk, CurrentPacket.FrameID, CurrentPacket.LastChunk, len(ApplicationData))
}
if CurrentPacket.LastChunk != 0 && CurrentPacket.LastChunk != CurrentChunk-1 {
if uint16(^(CurrentChunk << 15)) != 65534 {
log.Printf("Dropped packet because of desync detected (%d dropped so far, %d because of desync)",
droppedframes, desyncframes)
log.Printf("You see; %d != %d-1",
CurrentPacket.LastChunk, CurrentChunk)
// Oh dear, we are out of sync, Drop the frame
droppedframes++
desyncframes++
CurrentPacket = Frame{}
CurrentPacket.Data = make([]byte, 0)
CurrentPacket.LastChunk = 0
continue
}
CurrentPacket.LastChunk = CurrentChunk
}
CurrentPacket.Data = append(CurrentPacket.Data, ApplicationData[4:]...)
if uint16(^(CurrentChunk >> 15)) == 65534 {
// Flush the frame to output
fin := []byte("\n--myboundary\nContent-Type: image/jpeg\n\n")
fin = append(fin, CurrentPacket.Data...)
select {
case videodis <- fin:
default:
}
totalframes++
if *debug {
log.Printf("Size: %d", len(CurrentPacket.Data))
}
CurrentPacket = Frame{}
CurrentPacket.Data = make([]byte, 0)
CurrentPacket.FrameID = 0
CurrentPacket.LastChunk = 0
}
}
}
func randString(n int) string {
const alphanum = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
var bytes = make([]byte, n)
rand.Read(bytes)
for i, b := range bytes {
bytes[i] = alphanum[b%byte(len(alphanum))]
}
return string(bytes)
}
func WrapinMKV(uuidpath string, audioin chan []byte, audio bool) {
var ffmpeg *exec.Cmd
if audio {
ffmpeg = exec.Command("ffmpeg", "-f", "mjpeg", "-i", uuidpath, "-f", "s32be", "-ac", "2", "-ar", "44100", "-i", "pipe:0", "-f", "matroska", "-codec", "copy", "pipe:1")
} else {
ffmpeg = exec.Command("ffmpeg", "-f", "mjpeg", "-i", uuidpath, "-f", "matroska", "-codec", "copy", "pipe:1")
}
ffmpegstdout, err := ffmpeg.StdoutPipe()
if err != nil {
log.Fatalf("Unable to setup pipes for ffmpeg (stdout)")
}
ffmpeg.Stderr = os.Stderr
audiofile, err := ffmpeg.StdinPipe()
go DumpChanToFile(audioin, audiofile)
ffmpeg.Start()
for {
_, err := io.Copy(os.Stdout, ffmpegstdout)
if err != nil {
log.Fatalf("unable to read to stdout: %s", err.Error())
}
}
}
func DumpChanToFile(channel chan []byte, file io.WriteCloser) {
for blob := range channel {
buf := bytes.NewBuffer(blob)
_, err := io.Copy(file, buf)
if err != nil {
log.Fatalf("unable to write to pipe: %s", err.Error())
}
}
log.Fatalf("Channel closed")
}
func BroadcastWakeups(ifname string, sendermac string) {
ifc, err := net.InterfaceByName(ifname)
if err != nil {
log.Fatalf("Unable get the interface name of %s, %s", ifname, err.Error())
}
macbytes, err := hex.DecodeString(sendermac)
if err != nil {
log.Fatalf("Invalid MAC address string %s , %s", sendermac, err.Error())
}
packet := append([]byte{
0x0b, 0x00, 0x0b, 0x78, 0x00, 0x60, 0x02, 0x90, 0x2b, 0x34, 0x31, 0x02, 0x08, 0x00, 0x45, 0xfc,
0x02, 0x1c, 0x00, 0x0a, 0x00, 0x00, 0x40, 0x11, 0xa6, 0x0a, 0xc0, 0xa8, 0xa8, 0x38, 0xc0, 0xa8,
0xa8, 0x37, 0xbe, 0x31, 0xbe, 0x31, 0x02, 0x08, 0xd6, 0xdc, 0x54, 0x46, 0x36, 0x7a, 0x60, 0x02,
0x00, 0x00, 0x0a, 0x00, 0x00, 0x03, 0x03, 0x01, 0x00, 0x26, 0x00, 0x00, 0x00, 0x00, 0x02, 0xef,
0xdc}, make([]byte, 489)...)
packet = append(macbytes[:6], packet[6:]...)
for {
conn, err := raw.ListenPacket(ifc, protocolARP, nil)
if err != nil {
log.Fatalf("Unable to keep broadcasting the keepalives, %s", err.Error())
}
conn.WriteTo(packet, &raw.Addr{HardwareAddr: macbytes})
conn.Close()
time.Sleep(time.Second)
}
}
const (
protocolARP = uint16(0x0806)
)