[libdvdcss-devel] [PATCH] Add dvdcss_open_stream() API function
Diego Biurrun <[email protected]> Thu, 6 Nov 2014 21:46:27 +0100
| Newsgroups | gmane.comp.video.videolan.libdvdcss,gmane.comp.video.videolan.libdvdcss.devel |
|---|---|
| Message-ID | <[email protected]> |
From: Thomas Guillem <[email protected]> This allows opening a DVD device using external read/seek callbacks. Signed-off-by: Diego Biurrun <[email protected]> --- Rebased again, Doxygen documentation added. The docs could be improved. Suggestions welcome... NEWS | 2 + src/device.c | 109 +++++++++++++++++++++++++++++++++++++++++++++++++++- src/dvdcss/dvdcss.h | 12 ++++++ src/libdvdcss.c | 30 +++++++++++++++ src/libdvdcss.h | 3 ++ 5 files changed, 154 insertions(+), 2 deletions(-) diff --git a/NEWS b/NEWS index 61c7b58..3896654 100644 --- a/NEWS +++ b/NEWS @@ -5,6 +5,8 @@ Changes since 1.3.0: Windows NT 4.0 SP4 with IE 5.0 is now required. * Replace BeOS support by Haiku support. * dvdcss_error() now returns "const char *" instad of "char *". + * Add dvdcss_open_stream() to public API. This allows installing custom + callback functions for accessing DVD, e.g. over the network. * Miscellaneous cleanups to code, documentation, build system. diff --git a/src/device.c b/src/device.c index fd53ea6..6cf3905 100644 --- a/src/device.c +++ b/src/device.c @@ -82,6 +82,10 @@ static int libc_seek ( dvdcss_t, int ); static int libc_read ( dvdcss_t, void *, int ); static int libc_readv ( dvdcss_t, struct iovec *, int ); +static int stream_seek ( dvdcss_t, int ); +static int stream_read ( dvdcss_t, void *, int ); +static int stream_readv ( dvdcss_t, struct iovec *, int ); + #ifdef WIN32 static int win2k_open ( dvdcss_t, const char * ); static int win2k_seek ( dvdcss_t, int ); @@ -98,6 +102,9 @@ static int os2_open ( dvdcss_t, const char * ); int dvdcss_use_ioctls( dvdcss_t dvdcss ) { + if( dvdcss->p_stream ) + return 0; + #if defined( WIN32 ) if( dvdcss->b_file ) { @@ -189,8 +196,8 @@ void dvdcss_check_device ( dvdcss_t dvdcss ) int i, i_fd; #endif - /* If the device name is non-null, return */ - if( dvdcss->psz_device[0] ) + /* If the device name is non-NULL or stream is set, return. */ + if( dvdcss->psz_device[0] || dvdcss->p_stream ) { return; } @@ -347,6 +354,16 @@ int dvdcss_open_device ( dvdcss_t dvdcss ) print_debug( dvdcss, "opening target `%s'", psz_device ); + /* if callback functions are initialized */ + if( dvdcss->p_stream ) + { + print_debug( dvdcss, "using stream API for access" ); + dvdcss->pf_seek = stream_seek; + dvdcss->pf_read = stream_read; + dvdcss->pf_readv = stream_readv; + return 0; + } + #if defined( WIN32 ) dvdcss->b_file = 1; /* If device is "X:" or "X:\", we are not actually opening a file. */ @@ -562,6 +579,31 @@ static int libc_seek( dvdcss_t dvdcss, int i_blocks ) return dvdcss->i_pos; } +static int stream_seek( dvdcss_t dvdcss, int i_blocks ) +{ + off_t i_seek = i_blocks * DVDCSS_BLOCK_SIZE; + + if( !dvdcss->p_stream_cb->pf_seek ) + return -1; + + if( dvdcss->i_pos == i_blocks ) + { + /* We are already in position */ + return i_blocks; + } + + if( dvdcss->p_stream_cb->pf_seek( dvdcss->p_stream, i_seek ) != 0 ) + { + print_error( dvdcss, "seek error" ); + dvdcss->i_pos = -1; + return -1; + } + + dvdcss->i_pos = i_blocks; + + return dvdcss->i_pos; +} + #if defined( WIN32 ) static int win2k_seek( dvdcss_t dvdcss, int i_blocks ) { @@ -630,6 +672,46 @@ static int libc_read ( dvdcss_t dvdcss, void *p_buffer, int i_blocks ) return i_ret_blocks; } +static int stream_read ( dvdcss_t dvdcss, void *p_buffer, int i_blocks ) +{ + off_t i_size, i_ret, i_ret_blocks; + + i_size = i_blocks * DVDCSS_BLOCK_SIZE; + + if( !dvdcss->p_stream_cb->pf_read ) + return -1; + + i_ret = dvdcss->p_stream_cb->pf_read( dvdcss->p_stream, p_buffer, i_size ); + + if( i_ret < 0 ) + { + print_error( dvdcss, "read error" ); + dvdcss->i_pos = -1; + return i_ret; + } + + i_ret_blocks = i_ret / DVDCSS_BLOCK_SIZE; + + /* Handle partial reads */ + if( i_ret != i_size ) + { + int i_seek; + + dvdcss->i_pos = -1; + i_seek = stream_seek( dvdcss, i_ret_blocks ); + if( i_seek < 0 ) + { + return i_seek; + } + + /* We have to return now so that i_pos isn't clobbered */ + return i_ret_blocks; + } + + dvdcss->i_pos += i_ret_blocks; + return i_ret_blocks; +} + #if defined( WIN32 ) static int win2k_read ( dvdcss_t dvdcss, void *p_buffer, int i_blocks ) { @@ -721,6 +803,29 @@ static int libc_readv ( dvdcss_t dvdcss, struct iovec *p_iovec, int i_blocks ) #endif } +/***************************************************************************** + * stream_readv: vectored read + *****************************************************************************/ +static int stream_readv ( dvdcss_t dvdcss, struct iovec *p_iovec, int i_blocks ) +{ + int i_read; + + if( !dvdcss->p_stream_cb->pf_readv ) + return -1; + + i_read = dvdcss->p_stream_cb->pf_readv( dvdcss->p_stream, p_iovec, + i_blocks ); + + if( i_read < 0 ) + { + dvdcss->i_pos = -1; + return i_read; + } + + dvdcss->i_pos += i_read / DVDCSS_BLOCK_SIZE; + return i_read / DVDCSS_BLOCK_SIZE; +} + #if defined( WIN32 ) /***************************************************************************** * win2k_readv: vectored read using ReadFile for Win2K diff --git a/src/dvdcss/dvdcss.h b/src/dvdcss/dvdcss.h index ea62b8b..439680c 100644 --- a/src/dvdcss/dvdcss.h +++ b/src/dvdcss/dvdcss.h @@ -31,6 +31,8 @@ #define DVDCSS_DVDCSS_H 1 #endif +#include <stdint.h> + #ifdef __cplusplus extern "C" { #endif @@ -38,6 +40,14 @@ extern "C" { /** Library instance handle, to be used for each library call. */ typedef struct dvdcss_s* dvdcss_t; +struct dvdcss_stream_cb +{ + int ( *pf_seek ) ( void *p_stream, uint64_t i_pos); + int ( *pf_read ) ( void *p_stream, void *buffer, int i_read); + int ( *pf_readv ) ( void *p_stream, void *p_iovec, int i_blocks); +}; +typedef struct dvdcss_stream_cb dvdcss_stream_cb; + /** The block size of a DVD. */ #define DVDCSS_BLOCK_SIZE 2048 @@ -73,6 +83,8 @@ typedef struct dvdcss_s* dvdcss_t; * Exported prototypes. */ LIBDVDCSS_EXPORT dvdcss_t dvdcss_open ( const char *psz_target ); +LIBDVDCSS_EXPORT dvdcss_t dvdcss_open_stream( void *p_stream, + dvdcss_stream_cb *p_stream_cb ); LIBDVDCSS_EXPORT int dvdcss_close ( dvdcss_t ); LIBDVDCSS_EXPORT int dvdcss_seek ( dvdcss_t, int i_blocks, diff --git a/src/libdvdcss.c b/src/libdvdcss.c index 4c19c6d..b90243d 100644 --- a/src/libdvdcss.c +++ b/src/libdvdcss.c @@ -145,6 +145,8 @@ #define MANUFACTURING_DATE_LENGTH 16 +static dvdcss_t dvdcss_open_common ( const char *psz_target, void *p_stream, + dvdcss_stream_cb *p_stream_cb ); static void set_verbosity( dvdcss_t dvdcss ) { const char *psz_verbose = getenv( "DVDCSS_VERBOSE" ); @@ -452,12 +454,37 @@ static void init_cache( dvdcss_t dvdcss ) */ LIBDVDCSS_EXPORT dvdcss_t dvdcss_open ( const char *psz_target ) { + return dvdcss_open_common( psz_target, NULL, NULL ); +} + +/** + * \brief Open a DVD device using dvdcss_stream_cb. + * + * \param p_stream a private handle used by p_stream_cb + * \param p_stream_cb a struct containing seek and read functions + * \return a handle to a dvdcss instance or NULL on error. + * + * \see dvdcss_open() + */ +LIBDVDCSS_EXPORT dvdcss_t dvdcss_open_stream ( void *p_stream, + dvdcss_stream_cb *p_stream_cb ) +{ + return dvdcss_open_common( NULL, p_stream, p_stream_cb ); +} + +static dvdcss_t dvdcss_open_common ( const char *psz_target, void *p_stream, + dvdcss_stream_cb *p_stream_cb ) +{ int i_ret; #ifdef DVDCSS_RAW_OPEN const char *psz_raw_device = getenv( "DVDCSS_RAW_DEVICE" ); #endif + if( psz_target == NULL && + ( p_stream == NULL || p_stream_cb == NULL ) ) + return NULL; + /* Allocate the library structure. */ dvdcss_t dvdcss = malloc( sizeof( *dvdcss ) ); if( dvdcss == NULL ) @@ -475,6 +502,9 @@ LIBDVDCSS_EXPORT dvdcss_t dvdcss_open ( const char *psz_target ) dvdcss->i_method = DVDCSS_METHOD_KEY; dvdcss->psz_cachefile[0] = '\0'; + dvdcss->p_stream = p_stream; + dvdcss->p_stream_cb = p_stream_cb; + /* Set library verbosity from DVDCSS_VERBOSE environment variable. */ set_verbosity( dvdcss ); diff --git a/src/libdvdcss.h b/src/libdvdcss.h index 9750c37..8105fc5 100644 --- a/src/libdvdcss.h +++ b/src/libdvdcss.h @@ -80,6 +80,9 @@ struct dvdcss_s int i_readv_buf_size; #endif /* WIN32 */ + void *p_stream; + dvdcss_stream_cb *p_stream_cb; + #ifdef DVDCSS_RAW_OPEN int i_raw_fd; #endif -- 2.1.0 _______________________________________________ libdvdcss-devel mailing list [email protected] https://mailman.videolan.org/listinfo/libdvdcss-devel