1 /*
2  * Copyright (c) 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 #include "softbus_bus_center.h"
33 #include "softbus_common.h"
34 #include "token_setproc.h"
35 
36 enum class DeviceStatus : uint32_t {
37     DEVICE_IDLE = 0,
38     DEVICE_OPEN = 1,
39     DEVICE_START = 2,
40     DEVICE_STOP = 3,
41 };
42 
43 struct WAV_HEADER {
44     /* RIFF Chunk Descriptor */
45     uint8_t riff[4] = {'R', 'I', 'F', 'F'};
46     uint32_t chunkSize = 0;
47     uint8_t wave[4] = {'W', 'A', 'V', 'E'};
48     /* "fmt" sub-chunk */
49     uint8_t fmt[4] = {'f', 'm', 't', ' '};
50     uint32_t subchunk1Size = 16;
51     uint16_t audioFormat = 1;
52     uint16_t numOfChan = 2;
53     uint32_t samplesPerSec = 44100;
54     uint32_t bytesPerSec = 176400;
55     uint16_t blockAlign = 2;
56     uint16_t bitsPerSample = 16;
57     /* "data" sub-chunk */
58     uint8_t subchunk2ID[4] = {'d', 'a', 't', 'a'};
59     uint32_t subchunk2Size = 0;
60 };
61 
62 enum DAudioErrorCode {
63     DH_SUCCESS = 0,
64     ERR_DH_AUDIO_NULLPTR = -40000,
65     ERR_DH_AUDIO_FAILED = -40001,
66     ERR_DH_AUDIO_NOT_SUPPORT = -40002,
67 
68     ERR_DH_AUDIO_SA_WRITE_INTERFACE_TOKEN_FAILED = -40003,
69     ERR_DH_AUDIO_SA_WRITE_PARAM_FAIED = -40004,
70     ERR_DH_AUDIO_SA_CALLBACK_NOT_FOUND = -40005,
71     ERR_DH_AUDIO_SA_INVALID_INTERFACE_TOKEN = -40006,
72     ERR_DH_AUDIO_SA_WAIT_TIMEOUT = -40007,
73     ERR_DH_AUDIO_SA_PARAM_INVALID = -40008,
74     ERR_DH_AUDIO_SA_DEVICE_NOT_EXIST = -40009,
75     ERR_DH_AUDIO_SA_PROXY_NOT_INIT = -40010,
76     ERR_DH_AUDIO_SA_LOAD_FAILED = -40011,
77     ERR_DH_AUDIO_SA_STATUS_ERR = -40012,
78     ERR_DH_AUDIO_NOT_FOUND_KEY = -40013,
79     ERR_DH_AUDIO_SA_DEVID_ILLEGAL = -40014,
80     ERR_DH_AUDIO_SA_PERMISSION_FAIED = -40015,
81 
82     // trans error
83     ERR_DH_AUDIO_TRANS_ERROR = -40015,
84     ERR_DH_AUDIO_TRANS_ILLEGAL_OPERATION = -40016,
85     ERR_DH_AUDIO_TRANS_SESSION_NOT_OPEN = -40017,
86 
87     // codec error
88     ERR_DH_AUDIO_BAD_VALUE = -42000,
89     ERR_DH_AUDIO_BAD_OPERATE = -42001,
90     ERR_DH_AUDIO_CODEC_CONFIG = -42002,
91     ERR_DH_AUDIO_CODEC_START = -42003,
92     ERR_DH_AUDIO_CODEC_STOP = -42004,
93     ERR_DH_AUDIO_CODEC_RELEASE = -42005,
94     ERR_DH_AUDIO_CODEC_INPUT = -42006,
95 
96     // spk client error
97     ERR_DH_AUDIO_CLIENT_PARAM_ERROR = -43000,
98     ERR_DH_AUDIO_CLIENT_RENDER_CREATE_FAILED = -43001,
99     ERR_DH_AUDIO_CLIENT_RENDER_STARTUP_FAILURE = -43002,
100     ERR_DH_AUDIO_CLIENT_RENDER_STOP_FAILED = -43003,
101     ERR_DH_AUDIO_CLIENT_RENDER_RELEASE_FAILED = -43004,
102     ERR_DH_AUDIO_CLIENT_SET_VOLUME_FAILED = -43005,
103     ERR_DH_AUDIO_CLIENT_SET_MUTE_FAILED = -43006,
104 
105     // mic client error
106     ERR_DH_AUDIO_CLIENT_CAPTURER_CREATE_FAILED = -43007,
107     ERR_DH_AUDIO_CLIENT_CAPTURER_START_FAILED = -43008,
108 
109     // other error
110     ERR_DH_AUDIO_HDI_CALL_FAILED = -44000,
111     ERR_DH_AUDIO_HDI_INVALID_PARAM = -44001,
112     ERR_DH_AV_TRANS_CREATE_CHANNEL_FAILED = -44002,
113     ERR_DH_AUDIO_ACCESS_PERMISSION_CHECK_FAIL = -44003,
114 };
115 
116 using WavHdr = struct WAV_HEADER;
117 int32_t InitTestDemo();
118 std::string FindAudioDevice();
119 std::string OpenSpk(std::string devId);
120 std::string StartRender();
121 std::string StopRender();
122 std::string CloseSpk();
123 std::string OpenMic(std::string devId);
124 std::string StartCapture();
125 std::string StopCapture();
126 std::string CloseMic();
127 std::string SetVolume(int vol);
128 std::string GetVolume();
129 std::string HandleAudioEvent(int32_t cmd);
130 int32_t GetSyncOnlineDevices(std::string& netWorkId);
131 void PrintNodeProperty(NodeBasicInfo *nodeInfo);
132 void PrintNodeProperty(NodeBasicInfo *nodeInfo, std::string& netWorkId);
133 void AddPermission(void);
134 #endif