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()
39 : m_json_obj(nullptr), m_json_device_files_obj(nullptr)
41 open_json("/etc/pulse/device-map.json");
44 CDeviceMapParser::CDeviceMapParser(const char* map_file)
45 : m_json_obj(nullptr), m_json_device_files_obj(nullptr)
50 CDeviceMapParser::~CDeviceMapParser()
55 void CDeviceMapParser::open_json(const char* json_file)
57 m_json_obj = json_object_from_file(json_file);
59 cout << "Read device-map " << json_file << " failed" << endl;
63 if (!json_object_object_get_ex(m_json_obj, DEVICE_FILE_OBJECT, &m_json_device_files_obj)) {
64 cout << "Get device files object failed" << endl;
67 if (!json_object_is_type(m_json_device_files_obj, json_type_object)) {
68 cout << "json object type failed" << endl;
69 json_object_put(m_json_obj);
74 cout << DEVICE_FILE_OBJECT << " : {" << endl;
77 void CDeviceMapParser::close_json()
82 json_object_put(m_json_obj);
87 void CDeviceMapParser::parse_playback()
89 json_object *playback_devices_o = nullptr;
91 if (!json_object_object_get_ex(m_json_device_files_obj, DEVICE_TYPE_PROP_PLAYBACK_DEVICES, &playback_devices_o)) {
92 cout << "failed to get playback" << endl;
96 cout << " " << DEVICE_TYPE_PROP_PLAYBACK_DEVICES << " : [" << endl;
97 parse_device_file_array_object(playback_devices_o, m_playback);
101 void CDeviceMapParser::parse_capture()
103 json_object *capture_devices_o = nullptr;
105 if (!json_object_object_get_ex(m_json_device_files_obj, DEVICE_TYPE_PROP_CAPTURE_DEVICES, &capture_devices_o)) {
106 cout << "failed to get capture" << endl;
110 cout << " " << DEVICE_TYPE_PROP_CAPTURE_DEVICES << " : [" << endl;
111 parse_device_file_array_object(capture_devices_o, m_capture);
112 cout << " ]" << endl;
115 void CDeviceMapParser::get_device(string& s, string& card, string& device_num)
118 string delimiter = ",";
119 string s1(s.substr(s.find_last_of(':') + 1));
122 size_t pos = s1.find(delimiter);
123 string token(s1.substr(0, pos));
124 s1.erase(0, pos + delimiter.length());
127 device_num.assign(s1);
130 void CDeviceMapParser::get_single_param(string& s, int& rate, int& channels)
133 string delimiter = "=";
134 size_t pos = s.find(delimiter);
135 string token = s.substr(0, pos);
136 s.erase(0, pos + delimiter.length());
138 if (token.compare("rate") == 0)
140 else if (token.compare("channels") == 0)
144 void CDeviceMapParser::get_params(string& s, int& rate, int& channels)
146 // eg. rate=44100 channels=1
147 string delimiter = " ";
152 while ((pos = s1.find(delimiter)) != string::npos) {
153 token = s1.substr(0, pos);
154 get_single_param(token, rate, channels);
155 s1.erase(0, pos + delimiter.length());
157 get_single_param(s1, rate, channels);
161 void CDeviceMapParser::dump_devices()
163 string card, device_num;
164 int rate = -1, channels = -1;
166 get_playback(card, device_num, rate, channels);
167 get_capture(card, device_num, rate, channels);
170 void CDeviceMapParser::get_playback(string& card, string& device_num, int& rate, int& channels)
174 get_device(m_playback.first, card, device_num);
175 get_params(m_playback.second, rate, channels);
176 cout << " 1. PLAYBACK" << endl;
177 cout << " > card=" << card << ", device=" << device_num << endl;
178 cout << " > rate=" << rate << ", channels=" << channels << endl << endl;
181 void CDeviceMapParser::get_capture(string& card, string& device_num, int& rate, int& channels)
185 get_device(m_capture.first, card, device_num);
186 get_params(m_capture.second, rate, channels);
187 cout << " 2. CAPTURE" << endl;
188 cout << " > card=" << card << ", device=" << device_num << endl;
189 cout << " > rate=" << rate << ", channels=" << channels << endl;
192 void CDeviceMapParser::parse_device_string_object(json_object *device_string_o, string& device_string)
194 assert(device_string_o);
195 assert(json_object_is_type(device_string_o, json_type_string));
198 // device-string : alsa:sprdphone,0,
200 device_string.assign(json_object_get_string(device_string_o));
202 cout << " " << DEVICE_TYPE_PROP_DEVICE_STRING << " : " << device_string << "," << endl;
205 void CDeviceMapParser::parse_device_role_object(json_object *device_role_o, string& device_params)
207 struct json_object_iterator it, it_end;
209 assert(device_role_o);
210 assert(json_object_is_type(device_role_o, json_type_object));
213 // role : { normal : rate=44100 }
215 it = json_object_iter_begin(device_role_o);
216 it_end = json_object_iter_end(device_role_o);
218 while (!json_object_iter_equal(&it, &it_end)) {
219 if (strcmp(json_object_iter_peek_name(&it), "normal") == 0) {
220 device_params.assign(json_object_get_string(json_object_iter_peek_value(&it)));
221 cout << " " << DEVICE_TYPE_PROP_ROLE << " : { normal : " << device_params << " }" << endl;
225 json_object_iter_next(&it);
229 void CDeviceMapParser::parse_device_file_object(json_object *device_file_o, pair<string, string>& device)
231 json_object *device_file_prop_o = nullptr;
232 string device_string, device_param;
234 assert(device_file_o);
235 assert(json_object_is_type(device_file_o, json_type_object));
238 // device-string : alsa:sprdphone,0,
239 // role : { normal : rate=44100 }
242 if (!json_object_object_get_ex(device_file_o, DEVICE_TYPE_PROP_ROLE, &device_file_prop_o)) {
243 cout << "Get device role object failed" << endl;
246 parse_device_role_object(device_file_prop_o, device_param);
248 if (device_param.empty()) {
249 cout << " " << "[E] This is not a normal device..skip" << endl;
253 // parse device-string
254 if (!json_object_object_get_ex(device_file_o, DEVICE_TYPE_PROP_DEVICE_STRING, &device_file_prop_o)) {
255 cout << "Get device-string object failed" << endl;
258 parse_device_string_object(device_file_prop_o, device_string);
260 // store device information
261 device = make_pair(device_string, device_param);
264 void CDeviceMapParser::parse_device_file_array_object(json_object *device_file_array_o, pair<string, string>& device)
267 json_object *device_file_o = nullptr;
269 assert(device_file_array_o);
270 assert(json_object_is_type(device_file_array_o, json_type_array));
274 // device-string : alsa:sprdphone,0,
275 // role : { normal : rate=44100 }
278 // ToDo : this might be replaced with iterator such as foreach?
279 num = json_object_array_length(device_file_array_o);
280 for (idx = 0; idx < num; idx++) {
281 if (!(device_file_o = json_object_array_get_idx(device_file_array_o, idx))) {
282 cout << "Get device file object failed" << endl;
286 cout << " {" << endl;
287 parse_device_file_object(device_file_o, device);
288 cout << " }" << endl;