Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / content / renderer / media / rtc_video_encoder.h
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.
4
5 #ifndef CONTENT_RENDERER_MEDIA_RTC_VIDEO_ENCODER_H_
6 #define CONTENT_RENDERER_MEDIA_RTC_VIDEO_ENCODER_H_
7
8 #include <vector>
9
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/size.h"
19
20 namespace base {
21
22 class MessageLoopProxy;
23
24 }  // namespace base
25
26 namespace media {
27
28 class GpuVideoAcceleratorFactories;
29
30 }  // namespace media
31
32 namespace content {
33
34 // RTCVideoEncoder uses a media::VideoEncodeAccelerator to implement a
35 // webrtc::VideoEncoder class for WebRTC.  Internally, VEA methods are
36 // trampolined to a private RTCVideoEncoder::Impl instance.  The Impl class runs
37 // on the worker thread queried from the |gpu_factories_|, which is presently
38 // the media thread.  RTCVideoEncoder is sychronized by webrtc::VideoSender.
39 // webrtc::VideoEncoder methods do not run concurrently. RTCVideoEncoder is run
40 // and destroyed on the thread it is constructed on, which is presently the
41 // libjingle worker thread. Encode is run on ViECaptureThread. SetRates and
42 // SetChannelParameters are run on ProcessThread or the libjingle worker thread.
43 // Callbacks from the Impl due to its VEA::Client notifications are posted back
44 // to RTCVideoEncoder on the libjingle worker thread.
45 class CONTENT_EXPORT RTCVideoEncoder
46     : NON_EXPORTED_BASE(public webrtc::VideoEncoder) {
47  public:
48   RTCVideoEncoder(
49       webrtc::VideoCodecType type,
50       const scoped_refptr<media::GpuVideoAcceleratorFactories>& gpu_factories);
51   ~RTCVideoEncoder() override;
52
53   // webrtc::VideoEncoder implementation.  Tasks are posted to |impl_| using the
54   // appropriate VEA methods.
55   int32_t InitEncode(const webrtc::VideoCodec* codec_settings,
56                      int32_t number_of_cores,
57                      uint32_t max_payload_size) override;
58   int32_t Encode(
59       const webrtc::I420VideoFrame& input_image,
60       const webrtc::CodecSpecificInfo* codec_specific_info,
61       const std::vector<webrtc::VideoFrameType>* frame_types) override;
62   int32_t RegisterEncodeCompleteCallback(
63       webrtc::EncodedImageCallback* callback) override;
64   int32_t Release() override;
65   int32_t SetChannelParameters(uint32_t packet_loss, int rtt) override;
66   int32_t SetRates(uint32_t new_bit_rate, uint32_t frame_rate) override;
67
68  private:
69   class Impl;
70   friend class RTCVideoEncoder::Impl;
71
72   // Return an encoded output buffer to WebRTC.
73   void ReturnEncodedImage(scoped_ptr<webrtc::EncodedImage> image,
74                           int32 bitstream_buffer_id,
75                           uint16 picture_id);
76
77   void NotifyError(int32_t error);
78
79   void RecordInitEncodeUMA(int32_t init_retval,
80                            media::VideoCodecProfile profile);
81
82   base::ThreadChecker thread_checker_;
83
84   // The video codec type, as reported to WebRTC.
85   const webrtc::VideoCodecType video_codec_type_;
86
87   // Factory for creating VEAs, shared memory buffers, etc.
88   scoped_refptr<media::GpuVideoAcceleratorFactories> gpu_factories_;
89
90   // webrtc::VideoEncoder encode complete callback.
91   webrtc::EncodedImageCallback* encoded_image_callback_;
92
93   // The RTCVideoEncoder::Impl that does all the work.
94   scoped_refptr<Impl> impl_;
95
96   // We cannot immediately return error conditions to the WebRTC user of this
97   // class, as there is no error callback in the webrtc::VideoEncoder interface.
98   // Instead, we cache an error status here and return it the next time an
99   // interface entry point is called.
100   int32_t impl_status_;
101
102   // Weak pointer factory for posting back VEA::Client notifications to
103   // RTCVideoEncoder.
104   // NOTE: Weak pointers must be invalidated before all other member variables.
105   base::WeakPtrFactory<RTCVideoEncoder> weak_factory_;
106
107   DISALLOW_COPY_AND_ASSIGN(RTCVideoEncoder);
108 };
109
110 }  // namespace content
111
112 #endif  // CONTENT_RENDERER_MEDIA_RTC_VIDEO_ENCODER_H_