1 /*
2  * Copyright (c) 2023-2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef DISTRIBUTED_AUDIO_TEST_H
17 #define DISTRIBUTED_AUDIO_TEST_H
18 
19 #include <chrono>
20 #include <iostream>
21 #include <string>
22 #include <cstdlib>
23 #include <sys/stat.h>
24 #include <thread>
25 #include <vector>
26 
27 #include <v1_0/iaudio_adapter.h>
28 #include <v1_0/iaudio_manager.h>
29 #include <v1_0/iaudio_callback.h>
30 #include <v1_0/audio_types.h>
31 #include "distributed_hardware_log.h"
32 enum class DeviceStatus : uint32_t {
33     DEVICE_IDLE = 0,
34     DEVICE_OPEN = 1,
35     DEVICE_START = 2,
36     DEVICE_STOP = 3,
37 };
38 
39 struct WAV_HEADER {
40     /* RIFF Chunk Descriptor */
41     uint8_t riff[4] = {'R', 'I', 'F', 'F'};
42     uint32_t chunkSize = 0;
43     uint8_t wave[4] = {'W', 'A', 'V', 'E'};
44     /* "fmt" sub-chunk */
45     uint8_t fmt[4] = {'f', 'm', 't', ' '};
46     uint32_t subchunk1Size = 16;
47     uint16_t audioFormat = 1;
48     uint16_t numOfChan = 2;
49     uint32_t samplesPerSec = 44100;
50     uint32_t bytesPerSec = 176400;
51     uint16_t blockAlign = 2;
52     uint16_t bitsPerSample = 16;
53     /* "data" sub-chunk */
54     uint8_t subchunk2ID[4] = {'d', 'a', 't', 'a'};
55     uint32_t subchunk2Size = 0;
56 };
57 
58 enum DAudioErrorCode {
59     DH_SUCCESS = 0,
60     ERR_DH_AUDIO_NULLPTR = -40000,
61     ERR_DH_AUDIO_FAILED = -40001,
62     ERR_DH_AUDIO_NOT_SUPPORT = -40002,
63 
64     ERR_DH_AUDIO_SA_WRITE_INTERFACE_TOKEN_FAILED = -40003,
65     ERR_DH_AUDIO_SA_WRITE_PARAM_FAIED = -40004,
66     ERR_DH_AUDIO_SA_CALLBACK_NOT_FOUND = -40005,
67     ERR_DH_AUDIO_SA_INVALID_INTERFACE_TOKEN = -40006,
68     ERR_DH_AUDIO_SA_WAIT_TIMEOUT = -40007,
69     ERR_DH_AUDIO_SA_PARAM_INVALID = -40008,
70     ERR_DH_AUDIO_SA_DEVICE_NOT_EXIST = -40009,
71     ERR_DH_AUDIO_SA_PROXY_NOT_INIT = -40010,
72     ERR_DH_AUDIO_SA_LOAD_FAILED = -40011,
73     ERR_DH_AUDIO_SA_STATUS_ERR = -40012,
74     ERR_DH_AUDIO_NOT_FOUND_KEY = -40013,
75     ERR_DH_AUDIO_SA_DEVID_ILLEGAL = -40014,
76     ERR_DH_AUDIO_SA_PERMISSION_FAIED = -40015,
77 
78     // trans error
79     ERR_DH_AUDIO_TRANS_ERROR = -40015,
80     ERR_DH_AUDIO_TRANS_ILLEGAL_OPERATION = -40016,
81     ERR_DH_AUDIO_TRANS_SESSION_NOT_OPEN = -40017,
82 
83     // codec error
84     ERR_DH_AUDIO_BAD_VALUE = -42000,
85     ERR_DH_AUDIO_BAD_OPERATE = -42001,
86     ERR_DH_AUDIO_CODEC_CONFIG = -42002,
87     ERR_DH_AUDIO_CODEC_START = -42003,
88     ERR_DH_AUDIO_CODEC_STOP = -42004,
89     ERR_DH_AUDIO_CODEC_RELEASE = -42005,
90     ERR_DH_AUDIO_CODEC_INPUT = -42006,
91 
92     // spk client error
93     ERR_DH_AUDIO_CLIENT_PARAM_ERROR = -43000,
94     ERR_DH_AUDIO_CLIENT_RENDER_CREATE_FAILED = -43001,
95     ERR_DH_AUDIO_CLIENT_RENDER_STARTUP_FAILURE = -43002,
96     ERR_DH_AUDIO_CLIENT_RENDER_STOP_FAILED = -43003,
97     ERR_DH_AUDIO_CLIENT_RENDER_RELEASE_FAILED = -43004,
98     ERR_DH_AUDIO_CLIENT_SET_VOLUME_FAILED = -43005,
99     ERR_DH_AUDIO_CLIENT_SET_MUTE_FAILED = -43006,
100 
101     // mic client error
102     ERR_DH_AUDIO_CLIENT_CAPTURER_CREATE_FAILED = -43007,
103     ERR_DH_AUDIO_CLIENT_CAPTURER_START_FAILED = -43008,
104 
105     // other error
106     ERR_DH_AUDIO_HDI_CALL_FAILED = -44000,
107     ERR_DH_AUDIO_HDI_INVALID_PARAM = -44001,
108     ERR_DH_AV_TRANS_CREATE_CHANNEL_FAILED = -44002,
109     ERR_DH_AUDIO_ACCESS_PERMISSION_CHECK_FAIL = -44003,
110 };
111 
112 using WavHdr = struct WAV_HEADER;
113 int32_t InitTestDemo();
114 std::string FindAudioDevice();
115 std::string OpenSpk(std::string devId);
116 std::string StartRender();
117 std::string StopRender();
118 std::string CloseSpk();
119 std::string OpenMic(std::string devId);
120 std::string StartCapture();
121 std::string StopCapture();
122 std::string CloseMic();
123 std::string SetVolume(int vol);
124 std::string GetVolume();
125 std::string HandleAudioEvent(int32_t cmd);
126 #endif
127