Wu-Cheng Li | 9e5ae90 | 2017-10-26 22:55:48 +0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2017 The Chromium OS Authors. All rights reserved. |
| 3 | * Use of this source code is governed by a BSD-style license that can be |
| 4 | * found in the LICENSE file. |
| 5 | */ |
| 6 | |
| 7 | #include <alsa/asoundlib.h> |
| 8 | #include <getopt.h> |
| 9 | #include <stdio.h> |
| 10 | #include <stdlib.h> |
| 11 | |
| 12 | static void print_supported_capture_channels(const char *dev) |
| 13 | { |
| 14 | int err; |
| 15 | unsigned int channels_min = 0, channels_max = 0; |
| 16 | snd_pcm_hw_params_t *params; |
| 17 | snd_pcm_t *handle; |
| 18 | |
| 19 | if ((err = snd_pcm_open(&handle, dev, SND_PCM_STREAM_CAPTURE, 0)) < 0) { |
| 20 | fprintf(stderr, "Cannot open audio device for capture: %s (%s)\n", |
| 21 | dev, snd_strerror(err)); |
| 22 | exit(EXIT_FAILURE); |
| 23 | } |
| 24 | |
| 25 | if ((err = snd_pcm_hw_params_malloc(¶ms)) < 0) { |
| 26 | fprintf(stderr, "Cannot allocate hw params structure (%s)\n", |
| 27 | snd_strerror(err)); |
| 28 | exit(EXIT_FAILURE); |
| 29 | } |
| 30 | |
| 31 | if ((err = snd_pcm_hw_params_any(handle, params)) < 0) { |
| 32 | fprintf(stderr, "Cannot initialize hw params structure (%s)\n", |
| 33 | snd_strerror(err)); |
| 34 | exit(EXIT_FAILURE); |
| 35 | } |
| 36 | |
| 37 | if ((err = snd_pcm_hw_params_get_channels_min(params, &channels_min)) < 0) { |
| 38 | fprintf(stderr, "Cannot get channels min (%s)\n", |
| 39 | snd_strerror(err)); |
| 40 | exit(EXIT_FAILURE); |
| 41 | } |
| 42 | fprintf(stderr, "channels min=%u\n", channels_min); |
| 43 | |
| 44 | if ((err = snd_pcm_hw_params_get_channels_max(params, &channels_max)) < 0) { |
| 45 | fprintf(stderr, "Cannot get channels max (%s)\n", |
| 46 | snd_strerror(err)); |
| 47 | exit(EXIT_FAILURE); |
| 48 | } |
| 49 | fprintf(stderr, "channels max=%u\n", channels_max); |
| 50 | |
| 51 | for (unsigned int i = channels_min; i <= channels_max; i++) { |
| 52 | if ((err = snd_pcm_hw_params_test_channels(handle, params, i)) < 0) { |
| 53 | fprintf(stderr, "Test channels %u failed (%s)\n", |
| 54 | i, snd_strerror(err)); |
| 55 | } else { |
| 56 | printf("%u\n", i); |
| 57 | fprintf(stderr, "Channels %u is supported\n", i); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | snd_pcm_hw_params_free(params); |
| 62 | snd_pcm_close(handle); |
| 63 | } |
| 64 | |
| 65 | |
| 66 | /* This program has helper functions that are used by autotests. The returned |
| 67 | values are printed in stdout. Debug and error logs are printed in stderr. |
| 68 | If the program fails to get the requested value, it returns 1. */ |
| 69 | int main(int argc, char *argv[]) |
| 70 | { |
| 71 | int c, get_capture_channels = 0; |
| 72 | const char *short_opt = "hd:c"; |
| 73 | char *dev = "hw:0,0"; |
| 74 | |
| 75 | struct option long_opt[] = |
| 76 | { |
| 77 | {"help", no_argument, NULL, 'h'}, |
| 78 | {"device", required_argument, NULL, 'd'}, |
| 79 | {"get_capture_channels", no_argument, NULL, 'c'}, |
| 80 | {NULL, 0, NULL, 0 } |
| 81 | }; |
| 82 | |
| 83 | while(1) { |
| 84 | c = getopt_long(argc, argv, short_opt, long_opt, NULL); |
| 85 | if (c == -1) |
| 86 | break; |
| 87 | switch(c) { |
| 88 | case 'd': |
| 89 | dev = optarg; |
| 90 | fprintf(stderr, "Assign device to %s\n", dev); |
| 91 | break; |
| 92 | |
| 93 | case 'c': |
| 94 | get_capture_channels = 1; |
| 95 | break; |
| 96 | |
| 97 | case 'h': |
| 98 | printf("Usage: %s [OPTIONS]\n", argv[0]); |
| 99 | printf(" -d, --device <Device> Device, default to hw:0,0\n"); |
| 100 | printf(" -h, --help Print this help and exit\n"); |
| 101 | printf(" -c --get_capture_channels Get supported channels of the " |
| 102 | "capture device\n"); |
| 103 | return(EXIT_SUCCESS); |
| 104 | break; |
| 105 | |
| 106 | default: |
| 107 | fprintf(stderr, "Try `%s --help' for more information.\n", argv[0]); |
| 108 | exit(EXIT_FAILURE); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | if (get_capture_channels) { |
| 113 | print_supported_capture_channels(dev); |
| 114 | exit(EXIT_SUCCESS); |
| 115 | } |
| 116 | |
| 117 | fprintf(stderr, "Try `%s --help' for more information.\n", argv[0]); |
| 118 | exit(EXIT_FAILURE); |
| 119 | } |