Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #define LOG_TAG "alsa_device_proxy" |
| 18 | /*#define LOG_NDEBUG 0*/ |
| 19 | /*#define LOG_PCM_PARAMS 0*/ |
| 20 | |
| 21 | #include <log/log.h> |
| 22 | |
| 23 | #include "alsa_device_proxy.h" |
| 24 | |
| 25 | #include "logging.h" |
| 26 | |
| 27 | #define DEFAULT_PERIOD_SIZE 1024 |
| 28 | #define DEFAULT_PERIOD_COUNT 2 |
| 29 | |
| 30 | //void proxy_init(alsa_device_proxy * proxy) |
| 31 | //{ |
| 32 | // proxy->profile = NULL; |
| 33 | // |
| 34 | // proxy->alsa_config.format = DEFAULT_SAMPLE_FORMAT; |
| 35 | // proxy->alsa_config.rate = DEFAULT_SAMPLE_RATE; |
| 36 | // proxy->alsa_config.channels = DEFAULT_CHANNEL_COUNT; |
| 37 | // |
| 38 | // proxy->alsa_config.period_size = DEFAULT_PERIOD_SIZE; |
| 39 | // proxy->alsa_config.period_count = DEFAULT_PERIOD_COUNT; |
| 40 | // |
| 41 | // proxy->pcm = NULL; |
| 42 | //} |
| 43 | |
| 44 | void proxy_prepare(alsa_device_proxy * proxy, alsa_device_profile* profile, |
| 45 | struct pcm_config * config) |
| 46 | { |
| 47 | ALOGV("proxy_prepare()"); |
| 48 | |
| 49 | proxy->profile = profile; |
| 50 | |
| 51 | #if LOG_PCM_PARAMS |
| 52 | log_pcm_config(config, "proxy_setup()"); |
| 53 | #endif |
| 54 | |
| 55 | proxy->alsa_config.format = |
| 56 | config->format != PCM_FORMAT_INVALID && profile_is_format_valid(profile, config->format) |
| 57 | ? config->format : profile->default_config.format; |
| 58 | proxy->alsa_config.rate = |
| 59 | config->rate != 0 && profile_is_sample_rate_valid(profile, config->rate) |
| 60 | ? config->rate : profile->default_config.rate; |
| 61 | proxy->alsa_config.channels = |
| 62 | config->channels != 0 && profile_is_channel_count_valid(profile, config->channels) |
| 63 | ? config->channels : profile->default_config.channels; |
| 64 | |
| 65 | proxy->alsa_config.period_count = profile->default_config.period_count; |
| 66 | proxy->alsa_config.period_size = |
| 67 | profile_get_period_size(proxy->profile, proxy->alsa_config.rate); |
| 68 | |
| 69 | proxy->pcm = NULL; |
| 70 | } |
| 71 | |
| 72 | int proxy_open(alsa_device_proxy * proxy) |
| 73 | { |
| 74 | alsa_device_profile* profile = proxy->profile; |
| 75 | |
| 76 | ALOGV("proxy_open(card:%d device:%d %s)", profile->card, profile->device, |
| 77 | profile->direction == PCM_OUT ? "PCM_OUT" : "PCM_IN"); |
| 78 | |
| 79 | proxy->pcm = pcm_open(profile->card, profile->device, profile->direction, &proxy->alsa_config); |
| 80 | if (proxy->pcm == NULL) { |
| 81 | return -ENOMEM; |
| 82 | } |
| 83 | |
| 84 | if (!pcm_is_ready(proxy->pcm)) { |
| 85 | ALOGE("[%s] proxy_open() pcm_open() failed: %s", LOG_TAG, pcm_get_error(proxy->pcm)); |
| 86 | #ifdef LOG_PCM_PARAMS |
| 87 | log_pcm_config(&proxy->alsa_config, "config"); |
| 88 | #endif |
| 89 | pcm_close(proxy->pcm); |
| 90 | proxy->pcm = NULL; |
| 91 | return -ENOMEM; |
| 92 | } |
| 93 | |
| 94 | return 0; |
| 95 | } |
| 96 | |
| 97 | void proxy_close(alsa_device_proxy * proxy) |
| 98 | { |
| 99 | pcm_close(proxy->pcm); |
| 100 | proxy->pcm = NULL; |
| 101 | } |
| 102 | |
| 103 | /* |
| 104 | * Sample Rate |
| 105 | */ |
| 106 | unsigned proxy_get_sample_rate(const alsa_device_proxy * proxy) |
| 107 | { |
| 108 | return proxy->alsa_config.rate; |
| 109 | } |
| 110 | |
| 111 | /* |
| 112 | * Format |
| 113 | */ |
| 114 | enum pcm_format proxy_get_format(const alsa_device_proxy * proxy) |
| 115 | { |
| 116 | return proxy->alsa_config.format; |
| 117 | } |
| 118 | |
| 119 | /* |
| 120 | * Channel Count |
| 121 | */ |
| 122 | unsigned proxy_get_channel_count(const alsa_device_proxy * proxy) |
| 123 | { |
| 124 | return proxy->alsa_config.channels; |
| 125 | } |
| 126 | |
| 127 | /* |
| 128 | * Other |
| 129 | */ |
| 130 | unsigned int proxy_get_period_size(const alsa_device_proxy * proxy) |
| 131 | { |
| 132 | return proxy->alsa_config.period_size; |
| 133 | } |
| 134 | |
| 135 | unsigned int proxy_get_period_count(const alsa_device_proxy * proxy) |
| 136 | { |
| 137 | return proxy->alsa_config.period_count; |
| 138 | } |
| 139 | |
| 140 | unsigned proxy_get_latency(const alsa_device_proxy * proxy) |
| 141 | { |
| 142 | return (proxy_get_period_size(proxy) * proxy_get_period_count(proxy) * 1000) |
| 143 | / proxy_get_sample_rate(proxy); |
| 144 | } |
| 145 | |
| 146 | /* |
| 147 | * I/O |
| 148 | */ |
| 149 | int proxy_write(const alsa_device_proxy * proxy, const void *data, unsigned int count) |
| 150 | { |
| 151 | return pcm_write(proxy->pcm, data, count); |
| 152 | } |
| 153 | |
| 154 | int proxy_read(const alsa_device_proxy * proxy, void *data, unsigned int count) |
| 155 | { |
| 156 | return pcm_read(proxy->pcm, data, count); |
| 157 | } |