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
3 // found in the LICENSE file.
5 #ifndef CONTENT_RENDERER_MEDIA_RTC_VIDEO_ENCODER_H_
6 #define CONTENT_RENDERER_MEDIA_RTC_VIDEO_ENCODER_H_
10 #include "base/memory/ref_counted.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/memory/weak_ptr.h"
13 #include "base/threading/thread_checker.h"
14 #include "base/time/time.h"
15 #include "content/common/content_export.h"
16 #include "media/base/video_decoder_config.h"
17 #include "third_party/webrtc/modules/video_coding/codecs/interface/video_codec_interface.h"
18 #include "ui/gfx/geometry/size.h"
21 class GpuVideoAcceleratorFactories
;
26 // RTCVideoEncoder uses a media::VideoEncodeAccelerator to implement a
27 // webrtc::VideoEncoder class for WebRTC. Internally, VEA methods are
28 // trampolined to a private RTCVideoEncoder::Impl instance. The Impl class runs
29 // on the worker thread queried from the |gpu_factories_|, which is presently
30 // the media thread. RTCVideoEncoder is sychronized by webrtc::VideoSender.
31 // webrtc::VideoEncoder methods do not run concurrently. RTCVideoEncoder is run
32 // and destroyed on the thread it is constructed on, which is presently the
33 // libjingle worker thread. Encode is run on ViECaptureThread. SetRates and
34 // SetChannelParameters are run on ProcessThread or the libjingle worker thread.
35 // Callbacks from the Impl due to its VEA::Client notifications are posted back
36 // to RTCVideoEncoder on the libjingle worker thread.
37 class CONTENT_EXPORT RTCVideoEncoder
38 : NON_EXPORTED_BASE(public webrtc::VideoEncoder
) {
41 webrtc::VideoCodecType type
,
42 const scoped_refptr
<media::GpuVideoAcceleratorFactories
>& gpu_factories
);
43 ~RTCVideoEncoder() override
;
45 // webrtc::VideoEncoder implementation. Tasks are posted to |impl_| using the
46 // appropriate VEA methods.
47 int32_t InitEncode(const webrtc::VideoCodec
* codec_settings
,
48 int32_t number_of_cores
,
49 size_t max_payload_size
) override
;
51 const webrtc::VideoFrame
& input_image
,
52 const webrtc::CodecSpecificInfo
* codec_specific_info
,
53 const std::vector
<webrtc::VideoFrameType
>* frame_types
) override
;
54 int32_t RegisterEncodeCompleteCallback(
55 webrtc::EncodedImageCallback
* callback
) override
;
56 int32_t Release() override
;
57 int32_t SetChannelParameters(uint32_t packet_loss
, int64_t rtt
) override
;
58 int32_t SetRates(uint32_t new_bit_rate
, uint32_t frame_rate
) override
;
62 friend class RTCVideoEncoder::Impl
;
64 // Return an encoded output buffer to WebRTC.
65 void ReturnEncodedImage(scoped_ptr
<webrtc::EncodedImage
> image
,
66 int32 bitstream_buffer_id
,
69 void NotifyError(int32_t error
);
71 void RecordInitEncodeUMA(int32_t init_retval
,
72 media::VideoCodecProfile profile
);
74 base::ThreadChecker thread_checker_
;
76 // The video codec type, as reported to WebRTC.
77 const webrtc::VideoCodecType video_codec_type_
;
79 // Factory for creating VEAs, shared memory buffers, etc.
80 const scoped_refptr
<media::GpuVideoAcceleratorFactories
> gpu_factories_
;
82 // webrtc::VideoEncoder encode complete callback.
83 webrtc::EncodedImageCallback
* encoded_image_callback_
;
85 // The RTCVideoEncoder::Impl that does all the work.
86 scoped_refptr
<Impl
> impl_
;
88 // We cannot immediately return error conditions to the WebRTC user of this
89 // class, as there is no error callback in the webrtc::VideoEncoder interface.
90 // Instead, we cache an error status here and return it the next time an
91 // interface entry point is called.
94 // Weak pointer factory for posting back VEA::Client notifications to
96 // NOTE: Weak pointers must be invalidated before all other member variables.
97 base::WeakPtrFactory
<RTCVideoEncoder
> weak_factory_
;
99 DISALLOW_COPY_AND_ASSIGN(RTCVideoEncoder
);
102 } // namespace content
104 #endif // CONTENT_RENDERER_MEDIA_RTC_VIDEO_ENCODER_H_