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