Commit 1ab8cca1 authored by Linshizhi's avatar Linshizhi

Add AudioEffecter as Builtins.

parent 8065f496
...@@ -628,6 +628,7 @@ namespace internal { ...@@ -628,6 +628,7 @@ namespace internal {
CPP(Save) \ CPP(Save) \
CPP(AddAudioBuffer) \ CPP(AddAudioBuffer) \
CPP(MergeMP4) \ CPP(MergeMP4) \
CPP(ApplyAudioEffects) \
\ \
/* Web snapshots */ \ /* Web snapshots */ \
CPP(WebSnapshotSerialize) \ CPP(WebSnapshotSerialize) \
......
...@@ -17,16 +17,37 @@ char *audioPath; ...@@ -17,16 +17,37 @@ char *audioPath;
void *loadedSyms; void *loadedSyms;
void *encoder; void *encoder;
void *audioEffecter;
///////////////////////////////////////////////////////////////////////////////
// Dynamic load procedures //
///////////////////////////////////////////////////////////////////////////////
// Video Encoder
void* (*spawnEncoder)(const char *output, uint32_t width, void* (*spawnEncoder)(const char *output, uint32_t width,
uint32_t height, uint32_t bitrate, uint32_t height, uint32_t bitrate,
int fmt, int32_t framerate, int fmt, int32_t framerate,
std::string encoderName); const char *encoderName);
void (*encoding)(void *encoder_, const uint8_t *buff, int length); void (*encoding)(void *encoder_, const uint8_t *buff, int length);
bool (*encoderFailed)(void *encoder_); bool (*encoderFailed)(void *encoder_);
void (*closeEncoder)(void *encoder_); void (*closeEncoder)(void *encoder_);
const char* (*outputOfEncoder)(void *encoder_); const char* (*outputOfEncoder)(void *encoder_);
// Audio Effecter
void* (*getAudioEffecter)();
int (*addInputFileAE)(void *effecter, const char *filename, const double volume,
const double strong_end_time, const double start_time,
const double end_time, const double delay,
const double duration, const double fade_out_duration,
bool isLoop);
int (*doTranscodingAE)(void *effecter, const char *output, const double duration);
int (*isWeakAE)(void *effecter);
void (*removeInputsAE)(void *effecter);
// AV Merger
void* (*spawnMerger)(const char *output, const char *videoPath, const char *audioPath); void* (*spawnMerger)(const char *output, const char *videoPath, const char *audioPath);
void (*merging)(void *merger_); void (*merging)(void *merger_);
} }
BUILTIN(InitEncodeContext) { BUILTIN(InitEncodeContext) {
...@@ -39,8 +60,6 @@ BUILTIN(InitEncodeContext) { ...@@ -39,8 +60,6 @@ BUILTIN(InitEncodeContext) {
printf("Failed to load libencoder.so"); printf("Failed to load libencoder.so");
THROW_NEW_ERROR_RETURN_FAILURE( THROW_NEW_ERROR_RETURN_FAILURE(
isolate, NewEvalError(MessageTemplate::kInvalidArgument)); isolate, NewEvalError(MessageTemplate::kInvalidArgument));
} else {
printf("libencoder.so loaded\n");
} }
spawnEncoder = reinterpret_cast<decltype(spawnEncoder)>(dlsym(loadedSyms, "spawnEncoder")); spawnEncoder = reinterpret_cast<decltype(spawnEncoder)>(dlsym(loadedSyms, "spawnEncoder"));
...@@ -48,49 +67,77 @@ BUILTIN(InitEncodeContext) { ...@@ -48,49 +67,77 @@ BUILTIN(InitEncodeContext) {
THROW_NEW_ERROR_RETURN_FAILURE( THROW_NEW_ERROR_RETURN_FAILURE(
isolate, NewEvalError(MessageTemplate::kInvalidArgument)); isolate, NewEvalError(MessageTemplate::kInvalidArgument));
} }
printf("spawnEncoder loaded\n");
encoding = reinterpret_cast<decltype(encoding)>(dlsym(loadedSyms, "encoding")); encoding = reinterpret_cast<decltype(encoding)>(dlsym(loadedSyms, "encoding"));
if (encoding == NULL) { if (encoding == NULL) {
THROW_NEW_ERROR_RETURN_FAILURE( THROW_NEW_ERROR_RETURN_FAILURE(
isolate, NewEvalError(MessageTemplate::kInvalidArgument)); isolate, NewEvalError(MessageTemplate::kInvalidArgument));
} }
printf("encoding loaded\n");
encoderFailed = reinterpret_cast<decltype(encoderFailed)>(dlsym(loadedSyms, "encoderFailed")); encoderFailed = reinterpret_cast<decltype(encoderFailed)>(dlsym(loadedSyms, "encoderFailed"));
if (encoderFailed == NULL) { if (encoderFailed == NULL) {
THROW_NEW_ERROR_RETURN_FAILURE( THROW_NEW_ERROR_RETURN_FAILURE(
isolate, NewEvalError(MessageTemplate::kInvalidArgument)); isolate, NewEvalError(MessageTemplate::kInvalidArgument));
} }
printf("encoderFailed loaded\n");
closeEncoder = reinterpret_cast<decltype(closeEncoder)>(dlsym(loadedSyms, "closeEncoder")); closeEncoder = reinterpret_cast<decltype(closeEncoder)>(dlsym(loadedSyms, "closeEncoder"));
if (closeEncoder == NULL) { if (closeEncoder == NULL) {
THROW_NEW_ERROR_RETURN_FAILURE( THROW_NEW_ERROR_RETURN_FAILURE(
isolate, NewEvalError(MessageTemplate::kInvalidArgument)); isolate, NewEvalError(MessageTemplate::kInvalidArgument));
} }
printf("closeEncoder loaded\n");
outputOfEncoder = reinterpret_cast<decltype(outputOfEncoder)>(dlsym(loadedSyms, "outputOfEncoder")); outputOfEncoder = reinterpret_cast<decltype(outputOfEncoder)>(dlsym(loadedSyms, "outputOfEncoder"));
if (outputOfEncoder == NULL) { if (outputOfEncoder == NULL) {
THROW_NEW_ERROR_RETURN_FAILURE( THROW_NEW_ERROR_RETURN_FAILURE(
isolate, NewEvalError(MessageTemplate::kInvalidArgument)); isolate, NewEvalError(MessageTemplate::kInvalidArgument));
} }
printf("outputOfEncoder loaded\n");
spawnMerger = reinterpret_cast<decltype(spawnMerger)>(dlsym(loadedSyms, "spawnMerger")); spawnMerger = reinterpret_cast<decltype(spawnMerger)>(dlsym(loadedSyms, "spawnMerger"));
if (spawnMerger == NULL) { if (spawnMerger == NULL) {
THROW_NEW_ERROR_RETURN_FAILURE( THROW_NEW_ERROR_RETURN_FAILURE(
isolate, NewEvalError(MessageTemplate::kInvalidArgument)); isolate, NewEvalError(MessageTemplate::kInvalidArgument));
} }
printf("spawnMerger loaded\n");
merging = reinterpret_cast<decltype(merging)>(dlsym(loadedSyms, "merging")); merging = reinterpret_cast<decltype(merging)>(dlsym(loadedSyms, "merging"));
if (merging == NULL) { if (merging == NULL) {
THROW_NEW_ERROR_RETURN_FAILURE( THROW_NEW_ERROR_RETURN_FAILURE(
isolate, NewEvalError(MessageTemplate::kInvalidArgument)); isolate, NewEvalError(MessageTemplate::kInvalidArgument));
} }
printf("merging loaded\n");
getAudioEffecter = reinterpret_cast<decltype(getAudioEffecter)>(
dlsym(loadedSyms, "getAudioEffecter"));
if (getAudioEffecter == NULL) {
THROW_NEW_ERROR_RETURN_FAILURE(
isolate, NewEvalError(MessageTemplate::kInvalidArgument));
}
addInputFileAE = reinterpret_cast<decltype(addInputFileAE)>(
dlsym(loadedSyms, "addInputFileAE"));
if (addInputFileAE == NULL) {
THROW_NEW_ERROR_RETURN_FAILURE(
isolate, NewEvalError(MessageTemplate::kInvalidArgument));
}
doTranscodingAE = reinterpret_cast<decltype(doTranscodingAE)>(
dlsym(loadedSyms, "doTranscodingAE"));
if (doTranscodingAE == NULL) {
THROW_NEW_ERROR_RETURN_FAILURE(
isolate, NewEvalError(MessageTemplate::kInvalidArgument));
}
isWeakAE = reinterpret_cast<decltype(isWeakAE)>(
dlsym(loadedSyms, "isWeakAE"));
if (isWeakAE == NULL) {
THROW_NEW_ERROR_RETURN_FAILURE(
isolate, NewEvalError(MessageTemplate::kInvalidArgument));
}
removeInputsAE = reinterpret_cast<decltype(removeInputsAE)>(
dlsym(loadedSyms, "removeInputsAE"));
if (removeInputsAE == NULL) {
THROW_NEW_ERROR_RETURN_FAILURE(
isolate, NewEvalError(MessageTemplate::kInvalidArgument));
}
} }
// Width // Width
...@@ -217,6 +264,8 @@ BUILTIN(Close) { ...@@ -217,6 +264,8 @@ BUILTIN(Close) {
encoder = nullptr; encoder = nullptr;
inited = false; inited = false;
removeInputsAE(audioEffecter);
return *isolate->factory()->ToBoolean(true); return *isolate->factory()->ToBoolean(true);
} }
...@@ -296,7 +345,6 @@ BUILTIN(Save) { ...@@ -296,7 +345,6 @@ BUILTIN(Save) {
BUILTIN(AddAudioBuffer) { BUILTIN(AddAudioBuffer) {
HandleScope scope(isolate); HandleScope scope(isolate);
Handle<Object> audioBuffer = args.atOrUndefined(isolate, 1); Handle<Object> audioBuffer = args.atOrUndefined(isolate, 1);
if (audioBuffer->IsUndefined() || audioBuffer->IsNull()) { if (audioBuffer->IsUndefined() || audioBuffer->IsNull()) {
return ReadOnlyRoots(isolate).undefined_value(); return ReadOnlyRoots(isolate).undefined_value();
...@@ -306,6 +354,7 @@ BUILTIN(AddAudioBuffer) { ...@@ -306,6 +354,7 @@ BUILTIN(AddAudioBuffer) {
ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, array, ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, array,
JSTypedArray::Validate(isolate, args.atOrUndefined(isolate, 1), "ENCODER.encode()")); JSTypedArray::Validate(isolate, args.atOrUndefined(isolate, 1), "ENCODER.encode()"));
// Write Buffer into file system
Handle<JSArrayBuffer> arrayBuffer = array->GetBuffer(); Handle<JSArrayBuffer> arrayBuffer = array->GetBuffer();
std::shared_ptr<BackingStore> store = arrayBuffer->GetBackingStore(); std::shared_ptr<BackingStore> store = arrayBuffer->GetBackingStore();
...@@ -315,15 +364,80 @@ BUILTIN(AddAudioBuffer) { ...@@ -315,15 +364,80 @@ BUILTIN(AddAudioBuffer) {
std::string outpath = std::tmpnam(nullptr); std::string outpath = std::tmpnam(nullptr);
outpath += ".aac"; outpath += ".aac";
audioPath = strdup(outpath.c_str());
std::ofstream audioFile { outpath }; std::ofstream audioFile { outpath };
audioFile.write(reinterpret_cast<char*>(buffer), bufferLen); audioFile.write(reinterpret_cast<char*>(buffer), bufferLen);
audioFile.close(); audioFile.close();
return ReadOnlyRoots(isolate).undefined_value(); // Add audio file into AudioEffecter
Handle<Object> volume = args.atOrUndefined(isolate, 2);
Handle<Object> strong_end_time = args.atOrUndefined(isolate, 3);
Handle<Object> start_time = args.atOrUndefined(isolate, 4);
Handle<Object> end_time = args.atOrUndefined(isolate, 5);
Handle<Object> delay = args.atOrUndefined(isolate, 6);
Handle<Object> duration = args.atOrUndefined(isolate, 7);
Handle<Object> fade_out_duration = args.atOrUndefined(isolate, 8);
Handle<Object> isLoop = args.atOrUndefined(isolate, 9);
if (!volume->IsNumber() || !strong_end_time->IsNumber() ||
!start_time->IsNumber() || !end_time->IsNumber() ||
!delay->IsNumber() || !duration->IsNumber() ||
!fade_out_duration->IsNumber() || !isLoop->IsNumber()) {
THROW_NEW_ERROR_RETURN_FAILURE(
isolate, NewTypeError(MessageTemplate::kInvalidArgument));
}
double volume_, strong_end_time_, start_time_, end_time_, delay_,
duration_, fade_out_duration_, isLoop_;
volume_ = volume->Number();
strong_end_time_ = strong_end_time->Number();
start_time_ = start_time->Number();
end_time_ = end_time->Number();
delay_ = delay->Number();
duration_ = duration->Number();
fade_out_duration_ = fade_out_duration->Number();
isLoop_ = isLoop->Number();
if (audioEffecter == NULL) {
audioEffecter = getAudioEffecter();
if (audioEffecter == NULL) {
THROW_NEW_ERROR_RETURN_FAILURE(
isolate, NewEvalError(MessageTemplate::kInvalidArgument));
}
}
bool isLoop_bool = isLoop_ == 1;
int ret = addInputFileAE(audioEffecter, outpath.c_str(), volume_, strong_end_time_, start_time_,
end_time_, delay_, duration_, fade_out_duration_, isLoop_bool);
return *isolate->factory()->ToBoolean(ret == 0);
}
BUILTIN(ApplyAudioEffects) {
HandleScope scope(isolate);
Handle<Object> duration = args.atOrUndefined(isolate, 1);
if (!duration->IsNumber()) {
THROW_NEW_ERROR_RETURN_FAILURE(
isolate, NewTypeError(MessageTemplate::kInvalidArgument));
}
uint32_t duration_;
duration->ToUint32(&duration_);
std::string outpath = std::tmpnam(nullptr);
outpath += ".aac";
printf("Outputpath is %s, Duration is %d\n", outpath.c_str(), duration_);
int ret = doTranscodingAE(audioEffecter, outpath.c_str(), duration_);
if (ret == 0) {
audioPath = strdup(outpath.c_str());
}
return *isolate->factory()->ToBoolean(ret == 0);
} }
BUILTIN(MergeMP4) { BUILTIN(MergeMP4) {
......
...@@ -2805,9 +2805,11 @@ void Genesis::InitializeGlobal(Handle<JSGlobalObject> global_object, ...@@ -2805,9 +2805,11 @@ void Genesis::InitializeGlobal(Handle<JSGlobalObject> global_object,
SimpleInstallFunction(isolate_, mp4, "save", SimpleInstallFunction(isolate_, mp4, "save",
Builtin::kSave, 2, true); Builtin::kSave, 2, true);
SimpleInstallFunction(isolate_, mp4, "addAudioBuffer", SimpleInstallFunction(isolate_, mp4, "addAudioBuffer",
Builtin::kAddAudioBuffer, 1, true); Builtin::kAddAudioBuffer, 9, true);
SimpleInstallFunction(isolate_, mp4, "mergeMP4", SimpleInstallFunction(isolate_, mp4, "mergeMP4",
Builtin::kMergeMP4, 2, true); Builtin::kMergeMP4, 2, true);
SimpleInstallFunction(isolate_, mp4, "applyAudioEffects",
Builtin::kApplyAudioEffects, 1, true);
InstallToStringTag(isolate_, media_codex, "MediaCodex"); 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