dshowaudiosrc: fix audiocapture producing silence
authorAndoni Morales Alastruey <ylatuya@gmail.com>
Tue, 11 Jun 2013 09:54:41 +0000 (11:54 +0200)
committerAndoni Morales Alastruey <ylatuya@gmail.com>
Tue, 18 Jun 2013 10:04:58 +0000 (12:04 +0200)
Configure the capture latency using the IAMBufferNegotiation
interface and try to respect the configured latency-time and buffer-time

sys/dshowsrcwrapper/gstdshow.cpp
sys/dshowsrcwrapper/gstdshow.h
sys/dshowsrcwrapper/gstdshowaudiosrc.cpp

index 25a3f5d..4f59105 100644 (file)
@@ -496,3 +496,21 @@ gst_dshow_new_video_caps (GstVideoFormat video_format, const gchar * name,
 
   return video_caps;
 }
+
+bool gst_dshow_configure_latency (IPin *pCapturePin, guint bufSizeMS)
+{
+  HRESULT hr;
+  ALLOCATOR_PROPERTIES alloc_prop;
+  IAMBufferNegotiation * pNeg = NULL;
+  hr = pCapturePin->QueryInterface(IID_IAMBufferNegotiation, (void **)&pNeg);
+
+  if(!SUCCEEDED (hr))
+    return FALSE;
+
+  alloc_prop.cbAlign = -1;  // -1 means no preference.
+  alloc_prop.cbBuffer = bufSizeMS;
+  alloc_prop.cbPrefix = -1;
+  alloc_prop.cBuffers = -1;
+  hr = pNeg->SuggestAllocatorProperties (&alloc_prop);
+  return SUCCEEDED (hr);
+}
index a3e0c1a..92220b6 100644 (file)
@@ -96,4 +96,7 @@ GstVideoFormat gst_dshow_guid_to_gst_video_format (AM_MEDIA_TYPE *mediatype);
 GstCaps *gst_dshow_new_video_caps (GstVideoFormat video_format,
     const gchar * name, GstCapturePinMediaType * pin_mediatype);
 
+/* configure the latency of the capture source */
+bool gst_dshow_configure_latency (IPin *pCapturePin, guint bufSizeMS);
+
 #endif /* _GSTDSHOW_ */
index 49b5b04..1a816aa 100644 (file)
@@ -626,6 +626,18 @@ gst_dshowaudiosrc_prepare (GstAudioSrc * asrc, GstRingBufferSpec * spec)
           goto error;
         }
 
+        spec->segsize = (gint) (spec->bytes_per_sample * spec->rate * spec->latency_time /
+            GST_MSECOND);
+        spec->segtotal = (gint) ((gfloat) spec->buffer_time /
+            (gfloat) spec->latency_time + 0.5);
+        if (!gst_dshow_configure_latency (pin_mediatype->capture_pin,
+            spec->segsize))
+        {
+          GST_WARNING ("Could not change capture latency");
+          spec->segsize = spec->rate * spec->channels;
+          spec->segtotal = 2;
+        };
+        GST_INFO ("Configuring with segsize:%d segtotal:%d", spec->segsize, spec->segtotal);
         hres = src->filter_graph->ConnectDirect (pin_mediatype->capture_pin,
             input_pin, NULL);
         input_pin->Release ();
@@ -637,8 +649,6 @@ gst_dshowaudiosrc_prepare (GstAudioSrc * asrc, GstRingBufferSpec * spec)
           goto error;
         }
 
-        spec->segsize = spec->rate * spec->channels;
-        spec->segtotal = 1;
       }
     }
   }
@@ -720,7 +730,8 @@ gst_dshowaudiosrc_read (GstAudioSrc * asrc, gpointer data, guint length)
       g_mutex_unlock (src->gbarray_lock);
     } else {
       if (src->is_running) {
-        Sleep (100);
+        Sleep (GST_BASE_AUDIO_SRC(src)->ringbuffer->spec.latency_time /
+            GST_MSECOND / 10);
         goto test;
       }
     }