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 #include "media/cast/sender/video_frame_factory_pool_impl.h" |
18 | 19 |
19 namespace media { | 20 namespace media { |
20 namespace cast { | 21 namespace cast { |
21 | 22 |
22 namespace { | 23 namespace { |
23 | 24 |
24 // The following two constants are used to adjust the target | 25 // The following two constants are used to adjust the target |
25 // playout delay (when allowed). They were calculated using | 26 // playout delay (when allowed). They were calculated using |
26 // a combination of cast_benchmark runs and manual testing. | 27 // a combination of cast_benchmark runs and manual testing. |
27 // | 28 // |
(...skipping 12 matching lines...) Expand all Loading... |
40 // See details: crbug.com/392086. | 41 // See details: crbug.com/392086. |
41 VideoSender::VideoSender( | 42 VideoSender::VideoSender( |
42 scoped_refptr<CastEnvironment> cast_environment, | 43 scoped_refptr<CastEnvironment> cast_environment, |
43 const VideoSenderConfig& video_config, | 44 const VideoSenderConfig& video_config, |
44 const CastInitializationCallback& initialization_cb, | 45 const CastInitializationCallback& initialization_cb, |
45 const CreateVideoEncodeAcceleratorCallback& create_vea_cb, | 46 const CreateVideoEncodeAcceleratorCallback& create_vea_cb, |
46 const CreateVideoEncodeMemoryCallback& create_video_encode_mem_cb, | 47 const CreateVideoEncodeMemoryCallback& create_video_encode_mem_cb, |
47 CastTransportSender* const transport_sender, | 48 CastTransportSender* const transport_sender, |
48 const PlayoutDelayChangeCB& playout_delay_change_cb) | 49 const PlayoutDelayChangeCB& playout_delay_change_cb) |
49 : FrameSender( | 50 : FrameSender( |
50 cast_environment, | 51 cast_environment, |
51 false, | 52 false, |
52 transport_sender, | 53 transport_sender, |
53 base::TimeDelta::FromMilliseconds(video_config.rtcp_interval), | 54 base::TimeDelta::FromMilliseconds(video_config.rtcp_interval), |
54 kVideoFrequency, | 55 kVideoFrequency, |
55 video_config.ssrc, | 56 video_config.ssrc, |
56 video_config.max_frame_rate, | 57 video_config.max_frame_rate, |
57 video_config.min_playout_delay, | 58 video_config.min_playout_delay, |
58 video_config.max_playout_delay, | 59 video_config.max_playout_delay, |
59 video_config.use_external_encoder ? | 60 video_config.use_external_encoder |
60 NewFixedCongestionControl( | 61 ? NewFixedCongestionControl( |
61 (video_config.min_bitrate + video_config.max_bitrate) / 2) : | 62 (video_config.min_bitrate + video_config.max_bitrate) / 2) |
62 NewAdaptiveCongestionControl(cast_environment->Clock(), | 63 : NewAdaptiveCongestionControl(cast_environment->Clock(), |
63 video_config.max_bitrate, | 64 video_config.max_bitrate, |
64 video_config.min_bitrate, | 65 video_config.min_bitrate, |
65 video_config.max_frame_rate)), | 66 video_config.max_frame_rate)), |
| 67 video_frame_factory_(new VideoFrameFactoryPoolImpl(video_config)), |
66 frames_in_encoder_(0), | 68 frames_in_encoder_(0), |
67 last_bitrate_(0), | 69 last_bitrate_(0), |
68 playout_delay_change_cb_(playout_delay_change_cb), | 70 playout_delay_change_cb_(playout_delay_change_cb), |
69 weak_factory_(this) { | 71 weak_factory_(this) { |
70 cast_initialization_status_ = STATUS_VIDEO_UNINITIALIZED; | 72 cast_initialization_status_ = STATUS_VIDEO_UNINITIALIZED; |
71 | 73 |
72 if (video_config.use_external_encoder) { | 74 if (video_config.use_external_encoder) { |
73 video_encoder_.reset(new ExternalVideoEncoder( | 75 video_encoder_.reset(new ExternalVideoEncoder( |
74 cast_environment, | 76 cast_environment, |
75 video_config, | 77 video_config, |
76 base::Bind(&VideoSender::OnEncoderInitialized, | 78 base::Bind(&VideoSender::OnEncoderInitialized, |
77 weak_factory_.GetWeakPtr(), initialization_cb), | 79 weak_factory_.GetWeakPtr(), initialization_cb), |
78 create_vea_cb, | 80 create_vea_cb, |
79 create_video_encode_mem_cb)); | 81 create_video_encode_mem_cb)); |
80 } else { | 82 } else { |
81 // Software encoder is initialized immediately. | 83 // Software encoder is initialized immediately. |
82 video_encoder_.reset(new VideoEncoderImpl(cast_environment, video_config)); | 84 video_encoder_.reset(new VideoEncoderImpl(cast_environment, video_config)); |
83 cast_initialization_status_ = STATUS_VIDEO_INITIALIZED; | 85 cast_environment->PostTask(CastEnvironment::MAIN, |
84 } | 86 FROM_HERE, |
85 | 87 base::Bind(&VideoSender::OnEncoderInitialized, |
86 if (cast_initialization_status_ == STATUS_VIDEO_INITIALIZED) { | 88 weak_factory_.GetWeakPtr(), |
87 cast_environment->PostTask( | 89 initialization_cb, |
88 CastEnvironment::MAIN, | 90 STATUS_VIDEO_INITIALIZED)); |
89 FROM_HERE, | |
90 base::Bind(initialization_cb, cast_initialization_status_)); | |
91 } | 91 } |
92 | 92 |
93 media::cast::CastTransportRtpConfig transport_config; | 93 media::cast::CastTransportRtpConfig transport_config; |
94 transport_config.ssrc = video_config.ssrc; | 94 transport_config.ssrc = video_config.ssrc; |
95 transport_config.feedback_ssrc = video_config.incoming_feedback_ssrc; | 95 transport_config.feedback_ssrc = video_config.incoming_feedback_ssrc; |
96 transport_config.rtp_payload_type = video_config.rtp_payload_type; | 96 transport_config.rtp_payload_type = video_config.rtp_payload_type; |
97 transport_config.aes_key = video_config.aes_key; | 97 transport_config.aes_key = video_config.aes_key; |
98 transport_config.aes_iv_mask = video_config.aes_iv_mask; | 98 transport_config.aes_iv_mask = video_config.aes_iv_mask; |
99 | 99 |
100 transport_sender->InitializeVideo( | 100 transport_sender->InitializeVideo( |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
208 } | 208 } |
209 | 209 |
210 void VideoSender::OnAck(uint32 frame_id) { | 210 void VideoSender::OnAck(uint32 frame_id) { |
211 video_encoder_->LatestFrameIdToReference(frame_id); | 211 video_encoder_->LatestFrameIdToReference(frame_id); |
212 } | 212 } |
213 | 213 |
214 void VideoSender::OnEncoderInitialized( | 214 void VideoSender::OnEncoderInitialized( |
215 const CastInitializationCallback& initialization_cb, | 215 const CastInitializationCallback& initialization_cb, |
216 CastInitializationStatus status) { | 216 CastInitializationStatus status) { |
217 cast_initialization_status_ = status; | 217 cast_initialization_status_ = status; |
| 218 if (status == STATUS_VIDEO_INITIALIZED) { |
| 219 DCHECK(video_encoder_.get()) << "Invalid state"; |
| 220 auto video_frame_factory = video_encoder_->GetVideoFrameFactory(); |
| 221 if (video_frame_factory.get()) { |
| 222 video_frame_factory_ = video_frame_factory; |
| 223 } |
| 224 } |
218 initialization_cb.Run(status); | 225 initialization_cb.Run(status); |
219 } | 226 } |
220 | 227 |
221 void VideoSender::OnEncodedVideoFrame( | 228 void VideoSender::OnEncodedVideoFrame( |
222 int encoder_bitrate, | 229 int encoder_bitrate, |
223 scoped_ptr<EncodedFrame> encoded_frame) { | 230 scoped_ptr<EncodedFrame> encoded_frame) { |
224 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); | 231 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); |
225 | 232 |
226 frames_in_encoder_--; | 233 frames_in_encoder_--; |
227 DCHECK_GE(frames_in_encoder_, 0); | 234 DCHECK_GE(frames_in_encoder_, 0); |
228 | 235 |
229 duration_in_encoder_ = | 236 duration_in_encoder_ = |
230 last_enqueued_frame_reference_time_ - encoded_frame->reference_time; | 237 last_enqueued_frame_reference_time_ - encoded_frame->reference_time; |
231 | 238 |
232 SendEncodedFrame(encoder_bitrate, encoded_frame.Pass()); | 239 SendEncodedFrame(encoder_bitrate, encoded_frame.Pass()); |
233 } | 240 } |
234 | 241 |
235 } // namespace cast | 242 } // namespace cast |
236 } // namespace media | 243 } // namespace media |
OLD | NEW |