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