LiVES 3.2.0
ldvgrab.c
Go to the documentation of this file.
1// ldvgrab.c
2// LiVES
3// (c) G. Finch 2006 - 2016 <salsaman@gmail.com>
4// released under the GNU GPL 3 or later
5// see file ../COPYING for licensing details
6
7// portions of this file (c) Dan Dennedy (dan@dennedy.org)
8
9#include <sys/poll.h>
10#include <netinet/in.h>
11#include <sys/resource.h>
12
13#define RX_CHANNEL 63
14#define RAW_BUF_SIZE 4096
15
16unsigned char g_rx_packet[RAW_BUF_SIZE]; /* the received packet data */
17int g_rx_length; /* the size of a received packet */
18int g_alldone = 0; /* flag to indicate when to quit */
20
22
23#include "main.h"
24#include "ldvinterface.h"
25#include "ldvcallbacks.h"
26
27int raw_iso_handler(raw1394handle_t handle, int channel, size_t length, quadlet_t *data) {
28 if (length < RAW_BUF_SIZE && channel == g_rx_channel) {
29 g_rx_length = length;
30 lives_memcpy(g_rx_packet, data, length);
31 }
32 return 0;
33}
34
35/* libraw1394 executes this when there is a bus reset. We'll just keep it
36 simple and quit */
37int reset_handler(raw1394handle_t handle, unsigned int generation) {
38 raw1394_update_generation(handle, generation);
40 return 0;
41}
42
43
44raw1394handle_t open_raw1394(void) {
45 int numcards;
46 struct raw1394_portinfo pinf[16];
47 raw1394handle_t handle;
48 struct pollfd raw1394_poll;
49
50 if (!(handle = raw1394_new_handle())) {
51 d_print(_("raw1394 - couldn't get handle"));
52 do_error_dialog(_("\nThe ieee1394 driver is not loaded or /dev/raw1394 does not exist.\n"));
53 return NULL;
54 }
55
56 if ((numcards = raw1394_get_port_info(handle, pinf, 16)) < 0) {
57 do_error_dialog(_("\nraw1394 - couldn't get card info.\n"));
58 return NULL;
59 }
60
61 /* port 0 is the first host adapter card */
62 if (raw1394_set_port(handle, 0) < 0) {
63 do_error_dialog(_("\nraw1394 - couldn't set port.\n"));
64 return NULL;
65 }
66
67 /* tell libraw1394 the names of our callback functions */
68 //raw1394_set_iso_handler(handle, g_rx_channel, raw_iso_handler);
69 raw1394_set_bus_reset_handler(handle, reset_handler);
70
71 /* poll for leftover events */
72 raw1394_poll.fd = raw1394_get_fd(handle);
73 raw1394_poll.events = POLLIN;
74
75 while (1) {
76 if (poll(&raw1394_poll, 1, 10) < 1) break;
77 raw1394_loop_iterate(handle);
78 }
79
80 /* Starting iso receive */
81 /* if (raw1394_start_iso_rcv(handle, g_rx_channel) < 0) {
82 do_error_dialog(_("\nraw1394 - couldn't start iso receive.\n"));
83 return NULL;
84 }*/
85 return handle;
86}
87
88
89void close_raw1394(raw1394handle_t handle) {
90 //raw1394_stop_iso_rcv(handle, g_rx_channel);
91 raw1394_destroy_handle(handle);
92}
93
94
95void camdest(s_cam *cam) {
96 raw1394_destroy_handle(cam->handle);
97 lives_free(cam);
98}
99
100
102 rom1394_directory rom_dir;
103
104 struct raw1394_portinfo pinf[16];
105
106 s_cam *cam = (s_cam *)lives_malloc(sizeof(s_cam));
107
108 char *msg;
109
110 int n_ports;
111
112 register int i, j;
113
114 cam->device = -1;
115
116#ifdef RAW1394_V_0_8
117 cam->handle = raw1394_get_handle();
118#else
119 cam->handle = raw1394_new_handle();
120#endif
121
122 if (!cam->handle) {
123 if (!errno) {
124 do_error_dialog(_("\nraw1394 device not compatible!\n"));
125 } else {
126 d_print(_("Couldn't get 1394 handle"));
127 do_error_dialog(_("\nIs ieee1394, driver, and raw1394 loaded?\n"));
128 }
129 return NULL;
130 }
131
132 if ((n_ports = raw1394_get_port_info(cam->handle, pinf, 16)) < 0) {
133 msg = lives_strdup_printf(_("raw1394 - failed to get port info: %s.\n"), lives_strerror(errno));
134 d_print(msg);
135 lives_free(msg);
136 raw1394_destroy_handle(cam->handle);
137 return NULL;
138 }
139
140
141 for (j = 0; j < n_ports && cam->device == -1; j++) {
142 if (raw1394_set_port(cam->handle, j) < 0) {
143 msg = lives_strdup_printf(_("\nraw1394 - couldn't set port %d !\n"), j);
144 d_print(msg);
145 lives_free(msg);
146 continue;
147 }
148
149 for (i = 0; i < raw1394_get_nodecount(cam->handle); ++i) {
150 if (rom1394_get_directory(cam->handle, i, &rom_dir) < 0) {
151 msg = lives_strdup_printf(_("error reading config rom directory for node %d\n"), i);
152 d_print(msg);
153 lives_free(msg);
154 continue;
155 }
156
157 if ((rom1394_get_node_type(&rom_dir) == ROM1394_NODE_TYPE_AVC) &&
158 avc1394_check_subunit_type(cam->handle, i, AVC1394_SUBUNIT_TYPE_VCR)) {
159 cam->device = i;
160 break;
161 }
162 }
163 }
164
165 if (0 && cam->device == -1) {
167 _("\nLiVES could not find any firewire camera.\nPlease make sure your camera is switched on,\n"
168 "and check that you have read/write permissions for the camera device\n(generally /dev/raw1394*).\n"));
169 raw1394_destroy_handle(cam->handle);
170 return NULL;
171 }
172
173 return cam;
174}
175
176
178
179void camplay(s_cam *cam) {
180 avc1394_vcr_play(cam->handle, cam->device);
181}
182
183
184void camstop(s_cam *cam) {
185 g_alldone = 1;
186 avc1394_vcr_stop(cam->handle, cam->device);
187}
188
189
190void camrew(s_cam *cam) {
191 avc1394_vcr_rewind(cam->handle, cam->device);
192}
193
194
195void camff(s_cam *cam) {
196 avc1394_vcr_forward(cam->handle, cam->device);
197}
198
199
200void campause(s_cam *cam) {
201 avc1394_vcr_pause(cam->handle, cam->device);
202}
203
204
205void cameject(s_cam *cam) {
206 avc1394_vcr_eject(cam->handle, cam->device);
207}
208
209
211
212char *find_free_camfile(int format) {
213 char *filename = lives_strdup(lives_entry_get_text(LIVES_ENTRY(dvgrabw->filent)));
214 char *fname, *tmp = NULL, *tmp2, *tmp3;
215
216 register int i;
217
218 if (format == CAM_FORMAT_HDV) {
219 for (i = 1; i < 10000; i++) {
220 fname = lives_strdup_printf("%s%04d.mpg", filename, i);
221 if (!lives_file_test((tmp = lives_build_filename((tmp2 = lives_filename_from_utf8(dvgrabw->dirname, -1, NULL, NULL, NULL)),
222 (tmp3 = lives_filename_from_utf8(fname, -1, NULL, NULL, NULL)), NULL)),
223 LIVES_FILE_TEST_EXISTS)) break;
224 lives_free(tmp);
225 lives_free(tmp2);
226 lives_free(tmp3);
227 tmp = NULL;
228 }
229 } else {
230 for (i = 1; i < 1000; i++) {
231 fname = lives_strdup_printf("%s%03d.dv", filename, i);
232 if (!lives_file_test((tmp = lives_build_filename((tmp2 = lives_filename_from_utf8(dvgrabw->dirname, -1, NULL, NULL, NULL)),
233 (tmp3 = lives_filename_from_utf8(fname, -1, NULL, NULL, NULL)), NULL)),
234 LIVES_FILE_TEST_EXISTS)) break;
235 lives_free(tmp);
236 lives_free(tmp2);
237 lives_free(tmp3);
238 tmp = NULL;
239 }
240 }
241 if (tmp) lives_free(tmp);
242 lives_free(filename);
243
244 return fname;
245}
246
247
248boolean rec(s_cam *cam) {
249 // returns filename of file being written
250
251 char *tmp2, *tmp3, *com;
252 char *splits;
253
254 if (cam->pgid != 0) return FALSE;
255
256 if (lives_toggle_button_get_active(LIVES_TOGGLE_BUTTON(dvgrabw->split))) splits = lives_strdup("-autosplit ");
257 else splits = lives_strdup("");
258
259 if (cam->format == CAM_FORMAT_DV) {
260 // dv format
261#ifndef IS_MINGW
262 com = lives_strdup_printf("dvgrab -format raw %s\"%s/%s\" >/dev/null 2>&1 &", splits,
263 (tmp2 = lives_filename_from_utf8(dvgrabw->dirname, -1, NULL, NULL, NULL)),
264 (tmp3 = lives_filename_from_utf8(dvgrabw->filename, -1, NULL, NULL, NULL)));
265#else
266 com = lives_strdup_printf("dvgrab.exe -format raw %s\"%s\\%s\" >NUL 2>&1 &", splits,
267 (tmp2 = lives_filename_from_utf8(dvgrabw->dirname, -1, NULL, NULL, NULL)),
268 (tmp3 = lives_filename_from_utf8(dvgrabw->filename, -1, NULL, NULL, NULL)));
269#endif
270 cam->pgid = lives_fork(com);
271 lives_free(com);
272 lives_free(tmp2);
273 lives_free(tmp3);
274 lives_free(splits);
275 return TRUE;
276 }
277
278 // hdv format
279#ifndef IS_MINGW
280 com = lives_strdup_printf("dvgrab -format mpeg2 %s\"%s/%s\" >/dev/null 2>&1 &", splits,
281 (tmp2 = lives_filename_from_utf8(dvgrabw->dirname, -1, NULL, NULL, NULL)),
282 (tmp3 = lives_filename_from_utf8(dvgrabw->filename, -1, NULL, NULL, NULL)));
283#else
284 com = lives_strdup_printf("dvgrab.exe -format mpeg2 %s\"%s\\%s\" >NUL 2>&1 &", splits,
285 (tmp2 = lives_filename_from_utf8(dvgrabw->dirname, -1, NULL, NULL, NULL)),
286 (tmp3 = lives_filename_from_utf8(dvgrabw->filename, -1, NULL, NULL, NULL)));
287#endif
288
289 cam->pgid = lives_fork(com);
290
291 lives_free(com);
292 lives_free(tmp2);
293 lives_free(tmp3);
294 lives_free(splits);
295
296 return TRUE;
297}
298
299
300void on_open_fw_activate(LiVESMenuItem *menuitem, livespointer user_data) {
301 int type = LIVES_POINTER_TO_INT(user_data); // type 0==dv, type 1==hdv
302 s_cam *cam;
303
306 return;
307 }
308
309 if (type == CAM_FORMAT_DV && !capable->has_dvgrab) {
311 return;
312 }
313
314 cam = camready();
315 if (!cam) return;
316
317 /* if (type==CAM_FORMAT_HDV) {
318 cam->rec_handle=open_raw1394();
319 if (cam->rec_handle==NULL) return;
320 }
321 else*/
322 cam->rec_handle = NULL;
323
324 if (mainw->multitrack) {
325 if (mainw->multitrack->idlefunc > 0) {
327 mainw->multitrack->idlefunc = 0;
328 }
330 }
331
332 dvgrabw = create_camwindow(cam, type);
334 dvgrabw->cursor = NULL;
335 cam->format = type;
336 cam->grabbed_clips = FALSE;
337 cam->pgid = 0;
338 dvgrabw->cam = cam;
339}
340
LIVES_GLOBAL_INLINE void do_dvgrab_error(void)
Definition: dialogs.c:3714
LIVES_GLOBAL_INLINE void do_need_mplayer_dialog(void)
Definition: dialogs.c:3020
LIVES_GLOBAL_INLINE LiVESResponseType do_error_dialog(const char *text)
Definition: dialogs.c:749
void on_camquit_clicked(LiVESButton *button, livespointer user_data)
Definition: ldvcallbacks.c:95
#define RX_CHANNEL
Definition: ldvgrab.c:13
void camstop(s_cam *cam)
Definition: ldvgrab.c:184
void cameject(s_cam *cam)
Definition: ldvgrab.c:205
void camplay(s_cam *cam)
Definition: ldvgrab.c:179
int g_rx_channel
Definition: ldvgrab.c:19
void camrew(s_cam *cam)
Definition: ldvgrab.c:190
int g_rx_length
Definition: ldvgrab.c:17
char * find_free_camfile(int format)
Definition: ldvgrab.c:212
void close_raw1394(raw1394handle_t handle)
Definition: ldvgrab.c:89
void campause(s_cam *cam)
Definition: ldvgrab.c:200
int reset_handler(raw1394handle_t handle, unsigned int generation)
Definition: ldvgrab.c:37
void camdest(s_cam *cam)
Definition: ldvgrab.c:95
boolean rec(s_cam *cam)
Definition: ldvgrab.c:248
void camff(s_cam *cam)
Definition: ldvgrab.c:195
unsigned char g_rx_packet[RAW_BUF_SIZE]
Definition: ldvgrab.c:16
int raw_iso_handler(raw1394handle_t handle, int channel, size_t length, quadlet_t *data)
Definition: ldvgrab.c:27
int g_alldone
Definition: ldvgrab.c:18
s_cam * camready(void)
Definition: ldvgrab.c:101
void on_open_fw_activate(LiVESMenuItem *menuitem, livespointer user_data)
Definition: ldvgrab.c:300
raw1394handle_t open_raw1394(void)
Definition: ldvgrab.c:44
#define RAW_BUF_SIZE
Definition: ldvgrab.c:14
#define CAM_FORMAT_HDV
Definition: ldvgrab.h:15
struct _dvgrabw * dvgrabw
Definition: ldvgrab.h:61
#define CAM_FORMAT_DV
Definition: ldvgrab.h:14
struct _dvgrabw * create_camwindow(s_cam *cam, int type)
Definition: ldvinterface.c:12
#define lives_free
Definition: machinestate.h:52
#define lives_malloc
Definition: machinestate.h:46
#define lives_memcpy
Definition: machinestate.h:55
mainwindow * mainw
Definition: main.c:103
lives_pid_t lives_fork(const char *com)
Definition: utils.c:288
capability * capable
Definition: main.h:627
void d_print(const char *fmt,...)
Definition: utils.c:2542
void mt_desensitise(lives_mt *mt)
Definition: multitrack.c:16979
LiVESWidget * filent
Definition: ldvgrab.h:45
LiVESXCursor * cursor
Definition: ldvgrab.h:53
s_cam * cam
Definition: ldvgrab.h:57
char * filename
Definition: ldvgrab.h:56
char * dirname
Definition: ldvgrab.h:55
LiVESWidget * split
Definition: ldvgrab.h:52
LiVESWidget * dialog
Definition: ldvgrab.h:44
lives_checkstatus_t has_mplayer2
Definition: main.h:512
lives_checkstatus_t has_dvgrab
Definition: main.h:507
lives_checkstatus_t has_mplayer
Definition: main.h:511
lives_mt * multitrack
holds a pointer to the entire multitrack environment; NULL in Clip Edit mode
Definition: mainwindow.h:1087
Definition: ldvgrab.h:17
lives_pgid_t pgid
Definition: ldvgrab.h:23
raw1394handle_t rec_handle
Definition: ldvgrab.h:19
raw1394handle_t handle
Definition: ldvgrab.h:18
boolean grabbed_clips
Definition: ldvgrab.h:22
int format
Definition: ldvgrab.h:21
int device
Definition: ldvgrab.h:20
#define lives_strdup_printf(fmt,...)
Definition: support.c:27
#define _(String)
Definition: support.h:44
#define TRUE
Definition: videoplugin.h:59
#define FALSE
Definition: videoplugin.h:60
WIDGET_HELPER_GLOBAL_INLINE boolean lives_toggle_button_get_active(LiVESToggleButton *button)
WIDGET_HELPER_GLOBAL_INLINE const char * lives_entry_get_text(LiVESEntry *entry)
WIDGET_HELPER_GLOBAL_INLINE boolean lives_widget_show_all(LiVESWidget *widget)
WIDGET_HELPER_GLOBAL_INLINE boolean lives_source_remove(uint32_t handle)