Skip to content

Commit

Permalink
Cherry pick PR #4137: [XB1] Expand hardware video decoder logs
Browse files Browse the repository at this point in the history
Refer to the original PR: #4137

b/329326128
  • Loading branch information
osagie98 committed Sep 26, 2024
1 parent 7adb836 commit 455b7b1
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 14 deletions.
7 changes: 7 additions & 0 deletions starboard/shared/win32/media_transform.cc
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ MediaTransform::MediaTransform(CLSID clsid)
if (FAILED(hr) || !transform_) {
transform_ = nullptr;
state_ = kDrained;
transform_create_error_message_ =
"Could not create decoder transform. " +
::starboard::shared::win32::HResultToString(hr);
}
}

Expand Down Expand Up @@ -309,6 +312,10 @@ HRESULT MediaTransform::SendMessage(MFT_MESSAGE_TYPE msg,
return transform_->ProcessMessage(msg, data);
}

std::string MediaTransform::GetTransformCreateError() const {
return transform_create_error_message_;
}

void MediaTransform::Reset() {
if (stream_begun_) {
SendMessage(MFT_MESSAGE_COMMAND_FLUSH);
Expand Down
4 changes: 4 additions & 0 deletions starboard/shared/win32/media_transform.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <mfidl.h>
#include <wrl\client.h>

#include <string>
#include <vector>

#include "starboard/media.h"
Expand Down Expand Up @@ -84,6 +85,8 @@ class MediaTransform {

HRESULT SendMessage(MFT_MESSAGE_TYPE msg, ULONG_PTR data = 0);

std::string GetTransformCreateError() const;

// Reset the media transform to its original state.
void Reset();

Expand All @@ -106,6 +109,7 @@ class MediaTransform {
bool stream_begun_;
bool discontinuity_;
bool throttle_inputs_;
std::string transform_create_error_message_;
};

} // namespace win32
Expand Down
43 changes: 30 additions & 13 deletions starboard/shared/win32/video_decoder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -101,18 +101,22 @@ std::unique_ptr<MediaTransform> CreateVideoTransform(
const GUID& decoder_guid,
const GUID& input_guid,
const GUID& output_guid,
const IMFDXGIDeviceManager* device_manager) {
const IMFDXGIDeviceManager* device_manager,
std::string* error_message) {
SB_DCHECK(error_message);
std::unique_ptr<MediaTransform> media_transform(
new MediaTransform(decoder_guid));
if (!media_transform->HasValidTransform()) {
// Decoder Transform setup failed
*error_message = media_transform->GetTransformCreateError();
return std::unique_ptr<MediaTransform>();
}
media_transform->EnableInputThrottle(true);

ComPtr<IMFAttributes> attributes = media_transform->GetAttributes();
if (!attributes) {
// Decoder Transform setup failed
*error_message = "Invalid MediaTransform attributes";
return std::unique_ptr<MediaTransform>();
}

Expand All @@ -128,6 +132,8 @@ std::unique_ptr<MediaTransform> CreateVideoTransform(
hr = attributes->SetUINT32(CODECAPI_AVDecVideoAcceleration_H264, FALSE);
if (FAILED(hr)) {
SB_LOG(WARNING) << "Unable to disable DXVA.";
*error_message = "Unable to disable DXVA. " +
::starboard::shared::win32::HResultToString(hr);
return std::unique_ptr<MediaTransform>();
}
} else {
Expand All @@ -143,11 +149,13 @@ std::unique_ptr<MediaTransform> CreateVideoTransform(
// Tell the decoder to allocate resources for the maximum resolution in
// order to minimize glitching on resolution changes.
if (FAILED(attributes->SetUINT32(MF_MT_DECODER_USE_MAX_RESOLUTION, 1))) {
*error_message = "Cannot allocate resources for maximum resolution.";
return std::unique_ptr<MediaTransform>();
}

ComPtr<IMFMediaType> input_type;
if (FAILED(MFCreateMediaType(&input_type)) || !input_type) {
*error_message = "Invalid IMFMediaType.";
return std::unique_ptr<MediaTransform>();
}
CheckResult(input_type->SetGUID(MF_MT_MAJOR_TYPE, MFMediaType_Video));
Expand Down Expand Up @@ -278,11 +286,13 @@ void VideoDecoder::Initialize(const DecoderStatusCB& decoder_status_cb,
SB_DCHECK(error_cb);
decoder_status_cb_ = decoder_status_cb;
error_cb_ = error_cb;
std::string error_message;
if (video_device_) {
InitializeCodec();
InitializeCodec(&error_message);
}
if (!decoder_) {
error_cb_(kSbPlayerErrorDecode, "Cannot initialize codec.");
error_cb_(kSbPlayerErrorDecode,
"Cannot initialize codec. Error: " + error_message);
}
}

Expand Down Expand Up @@ -425,43 +435,50 @@ SbDecodeTarget VideoDecoder::CreateDecodeTarget() {
return decode_target;
}

void VideoDecoder::InitializeCodec() {
void VideoDecoder::InitializeCodec(std::string* error_message) {
SB_DCHECK(error_message);
std::unique_ptr<MediaTransform> media_transform;

// If this is updated then media_is_video_supported.cc also needs to be
// updated.
switch (video_codec_) {
case kSbMediaVideoCodecH264: {
media_transform =
CreateVideoTransform(CLSID_MSH264DecoderMFT, MFVideoFormat_H264,
MFVideoFormat_NV12, device_manager_.Get());
media_transform = CreateVideoTransform(
CLSID_MSH264DecoderMFT, MFVideoFormat_H264, MFVideoFormat_NV12,
device_manager_.Get(), error_message);
priming_output_count_ = 0;
if (!media_transform) {
SB_LOG(WARNING) << "H264 hardware decoder creation failed.";
*error_message =
"H264 hardware decoder creation failed. " + *error_message;
return;
}
break;
}
case kSbMediaVideoCodecVp9: {
if (IsHardwareVp9DecoderSupported()) {
media_transform =
CreateVideoTransform(CLSID_MSVPxDecoder, MFVideoFormat_VP90,
MFVideoFormat_NV12, device_manager_.Get());
media_transform = CreateVideoTransform(
CLSID_MSVPxDecoder, MFVideoFormat_VP90, MFVideoFormat_NV12,
device_manager_.Get(), error_message);
priming_output_count_ = kVp9PrimingFrameCount;
}
if (!media_transform) {
SB_LOG(WARNING) << "VP9 hardware decoder creation failed.";
*error_message =
"VP9 hardware decoder creation failed. " + *error_message;
return;
}
break;
}
case kSbMediaVideoCodecAv1: {
media_transform =
CreateVideoTransform(MFVideoFormat_AV1, MFVideoFormat_AV1,
MFVideoFormat_NV12, device_manager_.Get());
media_transform = CreateVideoTransform(
MFVideoFormat_AV1, MFVideoFormat_AV1, MFVideoFormat_NV12,
device_manager_.Get(), error_message);
priming_output_count_ = 0;
if (!media_transform) {
SB_LOG(WARNING) << "AV1 hardware decoder creation failed.";
*error_message =
"AV1 hardware decoder creation failed. " + *error_message;
return;
}
break;
Expand Down
3 changes: 2 additions & 1 deletion starboard/shared/win32/video_decoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <atomic>
#include <list>
#include <memory>
#include <string>

#include "starboard/common/mutex.h"
#include "starboard/common/ref_counted.h"
Expand Down Expand Up @@ -100,7 +101,7 @@ class VideoDecoder
return true;
}

void InitializeCodec();
void InitializeCodec(std::string* error_message);
void ShutdownCodec();
static void ReleaseDecodeTargets(void* context);

Expand Down

0 comments on commit 455b7b1

Please sign in to comment.