| 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_encoder.h" | 5 #include "media/cast/sender/audio_encoder.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <limits> | 8 #include <limits> |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| 11 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/bind_helpers.h" | 12 #include "base/bind_helpers.h" |
| 13 #include "base/location.h" | 13 #include "base/location.h" |
| 14 #include "base/stl_util.h" | 14 #include "base/stl_util.h" |
| 15 #include "base/sys_byteorder.h" | 15 #include "base/sys_byteorder.h" |
| 16 #include "base/time/time.h" | 16 #include "base/time/time.h" |
| 17 #include "base/trace_event/trace_event.h" |
| 17 #include "media/cast/cast_defines.h" | 18 #include "media/cast/cast_defines.h" |
| 18 | 19 |
| 19 #if !defined(OS_IOS) | 20 #if !defined(OS_IOS) |
| 20 #include "third_party/opus/src/include/opus.h" | 21 #include "third_party/opus/src/include/opus.h" |
| 21 #endif | 22 #endif |
| 22 | 23 |
| 23 #if defined(OS_MACOSX) | 24 #if defined(OS_MACOSX) |
| 24 #include <AudioToolbox/AudioToolbox.h> | 25 #include <AudioToolbox/AudioToolbox.h> |
| 25 #endif | 26 #endif |
| 26 | 27 |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 100 const int64 num_frames_missed = amount_ahead_by / frame_duration_; | 101 const int64 num_frames_missed = amount_ahead_by / frame_duration_; |
| 101 if (num_frames_missed > kUnderrunSkipThreshold) { | 102 if (num_frames_missed > kUnderrunSkipThreshold) { |
| 102 samples_dropped_from_buffer_ += buffer_fill_end_; | 103 samples_dropped_from_buffer_ += buffer_fill_end_; |
| 103 buffer_fill_end_ = 0; | 104 buffer_fill_end_ = 0; |
| 104 buffer_fill_duration = base::TimeDelta(); | 105 buffer_fill_duration = base::TimeDelta(); |
| 105 frame_rtp_timestamp_ += | 106 frame_rtp_timestamp_ += |
| 106 static_cast<uint32>(num_frames_missed * samples_per_frame_); | 107 static_cast<uint32>(num_frames_missed * samples_per_frame_); |
| 107 DVLOG(1) << "Skipping RTP timestamp ahead to account for " | 108 DVLOG(1) << "Skipping RTP timestamp ahead to account for " |
| 108 << num_frames_missed * samples_per_frame_ | 109 << num_frames_missed * samples_per_frame_ |
| 109 << " samples' worth of underrun."; | 110 << " samples' worth of underrun."; |
| 111 TRACE_EVENT_INSTANT2("cast.stream", "Audio Skip", |
| 112 TRACE_EVENT_SCOPE_THREAD, |
| 113 "frames missed", num_frames_missed, |
| 114 "sample_dropped", samples_dropped_from_buffer_); |
| 110 } | 115 } |
| 111 } | 116 } |
| 112 frame_capture_time_ = recorded_time - buffer_fill_duration; | 117 frame_capture_time_ = recorded_time - buffer_fill_duration; |
| 113 | 118 |
| 114 // Encode all audio in |audio_bus| into zero or more frames. | 119 // Encode all audio in |audio_bus| into zero or more frames. |
| 115 int src_pos = 0; | 120 int src_pos = 0; |
| 116 while (src_pos < audio_bus->frames()) { | 121 while (src_pos < audio_bus->frames()) { |
| 117 // Note: This is used to compute the deadline utilization and so it uses | 122 // Note: This is used to compute the deadline utilization and so it uses |
| 118 // the real-world clock instead of the CastEnvironment clock, the latter | 123 // the real-world clock instead of the CastEnvironment clock, the latter |
| 119 // of which might be simulated. | 124 // of which might be simulated. |
| (...skipping 11 matching lines...) Expand all Loading... |
| 131 break; | 136 break; |
| 132 | 137 |
| 133 scoped_ptr<SenderEncodedFrame> audio_frame( | 138 scoped_ptr<SenderEncodedFrame> audio_frame( |
| 134 new SenderEncodedFrame()); | 139 new SenderEncodedFrame()); |
| 135 audio_frame->dependency = EncodedFrame::KEY; | 140 audio_frame->dependency = EncodedFrame::KEY; |
| 136 audio_frame->frame_id = frame_id_; | 141 audio_frame->frame_id = frame_id_; |
| 137 audio_frame->referenced_frame_id = frame_id_; | 142 audio_frame->referenced_frame_id = frame_id_; |
| 138 audio_frame->rtp_timestamp = frame_rtp_timestamp_; | 143 audio_frame->rtp_timestamp = frame_rtp_timestamp_; |
| 139 audio_frame->reference_time = frame_capture_time_; | 144 audio_frame->reference_time = frame_capture_time_; |
| 140 | 145 |
| 146 TRACE_EVENT_ASYNC_BEGIN2("cast.stream", "Audio Encode", audio_frame.get(), |
| 147 "frame_id", frame_id_, |
| 148 "rtp_timestamp", frame_rtp_timestamp_); |
| 141 if (EncodeFromFilledBuffer(&audio_frame->data)) { | 149 if (EncodeFromFilledBuffer(&audio_frame->data)) { |
| 142 // Compute deadline utilization as the real-world time elapsed divided | 150 // Compute deadline utilization as the real-world time elapsed divided |
| 143 // by the signal duration. | 151 // by the signal duration. |
| 144 audio_frame->deadline_utilization = | 152 audio_frame->deadline_utilization = |
| 145 (base::TimeTicks::Now() - start_time).InSecondsF() / | 153 (base::TimeTicks::Now() - start_time).InSecondsF() / |
| 146 frame_duration_.InSecondsF(); | 154 frame_duration_.InSecondsF(); |
| 147 | 155 |
| 156 TRACE_EVENT_ASYNC_END1("cast.stream", "Audio Encode", audio_frame.get(), |
| 157 "Deadline utilization", |
| 158 audio_frame->deadline_utilization); |
| 148 cast_environment_->PostTask( | 159 cast_environment_->PostTask( |
| 149 CastEnvironment::MAIN, | 160 CastEnvironment::MAIN, |
| 150 FROM_HERE, | 161 FROM_HERE, |
| 151 base::Bind(callback_, | 162 base::Bind(callback_, |
| 152 base::Passed(&audio_frame), | 163 base::Passed(&audio_frame), |
| 153 samples_dropped_from_buffer_)); | 164 samples_dropped_from_buffer_)); |
| 154 samples_dropped_from_buffer_ = 0; | 165 samples_dropped_from_buffer_ = 0; |
| 155 } | 166 } |
| 156 | 167 |
| 157 // Reset the internal buffer, frame ID, and timestamps for the next frame. | 168 // Reset the internal buffer, frame ID, and timestamps for the next frame. |
| (...skipping 673 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 831 cast_environment_->PostTask(CastEnvironment::AUDIO, | 842 cast_environment_->PostTask(CastEnvironment::AUDIO, |
| 832 FROM_HERE, | 843 FROM_HERE, |
| 833 base::Bind(&AudioEncoder::ImplBase::EncodeAudio, | 844 base::Bind(&AudioEncoder::ImplBase::EncodeAudio, |
| 834 impl_, | 845 impl_, |
| 835 base::Passed(&audio_bus), | 846 base::Passed(&audio_bus), |
| 836 recorded_time)); | 847 recorded_time)); |
| 837 } | 848 } |
| 838 | 849 |
| 839 } // namespace cast | 850 } // namespace cast |
| 840 } // namespace media | 851 } // namespace media |
| OLD | NEW |