Commit a3868446 authored by Eugene Zemtsov's avatar Eugene Zemtsov Committed by Commit Bot

[webcodecs] Update VideoEncoder API shape

1. remove tune() from VideoEncoder
2. Remove promises from VideoEncoder
3. Split init and config

Bug: 1094166
Change-Id: I47b347c89bb11991cdd61f52e0e4a7ce99b8b1d6
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2244660Reviewed-by: default avatarJochen Eisinger <jochen@chromium.org>
Reviewed-by: default avatarDan Sanders <sandersd@chromium.org>
Commit-Queue: Eugene Zemtsov <eugene@chromium.org>
Cr-Commit-Position: refs/heads/master@{#779871}
parent c2018705
...@@ -733,10 +733,10 @@ static_idl_files_in_modules = get_path_info( ...@@ -733,10 +733,10 @@ static_idl_files_in_modules = get_path_info(
"//third_party/blink/renderer/modules/webcodecs/video_decoder.idl", "//third_party/blink/renderer/modules/webcodecs/video_decoder.idl",
"//third_party/blink/renderer/modules/webcodecs/video_decoder_init.idl", "//third_party/blink/renderer/modules/webcodecs/video_decoder_init.idl",
"//third_party/blink/renderer/modules/webcodecs/video_encoder.idl", "//third_party/blink/renderer/modules/webcodecs/video_encoder.idl",
"//third_party/blink/renderer/modules/webcodecs/video_encoder_config.idl",
"//third_party/blink/renderer/modules/webcodecs/video_encoder_encode_options.idl", "//third_party/blink/renderer/modules/webcodecs/video_encoder_encode_options.idl",
"//third_party/blink/renderer/modules/webcodecs/video_encoder_init.idl", "//third_party/blink/renderer/modules/webcodecs/video_encoder_init.idl",
"//third_party/blink/renderer/modules/webcodecs/video_encoder_output_callback.idl", "//third_party/blink/renderer/modules/webcodecs/video_encoder_output_callback.idl",
"//third_party/blink/renderer/modules/webcodecs/video_encoder_tune_options.idl",
"//third_party/blink/renderer/modules/webcodecs/video_frame.idl", "//third_party/blink/renderer/modules/webcodecs/video_frame.idl",
"//third_party/blink/renderer/modules/webcodecs/video_frame_init.idl", "//third_party/blink/renderer/modules/webcodecs/video_frame_init.idl",
"//third_party/blink/renderer/modules/webcodecs/video_frame_output_callback.idl", "//third_party/blink/renderer/modules/webcodecs/video_frame_output_callback.idl",
......
...@@ -23,9 +23,9 @@ modules_dictionary_idl_files = [ ...@@ -23,9 +23,9 @@ modules_dictionary_idl_files = [
"image_decoder_init.idl", "image_decoder_init.idl",
"image_frame.idl", "image_frame.idl",
"video_decoder_init.idl", "video_decoder_init.idl",
"video_encoder_config.idl",
"video_encoder_init.idl", "video_encoder_init.idl",
"video_encoder_encode_options.idl", "video_encoder_encode_options.idl",
"video_encoder_tune_options.idl",
"video_frame_init.idl", "video_frame_init.idl",
"video_track_writer_parameters.idl", "video_track_writer_parameters.idl",
] ]
......
...@@ -25,8 +25,8 @@ namespace blink { ...@@ -25,8 +25,8 @@ namespace blink {
class ExceptionState; class ExceptionState;
enum class DOMExceptionCode; enum class DOMExceptionCode;
class VideoEncoderConfig;
class VideoEncoderInit; class VideoEncoderInit;
class VideoEncoderTuneOptions;
class VideoEncoderEncodeOptions; class VideoEncoderEncodeOptions;
class Visitor; class Visitor;
...@@ -34,22 +34,24 @@ class MODULES_EXPORT VideoEncoder final : public ScriptWrappable { ...@@ -34,22 +34,24 @@ class MODULES_EXPORT VideoEncoder final : public ScriptWrappable {
DEFINE_WRAPPERTYPEINFO(); DEFINE_WRAPPERTYPEINFO();
public: public:
static VideoEncoder* Create(ScriptState*, ExceptionState&); static VideoEncoder* Create(ScriptState*,
VideoEncoder(ScriptState*, ExceptionState&); const VideoEncoderInit*,
ExceptionState&);
VideoEncoder(ScriptState*, const VideoEncoderInit*, ExceptionState&);
~VideoEncoder() override; ~VideoEncoder() override;
// video_encoder.idl implementation. // video_encoder.idl implementation.
ScriptPromise encode(const VideoFrame* frame, void encode(const VideoFrame* frame,
const VideoEncoderEncodeOptions*, const VideoEncoderEncodeOptions*,
ExceptionState&); ExceptionState&);
ScriptPromise configure(const VideoEncoderInit* init, ExceptionState&); void configure(const VideoEncoderConfig*, ExceptionState&);
ScriptPromise tune(const VideoEncoderTuneOptions* params, ExceptionState&); ScriptPromise flush(ExceptionState&);
ScriptPromise close(); void reset(ExceptionState&);
ScriptPromise flush(); void close(ExceptionState&);
// GarbageCollected override. // GarbageCollected override.
void Trace(Visitor*) const override; void Trace(Visitor*) const override;
...@@ -60,27 +62,24 @@ class MODULES_EXPORT VideoEncoder final : public ScriptWrappable { ...@@ -60,27 +62,24 @@ class MODULES_EXPORT VideoEncoder final : public ScriptWrappable {
kConfigure, kConfigure,
kEncode, kEncode,
kFlush, kFlush,
kClose,
}; };
void Trace(Visitor*) const; void Trace(Visitor*) const;
DOMException* Reject(DOMExceptionCode code, const String& message);
void Resolve();
Type type; Type type;
Member<const VideoEncoderInit> config; // used by kConfigure Member<const VideoEncoderConfig> config; // used by kConfigure
Member<const VideoFrame> frame; // used by kEncode Member<const VideoFrame> frame; // used by kEncode
Member<const VideoEncoderEncodeOptions> encodeOpts; // used by kEncode Member<const VideoEncoderEncodeOptions> encodeOpts; // used by kEncode
Member<ScriptPromiseResolver> resolver; Member<ScriptPromiseResolver> resolver; // used by kFlush
}; };
void CallOutputCallback(EncodedVideoChunk* chunk); void CallOutputCallback(EncodedVideoChunk* chunk);
void CallErrorCallback(DOMException* ex); void CallErrorCallback(DOMException* ex);
ScriptPromise EnqueueRequest(Request* request); void CallErrorCallback(DOMExceptionCode code, const String& message);
void EnqueueRequest(Request* request);
void ProcessRequests(); void ProcessRequests();
void ProcessEncode(Request* request); void ProcessEncode(Request* request);
void ProcessConfigure(Request* request); void ProcessConfigure(Request* request);
void ProcessClose(Request* request);
void ProcessFlush(Request* request); void ProcessFlush(Request* request);
void MediaEncoderOutputCallback(media::VideoEncoderOutput output); void MediaEncoderOutputCallback(media::VideoEncoderOutput output);
......
...@@ -9,15 +9,14 @@ ...@@ -9,15 +9,14 @@
RuntimeEnabled=WebCodecs RuntimeEnabled=WebCodecs
] interface VideoEncoder { ] interface VideoEncoder {
[CallWith=ScriptState, RaisesException] [CallWith=ScriptState, RaisesException]
constructor(); constructor(VideoEncoderInit init);
// Performs original configuration of the encoder. // Performs original configuration of the encoder.
// Resolved after configuration is done. It should be called only // Resolved after configuration is done. It should be called only
// once per encoder instance, before calling any other methods. // once per encoder instance, before calling any other methods.
[RaisesException] [RaisesException]
Promise<void> configure(VideoEncoderInit init); void configure(VideoEncoderConfig config);
[RaisesException]
// Enqueues a request to encode a frame. // Enqueues a request to encode a frame.
// Results of the encoding (EncodedVideoChunk) are returned via // Results of the encoding (EncodedVideoChunk) are returned via
// the output callback provided in configure(). // the output callback provided in configure().
...@@ -25,23 +24,27 @@ ...@@ -25,23 +24,27 @@
// The output callback can be called before or after the result is resolved. // The output callback can be called before or after the result is resolved.
// Several encoded requests can be resolved before even a single output // Several encoded requests can be resolved before even a single output
// is produced. // is produced.
Promise<void> encode(VideoFrame frame, [RaisesException]
void encode(VideoFrame frame,
optional VideoEncoderEncodeOptions options = {}); optional VideoEncoderEncodeOptions options = {});
[RaisesException]
// Enqueues a request to change certain parameters of the encoder.
// Resolved after the parameters are adjusted.
// All following encode requests will be executed according to
// the new parameters.
// Should be preceded by flush().
Promise<void> tune(VideoEncoderTuneOptions options);
// Enqueues a request to produce outputs for all already encoded frames. // Enqueues a request to produce outputs for all already encoded frames.
// Resolved after emitting outputs for all previously encoded frames. // Resolved after emitting outputs for all previously encoded frames.
[RaisesException]
Promise<void> flush(); Promise<void> flush();
// Discard all pending work and current encoder configuration.
//
// Output for earlier encoding requests will not be emitted.
// The next encoded frame will be a keyframe.
// Required a configure() to be call to set configuration once again.
[RaisesException]
void reset();
// Enqueues a request to shut down the encoder and free its resources. // Enqueues a request to shut down the encoder and free its resources.
// Resolved after all resources are released and all following requests // Resolved after all resources are released and all following requests
// rejected. // rejected.
Promise<void> close(); [RaisesException]
void close();
}; };
...@@ -4,7 +4,10 @@ ...@@ -4,7 +4,10 @@
// https://github.com/WICG/web-codecs // https://github.com/WICG/web-codecs
dictionary VideoEncoderTuneOptions { dictionary VideoEncoderConfig {
required DOMString codec;
DOMString profile;
unsigned long long bitrate; unsigned long long bitrate;
double framerate; double framerate;
......
...@@ -5,11 +5,6 @@ ...@@ -5,11 +5,6 @@
// https://github.com/WICG/web-codecs // https://github.com/WICG/web-codecs
dictionary VideoEncoderInit { dictionary VideoEncoderInit {
required DOMString codec;
DOMString profile;
required VideoEncoderTuneOptions tuneOptions;
// Called whenever there is a new encoded video chunk available. // Called whenever there is a new encoded video chunk available.
required VideoEncoderOutputCallback output; required VideoEncoderOutputCallback output;
......
...@@ -28,24 +28,26 @@ async function createFrame(width, height, ts) { ...@@ -28,24 +28,26 @@ async function createFrame(width, height, ts) {
} }
promise_test(async function() { promise_test(async function() {
const tune = { const init = {
output : () => { return true; }
};
const params = {
codec : "vp8",
width: 123, width: 123,
height: 321, height: 321,
bitrate: 1e6, bitrate: 1e6,
framerate: 24 framerate: 24,
}
const init_params = {
codec : "vp8",
tuneOptions: tune,
output : () => { return true; }
}; };
let encoder = new VideoEncoder(); let encoder = new VideoEncoder(init);
await encoder.configure(init_params); encoder.configure(params);
encoder = new VideoEncoder(init_params); encoder.close();
tune.width = null; encoder = new VideoEncoder(init);
encoder.configure(init_params).catch(e => { params.width = null;
try {
encoder.configure(params);
} catch (e) {
assert_equals(e.name, "InvalidStateError"); assert_equals(e.name, "InvalidStateError");
}); };
}, "Initialization"); }, "Initialization");
promise_test(async function() { promise_test(async function() {
...@@ -64,28 +66,27 @@ promise_test(async function() { ...@@ -64,28 +66,27 @@ promise_test(async function() {
frames_processed++; frames_processed++;
}; };
const init_params = { const init = {
codec : "vp9",
profile : "vp09.00.10.08",
tuneOptions : {
width : w,
height : h,
bitrate: 10e6,
framerate: 30
},
output : process_video_chunk, output : process_video_chunk,
error: (e) => { console.log(e.message); } error: (e) => { console.log(e.message); }
}; };
let encoder = new VideoEncoder(); const params = {
await encoder.configure(init_params); codec : "vp9",
profile : "vp09.00.10.08",
width : w,
height : h,
bitrate: 10e6,
framerate: 30,
};
let encoder = new VideoEncoder(init);
encoder.configure(params);
for (let i = 0; i < frames_to_encode; i++) { for (let i = 0; i < frames_to_encode; i++) {
var frame = await createFrame(w, h, i); var frame = await createFrame(w, h, i);
let keyframe = (i % 5 == 0); let keyframe = (i % 5 == 0);
await encoder.encode(frame, { keyFrame : keyframe}); encoder.encode(frame, { keyFrame : keyframe});
} }
await encoder.flush(); await encoder.flush();
await encoder.close(); encoder.close();
assert_equals(frames_processed, frames_to_encode); assert_equals(frames_processed, frames_to_encode);
}, "EncodeFrame"); }, "EncodeFrame");
......
...@@ -8733,7 +8733,7 @@ interface VideoEncoder ...@@ -8733,7 +8733,7 @@ interface VideoEncoder
method constructor method constructor
method encode method encode
method flush method flush
method tune method reset
interface VideoFrame interface VideoFrame
attribute @@toStringTag attribute @@toStringTag
getter codedHeight getter codedHeight
......
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