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