Skip to content

Commit 35e1b3a

Browse files
committed
Refactoring, fix crash
1 parent da3eb77 commit 35e1b3a

File tree

1 file changed

+23
-19
lines changed

1 file changed

+23
-19
lines changed

LiveViewKit/LVContext.m

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ + (instancetype)sharedInstance {
4545
- (instancetype)init {
4646
self = [super init];
4747
if (self) {
48-
// start alerts loop
4948
[self uvcInit];
5049
}
5150
return self;
@@ -69,13 +68,21 @@ - (void)reload {
6968
if (ctx) {
7069
[self uvcDeinit];
7170
}
71+
72+
// clear-out the vars
73+
ctx = NULL;
74+
dev = NULL;
75+
devh = NULL;
76+
7277
[self uvcInit];
7378
[self start];
7479
}
7580

7681
- (void)changeFrameDesc:(LVFrameDesc)frameDesc {
77-
[self uvcStopStreaming];
78-
[self uvcStartStreamingWithFrameDesc:frameDesc];
82+
if (devh) {
83+
[self uvcStopStreaming];
84+
[self uvcStartStreamingWithFrameDesc:frameDesc];
85+
}
7986
}
8087

8188
- (int)uvcInit {
@@ -120,13 +127,24 @@ - (int)uvcOpenDevice {
120127
return 0;
121128
}
122129

123-
- (int)uvcGetFrameDescriptors {
124-
// Select uncompressed format
130+
- (const uvc_format_desc_t *)uvcGetUncompressedFormatDesc {
125131
const uvc_format_desc_t *format_desc = uvc_get_format_descs(devh);
126132
while (format_desc && format_desc->bDescriptorSubtype != UVC_VS_FORMAT_UNCOMPRESSED) {
127133
format_desc = format_desc->next;
128134
}
129135

136+
if (format_desc == NULL) {
137+
printf("Can't find uncompressed format\n");
138+
return NULL;
139+
}
140+
141+
return format_desc;
142+
}
143+
144+
- (int)uvcGetFrameDescriptors {
145+
// Select uncompressed format
146+
const uvc_format_desc_t *format_desc = [self uvcGetUncompressedFormatDesc];
147+
130148
// format_desc->bNumFrameDescriptors returns 0
131149
uint8_t bNumFrameDescriptors = 0;
132150
const uvc_frame_desc_t *frame_desc = format_desc->frame_descs;
@@ -151,20 +169,6 @@ - (int)uvcGetFrameDescriptors {
151169
return 0;
152170
}
153171

154-
- (const uvc_format_desc_t *)uvcGetUncompressedFormatDesc {
155-
const uvc_format_desc_t *format_desc = uvc_get_format_descs(devh);
156-
while (format_desc && format_desc->bDescriptorSubtype != UVC_VS_FORMAT_UNCOMPRESSED) {
157-
format_desc = format_desc->next;
158-
}
159-
160-
if (format_desc == NULL) {
161-
printf("Can't find uncompressed format\n");
162-
return NULL;
163-
}
164-
165-
return format_desc;
166-
}
167-
168172
- (int)uvcStartStreaming {
169173
const uvc_format_desc_t *format_desc = [self uvcGetUncompressedFormatDesc];
170174

0 commit comments

Comments
 (0)