public class Codec extends Object
In normal mode, applications must manually transfer source data to the codec continuously.
In pipeline mode, applications only need to set the data source using the Source class. The codec
automatically reads source data from the data source and parses the data.
Applications are notified of the encoded or decoded data through the registered listener.
The following describes how to use the decoder.
To use the decoder in normal mode, perform the following steps:
createDecoder() to create a decoder instance.
setCodecFormat(ohos.media.common.Format) to set the data source format.
registerCodecListener(ohos.media.codec.Codec.ICodecListener) to register a listener.
start() to start decoding.
getAvailableBuffer(long) and writeBuffer(java.nio.ByteBuffer, ohos.media.common.BufferInfo) to transfer source data to the decoder instance.
stop() to stop decoding.
To use the decoder in pipeline mode, perform the following steps:
createDecoder() to create a decoder instance.
setSource(ohos.media.common.Source, ohos.media.codec.TrackInfo) to set the data source.
registerCodecListener(ohos.media.codec.Codec.ICodecListener) to register a listener.
start() to start decoding.
The following describes how to use the encoder.
To use the encoder in normal mode, perform the following steps:
createEncoder() to create an encoder instance.
setCodecFormat(ohos.media.common.Format) to set the output data format.
registerCodecListener(ohos.media.codec.Codec.ICodecListener) to register a listener.
start() to start encoding.
getAvailableBuffer(long) and writeBuffer(java.nio.ByteBuffer, ohos.media.common.BufferInfo) to transfer source data to the encoder instance.
stop() to stop encoding.
The encoder in pipeline mode applies only to videos. To use the encoder in pipeline mode, perform the following steps:
createEncoder() to create an encoder instance.
setSource(ohos.media.common.Source, ohos.media.codec.TrackInfo) to set the data source.
setSourceFormat(ohos.media.common.Format) to set the data source format and encoding parameters.
registerCodecListener(ohos.media.codec.Codec.ICodecListener) to register a listener.
start() to start encoding.
SystemCapability.Multimedia#MEDIA| 限定符和类型 | 类 | 说明 |
|---|---|---|
static interface |
Codec.ICodecListener |
Receives notifications during encoding or decoding, such as encoded or decoded data and error messages.
|
| 限定符和类型 | 字段 | 说明 |
|---|---|---|
static int |
VIDEO_SCALE_TYPE_CROP |
Indicates that the aspect ratio of the video remains unchanged.
|
static int |
VIDEO_SCALE_TYPE_FIT |
Indicates that the video is stretched in the rendering surface.
|
| 限定符和类型 | 方法 | 说明 |
|---|---|---|
static Codec |
createDecoder() |
Creates a decoder instance.
|
static Codec |
createEncoder() |
Creates an encoder instance.
|
Surface |
createLastingVideoSurface() |
Creates a lasting
Surface object for this encoder. |
protected void |
finalize() |
Called by the garbage collector on an object when garbage collection
determines that there are no more references to the object.
|
boolean |
flush() |
Flush all buffers.
|
ByteBuffer |
getAvailableBuffer(long timeout) |
Obtains a
ByteBuffer in normal mode. |
Format |
getBufferFormat(ByteBuffer buffer) |
Obtains the output data format.
|
int |
getInputBufferIndex(long timeout) |
Obtains a dequeued input buffer index.
|
CodecDescription |
obtainCodecDescription() |
Obtains the
CodecDescription object. |
Map<Integer,String> |
obtainCodecName() |
Obtains the codec name.
|
PixelMap |
obtainInputPixelMap(int index) |
Obtains a writable pixel map for a dequeued input buffer.
|
Surface |
obtainInputSurface() |
Obtains a
Surface object created for this encoder. |
PixelMap |
obtainOutputPixelMap(int index) |
Obtains a read-only pixel map for a dequeued output buffer.
|
boolean |
registerCodecListener(Codec.ICodecListener listener) |
Registers a listener to asynchronously receive encoded or decoded data.
|
boolean |
release() |
Releases all resources, including the memory applied by the native layer.
|
boolean |
setCodecFormat(Format format) |
Sets encoding or decoding parameters in normal mode.
|
boolean |
setLastingVideoSurface(Surface lastingSurface) |
Sets a lasting
Surface object for this encoder. |
boolean |
setProperty(Format format) |
Sets encoding or decoding properties.
|
boolean |
setSource(Source source,
TrackInfo trackInfo) |
Sets the data source.
|
boolean |
setSourceFormat(Format format) |
Sets the data source format and encoding parameters.
|
boolean |
setVideoScaleType(int type) |
Sets the video scaling type.
|
boolean |
setVideoSurface(Surface surface) |
Sets the surface for the decoder to display decoded video data.
|
boolean |
start() |
Starts encoding or decoding after the preparation is complete.
|
boolean |
stop() |
Stops encoding or decoding.
|
boolean |
stopVideoSurface() |
Informs the encoder of the end of input and stops encoding.
|
boolean |
writeBuffer(ByteBuffer buffer,
BufferInfo info) |
Pushes source data to the encoder or decoder.
|
public static final int VIDEO_SCALE_TYPE_CROP
If the aspect ratio of the video differs from that of the surface, the video will be cropped to fit the surface.
public static final int VIDEO_SCALE_TYPE_FIT
If the aspect ratio of the video differs from that of the surface, the original aspect ratio will not be retained during video rendering.
public static Codec createDecoder()
public static Codec createEncoder()
public boolean registerCodecListener(Codec.ICodecListener listener)
To use the Codec class for encoding or decoding, a listener must be registered. If
registerCodecListener is called multiple times, the codec records only the last registered listener.
If null is transferred, the codec clears the previous listener.
The current method must be called prior to start().
listener - Indicates a codec listener instance. For details, see ICodecListener.true if the listener is registered; returns false otherwise.public boolean setSource(Source source, TrackInfo trackInfo)
If you set the data source by calling this method, the pipeline mode is used. The codec automatically parses source data in the background and notifies your application of the parsed data through the listener.
For an encoder, you must also call setSourceFormat(ohos.media.common.Format) to set the data source format and
encoding parameters.
The current method must be called prior to start().
source - Indicates the data source. For details, see Source.trackInfo - Indicates the source track information for a decoder. This parameter is invalid for an encoder.
The value null indicates that no track information is available.true if the setting is successful; returns false otherwise.IllegalArgumentException - Throws this exception if a parameter is invalid.public boolean setSourceFormat(Format format)
After the data source is set for an encoder, you must call this method to set the data source format and encoding parameters.
format - Indicates the data source format and encoding parameters.true if the setting is successful; returns false otherwise.public boolean setCodecFormat(Format format)
If the normal mode is used, you must call this method to set the source data format and encoding or decoding parameters.
For an encoder, this method is used to set the source data format and encoding parameters.
For a decoder, this method is used to set the source data format.
The current method must be called prior to start().
If setSource(ohos.media.common.Source, ohos.media.codec.TrackInfo) is called for the decoder, the decoder automatically analyzes the source data format.
You do not need to call the current method.
format - Indicates the data format.true if the setting is successful; returns false otherwise.public boolean setProperty(Format format)
In normal mode, this method must be called after setCodecFormat(ohos.media.common.Format).
In pipeline mode, this method can only used for encoding and must be called after setSource(ohos.media.common.Source, ohos.media.codec.TrackInfo).
format - Indicates the data format.true if the setting is successful; returns false otherwise.public Surface obtainInputSurface()
Surface object created for this encoder.
The Surface object is used for the input of the encoder.
This method must be called after #SetCodecFormat(ohos.media.common.Format) or
setSourceFormat(ohos.media.common.Format) and before start().
Surface object.public boolean setVideoSurface(Surface surface)
surface - Indicates a surface instance.true if the setting is successful; returns false otherwise.public boolean setVideoScaleType(int type)
type - Indicates the way the video will be scaled. The value must be one of the following:
true if the operation is successful; returns false otherwise.public ByteBuffer getAvailableBuffer(long timeout)
ByteBuffer in normal mode.
This method applies only to the normal mode and is invalid in the pipeline mode. In normal mode, you must
manually transfer source data to the codec. Specifically, call getAvailableBuffer to obtain a
ByteBuffer, then fill the source data in the ByteBuffer, and finally call writeBuffer(java.nio.ByteBuffer, ohos.media.common.BufferInfo)
to pass the source data from the ByteBuffer to the codec.
The current method must be called after start().
timeout - Indicates the duration that the codec can wait for a ByteBuffer, in milliseconds. If
there is no available ByteBuffer after the specified duration is reached, the value null is
returned. If this parameter is set to -1, the method is blocked until it obtains a byte buffer or an
error occurs.ByteBuffer; returns null if a timeout or error occurs.public boolean writeBuffer(ByteBuffer buffer, BufferInfo info)
This method applies only to the normal mode and is invalid in the pipeline mode. The ByteBuffer
parameter must be set to that obtained by calling getAvailableBuffer(long).
The current method must be called after start().
buffer - Indicates the ByteBuffer returned by getAvailableBuffer(long).info - Indicates the BufferInfo type. For details, see BufferInfo.true if the source data is pushed to the encoder or decoder; returns false
otherwise.public Format getBufferFormat(ByteBuffer buffer)
This function is used to obtain the format of output data after encoding or decoding. The output data is
carried in the ByteBuffer parameter in ICodecListener#onReadBuffer.
buffer - Indicates the ByteBuffer returned by ICodecListener#onReadBuffer.null otherwise.public boolean start()
true if the task is started; returns false otherwise.public boolean stop()
true if the task is stopped; returns false otherwise.public boolean release()
Codec instance will not have the encoding or decoding capability.true if the resources are released; returns false otherwise.public boolean flush()
true if flush succeed; returns false otherwise.public boolean stopVideoSurface()
true if the encoder input ends normally; returns false otherwise.public Map<Integer,String> obtainCodecName()
public CodecDescription obtainCodecDescription()
CodecDescription object.CodecDescription object.public PixelMap obtainOutputPixelMap(int index)
index - Indicates the index of a dequeued output buffer returned by ICodecListener#onReadBuffer.null otherwise.public PixelMap obtainInputPixelMap(int index)
index - Indicates the index of a dequeued input buffer returned by getInputBufferIndex(long).null otherwise.public Surface createLastingVideoSurface()
Surface object for this encoder.
The lasting Surface object is used for the input of the encoder.
This method must be called after #SetCodecFormat(ohos.media.common.Format) or
setSourceFormat(ohos.media.common.Format) and before start().
Surface object.public boolean setLastingVideoSurface(Surface lastingSurface)
Surface object for this encoder.
The lasting Surface object is used for the input of the encoder.
This method must be called after #SetCodecFormat(ohos.media.common.Format) or
setSourceFormat(ohos.media.common.Format) and before start().
lastingSurface - Indicates the lasting surface used for input.true if the setting is successful; returns false otherwise.public int getInputBufferIndex(long timeout)
timeout - Indicates the duration that the codec can wait for a dequeued input buffer index, in milliseconds.-1 otherwise.protected void finalize()
throws Throwable
Objectfinalize method to dispose of
system resources or to perform other cleanup.
The general contract of finalize is that it is invoked
if and when the Java™ virtual
machine has determined that there is no longer any
means by which this object can be accessed by any thread that has
not yet died, except as a result of an action taken by the
finalization of some other object or class which is ready to be
finalized. The finalize method may take any action, including
making this object available again to other threads; the usual purpose
of finalize, however, is to perform cleanup actions before
the object is irrevocably discarded. For example, the finalize method
for an object that represents an input/output connection might perform
explicit I/O transactions to break the connection before the object is
permanently discarded.
The finalize method of class Object performs no
special action; it simply returns normally. Subclasses of
Object may override this definition.
The Java programming language does not guarantee which thread will
invoke the finalize method for any given object. It is
guaranteed, however, that the thread that invokes finalize will not
be holding any user-visible synchronization locks when finalize is
invoked. If an uncaught exception is thrown by the finalize method,
the exception is ignored and finalization of that object terminates.
After the finalize method has been invoked for an object, no
further action is taken until the Java virtual machine has again
determined that there is no longer any means by which this object can
be accessed by any thread that has not yet died, including possible
actions by other objects or classes which are ready to be finalized,
at which point the object may be discarded.
The finalize method is never invoked more than once by a Java
virtual machine for any given object.
Any exception thrown by the finalize method causes
the finalization of this object to be halted, but is otherwise
ignored.
finalize 在类中 ObjectThrowable - the Exception raised by this methodWeakReference,
PhantomReference