Skip to content

Commit

Permalink
Merge pull request #30 from tshino/default-image-handling
Browse files Browse the repository at this point in the history
Default image handling
  • Loading branch information
tshino authored May 30, 2023
2 parents edc70ed + 25b2244 commit 2e5886d
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 8 deletions.
35 changes: 27 additions & 8 deletions src/softcamcore/DShowSoftcam.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,16 +147,31 @@ AM_MEDIA_TYPE* makeMediaType(int width, int height, float framerate)
return amt;
}

const bool HasDefaultImage = false; //true; // TODO: Check if default image is available or not
const int DefaultImageWidth = 320; // TODO: Read the default image when it's necessary
const int DefaultImageHeight = 240; // TODO: Read the default image when it's necessary
const float DefaultFramerate = 60.0f; // TODO: Read the default image when it's necessary
bool UseDefaultBlankImage = false; // Testing purpose only
int DefaultImageWidth = 0; // Testing purpose only
int DefaultImageHeight = 0; // Testing purpose only
const float DefaultFramerate = 60.0f;

} //namespace

namespace softcam {


void Softcam::enableDefaultBlankImage(int width, int height)
{
UseDefaultBlankImage = true;
DefaultImageWidth = width;
DefaultImageHeight = height;
}

void Softcam::disableDefaultBlankImage()
{
UseDefaultBlankImage = false;
DefaultImageWidth = 0;
DefaultImageHeight = 0;
}


CUnknown * Softcam::CreateInstance(
LPUNKNOWN lpunk,
const GUID& clsid,
Expand All @@ -171,10 +186,14 @@ CUnknown * Softcam::CreateInstance(
Softcam::Softcam(LPUNKNOWN lpunk, const GUID& clsid, HRESULT *phr) :
CSource(NAME("DirectShow Softcam"), lpunk, clsid),
m_frame_buffer(FrameBuffer::open()),
m_valid(m_frame_buffer || HasDefaultImage),
m_width(m_frame_buffer ? m_frame_buffer.width() : HasDefaultImage ? DefaultImageWidth : 0),
m_height(m_frame_buffer ? m_frame_buffer.height() : HasDefaultImage ? DefaultImageHeight : 0),
m_framerate(m_frame_buffer ? m_frame_buffer.framerate() : HasDefaultImage ? DefaultFramerate : 0.0f)
m_default_image(
m_frame_buffer ? DefaultImage{} :
UseDefaultBlankImage ? DefaultImage::makeBlankImage(DefaultImageWidth, DefaultImageHeight) :
DefaultImage{} /* TODO: Read the default image when it's necessary */),
m_valid(m_frame_buffer || m_default_image),
m_width(m_frame_buffer ? m_frame_buffer.width() : m_default_image ? m_default_image.width() : 0),
m_height(m_frame_buffer ? m_frame_buffer.height() : m_default_image ? m_default_image.height() : 0),
m_framerate(m_frame_buffer ? m_frame_buffer.framerate() : m_default_image ? DefaultFramerate : 0.0f)
{
CAutoLock lock(&m_cStateLock);

Expand Down
6 changes: 6 additions & 0 deletions src/softcamcore/DShowSoftcam.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <memory>
#include <baseclasses/streams.h>
#include "FrameBuffer.h"
#include "DefaultImage.h"


namespace softcam {
Expand Down Expand Up @@ -33,8 +34,13 @@ class Softcam : public CSource, public IAMStreamConfig
float framerate() const { return m_framerate; }
void releaseFrameBuffer();

// Testing purpose only
static void enableDefaultBlankImage(int width, int height);
static void disableDefaultBlankImage();

private:
FrameBuffer m_frame_buffer;
DefaultImage m_default_image;
const bool m_valid;
const int m_width;
const int m_height;
Expand Down
82 changes: 82 additions & 0 deletions tests/core_tests/DShowSoftcamTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class Softcam : public ::testing::Test
{
m_softcam->Release();
}
sc::Softcam::disableDefaultBlankImage();
}
};

Expand Down Expand Up @@ -129,6 +130,87 @@ TEST_F(Softcam, AttributesNormal)
EXPECT_EQ( m_softcam->framerate(), 60.0f );
}

TEST_F(Softcam, AttributesNoServerButWithDefaultImage)
{
sc::Softcam::enableDefaultBlankImage(320, 240);

HRESULT hr = 555;
m_softcam = (sc::Softcam*)sc::Softcam::CreateInstance(nullptr, SOME_GUID, &hr);
ASSERT_NE( m_softcam, nullptr );
m_softcam->AddRef();

EXPECT_EQ( m_softcam->getFrameBuffer(), nullptr );
EXPECT_EQ( m_softcam->valid(), true );
EXPECT_EQ( m_softcam->width(), 320 );
EXPECT_EQ( m_softcam->height(), 240 );
EXPECT_EQ( m_softcam->framerate(), 60.0f );
}

TEST_F(Softcam, AttributesWithBothValidServerAndDefaultImage)
{
sc::Softcam::enableDefaultBlankImage(160, 120);
auto fb = createFrameBufer(320, 240, 30);

HRESULT hr = 555;
m_softcam = (sc::Softcam*)sc::Softcam::CreateInstance(nullptr, SOME_GUID, &hr);
ASSERT_NE( m_softcam, nullptr );
m_softcam->AddRef();

EXPECT_NE( m_softcam->getFrameBuffer(), nullptr );
EXPECT_EQ( m_softcam->valid(), true );
EXPECT_EQ( m_softcam->width(), 320 );
EXPECT_EQ( m_softcam->height(), 240 );
EXPECT_EQ( m_softcam->framerate(), 30.0f );
}

TEST_F(Softcam, AttributesSwitchingFromDefaultImageToActualServer)
{
sc::Softcam::enableDefaultBlankImage(320, 240);

HRESULT hr = 555;
m_softcam = (sc::Softcam*)sc::Softcam::CreateInstance(nullptr, SOME_GUID, &hr);
ASSERT_NE( m_softcam, nullptr );
m_softcam->AddRef();

EXPECT_EQ( m_softcam->getFrameBuffer(), nullptr );
EXPECT_EQ( m_softcam->valid(), true );
EXPECT_EQ( m_softcam->width(), 320 );
EXPECT_EQ( m_softcam->height(), 240 );
EXPECT_EQ( m_softcam->framerate(), 60.0f );

auto fb = createFrameBufer(320, 240, 30);

EXPECT_NE( m_softcam->getFrameBuffer(), nullptr );
EXPECT_EQ( m_softcam->valid(), true );
EXPECT_EQ( m_softcam->width(), 320 );
EXPECT_EQ( m_softcam->height(), 240 );
EXPECT_EQ( m_softcam->framerate(), 60.0f );
}

TEST_F(Softcam, AttributesKeepingDefaultImageIfIncompatibleServer)
{
sc::Softcam::enableDefaultBlankImage(320, 240);

HRESULT hr = 555;
m_softcam = (sc::Softcam*)sc::Softcam::CreateInstance(nullptr, SOME_GUID, &hr);
ASSERT_NE( m_softcam, nullptr );
m_softcam->AddRef();

EXPECT_EQ( m_softcam->getFrameBuffer(), nullptr );
EXPECT_EQ( m_softcam->valid(), true );
EXPECT_EQ( m_softcam->width(), 320 );
EXPECT_EQ( m_softcam->height(), 240 );
EXPECT_EQ( m_softcam->framerate(), 60.0f );

auto fb = createFrameBufer(300, 200, 30); // <--

EXPECT_EQ( m_softcam->getFrameBuffer(), nullptr ); // <--
EXPECT_EQ( m_softcam->valid(), true );
EXPECT_EQ( m_softcam->width(), 320 ); // <--
EXPECT_EQ( m_softcam->height(), 240 ); // <--
EXPECT_EQ( m_softcam->framerate(), 60.0f );
}

TEST_F(Softcam, AttributesDeactivatedServer)
{
auto fb = createFrameBufer(320, 240, 60);
Expand Down

0 comments on commit 2e5886d

Please sign in to comment.