Skip to content

Commit

Permalink
added media access, added udev access,added dvp and csi camera supported
Browse files Browse the repository at this point in the history
  • Loading branch information
yjdwbj committed Jul 24, 2024
1 parent d3045d6 commit 0a11d45
Show file tree
Hide file tree
Showing 11 changed files with 1,019 additions and 56 deletions.
12 changes: 6 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@ EXE=gwc
# https://github.com/theicfire/makefiletutorial
#
#
BLIBS := $(shell pkg-config --libs --cflags gstreamer-webrtc-1.0 gstreamer-sdp-1.0 libsoup-3.0 json-glib-1.0)
BLIBS := $(shell pkg-config --libs --cflags gstreamer-webrtc-1.0 gstreamer-sdp-1.0 libsoup-3.0 json-glib-1.0 libudev)

CFLAGS := $(CFLAGS) $$(pkg-config --cflags glib-2.0 gstreamer-1.0 json-glib-1.0 gstreamer-webrtc-1.0 gstreamer-sdp-1.0 libsoup-3.0 sqlite3)
LIBS=$$(pkg-config --libs glib-2.0 gstreamer-1.0 gstreamer-webrtc-1.0 gstreamer-sdp-1.0 gstreamer-app-1.0 gstreamer-base-1.0 libsoup-3.0 json-glib-1.0 sqlite3)
CFLAGS := $(CFLAGS) $$(pkg-config --cflags glib-2.0 gstreamer-1.0 json-glib-1.0 gstreamer-webrtc-1.0 gstreamer-sdp-1.0 libsoup-3.0 sqlite3 libudev)
LIBS=$$(pkg-config --libs glib-2.0 gstreamer-1.0 gstreamer-webrtc-1.0 gstreamer-sdp-1.0 gstreamer-app-1.0 gstreamer-base-1.0 libsoup-3.0 json-glib-1.0 sqlite3 libudev)


all: webrtc-sendonly rtspsrc-webrtc gwc
webrtc-sendonly: webrtc-sendonly.c v4l2ctl.c common_priv.c
webrtc-sendonly: webrtc-sendonly.c v4l2ctl.c common_priv.c media.c
"$(CC)" $(CFLAGS) $^ $(BLIBS) -o $@

rtspsrc-webrtc: rtspsrc-webrtc.c v4l2ctl.c common_priv.c
rtspsrc-webrtc: rtspsrc-webrtc.c v4l2ctl.c common_priv.c media.c
"$(CC)" $(CFLAGS) $^ $(BLIBS) -o $@

gwc: v4l2ctl.c sql.c soup.c gst-app.c main.c common_priv.c
gwc: v4l2ctl.c sql.c soup.c gst-app.c main.c common_priv.c media.c
"${CC}" -Wall -g -O0 ${CFLAGS} $^ ${LIBS} -o $@


Expand Down
3 changes: 2 additions & 1 deletion config.example
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"http": {
"port": 57778,
"port": 9001,
"host": "127.0.0.1",
"user": "test",
"password": "testsoup"
Expand All @@ -10,6 +10,7 @@
"height": 600,
"framerate": 30,
"io_mode": 2,
"devtype": "USB", /* USB for uvc camera, I2C for DVP and CSI camera */
"device": "/dev/video0",
"type": "image/jpeg",
"format": "NV12"
Expand Down
3 changes: 2 additions & 1 deletion config.jetson
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"videnc": "h264",
"http": {
"port": 57778,
"port": 9001,
"host": "127.0.0.1",
"user": "test",
"password": "testsoup"
Expand All @@ -11,6 +11,7 @@
"height": 2464,
"framerate": 21,
"io_mode": 2,
"devtype": "USB", /* USB for uvc camera, I2C for DVP and CSI camera */
"device": "/dev/video0",
"type": "video/x-raw(memory:NVMM)",
"format": "NV12"
Expand Down
2 changes: 2 additions & 0 deletions data_struct.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ struct _http_data {

typedef struct {
gchar *device;
gchar *devtype;
gchar *spec_drv; // specified command line, i.e: gst-launch-1.0 -v v4l2src device=${device} num-buffers=-1 ...
int32_t width;
int32_t height;
int32_t framerate;
Expand Down
55 changes: 37 additions & 18 deletions gst-app.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ static GMutex G_appsrc_lock;
static GList *G_AppsrcList;

GstConfigData config_data;
GHashTable *capture_htable = NULL;

#define MAKE_ELEMENT_AND_ADD(elem, name) \
G_STMT_START { \
Expand Down Expand Up @@ -667,7 +668,7 @@ static GstElement *get_audio_src() {
filter = gst_element_factory_make("ladspa-sine-so-sine-faaa", NULL);

if (!teesrc || !source || !amp || !postconv || !enc || !filter) {
g_printerr("audio source all elements could be created.\n");
g_warning("audio source all elements could be created.\n");
return NULL;
}

Expand Down Expand Up @@ -718,7 +719,7 @@ static GstPadLinkReturn link_request_src_pad_with_dst_name(GstElement *src, GstE
if ((lret = gst_pad_link(src_pad, sink_pad)) != GST_PAD_LINK_OK) {
gchar *sname = gst_pad_get_name(src_pad);
gchar *dname = gst_pad_get_name(sink_pad);
g_print("Src pad %s link to sink pad %s failed . return: %s\n", sname, dname, get_link_error(lret));
g_print("1Src pad %s link to sink pad %s failed . return: %s\n", sname, dname, get_link_error(lret));
get_pad_caps_info(src_pad);
get_pad_caps_info(sink_pad);
g_free(sname);
Expand All @@ -741,6 +742,7 @@ static GstPadLinkReturn link_request_src_pad(GstElement *src, GstElement *dst) {
gst_element_class_get_metadata(klass, GST_ELEMENT_METADATA_KLASS);
// g_print("class name:%s\n", klassname);
#if GST_VERSION_MINOR >= 20
g_print("GST_VERSION_MINOR >=20\n");
src_pad = gst_element_request_pad_simple(src, "src_%u");

if(src_pad == NULL)
Expand All @@ -756,7 +758,7 @@ static GstPadLinkReturn link_request_src_pad(GstElement *src, GstElement *dst) {
if ((lret = gst_pad_link(src_pad, sink_pad)) != GST_PAD_LINK_OK) {
gchar *sname = gst_pad_get_name(src_pad);
gchar *dname = gst_pad_get_name(sink_pad);
g_print("Src pad %s link to sink pad %s failed . return: %s\n", sname, dname, get_link_error(lret));
g_print("2Src pad %s link to sink pad %s failed . return: %s\n", sname, dname, get_link_error(lret));
get_pad_caps_info(src_pad);
get_pad_caps_info(sink_pad);
g_free(sname);
Expand Down Expand Up @@ -2093,32 +2095,25 @@ void start_udpsrc_webrtcbin(WebrtcItem *item) {
gchar *upenc = g_ascii_strup(config_data.videnc, strlen(config_data.videnc));
// here must have rtph264depay and rtph264pay to be compatible with mobile browser.

if (g_str_has_prefix(config_data.videnc, "h26"))
{
#if defined(HAS_JETSON_NANO)
video_src = g_strdup_printf("udpsrc port=%d multicast-group=%s socket-timestamp=1 ! "
" application/x-rtp,media=(string)video,clock-rate=(int)90000,encoding-name=(string)%s,payload=(int)96 ! "
" rtp%sdepay ! rtp%spay config-interval=-1 aggregate-mode=1 ! %s. ",
config_data.webrtc.udpsink.port, config_data.webrtc.udpsink.addr, upenc, config_data.videnc, config_data.videnc, webrtc_name);
#else
if (g_str_has_prefix(config_data.videnc, "h26")) {
gchar *rtp = get_rtp_args();
video_src = g_strdup_printf("udpsrc port=%d multicast-group=%s multicast-iface=lo socket-timestamp=1 ! "
" application/x-rtp,media=(string)video,clock-rate=(int)90000,encoding-name=(string)%s,payload=(int)96 ! "
" %s ! rtp%spay config-interval=-1 aggregate-mode=1 ! %s. ",
config_data.webrtc.udpsink.port, config_data.webrtc.udpsink.addr, upenc, rtp, config_data.videnc, webrtc_name);

g_free(rtp);
#endif
} else
video_src = g_strdup_printf("udpsrc port=%d multicast-group=%s multicast-iface=lo socket-timestamp=1 ! "
" application/x-rtp,media=(string)video,clock-rate=(int)90000,encoding-name=(string)%s,payload=(int)96 ! "
" %s. ",
config_data.webrtc.udpsink.port, config_data.webrtc.udpsink.addr, upenc, webrtc_name);
g_free(upenc);

g_free(upenc);
if (audio_source != NULL) {
gchar *audio_src = udpsrc_audio_cmdline(webrtc_name);
cmdline = g_strdup_printf("webrtcbin name=%s stun-server=stun://%s %s %s ", webrtc_name, config_data.webrtc.stun, audio_src, video_src);
g_print("webrtc cmdline: %s \n", cmdline);
// g_print("webrtc cmdline: %s \n", cmdline);
g_free(audio_src);
g_free(video_src);
} else {
Expand All @@ -2128,7 +2123,7 @@ void start_udpsrc_webrtcbin(WebrtcItem *item) {
cmdline = g_strdup_printf("webrtcbin name=%s stun-server=stun://%s %s", webrtc_name, config_data.webrtc.stun, video_src);
// g_free(turn_srv);
}

// g_print("webrtc cmdline: %s \n", cmdline);
item->sendpipe = gst_parse_launch(cmdline, NULL);
gst_element_set_state(item->sendpipe, GST_STATE_READY);

Expand Down Expand Up @@ -3073,6 +3068,7 @@ int edgedect_hlssink() {
static void _initial_device() {
if (is_initial)
return;

_mkdir(config_data.root_dir, 0755);
record_time = config_data.rec_len;

Expand Down Expand Up @@ -3125,10 +3121,33 @@ GThread *start_inotify_thread(void) {

GstElement *create_instance() {
pipeline = gst_pipeline_new("pipeline");

if (!capture_htable)
capture_htable = initial_capture_hashtable();

if (config_data.v4l2src_data.spec_drv) {
_v4l2src_data *data = &config_data.v4l2src_data;

g_print("found specfic capture driver is: %s\n", data->spec_drv);
gchar *cmdformat = g_hash_table_lookup(capture_htable, data->spec_drv);
gchar *cmdline = g_strdup_printf(cmdformat, data->device, data->format, data->width,
data->height, data->framerate,
config_data.webrtc.udpsink.addr,
config_data.webrtc.udpsink.port);
GstElement *cmdlinebin = gst_parse_launch(cmdline, NULL);
gst_element_set_state(cmdlinebin, GST_STATE_READY);
g_print("run cmdline: %s\n", cmdline);
gst_element_set_state(cmdlinebin, GST_STATE_PLAYING);
g_free(cmdline);

// gst_element_sync_state_with_parent(cmdlinebin);
gst_bin_add(GST_BIN(pipeline), cmdlinebin);
return pipeline;
}

if (!is_initial)
_initial_device();


// start_av_fakesink();
if (config_data.splitfile_sink.enable)
splitfile_sink();
Expand Down Expand Up @@ -3156,8 +3175,8 @@ GstElement *create_instance() {
start_av_appsink();
}

if (config_data.webrtc.enable) {
if (config_data.webrtc.enable)
start_av_udpsink();
}

return pipeline;
}
5 changes: 3 additions & 2 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ static void read_config_json(gchar *fullpath) {

object = json_object_get_object_member(root_obj, "v4l2src");
config_data.v4l2src_data.device = g_strdup(json_object_get_string_member(object, "device"));
config_data.v4l2src_data.devtype = g_strdup(json_object_get_string_member(object, "devtype"));
config_data.v4l2src_data.format = g_strdup(json_object_get_string_member(object, "format"));
config_data.v4l2src_data.type = g_strdup(json_object_get_string_member(object, "type"));

Expand Down Expand Up @@ -390,8 +391,8 @@ int main(int argc, char *argv[]) {
exit(1);
}

if (!find_video_device_fmt(&config_data.v4l2src_data,TRUE) &&
!get_capture_device(&config_data.v4l2src_data)) {
if (!find_video_device_fmt(&config_data.v4l2src_data, TRUE)
&& !get_capture_device(&config_data.v4l2src_data)) {
g_error("No video capture device found!!!\n");
exit(1);
}
Expand Down
Loading

0 comments on commit 0a11d45

Please sign in to comment.