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_logging"
18 /*#define LOG_NDEBUG 0*/
19 
20 #include <string.h>
21 
22 #include <log/log.h>
23 
24 #include "include/alsa_logging.h"
25 
26 #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
27 
28 /*
29  * Logging
30  */
log_pcm_mask(const char * mask_name,const struct pcm_mask * mask)31 void log_pcm_mask(const char* mask_name, const struct pcm_mask* mask)
32 {
33     const size_t num_slots = ARRAY_SIZE(mask->bits);
34     const size_t bits_per_slot = (sizeof(mask->bits[0]) * 8);
35     const size_t chars_per_slot = (bits_per_slot + 1); /* comma */
36 
37     const size_t BUFF_SIZE =
38             (num_slots * chars_per_slot + 2 + 1);  /* brackets and null-terminator */
39     char buff[BUFF_SIZE];
40     buff[0] = '\0';
41 
42     size_t slot_index, bit_index;
43     strcat(buff, "[");
44     for (slot_index = 0; slot_index < num_slots; slot_index++) {
45         unsigned bit_mask = 1;
46         for (bit_index = 0; bit_index < bits_per_slot; bit_index++) {
47             strcat(buff, (mask->bits[slot_index] & bit_mask) != 0 ? "1" : "0");
48             bit_mask <<= 1;
49         }
50         if (slot_index < num_slots - 1) {
51             strcat(buff, ",");
52         }
53     }
54     strcat(buff, "]");
55 
56     ALOGV("%s: mask:%s", mask_name, buff);
57 }
58 
log_pcm_params(struct pcm_params * alsa_hw_params)59 void log_pcm_params(struct pcm_params * alsa_hw_params)
60 {
61     ALOGV("usb:audio_hw - PCM_PARAM_SAMPLE_BITS min:%u, max:%u",
62           pcm_params_get_min(alsa_hw_params, PCM_PARAM_SAMPLE_BITS),
63           pcm_params_get_max(alsa_hw_params, PCM_PARAM_SAMPLE_BITS));
64     ALOGV("usb:audio_hw - PCM_PARAM_FRAME_BITS min:%u, max:%u",
65           pcm_params_get_min(alsa_hw_params, PCM_PARAM_FRAME_BITS),
66           pcm_params_get_max(alsa_hw_params, PCM_PARAM_FRAME_BITS));
67     log_pcm_mask("PCM_PARAM_FORMAT",
68                  pcm_params_get_mask(alsa_hw_params, PCM_PARAM_FORMAT));
69     log_pcm_mask("PCM_PARAM_SUBFORMAT",
70                  pcm_params_get_mask(alsa_hw_params, PCM_PARAM_SUBFORMAT));
71     ALOGV("usb:audio_hw - PCM_PARAM_CHANNELS min:%u, max:%u",
72           pcm_params_get_min(alsa_hw_params, PCM_PARAM_CHANNELS),
73           pcm_params_get_max(alsa_hw_params, PCM_PARAM_CHANNELS));
74     ALOGV("usb:audio_hw - PCM_PARAM_RATE min:%u, max:%u",
75           pcm_params_get_min(alsa_hw_params, PCM_PARAM_RATE),
76           pcm_params_get_max(alsa_hw_params, PCM_PARAM_RATE));
77     ALOGV("usb:audio_hw - PCM_PARAM_PERIOD_TIME min:%u, max:%u",
78           pcm_params_get_min(alsa_hw_params, PCM_PARAM_PERIOD_TIME),
79           pcm_params_get_max(alsa_hw_params, PCM_PARAM_PERIOD_TIME));
80     ALOGV("usb:audio_hw - PCM_PARAM_PERIOD_SIZE min:%u, max:%u",
81           pcm_params_get_min(alsa_hw_params, PCM_PARAM_PERIOD_SIZE),
82           pcm_params_get_max(alsa_hw_params, PCM_PARAM_PERIOD_SIZE));
83     ALOGV("usb:audio_hw - PCM_PARAM_PERIOD_BYTES min:%u, max:%u",
84           pcm_params_get_min(alsa_hw_params, PCM_PARAM_PERIOD_BYTES),
85           pcm_params_get_max(alsa_hw_params, PCM_PARAM_PERIOD_BYTES));
86     ALOGV("usb:audio_hw - PCM_PARAM_PERIODS min:%u, max:%u",
87           pcm_params_get_min(alsa_hw_params, PCM_PARAM_PERIODS),
88           pcm_params_get_max(alsa_hw_params, PCM_PARAM_PERIODS));
89     ALOGV("usb:audio_hw - PCM_PARAM_BUFFER_TIME min:%u, max:%u",
90           pcm_params_get_min(alsa_hw_params, PCM_PARAM_BUFFER_TIME),
91           pcm_params_get_max(alsa_hw_params, PCM_PARAM_BUFFER_TIME));
92     ALOGV("usb:audio_hw - PCM_PARAM_BUFFER_SIZE min:%u, max:%u",
93           pcm_params_get_min(alsa_hw_params, PCM_PARAM_BUFFER_SIZE),
94           pcm_params_get_max(alsa_hw_params, PCM_PARAM_BUFFER_SIZE));
95     ALOGV("usb:audio_hw - PCM_PARAM_BUFFER_BYTES min:%u, max:%u",
96           pcm_params_get_min(alsa_hw_params, PCM_PARAM_BUFFER_BYTES),
97           pcm_params_get_max(alsa_hw_params, PCM_PARAM_BUFFER_BYTES));
98     ALOGV("usb:audio_hw - PCM_PARAM_TICK_TIME min:%u, max:%u",
99           pcm_params_get_min(alsa_hw_params, PCM_PARAM_TICK_TIME),
100           pcm_params_get_max(alsa_hw_params, PCM_PARAM_TICK_TIME));
101 }
102 
log_pcm_config(struct pcm_config * config,const char * label)103 void log_pcm_config(struct pcm_config * config, const char* label) {
104     ALOGV("log_pcm_config() - %s", label);
105     ALOGV("  channels:%d", config->channels);
106     ALOGV("  rate:%d", config->rate);
107     ALOGV("  period_size:%d", config->period_size);
108     ALOGV("  period_count:%d", config->period_count);
109     ALOGV("  format:%d", config->format);
110 #if 0
111     /* Values to use for the ALSA start, stop and silence thresholds.  Setting
112      * any one of these values to 0 will cause the default tinyalsa values to be
113      * used instead.  Tinyalsa defaults are as follows.
114      *
115      * start_threshold   : period_count * period_size
116      * stop_threshold    : period_count * period_size
117      * silence_threshold : 0
118      */
119     unsigned int start_threshold;
120     unsigned int stop_threshold;
121     unsigned int silence_threshold;
122 
123     /* Minimum number of frames available before pcm_mmap_write() will actually
124      * write into the kernel buffer. Only used if the stream is opened in mmap mode
125      * (pcm_open() called with PCM_MMAP flag set).   Use 0 for default.
126      */
127     int avail_min;
128 #endif
129 }
130