Skip to content

Commit c2b77dd

Browse files
author
danielPaulusMesmer
authored
Fix multiple device issue#44 (#59)
* replace opening by vid/pid with looking for the serialnumber * fix nil pointer panic
1 parent d060e9a commit c2b77dd

File tree

4 files changed

+38
-5
lines changed

4 files changed

+38
-5
lines changed

screencapture/activator.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
func EnableQTConfig(device IosDevice) (IosDevice, error) {
1616
udid := device.SerialNumber
1717
ctx := gousb.NewContext()
18-
usbDevice, err := ctx.OpenDeviceWithVIDPID(device.VID, device.PID)
18+
usbDevice, err := OpenDevice(ctx, device)
1919
if err != nil {
2020
return IosDevice{}, err
2121
}

screencapture/discovery.go

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,39 @@ type IosDevice struct {
1919
UsbInfo string
2020
}
2121

22+
//OpenDevice finds a gousb.Device by using the provided iosDevice.SerialNumber. It returns an open device handle.
23+
//Opening using VID and PID is not specific enough, as different iOS devices can have identical VID/PID combinations.
24+
func OpenDevice(ctx *gousb.Context, iosDevice IosDevice) (*gousb.Device, error) {
25+
deviceList, err := ctx.OpenDevices(func(desc *gousb.DeviceDesc) bool {
26+
return true
27+
})
28+
29+
if err != nil {
30+
log.Warn("Error opening usb devices", err)
31+
}
32+
var usbDevice *gousb.Device = nil
33+
for _, device := range deviceList {
34+
sn, err := device.SerialNumber()
35+
if err != nil {
36+
log.Warn("Error retrieving Serialnumber", err)
37+
}
38+
if sn == iosDevice.SerialNumber {
39+
usbDevice = device
40+
} else {
41+
device.Close()
42+
}
43+
}
44+
45+
if usbDevice == nil {
46+
return nil, fmt.Errorf("Unable to find device:%+v", iosDevice)
47+
}
48+
return usbDevice, nil
49+
}
50+
2251
//ReOpen creates a new Ios device, opening it using VID and PID, using the given context
23-
func (d *IosDevice) ReOpen(ctx *gousb.Context) (IosDevice, error) {
24-
dev, err := ctx.OpenDeviceWithVIDPID(d.VID, d.PID)
52+
func (d IosDevice) ReOpen(ctx *gousb.Context) (IosDevice, error) {
53+
54+
dev, err := OpenDevice(ctx, d)
2555
if err != nil {
2656
return IosDevice{}, err
2757
}
@@ -71,7 +101,7 @@ func FindIosDevice(udid string) (IosDevice, error) {
71101
return IosDevice{}, errors.New("no iOS devices are connected to this host")
72102
}
73103
if udid == "" {
74-
log.Debugf("no udid specified, using '%s'", list[0].SerialNumber)
104+
log.Infof("no udid specified, using '%s'", list[0].SerialNumber)
75105
return list[0], nil
76106
}
77107
for _, device := range list {

screencapture/gstadapter/gst_adapter_macos.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@ func (gsta GstAdapter) Stop() {
9898
log.Warn("Failed sending EOS signal for video app source")
9999
}
100100

101+
if gsta.pipeline == nil {
102+
return
103+
}
101104
bus := gsta.pipeline.GetBus()
102105

103106
//I hope those are 60 seconds

screencapture/usbadapter.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func (usa *UsbAdapter) StartReading(device IosDevice, receiver UsbDataReceiver,
2626
ctx, cleanUp := createContext()
2727
defer cleanUp()
2828

29-
usbDevice, err := ctx.OpenDeviceWithVIDPID(device.VID, device.PID)
29+
usbDevice, err := OpenDevice(ctx, device)
3030
if err != nil {
3131
return err
3232
}

0 commit comments

Comments
 (0)