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/framer/cast_message_builder.h" | 5 #include "media/cast/framer/cast_message_builder.h" |
6 | 6 |
7 #include "media/cast/cast_defines.h" | 7 #include "media/cast/cast_defines.h" |
8 | 8 |
9 namespace media { | 9 namespace media { |
10 namespace cast { | 10 namespace cast { |
11 | 11 |
12 CastMessageBuilder::CastMessageBuilder( | 12 CastMessageBuilder::CastMessageBuilder( |
13 base::TickClock* clock, | 13 base::TickClock* clock, |
14 RtpPayloadFeedback* incoming_payload_feedback, | 14 RtpPayloadFeedback* incoming_payload_feedback, |
15 FrameIdMap* frame_id_map, | 15 FrameIdMap* frame_id_map, |
16 uint32 media_ssrc, | 16 uint32 media_ssrc, |
17 bool decoder_faster_than_max_frame_rate, | 17 bool decoder_faster_than_max_frame_rate, |
18 int max_unacked_frames) | 18 int max_unacked_frames) |
19 : clock_(clock), | 19 : clock_(clock), |
20 cast_feedback_(incoming_payload_feedback), | 20 cast_feedback_(incoming_payload_feedback), |
21 frame_id_map_(frame_id_map), | 21 frame_id_map_(frame_id_map), |
22 media_ssrc_(media_ssrc), | 22 media_ssrc_(media_ssrc), |
23 decoder_faster_than_max_frame_rate_(decoder_faster_than_max_frame_rate), | 23 decoder_faster_than_max_frame_rate_(decoder_faster_than_max_frame_rate), |
24 max_unacked_frames_(max_unacked_frames), | 24 max_unacked_frames_(max_unacked_frames), |
25 cast_msg_(media_ssrc), | 25 cast_msg_(media_ssrc), |
26 waiting_for_key_frame_(true), | |
27 slowing_down_ack_(false), | 26 slowing_down_ack_(false), |
28 acked_last_frame_(true), | 27 acked_last_frame_(true), |
29 last_acked_frame_id_(kStartFrameId) { | 28 last_acked_frame_id_(kStartFrameId) { |
30 cast_msg_.ack_frame_id_ = kStartFrameId; | 29 cast_msg_.ack_frame_id_ = kStartFrameId; |
31 } | 30 } |
32 | 31 |
33 CastMessageBuilder::~CastMessageBuilder() {} | 32 CastMessageBuilder::~CastMessageBuilder() {} |
34 | 33 |
35 void CastMessageBuilder::CompleteFrameReceived(uint32 frame_id, | 34 void CastMessageBuilder::CompleteFrameReceived(uint32 frame_id) { |
36 bool is_key_frame) { | 35 DCHECK_GE(static_cast<int32>(frame_id - last_acked_frame_id_), 0); |
| 36 VLOG(2) << "CompleteFrameReceived: " << frame_id; |
37 if (last_update_time_.is_null()) { | 37 if (last_update_time_.is_null()) { |
38 // Our first update. | 38 // Our first update. |
39 last_update_time_ = clock_->NowTicks(); | 39 last_update_time_ = clock_->NowTicks(); |
40 } | 40 } |
41 if (waiting_for_key_frame_) { | |
42 if (!is_key_frame) { | |
43 // Ignore that we have received this complete frame since we are | |
44 // waiting on a key frame. | |
45 return; | |
46 } | |
47 waiting_for_key_frame_ = false; | |
48 cast_msg_.missing_frames_and_packets_.clear(); | |
49 cast_msg_.ack_frame_id_ = frame_id; | |
50 last_update_time_ = clock_->NowTicks(); | |
51 // We might have other complete frames waiting after we receive the last | |
52 // packet in the key-frame. | |
53 UpdateAckMessage(); | |
54 } else { | |
55 if (!UpdateAckMessage()) | |
56 return; | |
57 | 41 |
58 BuildPacketList(); | 42 if (!UpdateAckMessage(frame_id)) { |
| 43 return; |
59 } | 44 } |
| 45 BuildPacketList(); |
| 46 |
60 // Send cast message. | 47 // Send cast message. |
61 VLOG(2) << "Send cast message Ack:" << static_cast<int>(frame_id); | 48 VLOG(2) << "Send cast message Ack:" << static_cast<int>(frame_id); |
62 cast_feedback_->CastFeedback(cast_msg_); | 49 cast_feedback_->CastFeedback(cast_msg_); |
63 } | 50 } |
64 | 51 |
65 bool CastMessageBuilder::UpdateAckMessage() { | 52 bool CastMessageBuilder::UpdateAckMessage(uint32 frame_id) { |
66 if (!decoder_faster_than_max_frame_rate_) { | 53 if (!decoder_faster_than_max_frame_rate_) { |
67 int complete_frame_count = frame_id_map_->NumberOfCompleteFrames(); | 54 int complete_frame_count = frame_id_map_->NumberOfCompleteFrames(); |
68 if (complete_frame_count > max_unacked_frames_) { | 55 if (complete_frame_count > max_unacked_frames_) { |
69 // We have too many frames pending in our framer; slow down ACK. | 56 // We have too many frames pending in our framer; slow down ACK. |
70 slowing_down_ack_ = true; | 57 if (!slowing_down_ack_) { |
| 58 slowing_down_ack_ = true; |
| 59 ack_queue_.push_back(last_acked_frame_id_); |
| 60 } |
71 } else if (complete_frame_count <= 1) { | 61 } else if (complete_frame_count <= 1) { |
72 // We are down to one or less frames in our framer; ACK normally. | 62 // We are down to one or less frames in our framer; ACK normally. |
73 slowing_down_ack_ = false; | 63 slowing_down_ack_ = false; |
| 64 ack_queue_.clear(); |
74 } | 65 } |
75 } | 66 } |
| 67 |
76 if (slowing_down_ack_) { | 68 if (slowing_down_ack_) { |
77 // We are slowing down acknowledgment by acknowledging every other frame. | 69 // We are slowing down acknowledgment by acknowledging every other frame. |
78 if (acked_last_frame_) { | 70 // Note: frame skipping and slowdown ACK is not supported at the same |
79 acked_last_frame_ = false; | 71 // time; and it's not needed since we can skip frames to catch up. |
80 } else { | 72 if (!ack_queue_.empty() && ack_queue_.back() == frame_id) { |
81 acked_last_frame_ = true; | 73 return false; |
82 last_acked_frame_id_++; | |
83 // Note: frame skipping and slowdown ACK is not supported at the same | |
84 // time; and it's not needed since we can skip frames to catch up. | |
85 } | 74 } |
86 } else { | 75 ack_queue_.push_back(frame_id); |
87 uint32 frame_id = frame_id_map_->LastContinuousFrame(); | 76 if (!acked_last_frame_) { |
| 77 ack_queue_.pop_front(); |
| 78 } |
| 79 frame_id = ack_queue_.front(); |
| 80 } |
88 | 81 |
89 // Is it a new frame? | 82 acked_last_frame_ = false; |
90 if (last_acked_frame_id_ == frame_id) | 83 // Is it a new frame? |
91 return false; | 84 if (last_acked_frame_id_ == frame_id) { |
92 | 85 return false; |
93 last_acked_frame_id_ = frame_id; | |
94 acked_last_frame_ = true; | |
95 } | 86 } |
| 87 acked_last_frame_ = true; |
| 88 last_acked_frame_id_ = frame_id; |
96 cast_msg_.ack_frame_id_ = last_acked_frame_id_; | 89 cast_msg_.ack_frame_id_ = last_acked_frame_id_; |
97 cast_msg_.missing_frames_and_packets_.clear(); | 90 cast_msg_.missing_frames_and_packets_.clear(); |
98 last_update_time_ = clock_->NowTicks(); | 91 last_update_time_ = clock_->NowTicks(); |
99 return true; | 92 return true; |
100 } | 93 } |
101 | 94 |
102 bool CastMessageBuilder::TimeToSendNextCastMessage( | 95 bool CastMessageBuilder::TimeToSendNextCastMessage( |
103 base::TimeTicks* time_to_send) { | 96 base::TimeTicks* time_to_send) { |
104 // We haven't received any packets. | 97 // We haven't received any packets. |
105 if (last_update_time_.is_null() && frame_id_map_->Empty()) | 98 if (last_update_time_.is_null() && frame_id_map_->Empty()) |
106 return false; | 99 return false; |
107 | 100 |
108 *time_to_send = last_update_time_ + base::TimeDelta::FromMilliseconds( | 101 *time_to_send = last_update_time_ + base::TimeDelta::FromMilliseconds( |
109 kCastMessageUpdateIntervalMs); | 102 kCastMessageUpdateIntervalMs); |
110 return true; | 103 return true; |
111 } | 104 } |
112 | 105 |
113 void CastMessageBuilder::UpdateCastMessage() { | 106 void CastMessageBuilder::UpdateCastMessage() { |
114 RtcpCastMessage message(media_ssrc_); | 107 RtcpCastMessage message(media_ssrc_); |
115 if (!UpdateCastMessageInternal(&message)) | 108 if (!UpdateCastMessageInternal(&message)) |
116 return; | 109 return; |
117 | 110 |
118 // Send cast message. | 111 // Send cast message. |
119 cast_feedback_->CastFeedback(message); | 112 cast_feedback_->CastFeedback(message); |
120 } | 113 } |
121 | 114 |
122 void CastMessageBuilder::Reset() { | 115 void CastMessageBuilder::Reset() { |
123 waiting_for_key_frame_ = true; | |
124 cast_msg_.ack_frame_id_ = kStartFrameId; | 116 cast_msg_.ack_frame_id_ = kStartFrameId; |
125 cast_msg_.missing_frames_and_packets_.clear(); | 117 cast_msg_.missing_frames_and_packets_.clear(); |
126 time_last_nacked_map_.clear(); | 118 time_last_nacked_map_.clear(); |
127 } | 119 } |
128 | 120 |
129 bool CastMessageBuilder::UpdateCastMessageInternal(RtcpCastMessage* message) { | 121 bool CastMessageBuilder::UpdateCastMessageInternal(RtcpCastMessage* message) { |
130 if (last_update_time_.is_null()) { | 122 if (last_update_time_.is_null()) { |
131 if (!frame_id_map_->Empty()) { | 123 if (!frame_id_map_->Empty()) { |
132 // We have received packets. | 124 // We have received packets. |
133 last_update_time_ = clock_->NowTicks(); | 125 last_update_time_ = clock_->NowTicks(); |
134 } | 126 } |
135 return false; | 127 return false; |
136 } | 128 } |
137 // Is it time to update the cast message? | 129 // Is it time to update the cast message? |
138 base::TimeTicks now = clock_->NowTicks(); | 130 base::TimeTicks now = clock_->NowTicks(); |
139 if (now - last_update_time_ < | 131 if (now - last_update_time_ < |
140 base::TimeDelta::FromMilliseconds(kCastMessageUpdateIntervalMs)) { | 132 base::TimeDelta::FromMilliseconds(kCastMessageUpdateIntervalMs)) { |
141 return false; | 133 return false; |
142 } | 134 } |
143 last_update_time_ = now; | 135 last_update_time_ = now; |
144 | 136 |
145 UpdateAckMessage(); // Needed to cover when a frame is skipped. | 137 // Needed to cover when a frame is skipped. |
| 138 UpdateAckMessage(last_acked_frame_id_); |
146 BuildPacketList(); | 139 BuildPacketList(); |
147 message->Copy(cast_msg_); | 140 message->Copy(cast_msg_); |
148 return true; | 141 return true; |
149 } | 142 } |
150 | 143 |
151 void CastMessageBuilder::BuildPacketList() { | 144 void CastMessageBuilder::BuildPacketList() { |
152 base::TimeTicks now = clock_->NowTicks(); | 145 base::TimeTicks now = clock_->NowTicks(); |
153 | 146 |
154 // Clear message NACK list. | 147 // Clear message NACK list. |
155 cast_msg_.missing_frames_and_packets_.clear(); | 148 cast_msg_.missing_frames_and_packets_.clear(); |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
188 } else { | 181 } else { |
189 time_last_nacked_map_[next_expected_frame_id] = now; | 182 time_last_nacked_map_[next_expected_frame_id] = now; |
190 missing.insert(kRtcpCastAllPacketsLost); | 183 missing.insert(kRtcpCastAllPacketsLost); |
191 cast_msg_.missing_frames_and_packets_[next_expected_frame_id] = missing; | 184 cast_msg_.missing_frames_and_packets_[next_expected_frame_id] = missing; |
192 } | 185 } |
193 } | 186 } |
194 } | 187 } |
195 | 188 |
196 } // namespace cast | 189 } // namespace cast |
197 } // namespace media | 190 } // namespace media |
OLD | NEW |