Skip to content

Commit 9b2621b

Browse files
author
Daniel Paulus
committed
simplify sync packets
1 parent 8aa31d1 commit 9b2621b

18 files changed

+141
-148
lines changed

screencapture/packet/sync_afmt.go

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,14 @@ package packet
33
import (
44
"encoding/binary"
55
"fmt"
6+
67
"github.com/danielpaulus/quicktime_video_hack/screencapture/common"
78
"github.com/danielpaulus/quicktime_video_hack/screencapture/coremedia"
89
)
910

1011
// SyncAfmtPacket contains what I think is information about the audio format
1112
type SyncAfmtPacket struct {
12-
SyncMagic uint32
1313
ClockRef CFTypeID
14-
MessageType uint32
1514
CorrelationID uint64
1615
AudioStreamBasicDescription coremedia.AudioStreamBasicDescription
1716
}
@@ -23,20 +22,13 @@ func (sp SyncAfmtPacket) String() string {
2322

2423
// NewSyncAfmtPacketFromBytes parses a new AsynFmtPacket from byte array
2524
func NewSyncAfmtPacketFromBytes(data []byte) (SyncAfmtPacket, error) {
26-
var packet = SyncAfmtPacket{}
27-
packet.SyncMagic = binary.LittleEndian.Uint32(data)
28-
if packet.SyncMagic != SyncPacketMagic {
29-
return packet, fmt.Errorf("invalid sync magic: %x", data)
30-
}
31-
packet.ClockRef = binary.LittleEndian.Uint64(data[4:])
32-
packet.MessageType = binary.LittleEndian.Uint32(data[12:])
33-
if packet.MessageType != AFMT {
34-
return packet, fmt.Errorf("invalid packet type in sync afmt:%x", data)
25+
remainingBytes, clockRef, correlationID, err := ParseSyncHeader(data, AFMT)
26+
if err != nil {
27+
return SyncAfmtPacket{}, err
3528
}
36-
packet.CorrelationID = binary.LittleEndian.Uint64(data[16:])
29+
packet := SyncAfmtPacket{ClockRef: clockRef, CorrelationID: correlationID}
3730

38-
var err error
39-
packet.AudioStreamBasicDescription, err = coremedia.NewAudioStreamBasicDescriptionFromBytes(data[24:])
31+
packet.AudioStreamBasicDescription, err = coremedia.NewAudioStreamBasicDescriptionFromBytes(remainingBytes)
4032
if err != nil {
4133
return packet, fmt.Errorf("Error parsing AudioStreamBasicDescription data in asyn afmt: %s, ", err)
4234
}

screencapture/packet/sync_afmt_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,16 @@ func TestAfmt(t *testing.T) {
1818
afmtPacket, err := packet.NewSyncAfmtPacketFromBytes(dat[4:])
1919
if assert.NoError(t, err) {
2020
assert.Equal(t, uint64(0x7fa66ce20cb0), afmtPacket.ClockRef)
21-
assert.Equal(t, packet.SyncPacketMagic, afmtPacket.SyncMagic)
22-
assert.Equal(t, packet.AFMT, afmtPacket.MessageType)
2321
expectedAsbd := coremedia.DefaultAudioStreamBasicDescription()
2422
expectedAsbd.FormatFlags = 0x4C
2523
assert.Equal(t, expectedAsbd, afmtPacket.AudioStreamBasicDescription)
2624
expectedString := "SYNC_AFMT{ClockRef:7fa66ce20cb0, CorrelationID:113229d80, AudioStreamBasicDescription:{SampleRate:48000.000000,FormatFlags:76,BytesPerPacket:4,FramesPerPacket:1,BytesPerFrame:4,ChannelsPerFrame:2,BitsPerChannel:16,Reserved:0}}"
2725
assert.Equal(t, expectedString, afmtPacket.String())
2826
testSerializationOfAfmtReply(afmtPacket, t)
2927
}
28+
29+
_, err = packet.NewSyncAfmtPacketFromBytes(dat)
30+
assert.Error(t, err)
3031
}
3132

3233
func testSerializationOfAfmtReply(clok packet.SyncAfmtPacket, t *testing.T) {

screencapture/packet/sync_clok.go

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,22 @@
11
package packet
22

33
import (
4-
"encoding/binary"
54
"fmt"
65
)
76

87
//SyncClokPacket contains a decoded Clok packet from the device
98
type SyncClokPacket struct {
10-
SyncMagic uint32
119
ClockRef CFTypeID
12-
MessageType uint32
1310
CorrelationID uint64
1411
}
1512

1613
//NewSyncClokPacketFromBytes parses a SynClokPacket from bytes
1714
func NewSyncClokPacketFromBytes(data []byte) (SyncClokPacket, error) {
18-
packet := SyncClokPacket{}
19-
packet.SyncMagic = binary.LittleEndian.Uint32(data)
20-
if packet.SyncMagic != SyncPacketMagic {
21-
return packet, fmt.Errorf("invalid SYNC Clok Packet: %x", data)
15+
_, clockRef, correlationID, err := ParseSyncHeader(data, CLOK)
16+
if err != nil {
17+
return SyncClokPacket{}, err
2218
}
23-
24-
packet.ClockRef = binary.LittleEndian.Uint64(data[4:])
25-
packet.MessageType = binary.LittleEndian.Uint32(data[12:])
26-
if packet.MessageType != CLOK {
27-
return packet, fmt.Errorf("wrong message type for Clok message: %x", packet.MessageType)
28-
}
29-
packet.CorrelationID = binary.LittleEndian.Uint64(data[16:])
19+
packet := SyncClokPacket{ClockRef: clockRef, CorrelationID: correlationID}
3020
return packet, nil
3121
}
3222

screencapture/packet/sync_clok_test.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package packet_test
22

33
import (
4-
"github.com/danielpaulus/quicktime_video_hack/screencapture/packet"
5-
"github.com/stretchr/testify/assert"
64
"io/ioutil"
75
"log"
86
"testing"
7+
8+
"github.com/danielpaulus/quicktime_video_hack/screencapture/packet"
9+
"github.com/stretchr/testify/assert"
910
)
1011

1112
func TestClok(t *testing.T) {
@@ -16,11 +17,12 @@ func TestClok(t *testing.T) {
1617
clok, err := packet.NewSyncClokPacketFromBytes(dat[4:])
1718
if assert.NoError(t, err) {
1819
assert.Equal(t, uint64(0x7fa66cd10250), clok.ClockRef)
19-
assert.Equal(t, packet.SyncPacketMagic, clok.SyncMagic)
20-
assert.Equal(t, packet.CLOK, clok.MessageType)
2120
assert.Equal(t, uint64(0x113584970), clok.CorrelationID)
21+
assert.Equal(t, "SYNC_CLOK{ClockRef:7fa66cd10250, CorrelationID:113584970}", clok.String())
2222
}
2323
testSerializationOfClokReply(clok, t)
24+
_, err = packet.NewSyncClokPacketFromBytes(dat)
25+
assert.Error(t, err)
2426
}
2527

2628
func testSerializationOfClokReply(clok packet.SyncClokPacket, t *testing.T) {

screencapture/packet/sync_cvrp.go

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,33 @@ package packet
33
import (
44
"encoding/binary"
55
"fmt"
6+
67
"github.com/danielpaulus/quicktime_video_hack/screencapture/coremedia"
78
)
89

910
//SyncCvrpPacket contains all info from a CVRP packet sent by the device
1011
type SyncCvrpPacket struct {
11-
SyncMagic uint32
1212
ClockRef CFTypeID
13-
MessageType uint32
1413
CorrelationID uint64
1514
DeviceClockRef CFTypeID
1615
Payload coremedia.StringKeyDict
1716
}
1817

1918
//NewSyncCvrpPacketFromBytes parses a SyncCvrpPacket from a []byte
2019
func NewSyncCvrpPacketFromBytes(data []byte) (SyncCvrpPacket, error) {
21-
packet := SyncCvrpPacket{}
22-
packet.SyncMagic = binary.LittleEndian.Uint32(data)
23-
if packet.SyncMagic != SyncPacketMagic {
24-
return packet, fmt.Errorf("invalid SYNC Cvrp Packet: %x", data)
20+
remainingBytes, clockRef, correlationID, err := ParseSyncHeader(data, CVRP)
21+
if err != nil {
22+
return SyncCvrpPacket{}, err
2523
}
26-
24+
packet := SyncCvrpPacket{ClockRef: clockRef, CorrelationID: correlationID}
2725
packet.ClockRef = binary.LittleEndian.Uint64(data[4:])
2826
if packet.ClockRef != EmptyCFType {
29-
return packet, fmt.Errorf("Cvrp packet should have empty CFTypeID for ClockRef but has:%x", packet.ClockRef)
27+
return packet, fmt.Errorf("CVRP packet should have empty CFTypeID for ClockRef but has:%x", packet.ClockRef)
3028
}
31-
packet.MessageType = binary.LittleEndian.Uint32(data[12:])
32-
if packet.MessageType != CVRP {
33-
return packet, fmt.Errorf("wrong message type for Cvrp message: %x", packet.MessageType)
34-
}
35-
packet.CorrelationID = binary.LittleEndian.Uint64(data[16:])
36-
packet.DeviceClockRef = binary.LittleEndian.Uint64(data[24:])
37-
payloadDict, err := coremedia.NewStringDictFromBytes(data[32:])
29+
30+
packet.DeviceClockRef = binary.LittleEndian.Uint64(remainingBytes)
31+
32+
payloadDict, err := coremedia.NewStringDictFromBytes(remainingBytes[8:])
3833
if err != nil {
3934
return packet, err
4035
}

screencapture/packet/sync_cvrp_test.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
package packet_test
22

33
import (
4-
"github.com/danielpaulus/quicktime_video_hack/screencapture/packet"
5-
"github.com/stretchr/testify/assert"
64
"io/ioutil"
75
"log"
86
"testing"
7+
8+
"github.com/danielpaulus/quicktime_video_hack/screencapture/packet"
9+
"github.com/stretchr/testify/assert"
910
)
1011

12+
const expectedCvrpString = "SYNC_CVRP{ClockRef:1, CorrelationID:1135659d0, DeviceClockRef:113538da0, Payload:StringKeyDict:[{PreparedQueueHighWaterLevel : StringKeyDict:[{flags : Int32[1]},{value : UInt64[5]},{timescale : Int32[30]},{epoch : UInt64[0]},]},{PreparedQueueLowWaterLevel : StringKeyDict:[{flags : Int32[1]},{value : UInt64[3]},{timescale : Int32[30]},{epoch : UInt64[0]},]},{FormatDescription : fdsc:{MediaType:Video, VideoDimension:(1126x2436), Codec:AVC-1, PPS:27640033ac5680470133e69e6e04040404, SPS:28ee3cb0, Extensions:IndexKeyDict:[{49 : IndexKeyDict:[{105 : 0x01640033ffe1001127640033ac5680470133e69e6e0404040401000428ee3cb0fdf8f800},]},{52 : H.264},]}},]}"
13+
1114
func TestCvrp(t *testing.T) {
1215
dat, err := ioutil.ReadFile("fixtures/cvrp-request")
1316
if err != nil {
@@ -17,12 +20,13 @@ func TestCvrp(t *testing.T) {
1720
if assert.NoError(t, err) {
1821
assert.Equal(t, 3, len(cvrp.Payload.Entries))
1922
assert.Equal(t, packet.EmptyCFType, cvrp.ClockRef)
20-
assert.Equal(t, packet.SyncPacketMagic, cvrp.SyncMagic)
21-
assert.Equal(t, packet.CVRP, cvrp.MessageType)
2223
assert.Equal(t, uint64(0x113538da0), cvrp.DeviceClockRef)
2324
assert.Equal(t, uint64(0x1135659d0), cvrp.CorrelationID)
25+
assert.Equal(t, expectedCvrpString, cvrp.String())
2426
}
2527
testSerializationOfCvrpReply(cvrp, t)
28+
_, err = packet.NewSyncCvrpPacketFromBytes(dat)
29+
assert.Error(t, err)
2630
}
2731

2832
func testSerializationOfCvrpReply(cvrp packet.SyncCvrpPacket, t *testing.T) {

screencapture/packet/sync_cwpa.go

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,24 @@ import (
77

88
//SyncCwpaPacket contains all info from a CWPA packet sent by the device
99
type SyncCwpaPacket struct {
10-
SyncMagic uint32
1110
ClockRef CFTypeID
12-
MessageType uint32
1311
CorrelationID uint64
1412
DeviceClockRef CFTypeID
1513
}
1614

1715
//NewSyncCwpaPacketFromBytes parses a SyncCwpaPacket from a []byte
1816
func NewSyncCwpaPacketFromBytes(data []byte) (SyncCwpaPacket, error) {
19-
var packet = SyncCwpaPacket{}
20-
21-
packet.SyncMagic = binary.LittleEndian.Uint32(data)
22-
if packet.SyncMagic != SyncPacketMagic {
23-
return packet, fmt.Errorf("invalid SYNC CWPA Packet: %x", data)
17+
remainingBytes, clockRef, correlationID, err := ParseSyncHeader(data, CWPA)
18+
if err != nil {
19+
return SyncCwpaPacket{}, err
2420
}
25-
21+
packet := SyncCwpaPacket{ClockRef: clockRef, CorrelationID: correlationID}
2622
packet.ClockRef = binary.LittleEndian.Uint64(data[4:])
2723
if packet.ClockRef != EmptyCFType {
2824
return packet, fmt.Errorf("CWPA packet should have empty CFTypeID for ClockRef but has:%x", packet.ClockRef)
2925
}
30-
packet.MessageType = binary.LittleEndian.Uint32(data[12:])
31-
if packet.MessageType != CWPA {
32-
return packet, fmt.Errorf("wrong message type for CWPA message: %x", packet.MessageType)
33-
}
34-
packet.CorrelationID = binary.LittleEndian.Uint64(data[16:])
35-
packet.DeviceClockRef = binary.LittleEndian.Uint64(data[24:])
26+
27+
packet.DeviceClockRef = binary.LittleEndian.Uint64(remainingBytes)
3628
return packet, nil
3729
}
3830

screencapture/packet/sync_cwpa_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package packet_test
22

33
import (
4-
"github.com/danielpaulus/quicktime_video_hack/screencapture/packet"
5-
"github.com/stretchr/testify/assert"
64
"io/ioutil"
75
"log"
86
"testing"
7+
8+
"github.com/danielpaulus/quicktime_video_hack/screencapture/packet"
9+
"github.com/stretchr/testify/assert"
910
)
1011

1112
func TestCwpa(t *testing.T) {
@@ -16,10 +17,9 @@ func TestCwpa(t *testing.T) {
1617
cwpa, err := packet.NewSyncCwpaPacketFromBytes(dat[4:])
1718
if assert.NoError(t, err) {
1819
assert.Equal(t, packet.EmptyCFType, cwpa.ClockRef)
19-
assert.Equal(t, packet.SyncPacketMagic, cwpa.SyncMagic)
20-
assert.Equal(t, packet.CWPA, cwpa.MessageType)
2120
assert.Equal(t, uint64(0x1135a74e0), cwpa.DeviceClockRef)
2221
assert.Equal(t, uint64(0x113573de0), cwpa.CorrelationID)
22+
assert.Equal(t, "SYNC_CWPA{ClockRef:1, CorrelationID:113573de0, DeviceClockRef:1135a74e0}", cwpa.String())
2323
}
2424
_, err = packet.NewSyncCwpaPacketFromBytes(dat)
2525
assert.Error(t, err)

screencapture/packet/sync_og.go

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,20 @@ import (
77

88
//SyncOgPacket represents the OG Message. I do not know what these messages mean.
99
type SyncOgPacket struct {
10-
SyncMagic uint32
1110
ClockRef CFTypeID
12-
MessageType uint32
1311
CorrelationID uint64
1412
Unknown uint32
1513
}
1614

1715
//NewSyncOgPacketFromBytes parses a SyncOgPacket form bytes assuming it starts with SYNC magic and has the correct length.
1816
func NewSyncOgPacketFromBytes(data []byte) (SyncOgPacket, error) {
19-
packet := SyncOgPacket{}
20-
packet.SyncMagic = binary.LittleEndian.Uint32(data)
21-
if packet.SyncMagic != SyncPacketMagic {
22-
return packet, fmt.Errorf("invalid SYNC Og Packet: %x", data)
17+
remainingBytes, clockRef, correlationID, err := ParseSyncHeader(data, OG)
18+
if err != nil {
19+
return SyncOgPacket{}, err
2320
}
21+
packet := SyncOgPacket{ClockRef: clockRef, CorrelationID: correlationID}
2422

25-
packet.ClockRef = binary.LittleEndian.Uint64(data[4:])
26-
packet.MessageType = binary.LittleEndian.Uint32(data[12:])
27-
if packet.MessageType != OG {
28-
return packet, fmt.Errorf("wrong message type for OG message: %x", packet.MessageType)
29-
}
30-
packet.CorrelationID = binary.LittleEndian.Uint64(data[16:])
31-
packet.Unknown = binary.LittleEndian.Uint32(data[24:])
23+
packet.Unknown = binary.LittleEndian.Uint32(remainingBytes)
3224
return packet, nil
3325
}
3426

screencapture/packet/sync_og_test.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,28 @@
11
package packet_test
22

33
import (
4-
"github.com/danielpaulus/quicktime_video_hack/screencapture/packet"
5-
"github.com/stretchr/testify/assert"
64
"io/ioutil"
75
"log"
86
"testing"
7+
8+
"github.com/danielpaulus/quicktime_video_hack/screencapture/packet"
9+
"github.com/stretchr/testify/assert"
910
)
1011

1112
func TestOg(t *testing.T) {
1213
dat, err := ioutil.ReadFile("fixtures/og-request")
1314
if err != nil {
1415
log.Fatal(err)
1516
}
16-
clok, err := packet.NewSyncOgPacketFromBytes(dat[4:])
17+
og, err := packet.NewSyncOgPacketFromBytes(dat[4:])
1718
if assert.NoError(t, err) {
18-
assert.Equal(t, uint64(0x7fba35425ff0), clok.ClockRef)
19-
assert.Equal(t, packet.SyncPacketMagic, clok.SyncMagic)
20-
assert.Equal(t, packet.OG, clok.MessageType)
21-
assert.Equal(t, uint64(0x102d32f30), clok.CorrelationID)
19+
assert.Equal(t, uint64(0x7fba35425ff0), og.ClockRef)
20+
assert.Equal(t, uint64(0x102d32f30), og.CorrelationID)
21+
assert.Equal(t, "SYNC_OG{ClockRef:7fba35425ff0, CorrelationID:102d32f30, Unknown:1}", og.String())
2222
}
23-
testSerializationOfOgReply(clok, t)
23+
testSerializationOfOgReply(og, t)
24+
_, err = packet.NewSyncOgPacketFromBytes(dat)
25+
assert.Error(t, err)
2426
}
2527

2628
func testSerializationOfOgReply(clok packet.SyncOgPacket, t *testing.T) {

0 commit comments

Comments
 (0)