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/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/cast_defines.h" | 10 #include "media/cast/cast_defines.h" |
(...skipping 17 matching lines...) Expand all Loading... |
28 : FrameSender( | 28 : FrameSender( |
29 cast_environment, | 29 cast_environment, |
30 true, | 30 true, |
31 transport_sender, | 31 transport_sender, |
32 base::TimeDelta::FromMilliseconds(audio_config.rtcp_interval), | 32 base::TimeDelta::FromMilliseconds(audio_config.rtcp_interval), |
33 audio_config.frequency, | 33 audio_config.frequency, |
34 audio_config.ssrc, | 34 audio_config.ssrc, |
35 kAudioFrameRate * 2.0, // We lie to increase max outstanding frames. | 35 kAudioFrameRate * 2.0, // We lie to increase max outstanding frames. |
36 audio_config.target_playout_delay, | 36 audio_config.target_playout_delay, |
37 NewFixedCongestionControl(audio_config.bitrate)), | 37 NewFixedCongestionControl(audio_config.bitrate)), |
38 samples_sent_to_encoder_(0), | 38 samples_in_encoder_(0), |
39 weak_factory_(this) { | 39 weak_factory_(this) { |
40 cast_initialization_status_ = STATUS_AUDIO_UNINITIALIZED; | 40 cast_initialization_status_ = STATUS_AUDIO_UNINITIALIZED; |
41 VLOG(1) << "max_unacked_frames " << max_unacked_frames_; | 41 VLOG(1) << "max_unacked_frames " << max_unacked_frames_; |
42 DCHECK_GT(max_unacked_frames_, 0); | 42 DCHECK_GT(max_unacked_frames_, 0); |
43 | 43 |
44 if (!audio_config.use_external_encoder) { | 44 if (!audio_config.use_external_encoder) { |
45 audio_encoder_.reset( | 45 audio_encoder_.reset( |
46 new AudioEncoder(cast_environment, | 46 new AudioEncoder(cast_environment, |
47 audio_config.channels, | 47 audio_config.channels, |
48 audio_config.frequency, | 48 audio_config.frequency, |
49 audio_config.bitrate, | 49 audio_config.bitrate, |
50 audio_config.codec, | 50 audio_config.codec, |
51 base::Bind(&FrameSender::SendEncodedFrame, | 51 base::Bind(&AudioSender::OnEncodedAudioFrame, |
52 weak_factory_.GetWeakPtr(), | 52 weak_factory_.GetWeakPtr(), |
53 audio_config.bitrate))); | 53 audio_config.bitrate))); |
54 cast_initialization_status_ = audio_encoder_->InitializationResult(); | 54 cast_initialization_status_ = audio_encoder_->InitializationResult(); |
55 } else { | 55 } else { |
56 NOTREACHED(); // No support for external audio encoding. | 56 NOTREACHED(); // No support for external audio encoding. |
57 cast_initialization_status_ = STATUS_AUDIO_UNINITIALIZED; | 57 cast_initialization_status_ = STATUS_AUDIO_UNINITIALIZED; |
58 } | 58 } |
59 | 59 |
60 media::cast::CastTransportRtpConfig transport_config; | 60 media::cast::CastTransportRtpConfig transport_config; |
61 transport_config.ssrc = audio_config.ssrc; | 61 transport_config.ssrc = audio_config.ssrc; |
(...skipping 17 matching lines...) Expand all Loading... |
79 | 79 |
80 void AudioSender::InsertAudio(scoped_ptr<AudioBus> audio_bus, | 80 void AudioSender::InsertAudio(scoped_ptr<AudioBus> audio_bus, |
81 const base::TimeTicks& recorded_time) { | 81 const base::TimeTicks& recorded_time) { |
82 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); | 82 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); |
83 if (cast_initialization_status_ != STATUS_AUDIO_INITIALIZED) { | 83 if (cast_initialization_status_ != STATUS_AUDIO_INITIALIZED) { |
84 NOTREACHED(); | 84 NOTREACHED(); |
85 return; | 85 return; |
86 } | 86 } |
87 DCHECK(audio_encoder_.get()) << "Invalid internal state"; | 87 DCHECK(audio_encoder_.get()) << "Invalid internal state"; |
88 | 88 |
| 89 // TODO(miu): An |audio_bus| that represents more duration than a single |
| 90 // frame's duration can defeat our logic here, causing too much data to become |
| 91 // enqueued. This will be addressed in a soon-upcoming change. |
89 if (ShouldDropNextFrame(recorded_time)) { | 92 if (ShouldDropNextFrame(recorded_time)) { |
90 VLOG(1) << "Dropping frame due to too many frames currently in-flight."; | 93 VLOG(1) << "Dropping frame due to too many frames currently in-flight."; |
91 return; | 94 return; |
92 } | 95 } |
93 | 96 |
94 int64 old_frames_sent = | 97 samples_in_encoder_ += audio_bus->frames(); |
95 samples_sent_to_encoder_ * kAudioFrameRate / rtp_timebase_; | |
96 samples_sent_to_encoder_ += audio_bus->frames(); | |
97 int64 new_frames_sent = | |
98 samples_sent_to_encoder_ * kAudioFrameRate / rtp_timebase_; | |
99 frames_in_encoder_ += new_frames_sent - old_frames_sent; | |
100 | 98 |
101 audio_encoder_->InsertAudio(audio_bus.Pass(), recorded_time); | 99 audio_encoder_->InsertAudio(audio_bus.Pass(), recorded_time); |
102 } | 100 } |
103 | 101 |
| 102 int AudioSender::GetNumberOfFramesInEncoder() const { |
| 103 // Note: It's possible for a partial frame to be in the encoder, but returning |
| 104 // the floor() is good enough for the "design limit" check in FrameSender. |
| 105 return samples_in_encoder_ / audio_encoder_->GetSamplesPerFrame(); |
| 106 } |
| 107 |
104 void AudioSender::OnAck(uint32 frame_id) { | 108 void AudioSender::OnAck(uint32 frame_id) { |
105 } | 109 } |
106 | 110 |
| 111 void AudioSender::OnEncodedAudioFrame( |
| 112 int encoder_bitrate, |
| 113 scoped_ptr<EncodedFrame> encoded_frame, |
| 114 int samples_skipped) { |
| 115 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); |
| 116 |
| 117 samples_in_encoder_ -= audio_encoder_->GetSamplesPerFrame() + samples_skipped; |
| 118 DCHECK_GE(samples_in_encoder_, 0); |
| 119 |
| 120 SendEncodedFrame(encoder_bitrate, encoded_frame.Pass()); |
| 121 } |
| 122 |
107 } // namespace cast | 123 } // namespace cast |
108 } // namespace media | 124 } // namespace media |
OLD | NEW |