Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in / Register
Toggle navigation
V
V8
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Linshizhi
V8
Commits
0fd03d10
Commit
0fd03d10
authored
Oct 24, 2022
by
Linshizhi
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add getVideo to MediaCodex.MP4.
parent
095f896e
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
116 additions
and
78 deletions
+116
-78
builtins-definitions.h
src/builtins/builtins-definitions.h
+1
-0
builtins-encoder.cc
src/builtins/builtins-encoder.cc
+42
-13
builtins-encoder.h
src/builtins/builtins-encoder.h
+70
-64
bootstrapper.cc
src/init/bootstrapper.cc
+3
-1
No files found.
src/builtins/builtins-definitions.h
View file @
0fd03d10
...
...
@@ -624,6 +624,7 @@ namespace internal {
CPP(InitEncodeContext) \
CPP(Encode) \
CPP(Close) \
CPP(GetVideo) \
\
/* Web snapshots */
\
CPP(WebSnapshotSerialize) \
...
...
src/builtins/builtins-encoder.cc
View file @
0fd03d10
#include <stdio.h>
#include <iostream>
#include <cstdio>
#include <fstream>
#include "builtins-encoder.h"
#include "src/execution/isolate.h"
#include "extensions/encoder/src/encoder.h"
#include "src/builtins/builtins.h"
...
...
@@ -17,24 +20,22 @@ LAIPIC_ENCODER::Encoder *enc;
BUILTIN
(
InitEncodeContext
)
{
HandleScope
scope
(
isolate
);
// Output path
Handle
<
Object
>
path
=
args
.
atOrUndefined
(
isolate
,
1
);
// Width
Handle
<
Object
>
width
=
args
.
atOrUndefined
(
isolate
,
2
);
Handle
<
Object
>
width
=
args
.
atOrUndefined
(
isolate
,
1
);
// Height
Handle
<
Object
>
height
=
args
.
atOrUndefined
(
isolate
,
3
);
Handle
<
Object
>
height
=
args
.
atOrUndefined
(
isolate
,
2
);
// Bitrate
Handle
<
Object
>
bitrate
=
args
.
atOrUndefined
(
isolate
,
4
);
Handle
<
Object
>
bitrate
=
args
.
atOrUndefined
(
isolate
,
3
);
// Framerate
Handle
<
Object
>
framerate
=
args
.
atOrUndefined
(
isolate
,
5
);
Handle
<
Object
>
framerate
=
args
.
atOrUndefined
(
isolate
,
4
);
// Libav Path, Optional
Handle
<
Object
>
libpath
=
args
.
atOrUndefined
(
isolate
,
6
);
Handle
<
Object
>
libpath
=
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
()
||
if
(
!
width
->
IsNumber
()
||
!
height
->
IsNumber
()
||
!
bitrate
->
IsNumber
()
||
!
framerate
->
IsNumber
())
{
THROW_NEW_ERROR_RETURN_FAILURE
(
...
...
@@ -52,12 +53,11 @@ BUILTIN(InitEncodeContext) {
libpath_cppstr
=
libpath_str
->
ToCString
().
get
();
}
// Convert to C/C++ type
Handle
<
String
>
path_str
;
ASSIGN_RETURN_FAILURE_ON_EXCEPTION
(
isolate
,
path_str
,
Object
::
ToString
(
isolate
,
path
));
std
::
string
outpath
=
{
path_str
->
ToCString
().
get
()
};
// Output mp4 file path
std
::
string
outpath
=
std
::
tmpnam
(
nullptr
);
outpath
+=
".mp4"
;
// Convert to C/C++ type
uint32_t
width_int
,
height_int
,
bitrate_int
,
framerate_int
;
width
->
ToUint32
(
&
width_int
);
...
...
@@ -133,5 +133,34 @@ BUILTIN(Close) {
return
*
isolate
->
factory
()
->
ToBoolean
(
true
);
}
// Return Values;
// Success: An Uint8Array contains video info.
// Failed: Undefined.
BUILTIN
(
GetVideo
)
{
HandleScope
scope
(
isolate
);
if
(
!
inited
)
{
return
ReadOnlyRoots
(
isolate
).
undefined_value
();
}
// Open video file so able to determine the size of file
std
::
ifstream
videoFile
{
enc
->
getOutputPath
(),
std
::
ifstream
::
binary
|
std
::
ifstream
::
ate
};
size_t
fileLength
=
videoFile
.
tellg
();
Handle
<
JSTypedArray
>
videoContent
=
EncodeNewJSTypedArray
(
isolate
,
fileLength
);
videoFile
.
seekg
(
0
);
// Get internal buffer of JSTypedArray
std
::
shared_ptr
<
BackingStore
>
store
=
videoContent
->
GetBuffer
()
->
GetBackingStore
();
char
*
buffer
=
reinterpret_cast
<
char
*>
(
store
->
buffer_start
());
// Fill file content into TypedArray
videoFile
.
read
(
buffer
,
fileLength
);
return
*
videoContent
;
}
}
// internal
}
// v8
src/builtins/builtins-encoder.h
View file @
0fd03d10
...
...
@@ -5,10 +5,10 @@
#include "src/builtins/builtins-utils-inl.h"
namespace
v8
{
namespace
internal
{
namespace
internal
{
//提取字段
inline
Handle
<
Object
>
EncodeGetProperty
(
v8
::
internal
::
Isolate
*
isolate
,
v8
::
internal
::
Handle
<
v8
::
internal
::
Object
>
object
,
const
char
*
name
)
{
//提取字段
inline
Handle
<
Object
>
EncodeGetProperty
(
v8
::
internal
::
Isolate
*
isolate
,
v8
::
internal
::
Handle
<
v8
::
internal
::
Object
>
object
,
const
char
*
name
)
{
Handle
<
String
>
h_name
=
isolate
->
factory
()
->
InternalizeUtf8String
(
name
);
Handle
<
Object
>
result
;
bool
succ
=
Object
::
GetProperty
(
isolate
,
object
,
h_name
).
ToHandle
(
&
result
);
...
...
@@ -16,17 +16,17 @@ namespace v8 {
return
isolate
->
factory
()
->
undefined_value
();
}
return
result
;
}
}
//对象转cstring
inline
std
::
unique_ptr
<
char
[]
>
EncodeToCString
(
v8
::
internal
::
Isolate
*
isolate
,
v8
::
internal
::
Handle
<
v8
::
internal
::
Object
>
object
)
{
//对象转cstring
inline
std
::
unique_ptr
<
char
[]
>
EncodeToCString
(
v8
::
internal
::
Isolate
*
isolate
,
v8
::
internal
::
Handle
<
v8
::
internal
::
Object
>
object
)
{
if
(
!
object
->
IsString
())
return
std
::
unique_ptr
<
char
[]
>
(
nullptr
);
Handle
<
String
>
string
=
Handle
<
String
>::
cast
(
object
);
return
string
->
ToCString
();
}
}
//TypedArray 或 ArrayBuffer 对象转指针
inline
std
::
tuple
<
uint8_t
*
,
size_t
>
EncodeToBuffer
(
v8
::
internal
::
Isolate
*
isolate
,
v8
::
internal
::
Handle
<
v8
::
internal
::
Object
>
object
)
{
//TypedArray 或 ArrayBuffer 对象转指针
inline
std
::
tuple
<
uint8_t
*
,
size_t
>
EncodeToBuffer
(
v8
::
internal
::
Isolate
*
isolate
,
v8
::
internal
::
Handle
<
v8
::
internal
::
Object
>
object
)
{
Handle
<
JSArrayBuffer
>
jsab
;
//size_t size;
if
(
object
->
IsJSTypedArray
())
{
...
...
@@ -44,28 +44,28 @@ namespace v8 {
if
(
jsab
->
IsNullOrUndefined
())
return
std
::
tuple
<
uint8_t
*
,
size_t
>
(
nullptr
,
0
);
std
::
shared_ptr
<
v8
::
internal
::
BackingStore
>
store
=
jsab
->
GetBackingStore
();
return
std
::
tuple
<
uint8_t
*
,
size_t
>
((
uint8_t
*
)
store
->
buffer_start
(),
store
->
byte_length
());
}
}
inline
Handle
<
JSObject
>
EncodeNewJSObject
(
v8
::
internal
::
Isolate
*
isolate
)
{
inline
Handle
<
JSObject
>
EncodeNewJSObject
(
v8
::
internal
::
Isolate
*
isolate
)
{
return
isolate
->
factory
()
->
NewJSObject
(
isolate
->
object_function
(),
AllocationType
::
kOld
);
}
}
inline
void
EncodeAddPropertyObject
(
Isolate
*
isolate
,
Handle
<
JSObject
>
object
,
const
char
*
key
,
Handle
<
Object
>
value
)
{
inline
void
EncodeAddPropertyObject
(
Isolate
*
isolate
,
Handle
<
JSObject
>
object
,
const
char
*
key
,
Handle
<
Object
>
value
)
{
JSObject
::
AddProperty
(
isolate
,
object
,
key
,
value
,
PropertyAttributes
::
NONE
);
}
}
inline
void
EncodeAddPropertyNumber
(
Isolate
*
isolate
,
Handle
<
JSObject
>
object
,
const
char
*
key
,
double
value
)
{
inline
void
EncodeAddPropertyNumber
(
Isolate
*
isolate
,
Handle
<
JSObject
>
object
,
const
char
*
key
,
double
value
)
{
Handle
<
Object
>
valueObj
=
isolate
->
factory
()
->
NewNumber
(
value
);
JSObject
::
AddProperty
(
isolate
,
object
,
key
,
valueObj
,
PropertyAttributes
::
NONE
);
}
}
inline
void
EncodeAddPropertyString
(
Isolate
*
isolate
,
Handle
<
JSObject
>
object
,
const
char
*
key
,
const
char
*
value
)
{
inline
void
EncodeAddPropertyString
(
Isolate
*
isolate
,
Handle
<
JSObject
>
object
,
const
char
*
key
,
const
char
*
value
)
{
Handle
<
String
>
strObj
=
isolate
->
factory
()
->
InternalizeUtf8String
(
value
);
//字段设置为 PropertyAttributes::DontEnum 时,无法通过键值对遍历 Object.keys(retObj)
JSObject
::
AddProperty
(
isolate
,
object
,
key
,
strObj
,
PropertyAttributes
::
NONE
);
}
}
inline
Handle
<
JSArrayBuffer
>
EncodeNewJSArrayBuffer
(
v8
::
internal
::
Isolate
*
isolate
,
size_t
size
)
{
inline
Handle
<
JSArrayBuffer
>
EncodeNewJSArrayBuffer
(
v8
::
internal
::
Isolate
*
isolate
,
size_t
size
)
{
MaybeHandle
<
JSArrayBuffer
>
arrbuffOjb
=
isolate
->
factory
()
->
NewJSArrayBufferAndBackingStore
(
size
,
InitializedFlag
::
kZeroInitialized
);
...
...
@@ -76,9 +76,15 @@ namespace v8 {
return
result
;
}
return
result
;
}
}
inline
Handle
<
JSTypedArray
>
EncodeNewJSTypedArray
(
v8
::
internal
::
Isolate
*
isolate
,
size_t
size
)
{
Handle
<
JSArrayBuffer
>
buffer
=
EncodeNewJSArrayBuffer
(
isolate
,
size
);
return
isolate
->
factory
()
->
NewJSTypedArray
(
kExternalInt8Array
,
buffer
,
0
,
size
);
}
}
// namespace internal
}
// namespace internal
}
// namespace v8
#endif // V8_BUILTINS_BUILTINS_ENCODER_H_
src/init/bootstrapper.cc
View file @
0fd03d10
...
...
@@ -2795,11 +2795,13 @@ void Genesis::InitializeGlobal(Handle<JSGlobalObject> global_object,
factory
->
NewJSObject
(
isolate_
->
object_function
(),
AllocationType
::
kOld
);
JSObject
::
AddProperty
(
isolate_
,
media_codex
,
"Mp4"
,
mp4
,
DONT_ENUM
);
SimpleInstallFunction
(
isolate_
,
mp4
,
"init"
,
Builtin
::
kInitEncodeContext
,
6
,
true
);
Builtin
::
kInitEncodeContext
,
5
,
true
);
SimpleInstallFunction
(
isolate_
,
mp4
,
"encode"
,
Builtin
::
kEncode
,
1
,
true
);
SimpleInstallFunction
(
isolate_
,
mp4
,
"close"
,
Builtin
::
kClose
,
0
,
true
);
SimpleInstallFunction
(
isolate_
,
mp4
,
"getVideo"
,
Builtin
::
kGetVideo
,
0
,
true
);
InstallToStringTag
(
isolate_
,
media_codex
,
"MediaCodex"
);
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment