gstreamer-test

houxiaoni01發表於2020-12-24

https://download.csdn.net/download/houxiaoni01/13770958

#include <gst/gst.h>
#include <glib.h>
#include <glib/gprintf.h>
#include <glib-object.h>
#include <gmodule.h>

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <pthread.h>
#include <stdlib.h>
#include <fcntl.h>
#include <fcntl.h>

#define SOCKET_NAME "/tmp/ipc_qt_gst.socket"

int init_p(gchar *file_path);
void start_p_ex();
int start_p(gchar *file_path);
void pause_p();
void recover_p();
void release_p();

//command
enum SIG { INIT=0x02, START=0x03, PAUSE=0x04, EOS=0x05, RELEASE=0x06 };

enum SIG sig; 
GstElement *pipeline;
GstElement *source, *demux, *videobox;
GstElement *videoqueue, *h264parse, *videodecoder, *fbsink;
GstElement *audioqueue, *audioparse, *audiodecoder, *alsasink;
GMainLoop *loop;

int sockfd = -1;

GstBus *bus;
guint bus_watch_id;

gint64 record_pos = 0;

static void *rpc_with_qt()
{
	g_print("=================into rpc_with_qt==================.\n");	
	while(TRUE)
	{
		sleep(20);
		g_print("=================pause video==================.\n");
		sig = PAUSE;
		pause_p();
		sleep(3);
		g_print("=================release video==================.\n");
		sig = EOS;
		release_p();
	}
	return 0;
}

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

    case GST_MESSAGE_EOS: {
		g_print ("End of stream\n");
		//close and unref all element
		//gst_element_query_position (pipeline, GST_FORMAT_TIME, &record_pos);
		//g_print("record postion:%lld\n", record_pos);
		
		//del by zk test
		gst_element_set_state (pipeline, GST_STATE_NULL);
		g_print("========bus_call==========GST_STATE_NULL==================.\n");
		
		//add zzyk
		//gst_element_set_state (pipeline, GST_STATE_READY);
		usleep(500000);
		gst_element_set_state (pipeline, GST_STATE_PLAYING);
		g_print("========bus_call==========GST_STATE_PLAYING==================.\n");
		/* test del by zk
		g_print ("Deleting pipeline\n");
		gst_object_unref (GST_OBJECT (pipeline));
		g_source_remove (bus_watch_id);
		*/
		
		//del zzyk
		/*if(sockfd > 0)
		{
			//send message to QT 
			sig = EOS;
			char buf[100];
			char *info = "got EOS";
			bzero(buf, 100);
			buf[0] = (char)EOS;
			memcpy(buf+1, info, strlen(info));
			if(write(sockfd, buf, 100) < 0)
				g_print("err write eos signal to socket.\n");
		}
		else
		{
			g_print("sockfd is -1.\n");
		}*/
		//record_pos = 0;
		//g_main_loop_quit (loop_t);
		break;
	}
    case GST_MESSAGE_ERROR: 
	{
		g_print("========bus_call==========GST_MESSAGE_ERROR==================.\n");
		gchar  *debug;
		GError *error;
	
		gst_message_parse_error (msg, &error, &debug);
		g_free (debug);
	
		g_print ("Error: %s\n", error->message);
		g_error_free (error);
	
		g_main_loop_quit (loop_t);
		break;
    }
	/*
	case GST_MESSAGE_STATE_CHANGED:
	{	
		g_print("========bus_call==========GST_MESSAGE_ERROR==================.\n");	
		
		GstState old_state, new_state;
    
		gst_message_parse_state_changed (msg, &old_state, &new_state, NULL);
		g_print ("Element %s changed state from %s to %s.\n",
        GST_OBJECT_NAME (msg->src),
        gst_element_state_get_name (old_state),
        gst_element_state_get_name (new_state));
		
		break;
	}
	*/
    default:
      break;
  }

  return TRUE;
}

static void demux_pad_added(GstElement *element, 
					 GstPad *new_pad, 
					 gpointer user_data)
{
	g_print("===========demux_pad_added============begin=================\n");
	gchar *name;
	gchar *audio = "audio";
	gchar *video = "video";
	GstPad *sink_pad = NULL;
	
	name = gst_pad_get_name(new_pad);
	g_print("A new pad %s of tsdemux was created\n", name);
	//g_free(name);
	
	if(g_str_match_string(audio, name, FALSE))
		sink_pad = gst_element_get_static_pad(audioqueue, "sink");

	if(g_str_match_string(video, name, FALSE))
		sink_pad = gst_element_get_static_pad(videoqueue, "sink");
		
	if(sink_pad != NULL)
	{
		name = gst_pad_get_name(sink_pad);
		g_print("get pad %s to connect new pad.\n", name);
		//g_free(name);
		gst_pad_link(new_pad, sink_pad);
		gst_object_unref(sink_pad);
	}
	g_print("===========demux_pad_added============end=================\n");
	g_free(name);
}

static GstPadProbeReturn
pad_probe_cb (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
{
	g_print("pad is blocked now.\n");

	/* remove the probe first */
	gst_pad_remove_probe (pad, GST_PAD_PROBE_INFO_ID (info));

	gst_element_set_state (pipeline,GST_STATE_PAUSED);
	g_print("pipeline is pause.\n");
	
	/*while(sig == PAUSE)
	{
		usleep(500000);
	}
	//set state to play
	gst_element_set_state (pipeline ,GST_STATE_PLAYING);
	
	g_print("set sunxifbsink to PLAYING.\n");*/
	
	while(sig == PAUSE)
	{
		//g_print("===============================while=============================\n");
		usleep(500000);
	}
	
	if(sig == EOS)
	{
		//g_print("===============================do nothing==========================\n");
	}
	else
	{
		g_print("pad_probe_cb=====================GST_STATE_PLAYING================== \n");
		//set state to play
		gst_element_set_state (pipeline ,GST_STATE_PLAYING);
	}
	g_print("set sunxifbsink to PLAYING.\n");
	
	return GST_PAD_PROBE_OK;
}

int main(int argc, char *argv[])
{	
	pthread_t rpc_id;
	
	//gst init
	gst_init(&argc, &argv);
	
	loop = g_main_loop_new(NULL, FALSE);
	
	if(argc >= 2)	
	{
		g_print("paly filename is %s\n",argv[1]);
		start_p (argv[1]);
		
		pthread_create (&rpc_id, NULL, rpc_with_qt, NULL);
	}
	else
	{
		g_print("please input filename.\n");
		return 0;
	}
	
	//into main loop
	g_main_loop_run (loop);
	
	g_main_loop_unref (loop);
	
	return 0;
}
int start_p(gchar *file_path)
{
	//GstState state, pending;
	
	gchar uri[100] = "file://";
	
	strcat(uri, file_path);
	
	//creat pipeline
	pipeline = gst_pipeline_new("pipeline");
	//creat elements,file source input/ouput
	source = gst_element_factory_make("filesrc", "source");
	if(source == NULL)
	{
		g_print("creat filesrc err.\n");
		return -1;
	} 
	//specify the source is form location(disk)
	g_object_set (GST_OBJECT (source), "location", file_path, NULL);

	demux = gst_element_factory_make("qtdemux", "demux");
	if(demux == NULL)
	{
		g_print("creat qtdemux err.\n");
		return -1;
	}

	//video branch
	videoqueue = gst_element_factory_make("queue", "videoqueue");
	if(videoqueue == NULL)
	{
		g_print("creat video queue err.\n");
		return -1;
	}

	h264parse = gst_element_factory_make("h264parse", "h264parse");
	if(h264parse == NULL)
	{
		g_print("creat h264parse err.\n");
		return -1;
	}

	videodecoder = gst_element_factory_make("omxh264dec", "videodecoder");
	if(videodecoder == NULL)
	{
		g_print("creat omxh264dec err.\n");
		return -1;
	}
	//g_object_set (GST_OBJECT (videodecoder), "max-threads", 20, NULL);
	//g_object_set (GST_OBJECT (videodecoder), "lowres", 1, NULL);
	
	//creat sink
	fbsink = gst_element_factory_make("sunxifbsink", "fbsink");

	if(fbsink == NULL)
	{
		g_print("creat sunxifbsink err.\n");
		return -1;
	}
	g_object_set(GST_OBJECT (fbsink), "device", "/dev/fb0", NULL);
	g_object_set(GST_OBJECT (fbsink), "async", FALSE, NULL);
	g_object_set(GST_OBJECT (fbsink), "width", 640, NULL);
	g_object_set(GST_OBJECT (fbsink), "height", 480, NULL);
	

	//audio branch
	audioqueue = gst_element_factory_make("queue", "audioqueue");
	if(audioqueue == NULL)
	{
		g_print("creat audio queue err.\n");
		return -1;
	}
	audioparse = gst_element_factory_make("mpegaudioparse", "audioparse");
	if(audioparse == NULL)
	{
		g_print("creat mpegaudioparse err.\n");
		return -1;
	}
	audiodecoder = gst_element_factory_make("avdec_mp3", "audiodecoder");
	if(audiodecoder == NULL)
	{
		g_print("creat avdec_mp3 err.\n");
		return -1;
	}
	alsasink = gst_element_factory_make("autoaudiosink", "alsasink");
	if(alsasink == NULL)
	{
		g_print("creat autoaudiosink err./n");
		return -1;
	}

	//connect tsdemux pad-added signal and pass element pointer to callback function
	g_signal_connect(demux, "pad-added", G_CALLBACK(demux_pad_added), loop);

	//add element to pipeline
	if(!gst_bin_add(GST_BIN(pipeline), source))
		g_print("add udpsrc to pipeline err.\n");
	if(!gst_bin_add(GST_BIN(pipeline), demux))
		g_print("add tsdemux to pipeline err.\n");

	gst_element_link(source, demux);

	//video branch
	if(!gst_bin_add(GST_BIN(pipeline), videoqueue))
		g_print("add videoqueue to pipeline err.\n");
	if(!gst_bin_add(GST_BIN(pipeline), h264parse))
		g_print("add h264parse to pipeline err.\n");
	if(!gst_bin_add(GST_BIN(pipeline), videodecoder))
		g_print("add videodecoder to pipeline err.\n");
	if(!gst_bin_add(GST_BIN(pipeline), fbsink))
		g_print("add fbsink to pipeline err./n");

	gst_element_link_many (videoqueue, h264parse, videodecoder, fbsink, NULL);
	
	//audio branch
	if(!gst_bin_add(GST_BIN(pipeline), audioqueue))
		g_print("add audio queue to pipeline err.\n");
	if(!gst_bin_add(GST_BIN(pipeline), audioparse))
		g_print("add mpegaudioparse to pipeline err.\n");
	if(!gst_bin_add(GST_BIN(pipeline), audiodecoder))
		g_print("add avdec_mp3 to pipeline err.\n");
	if(!gst_bin_add(GST_BIN(pipeline), alsasink))
		g_print("add autoaudiosink to pipeline err.\n");
	
	gst_element_link_many (audioqueue, audioparse, audiodecoder, alsasink, NULL);
	//get bus to handle message
	bus = gst_pipeline_get_bus(GST_PIPELINE(pipeline));
	bus_watch_id = gst_bus_add_watch(bus, bus_call, NULL);
	gst_object_unref(bus);
	
	/*if(record_pos > GST_SECOND) {
		gst_element_set_state (pipeline, GST_STATE_PAUSED);
		do{
			gst_element_get_state (pipeline,
									&state,
									&pending,
									GST_CLOCK_TIME_NONE);
		}while(GST_STATE_PAUSED != state);
		g_print ("<----------------------------------------->seek\n");
		if (!gst_element_seek (pipeline, 1.0, GST_FORMAT_TIME, 
				GST_SEEK_FLAG_FLUSH |GST_SEEK_FLAG_KEY_UNIT |GST_SEEK_FLAG_SNAP_BEFORE,
							GST_SEEK_TYPE_SET, record_pos,
							GST_SEEK_TYPE_NONE, GST_CLOCK_TIME_NONE)) {
			g_print ("<-------------------------------------->Seek failed!\n");
		}
		g_print ("<----------------------------------------->end seek\n");
		do{
			gst_element_get_state (pipeline,
									&state,
									&pending,
									GST_CLOCK_TIME_NONE);
		}while(GST_STATE_PAUSED != state);
	}*/
	g_print ("<----------------------------------------->start_p\n");
	gst_element_set_state (pipeline, GST_STATE_PLAYING);
	return 0;
}

int init_p(gchar *file_path)
{
	//GstState state, pending;
	
	gchar uri[100] = "file://";
	
	strcat(uri, file_path);
	
	//creat pipeline
	pipeline = gst_pipeline_new("pipeline");
	//creat elements,file source input/ouput
	source = gst_element_factory_make("filesrc", "source");
	if(source == NULL)
	{
		g_print("creat filesrc err.\n");
		return -1;
	} 
	//specify the source is form location(disk)
	g_object_set (GST_OBJECT (source), "location", file_path, NULL);

	demux = gst_element_factory_make("qtdemux", "demux");
	if(demux == NULL)
	{
		g_print("creat qtdemux err.\n");
		return -1;
	}

	//video branch
	videoqueue = gst_element_factory_make("queue", "videoqueue");
	if(videoqueue == NULL)
	{
		g_print("creat video queue err.\n");
		return -1;
	}

	h264parse = gst_element_factory_make("h264parse", "h264parse");
	if(h264parse == NULL)
	{
		g_print("creat h264parse err.\n");
		return -1;
	}

	videodecoder = gst_element_factory_make("omxh264dec", "videodecoder");
	if(videodecoder == NULL)
	{
		g_print("creat omxh264dec err.\n");
		return -1;
	}
	//g_object_set (GST_OBJECT (videodecoder), "max-threads", 20, NULL);
	//g_object_set (GST_OBJECT (videodecoder), "lowres", 1, NULL);
	
	//creat sink
	fbsink = gst_element_factory_make("sunxifbsink", "fbsink");
	if(fbsink == NULL)
	{
		g_print("creat sunxifbsink err.\n");
		return -1;
	}
	g_object_set(GST_OBJECT (fbsink), "device", "/dev/fb0", NULL);
	g_object_set(GST_OBJECT (fbsink), "async", FALSE, NULL);
	g_object_set(GST_OBJECT (fbsink), "width", 1360, NULL);
	g_object_set(GST_OBJECT (fbsink), "height", 768, NULL);

	//audio branch
	audioqueue = gst_element_factory_make("queue", "audioqueue");
	if(audioqueue == NULL)
	{
		g_print("creat audio queue err.\n");
		return -1;
	}
	audioparse = gst_element_factory_make("mpegaudioparse", "audioparse");
	if(audioparse == NULL)
	{
		g_print("creat mpegaudioparse err.\n");
		return -1;
	}
	audiodecoder = gst_element_factory_make("avdec_mp3", "audiodecoder");
	if(audiodecoder == NULL)
	{
		g_print("creat avdec_mp3 err.\n");
		return -1;
	}
	alsasink = gst_element_factory_make("autoaudiosink", "alsasink");
	if(alsasink == NULL)
	{
		g_print("creat autoaudiosink err./n");
		return -1;
	}

	//connect tsdemux pad-added signal and pass element pointer to callback function
	g_signal_connect(demux, "pad-added", G_CALLBACK(demux_pad_added), loop);

	//add element to pipeline
	if(!gst_bin_add(GST_BIN(pipeline), source))
		g_print("add udpsrc to pipeline err.\n");
	if(!gst_bin_add(GST_BIN(pipeline), demux))
		g_print("add tsdemux to pipeline err.\n");

	gst_element_link(source, demux);
	//video branch
	if(!gst_bin_add(GST_BIN(pipeline), videoqueue))
		g_print("add video queue to pipeline err.\n");
	if(!gst_bin_add(GST_BIN(pipeline), h264parse))
		g_print("add h264parse to pipeline err.\n");
	if(!gst_bin_add(GST_BIN(pipeline), videodecoder))
		g_print("add omxh264dec to pipeline err.\n");
	if(!gst_bin_add(GST_BIN(pipeline), fbsink))
		g_print("add sunxifbsink to pipeline err./n");

	gst_element_link_many (videoqueue, h264parse, videodecoder, fbsink, NULL);

	//audio branch
	if(!gst_bin_add(GST_BIN(pipeline), audioqueue))
		g_print("add audio queue to pipeline err.\n");
	if(!gst_bin_add(GST_BIN(pipeline), audioparse))
		g_print("add mpegaudioparse to pipeline err.\n");
	if(!gst_bin_add(GST_BIN(pipeline), audiodecoder))
		g_print("add avdec_mp3 to pipeline err.\n");
	if(!gst_bin_add(GST_BIN(pipeline), alsasink))
		g_print("add autoaudiosink to pipeline err.\n");
	
	gst_element_link_many (audioqueue, audioparse, audiodecoder, alsasink, NULL);
	//get bus to handle message
	bus = gst_pipeline_get_bus(GST_PIPELINE(pipeline));
	bus_watch_id = gst_bus_add_watch(bus, bus_call, NULL);
	gst_object_unref(bus);
	
	/*if(record_pos > GST_SECOND) {
		gst_element_set_state (pipeline, GST_STATE_PAUSED);
		do{
			gst_element_get_state (pipeline,
									&state,
									&pending,
									GST_CLOCK_TIME_NONE);
		}while(GST_STATE_PAUSED != state);
		g_print ("<----------------------------------------->seek\n");
		if (!gst_element_seek (pipeline, 1.0, GST_FORMAT_TIME, 
				GST_SEEK_FLAG_FLUSH |GST_SEEK_FLAG_KEY_UNIT |GST_SEEK_FLAG_SNAP_BEFORE,
							GST_SEEK_TYPE_SET, record_pos,
							GST_SEEK_TYPE_NONE, GST_CLOCK_TIME_NONE)) {
			g_print ("<-------------------------------------->Seek failed!\n");
		}
		g_print ("<----------------------------------------->end seek\n");
		do{
			gst_element_get_state (pipeline,
									&state,
									&pending,
									GST_CLOCK_TIME_NONE);
		}while(GST_STATE_PAUSED != state);
	}*/
	g_print ("<----------------------------------------->init_p\n");
	gst_element_set_state (pipeline, GST_STATE_READY);

	return 0;
}
void start_p_ex()
{
	g_print ("<----------------------------------------->start_p_ex\n");
	gst_element_set_state(pipeline,  GST_STATE_PLAYING);
}

void pause_p()
{   
	g_print ("<----------------------------------------->pause_p\n");
	GstPad *blockpad;
	blockpad = gst_element_get_static_pad (videodecoder, "src");
	gst_pad_add_probe (blockpad, GST_PAD_PROBE_TYPE_BLOCK_DOWNSTREAM,pad_probe_cb, NULL, NULL);
	gst_object_unref (blockpad);
}

void recover_p()
{
	g_print ("<----------------------------------------->recover_p\n");
	gst_element_set_state (fbsink ,GST_STATE_PLAYING);
}
void release_p()
{
	g_print ("<-----------------begin------------------------>release_p\n");
	//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);//test	

	gst_element_set_state(pipeline, GST_STATE_NULL);
	usleep(1000000);
	gst_element_set_state(pipeline, GST_STATE_READY);
	usleep(3000000);
	g_print ("--------------GST_STATE_PLAYING---------------------------\n");
	gst_element_set_state(pipeline,  GST_STATE_PLAYING);
	/*
	usleep(1000000);
	if(sockfd > 0)
	{
		//send message to QT 
		sig = EOS;
		char buf[100];
		char *info = "got EOS";
		bzero(buf, 100);
		buf[0] = (char)EOS;
		memcpy(buf+1, info, strlen(info));
		if(write(sockfd, buf, 100) < 0)
			g_print("err write eos signal to socket.\n");
	}
	else
	{
		g_print("sockfd is -1.\n");
	}
	*/
	g_print ("<----------------------end------------------->release_p\n");
}