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