#include "media/capture/video/video_capture_device.h"
#include <stddef.h>
#include <stdint.h>
#include <memory>
#include <utility>
#include "base/containers/contains.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/memory/ref_counted.h"
#include "base/run_loop.h"
#include "base/task/bind_post_task.h"
#include "base/task/single_thread_task_runner.h"
#include "base/test/bind.h"
#include "base/test/gmock_callback_support.h"
#include "base/test/task_environment.h"
#include "base/test/test_timeouts.h"
#include "base/threading/thread.h"
#include "build/build_config.h"
#include "build/chromeos_buildflags.h"
#include "media/base/video_frame.h"
#include "media/capture/video/create_video_capture_device_factory.h"
#include "media/capture/video/mock_video_capture_device_client.h"
#include "media/capture/video_capture_types.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#if BUILDFLAG(IS_WIN)
#include <mfcaptureengine.h>
#include "base/win/scoped_com_initializer.h"
#include "media/capture/video/win/video_capture_device_factory_win.h"
#include "media/capture/video/win/video_capture_device_mf_win.h"
#endif
#if BUILDFLAG(IS_APPLE)
#include "media/capture/video/apple/video_capture_device_factory_apple.h"
#endif
#if BUILDFLAG(IS_ANDROID)
#include "base/android/build_info.h"
#include "base/android/jni_android.h"
#include "media/capture/video/android/video_capture_device_android.h"
#include "media/capture/video/android/video_capture_device_factory_android.h"
#endif
#if BUILDFLAG(IS_CHROMEOS_ASH)
#include "chromeos/ash/components/mojo_service_manager/connection.h"
#include "media/capture/video/chromeos/camera_buffer_factory.h"
#include "media/capture/video/chromeos/public/cros_features.h"
#include "media/capture/video/chromeos/video_capture_device_chromeos_halv3.h"
#include "media/capture/video/chromeos/video_capture_device_factory_chromeos.h"
#include "media/gpu/test/local_gpu_memory_buffer_manager.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
#endif
#if BUILDFLAG(IS_APPLE)
#define MAYBE_UsingRealWebcam_AllocateBadSize …
#define MAYBE_UsingRealWebcam_CaptureMjpeg …
#define MAYBE_UsingRealWebcam_TakePhoto …
#define MAYBE_UsingRealWebcam_GetPhotoState …
#define MAYBE_UsingRealWebcam_CaptureWithSize …
#define MAYBE_UsingRealWebcam_CheckPhotoCallbackRelease …
#elif BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_WIN) || BUILDFLAG(IS_FUCHSIA)
#define MAYBE_UsingRealWebcam_AllocateBadSize …
#define MAYBE_UsingRealWebcam_CaptureMjpeg …
#define MAYBE_UsingRealWebcam_TakePhoto …
#define MAYBE_UsingRealWebcam_GetPhotoState …
#define MAYBE_UsingRealWebcam_CaptureWithSize …
#define MAYBE_UsingRealWebcam_CheckPhotoCallbackRelease …
#elif BUILDFLAG(IS_ANDROID)
#define MAYBE_UsingRealWebcam_AllocateBadSize …
#define MAYBE_UsingRealWebcam_CaptureMjpeg …
#define MAYBE_UsingRealWebcam_TakePhoto …
#define MAYBE_UsingRealWebcam_GetPhotoState …
#define MAYBE_UsingRealWebcam_CaptureWithSize …
#define MAYBE_UsingRealWebcam_CheckPhotoCallbackRelease …
#elif BUILDFLAG(IS_CHROMEOS_ASH)
#define MAYBE_UsingRealWebcam_AllocateBadSize …
#define MAYBE_UsingRealWebcam_CaptureMjpeg …
#define MAYBE_UsingRealWebcam_TakePhoto …
#define MAYBE_UsingRealWebcam_GetPhotoState …
#define MAYBE_UsingRealWebcam_CaptureWithSize …
#define MAYBE_UsingRealWebcam_CheckPhotoCallbackRelease …
#elif BUILDFLAG(IS_CHROMEOS_LACROS)
#define MAYBE_UsingRealWebcam_AllocateBadSize …
#define MAYBE_UsingRealWebcam_CaptureMjpeg …
#define MAYBE_UsingRealWebcam_TakePhoto …
#define MAYBE_UsingRealWebcam_GetPhotoState …
#define MAYBE_UsingRealWebcam_CaptureWithSize …
#define MAYBE_UsingRealWebcam_CheckPhotoCallbackRelease …
#else
#define MAYBE_UsingRealWebcam_AllocateBadSize …
#define MAYBE_UsingRealWebcam_CaptureMjpeg …
#define MAYBE_UsingRealWebcam_TakePhoto …
#define MAYBE_UsingRealWebcam_GetPhotoState …
#define MAYBE_UsingRealWebcam_CaptureWithSize …
#define MAYBE_UsingRealWebcam_CheckPhotoCallbackRelease …
#endif
#define WRAPPED_TEST_P(test_case_name, test_name) …
RunClosure;
_;
Invoke;
Return;
SaveArg;
WithArgs;
namespace media {
namespace {
void DumpError(media::VideoCaptureError,
const base::Location& location,
const std::string& message) { … }
enum VideoCaptureImplementationTweak { … };
#if BUILDFLAG(IS_WIN)
class MockMFPhotoCallback final : public IMFCaptureEngineOnSampleCallback {
public:
~MockMFPhotoCallback() {}
MOCK_METHOD2(DoQueryInterface, HRESULT(REFIID, void**));
MOCK_METHOD0(DoAddRef, ULONG(void));
MOCK_METHOD0(DoRelease, ULONG(void));
MOCK_METHOD1(DoOnSample, HRESULT(IMFSample*));
IFACEMETHODIMP QueryInterface(REFIID riid, void** object) override {
return DoQueryInterface(riid, object);
}
IFACEMETHODIMP_(ULONG) AddRef() override { return DoAddRef(); }
IFACEMETHODIMP_(ULONG) Release() override { return DoRelease(); }
IFACEMETHODIMP OnSample(IMFSample* sample) override {
return DoOnSample(sample);
}
};
#endif
class MockImageCaptureClient
: public base::RefCountedThreadSafe<MockImageCaptureClient> { … };
constexpr auto kMainThreadType = …
base::test::TaskEnvironment::MainThreadType::UI;
#elif BUILDFLAG(IS_FUCHSIA)
base::test::TaskEnvironment::MainThreadType::IO;
#else
base::test::TaskEnvironment::MainThreadType::DEFAULT;
#endif
}
class VideoCaptureDeviceTest
: public testing::TestWithParam<
std::tuple<gfx::Size, VideoCaptureImplementationTweak>> { … };
#if (BUILDFLAG(IS_WIN) && !defined(NDEBUG))
#define MAYBE_UsingRealWebcam_OpenInvalidDevice …
#else
#define MAYBE_UsingRealWebcam_OpenInvalidDevice …
#endif
WRAPPED_TEST_P(VideoCaptureDeviceTest,
MAYBE_UsingRealWebcam_OpenInvalidDevice) { … }
void VideoCaptureDeviceTest::RunOpenInvalidDeviceTestCase() { … }
TEST(VideoCaptureDeviceDescriptor, RemoveTrailingWhitespaceFromDisplayName) { … }
WRAPPED_TEST_P(VideoCaptureDeviceTest, MAYBE_UsingRealWebcam_CaptureWithSize) { … }
void VideoCaptureDeviceTest::RunCaptureWithSizeTestCase() { … }
const gfx::Size kCaptureSizes[] = …;
const VideoCaptureImplementationTweak kCaptureImplementationTweaks[] = …;
INSTANTIATE_TEST_SUITE_P(…);
WRAPPED_TEST_P(VideoCaptureDeviceTest, MAYBE_UsingRealWebcam_AllocateBadSize) { … }
void VideoCaptureDeviceTest::RunAllocateBadSizeTestCase() { … }
WRAPPED_TEST_P(VideoCaptureDeviceTest,
DISABLED_UsingRealWebcam_ReAllocateCamera) { … }
void VideoCaptureDeviceTest::RunReAllocateCameraTestCase() { … }
WRAPPED_TEST_P(VideoCaptureDeviceTest, MAYBE_UsingRealWebcam_CaptureMjpeg) { … }
void VideoCaptureDeviceTest::RunCaptureMjpegTestCase() { … }
#define MAYBE_UsingRealWebcam_NoCameraSupportsPixelFormatMax …
WRAPPED_TEST_P(VideoCaptureDeviceTest,
MAYBE_UsingRealWebcam_NoCameraSupportsPixelFormatMax) { … }
void VideoCaptureDeviceTest::RunNoCameraSupportsPixelFormatMaxTestCase() { … }
WRAPPED_TEST_P(VideoCaptureDeviceTest, MAYBE_UsingRealWebcam_TakePhoto) { … }
void VideoCaptureDeviceTest::RunTakePhotoTestCase() { … }
WRAPPED_TEST_P(VideoCaptureDeviceTest, MAYBE_UsingRealWebcam_GetPhotoState) { … }
void VideoCaptureDeviceTest::RunGetPhotoStateTestCase() { … }
#if BUILDFLAG(IS_WIN)
WRAPPED_TEST_P(VideoCaptureDeviceTest,
MAYBE_UsingRealWebcam_CheckPhotoCallbackRelease) {
if (!UseWinMediaFoundation())
return;
auto device_info = GetFirstDeviceSupportingPixelFormat(PIXEL_FORMAT_MJPEG);
ASSERT_TRUE(device_info);
EXPECT_CALL(*video_capture_client_, OnError(_, _, _)).Times(0);
EXPECT_CALL(*video_capture_client_, OnStarted());
VideoCaptureErrorOrDevice device_status =
video_capture_device_factory_->CreateDevice(device_info->descriptor);
ASSERT_TRUE(device_status.ok());
std::unique_ptr<VideoCaptureDevice> device(device_status.ReleaseDevice());
VideoCaptureParams capture_params;
capture_params.requested_format.frame_size.SetSize(320, 240);
capture_params.requested_format.frame_rate = 30;
capture_params.requested_format.pixel_format = PIXEL_FORMAT_MJPEG;
device->AllocateAndStart(capture_params, std::move(video_capture_client_));
if (!static_cast<VideoCaptureDeviceMFWin*>(device.get())
->get_use_photo_stream_to_take_photo_for_testing()) {
DVLOG(1) << "The device is not using the MediaFoundation photo callback. "
"Exiting test.";
device->StopAndDeAllocate();
return;
}
MockMFPhotoCallback* callback = new MockMFPhotoCallback();
EXPECT_CALL(*callback, DoQueryInterface(_, _)).WillRepeatedly(Return(S_OK));
EXPECT_CALL(*callback, DoAddRef()).WillOnce(Return(1U));
EXPECT_CALL(*callback, DoRelease()).WillOnce(Return(1U));
EXPECT_CALL(*callback, DoOnSample(_)).WillOnce(Return(S_OK));
static_cast<VideoCaptureDeviceMFWin*>(device.get())
->set_create_mf_photo_callback_for_testing(base::BindRepeating(
&VideoCaptureDeviceTest::CreateMockPhotoCallback,
base::Unretained(this), base::Unretained(callback)));
VideoCaptureDevice::TakePhotoCallback scoped_callback = base::BindOnce(
&MockImageCaptureClient::DoOnPhotoTaken, image_capture_client_);
base::RunLoop run_loop(base::RunLoop::Type::kNestableTasksAllowed);
base::RepeatingClosure quit_closure =
base::BindPostTaskToCurrentDefault(run_loop.QuitClosure());
EXPECT_CALL(*image_capture_client_.get(), OnCorrectPhotoTaken())
.WillOnce(RunClosure(quit_closure));
device->TakePhoto(std::move(scoped_callback));
run_loop.Run();
device->StopAndDeAllocate();
}
#endif
}