2020-05-15 12:27:34 +02:00

113 lines
3.3 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <gst/gst.h>
#include "streaming.h"
static GstElement* streaming_pipeline = NULL;
int streaming_enable(char* host, char* port, char* app, char* serial_number, char* handle) {
char pipeline_command[512];
GError* error = NULL;
sprintf(pipeline_command, "v4l2src device=/dev/video0 ! video/x-raw,width=640,height=480,framerate=30/1 ! clockoverlay font-desc=\"DejaVu Sans Book 15\" shaded-background=false ! omxh264enc target-bitrate=700000 control-rate=variable ! video/x-h264,profile=high ! h264parse ! flvmux ! rtmpsink location=rtmp://%s:%s/%s/%s?rtmp_handle=%s", host, port, app, serial_number, handle);
if(streaming_pipeline != NULL) {
return 0;
}
printf("[CameraManager] : Enable RTMP streaming...");
streaming_pipeline = gst_parse_launch(pipeline_command, &error);
if(streaming_pipeline == NULL) {
printf("Failed\n");
fprintf(stderr, "[CameraManager] (error) : Unable to create the pipeline, error = %s\n", error->message);
g_error_free(error);
return -1;
}
gst_element_set_state(streaming_pipeline, GST_STATE_PLAYING);
if(gst_element_get_state(streaming_pipeline, NULL, NULL, 5000000000) != GST_STATE_CHANGE_SUCCESS) {
printf("Failed\n");
fprintf(stderr, "[CameraManager] (error) : Unable to change state of pipeline\n");
gst_object_unref(streaming_pipeline);
streaming_pipeline = NULL;
return -2;
}
printf("Success\n");
return 0;
}
int streaming_disable() {
if(streaming_pipeline == NULL) {
return 0;
}
printf("[CameraManager] : Disable RTMP streaming...");
gst_element_set_state(streaming_pipeline, GST_STATE_NULL);
if(gst_element_get_state(streaming_pipeline, NULL, NULL, 5000000000) != GST_STATE_CHANGE_SUCCESS) {
printf("Failed\n");
fprintf(stderr, "[CameraManager] (error) : Unable to change state of pipeline\n");
gst_object_unref(streaming_pipeline);
streaming_pipeline = NULL;
return -1;
}
gst_object_unref(streaming_pipeline);
streaming_pipeline = NULL;
printf("Success\n");
return 0;
}
int streaming_pop_error() {
GstBus* bus = NULL;
GstMessage* msg = NULL;
GError* error = NULL;
gchar* dbg = NULL;
if(streaming_pipeline == NULL) {
return 0;
}
bus = gst_element_get_bus(streaming_pipeline);
msg = gst_bus_pop_filtered(bus, GST_MESSAGE_EOS | GST_MESSAGE_ERROR);
if(msg != NULL) {
switch(GST_MESSAGE_TYPE(msg)) {
case GST_MESSAGE_EOS:
fprintf(stderr, "[CameraManager] (error) : End of stream\n");
gst_object_unref(streaming_pipeline);
streaming_pipeline = NULL;
gst_object_unref(bus);
gst_message_unref(msg);
return -1;
break;
case GST_MESSAGE_ERROR:
gst_message_parse_error(msg, &error, &dbg);
if(error != NULL) {
fprintf(stderr, "[CameraManager] (error) : Gstreamer error = %s\n", error->message);
g_error_free(error);
}
if(dbg != NULL) {
fprintf(stderr, "[CameraManager] (error) : Gstreamer debug = %s\n", dbg);
g_free(dbg);
}
gst_object_unref(streaming_pipeline);
streaming_pipeline = NULL;
gst_object_unref(bus);
gst_message_unref(msg);
return -2;
break;
default:
gst_message_unref(msg);
break;
}
}
gst_object_unref(bus);
return 0;
}
void streaming_close() {
if(streaming_pipeline != NULL) {
gst_object_unref(streaming_pipeline);
streaming_pipeline = NULL;
}
}