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
| #include <gst/gst.h> #include <gst/app/gstappsrc.h> #include <opencv2/opencv.hpp>
#define RTSP_SERVER_URL "rtsp://127.0.0.1:8554/live" #define CAPTURE_WIDTH 640 #define CAPTURE_HEIGHT 480 #define CAPTURE_FPS 30 #define FRAME_INTERVAL_MS (1000 / CAPTURE_FPS)
int main(int argc, char* argv[]) { gst_init(&argc, &argv); std::cout << "3e" << std::endl; GstElement* pipeline = gst_pipeline_new("rtsp-pipeline"); GstElement* appsrc = gst_element_factory_make("appsrc", "app-source"); GstElement* videoconvert = gst_element_factory_make("videoconvert", "convert"); GstElement* x264enc = gst_element_factory_make("x264enc", "encoder"); GstElement* rtph264pay = gst_element_factory_make("rtph264pay", "payloader"); GstElement* rtspclientsink = gst_element_factory_make("rtspclientsink", "sink");
g_object_set(G_OBJECT(appsrc), "is-live", TRUE, NULL); g_object_set(G_OBJECT(appsrc), "format", GST_FORMAT_TIME, NULL); g_object_set(G_OBJECT(appsrc), "caps", gst_caps_new_simple("video/x-raw", "format", G_TYPE_STRING, "BGR", "width", G_TYPE_INT, CAPTURE_WIDTH, "height", G_TYPE_INT, CAPTURE_HEIGHT, "framerate", GST_TYPE_FRACTION, CAPTURE_FPS, 1, NULL), NULL);
gst_bin_add_many(GST_BIN(pipeline), appsrc, videoconvert, x264enc, rtph264pay, rtspclientsink, NULL);
gst_element_link_many(appsrc, videoconvert, x264enc, rtph264pay, rtspclientsink, NULL);
g_object_set(G_OBJECT(rtspclientsink), "location", RTSP_SERVER_URL, NULL);
gst_element_set_state(pipeline, GST_STATE_PLAYING);
cv::VideoCapture cap(0); if (!cap.isOpened()) { std::cerr << "Error: Unable to open camera." << std::endl; return -1; }
while (true) { cv::Mat frame; cap >> frame; if (frame.empty()) { std::cerr << "Warning: Empty frame." << std::endl; continue; }
std::chrono::system_clock::time_point now = std::chrono::system_clock::now(); auto t = std::chrono::system_clock::to_time_t(now); auto tp = std::chrono::system_clock::from_time_t(t); auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(now - tp).count(); auto in_time_t = std::chrono::system_clock::to_time_t(now); std::cout << std::put_time(std::localtime(&in_time_t), "%Y-%m-%d %X") << "." << ms << std::endl;
GstBuffer* buffer = gst_buffer_new_allocate(NULL, frame.total() * frame.elemSize(), NULL); GstMapInfo map; gst_buffer_map(buffer, &map, GST_MAP_WRITE); memcpy(map.data, frame.data, frame.total() * frame.elemSize()); gst_buffer_unmap(buffer, &map);
GstFlowReturn ret; g_signal_emit_by_name(appsrc, "push-buffer", buffer, &ret); gst_buffer_unref(buffer);
g_usleep(FRAME_INTERVAL_MS * 1000); }
gst_element_set_state(pipeline, GST_STATE_NULL); gst_object_unref(pipeline);
return 0; }
|