OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "media/cast/video_sender/video_encoder.h" | 5 #include "media/cast/video_sender/video_encoder.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | |
9 #include "base/callback.h" | |
10 #include "base/logging.h" | 8 #include "base/logging.h" |
11 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
12 #include "media/base/video_frame.h" | 10 #include "media/base/video_frame.h" |
13 #include "media/cast/cast_defines.h" | 11 #include "media/cast/cast_defines.h" |
14 #include "media/cast/video_sender/video_encoder_impl.h" | 12 #include "media/cast/video_sender/video_encoder_impl.h" |
15 | 13 |
16 namespace media { | 14 namespace media { |
17 namespace cast { | 15 namespace cast { |
18 | 16 |
19 namespace { | |
20 | |
21 typedef base::Callback<void(Vp8Encoder*)> PassEncoderCallback; | |
22 | |
23 void LogFrameEncodedEvent(const base::TimeTicks& now, | 17 void LogFrameEncodedEvent(const base::TimeTicks& now, |
24 scoped_refptr<CastEnvironment> cast_environment, | 18 CastEnvironment* const cast_environment, |
25 const base::TimeTicks& capture_time) { | 19 const base::TimeTicks& capture_time) { |
26 DCHECK(cast_environment->CurrentlyOn(CastEnvironment::MAIN)); | |
27 cast_environment->Logging()->InsertFrameEvent(now, kVideoFrameEncoded, | 20 cast_environment->Logging()->InsertFrameEvent(now, kVideoFrameEncoded, |
28 GetVideoRtpTimestamp(capture_time), kFrameIdUnknown); | 21 GetVideoRtpTimestamp(capture_time), kFrameIdUnknown); |
29 } | 22 } |
30 | 23 |
31 void InitializeVp8EncoderOnEncoderThread( | |
32 const scoped_refptr<CastEnvironment>& environment, | |
33 Vp8Encoder* vp8_encoder) { | |
34 DCHECK(environment->CurrentlyOn(CastEnvironment::VIDEO_ENCODER)); | |
35 vp8_encoder->Initialize(); | |
36 } | |
37 | |
38 void EncodeVideoFrameOnEncoderThread( | |
39 scoped_refptr<CastEnvironment> environment, | |
40 Vp8Encoder* vp8_encoder, | |
41 const scoped_refptr<media::VideoFrame>& video_frame, | |
42 const base::TimeTicks& capture_time, | |
43 const VideoEncoderImpl::CodecDynamicConfig& dynamic_config, | |
44 const VideoEncoderImpl::FrameEncodedCallback& frame_encoded_callback) { | |
45 DCHECK(environment->CurrentlyOn(CastEnvironment::VIDEO_ENCODER)); | |
46 if (dynamic_config.key_frame_requested) { | |
47 vp8_encoder->GenerateKeyFrame(); | |
48 } | |
49 vp8_encoder->LatestFrameIdToReference( | |
50 dynamic_config.latest_frame_id_to_reference); | |
51 vp8_encoder->UpdateRates(dynamic_config.bit_rate); | |
52 | |
53 scoped_ptr<transport::EncodedVideoFrame> encoded_frame( | |
54 new transport::EncodedVideoFrame()); | |
55 bool retval = vp8_encoder->Encode(video_frame, encoded_frame.get()); | |
56 | |
57 base::TimeTicks now = environment->Clock()->NowTicks(); | |
58 environment->PostTask( | |
59 CastEnvironment::MAIN, FROM_HERE, | |
60 base::Bind(LogFrameEncodedEvent, now, environment, capture_time)); | |
61 | |
62 if (!retval) { | |
63 VLOG(1) << "Encoding failed"; | |
64 return; | |
65 } | |
66 if (encoded_frame->data.size() <= 0) { | |
67 VLOG(1) << "Encoding resulted in an empty frame"; | |
68 return; | |
69 } | |
70 environment->PostTask( | |
71 CastEnvironment::MAIN, FROM_HERE, | |
72 base::Bind( | |
73 frame_encoded_callback, | |
74 base::Passed(&encoded_frame), | |
75 capture_time)); | |
76 } | |
77 | |
78 } // namespace | |
79 | |
80 VideoEncoderImpl::VideoEncoderImpl( | 24 VideoEncoderImpl::VideoEncoderImpl( |
81 scoped_refptr<CastEnvironment> cast_environment, | 25 scoped_refptr<CastEnvironment> cast_environment, |
82 const VideoSenderConfig& video_config, | 26 const VideoSenderConfig& video_config, |
83 uint8 max_unacked_frames) | 27 uint8 max_unacked_frames) |
84 : video_config_(video_config), | 28 : video_config_(video_config), |
85 cast_environment_(cast_environment), | 29 cast_environment_(cast_environment), |
86 skip_next_frame_(false), | 30 skip_next_frame_(false), |
87 skip_count_(0) { | 31 skip_count_(0) { |
88 if (video_config.codec == transport::kVp8) { | 32 if (video_config.codec == transport::kVp8) { |
89 vp8_encoder_.reset(new Vp8Encoder(video_config, max_unacked_frames)); | 33 vp8_encoder_.reset(new Vp8Encoder(video_config, max_unacked_frames)); |
90 cast_environment_->PostTask( | |
91 CastEnvironment::VIDEO_ENCODER, FROM_HERE, | |
92 base::Bind( | |
93 &InitializeVp8EncoderOnEncoderThread, | |
94 cast_environment, | |
95 vp8_encoder_.get())); | |
96 } else { | 34 } else { |
97 DCHECK(false) << "Invalid config"; // Codec not supported. | 35 DCHECK(false) << "Invalid config"; // Codec not supported. |
98 } | 36 } |
99 | 37 |
100 dynamic_config_.key_frame_requested = false; | 38 dynamic_config_.key_frame_requested = false; |
101 dynamic_config_.latest_frame_id_to_reference = kStartFrameId; | 39 dynamic_config_.latest_frame_id_to_reference = kStartFrameId; |
102 dynamic_config_.bit_rate = video_config.start_bitrate; | 40 dynamic_config_.bit_rate = video_config.start_bitrate; |
103 } | 41 } |
104 | 42 |
105 VideoEncoderImpl::~VideoEncoderImpl() { | 43 VideoEncoderImpl::~VideoEncoderImpl() {} |
106 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); | |
107 if (vp8_encoder_) { | |
108 cast_environment_->PostTask( | |
109 CastEnvironment::VIDEO_ENCODER, FROM_HERE, | |
110 base::Bind(&base::DeletePointer<Vp8Encoder>, vp8_encoder_.release())); | |
111 } | |
112 } | |
113 | 44 |
114 bool VideoEncoderImpl::EncodeVideoFrame( | 45 bool VideoEncoderImpl::EncodeVideoFrame( |
115 const scoped_refptr<media::VideoFrame>& video_frame, | 46 const scoped_refptr<media::VideoFrame>& video_frame, |
116 const base::TimeTicks& capture_time, | 47 const base::TimeTicks& capture_time, |
117 const FrameEncodedCallback& frame_encoded_callback) { | 48 const FrameEncodedCallback& frame_encoded_callback) { |
118 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); | 49 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); |
119 if (video_config_.codec != transport::kVp8) return false; | 50 if (video_config_.codec != transport::kVp8) return false; |
120 | 51 |
121 if (skip_next_frame_) { | 52 if (skip_next_frame_) { |
122 ++skip_count_; | 53 ++skip_count_; |
123 skip_next_frame_ = false; | 54 skip_next_frame_ = false; |
124 VLOG(1) << "Skip encoding frame"; | 55 VLOG(1) << "Skip encoding frame"; |
125 return false; | 56 return false; |
126 } | 57 } |
127 | 58 |
128 base::TimeTicks now = cast_environment_->Clock()->NowTicks(); | 59 base::TimeTicks now = cast_environment_->Clock()->NowTicks(); |
129 cast_environment_->Logging()->InsertFrameEvent(now, kVideoFrameSentToEncoder, | 60 cast_environment_->Logging()->InsertFrameEvent(now, kVideoFrameSentToEncoder, |
130 GetVideoRtpTimestamp(capture_time), kFrameIdUnknown); | 61 GetVideoRtpTimestamp(capture_time), kFrameIdUnknown); |
131 cast_environment_->PostTask(CastEnvironment::VIDEO_ENCODER, FROM_HERE, | 62 cast_environment_->PostTask(CastEnvironment::VIDEO_ENCODER, FROM_HERE, |
132 base::Bind(&EncodeVideoFrameOnEncoderThread, | 63 base::Bind(&VideoEncoderImpl::EncodeVideoFrameEncoderThread, |
133 cast_environment_, | 64 base::Unretained(this), video_frame, capture_time, |
134 vp8_encoder_.get(), | 65 dynamic_config_, frame_encoded_callback)); |
135 video_frame, | |
136 capture_time, | |
137 dynamic_config_, | |
138 frame_encoded_callback)); | |
139 | 66 |
140 dynamic_config_.key_frame_requested = false; | 67 dynamic_config_.key_frame_requested = false; |
141 return true; | 68 return true; |
142 } | 69 } |
143 | 70 |
| 71 void VideoEncoderImpl::EncodeVideoFrameEncoderThread( |
| 72 const scoped_refptr<media::VideoFrame>& video_frame, |
| 73 const base::TimeTicks& capture_time, |
| 74 const CodecDynamicConfig& dynamic_config, |
| 75 const FrameEncodedCallback& frame_encoded_callback) { |
| 76 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::VIDEO_ENCODER)); |
| 77 if (dynamic_config.key_frame_requested) { |
| 78 vp8_encoder_->GenerateKeyFrame(); |
| 79 } |
| 80 vp8_encoder_->LatestFrameIdToReference( |
| 81 dynamic_config.latest_frame_id_to_reference); |
| 82 vp8_encoder_->UpdateRates(dynamic_config.bit_rate); |
| 83 |
| 84 scoped_ptr<transport::EncodedVideoFrame> encoded_frame( |
| 85 new transport::EncodedVideoFrame()); |
| 86 bool retval = vp8_encoder_->Encode(video_frame, encoded_frame.get()); |
| 87 |
| 88 base::TimeTicks now = cast_environment_->Clock()->NowTicks(); |
| 89 cast_environment_->PostTask(CastEnvironment::MAIN, FROM_HERE, |
| 90 base::Bind(LogFrameEncodedEvent, now, cast_environment_, capture_time)); |
| 91 |
| 92 if (!retval) { |
| 93 VLOG(1) << "Encoding failed"; |
| 94 return; |
| 95 } |
| 96 if (encoded_frame->data.size() <= 0) { |
| 97 VLOG(1) << "Encoding resulted in an empty frame"; |
| 98 return; |
| 99 } |
| 100 cast_environment_->PostTask(CastEnvironment::MAIN, FROM_HERE, |
| 101 base::Bind(frame_encoded_callback, |
| 102 base::Passed(&encoded_frame), capture_time)); |
| 103 } |
| 104 |
144 // Inform the encoder about the new target bit rate. | 105 // Inform the encoder about the new target bit rate. |
145 void VideoEncoderImpl::SetBitRate(int new_bit_rate) { | 106 void VideoEncoderImpl::SetBitRate(int new_bit_rate) { |
146 dynamic_config_.bit_rate = new_bit_rate; | 107 dynamic_config_.bit_rate = new_bit_rate; |
147 } | 108 } |
148 | 109 |
149 // Inform the encoder to not encode the next frame. | 110 // Inform the encoder to not encode the next frame. |
150 void VideoEncoderImpl::SkipNextFrame(bool skip_next_frame) { | 111 void VideoEncoderImpl::SkipNextFrame(bool skip_next_frame) { |
151 skip_next_frame_ = skip_next_frame; | 112 skip_next_frame_ = skip_next_frame; |
152 } | 113 } |
153 | 114 |
154 // Inform the encoder to encode the next frame as a key frame. | 115 // Inform the encoder to encode the next frame as a key frame. |
155 void VideoEncoderImpl::GenerateKeyFrame() { | 116 void VideoEncoderImpl::GenerateKeyFrame() { |
156 dynamic_config_.key_frame_requested = true; | 117 dynamic_config_.key_frame_requested = true; |
157 } | 118 } |
158 | 119 |
159 // Inform the encoder to only reference frames older or equal to frame_id; | 120 // Inform the encoder to only reference frames older or equal to frame_id; |
160 void VideoEncoderImpl::LatestFrameIdToReference(uint32 frame_id) { | 121 void VideoEncoderImpl::LatestFrameIdToReference(uint32 frame_id) { |
161 dynamic_config_.latest_frame_id_to_reference = frame_id; | 122 dynamic_config_.latest_frame_id_to_reference = frame_id; |
162 } | 123 } |
163 | 124 |
164 int VideoEncoderImpl::NumberOfSkippedFrames() const { | 125 int VideoEncoderImpl::NumberOfSkippedFrames() const { |
165 return skip_count_; | 126 return skip_count_; |
166 } | 127 } |
167 | 128 |
168 } // namespace cast | 129 } // namespace cast |
169 } // namespace media | 130 } // namespace media |
OLD | NEW |