|
From: libvidcap c. m. <lib...@li...> - 2007-09-20 17:51:52
|
Revision: 26
http://libvidcap.svn.sourceforge.net/libvidcap/?rev=26&view=rev
Author: bcholew
Date: 2007-09-20 10:51:50 -0700 (Thu, 20 Sep 2007)
Log Message:
-----------
ove AddFilter() of pSource_ and pNullRenderer to construction time. This simplifies the code, and has the added benefit of reducing the memory leak that exists when using the MS LifeCam NX-6000 in scenarios that re-bind a source. Move setting of pStreamConfig_'s format from bind time (in main thread) to capture start time (in source thread). This keeps the main thread running smoothly if/when this operation blocks for a while. Return failure at bind time if we fail to remove the sample grabber from the filter graph. Rename a flag.
Modified Paths:
--------------
trunk/src/directshow/DirectShowSource.cpp
trunk/src/directshow/DirectShowSource.h
Modified: trunk/src/directshow/DirectShowSource.cpp
===================================================================
--- trunk/src/directshow/DirectShowSource.cpp 2007-09-18 18:38:47 UTC (rev 25)
+++ trunk/src/directshow/DirectShowSource.cpp 2007-09-20 17:51:50 UTC (rev 26)
@@ -51,7 +51,7 @@
pNullRenderer_(0),
pMediaControlIF_(0),
nativeMediaType_(0),
- captureIsSetup_(false),
+ graphIsSetup_(false),
eventInitDone_(0),
eventStart_(0),
eventStop_(0),
@@ -287,6 +287,20 @@
return -1;
}
+ hr = pFilterGraph_->AddFilter(pSource_, L"Source Device");
+ if ( FAILED(hr) )
+ {
+ log_error("failed to add source (%d)\n", hr);
+ return -1;
+ }
+
+ hr = pFilterGraph_->AddFilter(pNullRenderer_, L"NullRenderer");
+ if ( FAILED(hr) )
+ {
+ log_error("failed to add null renderer (%d)\n", hr);
+ return -1;
+ }
+
return 0;
}
@@ -466,47 +480,45 @@
return 0;
}
-void
+int
DirectShowSource::resetCapGraphFoo()
{
- if ( captureIsSetup_ )
+ if ( graphIsSetup_ )
{
- // some or all of this appears to be necessary to allow
- // subsequent captures with a different media type
- HRESULT hr = pFilterGraph_->RemoveFilter(pSource_);
- if ( FAILED(hr) )
- log_error("failed to remove source (%d)\n", hr);
+ graphIsSetup_ = false;
- hr = pFilterGraph_->RemoveFilter(pSampleGrabber_);
+ // necessary to allow subsequent calls to RenderStream()
+ // (like after rebinding) to succeed
+ HRESULT hr = pFilterGraph_->RemoveFilter(pSampleGrabber_);
if ( FAILED(hr) )
- log_error("failed to remove Sample Grabber (%d)\n",
- hr);
-
- hr = pFilterGraph_->RemoveFilter(pNullRenderer_);
- if ( FAILED(hr) )
- log_error("failed to remove null renderer (%d)\n", hr);
-
- captureIsSetup_ = false;
+ {
+ log_error("failed to remove Sample Grabber (%d)\n", hr);
+ return 1;
+ }
}
+
+ return 0;
}
int
DirectShowSource::setupCapGraphFoo()
{
- if ( !captureIsSetup_ )
+ if ( !graphIsSetup_ )
{
- // Set sample grabber's media type to match that of the source
- HRESULT hr = pSampleGrabberIF_->SetMediaType(nativeMediaType_);
+ // set the stream's media type
+ HRESULT hr = pStreamConfig_->SetFormat(nativeMediaType_);
if ( FAILED(hr) )
{
- log_error("failed to set grabber media type (%d)\n", hr);
+ log_error("failed setting stream format (%d)\n", hr);
+
return -1;
}
- hr = pFilterGraph_->AddFilter(pSource_, L"Source Device");
+ // Set sample grabber's media type
+ hr = pSampleGrabberIF_->SetMediaType(nativeMediaType_);
if ( FAILED(hr) )
{
- log_error("failed to add source (%d)\n", hr);
+ log_error("failed to set grabber media type (%d)\n", hr);
return -1;
}
@@ -517,13 +529,6 @@
return -1;
}
- hr = pFilterGraph_->AddFilter(pNullRenderer_, L"NullRenderer");
- if ( FAILED(hr) )
- {
- log_error("failed to add null renderer (%d)\n", hr);
- return -1;
- }
-
hr = pCapGraphBuilder_->RenderStream(&PIN_CATEGORY_CAPTURE,
&MEDIATYPE_Video,
pSource_,
@@ -536,7 +541,7 @@
return -1;
}
- captureIsSetup_ = true;
+ graphIsSetup_ = true;
}
return 0;
@@ -577,7 +582,6 @@
if ( !setupCapGraphFoo() )
{
HRESULT hr = pMediaControlIF_->Run();
-
if ( SUCCEEDED(hr) )
return;
else
@@ -593,7 +597,7 @@
int
DirectShowSource::stop()
{
- // signal to source thread to stop capturing
+ // signal to source thread to stop capturing
if ( !SetEvent(eventStop_) )
{
log_error("failed to signal source to stop (%d)\n",
@@ -607,7 +611,7 @@
void
DirectShowSource::doStop()
{
- if ( captureIsSetup_ )
+ if ( graphIsSetup_ )
{
HRESULT hr = pMediaControlIF_->Stop();
if ( FAILED(hr) )
@@ -655,21 +659,7 @@
vih->bmiHeader.biWidth = fmtNative.width;
vih->bmiHeader.biHeight = fmtNative.height;
- resetCapGraphFoo();
-
- // set the stream's media type
- HRESULT hr = pStreamConfig_->SetFormat(nativeMediaType_);
- if ( FAILED(hr) )
- {
- log_error("failed setting stream format (%d)\n", hr);
-
- freeMediaType(*nativeMediaType_);
- nativeMediaType_ = 0;
-
- return 1;
- }
-
- return 0;
+ return resetCapGraphFoo();
}
bool
Modified: trunk/src/directshow/DirectShowSource.h
===================================================================
--- trunk/src/directshow/DirectShowSource.h 2007-09-18 18:38:47 UTC (rev 25)
+++ trunk/src/directshow/DirectShowSource.h 2007-09-20 17:51:50 UTC (rev 26)
@@ -62,7 +62,7 @@
int createCapGraphFoo();
void destroyCapGraphFoo();
int setupCapGraphFoo();
- void resetCapGraphFoo();
+ int resetCapGraphFoo();
bool checkFormat(const vidcap_fmt_info * fmtNominal,
vidcap_fmt_info * fmtNative,
int formatNum,
@@ -104,7 +104,7 @@
IBaseFilter * pNullRenderer_;
IMediaControl * pMediaControlIF_;
AM_MEDIA_TYPE *nativeMediaType_;
- bool captureIsSetup_;
+ bool graphIsSetup_;
HANDLE eventInitDone_;
HANDLE eventStart_;
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|