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 pa_sample_format_t BitsToPASampleFormat(int bits_per_sample) { |
| 52 switch (bits_per_sample) { |
| 53 case 8: |
| 54 return PA_SAMPLE_U8; |
| 55 case 16: |
| 56 return PA_SAMPLE_S16LE; |
| 57 case 24: |
| 58 return PA_SAMPLE_S24LE; |
| 59 case 32: |
| 60 return PA_SAMPLE_S32LE; |
| 61 default: |
| 62 NOTREACHED() << "Invalid bits per sample: " << bits_per_sample; |
| 63 return PA_SAMPLE_INVALID; |
| 64 } |
| 65 } |
| 66 |
| 67 // pa_stream_success_cb_t |
| 68 void StreamSuccessCallback(pa_stream* s, int error, 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_context| and |pa_stream| state changed cb. |
| 75 void ContextStateCallback(pa_context* context, void* mainloop) { |
| 76 pa_threaded_mainloop* pa_mainloop = |
| 77 static_cast<pa_threaded_mainloop*>(mainloop); |
| 78 pa_threaded_mainloop_signal(pa_mainloop, 0); |
| 79 } |
| 80 |
| 81 pa_channel_map ChannelLayoutToPAChannelMap(ChannelLayout channel_layout) { |
| 82 pa_channel_map channel_map; |
| 83 pa_channel_map_init(&channel_map); |
| 84 |
| 85 channel_map.channels = ChannelLayoutToChannelCount(channel_layout); |
| 86 for (Channels ch = LEFT; ch < CHANNELS_MAX; |
| 87 ch = static_cast<Channels>(ch + 1)) { |
| 88 int channel_index = ChannelOrder(channel_layout, ch); |
| 89 if (channel_index < 0) |
| 90 continue; |
| 91 |
| 92 channel_map.map[channel_index] = ChromiumToPAChannelPosition(ch); |
| 93 } |
| 94 |
| 95 return channel_map; |
| 96 } |
| 97 |
| 98 void WaitForOperationCompletion(pa_threaded_mainloop* pa_mainloop, |
| 99 pa_operation* operation) { |
| 100 if (!operation) { |
| 101 DLOG(WARNING) << "Operation is NULL"; |
| 102 return; |
| 103 } |
| 104 |
| 105 while (pa_operation_get_state(operation) == PA_OPERATION_RUNNING) |
| 106 pa_threaded_mainloop_wait(pa_mainloop); |
| 107 |
| 108 pa_operation_unref(operation); |
| 109 } |
| 110 |
| 111 int GetHardwareLatencyInBytes(pa_stream* stream, |
| 112 int sample_rate, |
| 113 int bytes_per_frame) { |
| 114 DCHECK(stream); |
| 115 int negative = 0; |
| 116 pa_usec_t latency_micros = 0; |
| 117 if (pa_stream_get_latency(stream, &latency_micros, &negative) != 0) |
| 118 return 0; |
| 119 |
| 120 if (negative) |
| 121 return 0; |
| 122 |
| 123 return latency_micros * sample_rate * bytes_per_frame / |
| 124 base::Time::kMicrosecondsPerSecond; |
| 125 |
| 126 } |
| 127 |
| 128 } // namespace media |
OLD | NEW |