startRecording method

Future<void> startRecording()

If already connected, record the delivered video.

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

You can get the corresponding video from the URL in recordURL. m3u8 format files must be able to be played.

すでに接続されている場合、配信映像の録画を行います。

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

recordURLのURLから該当の映像を取得できます。m3u8形式のファイルを再生できる必要があります。

Implementation

Future<void> startRecording() async {
  if (recordingVideo) {
    throw Exception("Already started recording video.");
  }
  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": _recordingVideoId.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/mix/start",
        headers: {
          "Content-Type": "application/json;charset=utf-8",
          "Authorization": "Basic $credential",
        },
        body: jsonEncode(
          {
            "cname": channelName,
            "uid": _recordingVideoId.toString(),
            "clientRequest": {
              "token": _token,
              "recordingConfig": {
                "maxIdleTime": 30,
                "streamTypes": 2,
                "audioProfile": 1,
                "channelType": 1,
                "transcodingConfig": {
                  "height": videoDimensions.height,
                  "width": videoDimensions.width,
                  "bitrate": bitRate,
                  "fps": frameRate.toInt(),
                  "mixedVideoLayout": 1,
                  "backgroundColor": "#000000"
                },
              },
              "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")) {
          _sidVideo = resultMap.get("sid", "");
          _resourceVideoId = resultMap.get("resourceId", "");
          _sendLog(AgoraLoggerEvent.startRecording, parameters: {
            AgoraLoggerEvent.captureSid: _sidVideo,
            AgoraLoggerEvent.captureResourceId: _resourceVideoId,
          });
          if (adapter.storageBucketConfig?.enableDebug ?? false) {
            _debugRecording();
          }
          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);
  }
}