Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "media/audio/pulse/pulse_util.h" | |
| 6 | |
| 7 #include "base/logging.h" | |
| 8 #include "base/time.h" | |
| 9 #include "media/audio/audio_manager_base.h" | |
| 10 | |
| 11 namespace media { | |
| 12 | |
| 13 namespace { | |
| 14 | |
| 15 pa_channel_position ChromiumToPAChannelPosition(Channels channel) { | |
| 16 switch (channel) { | |
| 17 // PulseAudio does not differentiate between left/right and | |
| 18 // stereo-left/stereo-right, both translate to front-left/front-right. | |
| 19 case LEFT: | |
| 20 return PA_CHANNEL_POSITION_FRONT_LEFT; | |
| 21 case RIGHT: | |
| 22 return PA_CHANNEL_POSITION_FRONT_RIGHT; | |
| 23 case CENTER: | |
| 24 return PA_CHANNEL_POSITION_FRONT_CENTER; | |
| 25 case LFE: | |
| 26 return PA_CHANNEL_POSITION_LFE; | |
| 27 case BACK_LEFT: | |
| 28 return PA_CHANNEL_POSITION_REAR_LEFT; | |
| 29 case BACK_RIGHT: | |
| 30 return PA_CHANNEL_POSITION_REAR_RIGHT; | |
| 31 case LEFT_OF_CENTER: | |
| 32 return PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER; | |
| 33 case RIGHT_OF_CENTER: | |
| 34 return PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER; | |
| 35 case BACK_CENTER: | |
| 36 return PA_CHANNEL_POSITION_REAR_CENTER; | |
| 37 case SIDE_LEFT: | |
| 38 return PA_CHANNEL_POSITION_SIDE_LEFT; | |
| 39 case SIDE_RIGHT: | |
| 40 return PA_CHANNEL_POSITION_SIDE_RIGHT; | |
| 41 case CHANNELS_MAX: | |
| 42 return PA_CHANNEL_POSITION_INVALID; | |
| 43 default: | |
| 44 NOTREACHED() << "Invalid channel: " << channel; | |
| 45 return PA_CHANNEL_POSITION_INVALID; | |
| 46 } | |
| 47 } | |
| 48 | |
| 49 } // namespace | |
| 50 | |
| 51 AutoPulseLock::AutoPulseLock(pa_threaded_mainloop* pa_mainloop) | |
| 52 : pa_mainloop_(pa_mainloop) { | |
| 53 pa_threaded_mainloop_lock(pa_mainloop_); | |
| 54 } | |
| 55 | |
| 56 AutoPulseLock::~AutoPulseLock() { | |
| 57 pa_threaded_mainloop_unlock(pa_mainloop_); | |
| 58 } | |
| 59 | |
| 60 // static, pa_stream_success_cb_t | |
| 61 void StreamSuccessCallback(pa_stream* s, int error, void* mainloop) { | |
| 62 pa_threaded_mainloop* pa_mainloop = | |
| 63 static_cast<pa_threaded_mainloop*>(mainloop); | |
| 64 pa_threaded_mainloop_signal(pa_mainloop, 0); | |
| 65 } | |
| 66 | |
| 67 // |pa_context| and |pa_stream| state changed cb. | |
| 68 void ContextStateCallback(pa_context* context, void* mainloop) { | |
| 69 pa_threaded_mainloop* pa_mainloop = | |
| 70 static_cast<pa_threaded_mainloop*>(mainloop); | |
| 71 pa_threaded_mainloop_signal(pa_mainloop, 0); | |
| 72 } | |
| 73 | |
| 74 pa_sample_format_t BitsToPASampleFormat(int bits_per_sample) { | |
| 75 switch (bits_per_sample) { | |
| 76 case 8: | |
| 77 return PA_SAMPLE_U8; | |
| 78 case 16: | |
| 79 return PA_SAMPLE_S16LE; | |
| 80 case 24: | |
| 81 return PA_SAMPLE_S24LE; | |
| 82 case 32: | |
| 83 return PA_SAMPLE_S32LE; | |
| 84 default: | |
| 85 NOTREACHED() << "Invalid bits per sample: " << bits_per_sample; | |
| 86 return PA_SAMPLE_INVALID; | |
| 87 } | |
| 88 } | |
| 89 | |
| 90 pa_channel_map ChannelLayoutToPAChannelMap(ChannelLayout channel_layout) { | |
| 91 pa_channel_map channel_map; | |
| 92 pa_channel_map_init(&channel_map); | |
| 93 | |
| 94 channel_map.channels = ChannelLayoutToChannelCount(channel_layout); | |
| 95 for (Channels ch = LEFT; ch < CHANNELS_MAX; | |
| 96 ch = static_cast<Channels>(ch + 1)) { | |
|
DaleCurtis
2013/02/20 00:17:38
Indent.
no longer working on chromium
2013/02/20 14:43:38
Done.
| |
| 97 int channel_index = ChannelOrder(channel_layout, ch); | |
| 98 if (channel_index < 0) | |
| 99 continue; | |
| 100 | |
| 101 channel_map.map[channel_index] = ChromiumToPAChannelPosition(ch); | |
| 102 } | |
| 103 | |
| 104 return channel_map; | |
| 105 } | |
| 106 | |
| 107 void WaitForOperationCompletion(pa_threaded_mainloop* pa_mainloop, | |
| 108 pa_operation* operation) { | |
| 109 if (!operation) { | |
| 110 DLOG(WARNING) << "Operation is NULL"; | |
| 111 return; | |
| 112 } | |
| 113 | |
| 114 while (pa_operation_get_state(operation) == PA_OPERATION_RUNNING) | |
| 115 pa_threaded_mainloop_wait(pa_mainloop); | |
| 116 | |
| 117 pa_operation_unref(operation); | |
| 118 } | |
| 119 | |
| 120 int GetHardwareLatencyInBytes(pa_stream* stream, | |
| 121 int sample_rate, | |
| 122 int bytes_per_frame) { | |
| 123 DCHECK(stream); | |
| 124 int negative = 0; | |
| 125 pa_usec_t latency_micros = 0; | |
| 126 if (pa_stream_get_latency(stream, &latency_micros, &negative) != 0) | |
| 127 return 0; | |
| 128 | |
| 129 if (negative) | |
| 130 return 0; | |
| 131 | |
| 132 return latency_micros * sample_rate * bytes_per_frame / | |
| 133 base::Time::kMicrosecondsPerSecond; | |
| 134 } | |
| 135 | |
| 136 } // namespace media | |
| OLD | NEW |