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 <stdint.h> | 7 #include <stdint.h> |
8 #include <algorithm> | 8 #include <algorithm> |
9 #include <cmath> | 9 #include <cmath> |
10 #include <cstring> | 10 #include <cstring> |
11 #include <utility> | 11 #include <utility> |
12 | 12 |
13 #include "base/bind.h" | 13 #include "base/bind.h" |
14 #include "base/logging.h" | 14 #include "base/logging.h" |
| 15 #include "base/memory/ptr_util.h" |
15 #include "base/trace_event/trace_event.h" | 16 #include "base/trace_event/trace_event.h" |
16 #include "media/cast/cast_defines.h" | 17 #include "media/cast/cast_defines.h" |
17 #include "media/cast/net/cast_transport_config.h" | 18 #include "media/cast/net/cast_transport_config.h" |
18 #include "media/cast/sender/performance_metrics_overlay.h" | 19 #include "media/cast/sender/performance_metrics_overlay.h" |
19 #include "media/cast/sender/video_encoder.h" | 20 #include "media/cast/sender/video_encoder.h" |
20 | 21 |
21 namespace media { | 22 namespace media { |
22 namespace cast { | 23 namespace cast { |
23 | 24 |
24 namespace { | 25 namespace { |
(...skipping 20 matching lines...) Expand all Loading... |
45 // request to the encoder on receiving Pli messages. This is used to prevent | 46 // request to the encoder on receiving Pli messages. This is used to prevent |
46 // sending multiple requests while the sender is waiting for an encoded key | 47 // sending multiple requests while the sender is waiting for an encoded key |
47 // frame or receiving multiple Pli messages in a short period. | 48 // frame or receiving multiple Pli messages in a short period. |
48 const int64_t kMinKeyFrameRequestOnPliIntervalMs = 500; | 49 const int64_t kMinKeyFrameRequestOnPliIntervalMs = 500; |
49 | 50 |
50 // Extract capture begin/end timestamps from |video_frame|'s metadata and log | 51 // Extract capture begin/end timestamps from |video_frame|'s metadata and log |
51 // it. | 52 // it. |
52 void LogVideoCaptureTimestamps(CastEnvironment* cast_environment, | 53 void LogVideoCaptureTimestamps(CastEnvironment* cast_environment, |
53 const media::VideoFrame& video_frame, | 54 const media::VideoFrame& video_frame, |
54 RtpTimeTicks rtp_timestamp) { | 55 RtpTimeTicks rtp_timestamp) { |
55 scoped_ptr<FrameEvent> capture_begin_event(new FrameEvent()); | 56 std::unique_ptr<FrameEvent> capture_begin_event(new FrameEvent()); |
56 capture_begin_event->type = FRAME_CAPTURE_BEGIN; | 57 capture_begin_event->type = FRAME_CAPTURE_BEGIN; |
57 capture_begin_event->media_type = VIDEO_EVENT; | 58 capture_begin_event->media_type = VIDEO_EVENT; |
58 capture_begin_event->rtp_timestamp = rtp_timestamp; | 59 capture_begin_event->rtp_timestamp = rtp_timestamp; |
59 | 60 |
60 scoped_ptr<FrameEvent> capture_end_event(new FrameEvent()); | 61 std::unique_ptr<FrameEvent> capture_end_event(new FrameEvent()); |
61 capture_end_event->type = FRAME_CAPTURE_END; | 62 capture_end_event->type = FRAME_CAPTURE_END; |
62 capture_end_event->media_type = VIDEO_EVENT; | 63 capture_end_event->media_type = VIDEO_EVENT; |
63 capture_end_event->rtp_timestamp = rtp_timestamp; | 64 capture_end_event->rtp_timestamp = rtp_timestamp; |
64 capture_end_event->width = video_frame.visible_rect().width(); | 65 capture_end_event->width = video_frame.visible_rect().width(); |
65 capture_end_event->height = video_frame.visible_rect().height(); | 66 capture_end_event->height = video_frame.visible_rect().height(); |
66 | 67 |
67 if (!video_frame.metadata()->GetTimeTicks( | 68 if (!video_frame.metadata()->GetTimeTicks( |
68 media::VideoFrameMetadata::CAPTURE_BEGIN_TIME, | 69 media::VideoFrameMetadata::CAPTURE_BEGIN_TIME, |
69 &capture_begin_event->timestamp) || | 70 &capture_begin_event->timestamp) || |
70 !video_frame.metadata()->GetTimeTicks( | 71 !video_frame.metadata()->GetTimeTicks( |
71 media::VideoFrameMetadata::CAPTURE_END_TIME, | 72 media::VideoFrameMetadata::CAPTURE_END_TIME, |
72 &capture_end_event->timestamp)) { | 73 &capture_end_event->timestamp)) { |
73 // The frame capture timestamps were not provided by the video capture | 74 // The frame capture timestamps were not provided by the video capture |
74 // source. Simply log the events as happening right now. | 75 // source. Simply log the events as happening right now. |
75 capture_begin_event->timestamp = capture_end_event->timestamp = | 76 capture_begin_event->timestamp = capture_end_event->timestamp = |
76 cast_environment->Clock()->NowTicks(); | 77 cast_environment->Clock()->NowTicks(); |
77 } | 78 } |
78 | 79 |
79 cast_environment->logger()->DispatchFrameEvent( | 80 cast_environment->logger()->DispatchFrameEvent( |
80 std::move(capture_begin_event)); | 81 std::move(capture_begin_event)); |
81 cast_environment->logger()->DispatchFrameEvent(std::move(capture_end_event)); | 82 cast_environment->logger()->DispatchFrameEvent(std::move(capture_end_event)); |
82 } | 83 } |
83 | 84 |
| 85 class VideoSenderRtcpClient : public RtpSenderRtcpClient { |
| 86 public: |
| 87 explicit VideoSenderRtcpClient(base::WeakPtr<VideoSender> video_sender) |
| 88 : video_sender_(video_sender) {} |
| 89 |
| 90 void OnCastMessageReceived(const RtcpCastMessage& cast_message) override { |
| 91 if (video_sender_) |
| 92 video_sender_->OnReceivedCastFeedback(cast_message); |
| 93 } |
| 94 |
| 95 void OnRttReceived(base::TimeDelta round_trip_time) override { |
| 96 if (video_sender_) |
| 97 video_sender_->OnMeasuredRoundTripTime(round_trip_time); |
| 98 } |
| 99 |
| 100 void OnPliReceived() override { |
| 101 if (video_sender_) |
| 102 video_sender_->OnReceivedPli(); |
| 103 } |
| 104 |
| 105 private: |
| 106 base::WeakPtr<VideoSender> video_sender_; |
| 107 |
| 108 DISALLOW_COPY_AND_ASSIGN(VideoSenderRtcpClient); |
| 109 }; |
| 110 |
84 } // namespace | 111 } // namespace |
85 | 112 |
86 // Note, we use a fixed bitrate value when external video encoder is used. | 113 // Note, we use a fixed bitrate value when external video encoder is used. |
87 // Some hardware encoder shows bad behavior if we set the bitrate too | 114 // Some hardware encoder shows bad behavior if we set the bitrate too |
88 // frequently, e.g. quality drop, not abiding by target bitrate, etc. | 115 // frequently, e.g. quality drop, not abiding by target bitrate, etc. |
89 // See details: crbug.com/392086. | 116 // See details: crbug.com/392086. |
90 VideoSender::VideoSender( | 117 VideoSender::VideoSender( |
91 scoped_refptr<CastEnvironment> cast_environment, | 118 scoped_refptr<CastEnvironment> cast_environment, |
92 const VideoSenderConfig& video_config, | 119 const VideoSenderConfig& video_config, |
93 const StatusChangeCallback& status_change_cb, | 120 const StatusChangeCallback& status_change_cb, |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
133 } | 160 } |
134 | 161 |
135 media::cast::CastTransportRtpConfig transport_config; | 162 media::cast::CastTransportRtpConfig transport_config; |
136 transport_config.ssrc = video_config.ssrc; | 163 transport_config.ssrc = video_config.ssrc; |
137 transport_config.feedback_ssrc = video_config.receiver_ssrc; | 164 transport_config.feedback_ssrc = video_config.receiver_ssrc; |
138 transport_config.rtp_payload_type = video_config.rtp_payload_type; | 165 transport_config.rtp_payload_type = video_config.rtp_payload_type; |
139 transport_config.aes_key = video_config.aes_key; | 166 transport_config.aes_key = video_config.aes_key; |
140 transport_config.aes_iv_mask = video_config.aes_iv_mask; | 167 transport_config.aes_iv_mask = video_config.aes_iv_mask; |
141 | 168 |
142 transport_sender->InitializeVideo( | 169 transport_sender->InitializeVideo( |
143 transport_config, base::Bind(&VideoSender::OnReceivedCastFeedback, | 170 transport_config, |
144 weak_factory_.GetWeakPtr()), | 171 base::WrapUnique(new VideoSenderRtcpClient(weak_factory_.GetWeakPtr()))); |
145 base::Bind(&VideoSender::OnMeasuredRoundTripTime, | |
146 weak_factory_.GetWeakPtr()), | |
147 base::Bind(&VideoSender::OnReceivedPli, weak_factory_.GetWeakPtr())); | |
148 } | 172 } |
149 | 173 |
150 VideoSender::~VideoSender() { | 174 VideoSender::~VideoSender() { |
151 } | 175 } |
152 | 176 |
153 void VideoSender::InsertRawVideoFrame( | 177 void VideoSender::InsertRawVideoFrame( |
154 const scoped_refptr<media::VideoFrame>& video_frame, | 178 const scoped_refptr<media::VideoFrame>& video_frame, |
155 const base::TimeTicks& reference_time) { | 179 const base::TimeTicks& reference_time) { |
156 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); | 180 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); |
157 | 181 |
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
293 last_enqueued_frame_rtp_timestamp_ = rtp_timestamp; | 317 last_enqueued_frame_rtp_timestamp_ = rtp_timestamp; |
294 last_enqueued_frame_reference_time_ = reference_time; | 318 last_enqueued_frame_reference_time_ = reference_time; |
295 } else { | 319 } else { |
296 VLOG(1) << "Encoder rejected a frame. Skipping..."; | 320 VLOG(1) << "Encoder rejected a frame. Skipping..."; |
297 TRACE_EVENT_INSTANT1("cast.stream", "Video Encode Reject", | 321 TRACE_EVENT_INSTANT1("cast.stream", "Video Encode Reject", |
298 TRACE_EVENT_SCOPE_THREAD, | 322 TRACE_EVENT_SCOPE_THREAD, |
299 "rtp_timestamp", rtp_timestamp.lower_32_bits()); | 323 "rtp_timestamp", rtp_timestamp.lower_32_bits()); |
300 } | 324 } |
301 } | 325 } |
302 | 326 |
303 scoped_ptr<VideoFrameFactory> VideoSender::CreateVideoFrameFactory() { | 327 std::unique_ptr<VideoFrameFactory> VideoSender::CreateVideoFrameFactory() { |
304 return video_encoder_ ? video_encoder_->CreateVideoFrameFactory() : nullptr; | 328 return video_encoder_ ? video_encoder_->CreateVideoFrameFactory() : nullptr; |
305 } | 329 } |
306 | 330 |
307 int VideoSender::GetNumberOfFramesInEncoder() const { | 331 int VideoSender::GetNumberOfFramesInEncoder() const { |
308 return frames_in_encoder_; | 332 return frames_in_encoder_; |
309 } | 333 } |
310 | 334 |
311 base::TimeDelta VideoSender::GetInFlightMediaDuration() const { | 335 base::TimeDelta VideoSender::GetInFlightMediaDuration() const { |
312 if (GetUnacknowledgedFrameCount() > 0) { | 336 if (GetUnacknowledgedFrameCount() > 0) { |
313 const uint32_t oldest_unacked_frame_id = latest_acked_frame_id_ + 1; | 337 const uint32_t oldest_unacked_frame_id = latest_acked_frame_id_ + 1; |
314 return last_enqueued_frame_reference_time_ - | 338 return last_enqueued_frame_reference_time_ - |
315 GetRecordedReferenceTime(oldest_unacked_frame_id); | 339 GetRecordedReferenceTime(oldest_unacked_frame_id); |
316 } else { | 340 } else { |
317 return duration_in_encoder_; | 341 return duration_in_encoder_; |
318 } | 342 } |
319 } | 343 } |
320 | 344 |
321 void VideoSender::OnEncodedVideoFrame( | 345 void VideoSender::OnEncodedVideoFrame( |
322 const scoped_refptr<media::VideoFrame>& video_frame, | 346 const scoped_refptr<media::VideoFrame>& video_frame, |
323 int encoder_bitrate, | 347 int encoder_bitrate, |
324 scoped_ptr<SenderEncodedFrame> encoded_frame) { | 348 std::unique_ptr<SenderEncodedFrame> encoded_frame) { |
325 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); | 349 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); |
326 | 350 |
327 frames_in_encoder_--; | 351 frames_in_encoder_--; |
328 DCHECK_GE(frames_in_encoder_, 0); | 352 DCHECK_GE(frames_in_encoder_, 0); |
329 | 353 |
330 duration_in_encoder_ = | 354 duration_in_encoder_ = |
331 last_enqueued_frame_reference_time_ - encoded_frame->reference_time; | 355 last_enqueued_frame_reference_time_ - encoded_frame->reference_time; |
332 | 356 |
333 last_reported_deadline_utilization_ = encoded_frame->deadline_utilization; | 357 last_reported_deadline_utilization_ = encoded_frame->deadline_utilization; |
334 last_reported_lossy_utilization_ = encoded_frame->lossy_utilization; | 358 last_reported_lossy_utilization_ = encoded_frame->lossy_utilization; |
(...skipping 17 matching lines...) Expand all Loading... |
352 media::VideoFrameMetadata::RESOURCE_UTILIZATION, | 376 media::VideoFrameMetadata::RESOURCE_UTILIZATION, |
353 encoded_frame->dependency == EncodedFrame::KEY ? | 377 encoded_frame->dependency == EncodedFrame::KEY ? |
354 std::min(1.0, attenuated_utilization) : attenuated_utilization); | 378 std::min(1.0, attenuated_utilization) : attenuated_utilization); |
355 } | 379 } |
356 | 380 |
357 SendEncodedFrame(encoder_bitrate, std::move(encoded_frame)); | 381 SendEncodedFrame(encoder_bitrate, std::move(encoded_frame)); |
358 } | 382 } |
359 | 383 |
360 } // namespace cast | 384 } // namespace cast |
361 } // namespace media | 385 } // namespace media |
OLD | NEW |