]> git.sesse.net Git - vlc/blobdiff - modules/access/oss.c
demux: provide both URL and file path as with access
[vlc] / modules / access / oss.c
index 01073a54bfea4918f7279ad164b30347d65856fb..3fe0b7c7ffc0bd688376e854933d1f6b5cc1cc3f 100644 (file)
 #include <vlc_plugin.h>
 #include <vlc_access.h>
 #include <vlc_demux.h>
-#include <vlc_input.h>
-#include <vlc_vout.h>
+#include <vlc_fs.h>
 
-#include <ctype.h>
 #include <fcntl.h>
 #include <unistd.h>
 #include <sys/ioctl.h>
 #include <sys/mman.h>
 
-#include <sys/soundcard.h>
+#ifdef HAVE_SYS_SOUNDCARD_H
+#  include <sys/soundcard.h>
+#endif
+#ifdef HAVE_SOUNDCARD_H
+#  include <soundcard.h>
+#endif
 
 #include <poll.h>
 
@@ -84,11 +87,11 @@ vlc_module_begin ()
     set_callbacks( DemuxOpen, DemuxClose )
 
     add_bool( CFG_PREFIX "stereo", true, NULL, STEREO_TEXT, STEREO_LONGTEXT,
-                true );
+                true )
     add_integer( CFG_PREFIX "samplerate", 48000, NULL, SAMPLERATE_TEXT,
-                SAMPLERATE_LONGTEXT, true );
+                SAMPLERATE_LONGTEXT, true )
     add_integer( CFG_PREFIX "caching", DEFAULT_PTS_DELAY / 1000, NULL,
-                CACHING_TEXT, CACHING_LONGTEXT, true );
+                CACHING_TEXT, CACHING_LONGTEXT, true )
 vlc_module_end ()
 
 /*****************************************************************************
@@ -115,15 +118,17 @@ struct demux_sys_t
 {
     const char *psz_device;  /* OSS device from MRL */
 
-    int  i_fd_audio;
+    int  i_fd;
 
     /* Audio */
-    int i_pts;
+    int i_cache;
     unsigned int i_sample_rate;
     bool b_stereo;
-    size_t i_audio_max_frame_size;
-    block_t *p_block_audio;
-    es_out_id_t *p_es_audio;
+    size_t i_max_frame_size;
+    block_t *p_block;
+    es_out_id_t *p_es;
+
+    int64_t i_next_demux_date; /* Used to handle oss:// as input-slave properly */
 };
 
 static int FindMainDevice( demux_t *p_demux )
@@ -132,10 +137,10 @@ static int FindMainDevice( demux_t *p_demux )
     if( ProbeAudioDevOss( p_demux, p_demux->p_sys->psz_device ) )
     {
         msg_Dbg( p_demux, "'%s' is an audio device", p_demux->p_sys->psz_device );
-        p_demux->p_sys->i_fd_audio = OpenAudioDev( p_demux );
+        p_demux->p_sys->i_fd = OpenAudioDev( p_demux );
     }
 
-    if( p_demux->p_sys->i_fd_audio < 0 )
+    if( p_demux->p_sys->i_fd < 0 )
         return VLC_EGENERIC;
     return VLC_SUCCESS;
 }
@@ -167,17 +172,16 @@ static int DemuxOpen( vlc_object_t *p_this )
 
     p_sys->i_sample_rate = var_CreateGetInteger( p_demux, CFG_PREFIX "samplerate" );
     p_sys->b_stereo = var_CreateGetBool( p_demux, CFG_PREFIX "stereo" );
-    p_sys->i_pts = var_CreateGetInteger( p_demux, CFG_PREFIX "caching" );
-    p_sys->psz_device = NULL;
-    p_sys->i_fd_audio = -1;
-    p_sys->p_es_audio = 0;
-    p_sys->p_block_audio = 0;
-
-    if( p_demux->psz_path && *p_demux->psz_path )
-        p_sys->psz_device = p_demux->psz_path;
+    p_sys->i_cache = var_CreateGetInteger( p_demux, CFG_PREFIX "caching" );
+    p_sys->i_fd = -1;
+    p_sys->p_es = NULL;
+    p_sys->p_block = NULL;
+    p_sys->i_next_demux_date = -1;
+
+    if( p_demux->psz_location && *p_demux->psz_location )
+        p_sys->psz_device = p_demux->psz_location;
     else
         p_sys->psz_device = OSS_DEFAULT;
-    msg_Err( p_this, "Device is %s", p_sys->psz_device );
 
     if( FindMainDevice( p_demux ) != VLC_SUCCESS )
     {
@@ -196,9 +200,9 @@ static void DemuxClose( vlc_object_t *p_this )
     demux_t     *p_demux = (demux_t *)p_this;
     demux_sys_t *p_sys   = p_demux->p_sys;
 
-    if( p_sys->i_fd_audio >= 0 ) close( p_sys->i_fd_audio );
+    if( p_sys->i_fd >= 0 ) close( p_sys->i_fd );
 
-    if( p_sys->p_block_audio ) block_Release( p_sys->p_block_audio );
+    if( p_sys->p_block ) block_Release( p_sys->p_block );
     free( p_sys );
 }
 
@@ -216,7 +220,6 @@ static int DemuxControl( demux_t *p_demux, int i_query, va_list args )
         /* Special for access_demux */
         case DEMUX_CAN_PAUSE:
         case DEMUX_CAN_SEEK:
-        case DEMUX_SET_PAUSE_STATE:
         case DEMUX_CAN_CONTROL_PACE:
             pb = (bool*)va_arg( args, bool * );
             *pb = false;
@@ -224,7 +227,7 @@ static int DemuxControl( demux_t *p_demux, int i_query, va_list args )
 
         case DEMUX_GET_PTS_DELAY:
             pi64 = (int64_t*)va_arg( args, int64_t * );
-            *pi64 = (int64_t)p_sys->i_pts * 1000;
+            *pi64 = (int64_t)p_sys->i_cache * 1000;
             return VLC_SUCCESS;
 
         case DEMUX_GET_TIME:
@@ -232,6 +235,10 @@ static int DemuxControl( demux_t *p_demux, int i_query, va_list args )
             *pi64 = mdate();
             return VLC_SUCCESS;
 
+        case DEMUX_SET_NEXT_DEMUX_TIME:
+            p_sys->i_next_demux_date = (int64_t)va_arg( args, int64_t );
+            return VLC_SUCCESS;
+
         /* TODO implement others */
         default:
             return VLC_EGENERIC;
@@ -248,23 +255,35 @@ static int Demux( demux_t *p_demux )
     demux_sys_t *p_sys = p_demux->p_sys;
 
     struct pollfd fd;
-    fd.fd = p_sys->i_fd_audio;
+    fd.fd = p_sys->i_fd;
     fd.events = POLLIN|POLLPRI;
     fd.revents = 0;
 
-    /* Wait for data */
-    if( poll( &fd, 1, 500 ) ) /* Timeout after 0.5 seconds since I don't know if pf_demux can be blocking. */
+    block_t *p_block = NULL;
+
+    do
     {
-        if( fd.revents & (POLLIN|POLLPRI) )
+        if( p_block )
+        {
+            es_out_Send( p_demux->out, p_sys->p_es, p_block );
+            p_block = NULL;
+        }
+
+        /* Wait for data */
+        if( poll( &fd, 1, 10 ) ) /* Timeout after 0.01 seconds. Bigger delays are an issue when used with/as an input-slave since all the inputs run in the same thread. */
         {
-            block_t *p_block = GrabAudio( p_demux );
-            if( p_block )
+            if( fd.revents & (POLLIN|POLLPRI) )
             {
-                es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block->i_pts );
-                es_out_Send( p_demux->out, p_sys->p_es_audio, p_block );
+                p_block = GrabAudio( p_demux );
+                if( p_block )
+                    es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block->i_pts );
             }
         }
-    }
+    } while( p_block && p_sys->i_next_demux_date > 0 &&
+             p_block->i_pts < p_sys->i_next_demux_date );
+
+    if( p_block )
+        es_out_Send( p_demux->out, p_sys->p_es, p_block );
 
     return 1;
 }
@@ -279,8 +298,8 @@ static block_t* GrabAudio( demux_t *p_demux )
     int i_read = 0, i_correct;
     block_t *p_block;
 
-    if( p_sys->p_block_audio ) p_block = p_sys->p_block_audio;
-    else p_block = block_New( p_demux, p_sys->i_audio_max_frame_size );
+    if( p_sys->p_block ) p_block = p_sys->p_block;
+    else p_block = block_New( p_demux, p_sys->i_max_frame_size );
 
     if( !p_block )
     {
@@ -288,19 +307,19 @@ static block_t* GrabAudio( demux_t *p_demux )
         return 0;
     }
 
-    p_sys->p_block_audio = p_block;
+    p_sys->p_block = p_block;
 
-    i_read = read( p_sys->i_fd_audio, p_block->p_buffer,
-                p_sys->i_audio_max_frame_size );
+    i_read = read( p_sys->i_fd, p_block->p_buffer,
+                p_sys->i_max_frame_size );
 
     if( i_read <= 0 ) return 0;
 
     p_block->i_buffer = i_read;
-    p_sys->p_block_audio = 0;
+    p_sys->p_block = 0;
 
     /* Correct the date because of kernel buffering */
     i_correct = i_read;
-    if( ioctl( p_sys->i_fd_audio, SNDCTL_DSP_GETISPACE, &buf_info ) == 0 )
+    if( ioctl( p_sys->i_fd, SNDCTL_DSP_GETISPACE, &buf_info ) == 0 )
     {
         i_correct += buf_info.bytes;
     }
@@ -321,7 +340,7 @@ static int OpenAudioDevOss( demux_t *p_demux )
     int i_fd;
     int i_format;
 
-    i_fd = open( p_demux->p_sys->psz_device, O_RDONLY | O_NONBLOCK );
+    i_fd = vlc_open( p_demux->p_sys->psz_device, O_RDONLY | O_NONBLOCK );
 
     if( i_fd < 0 )
     {
@@ -352,7 +371,7 @@ static int OpenAudioDevOss( demux_t *p_demux )
         goto adev_fail;
     }
 
-    p_demux->p_sys->i_audio_max_frame_size = 6 * 1024;
+    p_demux->p_sys->i_max_frame_size = 6 * 1024;
 
     return i_fd;
 
@@ -375,7 +394,7 @@ static int OpenAudioDev( demux_t *p_demux )
              p_sys->i_sample_rate );
 
     es_format_t fmt;
-    es_format_Init( &fmt, AUDIO_ES, VLC_FOURCC('a','r','a','w') );
+    es_format_Init( &fmt, AUDIO_ES, VLC_CODEC_S16L );
 
     fmt.audio.i_channels = p_sys->b_stereo ? 2 : 1;
     fmt.audio.i_rate = p_sys->i_sample_rate;
@@ -386,7 +405,7 @@ static int OpenAudioDev( demux_t *p_demux )
     msg_Dbg( p_demux, "new audio es %d channels %dHz",
              fmt.audio.i_channels, fmt.audio.i_rate );
 
-    p_sys->p_es_audio = es_out_Add( p_demux->out, &fmt );
+    p_sys->p_es = es_out_Add( p_demux->out, &fmt );
 
     return i_fd;
 }
@@ -397,7 +416,7 @@ static int OpenAudioDev( demux_t *p_demux )
 static bool ProbeAudioDevOss( demux_t *p_demux, const char *psz_device )
 {
     int i_caps;
-    int i_fd = open( psz_device, O_RDONLY | O_NONBLOCK );
+    int i_fd = vlc_open( psz_device, O_RDONLY | O_NONBLOCK );
 
     if( i_fd < 0 )
     {