Skip to content

Instantly share code, notes, and snippets.

@robinvanemden
Created April 30, 2022 12:30
Show Gist options
  • Save robinvanemden/baca07fb8e6bc10699da18ce3842116b to your computer and use it in GitHub Desktop.
Save robinvanemden/baca07fb8e6bc10699da18ce3842116b to your computer and use it in GitHub Desktop.
minimal gstreamer rtsp example: retrieve frame, save jpeg
#include <stdio.h>
#include <gst/gst.h>
#include <gst/app/gstappsink.h>
#include <glib.h>
#include <libavcodec/avcodec.h>
#include "stb/stb_image.h"
void minimal_rtsp() {
gst_init (NULL, NULL);
gst_init_static_plugins();
GstElement *pipeline = gst_parse_launch ("rtspsrc location=rtsp://admin:[email protected]/cam/realmonitor?"
"channel=1&subtype=00&authbasic=YWRtaW46a3JlZWxrYXQx !"
" decodebin ! videoconvert ! video/x-raw, format=RGB ! appsink name=sink",NULL);
gst_element_set_state(pipeline, GST_STATE_PLAYING);
GstElement *sink = gst_bin_get_by_name (GST_BIN (pipeline), "sink");
if(!sink){
printf("Error: GstElement sink is NULL\n");
exit(1);
}
GstAppSink *appsink = GST_APP_SINK(sink);
if(!appsink){
printf("Error: GstAppSink appsink is NULL\n");
exit(1);
}
GstSample *sample = gst_app_sink_pull_sample(appsink);
if(!sample){
printf("Error: GstSample sample is NULL\n");
exit(1);
}
GstCaps *caps = gst_sample_get_caps(sample);
GstStructure *capsStruct = gst_caps_get_structure(caps,0);
int gs_width, gs_height;
gst_structure_get_int (capsStruct, "width", &gs_width);
gst_structure_get_int (capsStruct, "height", &gs_height);
printf("Notice: %d %d \n", gs_width, gs_height);
GstBuffer *buffer = gst_sample_get_buffer(sample);
GstMapInfo map;
gst_buffer_map(buffer, &map, GST_MAP_READ);
stbi_write_jpg("jpg_test.jpg", gs_width, gs_height, 3, (char *) map.data, 70);
exit(0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment