-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
manishiitg
committed
Jun 21, 2021
1 parent
7ce93f9
commit 95e39d3
Showing
8 changed files
with
309 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
#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(NULL, FALSE); | ||
|
||
g_main_loop_run(gstreamer_send_main_loop); | ||
} | ||
|
||
static gboolean gstreamer_send_bus_call(GstBus *bus, GstMessage *msg, gpointer data) { | ||
switch (GST_MESSAGE_TYPE(msg)) { | ||
|
||
case GST_MESSAGE_EOS: | ||
g_print("End of stream\n"); | ||
exit(1); | ||
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); | ||
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; | ||
SampleHandlerUserData *s = (SampleHandlerUserData *)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), ©, ©_size); | ||
goHandlePipelineBuffer(copy, copy_size, GST_BUFFER_DURATION(buffer), s->pipelineId); | ||
} | ||
gst_sample_unref (sample); | ||
} | ||
|
||
return GST_FLOW_OK; | ||
} | ||
|
||
GstElement *gstreamer_send_create_pipeline(char *pipeline) { | ||
gst_init(NULL, NULL); | ||
GError *error = NULL; | ||
return gst_parse_launch(pipeline, &error); | ||
} | ||
|
||
void gstreamer_send_start_pipeline(GstElement *pipeline, int pipelineId) { | ||
SampleHandlerUserData *s = calloc(1, sizeof(SampleHandlerUserData)); | ||
s->pipelineId = pipelineId; | ||
|
||
GstBus *bus = gst_pipeline_get_bus(GST_PIPELINE(pipeline)); | ||
gst_bus_add_watch(bus, gstreamer_send_bus_call, NULL); | ||
gst_object_unref(bus); | ||
|
||
GstElement *appsink = gst_bin_get_by_name(GST_BIN(pipeline), "appsink"); | ||
g_object_set(appsink, "emit-signals", TRUE, NULL); | ||
g_signal_connect(appsink, "new-sample", G_CALLBACK(gstreamer_send_new_sample_handler), s); | ||
gst_object_unref(appsink); | ||
|
||
gst_element_set_state(pipeline, GST_STATE_PLAYING); | ||
} | ||
|
||
void gstreamer_send_stop_pipeline(GstElement *pipeline) { | ||
gst_element_set_state(pipeline, GST_STATE_NULL); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
// Package gst provides an easy API to create an appsink pipeline | ||
package gstreamergst | ||
|
||
/* | ||
#cgo pkg-config: gstreamer-1.0 gstreamer-app-1.0 | ||
#include "gst.h" | ||
*/ | ||
import "C" | ||
import ( | ||
"fmt" | ||
"sync" | ||
"time" | ||
"unsafe" | ||
|
||
"github.com/pion/webrtc/v3" | ||
"github.com/pion/webrtc/v3/pkg/media" | ||
) | ||
|
||
func init() { | ||
go C.gstreamer_send_start_mainloop() | ||
} | ||
|
||
// Pipeline is a wrapper for a GStreamer Pipeline | ||
type Pipeline struct { | ||
Pipeline *C.GstElement | ||
tracks []*webrtc.TrackLocalStaticSample | ||
id int | ||
codecName string | ||
clockRate float32 | ||
} | ||
|
||
var pipelines = make(map[int]*Pipeline) | ||
var pipelinesLock sync.Mutex | ||
|
||
const ( | ||
videoClockRate = 90000 | ||
audioClockRate = 48000 | ||
pcmClockRate = 8000 | ||
) | ||
|
||
// CreatePipeline creates a GStreamer Pipeline | ||
func CreatePipeline(codecName string, tracks []*webrtc.TrackLocalStaticSample, pipelineSrc string) *Pipeline { | ||
pipelineStr := "appsink name=appsink" | ||
var clockRate float32 | ||
|
||
switch codecName { | ||
case "vp8": | ||
pipelineStr = pipelineSrc + " ! vp8enc error-resilient=partitions keyframe-max-dist=10 auto-alt-ref=true cpu-used=5 deadline=1 ! " + pipelineStr | ||
clockRate = videoClockRate | ||
|
||
case "vp9": | ||
pipelineStr = pipelineSrc + " ! vp9enc ! " + pipelineStr | ||
clockRate = videoClockRate | ||
|
||
case "h264": | ||
pipelineStr = pipelineSrc + " ! video/x-raw,format=I420 ! x264enc speed-preset=ultrafast tune=zerolatency key-int-max=20 ! video/x-h264,stream-format=byte-stream ! " + pipelineStr | ||
clockRate = videoClockRate | ||
|
||
case "opus": | ||
pipelineStr = pipelineSrc + " ! opusenc ! " + pipelineStr | ||
clockRate = audioClockRate | ||
|
||
case "g722": | ||
pipelineStr = pipelineSrc + " ! avenc_g722 ! " + pipelineStr | ||
clockRate = audioClockRate | ||
|
||
case "pcmu": | ||
pipelineStr = pipelineSrc + " ! audio/x-raw, rate=8000 ! mulawenc ! " + pipelineStr | ||
clockRate = pcmClockRate | ||
|
||
case "pcma": | ||
pipelineStr = pipelineSrc + " ! audio/x-raw, rate=8000 ! alawenc ! " + pipelineStr | ||
clockRate = pcmClockRate | ||
|
||
default: | ||
panic("Unhandled codec " + codecName) | ||
} | ||
|
||
pipelineStrUnsafe := C.CString(pipelineStr) | ||
defer C.free(unsafe.Pointer(pipelineStrUnsafe)) | ||
|
||
pipelinesLock.Lock() | ||
defer pipelinesLock.Unlock() | ||
|
||
pipeline := &Pipeline{ | ||
Pipeline: C.gstreamer_send_create_pipeline(pipelineStrUnsafe), | ||
tracks: tracks, | ||
id: len(pipelines), | ||
codecName: codecName, | ||
clockRate: clockRate, | ||
} | ||
|
||
pipelines[pipeline.id] = pipeline | ||
return pipeline | ||
} | ||
|
||
// Start starts the GStreamer Pipeline | ||
func (p *Pipeline) Start() { | ||
C.gstreamer_send_start_pipeline(p.Pipeline, C.int(p.id)) | ||
} | ||
|
||
// Stop stops the GStreamer Pipeline | ||
func (p *Pipeline) Stop() { | ||
C.gstreamer_send_stop_pipeline(p.Pipeline) | ||
} | ||
|
||
//export goHandlePipelineBuffer | ||
func goHandlePipelineBuffer(buffer unsafe.Pointer, bufferLen C.int, duration C.int, pipelineID C.int) { | ||
pipelinesLock.Lock() | ||
pipeline, ok := pipelines[int(pipelineID)] | ||
pipelinesLock.Unlock() | ||
|
||
if ok { | ||
for _, t := range pipeline.tracks { | ||
if err := t.WriteSample(media.Sample{Data: C.GoBytes(buffer, bufferLen), Duration: time.Duration(duration)}); err != nil { | ||
panic(err) | ||
} | ||
} | ||
} else { | ||
fmt.Printf("discarding buffer, no pipeline with id %d", int(pipelineID)) | ||
} | ||
C.free(buffer) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// #ifndef GST_H | ||
// #define GST_H | ||
|
||
// #include <glib.h> | ||
// #include <gst/gst.h> | ||
// #include <stdint.h> | ||
// #include <stdlib.h> | ||
|
||
// extern void goHandlePipelineBuffer(void *buffer, int bufferLen, int samples, int pipelineId); | ||
|
||
// GstElement *gstreamer_send_create_pipeline(char *pipeline); | ||
// void gstreamer_send_start_pipeline(GstElement *pipeline, int pipelineId); | ||
// void gstreamer_send_stop_pipeline(GstElement *pipeline); | ||
// void gstreamer_send_start_mainloop(void); | ||
|
||
// #endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.