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