|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/binary" |
| 6 | + "flag" |
| 7 | + "fmt" |
| 8 | + "github.com/miekg/pcap" |
| 9 | + "io" |
| 10 | + "log" |
| 11 | + "os" |
| 12 | +) |
| 13 | + |
| 14 | +type Frame struct { |
| 15 | + FrameID uint16 |
| 16 | + LastChunk uint16 |
| 17 | + Data []byte |
| 18 | +} |
| 19 | + |
| 20 | +func main() { |
| 21 | + interf := flag.String("interface", "eth0", "What interface the device is attached to") |
| 22 | + flag.Parse() |
| 23 | + |
| 24 | + MULTICAST_MAC := []byte{0x01, 0x00, 0x5e, 0x02, 0x02, 0x02} |
| 25 | + |
| 26 | + h, err := pcap.OpenLive(*interf, 1500, true, 500) |
| 27 | + if h == nil { |
| 28 | + fmt.Fprintf(os.Stderr, "de hdmi: %s\n", err) |
| 29 | + return |
| 30 | + } |
| 31 | + defer h.Close() |
| 32 | + |
| 33 | + droppedframes := 0 |
| 34 | + desyncframes := 0 |
| 35 | + |
| 36 | + CurrentPacket := Frame{} |
| 37 | + CurrentPacket.Data = make([]byte, 0) |
| 38 | + |
| 39 | + os.Stdout.WriteString("--myboundary\nContent-Type: image/jpeg\n\n") |
| 40 | + |
| 41 | + for pkt, r := h.NextEx(); r >= 0; pkt, r = h.NextEx() { |
| 42 | + if r == 0 { |
| 43 | + // Timeout, continue |
| 44 | + continue |
| 45 | + } |
| 46 | + |
| 47 | + MACADDR := pkt.Data[0:6] |
| 48 | + if bytes.Compare(MACADDR, MULTICAST_MAC) != 0 { |
| 49 | + // This isnt the multicast packet we are looking for |
| 50 | + continue |
| 51 | + } |
| 52 | + |
| 53 | + // Ethernet + IP + UDP = 50, Min packet size is 5 bytes, thus 55 |
| 54 | + if len(pkt.Data) < 100 { |
| 55 | + continue |
| 56 | + } |
| 57 | + |
| 58 | + // Check that the port is 2068 |
| 59 | + if pkt.Data[34] != 0x08 || pkt.Data[35] != 0x14 { |
| 60 | + continue |
| 61 | + } |
| 62 | + |
| 63 | + ApplicationData := pkt.Data[42:] |
| 64 | + |
| 65 | + FrameNumber := uint16(0) |
| 66 | + CurrentChunk := uint16(0) |
| 67 | + |
| 68 | + buf := bytes.NewBuffer(ApplicationData[:2]) |
| 69 | + buf2 := bytes.NewBuffer(ApplicationData[2:4]) |
| 70 | + binary.Read(buf, binary.BigEndian, &FrameNumber) |
| 71 | + binary.Read(buf2, binary.BigEndian, &CurrentChunk) |
| 72 | + |
| 73 | + if CurrentPacket.FrameID != FrameNumber && CurrentPacket.FrameID != 0 { |
| 74 | + // Did we drop a packet ? |
| 75 | + droppedframes++ |
| 76 | + if CurrentPacket.FrameID < FrameNumber { |
| 77 | + CurrentPacket = Frame{} |
| 78 | + CurrentPacket.Data = make([]byte, 0) |
| 79 | + CurrentPacket.FrameID = CurrentPacket.FrameID |
| 80 | + CurrentPacket.LastChunk = 0 |
| 81 | + log.Printf("Dropped packet because of non sane frame number (%d dropped so far)", droppedframes) |
| 82 | + } |
| 83 | + continue |
| 84 | + } |
| 85 | + |
| 86 | + // log.Printf("%d/%d - %d/%d - %d", FrameNumber, CurrentChunk, CurrentPacket.FrameID, CurrentPacket.LastChunk, len(ApplicationData)) |
| 87 | + |
| 88 | + if CurrentPacket.LastChunk != 0 && CurrentPacket.LastChunk != CurrentChunk-1 { |
| 89 | + if uint16(^(CurrentChunk << 15)) != 65534 { |
| 90 | + log.Printf("Dropped packet because of desync detected (%d dropped so far, %d because of desync)", |
| 91 | + droppedframes, desyncframes) |
| 92 | + |
| 93 | + log.Printf("You see; %d != %d-1", |
| 94 | + CurrentPacket.LastChunk, CurrentChunk) |
| 95 | + |
| 96 | + // Oh dear, we are out of sync, Drop the frame |
| 97 | + droppedframes++ |
| 98 | + desyncframes++ |
| 99 | + CurrentPacket = Frame{} |
| 100 | + CurrentPacket.Data = make([]byte, 0) |
| 101 | + CurrentPacket.FrameID = CurrentPacket.FrameID |
| 102 | + CurrentPacket.LastChunk = 0 |
| 103 | + |
| 104 | + continue |
| 105 | + } |
| 106 | + CurrentPacket.LastChunk = CurrentChunk |
| 107 | + } |
| 108 | + |
| 109 | + for _, v := range ApplicationData[4:] { |
| 110 | + CurrentPacket.Data = append(CurrentPacket.Data, v) |
| 111 | + } |
| 112 | + |
| 113 | + if uint16(^(CurrentChunk >> 15)) == 65534 { |
| 114 | + // Flush the frame to output |
| 115 | + os.Stdout.WriteString("\n--myboundary\nContent-Type: image/jpeg\n\n") |
| 116 | + log.Printf("Size: %d", len(CurrentPacket.Data)) |
| 117 | + buf := bytes.NewBuffer(CurrentPacket.Data) |
| 118 | + io.Copy(os.Stdout, buf) |
| 119 | + |
| 120 | + CurrentPacket = Frame{} |
| 121 | + CurrentPacket.Data = make([]byte, 0) |
| 122 | + CurrentPacket.FrameID = 0 |
| 123 | + CurrentPacket.LastChunk = 0 |
| 124 | + } |
| 125 | + |
| 126 | + } |
| 127 | +} |
0 commit comments