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/sender/video_sender.h" | 5 #include "media/cast/sender/video_sender.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <cstring> | 8 #include <cstring> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/debug/trace_event.h" | 11 #include "base/debug/trace_event.h" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/message_loop/message_loop.h" | 13 #include "base/message_loop/message_loop.h" |
14 #include "media/cast/cast_defines.h" | 14 #include "media/cast/cast_defines.h" |
15 #include "media/cast/net/cast_transport_config.h" | 15 #include "media/cast/net/cast_transport_config.h" |
16 #include "media/cast/sender/external_video_encoder.h" | 16 #include "media/cast/sender/external_video_encoder.h" |
17 #include "media/cast/sender/video_encoder_impl.h" | 17 #include "media/cast/sender/video_encoder_impl.h" |
18 | 18 |
19 namespace media { | 19 namespace media { |
20 namespace cast { | 20 namespace cast { |
21 | 21 |
22 // Note, we use a fixed bitrate value when external video encoder is used. | 22 // Note, we use a fixed bitrate value when external video encoder is used. |
23 // Some hardware encoder shows bad behavior if we set the bitrate too | 23 // Some hardware encoder shows bad behavior if we set the bitrate too |
24 // frequently, e.g. quality drop, not abiding by target bitrate, etc. | 24 // frequently, e.g. quality drop, not abiding by target bitrate, etc. |
25 // See details: crbug.com/392086. | 25 // See details: crbug.com/392086. |
26 VideoSender::VideoSender( | 26 VideoSender::VideoSender( |
27 scoped_refptr<CastEnvironment> cast_environment, | 27 scoped_refptr<CastEnvironment> cast_environment, |
28 const VideoSenderConfig& video_config, | 28 const VideoSenderConfig& video_config, |
| 29 const CastInitializationCallback& initialization_cb, |
29 const CreateVideoEncodeAcceleratorCallback& create_vea_cb, | 30 const CreateVideoEncodeAcceleratorCallback& create_vea_cb, |
30 const CreateVideoEncodeMemoryCallback& create_video_encode_mem_cb, | 31 const CreateVideoEncodeMemoryCallback& create_video_encode_mem_cb, |
31 CastTransportSender* const transport_sender) | 32 CastTransportSender* const transport_sender) |
32 : FrameSender( | 33 : FrameSender( |
33 cast_environment, | 34 cast_environment, |
34 false, | 35 false, |
35 transport_sender, | 36 transport_sender, |
36 base::TimeDelta::FromMilliseconds(video_config.rtcp_interval), | 37 base::TimeDelta::FromMilliseconds(video_config.rtcp_interval), |
37 kVideoFrequency, | 38 kVideoFrequency, |
38 video_config.ssrc, | 39 video_config.ssrc, |
39 video_config.max_frame_rate, | 40 video_config.max_frame_rate, |
40 video_config.target_playout_delay, | 41 video_config.target_playout_delay, |
41 NewFixedCongestionControl( | 42 NewFixedCongestionControl( |
42 (video_config.min_bitrate + video_config.max_bitrate) / 2)), | 43 (video_config.min_bitrate + video_config.max_bitrate) / 2)), |
43 frames_in_encoder_(0), | 44 frames_in_encoder_(0), |
44 last_bitrate_(0), | 45 last_bitrate_(0), |
45 weak_factory_(this) { | 46 weak_factory_(this) { |
46 cast_initialization_status_ = STATUS_VIDEO_UNINITIALIZED; | 47 cast_initialization_status_ = STATUS_VIDEO_UNINITIALIZED; |
47 VLOG(1) << "max_unacked_frames is " << max_unacked_frames_ | 48 VLOG(1) << "max_unacked_frames is " << max_unacked_frames_ |
48 << " for target_playout_delay=" | 49 << " for target_playout_delay=" |
49 << target_playout_delay_.InMilliseconds() << " ms" | 50 << target_playout_delay_.InMilliseconds() << " ms" |
50 << " and max_frame_rate=" << video_config.max_frame_rate; | 51 << " and max_frame_rate=" << video_config.max_frame_rate; |
51 DCHECK_GT(max_unacked_frames_, 0); | 52 DCHECK_GT(max_unacked_frames_, 0); |
52 | 53 |
53 if (video_config.use_external_encoder) { | 54 if (video_config.use_external_encoder) { |
54 video_encoder_.reset(new ExternalVideoEncoder(cast_environment, | 55 video_encoder_.reset(new ExternalVideoEncoder( |
55 video_config, | 56 cast_environment, |
56 create_vea_cb, | 57 video_config, |
57 create_video_encode_mem_cb)); | 58 base::Bind(&VideoSender::OnEncoderInitialized, |
| 59 weak_factory_.GetWeakPtr(), initialization_cb), |
| 60 create_vea_cb, |
| 61 create_video_encode_mem_cb)); |
58 } else { | 62 } else { |
| 63 // Software encoder is initialized immediately. |
59 congestion_control_.reset( | 64 congestion_control_.reset( |
60 NewAdaptiveCongestionControl(cast_environment->Clock(), | 65 NewAdaptiveCongestionControl(cast_environment->Clock(), |
61 video_config.max_bitrate, | 66 video_config.max_bitrate, |
62 video_config.min_bitrate, | 67 video_config.min_bitrate, |
63 max_unacked_frames_)); | 68 max_unacked_frames_)); |
64 video_encoder_.reset(new VideoEncoderImpl( | 69 video_encoder_.reset(new VideoEncoderImpl( |
65 cast_environment, video_config, max_unacked_frames_)); | 70 cast_environment, video_config, max_unacked_frames_)); |
| 71 cast_initialization_status_ = STATUS_VIDEO_INITIALIZED; |
66 } | 72 } |
67 cast_initialization_status_ = STATUS_VIDEO_INITIALIZED; | 73 |
| 74 if (cast_initialization_status_ == STATUS_VIDEO_INITIALIZED) { |
| 75 cast_environment->PostTask( |
| 76 CastEnvironment::MAIN, |
| 77 FROM_HERE, |
| 78 base::Bind(initialization_cb, cast_initialization_status_)); |
| 79 } |
68 | 80 |
69 media::cast::CastTransportRtpConfig transport_config; | 81 media::cast::CastTransportRtpConfig transport_config; |
70 transport_config.ssrc = video_config.ssrc; | 82 transport_config.ssrc = video_config.ssrc; |
71 transport_config.feedback_ssrc = video_config.incoming_feedback_ssrc; | 83 transport_config.feedback_ssrc = video_config.incoming_feedback_ssrc; |
72 transport_config.rtp_payload_type = video_config.rtp_payload_type; | 84 transport_config.rtp_payload_type = video_config.rtp_payload_type; |
73 transport_config.stored_frames = max_unacked_frames_; | 85 transport_config.stored_frames = max_unacked_frames_; |
74 transport_config.aes_key = video_config.aes_key; | 86 transport_config.aes_key = video_config.aes_key; |
75 transport_config.aes_iv_mask = video_config.aes_iv_mask; | 87 transport_config.aes_iv_mask = video_config.aes_iv_mask; |
76 | 88 |
77 transport_sender->InitializeVideo( | 89 transport_sender->InitializeVideo( |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
137 } | 149 } |
138 | 150 |
139 int VideoSender::GetNumberOfFramesInEncoder() const { | 151 int VideoSender::GetNumberOfFramesInEncoder() const { |
140 return frames_in_encoder_; | 152 return frames_in_encoder_; |
141 } | 153 } |
142 | 154 |
143 void VideoSender::OnAck(uint32 frame_id) { | 155 void VideoSender::OnAck(uint32 frame_id) { |
144 video_encoder_->LatestFrameIdToReference(frame_id); | 156 video_encoder_->LatestFrameIdToReference(frame_id); |
145 } | 157 } |
146 | 158 |
| 159 void VideoSender::OnEncoderInitialized( |
| 160 const CastInitializationCallback& initialization_cb, |
| 161 CastInitializationStatus status) { |
| 162 cast_initialization_status_ = status; |
| 163 initialization_cb.Run(status); |
| 164 } |
| 165 |
147 void VideoSender::OnEncodedVideoFrame( | 166 void VideoSender::OnEncodedVideoFrame( |
148 int encoder_bitrate, | 167 int encoder_bitrate, |
149 scoped_ptr<EncodedFrame> encoded_frame) { | 168 scoped_ptr<EncodedFrame> encoded_frame) { |
150 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); | 169 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); |
151 | 170 |
152 frames_in_encoder_--; | 171 frames_in_encoder_--; |
153 DCHECK_GE(frames_in_encoder_, 0); | 172 DCHECK_GE(frames_in_encoder_, 0); |
154 | 173 |
155 SendEncodedFrame(encoder_bitrate, encoded_frame.Pass()); | 174 SendEncodedFrame(encoder_bitrate, encoded_frame.Pass()); |
156 } | 175 } |
157 | 176 |
158 } // namespace cast | 177 } // namespace cast |
159 } // namespace media | 178 } // namespace media |
OLD | NEW |