CM APP Source

CM APP Source

The cmAppSource is a generic source component. It supports data input in any format that the GStreamer supports and it can be linked to any other component. Sometimes, the input data is not from the device, in this case, users can push data to cmAppSource and link to others to do the process.


API Instructions

create/destroy

gpointer cm_app_source_create(); void cm_app_source_destroy(gpointer hd);

start/stop

void cm_app_source_start(gpointer hd); void cm_app_source_stop(gpointer hd);

parameters

/* @caps_str0 the input data format, Gstreamer caps string, such as "video/x-raw,format=NV12,...*/ void cm_app_source_set_caps_str0(gpointer hd, const gchar* caps_str0); /* @return the actual format, for the link use*/ const char* cm_app_source_get_caps_str(gpointer hd);

data

/* @buffer push buffer in*/ gboolean cm_app_source_push_buffer(gpointer hd, GstBuffer *buffer); /* @cb data callback to get data */ void cm_app_source_set_data_callback(gpointer hd, cm_data_cb_ptr cb, gpointer user_data); void cm_app_source_remove_data_callback(gpointer hd, cm_data_cb_ptr cb, gpointer user_data);

Demo

APP Source to Mp4 Record

Structure

Link it to the Mp4 record and push YUV to it.

image-20240521-033103.png

Main codes

static char* filename = NULL; static const char * caps_str0 = "video/x-raw,format=UYVY,width=1280,height=720,framerate=(fraction)30/1,multiview-mode=mono,interlace-mode=progressive,colorimetry=bt601,pixel-aspect-ratio=(fraction)1/1"; gint main(gint argc, gchar* argv[]) { _exit_setup(); gst_init(&argc, &argv); if(argv[1]) filename = argv[1]; { //Create appsrc to push data src_hd = cm_app_source_create(); //Set data format caps string cm_app_source_set_caps_str0(src_hd, (const gchar*)caps_str0); cm_app_source_start(src_hd); //Create mp4 record record_hd = cm_mp4_record_create(FALSE); char sfilename[64] = {0}; if(filename == NULL){ time_t cur_time; time(&cur_time); struct tm *local = localtime(&cur_time); sprintf(sfilename, "REC_%04d%02d%02d%02d%02d%02d.mp4", local->tm_year + 1900, local->tm_mon + 1, local->tm_mday, local->tm_hour, local->tm_min, local->tm_sec); filename = sfilename; } //Set filename cm_mp4_record_set_filename(record_hd, filename); //Link to source, the app source has set capstr, so no need wait it ready before link cm_mp4_record_link_to_source(record_hd, src_hd, NULL); cm_mp4_record_start(record_hd); gst_print("Mp4 record start: %s\n", filename); } guint64 frame_idx = 0; GstClockTime start_pts = gst_util_get_timestamp(); //Feed yuv data while(!is_exit){ int yuv_size = 1280 * 720 * 2; unsigned *yuv_data = (unsigned*)malloc(yuv_size); GstBuffer *buffer= gst_buffer_new(); gst_buffer_append_memory (buffer, gst_memory_new_wrapped (GST_MEMORY_FLAG_READONLY, yuv_data, yuv_size, 0, yuv_size, NULL, NULL)); GST_BUFFER_PTS(buffer) = start_pts + 33 * 1000 * 1000 * frame_idx++; //ns GST_BUFFER_DURATION(buffer) = GST_TIME_AS_NSECONDS(33 * 1000 * 1000); //Feed yuv data cm_app_source_push_buffer(src_hd, buffer); gst_buffer_unref(buffer); free(yuv_data); guint64 duration; //Get current duration cm_mp4_record_get_control_info(record_hd, "current-duration", (void**)&duration); gst_print("Mp4 recording......%lld.%03ds\r", duration / 1000, duration % 1000); g_usleep(30*1000); } ...... }

Test result

image-20240521-033155.png

The mp4 file can be displayed on the PC tool PotPlayer. The screen will be green because the input data is fake.