Skip to content

Commit

Permalink
fix: Fix getNativeHandle not return correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
littleGnAl committed Jan 13, 2023
1 parent f800c8f commit ada041f
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
40 changes: 40 additions & 0 deletions lib/src/impl/agora_rtc_engine_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -971,6 +971,46 @@ class RtcEngineImpl extends rtc_engine_ex_binding.RtcEngineExImpl
}
}

@override
Future<int> getNativeHandle() async {
final apiType =
'${isOverrideClassName ? className : 'RtcEngine'}_getNativeHandle';

final param = createParams({});
final callApiResult =
await apiCaller.callIrisApi(apiType, jsonEncode(param), buffers: null);
if (callApiResult.irisReturnCode < 0) {
throw AgoraRtcException(code: callApiResult.irisReturnCode);
}

// In 64-bits system, the native handle ptr value (unsigned long 64) can be 2^64 - 1,
// which may greater than the dart int max value (2^63 - 1), so we can not decode
// the json with big int native handle ptr value and parse it directly.
//
// After dart sdk 2.0 support parse hexadecimal in unsigned int64 range.
// https://github.com/dart-lang/language/blob/ee1135e0c22391cee17bf3ee262d6a04582d25de/archive/newsletter/20170929.md#semantics
//
// So we retrive the native handle ptr value from the json string directly, and
// parse an int from hexadecimal here.
final rawJsonStr = callApiResult.rawData;
// TODO(littlegnal): Replace retrive nativeHandleIntPtr logic after EP-253 landed.
final rawJsonStrSplit = rawJsonStr.split(',');
String resultStr = '';
for (final s in rawJsonStrSplit) {
if (s.contains('result')) {
resultStr = s;
}
}
final nativeHandleIntPtr =
resultStr.substring(resultStr.indexOf(':') + 1, resultStr.length - 1);

BigInt nativeHandleBI = BigInt.parse(nativeHandleIntPtr);
int nativeHandleBIHexInt =
int.parse('0x${nativeHandleBI.toRadixString(16)}');

return nativeHandleBIHexInt;
}

/////////// debug ////////
/// [type] see [VideoSourceType], only [VideoSourceType.videoSourceCamera], [VideoSourceType.videoSourceRemote] supported
Expand Down
7 changes: 5 additions & 2 deletions lib/src/impl/api_caller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,14 @@ void setMockIrisApiEngineIntPtr(int? mockIrisApiEngineIntPtr) {
const int kBasicResultLength = 64 * 1024;

class CallApiResult {
CallApiResult({required this.irisReturnCode, required this.data});
CallApiResult({required this.irisReturnCode, required this.data, this.rawData = ''});

final int irisReturnCode;

final Map<String, dynamic> data;

// TODO(littlegnal): Remove rawData after EP-253 landed.
final String rawData;
}

Uint8List uint8ListFromPtr(int intPtr, int length) {
Expand Down Expand Up @@ -448,7 +451,7 @@ class _ApiCallExecutorInternal implements _ApiCallExecutorBase {

final resultMap = Map<String, dynamic>.from(jsonDecode(result));

return CallApiResult(irisReturnCode: irisReturnCode, data: resultMap);
return CallApiResult(irisReturnCode: irisReturnCode, data: resultMap, rawData: result);
} catch (e) {
debugPrint(
'[_ApiCallExecutor] $funcName, params: $params\nerror: ${e.toString()}');
Expand Down

0 comments on commit ada041f

Please sign in to comment.