Skip to content

Commit 11d4f9b

Browse files
authored
Fix error handling and improve logs for record, activate commands (#35)
* fix error handling * fix error messages, move logrus to json
1 parent aa20e80 commit 11d4f9b

File tree

5 files changed

+54
-54
lines changed

5 files changed

+54
-54
lines changed

main.go

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@ import (
1414
log "github.com/sirupsen/logrus"
1515
)
1616

17-
const version = "v0.1-alpha"
17+
const version = "v0.2-beta"
1818

1919
func main() {
2020
usage := fmt.Sprintf(`Q.uickTime V.ideo H.ack (qvh) %s
2121
2222
Usage:
2323
qvh devices [-v]
2424
qvh activate [--udid=<udid>] [-v]
25-
qvh record <h264file> <wavfile> [-v]
25+
qvh record <h264file> <wavfile> [-v] [--udid=<udid>]
2626
qvh gstreamer [-v]
2727
qvh --version | version
2828
@@ -42,6 +42,7 @@ The commands work as following:
4242
gstreamer qvh will open a new window and push AV data to gstreamer.
4343
`, version)
4444
arguments, _ := docopt.ParseDoc(usage)
45+
log.SetFormatter(&log.JSONFormatter{})
4546

4647
verboseLoggingEnabled, _ := arguments.Bool("-v")
4748
if verboseLoggingEnabled {
@@ -103,18 +104,6 @@ func startGStreamer(udid string) {
103104
startWithConsumer(gStreamer, udid)
104105
}
105106

106-
func waitForSigInt(stopSignalChannel chan interface{}) {
107-
c := make(chan os.Signal, 1)
108-
signal.Notify(c, os.Interrupt)
109-
go func() {
110-
for sig := range c {
111-
log.Debugf("Signal received: %s", sig)
112-
var stopSignal interface{}
113-
stopSignalChannel <- stopSignal
114-
}
115-
}()
116-
}
117-
118107
// Just dump a list of what was discovered to the console
119108
func devices() {
120109
deviceList, err := screencapture.FindIosDevices()
@@ -134,11 +123,16 @@ func devices() {
134123
// This command is for testing if we can enable the hidden Quicktime device config
135124
func activate(udid string) {
136125
device, err := screencapture.FindIosDevice(udid)
126+
if err != nil {
127+
printErrJSON(err, "no device found to activate")
128+
return
129+
}
137130

138131
log.Debugf("Enabling device: %v", device)
139132
device, err = screencapture.EnableQTConfig(device)
140133
if err != nil {
141-
log.Fatal("Error enabling QT config", err)
134+
printErrJSON(err, "Error enabling QT config")
135+
return
142136
}
143137

144138
printJSON(map[string]interface{}{
@@ -185,27 +179,45 @@ func record(h264FilePath string, wavFilePath string, udid string) {
185179
}
186180

187181
func startWithConsumer(consumer screencapture.CmSampleBufConsumer, udid string) {
188-
activate(udid)
189-
190-
deviceList, err := screencapture.FindIosDevices()
191-
182+
device, err := screencapture.FindIosDevice(udid)
192183
if err != nil {
193-
log.Fatal("Error finding iOS Devices", err)
184+
printErrJSON(err, "no device found to activate")
185+
return
194186
}
195187

196-
dev := deviceList[0]
188+
device, err = screencapture.EnableQTConfig(device)
189+
if err != nil {
190+
printErrJSON(err, "Error enabling QT config")
191+
return
192+
}
197193

198194
adapter := screencapture.UsbAdapter{}
199195
stopSignal := make(chan interface{})
200196
waitForSigInt(stopSignal)
201197
mp := screencapture.NewMessageProcessor(&adapter, stopSignal, consumer)
202198

203-
adapter.StartReading(dev, &mp, stopSignal)
199+
err = adapter.StartReading(device, &mp, stopSignal)
200+
if err != nil {
201+
printErrJSON(err, "failed connecting to usb")
202+
}
203+
}
204+
205+
func waitForSigInt(stopSignalChannel chan interface{}) {
206+
c := make(chan os.Signal, 1)
207+
signal.Notify(c, os.Interrupt)
208+
go func() {
209+
for sig := range c {
210+
log.Debugf("Signal received: %s", sig)
211+
var stopSignal interface{}
212+
stopSignalChannel <- stopSignal
213+
}
214+
}()
204215
}
216+
205217
func printErrJSON(err error, msg string) {
206218
printJSON(map[string]interface{}{
207-
"originalError": err.Error(),
208-
"message": msg,
219+
"original_error": err.Error(),
220+
"error_message": msg,
209221
})
210222
}
211223
func printJSON(output map[string]interface{}) {

screencapture/activator.go

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,7 @@ func EnableQTConfig(device IosDevice) (IosDevice, error) {
2424
return device, nil
2525
}
2626

27-
err = sendQTConfigControlRequest(usbDevice)
28-
if err != nil {
29-
return IosDevice{}, err
30-
}
27+
sendQTConfigControlRequest(usbDevice)
3128

3229
var i int
3330
for {
@@ -56,25 +53,22 @@ func EnableQTConfig(device IosDevice) (IosDevice, error) {
5653
return device, err
5754
}
5855

59-
func sendQTConfigControlRequest(device *gousb.Device) error {
56+
func sendQTConfigControlRequest(device *gousb.Device) {
6057
response := make([]byte, 0)
6158
val, err := device.Control(0x40, 0x52, 0x00, 0x02, response)
62-
6359
if err != nil {
64-
log.Warn("Failed sending control transfer for enabling hidden QT config. Seems like this happens sometimes but it still works usually.", err)
60+
log.Warnf("Failed sending control transfer for enabling hidden QT config. Seems like this happens sometimes but it still works usually: %s", err)
6561
}
6662
log.Debugf("Enabling QT config RC:%d", val)
67-
return nil
6863
}
6964

70-
func sendQTDisableConfigControlRequest(device *gousb.Device) error {
65+
func sendQTDisableConfigControlRequest(device *gousb.Device) {
7166
response := make([]byte, 0)
7267
val, err := device.Control(0x40, 0x52, 0x00, 0x00, response)
7368

7469
if err != nil {
75-
log.Fatal("Failed sending control transfer for disabling hidden QT config", err)
76-
return err
70+
log.Warnf("Failed sending control transfer for disabling hidden QT config:%s", err)
71+
7772
}
7873
log.Debugf("Disabled QT config RC:%d", val)
79-
return nil
8074
}

screencapture/discovery.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func FindIosDevice(udid string) (IosDevice, error) {
6868
return IosDevice{}, err
6969
}
7070
if len(list) == 0 {
71-
return IosDevice{}, errors.New("could not find any iOS device on this host")
71+
return IosDevice{}, errors.New("no iOS devices are connected to this host")
7272
}
7373
if udid == "" {
7474
log.Debugf("no udid specified, using '%s'", list[0].SerialNumber)

screencapture/messageprocessor.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func NewMessageProcessorWithClockBuilder(usbWriter UsbWriter, stopSignal chan in
5050
func (mp *MessageProcessor) ReceiveData(data []byte) {
5151
switch binary.LittleEndian.Uint32(data) {
5252
case packet.PingPacketMagic:
53-
log.Debug("initial ping received, sending ping back")
53+
log.Info("AudioVideo-Stream has started")
5454
mp.usbWriter.WriteDataToUsb(packet.NewPingPacketAsBytes())
5555
return
5656
case packet.SyncPacketMagic:

screencapture/usbadapter.go

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -42,29 +42,25 @@ func (usa *UsbAdapter) StartReading(device IosDevice, receiver UsbDataReceiver,
4242
return errors.New("Could not retrieve config")
4343
}
4444

45-
err = sendQTConfigControlRequest(usbDevice)
45+
sendQTConfigControlRequest(usbDevice)
4646

47-
if err != nil {
48-
log.Error("Error disabling config", err)
49-
}
50-
51-
log.Infof("QT Config is active: %s", config.String())
47+
log.Debugf("QT Config is active: %s", config.String())
5248

5349
val, err := usbDevice.Control(0x02, 0x01, 0, 0x86, make([]byte, 0))
5450
if err != nil {
55-
log.Warn("failed control", err)
51+
log.Debug("failed control", err)
5652
}
57-
log.Infof("Clear Feature RC: %d", val)
53+
log.Debugf("Clear Feature RC: %d", val)
5854

5955
val, err = usbDevice.Control(0x02, 0x01, 0, 0x05, make([]byte, 0))
6056
if err != nil {
61-
log.Warn("failed control", err)
57+
log.Debug("failed control", err)
6258
}
63-
log.Infof("Clear Feature RC: %d", val)
59+
log.Debugf("Clear Feature RC: %d", val)
6460

6561
iface, err := grabQuickTimeInterface(config)
6662
if err != nil {
67-
log.Error("Couldnt get Quicktime Interface")
63+
log.Debug("could not get Quicktime Interface")
6864
return err
6965
}
7066
log.Debugf("Got QT iface:%s", iface.String())
@@ -98,7 +94,7 @@ func (usa *UsbAdapter) StartReading(device IosDevice, receiver UsbDataReceiver,
9894
return err
9995
}
10096
log.Debug("Endpoint claimed")
101-
97+
log.Infof("Device '%s' USB connection ready", device.SerialNumber)
10298
go func() {
10399

104100
frameExtractor := NewLengthFieldBasedFrameExtractor()
@@ -128,10 +124,8 @@ func (usa *UsbAdapter) StartReading(device IosDevice, receiver UsbDataReceiver,
128124
log.Info("Closing usb interface")
129125
iface.Close()
130126

131-
err = sendQTDisableConfigControlRequest(usbDevice)
132-
if err != nil {
133-
log.Error("Error sending disable control request", err)
134-
}
127+
sendQTDisableConfigControlRequest(usbDevice)
128+
135129
return nil
136130
}
137131

0 commit comments

Comments
 (0)