fetchIceConfig method
Implementation
Future<bool>fetchIceConfig() async {
final nowTimestamp = DateTime.now().millisecondsSinceEpoch;
final diffTime = nowTimestamp - _iceServerConfigTime;
const configTimespan = 15000000;
if (_iceServerFromConfig != null && _iceServerFromConfig!.isNotEmpty && diffTime < configTimespan) {
// cached config still usable
return true;
}
// load ice from disk if any
await _loadIceServerConfigFromDisk();
// check if saved config is usable
if (_iceServerFromConfig != null && _iceServerFromConfig!.isNotEmpty && diffTime < configTimespan) {
// cached config still usable
return true;
}
// if config not usable, need to get a new one from server
final appId = BaseEntity.stringValue(_currentCallMetaInfo, "appId") ?? "";
final token = BaseEntity.stringValue(_currentCallMetaInfo, "token") ?? "";
final apiUrl = sprintf(CONFIG_SERVER_URL, [_serverDomain, appId]);
Log().info("fetchIceConfig apiUrl = $apiUrl");
final header = {"authorization":token};
final response = await http.get(Uri.parse(apiUrl), headers: header);
Log().info("fetchIceConfig response.body = ${response.body}");
if (response.statusCode == 200) {
var result = jsonDecode(response.body);
if (result != null && result is Map<String,dynamic> && result.isNotEmpty) {
Log().info('fetchIceConfig result = $result');
final success = BaseEntity.boolValue(result, "success");
final data = result["data"];
if (data is List<dynamic>) {
final configs = data;
// Log().info("Call config = $configs");
if (success == true) {
List<IceServer> serverConfigs = [];
for (final item in configs) {
final config = item;
var url = config["urls"] as String ?? "";
url = url.replaceAll("turns", "turn");
final username = config["username"] ?? "";
final password = config["password"] ?? "";
if (url.isNotEmpty) {
final iceServer = IceServer(
url: url, username: username, password: password);
serverConfigs.add(iceServer);
}
}
if (serverConfigs.isNotEmpty) {
_iceServerFromConfig = serverConfigs;
_saveIceServerConfigToDisk(serverConfigs);
}
}
}
}
return true;
} else {
return false;
}
}