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" |
11 #include "base/location.h" | 11 #include "base/location.h" |
12 #include "base/logging.h" | |
13 #include "base/stl_util.h" | 12 #include "base/stl_util.h" |
14 #include "base/sys_byteorder.h" | 13 #include "base/sys_byteorder.h" |
15 #include "base/time/time.h" | 14 #include "base/time/time.h" |
16 #include "media/base/audio_bus.h" | 15 #include "media/base/audio_bus.h" |
17 #include "media/cast/cast_defines.h" | 16 #include "media/cast/cast_defines.h" |
18 #include "media/cast/cast_environment.h" | 17 #include "media/cast/cast_environment.h" |
19 #include "media/cast/logging/logging_defines.h" | |
20 #include "third_party/opus/src/include/opus.h" | 18 #include "third_party/opus/src/include/opus.h" |
21 | 19 |
22 namespace media { | 20 namespace media { |
23 namespace cast { | 21 namespace cast { |
24 | 22 |
25 namespace { | 23 namespace { |
26 | 24 |
27 // The fixed number of audio frames per second and, inversely, the duration of | 25 // The fixed number of audio frames per second and, inversely, the duration of |
28 // one frame's worth of samples. | 26 // one frame's worth of samples. |
29 const int kFramesPerSecond = 100; | 27 const int kFramesPerSecond = 100; |
30 const int kFrameDurationMillis = 1000 / kFramesPerSecond; // No remainder! | 28 const int kFrameDurationMillis = 1000 / kFramesPerSecond; // No remainder! |
31 | 29 |
32 // Threshold used to decide whether audio being delivered to the encoder is | 30 // Threshold used to decide whether audio being delivered to the encoder is |
33 // coming in too slow with respect to the capture timestamps. | 31 // coming in too slow with respect to the capture timestamps. |
34 const int kUnderrunThresholdMillis = 3 * kFrameDurationMillis; | 32 const int kUnderrunThresholdMillis = 3 * kFrameDurationMillis; |
35 | 33 |
36 void LogAudioFrameEncodedEvent( | |
37 const scoped_refptr<media::cast::CastEnvironment>& cast_environment, | |
38 base::TimeTicks event_time, | |
39 media::cast::RtpTimestamp rtp_timestamp, | |
40 uint32 frame_id, | |
41 size_t frame_size) { | |
42 if (!cast_environment->CurrentlyOn(CastEnvironment::MAIN)) { | |
43 cast_environment->PostTask( | |
44 CastEnvironment::MAIN, | |
45 FROM_HERE, | |
46 base::Bind(&LogAudioFrameEncodedEvent, | |
47 cast_environment, event_time, | |
48 rtp_timestamp, frame_id, frame_size)); | |
49 return; | |
50 } | |
51 cast_environment->Logging()->InsertEncodedFrameEvent( | |
52 event_time, media::cast::FRAME_ENCODED, media::cast::AUDIO_EVENT, | |
53 rtp_timestamp, frame_id, | |
54 static_cast<int>(frame_size), /* key_frame - unused */ false, | |
55 /*target_bitrate - unused*/ 0); | |
56 } | |
57 | |
58 } // namespace | 34 } // namespace |
59 | 35 |
60 | 36 |
61 // Base class that handles the common problem of feeding one or more AudioBus' | 37 // Base class that handles the common problem of feeding one or more AudioBus' |
62 // data into a buffer and then, once the buffer is full, encoding the signal and | 38 // data into a buffer and then, once the buffer is full, encoding the signal and |
63 // emitting an EncodedFrame via the FrameEncodedCallback. | 39 // emitting an EncodedFrame via the FrameEncodedCallback. |
64 // | 40 // |
65 // Subclasses complete the implementation by handling the actual encoding | 41 // Subclasses complete the implementation by handling the actual encoding |
66 // details. | 42 // details. |
67 class AudioEncoder::ImplBase | 43 class AudioEncoder::ImplBase |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
143 | 119 |
144 scoped_ptr<transport::EncodedFrame> audio_frame( | 120 scoped_ptr<transport::EncodedFrame> audio_frame( |
145 new transport::EncodedFrame()); | 121 new transport::EncodedFrame()); |
146 audio_frame->dependency = transport::EncodedFrame::KEY; | 122 audio_frame->dependency = transport::EncodedFrame::KEY; |
147 audio_frame->frame_id = frame_id_; | 123 audio_frame->frame_id = frame_id_; |
148 audio_frame->referenced_frame_id = frame_id_; | 124 audio_frame->referenced_frame_id = frame_id_; |
149 audio_frame->rtp_timestamp = frame_rtp_timestamp_; | 125 audio_frame->rtp_timestamp = frame_rtp_timestamp_; |
150 audio_frame->reference_time = frame_capture_time_; | 126 audio_frame->reference_time = frame_capture_time_; |
151 | 127 |
152 if (EncodeFromFilledBuffer(&audio_frame->data)) { | 128 if (EncodeFromFilledBuffer(&audio_frame->data)) { |
153 LogAudioFrameEncodedEvent(cast_environment_, | |
154 cast_environment_->Clock()->NowTicks(), | |
155 audio_frame->rtp_timestamp, | |
156 audio_frame->frame_id, | |
157 audio_frame->data.size()); | |
158 cast_environment_->PostTask( | 129 cast_environment_->PostTask( |
159 CastEnvironment::MAIN, | 130 CastEnvironment::MAIN, |
160 FROM_HERE, | 131 FROM_HERE, |
161 base::Bind(callback_, base::Passed(&audio_frame))); | 132 base::Bind(callback_, base::Passed(&audio_frame))); |
162 } | 133 } |
163 | 134 |
164 // Reset the internal buffer, frame ID, and timestamps for the next frame. | 135 // Reset the internal buffer, frame ID, and timestamps for the next frame. |
165 buffer_fill_end_ = 0; | 136 buffer_fill_end_ = 0; |
166 ++frame_id_; | 137 ++frame_id_; |
167 frame_rtp_timestamp_ += samples_per_frame_; | 138 frame_rtp_timestamp_ += samples_per_frame_; |
(...skipping 234 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
402 cast_environment_->PostTask(CastEnvironment::AUDIO, | 373 cast_environment_->PostTask(CastEnvironment::AUDIO, |
403 FROM_HERE, | 374 FROM_HERE, |
404 base::Bind(&AudioEncoder::ImplBase::EncodeAudio, | 375 base::Bind(&AudioEncoder::ImplBase::EncodeAudio, |
405 impl_, | 376 impl_, |
406 base::Passed(&audio_bus), | 377 base::Passed(&audio_bus), |
407 recorded_time)); | 378 recorded_time)); |
408 } | 379 } |
409 | 380 |
410 } // namespace cast | 381 } // namespace cast |
411 } // namespace media | 382 } // namespace media |
OLD | NEW |