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/audio_sender/audio_sender.h" | 5 #include "media/cast/sender/audio_sender.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/cast/audio_sender/audio_encoder.h" | |
11 #include "media/cast/cast_defines.h" | 10 #include "media/cast/cast_defines.h" |
12 #include "media/cast/rtcp/rtcp_defines.h" | 11 #include "media/cast/net/cast_transport_config.h" |
13 #include "media/cast/transport/cast_transport_config.h" | 12 #include "media/cast/net/rtcp/rtcp_defines.h" |
| 13 #include "media/cast/sender/audio_encoder.h" |
14 | 14 |
15 namespace media { | 15 namespace media { |
16 namespace cast { | 16 namespace cast { |
17 namespace { | 17 namespace { |
18 | 18 |
19 const int kNumAggressiveReportsSentAtStart = 100; | 19 const int kNumAggressiveReportsSentAtStart = 100; |
20 const int kMinSchedulingDelayMs = 1; | 20 const int kMinSchedulingDelayMs = 1; |
21 | 21 |
22 // TODO(miu): This should be specified in AudioSenderConfig, but currently it is | 22 // TODO(miu): This should be specified in AudioSenderConfig, but currently it is |
23 // fixed to 100 FPS (i.e., 10 ms per frame), and AudioEncoder assumes this as | 23 // fixed to 100 FPS (i.e., 10 ms per frame), and AudioEncoder assumes this as |
24 // well. | 24 // well. |
25 const int kAudioFrameRate = 100; | 25 const int kAudioFrameRate = 100; |
26 | 26 |
27 // Helper function to compute the maximum unacked audio frames that is sent. | 27 // Helper function to compute the maximum unacked audio frames that is sent. |
28 int GetMaxUnackedFrames(base::TimeDelta target_delay) { | 28 int GetMaxUnackedFrames(base::TimeDelta target_delay) { |
29 // As long as it doesn't go over |kMaxUnackedFrames|, it is okay to send more | 29 // As long as it doesn't go over |kMaxUnackedFrames|, it is okay to send more |
30 // audio data than the target delay would suggest. Audio packets are tiny and | 30 // audio data than the target delay would suggest. Audio packets are tiny and |
31 // receiver has the ability to drop any one of the packets. | 31 // receiver has the ability to drop any one of the packets. |
32 // We send up to three times of the target delay of audio frames. | 32 // We send up to three times of the target delay of audio frames. |
33 int frames = | 33 int frames = |
34 1 + 3 * target_delay * kAudioFrameRate / base::TimeDelta::FromSeconds(1); | 34 1 + 3 * target_delay * kAudioFrameRate / base::TimeDelta::FromSeconds(1); |
35 return std::min(kMaxUnackedFrames, frames); | 35 return std::min(kMaxUnackedFrames, frames); |
36 } | 36 } |
37 } // namespace | 37 } // namespace |
38 | 38 |
39 AudioSender::AudioSender(scoped_refptr<CastEnvironment> cast_environment, | 39 AudioSender::AudioSender(scoped_refptr<CastEnvironment> cast_environment, |
40 const AudioSenderConfig& audio_config, | 40 const AudioSenderConfig& audio_config, |
41 transport::CastTransportSender* const transport_sender) | 41 CastTransportSender* const transport_sender) |
42 : cast_environment_(cast_environment), | 42 : cast_environment_(cast_environment), |
43 target_playout_delay_(audio_config.target_playout_delay), | 43 target_playout_delay_(audio_config.target_playout_delay), |
44 transport_sender_(transport_sender), | 44 transport_sender_(transport_sender), |
45 max_unacked_frames_(GetMaxUnackedFrames(target_playout_delay_)), | 45 max_unacked_frames_(GetMaxUnackedFrames(target_playout_delay_)), |
46 configured_encoder_bitrate_(audio_config.bitrate), | 46 configured_encoder_bitrate_(audio_config.bitrate), |
47 rtcp_(cast_environment, | 47 rtcp_(cast_environment, |
48 this, | 48 this, |
49 transport_sender_, | 49 transport_sender_, |
50 NULL, // paced sender. | 50 NULL, // paced sender. |
51 NULL, | 51 NULL, |
(...skipping 21 matching lines...) Expand all Loading... |
73 audio_config.bitrate, | 73 audio_config.bitrate, |
74 audio_config.codec, | 74 audio_config.codec, |
75 base::Bind(&AudioSender::SendEncodedAudioFrame, | 75 base::Bind(&AudioSender::SendEncodedAudioFrame, |
76 weak_factory_.GetWeakPtr()))); | 76 weak_factory_.GetWeakPtr()))); |
77 cast_initialization_status_ = audio_encoder_->InitializationResult(); | 77 cast_initialization_status_ = audio_encoder_->InitializationResult(); |
78 } else { | 78 } else { |
79 NOTREACHED(); // No support for external audio encoding. | 79 NOTREACHED(); // No support for external audio encoding. |
80 cast_initialization_status_ = STATUS_AUDIO_UNINITIALIZED; | 80 cast_initialization_status_ = STATUS_AUDIO_UNINITIALIZED; |
81 } | 81 } |
82 | 82 |
83 media::cast::transport::CastTransportRtpConfig transport_config; | 83 media::cast::CastTransportRtpConfig transport_config; |
84 transport_config.ssrc = audio_config.ssrc; | 84 transport_config.ssrc = audio_config.ssrc; |
85 transport_config.rtp_payload_type = audio_config.rtp_payload_type; | 85 transport_config.rtp_payload_type = audio_config.rtp_payload_type; |
86 // TODO(miu): AudioSender needs to be like VideoSender in providing an upper | 86 // TODO(miu): AudioSender needs to be like VideoSender in providing an upper |
87 // limit on the number of in-flight frames. | 87 // limit on the number of in-flight frames. |
88 transport_config.stored_frames = max_unacked_frames_; | 88 transport_config.stored_frames = max_unacked_frames_; |
89 transport_config.aes_key = audio_config.aes_key; | 89 transport_config.aes_key = audio_config.aes_key; |
90 transport_config.aes_iv_mask = audio_config.aes_iv_mask; | 90 transport_config.aes_iv_mask = audio_config.aes_iv_mask; |
91 transport_sender_->InitializeAudio(transport_config); | 91 transport_sender_->InitializeAudio(transport_config); |
92 | 92 |
93 rtcp_.SetCastReceiverEventHistorySize(kReceiverRtcpEventHistorySize); | 93 rtcp_.SetCastReceiverEventHistorySize(kReceiverRtcpEventHistorySize); |
(...skipping 14 matching lines...) Expand all Loading... |
108 | 108 |
109 if (AreTooManyFramesInFlight()) { | 109 if (AreTooManyFramesInFlight()) { |
110 VLOG(1) << "Dropping frame due to too many frames currently in-flight."; | 110 VLOG(1) << "Dropping frame due to too many frames currently in-flight."; |
111 return; | 111 return; |
112 } | 112 } |
113 | 113 |
114 audio_encoder_->InsertAudio(audio_bus.Pass(), recorded_time); | 114 audio_encoder_->InsertAudio(audio_bus.Pass(), recorded_time); |
115 } | 115 } |
116 | 116 |
117 void AudioSender::SendEncodedAudioFrame( | 117 void AudioSender::SendEncodedAudioFrame( |
118 scoped_ptr<transport::EncodedFrame> encoded_frame) { | 118 scoped_ptr<EncodedFrame> encoded_frame) { |
119 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); | 119 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); |
120 | 120 |
121 const uint32 frame_id = encoded_frame->frame_id; | 121 const uint32 frame_id = encoded_frame->frame_id; |
122 | 122 |
123 const bool is_first_frame_to_be_sent = last_send_time_.is_null(); | 123 const bool is_first_frame_to_be_sent = last_send_time_.is_null(); |
124 last_send_time_ = cast_environment_->Clock()->NowTicks(); | 124 last_send_time_ = cast_environment_->Clock()->NowTicks(); |
125 last_sent_frame_id_ = frame_id; | 125 last_sent_frame_id_ = frame_id; |
126 // If this is the first frame about to be sent, fake the value of | 126 // If this is the first frame about to be sent, fake the value of |
127 // |latest_acked_frame_id_| to indicate the receiver starts out all caught up. | 127 // |latest_acked_frame_id_| to indicate the receiver starts out all caught up. |
128 // Also, schedule the periodic frame re-send checks. | 128 // Also, schedule the periodic frame re-send checks. |
129 if (is_first_frame_to_be_sent) { | 129 if (is_first_frame_to_be_sent) { |
130 latest_acked_frame_id_ = frame_id - 1; | 130 latest_acked_frame_id_ = frame_id - 1; |
131 ScheduleNextResendCheck(); | 131 ScheduleNextResendCheck(); |
132 } | 132 } |
133 | 133 |
134 cast_environment_->Logging()->InsertEncodedFrameEvent( | 134 cast_environment_->Logging()->InsertEncodedFrameEvent( |
135 last_send_time_, FRAME_ENCODED, AUDIO_EVENT, encoded_frame->rtp_timestamp, | 135 last_send_time_, FRAME_ENCODED, AUDIO_EVENT, encoded_frame->rtp_timestamp, |
136 frame_id, static_cast<int>(encoded_frame->data.size()), | 136 frame_id, static_cast<int>(encoded_frame->data.size()), |
137 encoded_frame->dependency == transport::EncodedFrame::KEY, | 137 encoded_frame->dependency == EncodedFrame::KEY, |
138 configured_encoder_bitrate_); | 138 configured_encoder_bitrate_); |
139 // Only use lowest 8 bits as key. | 139 // Only use lowest 8 bits as key. |
140 frame_id_to_rtp_timestamp_[frame_id & 0xff] = encoded_frame->rtp_timestamp; | 140 frame_id_to_rtp_timestamp_[frame_id & 0xff] = encoded_frame->rtp_timestamp; |
141 | 141 |
142 DCHECK(!encoded_frame->reference_time.is_null()); | 142 DCHECK(!encoded_frame->reference_time.is_null()); |
143 rtp_timestamp_helper_.StoreLatestTime(encoded_frame->reference_time, | 143 rtp_timestamp_helper_.StoreLatestTime(encoded_frame->reference_time, |
144 encoded_frame->rtp_timestamp); | 144 encoded_frame->rtp_timestamp); |
145 | 145 |
146 // At the start of the session, it's important to send reports before each | 146 // At the start of the session, it's important to send reports before each |
147 // frame so that the receiver can properly compute playout times. The reason | 147 // frame so that the receiver can properly compute playout times. The reason |
(...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
340 rtcp_.Rtt(&rtt, &avg_rtt, &min_rtt, &max_rtt); | 340 rtcp_.Rtt(&rtt, &avg_rtt, &min_rtt, &max_rtt); |
341 | 341 |
342 // Sending this extra packet is to kick-start the session. There is | 342 // Sending this extra packet is to kick-start the session. There is |
343 // no need to optimize re-transmission for this case. | 343 // no need to optimize re-transmission for this case. |
344 transport_sender_->ResendPackets( | 344 transport_sender_->ResendPackets( |
345 true, missing_frames_and_packets, false, min_rtt); | 345 true, missing_frames_and_packets, false, min_rtt); |
346 } | 346 } |
347 | 347 |
348 } // namespace cast | 348 } // namespace cast |
349 } // namespace media | 349 } // namespace media |
OLD | NEW |