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