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_encoder.h" | 5 #include "media/cast/audio_sender/audio_encoder.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
68 uint32 frame_id, | 68 uint32 frame_id, |
69 CastLoggingEvent type) { | 69 CastLoggingEvent type) { |
70 cast_environment_->Logging()->InsertFrameEvent( | 70 cast_environment_->Logging()->InsertFrameEvent( |
71 cast_environment_->Clock()->NowTicks(), type, rtp_timestamp, frame_id); | 71 cast_environment_->Clock()->NowTicks(), type, rtp_timestamp, frame_id); |
72 } | 72 } |
73 | 73 |
74 void EncodeAudio(const AudioBus* audio_bus, | 74 void EncodeAudio(const AudioBus* audio_bus, |
75 const base::TimeTicks& recorded_time, | 75 const base::TimeTicks& recorded_time, |
76 const base::Closure& done_callback) { | 76 const base::Closure& done_callback) { |
77 int src_pos = 0; | 77 int src_pos = 0; |
| 78 int packet_count = 0; |
78 while (audio_bus && src_pos < audio_bus->frames()) { | 79 while (audio_bus && src_pos < audio_bus->frames()) { |
79 const int num_samples_to_xfer = std::min( | 80 const int num_samples_to_xfer = std::min( |
80 samples_per_10ms_ - buffer_fill_end_, audio_bus->frames() - src_pos); | 81 samples_per_10ms_ - buffer_fill_end_, audio_bus->frames() - src_pos); |
81 DCHECK_EQ(audio_bus->channels(), num_channels_); | 82 DCHECK_EQ(audio_bus->channels(), num_channels_); |
82 TransferSamplesIntoBuffer( | 83 TransferSamplesIntoBuffer( |
83 audio_bus, src_pos, buffer_fill_end_, num_samples_to_xfer); | 84 audio_bus, src_pos, buffer_fill_end_, num_samples_to_xfer); |
84 src_pos += num_samples_to_xfer; | 85 src_pos += num_samples_to_xfer; |
85 buffer_fill_end_ += num_samples_to_xfer; | 86 buffer_fill_end_ += num_samples_to_xfer; |
86 | 87 |
87 if (src_pos == audio_bus->frames()) { | 88 if (src_pos == audio_bus->frames()) { |
(...skipping 29 matching lines...) Expand all Loading... |
117 audio_frame->rtp_timestamp, | 118 audio_frame->rtp_timestamp, |
118 audio_frame->frame_id, | 119 audio_frame->frame_id, |
119 kAudioFrameEncoded)); | 120 kAudioFrameEncoded)); |
120 // Compute an offset to determine the recorded time for the first | 121 // Compute an offset to determine the recorded time for the first |
121 // audio sample in the buffer. | 122 // audio sample in the buffer. |
122 const base::TimeDelta buffer_time_offset = | 123 const base::TimeDelta buffer_time_offset = |
123 (buffer_fill_end_ - src_pos) * | 124 (buffer_fill_end_ - src_pos) * |
124 base::TimeDelta::FromMilliseconds(10) / samples_per_10ms_; | 125 base::TimeDelta::FromMilliseconds(10) / samples_per_10ms_; |
125 // TODO(miu): Consider batching EncodedAudioFrames so we only post a | 126 // TODO(miu): Consider batching EncodedAudioFrames so we only post a |
126 // at most one task for each call to this method. | 127 // at most one task for each call to this method. |
127 cast_environment_->PostTask( | 128 // Postpone every packet by 10mS with respect to the previous. Playout |
| 129 // is postponed already by 10mS, and this will better correlate with |
| 130 // the pacer's expectations. |
| 131 //TODO(mikhal): Turn this into a list of packets. |
| 132 // Update the end2end allowed error once this is fixed. |
| 133 cast_environment_->PostDelayedTask( |
128 CastEnvironment::MAIN, | 134 CastEnvironment::MAIN, |
129 FROM_HERE, | 135 FROM_HERE, |
130 base::Bind(callback_, | 136 base::Bind(callback_, |
131 base::Passed(&audio_frame), | 137 base::Passed(&audio_frame), |
132 recorded_time - buffer_time_offset)); | 138 recorded_time - buffer_time_offset), |
| 139 base::TimeDelta::FromMilliseconds(packet_count * 10)); |
| 140 ++packet_count; |
133 } | 141 } |
134 buffer_fill_end_ = 0; | 142 buffer_fill_end_ = 0; |
135 } | 143 } |
136 } | 144 } |
137 } | 145 } |
138 | 146 |
139 protected: | 147 protected: |
140 virtual void TransferSamplesIntoBuffer(const AudioBus* audio_bus, | 148 virtual void TransferSamplesIntoBuffer(const AudioBus* audio_bus, |
141 int source_offset, | 149 int source_offset, |
142 int buffer_fill_offset, | 150 int buffer_fill_offset, |
(...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
359 | 367 |
360 void AudioEncoder::EncodeAudio(const AudioBus* audio_bus, | 368 void AudioEncoder::EncodeAudio(const AudioBus* audio_bus, |
361 const base::TimeTicks& recorded_time, | 369 const base::TimeTicks& recorded_time, |
362 const base::Closure& done_callback) { | 370 const base::Closure& done_callback) { |
363 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::AUDIO_ENCODER)); | 371 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::AUDIO_ENCODER)); |
364 impl_->EncodeAudio(audio_bus, recorded_time, done_callback); | 372 impl_->EncodeAudio(audio_bus, recorded_time, done_callback); |
365 } | 373 } |
366 | 374 |
367 } // namespace cast | 375 } // namespace cast |
368 } // namespace media | 376 } // namespace media |
OLD | NEW |