Media Tool: Managing Media Handles
This tutorial demonstrates how you can create, set, and get media handles.
Warm-up
Become familiar with the Media Tool API basics by learning about:
-
Managing the Media Format Handle
Create, set, and get the <media_format_h> handle.
-
Managing the Media Packet Handle
Create, set, and get the <media_packet_h> handle.
Managing the Media Format Handle
To manage the media format handle:
-
To use the media format handle of the Media Tool API (in mobile and wearable applications), include the <media_format.h> header file in your application:
#include <media_format.h>
- Define a handle for media_format_h and pass it to the media_format_create() function, which returns the handle. Set the video (or audio) information with the media_format_set_video_info() function.
media_format_h format; if (media_format_create(&format) == MEDIA_FORMAT_ERROR_NONE) { if (media_format_set_video_info(format, MEDIA_FORMAT_H264_HP, 480, 640, 10000000, 15000000) != MEDIA_FORMAT_ERROR_NONE) { printf("media_format_set_video_info failed!"); } } else { printf("media_format_create() failed!"); }
- To get video format information, use the media_format_get_video_info() function:
media_format_h fmt; media_format_mimetype_e mime; int w; int h; int avg_bps; int max_bps; if (media_format_get_video_info(fmt, &mimetype, &w, &h, &avg_bps, &max_bps) == MEDIA_PACKET_ERROR_NONE) { printf("media_format_get_video_info success! width = %d, height = %d", w, h); } else { print("media_format_get_video is failed..."); }
Managing the Media Packet Handle
To manage the media packet handle:
-
To use the media packet handle of the Media Tool API (in mobile and wearable applications), include the <media_packet.h> header file in your application:
#include <media_packet.h>
- Define a handle for media_packet_h and pass it to a creation function with an existing media_format_h handle.
To reuse the packet handle, set the return value as MEDIA_PACKET_REUSE of int (*media_packet_finalize_cb) and use media_format_unref(media_format_h fmt) after creating the handle.
The following creation functions are available:
- Create a handle and allocate a buffer into the heap or TBM surface with the media_packet_create_alloc() function:
{ media_format_h fmt; media_packet_h packet; media_format_create(&fmt); media_format_set_video_info(fmt, MEDIA_FORMAT_H264_HP, 480, 640, 10000000, 15000000); media_packet_create_alloc (fmt, _finalize_callback, fcb_data, &packet); media_format_unref(fmt); } int _finalize_callback(media_packet_h packet, int err, void* userdata) { return MEDIA_PACKET_REUSE; }
- Create only a handle with the media_packet_create() function:
{ media_format_h fmt; media_packet_h packet; media_format_create(&fmt); media_format_set_video_info(fmt, MEDIA_FORMAT_H264_HP, 480, 640, 10000000, 15000000); media_packet_create (fmt, _finalize_callback, fcb_data, &packet); media_format_unref(fmt); } int _finalize_callback(media_packet_h packet, int err, void* userdata) { return MEDIA_PACKET_FINALIZE; }
- Create a handle and store the TBM surface data with the media_packet_create_from_tbm_surface() function:
{ media_format_h fmt; media_packet_h packet; media_format_create(&fmt); media_format_set_video_info(fmt, MEDIA_FORMAT_RGBA, 128, 128, 2000000, 15000000); media_packet_create_from_tbm_surface (fmt, surface, _finalize_callback, fcb_data, &packet); media_format_unref(fmt); } int _finalize_callback(media_packet_h packet, int err, void* userdata) { return MEDIA_PACKET_FINALIZE; }
- Create a handle and allocate a buffer into the heap or TBM surface with the media_packet_create_alloc() function:
- Set and get the metadata with the media packet handle:
int ret = MEDIA_PACKET_ERROR_NONE; // format1 already exists media_packet_create_alloc(format1, NULL, NULL, &packet); ret = media_packet_set_duration(packet, duration); // After media_packet_get_format(), use media_format_unref() media_format_h tmp; media_packet_get_format(packet, &tmp); media_format_unref(tmp); // Set previously created format2 to packet // After media_packet_set_format(), use media_format_unref() media_packet_set_format(packet, format2); // Packet format is format2. If format2 ref_count is 1, format2 is free // If format2 ref_count is bigger than 1, it is not free media_packet_destroy(packet);