xref: /OK3568_Linux_fs/external/rockit/mpi/example/mod/test_mpi_amix.cpp (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /*
2  * Copyright 2022 Rockchip Electronics Co. LTD
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 
18 #include <stdio.h>
19 #include <cstring>
20 #include <cstdlib>
21 #include "rk_debug.h"
22 #include "rk_mpi_amix.h"
23 #include "test_comm_argparse.h"
24 
25 typedef struct _rkTEST_AMIX_CTX_S {
26     RK_S32      s32DevId;
27     const char *psControlName;
28     char       *psControlValue;
29     RK_BOOL     bListControls;
30     RK_BOOL     bListContents;
31 } TEST_AMIX_CTX_S;
32 
unit_test_mpi_amix(TEST_AMIX_CTX_S * ctx)33 static RK_S32 unit_test_mpi_amix(TEST_AMIX_CTX_S *ctx) {
34     RK_S32 i = 0, ret = 0;
35 
36     if (ctx == RK_NULL)
37         return RK_FAILURE;
38 
39     if (ctx->psControlName && ctx->psControlValue) {
40         ret = RK_MPI_AMIX_SetControl(ctx->s32DevId, ctx->psControlName, ctx->psControlValue);
41         if (ret) {
42             RK_LOGE("RK_MPI_AMIX_SetControl failed, ret = %X", ret);
43             return RK_FAILURE;
44         }
45     } else if (ctx->psControlName && !ctx->psControlValue) {
46         char value[64] = {0};
47         // Usage 1: Print the current selected value of control
48         ret = RK_MPI_AMIX_GetControl(ctx->s32DevId, ctx->psControlName, value);
49         if (ret) {
50             RK_LOGE("RK_MPI_AMIX_GetControl failed, ret = %X", ret);
51             return RK_FAILURE;
52         }
53 
54         RK_PRINT("Get current control value: %s\n", value);
55         // Usage 2: List the values of control directly with the NULL value param
56         RK_PRINT("List the control values:\n");
57         ret = RK_MPI_AMIX_GetControl(ctx->s32DevId, ctx->psControlName, NULL);
58         if (ret) {
59             RK_LOGE("RK_MPI_AMIX_GetControl failed, ret = %X", ret);
60             return RK_FAILURE;
61         }
62     }
63 
64     if (ctx->bListControls)
65         RK_MPI_AMIX_ListControls(ctx->s32DevId);
66 
67     if (ctx->bListContents)
68         RK_MPI_AMIX_ListContents(ctx->s32DevId);
69 
70     return RK_SUCCESS;
71 }
72 
73 static const char *const usages[] = {
74     "./rk_mpi_amix_test [-C card] [--control ctl_name] [--value ctl_val] [--list_controls] [--list_contents]...",
75     NULL,
76 };
77 
mpi_amix_test_show_options(const TEST_AMIX_CTX_S * ctx)78 static void mpi_amix_test_show_options(const TEST_AMIX_CTX_S *ctx) {
79     RK_PRINT("cmd parse result:\n");
80     RK_PRINT("sound control id      : %d\n", ctx->s32DevId);
81     RK_PRINT("control name          : %s\n", ctx->psControlName);
82     RK_PRINT("control value         : %s\n", ctx->psControlValue);
83     RK_PRINT("list controls         : %d\n", ctx->bListControls);
84     RK_PRINT("list contents         : %d\n", ctx->bListContents);
85 }
86 
main(int argc,const char ** argv)87 int main(int argc, const char **argv) {
88     TEST_AMIX_CTX_S  *ctx = reinterpret_cast<TEST_AMIX_CTX_S *>(malloc(sizeof(TEST_AMIX_CTX_S)));
89 
90     memset(ctx, 0, sizeof(TEST_AMIX_CTX_S));
91     ctx->s32DevId           = 0;
92     ctx->psControlName      = RK_NULL;
93     ctx->psControlValue     = RK_NULL;
94     ctx->bListControls      = RK_FALSE;
95     ctx->bListContents      = RK_FALSE;
96 
97     struct argparse_option options[] = {
98         OPT_HELP(),
99         OPT_GROUP("basic options:"),
100         OPT_INTEGER('C', "card",  &(ctx->s32DevId),
101                    "specifies the card number of the mixer. default(0)", NULL, 0, 0),
102         OPT_STRING('\0', "control", &(ctx->psControlName),
103                     "sets (or gets without control value) the name of a control. default(required)", NULL, 0, 0),
104         OPT_STRING('\0', "value", &(ctx->psControlValue),
105                     "sets the value of a control. default(required)", NULL, 0, 0),
106         OPT_BOOLEAN('\0', "list_controls", &(ctx->bListControls),
107                     "lists controls of the mixer. default(false).", NULL, 0, 0),
108         OPT_BOOLEAN('\0', "list_contents", &(ctx->bListContents),
109                     "lists controls of the mixer and their contents. default(false).", NULL, 0, 0),
110         OPT_END(),
111     };
112 
113     struct argparse argparse;
114     argparse_init(&argparse, options, usages, 0);
115     argparse_describe(&argparse, "\nselect a test case to run.",
116                                  "\nuse --help for details.");
117 
118     argc = argparse_parse(&argparse, argc, argv);
119     mpi_amix_test_show_options(ctx);
120     if (ctx->psControlName == RK_NULL
121         && ctx->psControlValue == RK_NULL
122         && ctx->bListControls == RK_FALSE
123         && ctx->bListContents == RK_FALSE) {
124         argparse_usage(&argparse);
125         return RK_FAILURE;
126     }
127 
128     unit_test_mpi_amix(ctx);
129 
130     if (ctx) {
131         free(ctx);
132         ctx = RK_NULL;
133     }
134     return RK_SUCCESS;
135 }
136