Commit f27efcaf authored by Linshizhi's avatar Linshizhi

Implement global object ENCODER.

parent b85121b5
...@@ -623,6 +623,7 @@ namespace internal { ...@@ -623,6 +623,7 @@ namespace internal {
/* ENCODER */ \ /* ENCODER */ \
CPP(InitEncodeContext) \ CPP(InitEncodeContext) \
CPP(Encode) \ CPP(Encode) \
CPP(Close) \
\ \
/* Web snapshots */ \ /* Web snapshots */ \
CPP(WebSnapshotSerialize) \ CPP(WebSnapshotSerialize) \
......
#include <stdio.h> #include <stdio.h>
#include <iostream>
#include "src/execution/isolate.h"
#include "extensions/encoder/src/encoder.h" #include "extensions/encoder/src/encoder.h"
#include "src/builtins/builtins.h" #include "src/builtins/builtins.h"
#include "src/builtins/builtins-utils-inl.h" #include "src/builtins/builtins-utils-inl.h"
...@@ -8,23 +11,61 @@ ...@@ -8,23 +11,61 @@
namespace v8 { namespace v8 {
namespace internal { namespace internal {
namespace {
bool inited = false; bool inited = false;
LAIPIC_ENCODER::Encoder *enc;
}
BUILTIN(InitEncodeContext) { BUILTIN(InitEncodeContext) {
HandleScope scope(isolate); HandleScope scope(isolate);
// Output path
Handle<Object> path = args.atOrUndefined(isolate, 1); Handle<Object> path = args.atOrUndefined(isolate, 1);
Handle<String> string; // Width
ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, string, Handle<Object> width = args.atOrUndefined(isolate, 2);
// Height
Handle<Object> height = args.atOrUndefined(isolate, 3);
// Bitrate
Handle<Object> bitrate = args.atOrUndefined(isolate, 4);
// Framerate
Handle<Object> framerate = args.atOrUndefined(isolate, 5);
// Maybe an Encoder already exists.
if (inited) return *isolate->factory()->ToBoolean(true);
// Argument check
if (!path->IsString() || !width->IsNumber() || !height->IsNumber() ||
!bitrate->IsNumber() || !framerate->IsNumber()) {
THROW_NEW_ERROR_RETURN_FAILURE(
isolate, NewTypeError(MessageTemplate::kInvalidArgument));
}
// Convert to C/C++ type
Handle<String> path_str;
ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, path_str,
Object::ToString(isolate, path)); Object::ToString(isolate, path));
LAIPIC_ENCODER::Encoder enc { std::string outpath { path_str->ToCString().get() };
"", "/tmp/video.mp4", uint32_t width_int, height_int,
1920, 1080, 60000 bitrate_int, framerate_int;
}; width->ToUint32(&width_int);
height->ToUint32(&height_int);
bitrate->ToUint32(&bitrate_int);
framerate->ToUint32(&framerate_int);
printf("Encoder\n"); if (!path->IsString()) {
// Argument check
}
if (inited) return *isolate->factory()->ToBoolean(true); enc = new LAIPIC_ENCODER::Encoder {
"", outpath, width_int, width_int, bitrate_int,
};
if (!enc->isReady()) {
THROW_NEW_ERROR_RETURN_FAILURE(isolate,
NewEvalError(MessageTemplate::kInvalidArgument));
}
inited = true; inited = true;
...@@ -33,10 +74,48 @@ BUILTIN(InitEncodeContext) { ...@@ -33,10 +74,48 @@ BUILTIN(InitEncodeContext) {
BUILTIN(Encode) { BUILTIN(Encode) {
HandleScope scope(isolate); HandleScope scope(isolate);
//Handle<Object> frame = args.atOrUndefined(isolate, 1);
if (!inited) return *isolate->factory()->ToBoolean(false);
Handle<JSTypedArray> array; // = Handle<JSTypedArray>::cast(frame);
ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, array,
JSTypedArray::Validate(isolate, args.atOrUndefined(isolate, 1), "ENCODER.encode()"));
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());
if (bufferLen <= 0) {
// Operation of encodeing a zero length frame treat
// as a valid operation.
return *isolate->factory()->ToBoolean(true);
}
enc->encodeRGB(buffer, bufferLen);
if (!enc->isReady()) {
// Encoder fall into unready stat which
// means there is something wrong happens during
// during encodeing.
THROW_NEW_ERROR_RETURN_FAILURE(isolate,
NewEvalError(MessageTemplate::kInvalid));
}
return *isolate->factory()->ToBoolean(true);
}
BUILTIN(Close) {
HandleScope scope(isolate);
printf("Encoding...\n"); if (!inited) {
return *isolate->factory()->ToBoolean(true);
}
enc->close();
enc = nullptr;
inited = false;
return *isolate->factory()->ToBoolean(true); return *isolate->factory()->ToBoolean(true);
} }
......
...@@ -2791,9 +2791,11 @@ void Genesis::InitializeGlobal(Handle<JSGlobalObject> global_object, ...@@ -2791,9 +2791,11 @@ void Genesis::InitializeGlobal(Handle<JSGlobalObject> global_object,
factory->NewJSObject(isolate_->object_function(), AllocationType::kOld); factory->NewJSObject(isolate_->object_function(), AllocationType::kOld);
JSObject::AddProperty(isolate_, global, "ENCODER", encoder, DONT_ENUM); JSObject::AddProperty(isolate_, global, "ENCODER", encoder, DONT_ENUM);
SimpleInstallFunction(isolate_, encoder, "init", SimpleInstallFunction(isolate_, encoder, "init",
Builtin::kInitEncodeContext, 1, true); Builtin::kInitEncodeContext, 5, true);
SimpleInstallFunction(isolate_, encoder, "encode", SimpleInstallFunction(isolate_, encoder, "encode",
Builtin::kEncode, 0, true); Builtin::kEncode, 1, true);
SimpleInstallFunction(isolate_, encoder, "close",
Builtin::kClose, 0, true);
InstallToStringTag(isolate_, encoder, "ENCODER"); InstallToStringTag(isolate_, encoder, "ENCODER");
} }
......
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