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