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