]> git.sesse.net Git - vlc/blob - modules/access/dv.c
Run thread that reads from the DV camera at a slightly higher priority then the input...
[vlc] / modules / access / dv.c
1 /*****************************************************************************
2  * dv.c: Digital video/Firewire input (file: access plug-in)
3  *****************************************************************************
4  * Copyright (C) 2005 M2X
5  * $Id: dv.c 12318 2005-08-21 17:46:48Z jpsaman $
6  *
7  * Authors: Jean-Paul Saman <jpsaman at m2x dot nl>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program 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
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <vlc/vlc.h>
28 #include <vlc/input.h>
29
30 #include <stdlib.h>
31 #include <string.h>
32 #include <errno.h>
33 #ifdef HAVE_SYS_TYPES_H
34 #   include <sys/types.h>
35 #endif
36 #ifdef HAVE_SYS_TIME_H
37 #   include <sys/time.h>
38 #endif
39 #ifdef HAVE_SYS_STAT_H
40 #   include <sys/stat.h>
41 #endif
42 #ifdef HAVE_FCNTL_H
43 #   include <fcntl.h>
44 #endif
45
46 #ifdef HAVE_UNISTD_H
47 #   include <unistd.h>
48 #elif defined( WIN32 ) && !defined( UNDER_CE )
49 #   include <io.h>
50 #endif
51
52 #include <sys/poll.h>
53
54 #include <libraw1394/raw1394.h>
55 #include <libraw1394/csr.h>
56 #include <libavc1394/avc1394.h>
57 #include <libavc1394/avc1394_vcr.h>
58 #include <libavc1394/rom1394.h>
59
60 /*****************************************************************************
61  * Module descriptor
62  *****************************************************************************/
63 static int  Open ( vlc_object_t * );
64 static void Close( vlc_object_t * );
65 static block_t *Block( access_t * );
66 static int Control( access_t *, int, va_list );
67
68 #define CACHING_TEXT N_("Caching value in ms")
69 #define CACHING_LONGTEXT N_( \
70     "Allows you to modify the default caching value for file streams. This " \
71     "value should be set in millisecond units." )
72
73 vlc_module_begin();
74     set_description( _("Digital Video (Firewire/ieee1394)  input") );
75     set_shortname( _("dv") );
76     set_category( CAT_INPUT );
77     set_subcategory( SUBCAT_INPUT_ACCESS );
78     add_integer( "dv-caching", 60000 / 1000, NULL, CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
79     set_capability( "access2", 50 );
80     add_shortcut( "dv" );
81     add_shortcut( "dv1394" );
82     add_shortcut( "raw1394" );
83     set_callbacks( Open, Close );
84 vlc_module_end();
85
86 typedef struct
87 {
88     VLC_COMMON_MEMBERS
89
90     access_t        *p_access;
91     vlc_mutex_t     lock;
92     block_t         *p_frame;
93     block_t         **pp_last;
94
95 } event_thread_t;
96
97 static int Raw1394EventThread( vlc_object_t * );
98 static int Raw1394Handler( raw1394handle_t, int, size_t, quadlet_t * );
99
100 static int Raw1394GetNumPorts( access_t *p_access );
101 static raw1394handle_t Raw1394Open( access_t *, int );
102 static void Raw1394Close( raw1394handle_t );
103
104 static int DiscoverAVC( access_t *, int *, uint64_t );
105 static raw1394handle_t AVCOpen( access_t *, int );
106 static void AVCClose( access_t * );
107 static int AVCResetHandler( raw1394handle_t, unsigned int );
108 static int AVCPlay( access_t *, int );
109 static int AVCPause( access_t *, int );
110 static int AVCStop( access_t *, int );
111
112 struct access_sys_t
113 {
114     raw1394handle_t p_avc1394;
115     raw1394handle_t p_raw1394;
116     struct pollfd   raw1394_poll;
117
118     int i_cards;
119     int i_node;
120     int i_port;
121     int i_channel;
122     uint64_t i_guid;
123
124     /* event */
125     event_thread_t *p_ev;
126     vlc_mutex_t lock;
127     block_t *p_frame;
128 };
129
130 /*****************************************************************************
131  * Open: open the file
132  *****************************************************************************/
133 static int Open( vlc_object_t *p_this )
134 {
135     access_t     *p_access = (access_t*)p_this;
136     access_sys_t *p_sys;
137     char *psz_name = strdup( p_access->psz_path );
138
139     struct raw1394_portinfo port_inf[ 16 ];
140     iso_handler_t oldhandler;
141
142     msg_Dbg( p_access, "opening device %s", psz_name );
143
144     /* Set up p_access */
145     p_access->pf_read = NULL;
146     p_access->pf_block = Block;
147     p_access->pf_control = Control;
148     p_access->pf_seek = NULL;
149     p_access->info.i_update = 0;
150     p_access->info.i_size = 0;
151     p_access->info.i_pos = 0;
152     p_access->info.b_eof = VLC_FALSE;
153     p_access->info.b_prebuffered = VLC_FALSE;
154     p_access->info.i_title = 0;
155     p_access->info.i_seekpoint = 0;
156
157     p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );
158     if( !p_sys )
159     {
160         free( psz_name );
161         return VLC_EGENERIC;
162     }
163
164     p_sys->i_cards = 0;
165     p_sys->i_node = 0;
166     p_sys->i_port = 0;
167     p_sys->i_guid = 0;
168     p_sys->i_channel = 63;
169     p_sys->p_raw1394 = NULL;
170     p_sys->p_avc1394 = NULL;
171     p_sys->p_frame = NULL;
172
173     vlc_mutex_init( p_access, &p_sys->lock );
174
175     p_sys->i_node = DiscoverAVC( p_access, &p_sys->i_port, p_sys->i_guid );
176     p_sys->p_avc1394 = AVCOpen( p_access, p_sys->i_port );
177     if( !p_sys->p_avc1394 )
178     {
179         msg_Err( p_access, "no Digital Video Control device found on %s", psz_name );
180         Close( p_this );
181         free( psz_name );
182         return VLC_EGENERIC;
183     }
184
185     p_sys->p_raw1394 = raw1394_new_handle();
186     if( !p_sys->p_raw1394 )
187     {
188         msg_Err( p_access, "no Digital Video device found on %s", psz_name );
189         Close( p_this );
190         free( psz_name );
191         return VLC_EGENERIC;
192     }
193
194     p_sys->i_cards = raw1394_get_port_info( p_sys->p_raw1394, port_inf, 16 );
195     if( p_sys->i_cards < 0 )
196     {
197         msg_Err( p_access, "failed to get port info for %s", psz_name );
198         Close( p_this );
199         free( psz_name );
200         return VLC_EGENERIC;
201     }
202
203     if( raw1394_set_port( p_sys->p_raw1394, p_sys->i_port ) < 0 )
204     {
205         msg_Err( p_access, "failed to set port info for %s", psz_name );
206         Close( p_this );
207         free( psz_name );
208         return VLC_EGENERIC;
209     }
210
211     oldhandler = raw1394_set_iso_handler( p_sys->p_raw1394,
212                                           p_sys->i_channel, Raw1394Handler );
213     raw1394_set_userdata( p_sys->p_raw1394, p_access );
214     raw1394_start_iso_rcv( p_sys->p_raw1394, p_sys->i_channel );
215
216     p_sys->raw1394_poll.fd = raw1394_get_fd( p_sys->p_raw1394 );
217     p_sys->raw1394_poll.events = POLLIN | POLLERR | POLLHUP | POLLPRI;
218
219     /* Update default_pts to a suitable value for udp access */
220     var_Create( p_access, "dv-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
221
222     /* Now create our event thread catcher */
223     p_sys->p_ev = vlc_object_create( p_access, sizeof( event_thread_t ) );
224     p_sys->p_ev->p_frame = NULL;
225     p_sys->p_ev->pp_last = &p_sys->p_ev->p_frame;
226     p_sys->p_ev->p_access = p_access;
227     vlc_mutex_init( p_access, &p_sys->p_ev->lock );
228     vlc_thread_create( p_sys->p_ev, "dv event thread handler", Raw1394EventThread,
229                        VLC_THREAD_PRIORITY_OUTPUT, VLC_FALSE );
230
231     free( psz_name );
232     return VLC_SUCCESS;
233 }
234
235 /*****************************************************************************
236  * Close: free unused data structures
237  *****************************************************************************/
238 static void Close( vlc_object_t *p_this )
239 {
240     access_t     *p_access = (access_t*)p_this;
241     access_sys_t *p_sys = p_access->p_sys;
242
243     /* stop the event handler */
244     p_sys->p_ev->b_die = VLC_TRUE;
245
246     if( p_sys->p_raw1394)
247         raw1394_stop_iso_rcv( p_sys->p_raw1394, p_sys->i_channel );
248
249     vlc_mutex_destroy( &p_sys->p_ev->lock );
250     vlc_thread_join( p_sys->p_ev );
251
252     /* Cleanup frame data */
253     if( p_sys->p_ev->p_frame )
254     {
255         vlc_mutex_lock( &p_sys->p_ev->lock );
256         block_ChainRelease( p_sys->p_ev->p_frame );
257         p_sys->p_ev->p_frame = NULL;
258         p_sys->p_ev->pp_last = &p_sys->p_frame;
259         vlc_mutex_unlock( &p_sys->p_ev->lock );
260     }
261     vlc_object_destroy( p_sys->p_ev );
262
263     if( p_sys->p_frame )
264         block_ChainRelease( p_sys->p_frame );
265     if( p_sys->p_raw1394)
266         raw1394_destroy_handle( p_sys->p_raw1394 );
267
268     AVCClose( p_access );
269
270     vlc_mutex_destroy( &p_sys->lock );
271     free( p_sys );
272 }
273
274 /*****************************************************************************
275  * Control:
276  *****************************************************************************/
277 static int Control( access_t *p_access, int i_query, va_list args )
278 {
279     access_sys_t *p_sys = p_access->p_sys;
280     vlc_bool_t   *pb_bool;
281     int64_t      *pi_64;
282
283     switch( i_query )
284     {
285         /* */
286         case ACCESS_CAN_SEEK:
287         case ACCESS_CAN_FASTSEEK:
288         case ACCESS_CAN_PAUSE:
289             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
290             *pb_bool = VLC_TRUE;
291             break;
292
293         case ACCESS_CAN_CONTROL_PACE:
294             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
295             *pb_bool = VLC_FALSE;
296             break;
297
298         case ACCESS_GET_PTS_DELAY:
299             pi_64 = (int64_t*)va_arg( args, int64_t * );
300             *pi_64 = var_GetInteger( p_access, "dv-caching" ) * 1000;
301             break;
302
303         /* */
304         case ACCESS_SET_PAUSE_STATE:
305             AVCPause( p_access, p_sys->i_node );
306             break;
307
308         case ACCESS_GET_TITLE_INFO:
309         case ACCESS_SET_TITLE:
310         case ACCESS_SET_SEEKPOINT:
311         case ACCESS_SET_PRIVATE_ID_STATE:
312             return VLC_EGENERIC;
313
314         default:
315             msg_Warn( p_access, "unimplemented query in control" );
316             return VLC_EGENERIC;
317
318     }
319     return VLC_SUCCESS;
320 }
321
322 static block_t *Block( access_t *p_access )
323 {
324     access_sys_t *p_sys = p_access->p_sys;
325     block_t *p_block = NULL;
326
327 //     if( !p_access->psz_demux )
328 //         p_access->psz_demux = strdup( "rawdv" );
329
330     vlc_mutex_lock( &p_sys->lock );
331     p_block = p_sys->p_frame;
332     //msg_Dbg( p_access, "sending frame %p",p_block );
333     p_sys->p_frame = NULL;
334     vlc_mutex_unlock( &p_sys->lock );
335
336     return p_block;
337 }
338
339 static int Raw1394EventThread( vlc_object_t *p_this )
340 {
341     event_thread_t *p_ev = (event_thread_t *) p_this;
342     access_t *p_access = (access_t *) p_ev->p_access;
343     access_sys_t *p_sys = (access_sys_t *) p_access->p_sys;
344     int result = 0;
345
346     AVCPlay( p_access, p_sys->i_node );
347
348     vlc_thread_ready( p_this );
349
350     while( !p_sys->p_ev->b_die )
351     {
352         while( ( result = poll( &(p_sys->raw1394_poll), 1, 200 ) ) < 0 )
353         {
354             if( !( errno == EAGAIN || errno == EINTR ) )
355             {
356                 perror( "error: raw1394 poll" );
357                 msg_Err( p_access, "retrying device raw1394" );
358             }
359             if( p_sys->p_ev->b_die )
360                 break;
361         }
362         if( p_sys->p_ev->b_die )
363                 break;
364         if( result > 0 && ( ( p_sys->raw1394_poll.revents & POLLIN )
365                 || ( p_sys->raw1394_poll.revents & POLLPRI ) ) )
366             result = raw1394_loop_iterate( p_sys->p_raw1394 );
367     }
368
369     AVCStop( p_access, p_sys->i_node );
370     return VLC_SUCCESS;
371 }
372
373 static int Raw1394Handler( raw1394handle_t handle, int channel, size_t length, quadlet_t *data )
374 {
375     access_t *p_access = NULL;
376     access_sys_t *p_sys = NULL;
377     block_t *p_block = NULL;
378
379     p_access = (access_t *) raw1394_get_userdata( handle );
380     if( !p_access ) return 0;
381
382     p_sys = p_access->p_sys;
383
384     /* skip empty packets */
385     if ( length > 16 )
386     {
387         unsigned char * p = ( unsigned char* ) &data[ 3 ];
388         int section_type = p[ 0 ] >> 5;           /* section type is in bits 5 - 7 */
389         int dif_sequence = p[ 1 ] >> 4;           /* dif sequence number is in bits 4 - 7 */
390         int dif_block = p[ 2 ];
391
392         vlc_mutex_lock( &p_sys->p_ev->lock );
393
394         /* if we are at the beginning of a frame, we put the previous
395            frame in our output_queue. */
396         if( (section_type == 0) && (dif_sequence == 0) )
397         {
398             vlc_mutex_lock( &p_sys->lock );
399             if( p_sys->p_ev->p_frame )
400             {
401                 /* Push current frame to p_access thread. */
402                 //p_sys->p_ev->p_frame->i_pts = mdate();
403                 block_ChainAppend( &p_sys->p_frame, p_sys->p_ev->p_frame );
404             }
405             /* reset list */
406             p_sys->p_ev->p_frame = block_New( p_access, 144000 );
407             p_sys->p_ev->pp_last = &p_sys->p_frame;
408             vlc_mutex_unlock( &p_sys->lock );
409         }
410
411         p_block = p_sys->p_ev->p_frame;
412         if( p_block )
413         {
414             switch ( section_type )
415             {
416             case 0:    /* 1 Header block */
417                 /* p[3] |= 0x80; // hack to force PAL data */
418                 memcpy( p_block->p_buffer + dif_sequence * 150 * 80, p, 480 );
419                 break;
420
421             case 1:    /* 2 Subcode blocks */
422                 memcpy( p_block->p_buffer + dif_sequence * 150 * 80 + ( 1 + dif_block ) * 80, p, 480 );
423                 break;
424
425             case 2:    /* 3 VAUX blocks */
426                 memcpy( p_block->p_buffer + dif_sequence * 150 * 80 + ( 3 + dif_block ) * 80, p, 480 );
427                 break;
428
429             case 3:    /* 9 Audio blocks interleaved with video */
430                 memcpy( p_block->p_buffer + dif_sequence * 150 * 80 + ( 6 + dif_block * 16 ) * 80, p, 480 );
431                 break;
432
433             case 4:    /* 135 Video blocks interleaved with audio */
434                 memcpy( p_block->p_buffer + dif_sequence * 150 * 80 + ( 7 + ( dif_block / 15 ) + dif_block ) * 80, p, 480 );
435                 break;
436
437             default:    /* we canĀ“t handle any other data */
438                 block_Release( p_block );
439                 p_block = NULL;
440                 break;
441             }
442         }
443         vlc_mutex_unlock( &p_sys->p_ev->lock );
444     }
445     return 0;
446 }
447
448 /*
449  * Routing borrowed from dvgrab-1.8
450  * Copyright by Arne Schirmacher <dvgrab@schirmacher.de>
451  * Dan Dennedy <dan@dennedy.org> and others
452  */
453 static int Raw1394GetNumPorts( access_t *p_access )
454 {
455     int n_ports;
456     struct raw1394_portinfo pinf[ 16 ];
457     raw1394handle_t handle;
458
459     /* get a raw1394 handle */
460     if ( !( handle = raw1394_new_handle() ) )
461     {
462         msg_Err( p_access, "raw1394 - failed to get handle: %s.\n", strerror( errno ) );
463         return VLC_EGENERIC;
464     }
465
466     if ( ( n_ports = raw1394_get_port_info( handle, pinf, 16 ) ) < 0 )
467     {
468         msg_Err( p_access, "raw1394 - failed to get port info: %s.\n", strerror( errno ) );
469         raw1394_destroy_handle( handle );
470         return VLC_EGENERIC;
471     }
472     raw1394_destroy_handle( handle );
473
474     return n_ports;
475 }
476
477 static raw1394handle_t Raw1394Open( access_t *p_access, int port )
478 {
479     int n_ports;
480     struct raw1394_portinfo pinf[ 16 ];
481     raw1394handle_t handle;
482
483     /* get a raw1394 handle */
484 #ifdef RAW1394_V_0_8
485
486     handle = raw1394_get_handle();
487 #else
488
489     handle = raw1394_new_handle();
490 #endif
491
492     if ( !handle )
493     {
494         msg_Err( p_access, "raw1394 - failed to get handle: %s.\n", strerror( errno ) );
495         return NULL;
496     }
497
498     if ( ( n_ports = raw1394_get_port_info( handle, pinf, 16 ) ) < 0 )
499     {
500         msg_Err( p_access, "raw1394 - failed to get port info: %s.\n", strerror( errno ) );
501         raw1394_destroy_handle( handle );
502         return NULL;
503     }
504
505     /* tell raw1394 which host adapter to use */
506     if ( raw1394_set_port( handle, port ) < 0 )
507     {
508         msg_Err( p_access, "raw1394 - failed to set set port: %s.\n", strerror( errno ) );
509         return NULL;
510     }
511
512     return handle;
513 }
514
515 static void Raw1394Close( raw1394handle_t handle )
516 {
517     raw1394_destroy_handle( handle );
518 }
519
520 static int DiscoverAVC( access_t *p_access, int* port, uint64_t guid )
521 {
522     rom1394_directory rom_dir;
523     raw1394handle_t handle;
524     int device = -1;
525     int i, j = 0;
526     int m = Raw1394GetNumPorts( p_access );
527
528     if( *port >= 0 )
529     {
530         /* search on explicit port */
531         j = *port;
532         m = *port + 1;
533     }
534
535     for( ; j < m && device == -1; j++ )
536     {
537         handle = Raw1394Open( p_access, j );
538         for( i = 0; i < raw1394_get_nodecount( handle ); ++i )
539         {
540             if( guid != 0 )
541             {
542                 /* select explicitly by GUID */
543                 if( guid == rom1394_get_guid( handle, i ) )
544                 {
545                     device = i;
546                     *port = j;
547                     break;
548                 }
549             }
550             else
551             {
552                 /* select first AV/C Tape Reccorder Player node */
553                 if( rom1394_get_directory( handle, i, &rom_dir ) < 0 )
554                 {
555                     msg_Err( p_access, "error reading config rom directory for node %d\n", i );
556                     continue;
557                 }
558                 if( ( rom1394_get_node_type( &rom_dir ) == ROM1394_NODE_TYPE_AVC ) &&
559                         avc1394_check_subunit_type( handle, i, AVC1394_SUBUNIT_TYPE_VCR ) )
560                 {
561                     device = i;
562                     *port = j;
563                     break;
564                 }
565             }
566         }
567         Raw1394Close( handle );
568     }
569
570     return device;
571 }
572
573 /*
574  * Handle AVC commands
575  */
576 static raw1394handle_t AVCOpen( access_t *p_access, int port )
577 {
578     access_sys_t *p_sys = p_access->p_sys;
579     int numcards;
580     struct raw1394_portinfo pinf[ 16 ];
581
582     p_sys->p_avc1394 = raw1394_new_handle();
583     if( !p_sys->p_avc1394 )
584         return NULL;
585
586     numcards = raw1394_get_port_info( p_sys->p_avc1394, pinf, 16 );
587     if( numcards < -1 )
588         return NULL;
589     if( raw1394_set_port( p_sys->p_avc1394, port ) < 0 )
590         return NULL;
591
592     raw1394_set_bus_reset_handler( p_sys->p_avc1394, AVCResetHandler );
593
594     return  p_sys->p_avc1394;
595 }
596
597 static void AVCClose( access_t *p_access )
598 {
599     access_sys_t *p_sys = p_access->p_sys;
600
601     if( p_sys->p_avc1394 )
602     {
603         raw1394_destroy_handle( p_sys->p_avc1394 );
604         p_sys->p_avc1394 = NULL;
605     }
606 }
607
608
609 static int AVCResetHandler( raw1394handle_t handle, unsigned int generation )
610 {
611     raw1394_update_generation( handle, generation );
612     return 0;
613 }
614
615 static int AVCPlay( access_t *p_access, int phyID )
616 {
617     access_sys_t *p_sys = p_access->p_sys;
618
619     msg_Dbg( p_access, "send play command over Digital Video control channel" );
620     if( !p_sys->p_avc1394 )
621         return 0;
622
623     if( phyID >= 0 )
624     {
625         if( !avc1394_vcr_is_recording( p_sys->p_avc1394, phyID ) &&
626             avc1394_vcr_is_playing( p_sys->p_avc1394, phyID ) != AVC1394_VCR_OPERAND_PLAY_FORWARD )
627                 avc1394_vcr_play( p_sys->p_avc1394, phyID );
628     }
629     return 0;
630 }
631
632 static int AVCPause( access_t *p_access, int phyID )
633 {
634     access_sys_t *p_sys = p_access->p_sys;
635
636     if( !p_sys->p_avc1394 )
637         return 0;
638
639     if( phyID >= 0 )
640     {
641         if( !avc1394_vcr_is_recording( p_sys->p_avc1394, phyID ) &&
642             ( avc1394_vcr_is_playing( p_sys->p_avc1394, phyID ) != AVC1394_VCR_OPERAND_PLAY_FORWARD_PAUSE ) )
643                 avc1394_vcr_pause( p_sys->p_avc1394, phyID );
644     }
645     return 0;
646 }
647
648
649 static int AVCStop( access_t *p_access, int phyID )
650 {
651     access_sys_t *p_sys = p_access->p_sys;
652
653     msg_Dbg( p_access, "closing Digital Video control channel" );
654     if( !p_sys->p_avc1394 )
655         return 0;
656
657     if ( phyID >= 0 )
658         avc1394_vcr_stop( p_sys->p_avc1394, phyID );
659
660     return 0;
661 }