Commit 5019665f authored by Ted Meyer's avatar Ted Meyer Committed by Commit Bot

Reland "Call MFShutdown in places where MFStartup is called"

This reverts commit 5be12bf8.

Reason for revert: Fixing the issue, and running on more trybots

Original change's description:
> Revert "Call MFShutdown in places where MFStartup is called"
> 
> This reverts commit abf9a1f5.
> 
> Reason for revert: Broke tests on Win7 dbg, see linked bug
> 
> Original change's description:
> > Call MFShutdown in places where MFStartup is called
> > 
> > Bug: 1012527
> > Fixed: 1012527
> > Change-Id: Id9ef248aadb0aa24704dec973b60ea23c9e1f1ed
> > Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2032332
> > Commit-Queue: Ted Meyer <tmathmeyer@chromium.org>
> > Reviewed-by: Xiaohan Wang <xhwang@chromium.org>
> > Cr-Commit-Position: refs/heads/master@{#738063}
> 
> TBR=xhwang@chromium.org,tmathmeyer@chromium.org
> 
> Change-Id: I267213cc06b42769736c7eb337432ade7953de20
> No-Presubmit: true
> No-Tree-Checks: true
> No-Try: true
> Bug: 1012527
> Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2036057
> Reviewed-by: Marc Treib <treib@chromium.org>
> Commit-Queue: Marc Treib <treib@chromium.org>
> Cr-Commit-Position: refs/heads/master@{#738130}

TBR=xhwang@chromium.org,treib@chromium.org,tmathmeyer@chromium.org

Change-Id: I8f87e933e133d0dcbf9904c002af7738af450f41
No-Presubmit: true
No-Tree-Checks: true
No-Try: true
Bug: 1012527
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2037811Reviewed-by: default avatarXiaohan Wang <xhwang@chromium.org>
Commit-Queue: Ted Meyer <tmathmeyer@chromium.org>
Cr-Commit-Position: refs/heads/master@{#738344}
parent eaac8929
......@@ -25,7 +25,7 @@ class DXGIDeviceScopedHandleTest : public testing::Test {
if (!test_supported_)
return;
ASSERT_TRUE(InitializeMediaFoundation());
ASSERT_NE(nullptr, session_ = InitializeMediaFoundation());
// Get a shared DXGI Device Manager from Media Foundation.
ASSERT_HRESULT_SUCCEEDED(
......@@ -60,6 +60,7 @@ class DXGIDeviceScopedHandleTest : public testing::Test {
}
}
MFSessionLifetime session_;
Microsoft::WRL::ComPtr<IMFDXGIDeviceManager> dxgi_device_man_ = nullptr;
UINT device_reset_token_ = 0;
const bool test_supported_;
......
......@@ -8,13 +8,19 @@
#include "base/logging.h"
#include "base/optional.h"
namespace media {
bool InitializeMediaFoundation() {
static const bool success = MFStartup(MF_VERSION, MFSTARTUP_LITE) == S_OK;
DVLOG_IF(1, !success)
<< "Media Foundation unavailable or it failed to initialize";
return success;
MFSessionLifetime InitializeMediaFoundation() {
if (MFStartup(MF_VERSION, MFSTARTUP_LITE) == S_OK)
return std::make_unique<MFSession>();
DVLOG(1) << "Media Foundation unavailable or it failed to initialize";
return nullptr;
}
MFSession::~MFSession() {
MFShutdown();
}
} // namespace media
......@@ -5,15 +5,25 @@
#ifndef MEDIA_BASE_WIN_MF_INITIALIZER_H_
#define MEDIA_BASE_WIN_MF_INITIALIZER_H_
#include <mfapi.h>
#include "base/logging.h"
#include "media/base/win/mf_initializer_export.h"
namespace media {
// Makes sure MFStartup() is called exactly once. Returns true if Media
// Foundation is available and has been initialized successfully. Note that it
// is expected to return false on an "N" edition of Windows, see
// https://en.wikipedia.org/wiki/Windows_7_editions#Special-purpose_editions.
MF_INITIALIZER_EXPORT bool InitializeMediaFoundation();
// Handy-dandy wrapper struct that kills MediaFoundation on destruction.
struct MF_INITIALIZER_EXPORT MFSession {
~MFSession();
};
using MFSessionLifetime = std::unique_ptr<MFSession>;
// Make sure that MFShutdown is called for each MFStartup that is successful.
// The public documentation stating that it needs to have a corresponding
// shutdown for all startups (even failed ones) is wrong.
MF_INITIALIZER_EXPORT MFSessionLifetime InitializeMediaFoundation()
WARN_UNUSED_RESULT;
} // namespace media
......
......@@ -146,10 +146,8 @@ bool PrepareVideoCaptureAttributesMediaFoundation(
// Once https://bugs.chromium.org/p/chromium/issues/detail?id=791615 is fixed,
// we must make sure that this method succeeds in capture_unittests context
// when MediaFoundation is enabled.
if (!VideoCaptureDeviceFactoryWin::PlatformSupportsMediaFoundation() ||
!InitializeMediaFoundation()) {
if (!VideoCaptureDeviceFactoryWin::PlatformSupportsMediaFoundation())
return false;
}
if (FAILED(MFCreateAttributes(attributes, count)))
return false;
......@@ -382,6 +380,7 @@ VideoCaptureDeviceFactoryWin::VideoCaptureDeviceFactoryWin()
LogVideoCaptureWinBackendUsed(
VideoCaptureWinBackendUsed::kUsingDirectShowAsFallback);
} else if (use_media_foundation_) {
session_ = InitializeMediaFoundation();
LogVideoCaptureWinBackendUsed(
VideoCaptureWinBackendUsed::kUsingMediaFoundationAsDefault);
} else {
......@@ -436,7 +435,8 @@ void VideoCaptureDeviceFactoryWin::GetDeviceDescriptors(
VideoCaptureDeviceDescriptors* device_descriptors) {
DCHECK(thread_checker_.CalledOnValidThread());
if (use_media_foundation_) {
if (use_media_foundation_ && session_) {
DCHECK(PlatformSupportsMediaFoundation());
GetDeviceDescriptorsMediaFoundation(device_descriptors);
AugmentDescriptorListWithDirectShowOnlyDevices(device_descriptors);
} else {
......
......@@ -12,6 +12,7 @@
#include "base/macros.h"
#include "base/threading/thread.h"
#include "media/base/win/mf_initializer.h"
#include "media/capture/video/video_capture_device_factory.h"
namespace media {
......@@ -94,6 +95,7 @@ class CAPTURE_EXPORT VideoCaptureDeviceFactoryWin
VideoCaptureFormats* formats);
bool use_media_foundation_;
MFSessionLifetime session_;
// In production code, when Media Foundation libraries are available,
// |mf_enum_device_sources_func_| points to MFEnumDeviceSources. It enables
// mock of Media Foundation API in unit tests.
......
......@@ -47,7 +47,6 @@
#include "media/base/media_log.h"
#include "media/base/media_switches.h"
#include "media/base/win/mf_helpers.h"
#include "media/base/win/mf_initializer.h"
#include "media/filters/vp9_parser.h"
#include "media/gpu/windows/d3d11_video_device_format_support.h"
#include "media/gpu/windows/dxva_picture_buffer_win.h"
......@@ -629,7 +628,7 @@ bool DXVAVideoDecodeAccelerator::Initialize(const Config& config,
RETURN_ON_FAILURE((state == kUninitialized),
"Initialize: invalid state: " << state, false);
RETURN_ON_FAILURE(InitializeMediaFoundation(),
RETURN_ON_FAILURE(session_ = InitializeMediaFoundation(),
"Could not initialize Media Foundartion", false);
config_ = config;
......
......@@ -31,6 +31,7 @@
#include "base/threading/thread.h"
#include "gpu/config/gpu_preferences.h"
#include "media/base/video_color_space.h"
#include "media/base/win/mf_initializer.h"
#include "media/gpu/gpu_video_decode_accelerator_helpers.h"
#include "media/gpu/media_gpu_export.h"
#include "media/gpu/windows/d3d11_com_defs.h"
......@@ -400,6 +401,9 @@ class MEDIA_GPU_EXPORT DXVAVideoDecodeAccelerator
// To expose client callbacks from VideoDecodeAccelerator.
VideoDecodeAccelerator::Client* client_;
// MediaFoundation session, calls MFShutdown on deletion.
MFSessionLifetime session_;
Microsoft::WRL::ComPtr<IMFTransform> decoder_;
Microsoft::WRL::ComPtr<IDirect3D9Ex> d3d9_;
......
......@@ -359,7 +359,7 @@ bool MediaFoundationVideoEncodeAccelerator::CreateHardwareEncoderMFT() {
}
}
if (!InitializeMediaFoundation())
if (!(session_ = InitializeMediaFoundation()))
return false;
uint32_t flags = MFT_ENUM_FLAG_HARDWARE | MFT_ENUM_FLAG_SORTANDFILTER;
......
......@@ -18,6 +18,7 @@
#include "base/memory/weak_ptr.h"
#include "base/single_thread_task_runner.h"
#include "base/threading/thread.h"
#include "media/base/win/mf_initializer.h"
#include "media/gpu/media_gpu_export.h"
#include "media/video/video_encode_accelerator.h"
......@@ -136,6 +137,9 @@ class MEDIA_GPU_EXPORT MediaFoundationVideoEncodeAccelerator
Microsoft::WRL::ComPtr<IMFSample> input_sample_;
Microsoft::WRL::ComPtr<IMFSample> output_sample_;
// MediaFoundation session.
MFSessionLifetime session_;
// To expose client callbacks from VideoEncodeAccelerator.
// NOTE: all calls to this object *MUST* be executed on
// |main_client_task_runner_|.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment