]> git.sesse.net Git - vlc/blob - modules/access/dc1394.c
453fe87762e070028800b27eeb5c42e58cdb176d
[vlc] / modules / access / dc1394.c
1 /*****************************************************************************
2  * dc1394.c: firewire input module
3  *****************************************************************************
4  * Copyright (C) 2006 the VideoLAN team
5  *
6  * Authors: Xant Majere <xant@xant.net>
7  *
8  *****************************************************************************
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation;
12  * version 2 of the License.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  *
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <vlc/vlc.h>
34 #include <vlc_input.h>
35 #include <vlc_vout.h>
36 #include <vlc_demux.h>
37
38
39 #ifdef HAVE_FCNTL_H
40 #   include <fcntl.h>
41 #endif
42 #ifdef HAVE_UNISTD_H
43 #   include <unistd.h>
44 #elif defined( WIN32 ) && !defined( UNDER_CE )
45 #   include <io.h>
46 #endif
47
48 #include <sys/ioctl.h>
49 #include <sys/soundcard.h>
50
51 #include <libraw1394/raw1394.h>
52 #include <libdc1394/dc1394_control.h>
53
54 #define MAX_IEEE1394_HOSTS 32
55 #define MAX_CAMERA_NODES 32
56
57 /*****************************************************************************
58  * Module descriptor
59  *****************************************************************************/
60 static int  Open ( vlc_object_t * );
61 static void Close( vlc_object_t * );
62 static void OpenAudioDev( demux_t *p_demux );
63 static inline void CloseAudioDev( demux_t *p_demux );
64
65 vlc_module_begin();
66     set_description( _("dc1394 input") );
67     set_capability( "access_demux", 10 );
68     add_shortcut( "dc1394" );
69     set_callbacks( Open, Close );
70 vlc_module_end();
71
72 typedef struct __dc_camera
73 {
74     int port;
75     nodeid_t node;
76     u_int64_t uid;
77 } dc_camera;
78
79 typedef struct demux_sys_t
80 {
81     dc1394_cameracapture camera;
82     picture_t            pic;
83     int                  dma_capture;
84 #define DMA_OFF 0
85 #define DMA_ON 1
86     int                 num_ports;
87     int                 num_cameras;
88     int                 selected_camera;
89     u_int64_t           selected_uid;
90
91     dc_camera           *camera_nodes;
92     dc1394_camerainfo   camera_info;
93     dc1394_miscinfo     misc_info;
94     raw1394handle_t     fd_video;
95     quadlet_t           supported_framerates;
96
97     int                 width;
98     int                 height;
99     int                 frame_size;
100     int                 frame_rate;
101     unsigned int        brightness;
102     unsigned int        focus;
103     char                *dma_device;
104     es_out_id_t         *p_es_video;
105
106     /* audio stuff */
107     int                 i_sample_rate;
108     int                 channels;
109     int                 i_audio_max_frame_size;
110     int                 fd_audio;
111     char                *audio_device;
112 #define NO_ROTATION 0
113 #define ROTATION_LEFT 1
114 #define ROTATION_RIGHT 2
115     es_out_id_t         *p_es_audio;
116 } dc1394_sys;
117
118 /*****************************************************************************
119  * Local prototypes
120  *****************************************************************************/
121 static int Demux( demux_t *p_demux );
122 static int Control( demux_t *, int, va_list );
123 static block_t *GrabVideo( demux_t *p_demux );
124 static block_t *GrabAudio( demux_t *p_demux );
125 static int process_options( demux_t *p_demux);
126
127 /*****************************************************************************
128  * ScanCameras
129  *****************************************************************************/
130 static void ScanCameras( dc1394_sys *sys, demux_t *p_demux )
131 {
132     struct raw1394_portinfo portinfo[MAX_IEEE1394_HOSTS];
133     raw1394handle_t tempFd;
134     dc1394_camerainfo  info;
135     dc_camera *node_list = NULL;
136     nodeid_t  *nodes = NULL;
137     int num_ports = 0;
138     int num_cameras = 0;
139     int nodecount;
140     int i, n;
141
142     memset( &portinfo, 0, sizeof(portinfo) );
143
144     msg_Dbg( p_demux, "Scanning for ieee1394 ports ..." );
145
146     tempFd = raw1394_new_handle();
147     if( !tempFd )
148         return VLC_EGENERIC;
149     raw1394_get_port_info( tempFd, portinfo, MAX_IEEE1394_HOSTS);
150     raw1394_destroy_handle( tempFd );
151
152     for( i=0; i < MAX_IEEE1394_HOSTS; i++ )
153     {
154         /* check if port exists and has at least one node*/
155         if( !portinfo[i].nodes )
156             continue;
157         nodes = NULL;
158         nodecount = 0;
159         tempFd = dc1394_create_handle( i );
160
161         /* skip this port if we can't obtain a valid handle */
162         if( !tempFd )
163             continue;
164         msg_Dbg( p_demux, "Found ieee1394 port %d (%s) ... "
165                           "checking for camera nodes",
166                           i, portinfo[i].name );
167         num_ports++;
168
169         nodes = dc1394_get_camera_nodes( tempFd, &nodecount, 0 );
170         if( nodecount )
171         {
172             msg_Dbg( p_demux, "Found %d dc1394 cameras on port %d (%s)",
173                      nodecount, i, portinfo[i].name );
174
175             if( node_list )
176                 node_list = realloc( node_list, sizeof(dc_camera) * (num_cameras+nodecount) );
177             else
178                 node_list = malloc( sizeof(dc_camera) * nodecount);
179
180             for( n = 0; n < nodecount; n++ )
181             {
182                 int result = 0;
183
184                 result = dc1394_get_camera_info( tempFd, nodes[n], &info );
185                 if( result == DC1394_SUCCESS )
186                 {
187                     node_list[num_cameras+n].uid = info.euid_64;
188                 }
189                 node_list[num_cameras+n].node = nodes[n];
190                 node_list[num_cameras+n].port = i;
191             }
192             num_cameras += nodecount;
193         }
194         else
195             msg_Dbg( p_demux, "no cameras found  on port %d (%s)",
196                      i, portinfo[i].name );
197
198         if( tempFd )
199             dc1394_destroy_handle( tempFd );
200     }
201     sys->num_ports = num_ports;
202     sys->num_cameras = num_cameras;
203     sys->camera_nodes = node_list;
204 }
205
206 /*****************************************************************************
207  * Open:
208  *****************************************************************************/
209 static int Open( vlc_object_t *p_this )
210 {
211     demux_t     *p_demux = (demux_t*)p_this;
212     demux_sys_t *p_sys;
213     es_format_t fmt;
214     int i;
215     int i_width;
216     int i_height;
217     int i_aspect;
218     int result = 0;
219
220     /* Set up p_demux */
221     p_demux->pf_demux = Demux;
222     p_demux->pf_control = Control;
223     p_demux->info.i_update = 0;
224     p_demux->info.i_title = 0;
225     p_demux->info.i_seekpoint = 0;
226
227     p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
228     if( !p_sys )
229     {
230         msg_Err( p_demux, "not enough memory available" );
231         return VLC_ENOMEM;
232     }
233     memset( p_sys, 0, sizeof( demux_sys_t ) );
234     memset( &fmt, 0, sizeof( es_format_t ) );
235
236     /* DEFAULTS */
237     p_sys->frame_size = MODE_640x480_YUV422;
238     p_sys->width      = 640;
239     p_sys->height     = 480;
240     p_sys->frame_rate = FRAMERATE_30;
241     p_sys->brightness = 200;
242     p_sys->focus      = 0;
243     p_sys->dma_capture = DMA_ON; /* defaults to VIDEO1394 capture mode */
244     p_sys->fd_audio   = -1;
245     p_sys->fd_video   = NULL;
246     p_sys->camera_nodes = NULL;
247     p_sys->selected_camera = 0;
248     p_sys->dma_device = NULL;
249     p_sys->selected_uid = 0;
250
251     /* PROCESS INPUT OPTIONS */
252     if( process_options(p_demux) != VLC_SUCCESS )
253     {
254         msg_Err( p_demux, "Bad MRL, please check the option line "
255                           "(MRL was: %s)",
256                           p_demux->psz_path );
257         free( p_sys );
258         p_demux->p_sys = NULL;
259         return VLC_EGENERIC;
260     }
261
262     msg_Dbg( p_demux, "Selected camera %d", p_sys->selected_camera );
263     msg_Dbg( p_demux, "Selected uid 0x%llx", p_sys->selected_uid );
264
265     ScanCameras( p_sys, p_demux );
266     if( !p_sys->camera_nodes )
267     {
268         msg_Err( p_demux, "No camera found !!" );
269         free( p_sys );
270         p_demux->p_sys = NULL;
271         return VLC_EGENERIC;
272     }
273
274     if( p_sys->selected_uid )
275     {
276         int found = 0;
277         for( i=0; i < p_sys->num_cameras; i++ )
278         {
279             if( p_sys->camera_nodes[i].uid == p_sys->selected_uid )
280             {
281                 p_sys->selected_camera = i;
282                 found++;
283                 break;
284             }
285         }
286         if( !found )
287         {
288             msg_Err( p_demux, "Can't find camera with uid : 0x%llx.",
289                      p_sys->selected_uid );
290             Close( p_this );
291             return VLC_EGENERIC;
292         }
293     }
294     else if( p_sys->selected_camera >= p_sys->num_cameras )
295     {
296         msg_Err( p_demux, "there are not this many cameras. (%d/%d)",
297                           p_sys->selected_camera, p_sys->num_cameras );
298         Close( p_this );
299         return VLC_EGENERIC;
300     }
301
302     p_sys->fd_video = dc1394_create_handle(
303                         p_sys->camera_nodes[p_sys->selected_camera].port );
304     if( (int)p_sys->fd_video < 0 )
305     {
306         msg_Err( p_demux, "Can't init dc1394 handle" );
307         Close( p_this );
308         return VLC_EGENERIC;
309     }
310
311     /* get camera info */
312     result = dc1394_get_camera_info( p_sys->fd_video,
313                         p_sys->camera_nodes[p_sys->selected_camera].node,
314                         &p_sys->camera_info );
315     if( result != DC1394_SUCCESS )
316     {
317         msg_Err( p_demux ,"unable to get camera info" );
318         Close( p_this );
319         return VLC_EGENERIC;
320     }
321
322     dc1394_print_camera_info( &p_sys->camera_info );
323     result = dc1394_get_camera_misc_info( p_sys->fd_video,
324                         p_sys->camera_nodes[p_sys->selected_camera].node,
325                         &p_sys->misc_info );
326     if( result != DC1394_SUCCESS )
327     {
328         msg_Err( p_demux, "unable to get camera misc info" );
329         Close( p_this );
330         return VLC_EGENERIC;
331     }
332
333     /* init camera and set some video options  */
334     result = dc1394_init_camera( p_sys->camera_info.handle,
335                                  p_sys->camera_info.id );
336     if( result != DC1394_SUCCESS )
337     {
338         msg_Err( p_demux, "unable to get init dc1394 camera" );
339         Close( p_this );
340         return VLC_EGENERIC;
341     }
342
343     if( p_sys->focus )
344     {
345         result = dc1394_set_focus( p_sys->camera_info.handle,
346                         p_sys->camera_nodes[p_sys->selected_camera].node,
347                         p_sys->focus );
348         if( result != DC1394_SUCCESS )
349         {
350             msg_Err( p_demux, "unable to set initial focus to %u",
351                      p_sys->focus );
352         }
353         msg_Dbg( p_demux, "Initial focus set to %u", p_sys->focus );
354     }
355
356     result = dc1394_set_brightness( p_sys->camera_info.handle,
357                         p_sys->camera_nodes[p_sys->selected_camera].node,
358                         p_sys->brightness );
359     if( result != DC1394_SUCCESS )
360     {
361         msg_Err( p_demux, "unable to set init brightness to %d",
362                  p_sys->brightness);
363         Close( p_this );
364         return VLC_EGENERIC;
365     }
366
367     result = dc1394_set_video_framerate( p_sys->camera_info.handle,
368                         p_sys->camera_nodes[p_sys->selected_camera].node,
369                         p_sys->frame_rate );
370     if( result != DC1394_SUCCESS )
371     {
372         msg_Err( p_demux, "unable to set framerate to %d",
373                  p_sys->frame_rate );
374         Close( p_this );
375         return VLC_EGENERIC;
376     }
377     p_sys->misc_info.framerate = p_sys->frame_rate;
378
379     result = dc1394_set_video_format( p_sys->camera_info.handle,
380                         p_sys->camera_nodes[p_sys->selected_camera].node,
381                         FORMAT_VGA_NONCOMPRESSED );
382     if( result != DC1394_SUCCESS )
383     {
384         msg_Err( p_demux, "unable to set video format to VGA_NONCOMPRESSED" );
385         Close( p_this );
386         return VLC_EGENERIC;
387     }
388     p_sys->misc_info.format = FORMAT_VGA_NONCOMPRESSED;
389
390     result = dc1394_set_video_mode( p_sys->camera_info.handle,
391                         p_sys->camera_nodes[p_sys->selected_camera].node,
392                         p_sys->frame_size );
393     if( result != DC1394_SUCCESS )
394     {
395         msg_Err( p_demux, "unable to set video mode" );
396         Close( p_this );
397         return VLC_EGENERIC;
398     }
399     p_sys->misc_info.mode = p_sys->frame_size;
400
401     /* reprobe everything */
402     result = dc1394_get_camera_info( p_sys->camera_info.handle,
403                                      p_sys->camera_info.id,
404                                      &p_sys->camera_info );
405     if( result != DC1394_SUCCESS )
406     {
407         msg_Err( p_demux, "Could not get camera basic information!" );
408         Close( p_this );
409         return VLC_EGENERIC;
410     }
411
412     result = dc1394_get_camera_misc_info( p_sys->camera_info.handle,
413                                           p_sys->camera_info.id,
414                                           &p_sys->misc_info );
415     if( result != DC1394_SUCCESS )
416     {
417         msg_Err( p_demux, "Could not get camera misc information!" );
418         Close( p_this );
419         return VLC_EGENERIC;
420     }
421
422     /* set iso_channel */
423     result = dc1394_set_iso_channel_and_speed( p_sys->camera_info.handle,
424                                                p_sys->camera_info.id,
425                                                p_sys->selected_camera,
426                                                SPEED_400 );
427     if( result != DC1394_SUCCESS )
428     {
429         msg_Err( p_demux, "Could not set iso channel!" );
430         Close( p_this );
431         return VLC_EGENERIC;
432     }
433     msg_Dbg( p_demux, "Using ISO channel %d", p_sys->misc_info.iso_channel );
434     p_sys->misc_info.iso_channel = p_sys->selected_camera;
435
436     /* and setup capture */
437     if( p_sys->dma_capture )
438     {
439         result = dc1394_dma_setup_capture( p_sys->camera_info.handle,
440                         p_sys->camera_info.id,
441                         p_sys->misc_info.iso_channel,
442                         p_sys->misc_info.format,
443                         p_sys->misc_info.mode,
444                         SPEED_400,
445                         p_sys->misc_info.framerate,
446                         10, 0,
447                         p_sys->dma_device,
448                         &p_sys->camera );
449         if( result != DC1394_SUCCESS )
450         {
451             msg_Err( p_demux ,"unable to setup camera" );
452             Close( p_this );
453             return VLC_EGENERIC;
454         }
455     }
456     else
457     {
458         result = dc1394_setup_capture( p_sys->camera_info.handle,
459                     p_sys->camera_info.id,
460                     p_sys->misc_info.iso_channel,
461                     p_sys->misc_info.format,
462                     p_sys->misc_info.mode,
463                     SPEED_400,
464                     p_sys->misc_info.framerate,
465                     &p_sys->camera );
466         if( result != DC1394_SUCCESS)
467         {
468             msg_Err( p_demux ,"unable to setup camera" );
469             Close( p_this );
470             return VLC_EGENERIC;
471         }
472     }
473
474     /* TODO - UYV444 chroma converter is missing, when it will be available
475      * fourcc will become variable (and not just a fixed value for UYVY)
476      */
477     i_width = p_sys->camera.frame_width;
478     i_height = p_sys->camera.frame_height;
479
480     i_aspect = vout_InitPicture( VLC_OBJECT(p_demux), &p_sys->pic,
481                     VLC_FOURCC('U', 'Y', 'V', 'Y'),
482                     i_width, i_height,
483                     i_width * VOUT_ASPECT_FACTOR / i_height );
484
485     es_format_Init( &fmt, VIDEO_ES, VLC_FOURCC('U', 'Y', 'V', 'Y') );
486
487     fmt.video.i_width = i_width;
488     fmt.video.i_height = i_height;
489
490     msg_Dbg( p_demux, "added new video es %4.4s %dx%d",
491              (char*)&fmt.i_codec, fmt.video.i_width, fmt.video.i_height );
492
493     p_sys->p_es_video = es_out_Add( p_demux->out, &fmt );
494
495     if( p_sys->audio_device )
496     {
497         OpenAudioDev( p_demux );
498         if( p_sys->fd_audio >= 0 )
499         {
500             es_format_t fmt;
501             es_format_Init( &fmt, AUDIO_ES, VLC_FOURCC('a','r','a','w') );
502
503             fmt.audio.i_channels = p_sys->channels ? p_sys->channels : 1;
504             fmt.audio.i_rate = p_sys->i_sample_rate;
505             fmt.audio.i_bitspersample = 16; /* FIXME: hmm, ?? */
506             fmt.audio.i_blockalign = fmt.audio.i_channels *
507                                      fmt.audio.i_bitspersample / 8;
508             fmt.i_bitrate = fmt.audio.i_channels * fmt.audio.i_rate *
509                             fmt.audio.i_bitspersample;
510
511             msg_Dbg( p_demux, "new audio es %d channels %dHz",
512             fmt.audio.i_channels, fmt.audio.i_rate );
513
514             p_sys->p_es_audio = es_out_Add( p_demux->out, &fmt );
515         }
516     }
517
518     /* have the camera start sending us data */
519     result = dc1394_start_iso_transmission( p_sys->camera_info.handle,
520                                             p_sys->camera_info.id );
521     if( result != DC1394_SUCCESS )
522     {
523         msg_Err( p_demux, "unable to start camera iso transmission" );
524         if( p_sys->dma_capture )
525         {
526             dc1394_dma_release_camera( p_sys->fd_video, &p_sys->camera );
527         }
528         else
529         {
530             dc1394_release_camera( p_sys->fd_video, &p_sys->camera );
531         }
532         Close( p_this );
533         return VLC_EGENERIC;
534     }
535     p_sys->misc_info.is_iso_on = DC1394_TRUE;
536     return VLC_SUCCESS;
537 }
538
539 static void OpenAudioDev( demux_t *p_demux )
540 {
541     demux_sys_t *p_sys = p_demux->p_sys;
542     char *psz_device = p_sys->audio_device;
543     int i_format = AFMT_S16_LE;
544     int result;
545
546     p_sys->fd_audio = open( psz_device, O_RDONLY | O_NONBLOCK );
547     if( p_sys->fd_audio  < 0 )
548     {
549         msg_Err( p_demux, "cannot open audio device (%s)", psz_device );
550         CloseAudioDev( p_demux );
551     }
552
553     if( !p_sys->i_sample_rate )
554         p_sys->i_sample_rate = 44100;
555
556     result = ioctl( p_sys->fd_audio, SNDCTL_DSP_SETFMT, &i_format );
557     if( (result  < 0) || (i_format != AFMT_S16_LE) )
558     {
559         msg_Err( p_demux, "cannot set audio format (16b little endian) "
560                           "(%d)", i_format );
561         CloseAudioDev( p_demux );
562     }
563
564     result = ioctl( p_sys->fd_audio, SNDCTL_DSP_CHANNELS, &p_sys->channels );
565     if( result < 0 )
566     {
567         msg_Err( p_demux, "cannot set audio channels count (%d)",
568                  p_sys->channels );
569         CloseAudioDev( p_demux );
570     }
571
572     result = ioctl( p_sys->fd_audio, SNDCTL_DSP_SPEED, &p_sys->i_sample_rate );
573     if( result < 0 )
574     {
575         msg_Err( p_demux, "cannot set audio sample rate (%s)", p_sys->i_sample_rate );
576         CloseAudioDev( p_demux );
577     }
578
579     msg_Dbg( p_demux, "opened adev=`%s' %s %dHz",
580              psz_device,
581              (p_sys->channels > 1) ? "stereo" : "mono",
582              p_sys->i_sample_rate );
583
584     p_sys->i_audio_max_frame_size = 32 * 1024;
585 }
586
587 static inline void CloseAudioDev( demux_t *p_demux )
588 {
589     demux_sys_t *p_sys = NULL;
590
591     if( p_demux )
592     {
593         p_sys = p_demux->p_sys;
594         if( p_sys->fd_audio >= 0 )
595             close( p_sys->fd_audio );
596     }
597 }
598
599 /*****************************************************************************
600  * Close:
601  *****************************************************************************/
602 static void Close( vlc_object_t *p_this )
603 {
604     demux_t     *p_demux = (demux_t*)p_this;
605     demux_sys_t *p_sys = p_demux->p_sys;
606     int result = 0;
607
608     /* Stop data transmission */
609     result = dc1394_stop_iso_transmission( p_sys->fd_video,
610                                            p_sys->camera.node );
611     if( result != DC1394_SUCCESS )
612     {
613         msg_Err( p_demux, "couldn't stop the camera" );
614     }
615
616     /* Close camera */
617     if( p_sys->dma_capture )
618     {
619         dc1394_dma_unlisten( p_sys->fd_video, &p_sys->camera );
620         dc1394_dma_release_camera( p_sys->fd_video, &p_sys->camera );
621     }
622     else
623     {
624         dc1394_release_camera( p_sys->fd_video, &p_sys->camera );
625     }
626
627     if( p_sys->fd_video )
628         dc1394_destroy_handle( p_sys->fd_video );
629     CloseAudioDev( p_demux );
630
631     free( p_sys->camera_nodes );
632     free( p_sys->audio_device );
633
634     free( p_sys );
635 }
636
637 static void MovePixelUYVY( void *src, int spos, void *dst, int dpos )
638 {
639     char u,v,y;
640     u_char  *sc;
641     u_char  *dc;
642
643     sc = (u_char *)src + (spos*2);
644     if( spos % 2 )
645     {
646         v = sc[0];
647         y = sc[1];
648         u = *(sc -2);
649     }
650     else
651     {
652         u = sc[0];
653         y = sc[1];
654         v = sc[2];
655     }
656     dc = (u_char *)dst+(dpos*2);
657     if( dpos % 2 )
658     {
659         dc[0] = v;
660         dc[1] = y;
661     }
662     else
663     {
664         dc[0] = u;
665         dc[1] = y;
666     }
667 }
668
669 /*****************************************************************************
670  * Demux:
671  *****************************************************************************/
672 static block_t *GrabVideo( demux_t *p_demux )
673 {
674     demux_sys_t *p_sys = p_demux->p_sys;
675     block_t     *p_block = NULL;
676     int result = 0;
677
678     if( p_sys->dma_capture )
679     {
680         result = dc1394_dma_single_capture( &p_sys->camera );
681         if( result != DC1394_SUCCESS )
682         {
683             msg_Err( p_demux, "unable to capture a frame" );
684             return NULL;
685         }
686     }
687     else
688     {
689         result = dc1394_single_capture( p_sys->camera_info.handle,
690                                         &p_sys->camera );
691         if( result != DC1394_SUCCESS )
692         {
693             msg_Err( p_demux, "unable to capture a frame" );
694             return NULL;
695         }
696     }
697
698     p_block = block_New( p_demux, p_sys->camera.frame_width *
699                                   p_sys->camera.frame_height * 2 );
700     if( !p_block )
701     {
702         msg_Err( p_demux, "cannot get block" );
703         return NULL;
704     }
705
706     if( !p_sys->camera.capture_buffer )
707     {
708         msg_Err (p_demux, "caputer buffer empty");
709         block_Release( p_block );
710         return NULL;
711     }
712
713     memcpy( p_block->p_buffer, (const char *)p_sys->camera.capture_buffer,
714             p_sys->camera.frame_width * p_sys->camera.frame_height * 2 );
715
716     p_block->i_pts = p_block->i_dts = mdate();
717     if( p_sys->dma_capture )
718         dc1394_dma_done_with_buffer( &p_sys->camera );
719     return p_block;
720 }
721
722 static block_t *GrabAudio( demux_t *p_demux )
723 {
724     demux_sys_t *p_sys = p_demux->p_sys;
725     struct audio_buf_info buf_info;
726     block_t *p_block = NULL;
727     int i_read = 0;
728     int i_correct = 0;
729     int result = 0;
730
731     p_block = block_New( p_demux, p_sys->i_audio_max_frame_size );
732     if( !p_block )
733     {
734         msg_Warn( p_demux, "cannot get buffer" );
735         return NULL;
736     }
737
738     i_read = read( p_sys->fd_audio, p_block->p_buffer,
739                    p_sys->i_audio_max_frame_size );
740
741     if( i_read <= 0 )
742         return NULL;
743
744     p_block->i_buffer = i_read;
745
746     /* Correct the date because of kernel buffering */
747     i_correct = i_read;
748     result = ioctl( p_sys->fd_audio, SNDCTL_DSP_GETISPACE, &buf_info );
749     if( result == 0 )
750         i_correct += buf_info.bytes;
751
752     p_block->i_pts = p_block->i_dts =
753                         mdate() - INT64_C(1000000) * (mtime_t)i_correct /
754                         2 / p_sys->channels / p_sys->i_sample_rate;
755     return p_block;
756 }
757
758 static int Demux( demux_t *p_demux )
759 {
760     demux_sys_t *p_sys = p_demux->p_sys;
761     block_t *p_blocka = NULL;
762     block_t *p_blockv = NULL;
763
764     /* Try grabbing audio frames first */
765     if( p_sys->fd_audio > 0 )
766         p_blocka = GrabAudio( p_demux );
767
768     /* Try grabbing video frame */
769     if( (int)p_sys->fd_video > 0 )
770         p_blockv = GrabVideo( p_demux );
771
772     if( !p_blocka && !p_blockv )
773     {
774         /* Sleep so we do not consume all the cpu, 10ms seems
775          * like a good value (100fps)
776          */
777         msleep( 10000 );
778         return 1;
779     }
780
781     if( p_blocka )
782     {
783         es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_blocka->i_pts );
784         es_out_Send( p_demux->out, p_sys->p_es_audio, p_blocka );
785     }
786
787     if( p_blockv )
788     {
789         es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_blockv->i_pts );
790         es_out_Send( p_demux->out, p_sys->p_es_video, p_blockv );
791     }
792     return 1;
793 }
794
795 /*****************************************************************************
796  * Control:
797  *****************************************************************************/
798 static int Control( demux_t *p_demux, int i_query, va_list args )
799 {
800     bool *pb;
801     int64_t    *pi64;
802
803     switch( i_query )
804     {
805         /* Special for access_demux */
806         case DEMUX_CAN_PAUSE:
807         case DEMUX_CAN_SEEK:
808         case DEMUX_SET_PAUSE_STATE:
809         case DEMUX_CAN_CONTROL_PACE:
810             pb = (bool*)va_arg( args, bool * );
811             *pb = false;
812             return VLC_SUCCESS;
813
814         case DEMUX_GET_PTS_DELAY:
815             pi64 = (int64_t*)va_arg( args, int64_t * );
816             *pi64 = (int64_t)DEFAULT_PTS_DELAY;
817             return VLC_SUCCESS;
818
819         case DEMUX_GET_TIME:
820             pi64 = (int64_t*)va_arg( args, int64_t * );
821             *pi64 = mdate();
822             return VLC_SUCCESS;
823
824         /* TODO implement others */
825         default:
826             return VLC_EGENERIC;
827     }
828     return VLC_EGENERIC;
829 }
830
831 static int process_options( demux_t *p_demux )
832 {
833     demux_sys_t *p_sys = p_demux->p_sys;
834     char *psz_dup;
835     char *psz_parser;
836     char *token = NULL;
837     char *state = NULL;
838     float rate_f;
839
840     if( strncmp(p_demux->psz_access, "dc1394", 6) != 0 )
841         return VLC_EGENERIC;
842
843     psz_dup = strdup( p_demux->psz_path );
844     psz_parser = psz_dup;
845     for( token = strtok_r( psz_parser,":",&state); token;
846          token = strtok_r( NULL, ":", &state ) )
847     {
848         if( strncmp( token, "size=", strlen("size=") ) == 0 )
849         {
850             token += strlen("size=");
851             if( strncmp( token, "160x120", 7 ) == 0 )
852             {
853                 /* TODO - UYV444 chroma converter is needed ...
854                     * video size of 160x120 is temporarily disabled
855                     */
856                 msg_Err( p_demux,
857                     "video size of 160x120 is actually disabled for lack of chroma "
858                     "support. It will relased ASAP, until then try an higher size "
859                     "(320x240 and 640x480 are fully supported)" );
860                 free( psz_dup );
861                 return VLC_EGENERIC;
862 #if 0
863                 p_sys->frame_size = MODE_160x120_YUV444;
864                 p_sys->width = 160;
865                 p_sys->height = 120;
866 #endif
867             }
868             else if( strncmp( token, "320x240", 7 ) == 0 )
869             {
870                 p_sys->frame_size = MODE_320x240_YUV422;
871                 p_sys->width = 320;
872                 p_sys->height = 240;
873             }
874             else if( strncmp( token, "640x480", 7 ) == 0 )
875             {
876                 p_sys->frame_size = MODE_640x480_YUV422;
877                 p_sys->width = 640;
878                 p_sys->height = 480;
879             }
880             else
881             {
882                 msg_Err( p_demux,
883                     "This program currently suppots frame sizes of"
884                     " 160x120, 320x240, and 640x480. "
885                     "Please specify one of them. You have specified %s.",
886                     token );
887                 free( psz_dup );
888                 return VLC_EGENERIC;
889             }
890             msg_Dbg( p_demux, "Requested video size : %s",token );
891         }
892         else if( strncmp( token, "fps=", strlen( "fps=" ) ) == 0 )
893         {
894             token += strlen("fps=");
895             sscanf( token, "%g", &rate_f );
896             if( rate_f == 1.875 )
897                 p_sys->frame_rate = FRAMERATE_1_875;
898             else if( rate_f == 3.75 )
899                 p_sys->frame_rate = FRAMERATE_3_75;
900             else if( rate_f == 7.5 )
901                 p_sys->frame_rate = FRAMERATE_7_5;
902             else if( rate_f == 15 )
903                 p_sys->frame_rate = FRAMERATE_15;
904             else if( rate_f == 30 )
905                 p_sys->frame_rate = FRAMERATE_30;
906             else if( rate_f == 60 )
907                 p_sys->frame_rate = FRAMERATE_60;
908             else
909             {
910                 msg_Err( p_demux ,
911                     "This program supports framerates of"
912                     " 1.875, 3.75, 7.5, 15, 30, 60. "
913                     "Please specify one of them. You have specified %s.",
914                     token);
915                 free( psz_dup );
916                 return VLC_EGENERIC;
917             }
918             msg_Dbg( p_demux, "Requested frame rate : %s",token );
919         }
920         else if( strncmp( token, "brightness=", strlen( "brightness=" ) ) == 0 )
921         {
922             int nr = 0;
923             token += strlen("brightness=");
924             nr = sscanf( token, "%u", &p_sys->brightness);
925             if( nr != 1 )
926             {
927                 msg_Err( p_demux, "Bad brightness value '%s', "
928                                   "must be an unsigned integer.",
929                                   token );
930                 free( psz_dup );
931                 return VLC_EGENERIC;
932             }
933         }
934 #if 0
935         else if( strncmp( token, "controller=", strlen( "controller=" ) ) == 0 )
936         {
937             int nr = 0;
938             token += strlen("controller=");
939             nr = sscanf( token, "%u", &p_sys->controller );
940             if( nr != 1)
941             {
942                 msg_Err(p_demux, "Bad controller value '%s', "
943                                  "must be an unsigned integer.",
944                                  token );
945                 return VLC_EGENERIC;
946             }
947         }
948 #endif
949         else if( strncmp( token, "camera=", strlen( "camera=" ) ) == 0 )
950         {
951             int nr = 0;
952             token += strlen("camera=");
953             nr = sscanf(token,"%u",&p_sys->selected_camera);
954             if( nr != 1)
955             {
956                 msg_Err( p_demux, "Bad camera number '%s', "
957                                   "must be an unsigned integer.",
958                                   token );
959                 free( psz_dup );
960                 return VLC_EGENERIC;
961             }
962         }
963         else if( strncmp( token, "capture=", strlen( "capture=" ) ) == 0)
964         {
965             token += strlen("capture=");
966             if( strncmp(token, "raw1394",7) == 0 )
967             {
968                 msg_Dbg( p_demux, "DMA capture disabled!" );
969                 p_sys->dma_capture = DMA_OFF;
970             }
971             else if( strncmp(token,"video1394",9) == 0 )
972             {
973                 msg_Dbg( p_demux, "DMA capture enabled!" );
974                 p_sys->dma_capture = DMA_ON;
975             }
976             else
977             {
978                 msg_Err(p_demux, "Bad capture method value '%s', "
979                                  "it can be 'raw1394' or 'video1394'.",
980                                 token );
981                 free( psz_dup );
982                 return VLC_EGENERIC;
983             }
984         }
985         else if( strncmp( token, "adev=", strlen( "adev=" ) ) == 0 )
986         {
987             token += strlen("adev=");
988             p_sys->audio_device = strdup(token);
989             msg_Dbg( p_demux, "Using audio device '%s'.", token );
990         }
991         else if( strncmp( token, "samplerate=", strlen( "samplerate=" ) ) == 0 )
992         {
993             token += strlen("samplerate=");
994             sscanf( token, "%d", &p_sys->i_sample_rate );
995         }
996         else if( strncmp( token, "channels=", strlen("channels=" ) ) == 0 )
997         {
998             token += strlen("channels=");
999             sscanf( token, "%d", &p_sys->channels );
1000         }
1001         else if( strncmp( token, "focus=", strlen("focus=" ) ) == 0)
1002         {
1003             token += strlen("focus=");
1004             sscanf( token, "%u", &p_sys->focus );
1005         }
1006         else if( strncmp( token, "uid=", strlen("uid=") ) == 0)
1007         {
1008             token += strlen("uid=");
1009             sscanf( token, "0x%llx", &p_sys->selected_uid );
1010         }
1011     }
1012     free( psz_dup );
1013     return VLC_SUCCESS;
1014 }
1015