]> git.sesse.net Git - vlc/blob - modules/access/dv.c
DV can't (fast)seek; it does not even have a Seek callback.
[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$
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, 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     "Caching value for DV streams. This" \
71     "value should be set in milliseconds." )
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", 0 );
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     access_InitFields( p_access );
146     ACCESS_SET_CALLBACKS( NULL, Block, Control, NULL );
147     p_access->info.b_prebuffered = VLC_FALSE;
148
149     p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );
150     if( !p_sys )
151     {
152         free( psz_name );
153         return VLC_EGENERIC;
154     }
155
156     p_sys->i_cards = 0;
157     p_sys->i_node = 0;
158     p_sys->i_port = 0;
159     p_sys->i_guid = 0;
160     p_sys->i_channel = 63;
161     p_sys->p_raw1394 = NULL;
162     p_sys->p_avc1394 = NULL;
163     p_sys->p_frame = NULL;
164     p_sys->p_ev = NULL;
165
166     vlc_mutex_init( p_access, &p_sys->lock );
167
168     p_sys->i_node = DiscoverAVC( p_access, &p_sys->i_port, p_sys->i_guid );
169     if( p_sys->i_node < 0 )
170     {
171         msg_Err( p_access, "failed to open a Firewire (IEEE1394) connection" );
172         Close( p_this );
173         free( psz_name );
174         return VLC_EGENERIC;
175     }
176
177     p_sys->p_avc1394 = AVCOpen( p_access, p_sys->i_port );
178     if( !p_sys->p_avc1394 )
179     {
180         msg_Err( p_access, "no Digital Video Control device found on %s", psz_name );
181         Close( p_this );
182         free( psz_name );
183         return VLC_EGENERIC;
184     }
185
186     p_sys->p_raw1394 = raw1394_new_handle();
187     if( !p_sys->p_raw1394 )
188     {
189         msg_Err( p_access, "no Digital Video device found on %s", psz_name );
190         Close( p_this );
191         free( psz_name );
192         return VLC_EGENERIC;
193     }
194
195     p_sys->i_cards = raw1394_get_port_info( p_sys->p_raw1394, port_inf, 16 );
196     if( p_sys->i_cards < 0 )
197     {
198         msg_Err( p_access, "failed to get port info for %s", psz_name );
199         Close( p_this );
200         free( psz_name );
201         return VLC_EGENERIC;
202     }
203
204     if( raw1394_set_port( p_sys->p_raw1394, p_sys->i_port ) < 0 )
205     {
206         msg_Err( p_access, "failed to set port info for %s", psz_name );
207         Close( p_this );
208         free( psz_name );
209         return VLC_EGENERIC;
210     }
211
212     oldhandler = raw1394_set_iso_handler( p_sys->p_raw1394,
213                                           p_sys->i_channel, Raw1394Handler );
214     raw1394_set_userdata( p_sys->p_raw1394, p_access );
215     raw1394_start_iso_rcv( p_sys->p_raw1394, p_sys->i_channel );
216
217     p_sys->raw1394_poll.fd = raw1394_get_fd( p_sys->p_raw1394 );
218     p_sys->raw1394_poll.events = POLLIN | POLLERR | POLLHUP | POLLPRI;
219
220     /* Update default_pts to a suitable value for udp access */
221     var_Create( p_access, "dv-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
222
223     /* Now create our event thread catcher */
224     p_sys->p_ev = vlc_object_create( p_access, sizeof( event_thread_t ) );
225     if( !p_sys->p_ev )
226     {
227         msg_Err( p_access, "failed to create event thread for %s", psz_name );
228         Close( p_this );
229         free( psz_name );
230         return VLC_EGENERIC;
231     }
232     
233     p_sys->p_ev->p_frame = NULL;
234     p_sys->p_ev->pp_last = &p_sys->p_ev->p_frame;
235     p_sys->p_ev->p_access = p_access;
236     vlc_mutex_init( p_access, &p_sys->p_ev->lock );
237     vlc_thread_create( p_sys->p_ev, "dv event thread handler", Raw1394EventThread,
238                        VLC_THREAD_PRIORITY_OUTPUT, VLC_FALSE );
239
240     free( psz_name );
241     return VLC_SUCCESS;
242 }
243
244 /*****************************************************************************
245  * Close: free unused data structures
246  *****************************************************************************/
247 static void Close( vlc_object_t *p_this )
248 {
249     access_t     *p_access = (access_t*)p_this;
250     access_sys_t *p_sys = p_access->p_sys;
251
252     if( p_sys->p_ev )
253     {
254         /* stop the event handler */
255         p_sys->p_ev->b_die = VLC_TRUE;
256
257         if( p_sys->p_raw1394 )
258             raw1394_stop_iso_rcv( p_sys->p_raw1394, p_sys->i_channel );
259
260         vlc_mutex_destroy( &p_sys->p_ev->lock );
261         vlc_thread_join( p_sys->p_ev );
262
263         /* Cleanup frame data */
264         if( p_sys->p_ev->p_frame )
265         {
266             vlc_mutex_lock( &p_sys->p_ev->lock );
267             block_ChainRelease( p_sys->p_ev->p_frame );
268             p_sys->p_ev->p_frame = NULL;
269             p_sys->p_ev->pp_last = &p_sys->p_frame;
270             vlc_mutex_unlock( &p_sys->p_ev->lock );
271         }
272         vlc_object_destroy( p_sys->p_ev );
273     }
274
275     if( p_sys->p_frame )
276         block_ChainRelease( p_sys->p_frame );
277     if( p_sys->p_raw1394 )
278         raw1394_destroy_handle( p_sys->p_raw1394 );
279
280     AVCClose( p_access );
281
282     vlc_mutex_destroy( &p_sys->lock );
283     free( p_sys );
284 }
285
286 /*****************************************************************************
287  * Control:
288  *****************************************************************************/
289 static int Control( access_t *p_access, int i_query, va_list args )
290 {
291     access_sys_t *p_sys = p_access->p_sys;
292     vlc_bool_t   *pb_bool;
293     int64_t      *pi_64;
294
295     switch( i_query )
296     {
297         /* */
298         case ACCESS_CAN_PAUSE:
299             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
300             *pb_bool = VLC_TRUE;
301             break;
302
303        case ACCESS_CAN_SEEK:
304        case ACCESS_CAN_FASTSEEK:
305        case ACCESS_CAN_CONTROL_PACE:
306             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
307             *pb_bool = VLC_FALSE;
308             break;
309
310         case ACCESS_GET_PTS_DELAY:
311             pi_64 = (int64_t*)va_arg( args, int64_t * );
312             *pi_64 = var_GetInteger( p_access, "dv-caching" ) * 1000;
313             break;
314
315         /* */
316         case ACCESS_SET_PAUSE_STATE:
317             AVCPause( p_access, p_sys->i_node );
318             break;
319
320         case ACCESS_GET_TITLE_INFO:
321         case ACCESS_SET_TITLE:
322         case ACCESS_SET_SEEKPOINT:
323         case ACCESS_SET_PRIVATE_ID_STATE:
324             return VLC_EGENERIC;
325
326         default:
327             msg_Warn( p_access, "unimplemented query in control" );
328             return VLC_EGENERIC;
329
330     }
331     return VLC_SUCCESS;
332 }
333
334 static block_t *Block( access_t *p_access )
335 {
336     access_sys_t *p_sys = p_access->p_sys;
337     block_t *p_block = NULL;
338
339 //     if( !p_access->psz_demux )
340 //         p_access->psz_demux = strdup( "rawdv" );
341
342     vlc_mutex_lock( &p_sys->lock );
343     p_block = p_sys->p_frame;
344     //msg_Dbg( p_access, "sending frame %p",p_block );
345     p_sys->p_frame = NULL;
346     vlc_mutex_unlock( &p_sys->lock );
347
348     return p_block;
349 }
350
351 static int Raw1394EventThread( vlc_object_t *p_this )
352 {
353     event_thread_t *p_ev = (event_thread_t *) p_this;
354     access_t *p_access = (access_t *) p_ev->p_access;
355     access_sys_t *p_sys = (access_sys_t *) p_access->p_sys;
356     int result = 0;
357
358     AVCPlay( p_access, p_sys->i_node );
359
360     vlc_thread_ready( p_this );
361
362     while( !p_sys->p_ev->b_die )
363     {
364         while( ( result = poll( &(p_sys->raw1394_poll), 1, 200 ) ) < 0 )
365         {
366             if( !( errno == EAGAIN || errno == EINTR ) )
367             {
368                 perror( "error: raw1394 poll" );
369                 msg_Err( p_access, "retrying device raw1394" );
370             }
371             if( p_sys->p_ev->b_die )
372                 break;
373         }
374         if( p_sys->p_ev->b_die )
375                 break;
376         if( result > 0 && ( ( p_sys->raw1394_poll.revents & POLLIN )
377                 || ( p_sys->raw1394_poll.revents & POLLPRI ) ) )
378             result = raw1394_loop_iterate( p_sys->p_raw1394 );
379     }
380
381     AVCStop( p_access, p_sys->i_node );
382     return VLC_SUCCESS;
383 }
384
385 static int Raw1394Handler( raw1394handle_t handle, int channel, size_t length, quadlet_t *data )
386 {
387     access_t *p_access = NULL;
388     access_sys_t *p_sys = NULL;
389     block_t *p_block = NULL;
390
391     p_access = (access_t *) raw1394_get_userdata( handle );
392     if( !p_access ) return 0;
393
394     p_sys = p_access->p_sys;
395
396     /* skip empty packets */
397     if ( length > 16 )
398     {
399         unsigned char * p = ( unsigned char* ) &data[ 3 ];
400         int section_type = p[ 0 ] >> 5;           /* section type is in bits 5 - 7 */
401         int dif_sequence = p[ 1 ] >> 4;           /* dif sequence number is in bits 4 - 7 */
402         int dif_block = p[ 2 ];
403
404         vlc_mutex_lock( &p_sys->p_ev->lock );
405
406         /* if we are at the beginning of a frame, we put the previous
407            frame in our output_queue. */
408         if( (section_type == 0) && (dif_sequence == 0) )
409         {
410             vlc_mutex_lock( &p_sys->lock );
411             if( p_sys->p_ev->p_frame )
412             {
413                 /* Push current frame to p_access thread. */
414                 //p_sys->p_ev->p_frame->i_pts = mdate();
415                 block_ChainAppend( &p_sys->p_frame, p_sys->p_ev->p_frame );
416             }
417             /* reset list */
418             p_sys->p_ev->p_frame = block_New( p_access, 144000 );
419             p_sys->p_ev->pp_last = &p_sys->p_frame;
420             vlc_mutex_unlock( &p_sys->lock );
421         }
422
423         p_block = p_sys->p_ev->p_frame;
424         if( p_block )
425         {
426             switch ( section_type )
427             {
428             case 0:    /* 1 Header block */
429                 /* p[3] |= 0x80; // hack to force PAL data */
430                 memcpy( p_block->p_buffer + dif_sequence * 150 * 80, p, 480 );
431                 break;
432
433             case 1:    /* 2 Subcode blocks */
434                 memcpy( p_block->p_buffer + dif_sequence * 150 * 80 + ( 1 + dif_block ) * 80, p, 480 );
435                 break;
436
437             case 2:    /* 3 VAUX blocks */
438                 memcpy( p_block->p_buffer + dif_sequence * 150 * 80 + ( 3 + dif_block ) * 80, p, 480 );
439                 break;
440
441             case 3:    /* 9 Audio blocks interleaved with video */
442                 memcpy( p_block->p_buffer + dif_sequence * 150 * 80 + ( 6 + dif_block * 16 ) * 80, p, 480 );
443                 break;
444
445             case 4:    /* 135 Video blocks interleaved with audio */
446                 memcpy( p_block->p_buffer + dif_sequence * 150 * 80 + ( 7 + ( dif_block / 15 ) + dif_block ) * 80, p, 480 );
447                 break;
448
449             default:    /* we canĀ“t handle any other data */
450                 block_Release( p_block );
451                 p_block = NULL;
452                 break;
453             }
454         }
455         vlc_mutex_unlock( &p_sys->p_ev->lock );
456     }
457     return 0;
458 }
459
460 /*
461  * Routing borrowed from dvgrab-1.8
462  * Copyright by Arne Schirmacher <dvgrab@schirmacher.de>
463  * Dan Dennedy <dan@dennedy.org> and others
464  */
465 static int Raw1394GetNumPorts( access_t *p_access )
466 {
467     int n_ports;
468     struct raw1394_portinfo pinf[ 16 ];
469     raw1394handle_t handle;
470
471     /* get a raw1394 handle */
472     if ( !( handle = raw1394_new_handle() ) )
473     {
474         msg_Err( p_access, "raw1394 - failed to get handle: %s.\n", strerror( errno ) );
475         return VLC_EGENERIC;
476     }
477
478     if ( ( n_ports = raw1394_get_port_info( handle, pinf, 16 ) ) < 0 )
479     {
480         msg_Err( p_access, "raw1394 - failed to get port info: %s.\n", strerror( errno ) );
481         raw1394_destroy_handle( handle );
482         return VLC_EGENERIC;
483     }
484     raw1394_destroy_handle( handle );
485
486     return n_ports;
487 }
488
489 static raw1394handle_t Raw1394Open( access_t *p_access, int port )
490 {
491     int n_ports;
492     struct raw1394_portinfo pinf[ 16 ];
493     raw1394handle_t handle;
494
495     /* get a raw1394 handle */
496 #ifdef RAW1394_V_0_8
497
498     handle = raw1394_get_handle();
499 #else
500
501     handle = raw1394_new_handle();
502 #endif
503
504     if ( !handle )
505     {
506         msg_Err( p_access, "raw1394 - failed to get handle: %s.\n", strerror( errno ) );
507         return NULL;
508     }
509
510     if ( ( n_ports = raw1394_get_port_info( handle, pinf, 16 ) ) < 0 )
511     {
512         msg_Err( p_access, "raw1394 - failed to get port info: %s.\n", strerror( errno ) );
513         raw1394_destroy_handle( handle );
514         return NULL;
515     }
516
517     /* tell raw1394 which host adapter to use */
518     if ( raw1394_set_port( handle, port ) < 0 )
519     {
520         msg_Err( p_access, "raw1394 - failed to set set port: %s.\n", strerror( errno ) );
521         return NULL;
522     }
523
524     return handle;
525 }
526
527 static void Raw1394Close( raw1394handle_t handle )
528 {
529     raw1394_destroy_handle( handle );
530 }
531
532 static int DiscoverAVC( access_t *p_access, int* port, uint64_t guid )
533 {
534     rom1394_directory rom_dir;
535     raw1394handle_t handle = NULL;
536     int device = -1;
537     int i, j = 0;
538     int m = Raw1394GetNumPorts( p_access );
539
540     if( *port >= 0 )
541     {
542         /* search on explicit port */
543         j = *port;
544         m = *port + 1;
545     }
546
547     for( ; j < m && device == -1; j++ )
548     {
549         handle = Raw1394Open( p_access, j );
550         if( !handle )
551             return -1;
552
553         for( i = 0; i < raw1394_get_nodecount( handle ); ++i )
554         {
555             if( guid != 0 )
556             {
557                 /* select explicitly by GUID */
558                 if( guid == rom1394_get_guid( handle, i ) )
559                 {
560                     device = i;
561                     *port = j;
562                     break;
563                 }
564             }
565             else
566             {
567                 /* select first AV/C Tape Reccorder Player node */
568                 if( rom1394_get_directory( handle, i, &rom_dir ) < 0 )
569                 {
570                     msg_Err( p_access, "error reading config rom directory for node %d\n", i );
571                     continue;
572                 }
573                 if( ( rom1394_get_node_type( &rom_dir ) == ROM1394_NODE_TYPE_AVC ) &&
574                         avc1394_check_subunit_type( handle, i, AVC1394_SUBUNIT_TYPE_VCR ) )
575                 {
576                     device = i;
577                     *port = j;
578                     break;
579                 }
580             }
581         }
582         Raw1394Close( handle );
583     }
584
585     return device;
586 }
587
588 /*
589  * Handle AVC commands
590  */
591 static raw1394handle_t AVCOpen( access_t *p_access, int port )
592 {
593     access_sys_t *p_sys = p_access->p_sys;
594     int numcards;
595     struct raw1394_portinfo pinf[ 16 ];
596
597     p_sys->p_avc1394 = raw1394_new_handle();
598     if( !p_sys->p_avc1394 )
599         return NULL;
600
601     numcards = raw1394_get_port_info( p_sys->p_avc1394, pinf, 16 );
602     if( numcards < -1 )
603         return NULL;
604     if( raw1394_set_port( p_sys->p_avc1394, port ) < 0 )
605         return NULL;
606
607     raw1394_set_bus_reset_handler( p_sys->p_avc1394, AVCResetHandler );
608
609     return  p_sys->p_avc1394;
610 }
611
612 static void AVCClose( access_t *p_access )
613 {
614     access_sys_t *p_sys = p_access->p_sys;
615
616     if( p_sys->p_avc1394 )
617     {
618         raw1394_destroy_handle( p_sys->p_avc1394 );
619         p_sys->p_avc1394 = NULL;
620     }
621 }
622
623
624 static int AVCResetHandler( raw1394handle_t handle, unsigned int generation )
625 {
626     raw1394_update_generation( handle, generation );
627     return 0;
628 }
629
630 static int AVCPlay( access_t *p_access, int phyID )
631 {
632     access_sys_t *p_sys = p_access->p_sys;
633
634     msg_Dbg( p_access, "send play command over Digital Video control channel" );
635     if( !p_sys->p_avc1394 )
636         return 0;
637
638     if( phyID >= 0 )
639     {
640         if( !avc1394_vcr_is_recording( p_sys->p_avc1394, phyID ) &&
641             avc1394_vcr_is_playing( p_sys->p_avc1394, phyID ) != AVC1394_VCR_OPERAND_PLAY_FORWARD )
642                 avc1394_vcr_play( p_sys->p_avc1394, phyID );
643     }
644     return 0;
645 }
646
647 static int AVCPause( access_t *p_access, int phyID )
648 {
649     access_sys_t *p_sys = p_access->p_sys;
650
651     if( !p_sys->p_avc1394 )
652         return 0;
653
654     if( phyID >= 0 )
655     {
656         if( !avc1394_vcr_is_recording( p_sys->p_avc1394, phyID ) &&
657             ( avc1394_vcr_is_playing( p_sys->p_avc1394, phyID ) != AVC1394_VCR_OPERAND_PLAY_FORWARD_PAUSE ) )
658                 avc1394_vcr_pause( p_sys->p_avc1394, phyID );
659     }
660     return 0;
661 }
662
663
664 static int AVCStop( access_t *p_access, int phyID )
665 {
666     access_sys_t *p_sys = p_access->p_sys;
667
668     msg_Dbg( p_access, "closing Digital Video control channel" );
669     if( !p_sys->p_avc1394 )
670         return 0;
671
672     if ( phyID >= 0 )
673         avc1394_vcr_stop( p_sys->p_avc1394, phyID );
674
675     return 0;
676 }