Guest User

new_sample callback 2

a guest
Nov 8th, 2015
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.95 KB | None | 0 0
  1. /* Called when appsink receives a new sample */
  2. static GstFlowReturn new_sample_cb (GstElement *element, CustomData *data) {
  3.   GstSample *sample;
  4.   GstBuffer *buffer;
  5.   GstVideoInfo info;
  6.   GstCaps *caps;
  7.   jint textureId;
  8.   JNIEnv *env = get_jni_env ();
  9.   GstVideoFrame *pframe;
  10.  
  11.   //GST_DEBUG("Got callback");
  12.   pframe = &data->frame;
  13.  
  14.   // FIXME Assume that we already drew this one...
  15.   if (data->frame_is_valid) {
  16.       gst_video_frame_unmap(&data->frame);
  17.       data->frame_is_valid = FALSE;
  18.   }
  19.  
  20.   /* Get texture ID from buffer */
  21.   sample = gst_app_sink_pull_sample( GST_APP_SINK( data->video_sink ) );
  22.   //GST_DEBUG("Got sample");
  23.   if (!sample) {
  24.       GST_ERROR("Could not get sample");
  25.       goto exit_none;
  26.   }
  27.  
  28.   /* TODO can we cache this and the info? */
  29.   caps = gst_sample_get_caps (sample);
  30.   if (!caps) {
  31.     GST_ERROR("Could not get caps");
  32.     goto exit_sample;
  33.   }
  34.  
  35.   if (!gst_video_info_from_caps( &info, caps)) {
  36.     GST_ERROR("Could not get video info from caps");
  37.     goto exit_caps;
  38.   }
  39.  
  40.   buffer = gst_sample_get_buffer( sample );
  41.   //GST_DEBUG("Got buffer");
  42.   if ( !gst_video_frame_map( pframe, &info, buffer, GST_MAP_READ | GST_MAP_GL) )
  43.   {
  44.       GST_ERROR( "Failed to map video frame" );
  45.       goto exit_caps;
  46.   }
  47.   data->frame_is_valid = TRUE;
  48.   //GST_DEBUG("Map complete");
  49.   textureId = (jint)(*(guint *) pframe->data[0]);
  50.  
  51.   GST_DEBUG("new_sample_cb(): texture id:%d, width: %d, height: %d format name: %s",
  52.           textureId, pframe->info.width, pframe->info.height,
  53.           pframe->info.finfo->name);
  54.  
  55.   /* Notify application */
  56.   (*env)->CallVoidMethod (env, data->app, on_frame_available_method_id, textureId);
  57.   if ((*env)->ExceptionCheck (env)) {
  58.     GST_ERROR ("Failed to call Java method");
  59.     (*env)->ExceptionClear (env);
  60.   }
  61.  
  62. exit_caps:
  63.   gst_caps_unref(caps);
  64. exit_sample:
  65.   /* Releases the buffer automatically */
  66.   gst_sample_unref(sample);
  67. exit_none:
  68.   return GST_FLOW_OK;
  69. }
Advertisement
Add Comment
Please, Sign In to add comment