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

Side by Side Diff: content/renderer/media/rtc_video_decoder.cc

Issue 19534002: Make RendererGpuVideoDecoderFactories live on arbitrary threads. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: address review comments Created 7 years, 5 months 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 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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/renderer/media/rtc_video_decoder.h" 5 #include "content/renderer/media/rtc_video_decoder.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/logging.h" 8 #include "base/logging.h"
9 #include "base/memory/ref_counted.h" 9 #include "base/memory/ref_counted.h"
10 #include "base/message_loop/message_loop_proxy.h" 10 #include "base/message_loop/message_loop_proxy.h"
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
65 65
66 RTCVideoDecoder::BufferData::BufferData() {} 66 RTCVideoDecoder::BufferData::BufferData() {}
67 67
68 RTCVideoDecoder::BufferData::~BufferData() {} 68 RTCVideoDecoder::BufferData::~BufferData() {}
69 69
70 RTCVideoDecoder::RTCVideoDecoder( 70 RTCVideoDecoder::RTCVideoDecoder(
71 const scoped_refptr<media::GpuVideoDecoder::Factories>& factories) 71 const scoped_refptr<media::GpuVideoDecoder::Factories>& factories)
72 : weak_factory_(this), 72 : weak_factory_(this),
73 weak_this_(weak_factory_.GetWeakPtr()), 73 weak_this_(weak_factory_.GetWeakPtr()),
74 factories_(factories), 74 factories_(factories),
75 vda_loop_proxy_(factories_->GetMessageLoop()), 75 vda_loop_proxy_(factories->GetMessageLoop()),
76 create_shm_thread_("CreateSHMThread"),
77 decoder_texture_target_(0), 76 decoder_texture_target_(0),
78 next_picture_buffer_id_(0), 77 next_picture_buffer_id_(0),
79 state_(UNINITIALIZED), 78 state_(UNINITIALIZED),
80 decode_complete_callback_(NULL), 79 decode_complete_callback_(NULL),
81 num_shm_buffers_(0), 80 num_shm_buffers_(0),
82 next_bitstream_buffer_id_(0), 81 next_bitstream_buffer_id_(0),
83 reset_bitstream_buffer_id_(ID_INVALID) { 82 reset_bitstream_buffer_id_(ID_INVALID) {
84 create_shm_thread_.Start(); 83 DCHECK(!vda_loop_proxy_->BelongsToCurrentThread());
85 // Initialize directly if |vda_loop_proxy_| is the renderer thread. 84 base::WaitableEvent message_loop_async_waiter(false, false);
86 base::WaitableEvent compositor_loop_async_waiter(false, false); 85 // Waiting here is safe. The media thread is stopped in the child thread and
87 if (vda_loop_proxy_->BelongsToCurrentThread()) { 86 // the child thread is blocked when VideoDecoderFactory::CreateVideoDecoder
88 Initialize(&compositor_loop_async_waiter); 87 // runs.
89 return;
90 }
91 // Post the task if |vda_loop_proxy_| is the compositor thread. Waiting here
92 // is safe because the compositor thread will not be stopped until the
93 // renderer thread shuts down.
94 vda_loop_proxy_->PostTask(FROM_HERE, 88 vda_loop_proxy_->PostTask(FROM_HERE,
95 base::Bind(&RTCVideoDecoder::Initialize, 89 base::Bind(&RTCVideoDecoder::Initialize,
96 base::Unretained(this), 90 base::Unretained(this),
97 &compositor_loop_async_waiter)); 91 &message_loop_async_waiter));
98 compositor_loop_async_waiter.Wait(); 92 message_loop_async_waiter.Wait();
99 } 93 }
100 94
101 RTCVideoDecoder::~RTCVideoDecoder() { 95 RTCVideoDecoder::~RTCVideoDecoder() {
102 DVLOG(2) << "~RTCVideoDecoder"; 96 DVLOG(2) << "~RTCVideoDecoder";
103 factories_->Abort(); 97 // Remove |this| from the observer if vda thread is alive.
104 create_shm_thread_.Stop(); 98 if (vda_loop_proxy_->BelongsToCurrentThread())
105 // Delete vda and remove |this| from the observer if vda thread is alive.
106 if (vda_loop_proxy_->BelongsToCurrentThread()) {
107 base::MessageLoop::current()->RemoveDestructionObserver(this); 99 base::MessageLoop::current()->RemoveDestructionObserver(this);
108 DestroyVDA(); 100 // VDA should have been destroyed.
109 } else { 101 DCHECK(!vda_);
110 // VDA should have been destroyed in WillDestroyCurrentMessageLoop.
111 DCHECK(!vda_);
112 }
113 102
114 // Delete all shared memories. 103 // Delete all shared memories.
115 STLDeleteElements(&available_shm_segments_); 104 STLDeleteElements(&available_shm_segments_);
116 STLDeleteValues(&bitstream_buffers_in_decoder_); 105 STLDeleteValues(&bitstream_buffers_in_decoder_);
117 STLDeleteContainerPairFirstPointers(decode_buffers_.begin(), 106 STLDeleteContainerPairFirstPointers(decode_buffers_.begin(),
118 decode_buffers_.end()); 107 decode_buffers_.end());
119 decode_buffers_.clear(); 108 decode_buffers_.clear();
120 109
121 // Delete WebRTC input buffers. 110 // Delete WebRTC input buffers.
122 for (std::deque<std::pair<webrtc::EncodedImage, BufferData> >::iterator it = 111 for (std::deque<std::pair<webrtc::EncodedImage, BufferData> >::iterator it =
123 pending_buffers_.begin(); 112 pending_buffers_.begin();
124 it != pending_buffers_.end(); 113 it != pending_buffers_.end();
125 ++it) { 114 ++it) {
126 delete[] it->first._buffer; 115 delete[] it->first._buffer;
127 } 116 }
128 } 117 }
129 118
130 scoped_ptr<RTCVideoDecoder> RTCVideoDecoder::Create( 119 scoped_ptr<RTCVideoDecoder> RTCVideoDecoder::Create(
120 webrtc::VideoCodecType type,
131 const scoped_refptr<media::GpuVideoDecoder::Factories>& factories) { 121 const scoped_refptr<media::GpuVideoDecoder::Factories>& factories) {
132 scoped_ptr<RTCVideoDecoder> decoder(new RTCVideoDecoder(factories)); 122 scoped_ptr<RTCVideoDecoder> decoder;
133 decoder->vda_.reset(factories->CreateVideoDecodeAccelerator( 123 // Convert WebRTC codec type to media codec profile.
134 media::VP8PROFILE_MAIN, decoder.get())); 124 media::VideoCodecProfile profile;
125 switch (type) {
126 case webrtc::kVideoCodecVP8:
127 profile = media::VP8PROFILE_MAIN;
128 break;
129 default:
130 DVLOG(2) << "Video codec not supported:" << type;
131 return decoder.Pass();
132 }
133
134 decoder.reset(new RTCVideoDecoder(factories));
135 decoder->vda_
136 .reset(factories->CreateVideoDecodeAccelerator(profile, decoder.get()));
135 // vda can be NULL if VP8 is not supported. 137 // vda can be NULL if VP8 is not supported.
136 if (decoder->vda_ != NULL) { 138 if (decoder->vda_ != NULL) {
137 decoder->state_ = INITIALIZED; 139 decoder->state_ = INITIALIZED;
138 } else { 140 } else {
139 factories->GetMessageLoop()->DeleteSoon(FROM_HERE, decoder.release()); 141 factories->GetMessageLoop()->DeleteSoon(FROM_HERE, decoder.release());
140 } 142 }
141 return decoder.Pass(); 143 return decoder.Pass();
142 } 144 }
143 145
144 int32_t RTCVideoDecoder::InitDecode(const webrtc::VideoCodec* codecSettings, 146 int32_t RTCVideoDecoder::InitDecode(const webrtc::VideoCodec* codecSettings,
145 int32_t /*numberOfCores*/) { 147 int32_t /*numberOfCores*/) {
146 DVLOG(2) << "InitDecode"; 148 DVLOG(2) << "InitDecode";
147 DCHECK_EQ(codecSettings->codecType, webrtc::kVideoCodecVP8); 149 DCHECK_EQ(codecSettings->codecType, webrtc::kVideoCodecVP8);
148 if (codecSettings->codecSpecific.VP8.feedbackModeOn) { 150 if (codecSettings->codecSpecific.VP8.feedbackModeOn) {
149 LOG(ERROR) << "Feedback mode not supported"; 151 LOG(ERROR) << "Feedback mode not supported";
150 return WEBRTC_VIDEO_CODEC_ERROR; 152 return WEBRTC_VIDEO_CODEC_ERROR;
151 } 153 }
152 154
153 base::AutoLock auto_lock(lock_); 155 base::AutoLock auto_lock(lock_);
154 if (state_ == UNINITIALIZED || state_ == DECODE_ERROR) { 156 if (state_ == UNINITIALIZED || state_ == DECODE_ERROR) {
155 LOG(ERROR) << "VDA is not initialized. state=" << state_; 157 LOG(ERROR) << "VDA is not initialized. state=" << state_;
156 return WEBRTC_VIDEO_CODEC_UNINITIALIZED; 158 return WEBRTC_VIDEO_CODEC_UNINITIALIZED;
157 } 159 }
158 // Create some shared memory if the queue is empty. 160 // Create some shared memory if the queue is empty.
159 if (available_shm_segments_.size() == 0) { 161 if (available_shm_segments_.size() == 0) {
160 // Unretained is safe because the destructor will wait until 162 vda_loop_proxy_->PostTask(FROM_HERE,
161 // |create_shm_thread_| stops. 163 base::Bind(&RTCVideoDecoder::CreateSHM,
162 create_shm_thread_.message_loop_proxy() 164 weak_this_,
163 ->PostTask(FROM_HERE, 165 kMaxInFlightDecodes,
164 base::Bind(&RTCVideoDecoder::CreateSHM, 166 kSharedMemorySegmentBytes));
165 base::Unretained(this),
166 kMaxInFlightDecodes,
167 kSharedMemorySegmentBytes));
168 } 167 }
169 return WEBRTC_VIDEO_CODEC_OK; 168 return WEBRTC_VIDEO_CODEC_OK;
170 } 169 }
171 170
172 int32_t RTCVideoDecoder::Decode( 171 int32_t RTCVideoDecoder::Decode(
173 const webrtc::EncodedImage& inputImage, 172 const webrtc::EncodedImage& inputImage,
174 bool missingFrames, 173 bool missingFrames,
175 const webrtc::RTPFragmentationHeader* /*fragmentation*/, 174 const webrtc::RTPFragmentationHeader* /*fragmentation*/,
176 const webrtc::CodecSpecificInfo* /*codecSpecificInfo*/, 175 const webrtc::CodecSpecificInfo* /*codecSpecificInfo*/,
177 int64_t /*renderTimeMs*/) { 176 int64_t /*renderTimeMs*/) {
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
224 int32_t RTCVideoDecoder::RegisterDecodeCompleteCallback( 223 int32_t RTCVideoDecoder::RegisterDecodeCompleteCallback(
225 webrtc::DecodedImageCallback* callback) { 224 webrtc::DecodedImageCallback* callback) {
226 DVLOG(2) << "RegisterDecodeCompleteCallback"; 225 DVLOG(2) << "RegisterDecodeCompleteCallback";
227 base::AutoLock auto_lock(lock_); 226 base::AutoLock auto_lock(lock_);
228 decode_complete_callback_ = callback; 227 decode_complete_callback_ = callback;
229 return WEBRTC_VIDEO_CODEC_OK; 228 return WEBRTC_VIDEO_CODEC_OK;
230 } 229 }
231 230
232 int32_t RTCVideoDecoder::Release() { 231 int32_t RTCVideoDecoder::Release() {
233 DVLOG(2) << "Release"; 232 DVLOG(2) << "Release";
234 // Do not destroy VDA because the decoder will be recycled by 233 base::AutoLock auto_lock(lock_);
235 // RTCVideoDecoderFactory. Just reset VDA. 234 if (state_ == UNINITIALIZED) {
236 return Reset(); 235 LOG(ERROR) << "Decoder not initialized.";
236 return WEBRTC_VIDEO_CODEC_UNINITIALIZED;
237 }
238 if (next_bitstream_buffer_id_ != 0)
239 reset_bitstream_buffer_id_ = next_bitstream_buffer_id_ - 1;
240 else
241 reset_bitstream_buffer_id_ = ID_LAST;
242 factories_->Abort();
243 vda_loop_proxy_->PostTask(
244 FROM_HERE, base::Bind(&RTCVideoDecoder::DestroyVDA, weak_this_));
245 return WEBRTC_VIDEO_CODEC_OK;
237 } 246 }
238 247
239 int32_t RTCVideoDecoder::Reset() { 248 int32_t RTCVideoDecoder::Reset() {
240 DVLOG(2) << "Reset"; 249 DVLOG(2) << "Reset";
241 base::AutoLock auto_lock(lock_); 250 base::AutoLock auto_lock(lock_);
242 if (state_ == UNINITIALIZED) { 251 if (state_ == UNINITIALIZED) {
243 LOG(ERROR) << "Decoder not initialized."; 252 LOG(ERROR) << "Decoder not initialized.";
244 return WEBRTC_VIDEO_CODEC_UNINITIALIZED; 253 return WEBRTC_VIDEO_CODEC_UNINITIALIZED;
245 } 254 }
246 if (next_bitstream_buffer_id_ != 0) 255 if (next_bitstream_buffer_id_ != 0)
(...skipping 406 matching lines...) Expand 10 before | Expand all | Expand 10 after
653 662
654 scoped_ptr<RTCVideoDecoder::SHMBuffer> RTCVideoDecoder::GetSHM_Locked( 663 scoped_ptr<RTCVideoDecoder::SHMBuffer> RTCVideoDecoder::GetSHM_Locked(
655 size_t min_size) { 664 size_t min_size) {
656 // Reuse a SHM if possible. 665 // Reuse a SHM if possible.
657 SHMBuffer* ret = NULL; 666 SHMBuffer* ret = NULL;
658 if (!available_shm_segments_.empty() && 667 if (!available_shm_segments_.empty() &&
659 available_shm_segments_.back()->size >= min_size) { 668 available_shm_segments_.back()->size >= min_size) {
660 ret = available_shm_segments_.back(); 669 ret = available_shm_segments_.back();
661 available_shm_segments_.pop_back(); 670 available_shm_segments_.pop_back();
662 } 671 }
663 // Post to the child thread to create shared memory if SHM cannot be reused 672 // Post to vda thread to create shared memory if SHM cannot be reused or the
664 // or the queue is almost empty. 673 // queue is almost empty.
665 if (num_shm_buffers_ < kMaxNumSharedMemorySegments && 674 if (num_shm_buffers_ < kMaxNumSharedMemorySegments &&
666 (ret == NULL || available_shm_segments_.size() <= 1)) { 675 (ret == NULL || available_shm_segments_.size() <= 1)) {
667 create_shm_thread_.message_loop_proxy()->PostTask( 676 vda_loop_proxy_->PostTask(
668 FROM_HERE, 677 FROM_HERE,
669 // Unretained is safe because the destructor will wait until 678 base::Bind(&RTCVideoDecoder::CreateSHM, weak_this_, 1, min_size));
670 // |create_shm_thread_| stops.
671 base::Bind(
672 &RTCVideoDecoder::CreateSHM, base::Unretained(this), 1, min_size));
673 } 679 }
674 return scoped_ptr<SHMBuffer>(ret); 680 return scoped_ptr<SHMBuffer>(ret);
675 } 681 }
676 682
677 void RTCVideoDecoder::PutSHM_Locked(scoped_ptr<SHMBuffer> shm_buffer) { 683 void RTCVideoDecoder::PutSHM_Locked(scoped_ptr<SHMBuffer> shm_buffer) {
678 available_shm_segments_.push_back(shm_buffer.release()); 684 available_shm_segments_.push_back(shm_buffer.release());
679 } 685 }
680 686
681 void RTCVideoDecoder::CreateSHM(int number, size_t min_size) { 687 void RTCVideoDecoder::CreateSHM(int number, size_t min_size) {
682 DCHECK(create_shm_thread_.message_loop_proxy()->BelongsToCurrentThread()); 688 DCHECK(vda_loop_proxy_->BelongsToCurrentThread());
683 DVLOG(2) << "CreateSHM. size=" << min_size; 689 DVLOG(2) << "CreateSHM. size=" << min_size;
684 int number_to_allocate; 690 int number_to_allocate;
685 { 691 {
686 base::AutoLock auto_lock(lock_); 692 base::AutoLock auto_lock(lock_);
687 number_to_allocate = 693 number_to_allocate =
688 std::min(kMaxNumSharedMemorySegments - num_shm_buffers_, number); 694 std::min(kMaxNumSharedMemorySegments - num_shm_buffers_, number);
689 } 695 }
690 size_t size_to_allocate = std::max(min_size, kSharedMemorySegmentBytes); 696 size_t size_to_allocate = std::max(min_size, kSharedMemorySegmentBytes);
691 for (int i = 0; i < number_to_allocate; i++) { 697 for (int i = 0; i < number_to_allocate; i++) {
692 base::SharedMemory* shm = factories_->CreateSharedMemory(size_to_allocate); 698 base::SharedMemory* shm = factories_->CreateSharedMemory(size_to_allocate);
693 if (shm != NULL) { 699 if (shm != NULL) {
694 base::AutoLock auto_lock(lock_); 700 base::AutoLock auto_lock(lock_);
695 num_shm_buffers_++; 701 num_shm_buffers_++;
696 PutSHM_Locked( 702 PutSHM_Locked(
697 scoped_ptr<SHMBuffer>(new SHMBuffer(shm, size_to_allocate))); 703 scoped_ptr<SHMBuffer>(new SHMBuffer(shm, size_to_allocate)));
698 // Kick off the decoding.
699 vda_loop_proxy_->PostTask(
700 FROM_HERE,
701 base::Bind(&RTCVideoDecoder::RequestBufferDecode, weak_this_));
702 } 704 }
703 } 705 }
706 // Kick off the decoding.
707 RequestBufferDecode();
704 } 708 }
705 709
706 void RTCVideoDecoder::RecordBufferData(const BufferData& buffer_data) { 710 void RTCVideoDecoder::RecordBufferData(const BufferData& buffer_data) {
707 input_buffer_data_.push_front(buffer_data); 711 input_buffer_data_.push_front(buffer_data);
708 // Why this value? Because why not. avformat.h:MAX_REORDER_DELAY is 16, but 712 // Why this value? Because why not. avformat.h:MAX_REORDER_DELAY is 16, but
709 // that's too small for some pathological B-frame test videos. The cost of 713 // that's too small for some pathological B-frame test videos. The cost of
710 // using too-high a value is low (192 bits per extra slot). 714 // using too-high a value is low (192 bits per extra slot).
711 static const size_t kMaxInputBufferDataSize = 128; 715 static const size_t kMaxInputBufferDataSize = 128;
712 // Pop from the back of the list, because that's the oldest and least likely 716 // Pop from the back of the list, because that's the oldest and least likely
713 // to be useful in the future data. 717 // to be useful in the future data.
(...skipping 13 matching lines...) Expand all
727 continue; 731 continue;
728 *timestamp = it->timestamp; 732 *timestamp = it->timestamp;
729 *width = it->width; 733 *width = it->width;
730 *height = it->height; 734 *height = it->height;
731 return; 735 return;
732 } 736 }
733 NOTREACHED() << "Missing bitstream buffer id: " << bitstream_buffer_id; 737 NOTREACHED() << "Missing bitstream buffer id: " << bitstream_buffer_id;
734 } 738 }
735 739
736 } // namespace content 740 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698