TX2 Using Gsteramer to save YUV as mp4 failed

The following is my code:

#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <time.h>
#include <math.h>
#include <sys/ioctl.h>
#include <linux/fb.h>
#include <iostream>
#include <signal.h>

#include <gst/gst.h>
#include <gst/app/gstappsrc.h>
#include <gst/app/gstappsink.h>


using namespace cv;


const unsigned int FRAMEWIDTH = 1920;								
const unsigned int FRAMEHEIGHT = 1080;	
const unsigned int FRAMELENYUV =  FRAMEWIDTH * FRAMEHEIGHT ; 

static unsigned char rawdata[FRAMELENYUV];
unsigned char storage[1024*1024*150];
unsigned int storage_num = 0;

static GstElement *source;
static GstElement *filter1;
static GstCaps *filter_caps1;
static GstElement * videoparse;
static GstElement *enconder;
static GstElement *filter2;
static GstElement *parser;
static GstElement *muxer;
static GstElement *sink;
static GstCaps *filter_caps2;

GMainLoop *loop;
GstElement *pipeline ;
GstBus *bus;
guint bus_watch_id;


gint iFrame = 10;
char filename[200];
FILE *fd;


gboolean bus_call(GstBus *bus, GstMessage *msg, gpointer data)
{
	GMainLoop *loop = (GMainLoop *) data;
	switch (GST_MESSAGE_TYPE (msg))
	{

	case GST_MESSAGE_EOS:
		fprintf(stderr, "End of stream\n");
		g_main_loop_quit(loop);
		break;
	case GST_MESSAGE_ERROR:
	{
		gchar *debug;
		GError *error;
		gst_message_parse_error(msg, &error, &debug);
		g_free(debug);
		g_printerr("Error: %s\n", error->message);
		g_error_free(error);
		g_main_loop_quit(loop);
		break;
	}
	default:
		break;
	}
	return TRUE;
}


void PushBuffer()
{
	GstFlowReturn ret;
	GstBuffer *buffer;
	int size = FRAMELENYUV;
	GstMapInfo info;

	buffer = gst_buffer_new_allocate(NULL, size, NULL); 		
	gst_buffer_map(buffer, &info, GST_MAP_WRITE);
	unsigned char* buf = info.data;
	memmove(buf, rawdata, size);
	gst_buffer_unmap(buffer, &info);
	ret = gst_app_src_push_buffer(GST_APP_SRC(source), buffer);
	if(ret < 0)
	{
		puts("############################\n");
	}
	else
	{
		;
	}
}

int factory_make()
{	

	pipeline = gst_pipeline_new("mypipeline");
	source = gst_element_factory_make("appsrc", "mysource");	
	
	enconder = gst_element_factory_make("omxh264enc", "myencoder");
	filter1 = gst_element_factory_make ("capsfilter", "mufilter");
	parser = gst_element_factory_make ("h264parse", "myparser");
	muxer = gst_element_factory_make ("qtmux","mymuxer");	
	sink = 	gst_element_factory_make ("appsink","mysink");
	filter2 = gst_element_factory_make ("capsfilter", "mufilter");


	filter_caps1 = gst_caps_new_simple ("video/x-raw", "format", G_TYPE_STRING,"I420", "width",G_TYPE_INT,FRAMEWIDTH,"height",G_TYPE_INT,FRAMEHEIGHT,NULL);	
	filter_caps2 = gst_caps_new_simple ("video/x-h264", "stream-format", G_TYPE_STRING,"byte-stream", NULL);

	if(!pipeline || !source || !enconder || !filter1 || !filter2 ||!parser || !muxer ||!muxer || !filter_caps2 || !filter_caps1)
	{
		fprintf(stderr, "Could not gst_element_factory_make, terminating\n");
		exit(0);		
	}

		
	g_object_set (G_OBJECT (filter1), "caps", filter_caps1, NULL);
	g_object_set (G_OBJECT (filter2), "caps", filter_caps2, NULL);
	gst_caps_unref(filter_caps1);
	gst_caps_unref(filter_caps2);


	g_object_set(G_OBJECT (enconder), "insert-sps-pps", true , NULL);
	g_object_set(G_OBJECT (enconder), "profile", "high" , NULL);
	g_object_set(G_OBJECT(enconder) , "iframeinterval" , 15 , NULL);	
	
	char szTemp[64];
	sprintf(szTemp, "%d" , FRAMELENYUV);
	g_object_set(G_OBJECT (source), "blocksize", szTemp,NULL);
	g_object_set(G_OBJECT(videoparse) , "width" , FRAMEWIDTH , "height" , FRAMEHEIGHT , NULL);

	return 0;
}


int pipelinemake()
{	
	gst_bin_add_many(GST_BIN(pipeline), source, filter_caps1,enconder,filter2,parser,muxer,sink, NULL);		
	gst_element_link_many(source,filter1 ,enconder, filter2,parser,muxer,sink);
	
	return 0;	
}

int watcher_make()
{
	bus = gst_pipeline_get_bus(GST_PIPELINE(pipeline));
	bus_watch_id = gst_bus_add_watch(bus, bus_call, loop);
	gst_object_unref(bus);

	return 0;	
}


void *GstreamerThdFun(void *)
{
	
	gst_init(NULL, NULL);	
	
	loop = g_main_loop_new(NULL, FALSE);

	factory_make();
	watcher_make();
	pipelinemake();					
	gst_element_set_state(pipeline, GST_STATE_PLAYING);				
	g_main_loop_run(loop);											

	//release all															
	gst_element_set_state(pipeline, GST_STATE_NULL);				
	gst_object_unref(GST_OBJECT(pipeline));
	g_source_remove (bus_watch_id);
	g_main_loop_unref (loop);										
}

void *sendThdFun(void*)
{
       
    unsigned long int max_file = 1024*1024*512;
    int countnum = 0;

    while(1)
    {

	usleep(1000*5);

        GstSample* sample = gst_app_sink_pull_sample(GST_APP_SINK(sink));
        if(sample != NULL)
        {
            GstBuffer* buffer = gst_sample_get_buffer (sample);
            GstMapInfo map;
            gst_buffer_map (buffer, &map, GST_MAP_READ);
			
		
			if(storage_num>1024*1024*10)
			{
	    		        fwrite(storage, sizeof(unsigned char), storage_num , fd);
				storage_num = 0;
	    	}
			else
			{
				memcpy(storage + storage_num,map.data,map.size);
				storage_num += map.size;
			}
			countnum += map.size;
			if(countnum >= max_file)
			{
				fclose(fd);				
				countnum = 0;
			}
			
	       gst_buffer_unmap (buffer, &map);
           gst_sample_unref (sample);
       }
       else
       {
		printf("******************************\n");
       }
    }
}

void ReadFile(char* name)
{
	FILE* pFile = fopen(name, "rb");
	if(pFile == NULL)
	{
		fprintf(stderr, "Could not open input file. Exiting.\n");
		exit(-1);
	}

	if(fread(rawdata, 1, FRAMELENYUV, pFile) != FRAMELENYUV)
	{
		fclose(pFile);
		fprintf(stderr, "Could not read input file. Exiting.\n");
		exit(-1);
	}
	fclose(pFile);
}


int main()
{
   sprintf(filename,"test.mp4");
   fd = fopen(filename,"wb");
   
   pthread_t GstreamerThd;
   pthread_t sendFrameThd; 	

   
   int  ret ;
   
    ret = pthread_create(&GstreamerThd, NULL,GstreamerThdFun, NULL);
    if (ret != 0)
	{
		fprintf(stderr, "GstreamerThd returned error %d\n", ret);
		exit(0);
	}
	ret = pthread_create(&sendFrameThd, NULL,sendThdFun, NULL);	
	if (ret != 0)
	{
		fprintf(stderr, "sendFrameThd returned error %d\n", ret);
		exit(0);
	}
   
   ReadFile("a.yuv");

   while(1)
    {    
		usleep(1000*5);			
		
		PushBuffer();				

    }
	

    gst_app_src_end_of_stream(GST_APP_SRC(source));
   
    return(0);	
	
}

Error Message:
gst_app_sink_pull_sample: assertion ‘GST_IS_APP_SINK (appsink)’ failed
GLib-GObject-WARNING **: invalid unclassed pointer in cast to ‘GstAppSink’
gst_app_sink_pull_sample: assertion ‘GST_IS_APP_SINK (appsink)’ failed

Please not make duplicate threads https://devtalk.nvidia.com/default/topic/1038275/