Index: content/common/gpu/media/dxva_video_decode_accelerator.cc |
diff --git a/content/common/gpu/media/dxva_video_decode_accelerator.cc b/content/common/gpu/media/dxva_video_decode_accelerator.cc |
index 2fc53e7188acfd223d8cb847dd96e2b866adc4c9..e78e63563e0300b61e0b9130e9c458192e1a5ac1 100644 |
--- a/content/common/gpu/media/dxva_video_decode_accelerator.cc |
+++ b/content/common/gpu/media/dxva_video_decode_accelerator.cc |
@@ -488,7 +488,8 @@ DXVAVideoDecodeAccelerator::DXVAVideoDecodeAccelerator( |
inputs_before_decode_(0), |
make_context_current_(make_context_current), |
weak_this_factory_(this), |
- codec_(media::kUnknownVideoCodec) { |
+ codec_(media::kUnknownVideoCodec), |
+ decoder_thread_("DXVAVideoDecoderThread") { |
memset(&input_stream_info_, 0, sizeof(input_stream_info_)); |
memset(&output_stream_info_, 0, sizeof(output_stream_info_)); |
} |
@@ -511,8 +512,8 @@ bool DXVAVideoDecodeAccelerator::Initialize(media::VideoCodecProfile profile, |
HMODULE mfplat_dll = ::LoadLibrary(L"MFPlat.dll"); |
RETURN_ON_FAILURE(mfplat_dll, "MFPlat.dll is required for decoding", false); |
- // TODO(ananta) |
- // H264PROFILE_HIGH video decoding is janky at times. Needs more |
+ // TODO(ananta) |
+ // H264PROFILE_HIGH video decoding is janky at times. Needs more |
// investigation. http://crbug.com/426707 |
if (profile != media::H264PROFILE_BASELINE && |
profile != media::H264PROFILE_MAIN && |
@@ -528,8 +529,9 @@ bool DXVAVideoDecodeAccelerator::Initialize(media::VideoCodecProfile profile, |
PLATFORM_FAILURE, |
false); |
- RETURN_AND_NOTIFY_ON_FAILURE((state_ == kUninitialized), |
- "Initialize: invalid state: " << state_, ILLEGAL_STATE, false); |
+ State state = GetState(); |
+ RETURN_AND_NOTIFY_ON_FAILURE((state == kUninitialized), |
+ "Initialize: invalid state: " << state, ILLEGAL_STATE, false); |
HRESULT hr = MFStartup(MF_VERSION, MFSTARTUP_FULL); |
RETURN_AND_NOTIFY_ON_HR_FAILURE(hr, "MFStartup failed.", PLATFORM_FAILURE, |
@@ -556,7 +558,13 @@ bool DXVAVideoDecodeAccelerator::Initialize(media::VideoCodecProfile profile, |
"Send MFT_MESSAGE_NOTIFY_START_OF_STREAM notification failed", |
PLATFORM_FAILURE, false); |
- state_ = kNormal; |
+ SetState(kNormal); |
+ |
+ main_thread_task_runner_ = base::MessageLoop::current()->task_runner(); |
+ |
+ decoder_thread_.init_com_with_mta(false); |
+ decoder_thread_.Start(); |
+ decoder_thread_task_runner_ = decoder_thread_.task_runner(); |
return true; |
} |
@@ -564,9 +572,10 @@ void DXVAVideoDecodeAccelerator::Decode( |
const media::BitstreamBuffer& bitstream_buffer) { |
DCHECK(CalledOnValidThread()); |
- RETURN_AND_NOTIFY_ON_FAILURE((state_ == kNormal || state_ == kStopped || |
- state_ == kFlushing), |
- "Invalid state: " << state_, ILLEGAL_STATE,); |
+ State state = GetState(); |
+ RETURN_AND_NOTIFY_ON_FAILURE((state == kNormal || state == kStopped || |
+ state == kFlushing), |
+ "Invalid state: " << state, ILLEGAL_STATE,); |
base::win::ScopedComPtr<IMFSample> sample; |
sample.Attach(CreateSampleFromInputBuffer(bitstream_buffer, |
@@ -578,15 +587,18 @@ void DXVAVideoDecodeAccelerator::Decode( |
RETURN_AND_NOTIFY_ON_HR_FAILURE(sample->SetSampleTime(bitstream_buffer.id()), |
"Failed to associate input buffer id with sample", PLATFORM_FAILURE,); |
- DecodeInternal(sample); |
+ decoder_thread_task_runner_->PostTask(FROM_HERE, |
+ base::Bind(&DXVAVideoDecodeAccelerator::DecodeInternal, |
+ base::Unretained(this), sample)); |
} |
void DXVAVideoDecodeAccelerator::AssignPictureBuffers( |
const std::vector<media::PictureBuffer>& buffers) { |
DCHECK(CalledOnValidThread()); |
- RETURN_AND_NOTIFY_ON_FAILURE((state_ != kUninitialized), |
- "Invalid state: " << state_, ILLEGAL_STATE,); |
+ State state = GetState(); |
+ RETURN_AND_NOTIFY_ON_FAILURE((state != kUninitialized), |
+ "Invalid state: " << state, ILLEGAL_STATE,); |
RETURN_AND_NOTIFY_ON_FAILURE((kNumPictureBuffers == buffers.size()), |
"Failed to provide requested picture buffers. (Got " << buffers.size() << |
", requested " << kNumPictureBuffers << ")", INVALID_ARGUMENT,); |
@@ -605,7 +617,8 @@ void DXVAVideoDecodeAccelerator::AssignPictureBuffers( |
DCHECK(inserted); |
} |
ProcessPendingSamples(); |
- if (state_ == kFlushing && pending_output_samples_.empty()) |
+ |
+ if (state == kFlushing && pending_output_samples_.empty()) |
FlushInternal(); |
} |
@@ -613,8 +626,9 @@ void DXVAVideoDecodeAccelerator::ReusePictureBuffer( |
int32 picture_buffer_id) { |
DCHECK(CalledOnValidThread()); |
- RETURN_AND_NOTIFY_ON_FAILURE((state_ != kUninitialized), |
- "Invalid state: " << state_, ILLEGAL_STATE,); |
+ State state = GetState(); |
+ RETURN_AND_NOTIFY_ON_FAILURE((state != kUninitialized), |
+ "Invalid state: " << state, ILLEGAL_STATE,); |
if (output_picture_buffers_.empty() && stale_output_picture_buffers_.empty()) |
return; |
@@ -639,7 +653,7 @@ void DXVAVideoDecodeAccelerator::ReusePictureBuffer( |
it->second->ReusePictureBuffer(); |
ProcessPendingSamples(); |
- if (state_ == kFlushing && pending_output_samples_.empty()) |
+ if (state == kFlushing && pending_output_samples_.empty()) |
FlushInternal(); |
} |
@@ -648,10 +662,11 @@ void DXVAVideoDecodeAccelerator::Flush() { |
DVLOG(1) << "DXVAVideoDecodeAccelerator::Flush"; |
- RETURN_AND_NOTIFY_ON_FAILURE((state_ == kNormal || state_ == kStopped), |
- "Unexpected decoder state: " << state_, ILLEGAL_STATE,); |
+ State state = GetState(); |
+ RETURN_AND_NOTIFY_ON_FAILURE((state == kNormal || state == kStopped), |
+ "Unexpected decoder state: " << state, ILLEGAL_STATE,); |
- state_ = kFlushing; |
+ SetState(kFlushing); |
RETURN_AND_NOTIFY_ON_FAILURE(SendMFTMessage(MFT_MESSAGE_COMMAND_DRAIN, 0), |
"Failed to send drain message", PLATFORM_FAILURE,); |
@@ -663,28 +678,18 @@ void DXVAVideoDecodeAccelerator::Flush() { |
} |
void DXVAVideoDecodeAccelerator::Reset() { |
- DCHECK(CalledOnValidThread()); |
- |
DVLOG(1) << "DXVAVideoDecodeAccelerator::Reset"; |
- RETURN_AND_NOTIFY_ON_FAILURE((state_ == kNormal || state_ == kStopped), |
- "Reset: invalid state: " << state_, ILLEGAL_STATE,); |
- |
- state_ = kResetting; |
- |
- pending_output_samples_.clear(); |
- |
- NotifyInputBuffersDropped(); |
- |
- RETURN_AND_NOTIFY_ON_FAILURE(SendMFTMessage(MFT_MESSAGE_COMMAND_FLUSH, 0), |
- "Reset: Failed to send message.", PLATFORM_FAILURE,); |
- |
- base::MessageLoop::current()->PostTask( |
- FROM_HERE, |
- base::Bind(&DXVAVideoDecodeAccelerator::NotifyResetDone, |
- weak_this_factory_.GetWeakPtr())); |
+ State state = GetState(); |
+ RETURN_AND_NOTIFY_ON_FAILURE((state == kNormal || state == kStopped), |
+ "Reset: invalid state: " << state, ILLEGAL_STATE,); |
- state_ = DXVAVideoDecodeAccelerator::kNormal; |
+ if (decoder_thread_.thread_id() != ::GetCurrentThreadId()) { |
+ decoder_thread_task_runner_->PostTask(FROM_HERE, |
+ base::Bind(&DXVAVideoDecodeAccelerator::ResetHelper, |
+ base::Unretained(this))); |
+ return; |
+ } |
} |
void DXVAVideoDecodeAccelerator::Destroy() { |
@@ -931,9 +936,10 @@ bool DXVAVideoDecodeAccelerator::GetStreamsInfoAndBufferReqs() { |
void DXVAVideoDecodeAccelerator::DoDecode() { |
// This function is also called from FlushInternal in a loop which could |
// result in the state transitioning to kStopped due to no decoded output. |
+ State state = GetState(); |
RETURN_AND_NOTIFY_ON_FAILURE( |
- (state_ == kNormal || state_ == kFlushing || |
- state_ == kStopped || state_ == kFlushingPendingInputBuffers), |
+ (state == kNormal || state == kFlushing || |
+ state == kStopped || state == kFlushingPendingInputBuffers), |
"DoDecode: not in normal/flushing/stopped state", ILLEGAL_STATE,); |
MFT_OUTPUT_DATA_BUFFER output_data_buffer = {0}; |
@@ -956,7 +962,7 @@ void DXVAVideoDecodeAccelerator::DoDecode() { |
// Decoder didn't let us set NV12 output format. Not sure as to why |
// this can happen. Give up in disgust. |
NOTREACHED() << "Failed to set decoder output media type to NV12"; |
- state_ = kStopped; |
+ SetState(kStopped); |
} else { |
DVLOG(1) << "Received output format change from the decoder." |
" Recursively invoking DoDecode"; |
@@ -965,8 +971,8 @@ void DXVAVideoDecodeAccelerator::DoDecode() { |
return; |
} else if (hr == MF_E_TRANSFORM_NEED_MORE_INPUT) { |
// No more output from the decoder. Stop playback. |
- if (state_ != kFlushingPendingInputBuffers) |
- state_ = kStopped; |
+ if (state != kFlushingPendingInputBuffers) |
+ SetState(kStopped); |
return; |
} else { |
NOTREACHED() << "Unhandled error in DoDecode()"; |
@@ -1008,7 +1014,10 @@ bool DXVAVideoDecodeAccelerator::ProcessOutputSample(IMFSample* sample) { |
// If we have available picture buffers to copy the output data then use the |
// first one and then flag it as not being available for use. |
if (output_picture_buffers_.size()) { |
- ProcessPendingSamples(); |
+ main_thread_task_runner_->PostTask( |
+ FROM_HERE, |
+ base::Bind(&DXVAVideoDecodeAccelerator::ProcessPendingSamples, |
+ weak_this_factory_.GetWeakPtr())); |
return true; |
} |
if (pictures_requested_) { |
@@ -1024,7 +1033,7 @@ bool DXVAVideoDecodeAccelerator::ProcessOutputSample(IMFSample* sample) { |
RETURN_ON_HR_FAILURE(hr, "Failed to get surface description", false); |
// Go ahead and request picture buffers. |
- base::MessageLoop::current()->PostTask( |
+ main_thread_task_runner_->PostTask( |
FROM_HERE, |
base::Bind(&DXVAVideoDecodeAccelerator::RequestPictureBuffers, |
weak_this_factory_.GetWeakPtr(), |
@@ -1082,7 +1091,7 @@ void DXVAVideoDecodeAccelerator::ProcessPendingSamples() { |
media::Picture output_picture(index->second->id(), |
sample_info.input_buffer_id, |
gfx::Rect(index->second->size())); |
- base::MessageLoop::current()->PostTask( |
+ main_thread_task_runner_->PostTask( |
FROM_HERE, |
base::Bind(&DXVAVideoDecodeAccelerator::NotifyPictureReady, |
weak_this_factory_.GetWeakPtr(), |
@@ -1092,13 +1101,10 @@ void DXVAVideoDecodeAccelerator::ProcessPendingSamples() { |
pending_output_samples_.pop_front(); |
} |
} |
- |
- if (!pending_input_buffers_.empty() && pending_output_samples_.empty()) { |
- base::MessageLoop::current()->PostTask( |
- FROM_HERE, |
- base::Bind(&DXVAVideoDecodeAccelerator::DecodePendingInputBuffers, |
- weak_this_factory_.GetWeakPtr())); |
- } |
+ decoder_thread_task_runner_->PostTask( |
+ FROM_HERE, |
+ base::Bind(&DXVAVideoDecodeAccelerator::DecodePendingInputBuffers, |
+ base::Unretained(this))); |
} |
void DXVAVideoDecodeAccelerator::StopOnError( |
@@ -1109,14 +1115,15 @@ void DXVAVideoDecodeAccelerator::StopOnError( |
client_->NotifyError(error); |
client_ = NULL; |
- if (state_ != kUninitialized) { |
+ if (GetState() != kUninitialized) { |
Invalidate(); |
} |
} |
void DXVAVideoDecodeAccelerator::Invalidate() { |
- if (state_ == kUninitialized) |
+ if (GetState() == kUninitialized) |
return; |
+ decoder_thread_.Stop(); |
weak_this_factory_.InvalidateWeakPtrs(); |
output_picture_buffers_.clear(); |
stale_output_picture_buffers_.clear(); |
@@ -1124,7 +1131,7 @@ void DXVAVideoDecodeAccelerator::Invalidate() { |
pending_input_buffers_.clear(); |
decoder_.Release(); |
MFShutdown(); |
- state_ = kUninitialized; |
+ SetState(kUninitialized); |
} |
void DXVAVideoDecodeAccelerator::NotifyInputBufferRead(int input_buffer_id) { |
@@ -1138,13 +1145,14 @@ void DXVAVideoDecodeAccelerator::NotifyFlushDone() { |
} |
void DXVAVideoDecodeAccelerator::NotifyResetDone() { |
+ pending_output_samples_.clear(); |
if (client_) |
client_->NotifyResetDone(); |
} |
void DXVAVideoDecodeAccelerator::RequestPictureBuffers(int width, int height) { |
// This task could execute after the decoder has been torn down. |
- if (state_ != kUninitialized && client_) { |
+ if (GetState() != kUninitialized && client_) { |
client_->ProvidePictureBuffers( |
kNumPictureBuffers, |
gfx::Size(width, height), |
@@ -1155,27 +1163,28 @@ void DXVAVideoDecodeAccelerator::RequestPictureBuffers(int width, int height) { |
void DXVAVideoDecodeAccelerator::NotifyPictureReady( |
const media::Picture& picture) { |
// This task could execute after the decoder has been torn down. |
- if (state_ != kUninitialized && client_) |
+ if (GetState() != kUninitialized && client_) |
client_->PictureReady(picture); |
} |
-void DXVAVideoDecodeAccelerator::NotifyInputBuffersDropped() { |
- if (!client_ || !pending_output_samples_.empty()) |
+void DXVAVideoDecodeAccelerator::NotifyInputBuffersDropped( |
+ const PendingInputs& input_buffers) { |
+ if (!client_) |
return; |
- for (PendingInputs::iterator it = pending_input_buffers_.begin(); |
- it != pending_input_buffers_.end(); ++it) { |
+ for (PendingInputs::const_iterator it = input_buffers.begin(); |
+ it != input_buffers.end(); ++it) { |
LONGLONG input_buffer_id = 0; |
RETURN_ON_HR_FAILURE((*it)->GetSampleTime(&input_buffer_id), |
"Failed to get buffer id associated with sample",); |
client_->NotifyEndOfBitstreamBuffer(input_buffer_id); |
} |
- pending_input_buffers_.clear(); |
} |
void DXVAVideoDecodeAccelerator::DecodePendingInputBuffers() { |
- RETURN_AND_NOTIFY_ON_FAILURE((state_ != kUninitialized), |
- "Invalid state: " << state_, ILLEGAL_STATE,); |
+ State state = GetState(); |
+ RETURN_AND_NOTIFY_ON_FAILURE((state != kUninitialized), |
+ "Invalid state: " << state, ILLEGAL_STATE,); |
if (pending_input_buffers_.empty() || !pending_output_samples_.empty()) |
return; |
@@ -1188,7 +1197,7 @@ void DXVAVideoDecodeAccelerator::DecodePendingInputBuffers() { |
DecodeInternal(*it); |
} |
- if (state_ != kFlushingPendingInputBuffers) |
+ if (state != kFlushingPendingInputBuffers) |
return; |
// If we are scheduled during a flush operation then mark the flush as |
@@ -1197,7 +1206,7 @@ void DXVAVideoDecodeAccelerator::DecodePendingInputBuffers() { |
// scheduled again by the ProcessPendingSamples function once slots become |
// available. |
if (pending_input_buffers_.empty() && pending_output_samples_.empty()) { |
- state_ = kNormal; |
+ SetState(kNormal); |
base::MessageLoop::current()->PostTask( |
FROM_HERE, |
base::Bind(&DXVAVideoDecodeAccelerator::NotifyFlushDone, |
@@ -1206,12 +1215,18 @@ void DXVAVideoDecodeAccelerator::DecodePendingInputBuffers() { |
} |
void DXVAVideoDecodeAccelerator::FlushInternal() { |
+ if (decoder_thread_.thread_id() != ::GetCurrentThreadId()) { |
DaleCurtis
2014/12/03 01:04:33
decoder_thread_task_runner_->BelongsToCurrentThrea
ananta
2014/12/03 04:08:02
Done.
|
+ decoder_thread_task_runner_->PostTask(FROM_HERE, |
+ base::Bind(&DXVAVideoDecodeAccelerator::FlushInternal, |
+ base::Unretained(this))); |
+ return; |
+ } |
// The DoDecode function sets the state to kStopped when the decoder returns |
// MF_E_TRANSFORM_NEED_MORE_INPUT. |
// The MFT decoder can buffer upto 30 frames worth of input before returning |
// an output frame. This loop here attempts to retrieve as many output frames |
// as possible from the buffered set. |
- while (state_ != kStopped) { |
+ while (GetState() != kStopped) { |
DoDecode(); |
if (!pending_output_samples_.empty()) |
return; |
@@ -1223,27 +1238,25 @@ void DXVAVideoDecodeAccelerator::FlushInternal() { |
// transitions in the decoder are a touch intertwined with other portions of |
// the code like AssignPictureBuffers, ReusePictureBuffers etc. |
if (!pending_input_buffers_.empty()) { |
- state_ = kFlushingPendingInputBuffers; |
- base::MessageLoop::current()->PostTask( |
+ SetState(kFlushingPendingInputBuffers); |
+ main_thread_task_runner_->PostTask( |
FROM_HERE, |
base::Bind(&DXVAVideoDecodeAccelerator::DecodePendingInputBuffers, |
weak_this_factory_.GetWeakPtr())); |
return; |
} |
- base::MessageLoop::current()->PostTask( |
+ main_thread_task_runner_->PostTask( |
FROM_HERE, |
base::Bind(&DXVAVideoDecodeAccelerator::NotifyFlushDone, |
weak_this_factory_.GetWeakPtr())); |
- state_ = kNormal; |
+ SetState(kNormal); |
} |
void DXVAVideoDecodeAccelerator::DecodeInternal( |
const base::win::ScopedComPtr<IMFSample>& sample) { |
- DCHECK(CalledOnValidThread()); |
DaleCurtis
2014/12/03 01:04:33
Can you replace this with the appropriate DCHECK(t
ananta
2014/12/03 04:08:02
Done.
|
- |
- if (state_ == kUninitialized) |
+ if (GetState() == kUninitialized) |
return; |
if (!pending_output_samples_.empty() || !pending_input_buffers_.empty()) { |
@@ -1268,8 +1281,10 @@ void DXVAVideoDecodeAccelerator::DecodeInternal( |
// decoder failure. |
if (hr == MF_E_NOTACCEPTING) { |
DoDecode(); |
- RETURN_AND_NOTIFY_ON_FAILURE((state_ == kStopped || state_ == kNormal), |
- "Failed to process output. Unexpected decoder state: " << state_, |
+ State state = GetState(); |
+ RETURN_AND_NOTIFY_ON_FAILURE((state == kStopped || state == kNormal || |
+ state == kFlushing), |
+ "Failed to process output. Unexpected decoder state: " << state, |
PLATFORM_FAILURE,); |
hr = decoder_->ProcessInput(0, sample.get(), 0); |
// If we continue to get the MF_E_NOTACCEPTING error we do the following:- |
@@ -1297,9 +1312,11 @@ void DXVAVideoDecodeAccelerator::DecodeInternal( |
DoDecode(); |
- RETURN_AND_NOTIFY_ON_FAILURE((state_ == kStopped || state_ == kNormal || |
- state_ == kFlushingPendingInputBuffers), |
- "Failed to process output. Unexpected decoder state: " << state_, |
+ State state = GetState(); |
+ RETURN_AND_NOTIFY_ON_FAILURE((state == kStopped || state == kNormal || |
+ state == kFlushingPendingInputBuffers || |
+ state == kFlushing), |
+ "Failed to process output. Unexpected decoder state: " << state, |
ILLEGAL_STATE,); |
LONGLONG input_buffer_id = 0; |
@@ -1315,7 +1332,7 @@ void DXVAVideoDecodeAccelerator::DecodeInternal( |
// decoder to emit an output packet for every input packet. |
// http://code.google.com/p/chromium/issues/detail?id=108121 |
// http://code.google.com/p/chromium/issues/detail?id=150925 |
- base::MessageLoop::current()->PostTask( |
+ main_thread_task_runner_->PostTask( |
FROM_HERE, |
base::Bind(&DXVAVideoDecodeAccelerator::NotifyInputBufferRead, |
weak_this_factory_.GetWeakPtr(), |
@@ -1366,4 +1383,41 @@ void DXVAVideoDecodeAccelerator::DeferredDismissStaleBuffer( |
stale_output_picture_buffers_.erase(it); |
} |
+DXVAVideoDecodeAccelerator::State |
+DXVAVideoDecodeAccelerator::GetState() const { |
+ State state = kUninitialized; |
+ ::InterlockedExchange(reinterpret_cast<long*>(&state), |
+ state_); |
+ return state; |
+} |
+ |
+void DXVAVideoDecodeAccelerator::SetState(State new_state) { |
+ ::InterlockedCompareExchange(reinterpret_cast<long*>(&state_), |
DaleCurtis
2014/12/03 01:04:33
This can fail if it's racing against another SetSt
ananta
2014/12/03 04:08:02
SetState now runs on the main thread only.
|
+ new_state, state_); |
+} |
+ |
+void DXVAVideoDecodeAccelerator::ResetHelper() { |
+ SetState(kResetting); |
+ |
+ PendingInputs pending_input_buffers_copy; |
+ std::swap(pending_input_buffers_, pending_input_buffers_copy); |
+ pending_input_buffers_.clear(); |
+ |
+ main_thread_task_runner_->PostTask( |
+ FROM_HERE, |
+ base::Bind(&DXVAVideoDecodeAccelerator::NotifyInputBuffersDropped, |
+ weak_this_factory_.GetWeakPtr(), |
+ pending_input_buffers_copy)); |
+ |
+ RETURN_AND_NOTIFY_ON_FAILURE(SendMFTMessage(MFT_MESSAGE_COMMAND_FLUSH, 0), |
+ "Reset: Failed to send message.", PLATFORM_FAILURE,); |
+ |
+ main_thread_task_runner_->PostTask( |
+ FROM_HERE, |
+ base::Bind(&DXVAVideoDecodeAccelerator::NotifyResetDone, |
+ weak_this_factory_.GetWeakPtr())); |
+ |
+ SetState(DXVAVideoDecodeAccelerator::kNormal); |
+} |
+ |
} // namespace content |