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