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