Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(112)

Side by Side Diff: content/common/gpu/media/dxva_video_decode_accelerator.cc

Issue 765533005: Move the DXVA decoder off the GPU thread. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Code review comments Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "content/common/gpu/media/dxva_video_decode_accelerator.h" 5 #include "content/common/gpu/media/dxva_video_decode_accelerator.h"
6 6
7 #if !defined(OS_WIN) 7 #if !defined(OS_WIN)
8 #error This file should only be built on Windows. 8 #error This file should only be built on Windows.
9 #endif // !defined(OS_WIN) 9 #endif // !defined(OS_WIN)
10 10
11 #include <ks.h> 11 #include <ks.h>
12 #include <codecapi.h> 12 #include <codecapi.h>
13 #include <mfapi.h> 13 #include <mfapi.h>
14 #include <mferror.h> 14 #include <mferror.h>
15 #include <wmcodecdsp.h> 15 #include <wmcodecdsp.h>
16 16
17 #include "base/base_paths_win.h" 17 #include "base/base_paths_win.h"
18 #include "base/bind.h" 18 #include "base/bind.h"
19 #include "base/callback.h" 19 #include "base/callback.h"
20 #include "base/command_line.h" 20 #include "base/command_line.h"
21 #include "base/debug/trace_event.h" 21 #include "base/debug/trace_event.h"
22 #include "base/file_version_info.h" 22 #include "base/file_version_info.h"
23 #include "base/files/file_path.h" 23 #include "base/files/file_path.h"
24 #include "base/logging.h" 24 #include "base/logging.h"
25 #include "base/memory/scoped_ptr.h" 25 #include "base/memory/scoped_ptr.h"
26 #include "base/memory/shared_memory.h" 26 #include "base/memory/shared_memory.h"
27 #include "base/message_loop/message_loop.h" 27 #include "base/message_loop/message_loop.h"
28 #include "base/path_service.h" 28 #include "base/path_service.h"
29 #include "base/thread_task_runner_handle.h"
29 #include "base/win/windows_version.h" 30 #include "base/win/windows_version.h"
30 #include "media/video/video_decode_accelerator.h" 31 #include "media/video/video_decode_accelerator.h"
31 #include "ui/gl/gl_bindings.h" 32 #include "ui/gl/gl_bindings.h"
32 #include "ui/gl/gl_surface_egl.h" 33 #include "ui/gl/gl_surface_egl.h"
33 #include "ui/gl/gl_switches.h" 34 #include "ui/gl/gl_switches.h"
34 35
35 namespace { 36 namespace {
36 37
37 // Path is appended on to the PROGRAM_FILES base path. 38 // Path is appended on to the PROGRAM_FILES base path.
38 const wchar_t kVPXDecoderDLLPath[] = L"Intel\\Media SDK\\"; 39 const wchar_t kVPXDecoderDLLPath[] = L"Intel\\Media SDK\\";
(...skipping 442 matching lines...) Expand 10 before | Expand all | Expand 10 after
481 DXVAVideoDecodeAccelerator::DXVAVideoDecodeAccelerator( 482 DXVAVideoDecodeAccelerator::DXVAVideoDecodeAccelerator(
482 const base::Callback<bool(void)>& make_context_current) 483 const base::Callback<bool(void)>& make_context_current)
483 : client_(NULL), 484 : client_(NULL),
484 dev_manager_reset_token_(0), 485 dev_manager_reset_token_(0),
485 egl_config_(NULL), 486 egl_config_(NULL),
486 state_(kUninitialized), 487 state_(kUninitialized),
487 pictures_requested_(false), 488 pictures_requested_(false),
488 inputs_before_decode_(0), 489 inputs_before_decode_(0),
489 make_context_current_(make_context_current), 490 make_context_current_(make_context_current),
490 weak_this_factory_(this), 491 weak_this_factory_(this),
491 codec_(media::kUnknownVideoCodec) { 492 codec_(media::kUnknownVideoCodec),
493 decoder_thread_("DXVAVideoDecoderThread") {
492 memset(&input_stream_info_, 0, sizeof(input_stream_info_)); 494 memset(&input_stream_info_, 0, sizeof(input_stream_info_));
493 memset(&output_stream_info_, 0, sizeof(output_stream_info_)); 495 memset(&output_stream_info_, 0, sizeof(output_stream_info_));
494 } 496 }
495 497
496 DXVAVideoDecodeAccelerator::~DXVAVideoDecodeAccelerator() { 498 DXVAVideoDecodeAccelerator::~DXVAVideoDecodeAccelerator() {
497 client_ = NULL; 499 client_ = NULL;
498 } 500 }
499 501
500 bool DXVAVideoDecodeAccelerator::Initialize(media::VideoCodecProfile profile, 502 bool DXVAVideoDecodeAccelerator::Initialize(media::VideoCodecProfile profile,
501 Client* client) { 503 Client* client) {
502 DCHECK(CalledOnValidThread()); 504 DCHECK(CalledOnValidThread());
DaleCurtis 2014/12/03 22:34:41 I'd remove base::NonThreadSafe now from inheritanc
503 505
504 client_ = client; 506 client_ = client;
505 507
508 main_thread_task_runner_ = base::MessageLoop::current()->task_runner();
509
506 // Not all versions of Windows 7 and later include Media Foundation DLLs. 510 // Not all versions of Windows 7 and later include Media Foundation DLLs.
507 // Instead of crashing while delay loading the DLL when calling MFStartup() 511 // Instead of crashing while delay loading the DLL when calling MFStartup()
508 // below, probe whether we can successfully load the DLL now. 512 // below, probe whether we can successfully load the DLL now.
509 // 513 //
510 // See http://crbug.com/339678 for details. 514 // See http://crbug.com/339678 for details.
511 HMODULE mfplat_dll = ::LoadLibrary(L"MFPlat.dll"); 515 HMODULE mfplat_dll = ::LoadLibrary(L"MFPlat.dll");
512 RETURN_ON_FAILURE(mfplat_dll, "MFPlat.dll is required for decoding", false); 516 RETURN_ON_FAILURE(mfplat_dll, "MFPlat.dll is required for decoding", false);
513 517
514 // TODO(ananta) 518 // TODO(ananta)
515 // H264PROFILE_HIGH video decoding is janky at times. Needs more 519 // H264PROFILE_HIGH video decoding is janky at times. Needs more
516 // investigation. http://crbug.com/426707 520 // investigation. http://crbug.com/426707
517 if (profile != media::H264PROFILE_BASELINE && 521 if (profile != media::H264PROFILE_BASELINE &&
518 profile != media::H264PROFILE_MAIN && 522 profile != media::H264PROFILE_MAIN &&
519 profile != media::VP8PROFILE_ANY && 523 profile != media::VP8PROFILE_ANY &&
520 profile != media::VP9PROFILE_ANY) { 524 profile != media::VP9PROFILE_ANY) {
521 RETURN_AND_NOTIFY_ON_FAILURE(false, 525 RETURN_AND_NOTIFY_ON_FAILURE(false,
522 "Unsupported h.264, vp8, or vp9 profile", PLATFORM_FAILURE, false); 526 "Unsupported h.264, vp8, or vp9 profile", PLATFORM_FAILURE, false);
523 } 527 }
524 528
525 RETURN_AND_NOTIFY_ON_FAILURE( 529 RETURN_AND_NOTIFY_ON_FAILURE(
526 gfx::g_driver_egl.ext.b_EGL_ANGLE_surface_d3d_texture_2d_share_handle, 530 gfx::g_driver_egl.ext.b_EGL_ANGLE_surface_d3d_texture_2d_share_handle,
527 "EGL_ANGLE_surface_d3d_texture_2d_share_handle unavailable", 531 "EGL_ANGLE_surface_d3d_texture_2d_share_handle unavailable",
528 PLATFORM_FAILURE, 532 PLATFORM_FAILURE,
529 false); 533 false);
530 534
531 RETURN_AND_NOTIFY_ON_FAILURE((state_ == kUninitialized), 535 State state = GetState();
532 "Initialize: invalid state: " << state_, ILLEGAL_STATE, false); 536 RETURN_AND_NOTIFY_ON_FAILURE((state == kUninitialized),
537 "Initialize: invalid state: " << state, ILLEGAL_STATE, false);
533 538
534 HRESULT hr = MFStartup(MF_VERSION, MFSTARTUP_FULL); 539 HRESULT hr = MFStartup(MF_VERSION, MFSTARTUP_FULL);
535 RETURN_AND_NOTIFY_ON_HR_FAILURE(hr, "MFStartup failed.", PLATFORM_FAILURE, 540 RETURN_AND_NOTIFY_ON_HR_FAILURE(hr, "MFStartup failed.", PLATFORM_FAILURE,
536 false); 541 false);
537 542
538 RETURN_AND_NOTIFY_ON_FAILURE(CreateD3DDevManager(), 543 RETURN_AND_NOTIFY_ON_FAILURE(CreateD3DDevManager(),
539 "Failed to initialize D3D device and manager", 544 "Failed to initialize D3D device and manager",
540 PLATFORM_FAILURE, 545 PLATFORM_FAILURE,
541 false); 546 false);
542 547
543 RETURN_AND_NOTIFY_ON_FAILURE(InitDecoder(profile), 548 RETURN_AND_NOTIFY_ON_FAILURE(InitDecoder(profile),
544 "Failed to initialize decoder", PLATFORM_FAILURE, false); 549 "Failed to initialize decoder", PLATFORM_FAILURE, false);
545 550
546 RETURN_AND_NOTIFY_ON_FAILURE(GetStreamsInfoAndBufferReqs(), 551 RETURN_AND_NOTIFY_ON_FAILURE(GetStreamsInfoAndBufferReqs(),
547 "Failed to get input/output stream info.", PLATFORM_FAILURE, false); 552 "Failed to get input/output stream info.", PLATFORM_FAILURE, false);
548 553
549 RETURN_AND_NOTIFY_ON_FAILURE( 554 RETURN_AND_NOTIFY_ON_FAILURE(
550 SendMFTMessage(MFT_MESSAGE_NOTIFY_BEGIN_STREAMING, 0), 555 SendMFTMessage(MFT_MESSAGE_NOTIFY_BEGIN_STREAMING, 0),
551 "Send MFT_MESSAGE_NOTIFY_BEGIN_STREAMING notification failed", 556 "Send MFT_MESSAGE_NOTIFY_BEGIN_STREAMING notification failed",
552 PLATFORM_FAILURE, false); 557 PLATFORM_FAILURE, false);
553 558
554 RETURN_AND_NOTIFY_ON_FAILURE( 559 RETURN_AND_NOTIFY_ON_FAILURE(
555 SendMFTMessage(MFT_MESSAGE_NOTIFY_START_OF_STREAM, 0), 560 SendMFTMessage(MFT_MESSAGE_NOTIFY_START_OF_STREAM, 0),
556 "Send MFT_MESSAGE_NOTIFY_START_OF_STREAM notification failed", 561 "Send MFT_MESSAGE_NOTIFY_START_OF_STREAM notification failed",
557 PLATFORM_FAILURE, false); 562 PLATFORM_FAILURE, false);
558 563
559 state_ = kNormal; 564 SetState(kNormal);
565
566 StartDecoderThread();
560 return true; 567 return true;
561 } 568 }
562 569
563 void DXVAVideoDecodeAccelerator::Decode( 570 void DXVAVideoDecodeAccelerator::Decode(
564 const media::BitstreamBuffer& bitstream_buffer) { 571 const media::BitstreamBuffer& bitstream_buffer) {
565 DCHECK(CalledOnValidThread()); 572 DCHECK_EQ(main_thread_task_runner_.get(),
DaleCurtis 2014/12/03 22:34:41 Just DCHECK(main_thread_task_runner_->BelongsToCur
ananta 2014/12/04 00:45:24 Done.
573 base::ThreadTaskRunnerHandle::Get().get());
566 574
567 RETURN_AND_NOTIFY_ON_FAILURE((state_ == kNormal || state_ == kStopped || 575 State state = GetState();
568 state_ == kFlushing), 576 RETURN_AND_NOTIFY_ON_FAILURE((state == kNormal || state == kStopped ||
569 "Invalid state: " << state_, ILLEGAL_STATE,); 577 state == kFlushing),
578 "Invalid state: " << state, ILLEGAL_STATE,);
570 579
571 base::win::ScopedComPtr<IMFSample> sample; 580 base::win::ScopedComPtr<IMFSample> sample;
572 sample.Attach(CreateSampleFromInputBuffer(bitstream_buffer, 581 sample.Attach(CreateSampleFromInputBuffer(bitstream_buffer,
573 input_stream_info_.cbSize, 582 input_stream_info_.cbSize,
574 input_stream_info_.cbAlignment)); 583 input_stream_info_.cbAlignment));
575 RETURN_AND_NOTIFY_ON_FAILURE(sample.get(), "Failed to create input sample", 584 RETURN_AND_NOTIFY_ON_FAILURE(sample.get(), "Failed to create input sample",
576 PLATFORM_FAILURE, ); 585 PLATFORM_FAILURE, );
577 586
578 RETURN_AND_NOTIFY_ON_HR_FAILURE(sample->SetSampleTime(bitstream_buffer.id()), 587 RETURN_AND_NOTIFY_ON_HR_FAILURE(sample->SetSampleTime(bitstream_buffer.id()),
579 "Failed to associate input buffer id with sample", PLATFORM_FAILURE,); 588 "Failed to associate input buffer id with sample", PLATFORM_FAILURE,);
580 589
581 DecodeInternal(sample); 590 decoder_thread_task_runner_->PostTask(FROM_HERE,
DaleCurtis 2014/12/03 22:34:41 Wrapping is wrong, clang-format?
ananta 2014/12/04 00:45:24 Done.
591 base::Bind(&DXVAVideoDecodeAccelerator::DecodeInternal,
592 base::Unretained(this), sample));
582 } 593 }
583 594
584 void DXVAVideoDecodeAccelerator::AssignPictureBuffers( 595 void DXVAVideoDecodeAccelerator::AssignPictureBuffers(
585 const std::vector<media::PictureBuffer>& buffers) { 596 const std::vector<media::PictureBuffer>& buffers) {
586 DCHECK(CalledOnValidThread()); 597 DCHECK_EQ(main_thread_task_runner_.get(),
598 base::ThreadTaskRunnerHandle::Get().get());
587 599
588 RETURN_AND_NOTIFY_ON_FAILURE((state_ != kUninitialized), 600 State state = GetState();
589 "Invalid state: " << state_, ILLEGAL_STATE,); 601 RETURN_AND_NOTIFY_ON_FAILURE((state != kUninitialized),
602 "Invalid state: " << state, ILLEGAL_STATE,);
590 RETURN_AND_NOTIFY_ON_FAILURE((kNumPictureBuffers == buffers.size()), 603 RETURN_AND_NOTIFY_ON_FAILURE((kNumPictureBuffers == buffers.size()),
591 "Failed to provide requested picture buffers. (Got " << buffers.size() << 604 "Failed to provide requested picture buffers. (Got " << buffers.size() <<
592 ", requested " << kNumPictureBuffers << ")", INVALID_ARGUMENT,); 605 ", requested " << kNumPictureBuffers << ")", INVALID_ARGUMENT,);
593 606
594 // Copy the picture buffers provided by the client to the available list, 607 // Copy the picture buffers provided by the client to the available list,
595 // and mark these buffers as available for use. 608 // and mark these buffers as available for use.
596 for (size_t buffer_index = 0; buffer_index < buffers.size(); 609 for (size_t buffer_index = 0; buffer_index < buffers.size();
597 ++buffer_index) { 610 ++buffer_index) {
598 linked_ptr<DXVAPictureBuffer> picture_buffer = 611 linked_ptr<DXVAPictureBuffer> picture_buffer =
599 DXVAPictureBuffer::Create(*this, buffers[buffer_index], egl_config_); 612 DXVAPictureBuffer::Create(*this, buffers[buffer_index], egl_config_);
600 RETURN_AND_NOTIFY_ON_FAILURE(picture_buffer.get(), 613 RETURN_AND_NOTIFY_ON_FAILURE(picture_buffer.get(),
601 "Failed to allocate picture buffer", PLATFORM_FAILURE,); 614 "Failed to allocate picture buffer", PLATFORM_FAILURE,);
602 615
603 bool inserted = output_picture_buffers_.insert(std::make_pair( 616 bool inserted = output_picture_buffers_.insert(std::make_pair(
604 buffers[buffer_index].id(), picture_buffer)).second; 617 buffers[buffer_index].id(), picture_buffer)).second;
605 DCHECK(inserted); 618 DCHECK(inserted);
606 } 619 }
607 ProcessPendingSamples(); 620 ProcessPendingSamples();
608 if (state_ == kFlushing && pending_output_samples_.empty()) 621
622 if (state == kFlushing && !OutputSamplesPresent())
DaleCurtis 2014/12/03 22:34:41 This is racy; ProcessOutputSamples() might be bloc
ananta 2014/12/04 00:45:24 Moved the check for output samples in the decoder
609 FlushInternal(); 623 FlushInternal();
610 } 624 }
611 625
612 void DXVAVideoDecodeAccelerator::ReusePictureBuffer( 626 void DXVAVideoDecodeAccelerator::ReusePictureBuffer(
613 int32 picture_buffer_id) { 627 int32 picture_buffer_id) {
614 DCHECK(CalledOnValidThread()); 628 DCHECK_EQ(main_thread_task_runner_.get(),
629 base::ThreadTaskRunnerHandle::Get().get());
615 630
616 RETURN_AND_NOTIFY_ON_FAILURE((state_ != kUninitialized), 631 State state = GetState();
617 "Invalid state: " << state_, ILLEGAL_STATE,); 632 RETURN_AND_NOTIFY_ON_FAILURE((state != kUninitialized),
633 "Invalid state: " << state, ILLEGAL_STATE,);
618 634
619 if (output_picture_buffers_.empty() && stale_output_picture_buffers_.empty()) 635 if (output_picture_buffers_.empty() && stale_output_picture_buffers_.empty())
620 return; 636 return;
621 637
622 OutputBuffers::iterator it = output_picture_buffers_.find(picture_buffer_id); 638 OutputBuffers::iterator it = output_picture_buffers_.find(picture_buffer_id);
623 // If we didn't find the picture id in the |output_picture_buffers_| map we 639 // If we didn't find the picture id in the |output_picture_buffers_| map we
624 // try the |stale_output_picture_buffers_| map, as this may have been an 640 // try the |stale_output_picture_buffers_| map, as this may have been an
625 // output picture buffer from before a resolution change, that at resolution 641 // output picture buffer from before a resolution change, that at resolution
626 // change time had yet to be displayed. The client is calling us back to tell 642 // change time had yet to be displayed. The client is calling us back to tell
627 // us that we can now recycle this picture buffer, so if we were waiting to 643 // us that we can now recycle this picture buffer, so if we were waiting to
628 // dispose of it we now can. 644 // dispose of it we now can.
629 if (it == output_picture_buffers_.end()) { 645 if (it == output_picture_buffers_.end()) {
630 it = stale_output_picture_buffers_.find(picture_buffer_id); 646 it = stale_output_picture_buffers_.find(picture_buffer_id);
631 RETURN_AND_NOTIFY_ON_FAILURE(it != stale_output_picture_buffers_.end(), 647 RETURN_AND_NOTIFY_ON_FAILURE(it != stale_output_picture_buffers_.end(),
632 "Invalid picture id: " << picture_buffer_id, INVALID_ARGUMENT,); 648 "Invalid picture id: " << picture_buffer_id, INVALID_ARGUMENT,);
633 base::MessageLoop::current()->PostTask(FROM_HERE, 649 base::MessageLoop::current()->PostTask(FROM_HERE,
634 base::Bind(&DXVAVideoDecodeAccelerator::DeferredDismissStaleBuffer, 650 base::Bind(&DXVAVideoDecodeAccelerator::DeferredDismissStaleBuffer,
635 weak_this_factory_.GetWeakPtr(), picture_buffer_id)); 651 weak_this_factory_.GetWeakPtr(), picture_buffer_id));
636 return; 652 return;
637 } 653 }
638 654
639 it->second->ReusePictureBuffer(); 655 it->second->ReusePictureBuffer();
640 ProcessPendingSamples(); 656 ProcessPendingSamples();
641 657
642 if (state_ == kFlushing && pending_output_samples_.empty()) 658 if (state == kFlushing && !OutputSamplesPresent())
DaleCurtis 2014/12/03 22:34:41 Ditto.
ananta 2014/12/04 00:45:24 Done.
643 FlushInternal(); 659 FlushInternal();
644 } 660 }
645 661
646 void DXVAVideoDecodeAccelerator::Flush() { 662 void DXVAVideoDecodeAccelerator::Flush() {
647 DCHECK(CalledOnValidThread()); 663 DCHECK_EQ(main_thread_task_runner_.get(),
664 base::ThreadTaskRunnerHandle::Get().get());
648 665
649 DVLOG(1) << "DXVAVideoDecodeAccelerator::Flush"; 666 DVLOG(1) << "DXVAVideoDecodeAccelerator::Flush";
650 667
651 RETURN_AND_NOTIFY_ON_FAILURE((state_ == kNormal || state_ == kStopped), 668 State state = GetState();
652 "Unexpected decoder state: " << state_, ILLEGAL_STATE,); 669 RETURN_AND_NOTIFY_ON_FAILURE((state == kNormal || state == kStopped),
670 "Unexpected decoder state: " << state, ILLEGAL_STATE,);
653 671
654 state_ = kFlushing; 672 SetState(kFlushing);
655 673
656 RETURN_AND_NOTIFY_ON_FAILURE(SendMFTMessage(MFT_MESSAGE_COMMAND_DRAIN, 0), 674 RETURN_AND_NOTIFY_ON_FAILURE(SendMFTMessage(MFT_MESSAGE_COMMAND_DRAIN, 0),
657 "Failed to send drain message", PLATFORM_FAILURE,); 675 "Failed to send drain message", PLATFORM_FAILURE,);
658 676
659 if (!pending_output_samples_.empty()) 677 if (OutputSamplesPresent())
DaleCurtis 2014/12/03 22:34:41 Ditto.
ananta 2014/12/04 00:45:24 Done.
660 return; 678 return;
661 679
662 FlushInternal(); 680 FlushInternal();
663 } 681 }
664 682
665 void DXVAVideoDecodeAccelerator::Reset() { 683 void DXVAVideoDecodeAccelerator::Reset() {
666 DCHECK(CalledOnValidThread());
667
668 DVLOG(1) << "DXVAVideoDecodeAccelerator::Reset"; 684 DVLOG(1) << "DXVAVideoDecodeAccelerator::Reset";
DaleCurtis 2014/12/03 22:34:41 Seems you can add a DCHECK(main_thread_task_runner
ananta 2014/12/04 00:45:24 Done.
669 685
670 RETURN_AND_NOTIFY_ON_FAILURE((state_ == kNormal || state_ == kStopped), 686 State state = GetState();
671 "Reset: invalid state: " << state_, ILLEGAL_STATE,); 687 RETURN_AND_NOTIFY_ON_FAILURE((state == kNormal || state == kStopped),
688 "Reset: invalid state: " << state, ILLEGAL_STATE,);
672 689
673 state_ = kResetting; 690 decoder_thread_.Stop();
DaleCurtis 2014/12/03 22:34:41 Why does the thread need to be stopped? It seems
ananta 2014/12/04 00:45:24 Stopping and restarting the decoder thread gets ri
691
692 SetState(kResetting);
674 693
675 pending_output_samples_.clear(); 694 pending_output_samples_.clear();
676 695
677 NotifyInputBuffersDropped(); 696 NotifyInputBuffersDropped();
678 697
679 RETURN_AND_NOTIFY_ON_FAILURE(SendMFTMessage(MFT_MESSAGE_COMMAND_FLUSH, 0), 698 RETURN_AND_NOTIFY_ON_FAILURE(SendMFTMessage(MFT_MESSAGE_COMMAND_FLUSH, 0),
680 "Reset: Failed to send message.", PLATFORM_FAILURE,); 699 "Reset: Failed to send message.", PLATFORM_FAILURE,);
681 700
682 base::MessageLoop::current()->PostTask( 701 base::MessageLoop::current()->PostTask(
DaleCurtis 2014/12/03 22:34:42 Can you replace all of the MessageLoop::current()
ananta 2014/12/04 00:45:24 Done.
683 FROM_HERE, 702 FROM_HERE,
684 base::Bind(&DXVAVideoDecodeAccelerator::NotifyResetDone, 703 base::Bind(&DXVAVideoDecodeAccelerator::NotifyResetDone,
685 weak_this_factory_.GetWeakPtr())); 704 weak_this_factory_.GetWeakPtr()));
DaleCurtis 2014/12/03 22:34:41 Now that you've got the WeakPtr bouncing between t
ananta 2014/12/04 00:45:24 That is a valid concern. However given that the de
DaleCurtis 2014/12/04 01:54:27 It's not necessarily a lifetime issue, it's a raci
686 705
687 state_ = DXVAVideoDecodeAccelerator::kNormal; 706 StartDecoderThread();
707 SetState(kNormal);
688 } 708 }
689 709
690 void DXVAVideoDecodeAccelerator::Destroy() { 710 void DXVAVideoDecodeAccelerator::Destroy() {
691 DCHECK(CalledOnValidThread()); 711 DCHECK_EQ(main_thread_task_runner_.get(),
712 base::ThreadTaskRunnerHandle::Get().get());
692 Invalidate(); 713 Invalidate();
693 delete this; 714 delete this;
694 } 715 }
695 716
696 bool DXVAVideoDecodeAccelerator::CanDecodeOnIOThread() { 717 bool DXVAVideoDecodeAccelerator::CanDecodeOnIOThread() {
697 return false; 718 return false;
698 } 719 }
699 720
700 bool DXVAVideoDecodeAccelerator::InitDecoder(media::VideoCodecProfile profile) { 721 bool DXVAVideoDecodeAccelerator::InitDecoder(media::VideoCodecProfile profile) {
701 HMODULE decoder_dll = NULL; 722 HMODULE decoder_dll = NULL;
(...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after
922 << std::hex << std::showbase << output_stream_info_.dwFlags; 943 << std::hex << std::showbase << output_stream_info_.dwFlags;
923 if (codec_ == media::kCodecH264) { 944 if (codec_ == media::kCodecH264) {
924 CHECK_EQ(output_stream_info_.dwFlags, 0x107u); 945 CHECK_EQ(output_stream_info_.dwFlags, 0x107u);
925 } 946 }
926 DVLOG(1) << "Min buffer size: " << output_stream_info_.cbSize; 947 DVLOG(1) << "Min buffer size: " << output_stream_info_.cbSize;
927 DVLOG(1) << "Alignment: " << output_stream_info_.cbAlignment; 948 DVLOG(1) << "Alignment: " << output_stream_info_.cbAlignment;
928 return true; 949 return true;
929 } 950 }
930 951
931 void DXVAVideoDecodeAccelerator::DoDecode() { 952 void DXVAVideoDecodeAccelerator::DoDecode() {
953 DCHECK_EQ(decoder_thread_task_runner_.get(),
DaleCurtis 2014/12/03 22:34:41 Use BelongsToCurrentThread() instead.
ananta 2014/12/04 00:45:24 Done.
954 base::ThreadTaskRunnerHandle::Get().get());
932 // This function is also called from FlushInternal in a loop which could 955 // This function is also called from FlushInternal in a loop which could
933 // result in the state transitioning to kStopped due to no decoded output. 956 // result in the state transitioning to kStopped due to no decoded output.
957 State state = GetState();
934 RETURN_AND_NOTIFY_ON_FAILURE( 958 RETURN_AND_NOTIFY_ON_FAILURE(
935 (state_ == kNormal || state_ == kFlushing || 959 (state == kNormal || state == kFlushing ||
936 state_ == kStopped || state_ == kFlushingPendingInputBuffers), 960 state == kStopped || state == kFlushingPendingInputBuffers),
937 "DoDecode: not in normal/flushing/stopped state", ILLEGAL_STATE,); 961 "DoDecode: not in normal/flushing/stopped state", ILLEGAL_STATE,);
938 962
939 MFT_OUTPUT_DATA_BUFFER output_data_buffer = {0}; 963 MFT_OUTPUT_DATA_BUFFER output_data_buffer = {0};
940 DWORD status = 0; 964 DWORD status = 0;
941 965
942 HRESULT hr = decoder_->ProcessOutput(0, // No flags 966 HRESULT hr = decoder_->ProcessOutput(0, // No flags
943 1, // # of out streams to pull from 967 1, // # of out streams to pull from
944 &output_data_buffer, 968 &output_data_buffer,
945 &status); 969 &status);
946 IMFCollection* events = output_data_buffer.pEvents; 970 IMFCollection* events = output_data_buffer.pEvents;
947 if (events != NULL) { 971 if (events != NULL) {
948 DVLOG(1) << "Got events from ProcessOuput, but discarding"; 972 DVLOG(1) << "Got events from ProcessOuput, but discarding";
949 events->Release(); 973 events->Release();
950 } 974 }
951 if (FAILED(hr)) { 975 if (FAILED(hr)) {
952 // A stream change needs further ProcessInput calls to get back decoder 976 // A stream change needs further ProcessInput calls to get back decoder
953 // output which is why we need to set the state to stopped. 977 // output which is why we need to set the state to stopped.
954 if (hr == MF_E_TRANSFORM_STREAM_CHANGE) { 978 if (hr == MF_E_TRANSFORM_STREAM_CHANGE) {
955 if (!SetDecoderOutputMediaType(MFVideoFormat_NV12)) { 979 if (!SetDecoderOutputMediaType(MFVideoFormat_NV12)) {
956 // Decoder didn't let us set NV12 output format. Not sure as to why 980 // Decoder didn't let us set NV12 output format. Not sure as to why
957 // this can happen. Give up in disgust. 981 // this can happen. Give up in disgust.
958 NOTREACHED() << "Failed to set decoder output media type to NV12"; 982 NOTREACHED() << "Failed to set decoder output media type to NV12";
959 state_ = kStopped; 983 SetState(kStopped);
960 } else { 984 } else {
961 DVLOG(1) << "Received output format change from the decoder." 985 DVLOG(1) << "Received output format change from the decoder."
962 " Recursively invoking DoDecode"; 986 " Recursively invoking DoDecode";
963 DoDecode(); 987 DoDecode();
964 } 988 }
965 return; 989 return;
966 } else if (hr == MF_E_TRANSFORM_NEED_MORE_INPUT) { 990 } else if (hr == MF_E_TRANSFORM_NEED_MORE_INPUT) {
967 // No more output from the decoder. Stop playback. 991 // No more output from the decoder. Stop playback.
968 if (state_ != kFlushingPendingInputBuffers) 992 if (state != kFlushingPendingInputBuffers)
969 state_ = kStopped; 993 SetState(kStopped);
970 return; 994 return;
971 } else { 995 } else {
972 NOTREACHED() << "Unhandled error in DoDecode()"; 996 NOTREACHED() << "Unhandled error in DoDecode()";
973 return; 997 return;
974 } 998 }
975 } 999 }
976 TRACE_EVENT_END_ETW("DXVAVideoDecodeAccelerator.Decoding", this, ""); 1000 TRACE_EVENT_END_ETW("DXVAVideoDecodeAccelerator.Decoding", this, "");
977 1001
978 TRACE_COUNTER1("DXVA Decoding", "TotalPacketsBeforeDecode", 1002 TRACE_COUNTER1("DXVA Decoding", "TotalPacketsBeforeDecode",
979 inputs_before_decode_); 1003 inputs_before_decode_);
980 1004
981 inputs_before_decode_ = 0; 1005 inputs_before_decode_ = 0;
982 1006
983 RETURN_AND_NOTIFY_ON_FAILURE(ProcessOutputSample(output_data_buffer.pSample), 1007 RETURN_AND_NOTIFY_ON_FAILURE(ProcessOutputSample(output_data_buffer.pSample),
984 "Failed to process output sample.", PLATFORM_FAILURE,); 1008 "Failed to process output sample.", PLATFORM_FAILURE,);
985 } 1009 }
986 1010
987 bool DXVAVideoDecodeAccelerator::ProcessOutputSample(IMFSample* sample) { 1011 bool DXVAVideoDecodeAccelerator::ProcessOutputSample(IMFSample* sample) {
DaleCurtis 2014/12/03 22:34:41 I think this whole method needs to be run on the m
ananta 2014/12/04 00:45:24 I removed the output_buffers.size check and just p
988 RETURN_ON_FAILURE(sample, "Decode succeeded with NULL output sample", false); 1012 RETURN_ON_FAILURE(sample, "Decode succeeded with NULL output sample", false);
989 1013
990 base::win::ScopedComPtr<IMFMediaBuffer> output_buffer; 1014 base::win::ScopedComPtr<IMFMediaBuffer> output_buffer;
991 HRESULT hr = sample->GetBufferByIndex(0, output_buffer.Receive()); 1015 HRESULT hr = sample->GetBufferByIndex(0, output_buffer.Receive());
992 RETURN_ON_HR_FAILURE(hr, "Failed to get buffer from output sample", false); 1016 RETURN_ON_HR_FAILURE(hr, "Failed to get buffer from output sample", false);
993 1017
994 base::win::ScopedComPtr<IDirect3DSurface9> surface; 1018 base::win::ScopedComPtr<IDirect3DSurface9> surface;
995 hr = MFGetService(output_buffer.get(), MR_BUFFER_SERVICE, 1019 hr = MFGetService(output_buffer.get(), MR_BUFFER_SERVICE,
996 IID_PPV_ARGS(surface.Receive())); 1020 IID_PPV_ARGS(surface.Receive()));
997 RETURN_ON_HR_FAILURE(hr, "Failed to get D3D surface from output sample", 1021 RETURN_ON_HR_FAILURE(hr, "Failed to get D3D surface from output sample",
998 false); 1022 false);
999 1023
1000 LONGLONG input_buffer_id = 0; 1024 LONGLONG input_buffer_id = 0;
1001 RETURN_ON_HR_FAILURE(sample->GetSampleTime(&input_buffer_id), 1025 RETURN_ON_HR_FAILURE(sample->GetSampleTime(&input_buffer_id),
1002 "Failed to get input buffer id associated with sample", 1026 "Failed to get input buffer id associated with sample",
1003 false); 1027 false);
1004 1028
1029 decoder_lock_.Acquire();
1005 pending_output_samples_.push_back( 1030 pending_output_samples_.push_back(
1006 PendingSampleInfo(input_buffer_id, sample)); 1031 PendingSampleInfo(input_buffer_id, sample));
1032 decoder_lock_.Release();
1007 1033
1008 // If we have available picture buffers to copy the output data then use the 1034 // If we have available picture buffers to copy the output data then use the
1009 // first one and then flag it as not being available for use. 1035 // first one and then flag it as not being available for use.
1010 if (output_picture_buffers_.size()) { 1036 if (output_picture_buffers_.size()) {
1011 ProcessPendingSamples(); 1037 main_thread_task_runner_->PostTask(
1038 FROM_HERE,
1039 base::Bind(&DXVAVideoDecodeAccelerator::ProcessPendingSamples,
1040 weak_this_factory_.GetWeakPtr()));
1012 return true; 1041 return true;
1013 } 1042 }
1043
1014 if (pictures_requested_) { 1044 if (pictures_requested_) {
1015 DVLOG(1) << "Waiting for picture slots from the client."; 1045 DVLOG(1) << "Waiting for picture slots from the client.";
1016 return true; 1046 return true;
1017 } 1047 }
1018 1048
1019 // We only read the surface description, which contains its width/height when 1049 // We only read the surface description, which contains its width/height when
1020 // we need the picture buffers from the client. Once we have those, then they 1050 // we need the picture buffers from the client. Once we have those, then they
1021 // are reused. 1051 // are reused.
1022 D3DSURFACE_DESC surface_desc; 1052 D3DSURFACE_DESC surface_desc;
1023 hr = surface->GetDesc(&surface_desc); 1053 hr = surface->GetDesc(&surface_desc);
1024 RETURN_ON_HR_FAILURE(hr, "Failed to get surface description", false); 1054 RETURN_ON_HR_FAILURE(hr, "Failed to get surface description", false);
1025 1055
1026 // Go ahead and request picture buffers. 1056 // Go ahead and request picture buffers.
1027 base::MessageLoop::current()->PostTask( 1057 main_thread_task_runner_->PostTask(
1028 FROM_HERE, 1058 FROM_HERE,
1029 base::Bind(&DXVAVideoDecodeAccelerator::RequestPictureBuffers, 1059 base::Bind(&DXVAVideoDecodeAccelerator::RequestPictureBuffers,
1030 weak_this_factory_.GetWeakPtr(), 1060 weak_this_factory_.GetWeakPtr(),
1031 surface_desc.Width, 1061 surface_desc.Width,
1032 surface_desc.Height)); 1062 surface_desc.Height));
1033 1063
1034 pictures_requested_ = true; 1064 pictures_requested_ = true;
1035 return true; 1065 return true;
1036 } 1066 }
1037 1067
1038 void DXVAVideoDecodeAccelerator::ProcessPendingSamples() { 1068 void DXVAVideoDecodeAccelerator::ProcessPendingSamples() {
1069 DCHECK_EQ(main_thread_task_runner_.get(),
1070 base::ThreadTaskRunnerHandle::Get().get());
1071
1039 RETURN_AND_NOTIFY_ON_FAILURE(make_context_current_.Run(), 1072 RETURN_AND_NOTIFY_ON_FAILURE(make_context_current_.Run(),
1040 "Failed to make context current", PLATFORM_FAILURE,); 1073 "Failed to make context current", PLATFORM_FAILURE,);
1041 1074
1042 OutputBuffers::iterator index; 1075 OutputBuffers::iterator index;
1043 1076
1077 base::AutoLock lock(decoder_lock_);
1078
1044 for (index = output_picture_buffers_.begin(); 1079 for (index = output_picture_buffers_.begin();
1045 index != output_picture_buffers_.end() && 1080 index != output_picture_buffers_.end() &&
1046 !pending_output_samples_.empty(); 1081 !pending_output_samples_.empty();
1047 ++index) { 1082 ++index) {
1048 if (index->second->available()) { 1083 if (index->second->available()) {
1049 PendingSampleInfo sample_info = pending_output_samples_.front(); 1084 PendingSampleInfo sample_info = pending_output_samples_.front();
1050 1085
1051 base::win::ScopedComPtr<IMFMediaBuffer> output_buffer; 1086 base::win::ScopedComPtr<IMFMediaBuffer> output_buffer;
1052 HRESULT hr = sample_info.output_sample->GetBufferByIndex( 1087 HRESULT hr = sample_info.output_sample->GetBufferByIndex(
1053 0, output_buffer.Receive()); 1088 0, output_buffer.Receive());
(...skipping 21 matching lines...) Expand all
1075 } 1110 }
1076 1111
1077 RETURN_AND_NOTIFY_ON_FAILURE( 1112 RETURN_AND_NOTIFY_ON_FAILURE(
1078 index->second->CopyOutputSampleDataToPictureBuffer(*this, 1113 index->second->CopyOutputSampleDataToPictureBuffer(*this,
1079 surface.get()), 1114 surface.get()),
1080 "Failed to copy output sample", PLATFORM_FAILURE, ); 1115 "Failed to copy output sample", PLATFORM_FAILURE, );
1081 1116
1082 media::Picture output_picture(index->second->id(), 1117 media::Picture output_picture(index->second->id(),
1083 sample_info.input_buffer_id, 1118 sample_info.input_buffer_id,
1084 gfx::Rect(index->second->size())); 1119 gfx::Rect(index->second->size()));
1085 base::MessageLoop::current()->PostTask( 1120 main_thread_task_runner_->PostTask(
1086 FROM_HERE, 1121 FROM_HERE,
1087 base::Bind(&DXVAVideoDecodeAccelerator::NotifyPictureReady, 1122 base::Bind(&DXVAVideoDecodeAccelerator::NotifyPictureReady,
1088 weak_this_factory_.GetWeakPtr(), 1123 weak_this_factory_.GetWeakPtr(),
1089 output_picture)); 1124 output_picture));
1090 1125
1091 index->second->set_available(false); 1126 index->second->set_available(false);
1092 pending_output_samples_.pop_front(); 1127 pending_output_samples_.pop_front();
1093 } 1128 }
1094 } 1129 }
1095 1130 decoder_thread_task_runner_->PostTask(
1096 if (!pending_input_buffers_.empty() && pending_output_samples_.empty()) { 1131 FROM_HERE,
1097 base::MessageLoop::current()->PostTask( 1132 base::Bind(&DXVAVideoDecodeAccelerator::DecodePendingInputBuffers,
1098 FROM_HERE, 1133 base::Unretained(this)));
1099 base::Bind(&DXVAVideoDecodeAccelerator::DecodePendingInputBuffers,
1100 weak_this_factory_.GetWeakPtr()));
1101 }
1102 } 1134 }
1103 1135
1104 void DXVAVideoDecodeAccelerator::StopOnError( 1136 void DXVAVideoDecodeAccelerator::StopOnError(
1105 media::VideoDecodeAccelerator::Error error) { 1137 media::VideoDecodeAccelerator::Error error) {
1106 DCHECK(CalledOnValidThread()); 1138 DCHECK_EQ(main_thread_task_runner_.get(),
1139 base::ThreadTaskRunnerHandle::Get().get());
1107 1140
1108 if (client_) 1141 if (client_)
1109 client_->NotifyError(error); 1142 client_->NotifyError(error);
1110 client_ = NULL; 1143 client_ = NULL;
1111 1144
1112 if (state_ != kUninitialized) { 1145 if (GetState() != kUninitialized) {
1113 Invalidate(); 1146 Invalidate();
1114 } 1147 }
1115 } 1148 }
1116 1149
1117 void DXVAVideoDecodeAccelerator::Invalidate() { 1150 void DXVAVideoDecodeAccelerator::Invalidate() {
1118 if (state_ == kUninitialized) 1151 if (GetState() == kUninitialized)
1119 return; 1152 return;
1153 decoder_thread_.Stop();
1120 weak_this_factory_.InvalidateWeakPtrs(); 1154 weak_this_factory_.InvalidateWeakPtrs();
1121 output_picture_buffers_.clear(); 1155 output_picture_buffers_.clear();
1122 stale_output_picture_buffers_.clear(); 1156 stale_output_picture_buffers_.clear();
1123 pending_output_samples_.clear(); 1157 pending_output_samples_.clear();
1124 pending_input_buffers_.clear(); 1158 pending_input_buffers_.clear();
1125 decoder_.Release(); 1159 decoder_.Release();
1126 MFShutdown(); 1160 MFShutdown();
1127 state_ = kUninitialized; 1161 SetState(kUninitialized);
1128 } 1162 }
1129 1163
1130 void DXVAVideoDecodeAccelerator::NotifyInputBufferRead(int input_buffer_id) { 1164 void DXVAVideoDecodeAccelerator::NotifyInputBufferRead(int input_buffer_id) {
1165 DCHECK_EQ(main_thread_task_runner_.get(),
1166 base::ThreadTaskRunnerHandle::Get().get());
1131 if (client_) 1167 if (client_)
1132 client_->NotifyEndOfBitstreamBuffer(input_buffer_id); 1168 client_->NotifyEndOfBitstreamBuffer(input_buffer_id);
1133 } 1169 }
1134 1170
1135 void DXVAVideoDecodeAccelerator::NotifyFlushDone() { 1171 void DXVAVideoDecodeAccelerator::NotifyFlushDone() {
1172 DCHECK_EQ(main_thread_task_runner_.get(),
1173 base::ThreadTaskRunnerHandle::Get().get());
1136 if (client_) 1174 if (client_)
1137 client_->NotifyFlushDone(); 1175 client_->NotifyFlushDone();
1138 } 1176 }
1139 1177
1140 void DXVAVideoDecodeAccelerator::NotifyResetDone() { 1178 void DXVAVideoDecodeAccelerator::NotifyResetDone() {
1179 DCHECK_EQ(main_thread_task_runner_.get(),
1180 base::ThreadTaskRunnerHandle::Get().get());
1141 if (client_) 1181 if (client_)
1142 client_->NotifyResetDone(); 1182 client_->NotifyResetDone();
1143 } 1183 }
1144 1184
1145 void DXVAVideoDecodeAccelerator::RequestPictureBuffers(int width, int height) { 1185 void DXVAVideoDecodeAccelerator::RequestPictureBuffers(int width, int height) {
1186 DCHECK_EQ(main_thread_task_runner_.get(),
1187 base::ThreadTaskRunnerHandle::Get().get());
1146 // This task could execute after the decoder has been torn down. 1188 // This task could execute after the decoder has been torn down.
1147 if (state_ != kUninitialized && client_) { 1189 if (GetState() != kUninitialized && client_) {
1148 client_->ProvidePictureBuffers( 1190 client_->ProvidePictureBuffers(
1149 kNumPictureBuffers, 1191 kNumPictureBuffers,
1150 gfx::Size(width, height), 1192 gfx::Size(width, height),
1151 GL_TEXTURE_2D); 1193 GL_TEXTURE_2D);
1152 } 1194 }
1153 } 1195 }
1154 1196
1155 void DXVAVideoDecodeAccelerator::NotifyPictureReady( 1197 void DXVAVideoDecodeAccelerator::NotifyPictureReady(
1156 const media::Picture& picture) { 1198 const media::Picture& picture) {
1199 DCHECK_EQ(main_thread_task_runner_.get(),
1200 base::ThreadTaskRunnerHandle::Get().get());
1157 // This task could execute after the decoder has been torn down. 1201 // This task could execute after the decoder has been torn down.
1158 if (state_ != kUninitialized && client_) 1202 if (GetState() != kUninitialized && client_)
1159 client_->PictureReady(picture); 1203 client_->PictureReady(picture);
1160 } 1204 }
1161 1205
1162 void DXVAVideoDecodeAccelerator::NotifyInputBuffersDropped() { 1206 void DXVAVideoDecodeAccelerator::NotifyInputBuffersDropped() {
1163 if (!client_ || !pending_output_samples_.empty()) 1207 DCHECK_EQ(main_thread_task_runner_.get(),
1208 base::ThreadTaskRunnerHandle::Get().get());
1209
1210 if (!client_)
1164 return; 1211 return;
1165 1212
1166 for (PendingInputs::iterator it = pending_input_buffers_.begin(); 1213 for (PendingInputs::iterator it = pending_input_buffers_.begin();
1167 it != pending_input_buffers_.end(); ++it) { 1214 it != pending_input_buffers_.end(); ++it) {
1168 LONGLONG input_buffer_id = 0; 1215 LONGLONG input_buffer_id = 0;
1169 RETURN_ON_HR_FAILURE((*it)->GetSampleTime(&input_buffer_id), 1216 RETURN_ON_HR_FAILURE((*it)->GetSampleTime(&input_buffer_id),
1170 "Failed to get buffer id associated with sample",); 1217 "Failed to get buffer id associated with sample",);
1171 client_->NotifyEndOfBitstreamBuffer(input_buffer_id); 1218 client_->NotifyEndOfBitstreamBuffer(input_buffer_id);
1172 } 1219 }
1173 pending_input_buffers_.clear(); 1220 pending_input_buffers_.clear();
1174 } 1221 }
1175 1222
1176 void DXVAVideoDecodeAccelerator::DecodePendingInputBuffers() { 1223 void DXVAVideoDecodeAccelerator::DecodePendingInputBuffers() {
1177 RETURN_AND_NOTIFY_ON_FAILURE((state_ != kUninitialized), 1224 DCHECK_EQ(decoder_thread_task_runner_.get(),
1178 "Invalid state: " << state_, ILLEGAL_STATE,); 1225 base::ThreadTaskRunnerHandle::Get().get());
1179 1226
1180 if (pending_input_buffers_.empty() || !pending_output_samples_.empty()) 1227 State state = GetState();
1228 RETURN_AND_NOTIFY_ON_FAILURE((state != kUninitialized),
1229 "Invalid state: " << state, ILLEGAL_STATE,);
1230
1231 if (pending_input_buffers_.empty() || OutputSamplesPresent())
1181 return; 1232 return;
1182 1233
1183 PendingInputs pending_input_buffers_copy; 1234 PendingInputs pending_input_buffers_copy;
1184 std::swap(pending_input_buffers_, pending_input_buffers_copy); 1235 std::swap(pending_input_buffers_, pending_input_buffers_copy);
1185 1236
1186 for (PendingInputs::iterator it = pending_input_buffers_copy.begin(); 1237 for (PendingInputs::iterator it = pending_input_buffers_copy.begin();
1187 it != pending_input_buffers_copy.end(); ++it) { 1238 it != pending_input_buffers_copy.end(); ++it) {
1188 DecodeInternal(*it); 1239 DecodeInternal(*it);
1189 } 1240 }
1190 1241
1191 if (state_ != kFlushingPendingInputBuffers) 1242 if (state != kFlushingPendingInputBuffers)
1192 return; 1243 return;
1193 1244
1194 // If we are scheduled during a flush operation then mark the flush as 1245 // If we are scheduled during a flush operation then mark the flush as
1195 // complete if we have no pending input and pending output frames. 1246 // complete if we have no pending input and pending output frames.
1196 // If we don't have available output slots then this function will be 1247 // If we don't have available output slots then this function will be
1197 // scheduled again by the ProcessPendingSamples function once slots become 1248 // scheduled again by the ProcessPendingSamples function once slots become
1198 // available. 1249 // available.
1199 if (pending_input_buffers_.empty() && pending_output_samples_.empty()) { 1250 if (pending_input_buffers_.empty() && !OutputSamplesPresent()) {
1200 state_ = kNormal; 1251 SetState(kNormal);
1201 base::MessageLoop::current()->PostTask( 1252 base::MessageLoop::current()->PostTask(
1202 FROM_HERE, 1253 FROM_HERE,
1203 base::Bind(&DXVAVideoDecodeAccelerator::NotifyFlushDone, 1254 base::Bind(&DXVAVideoDecodeAccelerator::NotifyFlushDone,
1204 weak_this_factory_.GetWeakPtr())); 1255 weak_this_factory_.GetWeakPtr()));
1205 } 1256 }
1206 } 1257 }
1207 1258
1208 void DXVAVideoDecodeAccelerator::FlushInternal() { 1259 void DXVAVideoDecodeAccelerator::FlushInternal() {
1260 if (!decoder_thread_task_runner_->BelongsToCurrentThread()) {
1261 decoder_thread_task_runner_->PostTask(FROM_HERE,
1262 base::Bind(&DXVAVideoDecodeAccelerator::FlushInternal,
1263 base::Unretained(this)));
1264 return;
1265 }
1209 // The DoDecode function sets the state to kStopped when the decoder returns 1266 // The DoDecode function sets the state to kStopped when the decoder returns
1210 // MF_E_TRANSFORM_NEED_MORE_INPUT. 1267 // MF_E_TRANSFORM_NEED_MORE_INPUT.
1211 // The MFT decoder can buffer upto 30 frames worth of input before returning 1268 // The MFT decoder can buffer upto 30 frames worth of input before returning
1212 // an output frame. This loop here attempts to retrieve as many output frames 1269 // an output frame. This loop here attempts to retrieve as many output frames
1213 // as possible from the buffered set. 1270 // as possible from the buffered set.
1214 while (state_ != kStopped) { 1271 while (GetState() != kStopped) {
1215 DoDecode(); 1272 DoDecode();
1216 if (!pending_output_samples_.empty()) 1273 if (OutputSamplesPresent())
1217 return; 1274 return;
1218 } 1275 }
1219 1276
1220 // TODO(ananta) 1277 // TODO(ananta)
1221 // Look into whether we can simplify this function by combining the while 1278 // Look into whether we can simplify this function by combining the while
1222 // above and the code below into a single block which achieves both. The Flush 1279 // above and the code below into a single block which achieves both. The Flush
1223 // transitions in the decoder are a touch intertwined with other portions of 1280 // transitions in the decoder are a touch intertwined with other portions of
1224 // the code like AssignPictureBuffers, ReusePictureBuffers etc. 1281 // the code like AssignPictureBuffers, ReusePictureBuffers etc.
1225 if (!pending_input_buffers_.empty()) { 1282 if (!pending_input_buffers_.empty()) {
1226 state_ = kFlushingPendingInputBuffers; 1283 SetState(kFlushingPendingInputBuffers);
1227 base::MessageLoop::current()->PostTask( 1284 base::MessageLoop::current()->PostTask(
1228 FROM_HERE, 1285 FROM_HERE,
1229 base::Bind(&DXVAVideoDecodeAccelerator::DecodePendingInputBuffers, 1286 base::Bind(&DXVAVideoDecodeAccelerator::DecodePendingInputBuffers,
1230 weak_this_factory_.GetWeakPtr())); 1287 base::Unretained(this)));
1231 return; 1288 return;
1232 } 1289 }
1233 1290
1234 base::MessageLoop::current()->PostTask( 1291 main_thread_task_runner_->PostTask(
1235 FROM_HERE, 1292 FROM_HERE,
1236 base::Bind(&DXVAVideoDecodeAccelerator::NotifyFlushDone, 1293 base::Bind(&DXVAVideoDecodeAccelerator::NotifyFlushDone,
1237 weak_this_factory_.GetWeakPtr())); 1294 weak_this_factory_.GetWeakPtr()));
1238 1295
1239 state_ = kNormal; 1296 SetState(kNormal);
1240 } 1297 }
1241 1298
1242 void DXVAVideoDecodeAccelerator::DecodeInternal( 1299 void DXVAVideoDecodeAccelerator::DecodeInternal(
1243 const base::win::ScopedComPtr<IMFSample>& sample) { 1300 const base::win::ScopedComPtr<IMFSample>& sample) {
1244 DCHECK(CalledOnValidThread()); 1301 DCHECK_EQ(decoder_thread_task_runner_.get(),
1302 base::ThreadTaskRunnerHandle::Get().get());
1245 1303
1246 if (state_ == kUninitialized) 1304 if (GetState() == kUninitialized)
1247 return; 1305 return;
1248 1306
1249 if (!pending_output_samples_.empty() || !pending_input_buffers_.empty()) { 1307 if (OutputSamplesPresent() || !pending_input_buffers_.empty()) {
1250 pending_input_buffers_.push_back(sample); 1308 pending_input_buffers_.push_back(sample);
1251 return; 1309 return;
1252 } 1310 }
1253 1311
1254 if (!inputs_before_decode_) { 1312 if (!inputs_before_decode_) {
1255 TRACE_EVENT_BEGIN_ETW("DXVAVideoDecodeAccelerator.Decoding", this, ""); 1313 TRACE_EVENT_BEGIN_ETW("DXVAVideoDecodeAccelerator.Decoding", this, "");
1256 } 1314 }
1257 inputs_before_decode_++; 1315 inputs_before_decode_++;
1258 1316
1259 HRESULT hr = decoder_->ProcessInput(0, sample.get(), 0); 1317 HRESULT hr = decoder_->ProcessInput(0, sample.get(), 0);
1260 // As per msdn if the decoder returns MF_E_NOTACCEPTING then it means that it 1318 // As per msdn if the decoder returns MF_E_NOTACCEPTING then it means that it
1261 // has enough data to produce one or more output samples. In this case the 1319 // has enough data to produce one or more output samples. In this case the
1262 // recommended options are to 1320 // recommended options are to
1263 // 1. Generate new output by calling IMFTransform::ProcessOutput until it 1321 // 1. Generate new output by calling IMFTransform::ProcessOutput until it
1264 // returns MF_E_TRANSFORM_NEED_MORE_INPUT. 1322 // returns MF_E_TRANSFORM_NEED_MORE_INPUT.
1265 // 2. Flush the input data 1323 // 2. Flush the input data
1266 // We implement the first option, i.e to retrieve the output sample and then 1324 // We implement the first option, i.e to retrieve the output sample and then
1267 // process the input again. Failure in either of these steps is treated as a 1325 // process the input again. Failure in either of these steps is treated as a
1268 // decoder failure. 1326 // decoder failure.
1269 if (hr == MF_E_NOTACCEPTING) { 1327 if (hr == MF_E_NOTACCEPTING) {
1270 DoDecode(); 1328 DoDecode();
1271 RETURN_AND_NOTIFY_ON_FAILURE((state_ == kStopped || state_ == kNormal), 1329 State state = GetState();
1272 "Failed to process output. Unexpected decoder state: " << state_, 1330 RETURN_AND_NOTIFY_ON_FAILURE((state == kStopped || state == kNormal ||
1331 state == kFlushing),
1332 "Failed to process output. Unexpected decoder state: " << state,
1273 PLATFORM_FAILURE,); 1333 PLATFORM_FAILURE,);
1274 hr = decoder_->ProcessInput(0, sample.get(), 0); 1334 hr = decoder_->ProcessInput(0, sample.get(), 0);
1275 // If we continue to get the MF_E_NOTACCEPTING error we do the following:- 1335 // If we continue to get the MF_E_NOTACCEPTING error we do the following:-
1276 // 1. Add the input sample to the pending queue. 1336 // 1. Add the input sample to the pending queue.
1277 // 2. If we don't have any output samples we post the 1337 // 2. If we don't have any output samples we post the
1278 // DecodePendingInputBuffers task to process the pending input samples. 1338 // DecodePendingInputBuffers task to process the pending input samples.
1279 // If we have an output sample then the above task is posted when the 1339 // If we have an output sample then the above task is posted when the
1280 // output samples are sent to the client. 1340 // output samples are sent to the client.
1281 // This is because we only support 1 pending output sample at any 1341 // This is because we only support 1 pending output sample at any
1282 // given time due to the limitation with the Microsoft media foundation 1342 // given time due to the limitation with the Microsoft media foundation
1283 // decoder where it recycles the output Decoder surfaces. 1343 // decoder where it recycles the output Decoder surfaces.
1284 if (hr == MF_E_NOTACCEPTING) { 1344 if (hr == MF_E_NOTACCEPTING) {
1285 pending_input_buffers_.push_back(sample); 1345 pending_input_buffers_.push_back(sample);
1286 if (pending_output_samples_.empty()) { 1346 if (!OutputSamplesPresent()) {
1287 base::MessageLoop::current()->PostTask( 1347 base::MessageLoop::current()->PostTask(
1288 FROM_HERE, 1348 FROM_HERE,
1289 base::Bind(&DXVAVideoDecodeAccelerator::DecodePendingInputBuffers, 1349 base::Bind(&DXVAVideoDecodeAccelerator::DecodePendingInputBuffers,
1290 weak_this_factory_.GetWeakPtr())); 1350 base::Unretained(this)));
1291 } 1351 }
1292 return; 1352 return;
1293 } 1353 }
1294 } 1354 }
1295 RETURN_AND_NOTIFY_ON_HR_FAILURE(hr, "Failed to process input sample", 1355 RETURN_AND_NOTIFY_ON_HR_FAILURE(hr, "Failed to process input sample",
1296 PLATFORM_FAILURE,); 1356 PLATFORM_FAILURE,);
1297 1357
1298 DoDecode(); 1358 DoDecode();
1299 1359
1300 RETURN_AND_NOTIFY_ON_FAILURE((state_ == kStopped || state_ == kNormal || 1360 State state = GetState();
1301 state_ == kFlushingPendingInputBuffers), 1361 RETURN_AND_NOTIFY_ON_FAILURE((state == kStopped || state == kNormal ||
1302 "Failed to process output. Unexpected decoder state: " << state_, 1362 state == kFlushingPendingInputBuffers ||
1363 state == kFlushing),
1364 "Failed to process output. Unexpected decoder state: " << state,
1303 ILLEGAL_STATE,); 1365 ILLEGAL_STATE,);
1304 1366
1305 LONGLONG input_buffer_id = 0; 1367 LONGLONG input_buffer_id = 0;
1306 RETURN_ON_HR_FAILURE(sample->GetSampleTime(&input_buffer_id), 1368 RETURN_ON_HR_FAILURE(sample->GetSampleTime(&input_buffer_id),
1307 "Failed to get input buffer id associated with sample",); 1369 "Failed to get input buffer id associated with sample",);
1308 // The Microsoft Media foundation decoder internally buffers up to 30 frames 1370 // The Microsoft Media foundation decoder internally buffers up to 30 frames
1309 // before returning a decoded frame. We need to inform the client that this 1371 // before returning a decoded frame. We need to inform the client that this
1310 // input buffer is processed as it may stop sending us further input. 1372 // input buffer is processed as it may stop sending us further input.
1311 // Note: This may break clients which expect every input buffer to be 1373 // Note: This may break clients which expect every input buffer to be
1312 // associated with a decoded output buffer. 1374 // associated with a decoded output buffer.
1313 // TODO(ananta) 1375 // TODO(ananta)
1314 // Do some more investigation into whether it is possible to get the MFT 1376 // Do some more investigation into whether it is possible to get the MFT
1315 // decoder to emit an output packet for every input packet. 1377 // decoder to emit an output packet for every input packet.
1316 // http://code.google.com/p/chromium/issues/detail?id=108121 1378 // http://code.google.com/p/chromium/issues/detail?id=108121
1317 // http://code.google.com/p/chromium/issues/detail?id=150925 1379 // http://code.google.com/p/chromium/issues/detail?id=150925
1318 base::MessageLoop::current()->PostTask( 1380 main_thread_task_runner_->PostTask(
1319 FROM_HERE, 1381 FROM_HERE,
1320 base::Bind(&DXVAVideoDecodeAccelerator::NotifyInputBufferRead, 1382 base::Bind(&DXVAVideoDecodeAccelerator::NotifyInputBufferRead,
1321 weak_this_factory_.GetWeakPtr(), 1383 weak_this_factory_.GetWeakPtr(),
1322 input_buffer_id)); 1384 input_buffer_id));
1323 } 1385 }
1324 1386
1325 void DXVAVideoDecodeAccelerator::HandleResolutionChanged(int width, 1387 void DXVAVideoDecodeAccelerator::HandleResolutionChanged(int width,
1326 int height) { 1388 int height) {
1327 base::MessageLoop::current()->PostTask( 1389 base::MessageLoop::current()->PostTask(
1328 FROM_HERE, 1390 FROM_HERE,
(...skipping 30 matching lines...) Expand all
1359 void DXVAVideoDecodeAccelerator::DeferredDismissStaleBuffer( 1421 void DXVAVideoDecodeAccelerator::DeferredDismissStaleBuffer(
1360 int32 picture_buffer_id) { 1422 int32 picture_buffer_id) {
1361 OutputBuffers::iterator it = stale_output_picture_buffers_.find( 1423 OutputBuffers::iterator it = stale_output_picture_buffers_.find(
1362 picture_buffer_id); 1424 picture_buffer_id);
1363 DCHECK(it != stale_output_picture_buffers_.end()); 1425 DCHECK(it != stale_output_picture_buffers_.end());
1364 DVLOG(1) << "Dismissing picture id: " << it->second->id(); 1426 DVLOG(1) << "Dismissing picture id: " << it->second->id();
1365 client_->DismissPictureBuffer(it->second->id()); 1427 client_->DismissPictureBuffer(it->second->id());
1366 stale_output_picture_buffers_.erase(it); 1428 stale_output_picture_buffers_.erase(it);
1367 } 1429 }
1368 1430
1431 DXVAVideoDecodeAccelerator::State
1432 DXVAVideoDecodeAccelerator::GetState() const {
1433 State state = kUninitialized;
1434 ::InterlockedExchange(reinterpret_cast<long*>(&state),
1435 state_);
1436 return state;
1437 }
1438
1439 void DXVAVideoDecodeAccelerator::SetState(State new_state) {
1440 if (!main_thread_task_runner_->BelongsToCurrentThread()) {
1441 main_thread_task_runner_->PostTask(
1442 FROM_HERE,
1443 base::Bind(&DXVAVideoDecodeAccelerator::SetState,
1444 weak_this_factory_.GetWeakPtr(),
1445 new_state));
1446 return;
1447 }
1448 ::InterlockedCompareExchange(reinterpret_cast<long*>(&state_),
1449 new_state, state_);
1450 }
1451
1452 void DXVAVideoDecodeAccelerator::StartDecoderThread() {
1453 decoder_thread_.init_com_with_mta(false);
1454 decoder_thread_.Start();
1455 decoder_thread_task_runner_ = decoder_thread_.task_runner();
1456 }
1457
1458 bool DXVAVideoDecodeAccelerator::OutputSamplesPresent() {
1459 base::AutoLock lock(decoder_lock_);
1460 return !pending_output_samples_.empty();
1461 }
1462
1369 } // namespace content 1463 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698