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

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

Issue 27420004: Remove threading from RendererGpuVideoAcceleratorFactories (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@dthread
Patch Set: 51676bf4 Initial. Created 7 years, 2 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_encoder.h" 5 #include "content/renderer/media/rtc_video_encoder.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/location.h" 8 #include "base/location.h"
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "base/memory/scoped_vector.h" 10 #include "base/memory/scoped_vector.h"
11 #include "base/message_loop/message_loop_proxy.h" 11 #include "base/message_loop/message_loop_proxy.h"
12 #include "base/synchronization/waitable_event.h" 12 #include "base/synchronization/waitable_event.h"
13 #include "content/renderer/media/renderer_gpu_video_accelerator_factories.h"
14 #include "media/base/bitstream_buffer.h" 13 #include "media/base/bitstream_buffer.h"
15 #include "media/base/video_frame.h" 14 #include "media/base/video_frame.h"
16 #include "media/base/video_util.h" 15 #include "media/base/video_util.h"
17 #include "media/filters/gpu_video_accelerator_factories.h" 16 #include "media/filters/gpu_video_accelerator_factories.h"
18 #include "media/video/video_encode_accelerator.h" 17 #include "media/video/video_encode_accelerator.h"
19 #include "third_party/webrtc/system_wrappers/interface/tick_util.h" 18 #include "third_party/webrtc/system_wrappers/interface/tick_util.h"
20 19
21 #define NOTIFY_ERROR(x) \ 20 #define NOTIFY_ERROR(x) \
22 do { \ 21 do { \
23 DLOG(ERROR) << "calling NotifyError(): " << x; \ 22 DLOG(ERROR) << "calling NotifyError(): " << x; \
(...skipping 10 matching lines...) Expand all
34 // 33 //
35 // This class separates state related to the thread that RTCVideoEncoder 34 // This class separates state related to the thread that RTCVideoEncoder
36 // operates on (presently the libjingle worker thread) from the thread that 35 // operates on (presently the libjingle worker thread) from the thread that
37 // |gpu_factories_| provides for accelerator operations (presently the media 36 // |gpu_factories_| provides for accelerator operations (presently the media
38 // thread). The RTCVideoEncoder class can be deleted directly by WebRTC, while 37 // thread). The RTCVideoEncoder class can be deleted directly by WebRTC, while
39 // RTCVideoEncoder::Impl stays around long enough to properly shut down the VEA. 38 // RTCVideoEncoder::Impl stays around long enough to properly shut down the VEA.
40 class RTCVideoEncoder::Impl 39 class RTCVideoEncoder::Impl
41 : public media::VideoEncodeAccelerator::Client, 40 : public media::VideoEncodeAccelerator::Client,
42 public base::RefCountedThreadSafe<RTCVideoEncoder::Impl> { 41 public base::RefCountedThreadSafe<RTCVideoEncoder::Impl> {
43 public: 42 public:
44 Impl( 43 Impl(const base::WeakPtr<RTCVideoEncoder>& weak_encoder,
45 const base::WeakPtr<RTCVideoEncoder>& weak_encoder, 44 const scoped_refptr<media::GpuVideoAcceleratorFactories>& gpu_factories);
46 const scoped_refptr<RendererGpuVideoAcceleratorFactories>& gpu_factories);
47 45
48 // Create the VEA and call Initialize() on it. Called once per instantiation, 46 // Create the VEA and call Initialize() on it. Called once per instantiation,
49 // and then the instance is bound forevermore to whichever thread made the 47 // and then the instance is bound forevermore to whichever thread made the
50 // call. 48 // call.
51 // RTCVideoEncoder expects to be able to call this function synchronously from 49 // RTCVideoEncoder expects to be able to call this function synchronously from
52 // its own thread, hence the |async_waiter| and |async_retval| arguments. 50 // its own thread, hence the |async_waiter| and |async_retval| arguments.
53 void CreateAndInitializeVEA(const gfx::Size& input_visible_size, 51 void CreateAndInitializeVEA(const gfx::Size& input_visible_size,
54 uint32 bitrate, 52 uint32 bitrate,
55 media::VideoCodecProfile profile, 53 media::VideoCodecProfile profile,
56 base::WaitableEvent* async_waiter, 54 base::WaitableEvent* async_waiter,
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
111 base::ThreadChecker thread_checker_; 109 base::ThreadChecker thread_checker_;
112 110
113 // Weak pointer to the parent RTCVideoEncoder, for posting back VEA::Client 111 // Weak pointer to the parent RTCVideoEncoder, for posting back VEA::Client
114 // notifications. 112 // notifications.
115 const base::WeakPtr<RTCVideoEncoder> weak_encoder_; 113 const base::WeakPtr<RTCVideoEncoder> weak_encoder_;
116 114
117 // The message loop on which to post callbacks to |weak_encoder_|. 115 // The message loop on which to post callbacks to |weak_encoder_|.
118 const scoped_refptr<base::MessageLoopProxy> encoder_message_loop_proxy_; 116 const scoped_refptr<base::MessageLoopProxy> encoder_message_loop_proxy_;
119 117
120 // Factory for creating VEAs, shared memory buffers, etc. 118 // Factory for creating VEAs, shared memory buffers, etc.
121 const scoped_refptr<RendererGpuVideoAcceleratorFactories> gpu_factories_; 119 const scoped_refptr<media::GpuVideoAcceleratorFactories> gpu_factories_;
122 120
123 // webrtc::VideoEncoder expects InitEncode() and Encode() to be synchronous. 121 // webrtc::VideoEncoder expects InitEncode() and Encode() to be synchronous.
124 // Do this by waiting on the |async_waiter_| and returning the return value in 122 // Do this by waiting on the |async_waiter_| and returning the return value in
125 // |async_retval_| when initialization completes, encoding completes, or 123 // |async_retval_| when initialization completes, encoding completes, or
126 // an error occurs. 124 // an error occurs.
127 base::WaitableEvent* async_waiter_; 125 base::WaitableEvent* async_waiter_;
128 int32_t* async_retval_; 126 int32_t* async_retval_;
129 127
130 // The underlying VEA to perform encoding on. 128 // The underlying VEA to perform encoding on.
131 scoped_ptr<media::VideoEncodeAccelerator> video_encoder_; 129 scoped_ptr<media::VideoEncodeAccelerator> video_encoder_;
(...skipping 15 matching lines...) Expand all
147 145
148 // Input buffers ready to be filled with input from Encode(). As a LIFO since 146 // Input buffers ready to be filled with input from Encode(). As a LIFO since
149 // we don't care about ordering. 147 // we don't care about ordering.
150 std::vector<int> input_buffers_free_; 148 std::vector<int> input_buffers_free_;
151 149
152 DISALLOW_COPY_AND_ASSIGN(Impl); 150 DISALLOW_COPY_AND_ASSIGN(Impl);
153 }; 151 };
154 152
155 RTCVideoEncoder::Impl::Impl( 153 RTCVideoEncoder::Impl::Impl(
156 const base::WeakPtr<RTCVideoEncoder>& weak_encoder, 154 const base::WeakPtr<RTCVideoEncoder>& weak_encoder,
157 const scoped_refptr<RendererGpuVideoAcceleratorFactories>& gpu_factories) 155 const scoped_refptr<media::GpuVideoAcceleratorFactories>& gpu_factories)
158 : weak_encoder_(weak_encoder), 156 : weak_encoder_(weak_encoder),
159 encoder_message_loop_proxy_(base::MessageLoopProxy::current()), 157 encoder_message_loop_proxy_(base::MessageLoopProxy::current()),
160 gpu_factories_(gpu_factories), 158 gpu_factories_(gpu_factories),
161 async_waiter_(NULL), 159 async_waiter_(NULL),
162 async_retval_(NULL), 160 async_retval_(NULL),
163 input_next_frame_(NULL), 161 input_next_frame_(NULL),
164 input_next_frame_keyframe_(false) { 162 input_next_frame_keyframe_(false) {
165 thread_checker_.DetachFromThread(); 163 thread_checker_.DetachFromThread();
166 } 164 }
167 165
(...skipping 295 matching lines...) Expand 10 before | Expand all | Expand 10 after
463 461
464 //////////////////////////////////////////////////////////////////////////////// 462 ////////////////////////////////////////////////////////////////////////////////
465 // 463 //
466 // RTCVideoEncoder 464 // RTCVideoEncoder
467 // 465 //
468 //////////////////////////////////////////////////////////////////////////////// 466 ////////////////////////////////////////////////////////////////////////////////
469 467
470 RTCVideoEncoder::RTCVideoEncoder( 468 RTCVideoEncoder::RTCVideoEncoder(
471 webrtc::VideoCodecType type, 469 webrtc::VideoCodecType type,
472 media::VideoCodecProfile profile, 470 media::VideoCodecProfile profile,
473 const scoped_refptr<RendererGpuVideoAcceleratorFactories>& gpu_factories) 471 const scoped_refptr<media::GpuVideoAcceleratorFactories>& gpu_factories)
474 : video_codec_type_(type), 472 : video_codec_type_(type),
475 video_codec_profile_(profile), 473 video_codec_profile_(profile),
476 gpu_factories_(gpu_factories), 474 gpu_factories_(gpu_factories),
477 weak_this_factory_(this), 475 weak_this_factory_(this),
478 encoded_image_callback_(NULL), 476 encoded_image_callback_(NULL),
479 impl_status_(WEBRTC_VIDEO_CODEC_UNINITIALIZED) { 477 impl_status_(WEBRTC_VIDEO_CODEC_UNINITIALIZED) {
480 DVLOG(1) << "RTCVideoEncoder(): profile=" << profile; 478 DVLOG(1) << "RTCVideoEncoder(): profile=" << profile;
481 } 479 }
482 480
483 RTCVideoEncoder::~RTCVideoEncoder() { 481 RTCVideoEncoder::~RTCVideoEncoder() {
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
554 } 552 }
555 553
556 encoded_image_callback_ = callback; 554 encoded_image_callback_ = callback;
557 return WEBRTC_VIDEO_CODEC_OK; 555 return WEBRTC_VIDEO_CODEC_OK;
558 } 556 }
559 557
560 int32_t RTCVideoEncoder::Release() { 558 int32_t RTCVideoEncoder::Release() {
561 DVLOG(3) << "Release()"; 559 DVLOG(3) << "Release()";
562 DCHECK(thread_checker_.CalledOnValidThread()); 560 DCHECK(thread_checker_.CalledOnValidThread());
563 561
564 // Reset the gpu_factory_, in case we reuse this encoder.
565 gpu_factories_->Abort();
566 gpu_factories_ = gpu_factories_->Clone();
567 if (impl_) { 562 if (impl_) {
568 gpu_factories_->GetMessageLoop()->PostTask( 563 gpu_factories_->GetMessageLoop()->PostTask(
569 FROM_HERE, base::Bind(&RTCVideoEncoder::Impl::Destroy, impl_)); 564 FROM_HERE, base::Bind(&RTCVideoEncoder::Impl::Destroy, impl_));
570 impl_ = NULL; 565 impl_ = NULL;
571 weak_this_factory_.InvalidateWeakPtrs(); 566 weak_this_factory_.InvalidateWeakPtrs();
572 impl_status_ = WEBRTC_VIDEO_CODEC_UNINITIALIZED; 567 impl_status_ = WEBRTC_VIDEO_CODEC_UNINITIALIZED;
573 } 568 }
574 return WEBRTC_VIDEO_CODEC_OK; 569 return WEBRTC_VIDEO_CODEC_OK;
575 } 570 }
576 571
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
646 DCHECK(thread_checker_.CalledOnValidThread()); 641 DCHECK(thread_checker_.CalledOnValidThread());
647 DVLOG(1) << "NotifyError(): error=" << error; 642 DVLOG(1) << "NotifyError(): error=" << error;
648 643
649 impl_status_ = error; 644 impl_status_ = error;
650 gpu_factories_->GetMessageLoop()->PostTask( 645 gpu_factories_->GetMessageLoop()->PostTask(
651 FROM_HERE, base::Bind(&RTCVideoEncoder::Impl::Destroy, impl_)); 646 FROM_HERE, base::Bind(&RTCVideoEncoder::Impl::Destroy, impl_));
652 impl_ = NULL; 647 impl_ = NULL;
653 } 648 }
654 649
655 } // namespace content 650 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698