aboutsummaryrefslogtreecommitdiff
path: root/gst/gst.c
blob: e84316ea2c84e5e3230afe4d81aff11df58e2109 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include "gst.h"
 
#include <gst/app/gstappsrc.h>
 
typedef struct SampleHandlerUserData {
  int pipelineId;
} SampleHandlerUserData;
 
GMainLoop *gstreamer_send_main_loop = NULL;
void gstreamer_send_start_mainloop(void) {
  gstreamer_send_main_loop = g_main_loop_new(NULLFALSE);
 
  g_main_loop_run(gstreamer_send_main_loop);
}
 
static gboolean gstreamer_send_bus_call(GstBus *bus, GstMessage *msg, gpointer data) {
  GstElement *pipeline = GST_ELEMENT(data);
 
  switch (GST_MESSAGE_TYPE(msg)) {
  case GST_MESSAGE_EOS:
    if (!gst_element_seek (pipeline, 1.0, GST_FORMAT_TIME, GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_KEY_UNIT | GST_SEEK_FLAG_SKIP,
             GST_SEEK_TYPE_SET, 0,
             GST_SEEK_TYPE_NONE, GST_CLOCK_TIME_NONE)) {
        g_print ("EOS restart failed\n");
        exit(1);
    }
    break;
 
  case GST_MESSAGE_ERROR: {
    gchar *debug;
    GError *error;
 
    gst_message_parse_error(msg, &error, &debug);
    g_free(debug);
 
    g_printerr("GStreamer Error: %s\n", error->message);
    g_error_free(error);
    exit(1);
  }
  default:
    break;
  }
 
  return TRUE;
}
 
GstFlowReturn gstreamer_send_new_sample_handler(GstElement *object, gpointer user_data) {
  GstSample *sample = NULL;
  GstBuffer *buffer = NULL;
  gpointer copy = NULL;
  gsize copy_size = 0;
  int *isVideo = (int *) user_data;
 
  g_signal_emit_by_name (object, "pull-sample", &sample);
  if (sample) {
    buffer = gst_sample_get_buffer(sample);
    if (buffer) {
      gst_buffer_extract_dup(buffer, 0, gst_buffer_get_size(buffer), &copy, &copy_size);
      goHandlePipelineBuffer(copy, copy_size, GST_BUFFER_DURATION(buffer), *isVideo);
    }
    gst_sample_unref (sample);
  }
 
  return GST_FLOW_OK;
}
 
GstElement *gstreamer_send_create_pipeline(char *pipeline) {
  gst_init(NULLNULL);
  GError *error = NULL;
  return gst_parse_launch(pipeline, &error);
}
 
void gstreamer_send_start_pipeline(GstElement *pipeline) {
  GstBus *bus = gst_pipeline_get_bus(GST_PIPELINE(pipeline));
  gst_bus_add_watch(bus, gstreamer_send_bus_call, pipeline);
  gst_object_unref(bus);
 
  GstElement *audio = gst_bin_get_by_name(GST_BIN(pipeline), "audio"),
             *video = gst_bin_get_by_name(GST_BIN(pipeline), "video");
 
  int *isAudio = malloc(sizeof(int)),
      *isVideo = malloc(sizeof(int));
 
  *isVideo = 1;
  *isAudio = 0;
 
  g_object_set(audio, "emit-signals"TRUENULL);
  g_signal_connect(audio, "new-sample", G_CALLBACK(gstreamer_send_new_sample_handler), isAudio);
 
  g_object_set(video, "emit-signals"TRUENULL);
  g_signal_connect(video, "new-sample", G_CALLBACK(gstreamer_send_new_sample_handler), isVideo);
 
  gstreamer_send_play_pipeline(pipeline);
}
 
void gstreamer_send_play_pipeline(GstElement *pipeline) {
  gst_element_set_state(pipeline, GST_STATE_PLAYING);
}
 
void gstreamer_send_pause_pipeline(GstElement *pipeline) {
  gst_element_set_state(pipeline, GST_STATE_PAUSED);
}
 
void gstreamer_send_seek(GstElement *pipeline, int64_t seek_pos) {
    if (!gst_element_seek (pipeline, 1.0, GST_FORMAT_TIME, GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_KEY_UNIT | GST_SEEK_FLAG_SKIP,
             GST_SEEK_TYPE_SET, seek_pos * GST_SECOND,
             GST_SEEK_TYPE_NONE, GST_CLOCK_TIME_NONE)) {
        g_print ("Seek failed!\n");
    }
}