Archived
2
0

catch errors from gst pipeline.

This commit is contained in:
Miroslav Šedivý 2021-08-15 15:37:27 +02:00
parent 6708ce2caf
commit 9e466b08cc
7 changed files with 47 additions and 19 deletions

View File

@ -1,6 +1,8 @@
package broadcast package broadcast
import ( import (
"sync"
"github.com/rs/zerolog" "github.com/rs/zerolog"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
@ -9,6 +11,7 @@ import (
) )
type BroadcastManager struct { type BroadcastManager struct {
mu sync.Mutex
logger zerolog.Logger logger zerolog.Logger
pipeline *gst.Pipeline pipeline *gst.Pipeline
remote *config.Remote remote *config.Remote
@ -27,9 +30,9 @@ func New(remote *config.Remote, config *config.Broadcast) *BroadcastManager {
} }
} }
func (manager *BroadcastManager) Start() { func (manager *BroadcastManager) Start() error {
if !manager.enabled || manager.IsActive() { if !manager.enabled || manager.IsActive() {
return return nil
} }
var err error var err error
@ -40,18 +43,19 @@ func (manager *BroadcastManager) Start() {
manager.url, manager.url,
) )
if err != nil {
manager.pipeline = nil
return err
}
manager.logger.Info(). manager.logger.Info().
Str("audio_device", manager.remote.Device). Str("audio_device", manager.remote.Device).
Str("video_display", manager.remote.Display). Str("video_display", manager.remote.Display).
Str("rtmp_pipeline_src", manager.pipeline.Src). Str("rtmp_pipeline_src", manager.pipeline.Src).
Msgf("RTMP pipeline is starting...") Msgf("RTMP pipeline is starting...")
if err != nil {
manager.logger.Panic().Err(err).Msg("unable to create rtmp pipeline")
return
}
manager.pipeline.Play() manager.pipeline.Play()
return nil
} }
func (manager *BroadcastManager) Stop() { func (manager *BroadcastManager) Stop() {
@ -67,13 +71,25 @@ func (manager *BroadcastManager) IsActive() bool {
return manager.pipeline != nil return manager.pipeline != nil
} }
func (manager *BroadcastManager) Create(url string) { func (manager *BroadcastManager) Create(url string) error {
manager.mu.Lock()
defer manager.mu.Unlock()
manager.url = url manager.url = url
manager.enabled = true manager.enabled = true
manager.Start()
err := manager.Start()
if err != nil {
manager.enabled = false
}
return err
} }
func (manager *BroadcastManager) Destroy() { func (manager *BroadcastManager) Destroy() {
manager.mu.Lock()
defer manager.mu.Unlock()
manager.Stop() manager.Stop()
manager.enabled = false manager.enabled = false
} }

View File

@ -63,9 +63,8 @@ GstFlowReturn gstreamer_send_new_sample_handler(GstElement *object, gpointer use
return GST_FLOW_OK; return GST_FLOW_OK;
} }
GstElement *gstreamer_send_create_pipeline(char *pipeline) { GstElement *gstreamer_send_create_pipeline(char *pipeline, GError **error) {
GError *error = NULL; return gst_parse_launch(pipeline, error);
return gst_parse_launch(pipeline, &error);
} }
void gstreamer_send_start_pipeline(GstElement *pipeline, int pipelineId) { void gstreamer_send_start_pipeline(GstElement *pipeline, int pipelineId) {

View File

@ -208,8 +208,15 @@ func CreatePipeline(pipelineStr string) (*Pipeline, error) {
pipelinesLock.Lock() pipelinesLock.Lock()
defer pipelinesLock.Unlock() defer pipelinesLock.Unlock()
var err *C.GError
gstPipeline := C.gstreamer_send_create_pipeline(pipelineStrUnsafe, &err)
if err != nil {
defer C.g_error_free(err)
return nil, fmt.Errorf("%s", C.GoString(err.message))
}
p := &Pipeline{ p := &Pipeline{
Pipeline: C.gstreamer_send_create_pipeline(pipelineStrUnsafe), Pipeline: gstPipeline,
Sample: make(chan types.Sample), Sample: make(chan types.Sample),
Src: pipelineStr, Src: pipelineStr,
id: len(pipelines), id: len(pipelines),

View File

@ -8,7 +8,7 @@
extern void goHandlePipelineBuffer(void *buffer, int bufferLen, int samples, int pipelineId); extern void goHandlePipelineBuffer(void *buffer, int bufferLen, int samples, int pipelineId);
GstElement *gstreamer_send_create_pipeline(char *pipeline); GstElement *gstreamer_send_create_pipeline(char *pipeline, GError **error);
void gstreamer_send_start_pipeline(GstElement *pipeline, int pipelineId); void gstreamer_send_start_pipeline(GstElement *pipeline, int pipelineId);
void gstreamer_send_play_pipeline(GstElement *pipeline); void gstreamer_send_play_pipeline(GstElement *pipeline);

View File

@ -56,7 +56,9 @@ func (manager *RemoteManager) Start() {
} }
manager.createPipelines() manager.createPipelines()
manager.broadcast.Start() if err := manager.broadcast.Start(); err != nil {
manager.logger.Panic().Err(err).Msg("unable to create rtmp pipeline")
}
go func() { go func() {
defer func() { defer func() {
@ -171,7 +173,9 @@ func (manager *RemoteManager) ChangeResolution(width int, height int, rate int)
defer func() { defer func() {
manager.video.Start() manager.video.Start()
manager.broadcast.Start() if err := manager.broadcast.Start(); err != nil {
manager.logger.Panic().Err(err).Msg("unable to create rtmp pipeline")
}
manager.logger.Info().Msg("starting video pipeline...") manager.logger.Info().Msg("starting video pipeline...")
}() }()

View File

@ -1,10 +1,10 @@
package types package types
type BroadcastManager interface { type BroadcastManager interface {
Start() Start() error
Stop() Stop()
IsActive() bool IsActive() bool
Create(url string) Create(url string) error
Destroy() Destroy()
GetUrl() string GetUrl() string
} }

View File

@ -12,7 +12,9 @@ func (h *MessageHandler) boradcastCreate(session types.Session, payload *message
return nil return nil
} }
h.broadcast.Create(payload.URL) if err := h.broadcast.Create(payload.URL); err != nil {
return err
}
if err := h.boradcastStatus(session); err != nil { if err := h.boradcastStatus(session); err != nil {
return err return err