startScreenCapture method

Future<void> startScreenCapture()

If already connected, take a screenshot of the delivered video.

The AgoraMasamuneAdapter.storageBucketConfig setting is mandatory and must also be configured in each Cloud and Functions.

The corresponding screenshot image can be obtained from the URL in screenCaptureURL.

すでに接続されている場合、配信映像のスクリーンショットを撮影します。

AgoraMasamuneAdapter.storageBucketConfigの設定が必須で、各クラウドやFunctionsでも設定が必要になります。

screenCaptureURLのURLから該当のスクリーンショット画像を取得できます。

Implementation

Future<void> startScreenCapture() async {
  if (capturing) {
    throw Exception("Already started taking screen capture.");
  }
  if (_engine == null) {
    throw Exception(
      "The engine is not initialized. [connect] the engine first.",
    );
  }
  if (adapter.customerId.isEmpty || adapter.customerSecret.isEmpty) {
    throw Exception(
      "The Customer ID and Customer Secret have not been set.",
    );
  }
  if (_token.isEmpty) {
    throw Exception("Token does not exist. Please run [connect] first.");
  }
  final credential = base64Encode(
    utf8.encode("${adapter.customerId}:${adapter.customerSecret}"),
  );
  final res = await Api.post(
    "$_agoraURL/${adapter.appId}/cloud_recording/acquire",
    headers: {
      "Content-Type": "application/json;charset=utf-8",
      "Authorization": "Basic $credential",
    },
    body: jsonEncode(
      {
        "cname": channelName,
        "uid": _recordingCaptureId.toString(),
        "clientRequest": {"resourceExpiredHour": 1},
      },
    ),
  );
  if (res.statusCode == 200) {
    final map = jsonDecodeAsMap(res.body);
    if (map.containsKey("resourceId")) {
      final result = await Api.post(
        "$_agoraURL/${adapter.appId}/cloud_recording/resourceid/${map.get("resourceId", "")}/mode/individual/start",
        headers: {
          "Content-Type": "application/json;charset=utf-8",
          "Authorization": "Basic $credential",
        },
        body: jsonEncode(
          {
            "cname": channelName,
            "uid": _recordingCaptureId.toString(),
            "clientRequest": {
              "token": _token,
              "recordingConfig": {
                "maxIdleTime": 30,
                "streamTypes": 2,
                "channelType": 1,
                "subscribeUidGroup": 0
              },
              "snapshotConfig": {
                "captureInterval": 30,
                "fileType": ["jpg"]
              },
              "storageConfig": {
                "accessKey": adapter.storageBucketConfig?.accessKey,
                "region": 0,
                "bucket": adapter.storageBucketConfig?.bucketName,
                "secretKey": adapter.storageBucketConfig?.secretKey,
                "vendor": adapter.storageBucketConfig?.vendor.index ??
                    AgoraStorageVendor.googleCloud.index,
                "fileNamePrefix": adapter.storageBucketConfig == null ||
                        adapter.storageBucketConfig!.rootPath.isEmpty
                    ? []
                    : adapter.storageBucketConfig?.rootPath.split("/")
              }
            },
          },
        ),
      );
      if (result.statusCode == 200) {
        final resultMap = jsonDecodeAsMap(result.body);
        if (resultMap.containsKey("sid")) {
          _sidCapture = resultMap.get("sid", "");
          _resourceCaptureId = map.get("resourceId", "");
          _sendLog(AgoraLoggerEvent.startScreenCapture, parameters: {
            AgoraLoggerEvent.captureSid: _sidCapture,
            AgoraLoggerEvent.captureResourceId: _resourceCaptureId,
          });
          notifyListeners();
        } else {
          throw Exception("The capture could not be started.");
        }
      } else {
        throw Exception(result.body);
      }
    } else {
      throw Exception("The response is invalid.");
    }
  } else {
    throw Exception(res.body);
  }
}