Commit 625a113a authored by Linshizhi's avatar Linshizhi

Add save() to MediaCodex.MP4.

parent f05f5de0
......@@ -625,6 +625,7 @@ namespace internal {
CPP(Encode) \
CPP(Close) \
CPP(GetVideo) \
CPP(Save) \
\
/* Web snapshots */ \
CPP(WebSnapshotSerialize) \
......
......@@ -173,8 +173,55 @@ BUILTIN(GetVideo) {
// Fill file content into TypedArray
videoFile.read(buffer, fileLength);
videoFile.close();
std::remove(enc->getOutputPath().c_str());
return *videoContent;
}
BUILTIN(Save) {
HandleScope scope(isolate);
Handle<Object> opath = args.atOrUndefined(isolate, 2);
if (!opath->IsString()) {
THROW_NEW_ERROR_RETURN_FAILURE(
isolate, NewTypeError(MessageTemplate::kInvalidArgument));
}
Handle<JSTypedArray> array;
ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, array,
JSTypedArray::Validate(isolate, args.atOrUndefined(isolate, 1), "MediaCodex.Mp4.save()"));
Handle<JSArrayBuffer> arrayBuffer = array->GetBuffer();
std::shared_ptr<BackingStore> store = arrayBuffer->GetBackingStore();
uint8_t* buffer = (uint8_t*)store->buffer_start();
int bufferLen = static_cast<int>(store->byte_length());
std::string opath_cppstr;
Handle<String> opath_str;
ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
isolate, opath_str, Object::ToString(isolate, opath));
opath_cppstr = opath_str->ToCString().get();
// Make sure file correspond to the path is not exists or
// is a regular file.
std::ofstream ofile { opath_cppstr, std::ofstream::binary };
if (!ofile) {
return *isolate->factory()->ToBoolean(false);
}
// Write buffer to file
ofile.write(reinterpret_cast<char*>(buffer), bufferLen);
if (ofile.bad()) {
return *isolate->factory()->ToBoolean(false);
}
ofile.close();
return *isolate->factory()->ToBoolean(true);
}
} // internal
} // v8
......@@ -2802,6 +2802,8 @@ void Genesis::InitializeGlobal(Handle<JSGlobalObject> global_object,
Builtin::kClose, 0, true);
SimpleInstallFunction(isolate_, mp4, "getVideo",
Builtin::kGetVideo, 0, true);
SimpleInstallFunction(isolate_, mp4, "save",
Builtin::kSave, 2, true);
InstallToStringTag(isolate_, media_codex, "MediaCodex");
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment