]> git.sesse.net Git - vlc/blob - modules/access/dv.c
20079512b45978fda690a23b9d10d63008af090b
[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_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( N_("Digital Video (Firewire/ieee1394)  input") )
75     set_shortname( N_("DV") )
76     set_category( CAT_INPUT )
77     set_subcategory( SUBCAT_INPUT_ACCESS )
78     add_integer( "dv-caching", 60000 / 1000, NULL, CACHING_TEXT, CACHING_LONGTEXT, true )
79         change_safe()
80     set_capability( "access", 0 )
81     add_shortcut( "dv" )
82     add_shortcut( "dv1394" )
83     add_shortcut( "raw1394" )
84     set_callbacks( Open, Close )
85 vlc_module_end ()
86
87 typedef struct
88 {
89     VLC_COMMON_MEMBERS
90
91     access_t        *p_access;
92     vlc_mutex_t     lock;
93     block_t         *p_frame;
94     block_t         **pp_last;
95
96 } event_thread_t;
97
98 static void* Raw1394EventThread( vlc_object_t * );
99 static enum raw1394_iso_disposition
100 Raw1394Handler(raw1394handle_t, unsigned char *,
101         unsigned int, unsigned char,
102         unsigned char, unsigned char, unsigned int,
103         unsigned int);
104
105 static int Raw1394GetNumPorts( access_t *p_access );
106 static raw1394handle_t Raw1394Open( access_t *, int );
107 static void Raw1394Close( raw1394handle_t );
108
109 static int DiscoverAVC( access_t *, int *, uint64_t );
110 static raw1394handle_t AVCOpen( access_t *, int );
111 static void AVCClose( access_t * );
112 static int AVCResetHandler( raw1394handle_t, unsigned int );
113 static int AVCPlay( access_t *, int );
114 static int AVCPause( access_t *, int );
115 static int AVCStop( access_t *, int );
116
117 struct access_sys_t
118 {
119     raw1394handle_t p_avc1394;
120     raw1394handle_t p_raw1394;
121     struct pollfd   raw1394_poll;
122
123     int i_cards;
124     int i_node;
125     int i_port;
126     int i_channel;
127     uint64_t i_guid;
128
129     /* event */
130     event_thread_t *p_ev;
131     vlc_mutex_t lock;
132     block_t *p_frame;
133 };
134
135 #define ISOCHRONOUS_QUEUE_LENGTH 1000
136 #define ISOCHRONOUS_MAX_PACKET_SIZE 4096
137
138 /*****************************************************************************
139  * Open: open the file
140  *****************************************************************************/
141 static int Open( vlc_object_t *p_this )
142 {
143     access_t     *p_access = (access_t*)p_this;
144     access_sys_t *p_sys;
145     char *psz_name = strdup( p_access->psz_path );
146
147     struct raw1394_portinfo port_inf[ 16 ];
148
149     msg_Dbg( p_access, "opening device %s", psz_name );
150
151     /* Set up p_access */
152     access_InitFields( p_access );
153     ACCESS_SET_CALLBACKS( NULL, Block, Control, NULL );
154
155     p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );
156     if( !p_sys )
157     {
158         free( psz_name );
159         return VLC_EGENERIC;
160     }
161
162     p_sys->i_cards = 0;
163     p_sys->i_node = 0;
164     p_sys->i_port = 0;
165     p_sys->i_guid = 0;
166     p_sys->i_channel = 63;
167     p_sys->p_raw1394 = NULL;
168     p_sys->p_avc1394 = NULL;
169     p_sys->p_frame = NULL;
170     p_sys->p_ev = NULL;
171
172     vlc_mutex_init( &p_sys->lock );
173
174     p_sys->i_node = DiscoverAVC( p_access, &p_sys->i_port, p_sys->i_guid );
175     if( p_sys->i_node < 0 )
176     {
177         msg_Err( p_access, "failed to open a Firewire (IEEE1394) connection" );
178         Close( p_this );
179         free( psz_name );
180         return VLC_EGENERIC;
181     }
182
183     p_sys->p_avc1394 = AVCOpen( p_access, p_sys->i_port );
184     if( !p_sys->p_avc1394 )
185     {
186         msg_Err( p_access, "no Digital Video Control device found on %s", psz_name );
187         Close( p_this );
188         free( psz_name );
189         return VLC_EGENERIC;
190     }
191
192     p_sys->p_raw1394 = raw1394_new_handle();
193     if( !p_sys->p_raw1394 )
194     {
195         msg_Err( p_access, "no Digital Video device found on %s", psz_name );
196         Close( p_this );
197         free( psz_name );
198         return VLC_EGENERIC;
199     }
200
201     p_sys->i_cards = raw1394_get_port_info( p_sys->p_raw1394, port_inf, 16 );
202     if( p_sys->i_cards < 0 )
203     {
204         msg_Err( p_access, "failed to get port info for %s", psz_name );
205         Close( p_this );
206         free( psz_name );
207         return VLC_EGENERIC;
208     }
209
210     if( raw1394_set_port( p_sys->p_raw1394, p_sys->i_port ) < 0 )
211     {
212         msg_Err( p_access, "failed to set port info for %s", psz_name );
213         Close( p_this );
214         free( psz_name );
215         return VLC_EGENERIC;
216     }
217
218     if ( raw1394_iso_recv_init( p_sys->p_raw1394, Raw1394Handler,
219                 ISOCHRONOUS_QUEUE_LENGTH, ISOCHRONOUS_MAX_PACKET_SIZE,
220                 p_sys->i_channel, RAW1394_DMA_PACKET_PER_BUFFER, -1 ) < 0 )
221     {
222         msg_Err( p_access, "failed to init isochronous recv for %s", psz_name );
223         Close( p_this );
224         free( psz_name );
225         return VLC_EGENERIC;
226     }
227
228     raw1394_set_userdata( p_sys->p_raw1394, p_access );
229     raw1394_iso_recv_start( p_sys->p_raw1394, -1, -1, 0 );
230
231     p_sys->raw1394_poll.fd = raw1394_get_fd( p_sys->p_raw1394 );
232     p_sys->raw1394_poll.events = POLLIN | POLLPRI;
233
234     /* Update default_pts to a suitable value for udp access */
235     var_Create( p_access, "dv-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
236
237     /* Now create our event thread catcher */
238     p_sys->p_ev = vlc_object_create( p_access, sizeof( event_thread_t ) );
239     if( !p_sys->p_ev )
240     {
241         msg_Err( p_access, "failed to create event thread for %s", psz_name );
242         Close( p_this );
243         free( psz_name );
244         return VLC_EGENERIC;
245     }
246
247     p_sys->p_ev->p_frame = NULL;
248     p_sys->p_ev->pp_last = &p_sys->p_ev->p_frame;
249     p_sys->p_ev->p_access = p_access;
250     vlc_mutex_init( &p_sys->p_ev->lock );
251     vlc_thread_create( p_sys->p_ev, "dv event thread handler",
252                        Raw1394EventThread, VLC_THREAD_PRIORITY_OUTPUT );
253
254     free( psz_name );
255     return VLC_SUCCESS;
256 }
257
258 /*****************************************************************************
259  * Close: free unused data structures
260  *****************************************************************************/
261 static void Close( vlc_object_t *p_this )
262 {
263     access_t     *p_access = (access_t*)p_this;
264     access_sys_t *p_sys = p_access->p_sys;
265
266     if( p_sys->p_ev )
267     {
268         /* stop the event handler */
269         vlc_object_kill( p_sys->p_ev );
270
271         if( p_sys->p_raw1394 )
272             raw1394_iso_shutdown( p_sys->p_raw1394 );
273
274         vlc_thread_join( p_sys->p_ev );
275         vlc_mutex_destroy( &p_sys->p_ev->lock );
276
277         /* Cleanup frame data */
278         if( p_sys->p_ev->p_frame )
279         {
280             block_ChainRelease( p_sys->p_ev->p_frame );
281             p_sys->p_ev->p_frame = NULL;
282             p_sys->p_ev->pp_last = &p_sys->p_frame;
283         }
284         vlc_object_release( p_sys->p_ev );
285     }
286
287     if( p_sys->p_frame )
288         block_ChainRelease( p_sys->p_frame );
289     if( p_sys->p_raw1394 )
290         raw1394_destroy_handle( p_sys->p_raw1394 );
291
292     AVCClose( p_access );
293
294     vlc_mutex_destroy( &p_sys->lock );
295     free( p_sys );
296 }
297
298 /*****************************************************************************
299  * Control:
300  *****************************************************************************/
301 static int Control( access_t *p_access, int i_query, va_list args )
302 {
303     switch( i_query )
304     {
305         /* */
306         case ACCESS_CAN_PAUSE:
307             *va_arg( args, bool* ) = true;
308             break;
309
310        case ACCESS_CAN_SEEK:
311        case ACCESS_CAN_FASTSEEK:
312        case ACCESS_CAN_CONTROL_PACE:
313             *va_arg( args, bool* ) = false;
314             break;
315
316         case ACCESS_GET_PTS_DELAY:
317             *va_arg( args, int64_t * )
318                    = (int64_t)var_GetInteger( p_access, "dv-caching" ) * 1000;
319             break;
320
321         /* */
322         case ACCESS_SET_PAUSE_STATE:
323             AVCPause( p_access, p_access->p_sys->i_node );
324             break;
325
326         case ACCESS_GET_TITLE_INFO:
327         case ACCESS_SET_TITLE:
328         case ACCESS_SET_SEEKPOINT:
329         case ACCESS_SET_PRIVATE_ID_STATE:
330         case ACCESS_GET_CONTENT_TYPE:
331             return VLC_EGENERIC;
332
333         default:
334             msg_Warn( p_access, "unimplemented query in control" );
335             return VLC_EGENERIC;
336
337     }
338     return VLC_SUCCESS;
339 }
340
341 static block_t *Block( access_t *p_access )
342 {
343     access_sys_t *p_sys = p_access->p_sys;
344     block_t *p_block = NULL;
345
346     vlc_mutex_lock( &p_sys->lock );
347     p_block = p_sys->p_frame;
348     //msg_Dbg( p_access, "sending frame %p",p_block );
349     p_sys->p_frame = NULL;
350     vlc_mutex_unlock( &p_sys->lock );
351
352     return p_block;
353 }
354
355 static void* Raw1394EventThread( vlc_object_t *p_this )
356 {
357     event_thread_t *p_ev = (event_thread_t *) p_this;
358     access_t *p_access = (access_t *) p_ev->p_access;
359     access_sys_t *p_sys = (access_sys_t *) p_access->p_sys;
360     int result = 0;
361     int canc = vlc_savecancel ();
362
363     AVCPlay( p_access, p_sys->i_node );
364
365     while( vlc_object_alive (p_sys->p_ev) )
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         }
375         if( !vlc_object_alive (p_sys->p_ev) )
376                 break;
377         if( result > 0 && ( ( p_sys->raw1394_poll.revents & POLLIN )
378                 || ( p_sys->raw1394_poll.revents & POLLPRI ) ) )
379             result = raw1394_loop_iterate( p_sys->p_raw1394 );
380     }
381
382     AVCStop( p_access, p_sys->i_node );
383     vlc_restorecancel (canc);
384     return NULL;
385 }
386
387 static enum raw1394_iso_disposition
388 Raw1394Handler(raw1394handle_t handle, unsigned char *data,
389         unsigned int length, unsigned char channel,
390         unsigned char tag, unsigned char sy, unsigned int cycle,
391         unsigned int dropped)
392 {
393     access_t *p_access = NULL;
394     access_sys_t *p_sys = NULL;
395     block_t *p_block = NULL;
396     VLC_UNUSED(channel); VLC_UNUSED(tag);
397     VLC_UNUSED(sy); VLC_UNUSED(cycle); VLC_UNUSED(dropped);
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 = data + 8;
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
464         vlc_mutex_unlock( &p_sys->p_ev->lock );
465     }
466     return 0;
467 }
468
469 /*
470  * Routing borrowed from dvgrab-1.8
471  * Copyright by Arne Schirmacher <dvgrab@schirmacher.de>
472  * Dan Dennedy <dan@dennedy.org> and others
473  */
474 static int Raw1394GetNumPorts( access_t *p_access )
475 {
476     int n_ports;
477     struct raw1394_portinfo pinf[ 16 ];
478     raw1394handle_t handle;
479
480     /* get a raw1394 handle */
481     if( !( handle = raw1394_new_handle() ) )
482     {
483         msg_Err( p_access, "raw1394 - failed to get handle: %m." );
484         return VLC_EGENERIC;
485     }
486
487     if( ( n_ports = raw1394_get_port_info( handle, pinf, 16 ) ) < 0 )
488     {
489         msg_Err( p_access, "raw1394 - failed to get port info: %m." );
490         raw1394_destroy_handle( handle );
491         return VLC_EGENERIC;
492     }
493     raw1394_destroy_handle( handle );
494
495     return n_ports;
496 }
497
498 static raw1394handle_t Raw1394Open( access_t *p_access, int port )
499 {
500     int n_ports;
501     struct raw1394_portinfo pinf[ 16 ];
502     raw1394handle_t handle;
503
504     /* get a raw1394 handle */
505     handle = raw1394_new_handle();
506     if( !handle )
507     {
508         msg_Err( p_access, "raw1394 - failed to get handle: %m." );
509         return NULL;
510     }
511
512     if( ( n_ports = raw1394_get_port_info( handle, pinf, 16 ) ) < 0 )
513     {
514         msg_Err( p_access, "raw1394 - failed to get port info: %m." );
515         raw1394_destroy_handle( handle );
516         return NULL;
517     }
518
519     /* tell raw1394 which host adapter to use */
520     if( raw1394_set_port( handle, port ) < 0 )
521     {
522         msg_Err( p_access, "raw1394 - failed to set set port: %m." );
523         return NULL;
524     }
525
526     return handle;
527 }
528
529 static void Raw1394Close( raw1394handle_t handle )
530 {
531     raw1394_destroy_handle( handle );
532 }
533
534 static int DiscoverAVC( access_t *p_access, int* port, uint64_t guid )
535 {
536     rom1394_directory rom_dir;
537     raw1394handle_t handle = NULL;
538     int device = -1;
539     int i, j = 0;
540     int m = Raw1394GetNumPorts( p_access );
541
542     if( *port >= 0 )
543     {
544         /* search on explicit port */
545         j = *port;
546         m = *port + 1;
547     }
548
549     for( ; j < m && device == -1; j++ )
550     {
551         handle = Raw1394Open( p_access, j );
552         if( !handle )
553             return -1;
554
555         for( i = 0; i < raw1394_get_nodecount( handle ); ++i )
556         {
557             if( guid != 0 )
558             {
559                 /* select explicitly by GUID */
560                 if( guid == rom1394_get_guid( handle, i ) )
561                 {
562                     device = i;
563                     *port = j;
564                     break;
565                 }
566             }
567             else
568             {
569                 /* select first AV/C Tape Reccorder Player node */
570                 if( rom1394_get_directory( handle, i, &rom_dir ) < 0 )
571                 {
572                     msg_Err( p_access, "error reading config rom directory for node %d", i );
573                     continue;
574                 }
575                 if( ( rom1394_get_node_type( &rom_dir ) == ROM1394_NODE_TYPE_AVC ) &&
576                         avc1394_check_subunit_type( handle, i, AVC1394_SUBUNIT_TYPE_VCR ) )
577                 {
578                     device = i;
579                     *port = j;
580                     break;
581                 }
582             }
583         }
584         Raw1394Close( handle );
585     }
586
587     return device;
588 }
589
590 /*
591  * Handle AVC commands
592  */
593 static raw1394handle_t AVCOpen( access_t *p_access, int port )
594 {
595     access_sys_t *p_sys = p_access->p_sys;
596     struct raw1394_portinfo pinf[ 16 ];
597     int numcards;
598
599     p_sys->p_avc1394 = raw1394_new_handle();
600     if( !p_sys->p_avc1394 )
601         return NULL;
602
603     numcards = raw1394_get_port_info( p_sys->p_avc1394, pinf, 16 );
604     if( numcards < -1 )
605         return NULL;
606     if( raw1394_set_port( p_sys->p_avc1394, port ) < 0 )
607         return NULL;
608
609     raw1394_set_bus_reset_handler( p_sys->p_avc1394, AVCResetHandler );
610
611     return  p_sys->p_avc1394;
612 }
613
614 static void AVCClose( access_t *p_access )
615 {
616     access_sys_t *p_sys = p_access->p_sys;
617
618     if( p_sys->p_avc1394 )
619     {
620         raw1394_destroy_handle( p_sys->p_avc1394 );
621         p_sys->p_avc1394 = NULL;
622     }
623 }
624
625 static int AVCResetHandler( raw1394handle_t handle, unsigned int generation )
626 {
627     raw1394_update_generation( handle, generation );
628     return 0;
629 }
630
631 static int AVCPlay( access_t *p_access, int phyID )
632 {
633     access_sys_t *p_sys = p_access->p_sys;
634
635     msg_Dbg( p_access, "send play command over Digital Video control channel" );
636
637     if( p_sys->p_avc1394 && phyID >= 0 )
638     {
639         if( !avc1394_vcr_is_recording( p_sys->p_avc1394, phyID ) &&
640             avc1394_vcr_is_playing( p_sys->p_avc1394, phyID ) != AVC1394_VCR_OPERAND_PLAY_FORWARD )
641                 avc1394_vcr_play( p_sys->p_avc1394, phyID );
642     }
643     return 0;
644 }
645
646 static int AVCPause( access_t *p_access, int phyID )
647 {
648     access_sys_t *p_sys = p_access->p_sys;
649
650     if( p_sys->p_avc1394 && phyID >= 0 )
651     {
652         if( !avc1394_vcr_is_recording( p_sys->p_avc1394, phyID ) &&
653             ( avc1394_vcr_is_playing( p_sys->p_avc1394, phyID ) != AVC1394_VCR_OPERAND_PLAY_FORWARD_PAUSE ) )
654                 avc1394_vcr_pause( p_sys->p_avc1394, phyID );
655     }
656     return 0;
657 }
658
659
660 static int AVCStop( access_t *p_access, int phyID )
661 {
662     access_sys_t *p_sys = p_access->p_sys;
663
664     msg_Dbg( p_access, "closing Digital Video control channel" );
665
666     if ( p_sys->p_avc1394 && phyID >= 0 )
667         avc1394_vcr_stop( p_sys->p_avc1394, phyID );
668
669     return 0;
670 }