9 #define DEVICE_FILE_OBJECT "device-files"
10 #define DEVICE_TYPE_PROP_PLAYBACK_DEVICES "playback-devices"
11 #define DEVICE_TYPE_PROP_CAPTURE_DEVICES "capture-devices"
12 #define DEVICE_TYPE_PROP_DEVICE_STRING "device-string"
13 #define DEVICE_TYPE_PROP_ROLE "role"
15 /* device-files example
19 device-string : alsa:sprdphone,0,
20 role : { normal : rate=44100 }
23 device-string : alsa:VIRTUALAUDIOW,0,
24 role : { call-voice : rate=16000 channels=1 tsched=0 alternate_rate=16000 }
29 device-string : alsa:sprdphone,0,
30 role : { normal : rate=44100 }
38 CDeviceMapParser::CDeviceMapParser()
40 open_json("/etc/pulse/device-map.json");
43 CDeviceMapParser::CDeviceMapParser(const char* map_file)
48 CDeviceMapParser::~CDeviceMapParser()
53 void CDeviceMapParser::open_json(const char* json_file)
55 m_json_obj = json_object_from_file(json_file);
57 cout << "Read device-map " << json_file << " failed" << endl;
61 if (!json_object_object_get_ex(m_json_obj, DEVICE_FILE_OBJECT, &m_json_device_files_obj)) {
62 cout << "Get device files object failed" << endl;
65 if (!json_object_is_type(m_json_device_files_obj, json_type_object)) {
66 cout << "json object type failed" << endl;
67 json_object_put(m_json_obj);
72 cout << DEVICE_FILE_OBJECT << " : {" << endl;
75 void CDeviceMapParser::close_json()
80 json_object_put(m_json_obj);
85 void CDeviceMapParser::parse_playback()
87 json_object *playback_devices_o = NULL;
89 if (!json_object_object_get_ex(m_json_device_files_obj, DEVICE_TYPE_PROP_PLAYBACK_DEVICES, &playback_devices_o)) {
90 cout << "failed to get playback" << endl;
94 cout << " " << DEVICE_TYPE_PROP_PLAYBACK_DEVICES << " : [" << endl;
95 parse_device_file_array_object(playback_devices_o, m_playback);
99 void CDeviceMapParser::parse_capture()
101 json_object *capture_devices_o = NULL;
103 if (!json_object_object_get_ex(m_json_device_files_obj, DEVICE_TYPE_PROP_CAPTURE_DEVICES, &capture_devices_o)) {
104 cout << "failed to get capture" << endl;
108 cout << " " << DEVICE_TYPE_PROP_CAPTURE_DEVICES << " : [" << endl;
109 parse_device_file_array_object(capture_devices_o, m_capture);
110 cout << " ]" << endl;
113 void CDeviceMapParser::get_device(string& s, string& card, string& device_num)
116 string delimiter = ",";
117 string s1(s.substr(s.find_last_of(':') + 1));
120 size_t pos = s1.find(delimiter);
121 string token(s1.substr(0, pos));
122 s1.erase(0, pos + delimiter.length());
125 device_num.assign(s1);
128 void CDeviceMapParser::get_single_param(string& s, int& rate, int& channels)
131 string delimiter = "=";
132 size_t pos = s.find(delimiter);
133 string token = s.substr(0, pos);
134 s.erase(0, pos + delimiter.length());
136 if (token.compare("rate") == 0)
138 else if (token.compare("channels") == 0)
142 void CDeviceMapParser::get_params(string& s, int& rate, int& channels)
144 // eg. rate=44100 channels=1
145 string delimiter = " ";
150 while ((pos = s1.find(delimiter)) != string::npos) {
151 token = s1.substr(0, pos);
152 get_single_param(token, rate, channels);
153 s1.erase(0, pos + delimiter.length());
155 get_single_param(s1, rate, channels);
159 void CDeviceMapParser::dump_devices()
161 string card, device_num;
162 int rate = -1, channels = -1;
164 get_playback(card, device_num, rate, channels);
165 get_capture(card, device_num, rate, channels);
168 void CDeviceMapParser::get_playback(string& card, string& device_num, int& rate, int& channels)
172 get_device(m_playback.first, card, device_num);
173 get_params(m_playback.second, rate, channels);
174 cout << " 1. PLAYBACK" << endl;
175 cout << " > card=" << card << ", device=" << device_num << endl;
176 cout << " > rate=" << rate << ", channels=" << channels << endl << endl;
179 void CDeviceMapParser::get_capture(string& card, string& device_num, int& rate, int& channels)
183 get_device(m_capture.first, card, device_num);
184 get_params(m_capture.second, rate, channels);
185 cout << " 2. CAPTURE" << endl;
186 cout << " > card=" << card << ", device=" << device_num << endl;
187 cout << " > rate=" << rate << ", channels=" << channels << endl;
190 void CDeviceMapParser::parse_device_string_object(json_object *device_string_o, string& device_string)
192 assert(device_string_o);
193 assert(json_object_is_type(device_string_o, json_type_string));
196 // device-string : alsa:sprdphone,0,
198 device_string.assign(json_object_get_string(device_string_o));
200 cout << " " << DEVICE_TYPE_PROP_DEVICE_STRING << " : " << device_string << "," << endl;
203 void CDeviceMapParser::parse_device_role_object(json_object *device_role_o, string& device_params)
205 struct json_object_iterator it, it_end;
207 assert(device_role_o);
208 assert(json_object_is_type(device_role_o, json_type_object));
211 // role : { normal : rate=44100 }
213 it = json_object_iter_begin(device_role_o);
214 it_end = json_object_iter_end(device_role_o);
216 while (!json_object_iter_equal(&it, &it_end)) {
217 if (strcmp(json_object_iter_peek_name(&it), "normal") == 0) {
218 device_params.assign(json_object_get_string(json_object_iter_peek_value(&it)));
219 cout << " " << DEVICE_TYPE_PROP_ROLE << " : { normal : " << device_params << " }" << endl;
223 json_object_iter_next(&it);
227 void CDeviceMapParser::parse_device_file_object(json_object *device_file_o, pair<string, string>& device)
229 json_object *device_file_prop_o = NULL;
230 string device_string, device_param;
232 assert(device_file_o);
233 assert(json_object_is_type(device_file_o, json_type_object));
236 // device-string : alsa:sprdphone,0,
237 // role : { normal : rate=44100 }
240 if (!json_object_object_get_ex(device_file_o, DEVICE_TYPE_PROP_ROLE, &device_file_prop_o)) {
241 cout << "Get device role object failed" << endl;
244 parse_device_role_object(device_file_prop_o, device_param);
246 if (device_param.empty()) {
247 cout << " " << "[E] This is not a normal device..skip" << endl;
251 // parse device-string
252 if (!json_object_object_get_ex(device_file_o, DEVICE_TYPE_PROP_DEVICE_STRING, &device_file_prop_o)) {
253 cout << "Get device-string object failed" << endl;
256 parse_device_string_object(device_file_prop_o, device_string);
258 // store device information
259 device = make_pair(device_string, device_param);
262 void CDeviceMapParser::parse_device_file_array_object(json_object *device_file_array_o, pair<string, string>& device)
265 json_object *device_file_o = NULL;
267 assert(device_file_array_o);
268 assert(json_object_is_type(device_file_array_o, json_type_array));
272 // device-string : alsa:sprdphone,0,
273 // role : { normal : rate=44100 }
276 // ToDo : this might be replaced with iterator such as foreach?
277 num = json_object_array_length(device_file_array_o);
278 for (idx = 0; idx < num; idx++) {
279 if (!(device_file_o = json_object_array_get_idx(device_file_array_o, idx))) {
280 cout << "Get device file object failed" << endl;
284 cout << " {" << endl;
285 parse_device_file_object(device_file_o, device);
286 cout << " }" << endl;