• 使用gstreamer,rtsp拉流,保存图像, jeston,使用硬件加速nvdec/nvenc


    jeston,拉流,使用硬件加速nvdec/nvenc

    #include 
     
    /* Structure to contain all our information, so we can pass it to callbacks */
    typedef struct _CustomData {
        GstElement *pipeline;
        GstElement *source;
        GstElement *depay;
        GstElement *parse;
        GstElement *avdec;
        GstElement *convert;
        GstElement *encoder;
        GstElement *sink;
        // GstElement *pipeline, *source, *depay, *parse, *avdec, *convert, *encoder, *sink;
    } CustomData;
     
    /* Handler for the pad-added signal */
    static void pad_added_handler (GstElement *src, GstPad *pad, CustomData *data);
     
    int main(int argc, char *argv[]) {
        CustomData data;
        GstBus *bus;
        GstMessage *msg;
        GstStateChangeReturn ret;
        gboolean terminate = FALSE;
     
        /* Initialize GStreamer */
        gst_init (&argc, &argv);
    
    
        /* Create the elements 
          videotestsrc 是一个源元素(它生成数据),用于创建测试视频模式。
          此元素对于调试目的(和教程)非常有用,通常在实际应用程序中找不到。
          autovideosink是一个接收器元素(它消耗数据),在窗口上显示它接收的图像。
        */
        data.source = gst_element_factory_make ("rtspsrc", "source");
        g_object_set (G_OBJECT (data.source), "latency", 2000, NULL);
        data.depay = gst_element_factory_make ("rtph264depay", "depay");
        data.parse = gst_element_factory_make ("h264parse", "parse");
        data.avdec = gst_element_factory_make ("nvv4l2decoder", "decoder");
        data.convert = gst_element_factory_make ("nvvidconv", "convert");
        // nv4l2h264enc、nv4l2vp9enc和nvjpegenc
        // data.encoder = gst_element_factory_make("nvv4l2h264enc", "encoder");
        // data.encoder = gst_element_factory_make("jpegenc", "encoder");
        // data.encoder = gst_element_factory_make("omxh264enc", "encoder");
        // data.encoder = gst_element_factory_make("nvjpegenc", "encoder");
    
        data.encoder = gst_element_factory_make("nvv4l2h264enc", "encoder");
    
        data.sink = gst_element_factory_make ("multifilesink", "sink");
     
     
        /* Modify the source's properties 
           属性用g_object_get()读取,用g_object_set()写入。
           更改了videotestsrc的“pattern”属性,该属性控制元素输出的测试视频的类型。
        */
        g_object_set (data.source, "location", "rtsp://deepcong:666@192.168.100.52:5/0", NULL);
        g_object_set(G_OBJECT(data.sink), "location", "test666.jpg", NULL);
    
    
        /* Connect to the pad-added signal */
        g_signal_connect (data.source, "pad-added", G_CALLBACK (pad_added_handler), &data);
     
     
        /* Create the empty pipeline 
           GStreamer中的所有元素通常必须包含在管道中才能使用,负责一些时钟和消息传递功能。
        */
        data.pipeline = gst_pipeline_new ("test-pipeline");
    
    
        //&& 如果两个操作数都非零,则条件为真;
        // || 如果两个操作数中有任意一个非零,则条件为真。
        if (!data.pipeline || !data.source || !data.convert || !data.sink) {
            g_printerr ("Not all elements could be created.\n");
            return -1;
        }
     
        /* Build the pipeline. Note that we are NOT linking the source at this
        * point. We will do it later. */
        gst_bin_add_many (GST_BIN (data.pipeline), data.source, data.depay, data.parse, data.avdec, data.convert, data.encoder,data.sink, NULL);
        if (!gst_element_link_many (data.depay, data.parse, data.avdec, data.convert,data.encoder, data.sink, NULL)) {
            g_printerr ("Elements could not be linked.\n");
            gst_object_unref (data.pipeline);
            return -1;
        }
     
        /* Start playing */
        ret = gst_element_set_state (data.pipeline, GST_STATE_PLAYING);
        if (ret == GST_STATE_CHANGE_FAILURE) {
            g_printerr ("Unable to set the pipeline to the playing state.\n");
            gst_object_unref (data.pipeline);
            return -1;
        }
     
        /* Listen to the bus */
        bus = gst_element_get_bus (data.pipeline);
        do {
            msg = gst_bus_timed_pop_filtered (bus, GST_CLOCK_TIME_NONE,
                GST_MESSAGE_STATE_CHANGED | GST_MESSAGE_ERROR | GST_MESSAGE_EOS);
     
            /* Parse message */
            if (msg != NULL) {
                GError *err;
                gchar *debug_info;
     
                switch (GST_MESSAGE_TYPE (msg)) {
                    case GST_MESSAGE_ERROR:
                        gst_message_parse_error (msg, &err, &debug_info);
                        g_printerr ("Error received from element %s: %s\n", GST_OBJECT_NAME (msg->src), err->message);
                        g_printerr ("Debugging information: %s\n", debug_info ? debug_info : "none");
                        g_clear_error (&err);
                        g_free (debug_info);
                        terminate = TRUE;
                        break;
                    case GST_MESSAGE_EOS:
                        g_print ("End-Of-Stream reached.\n");
                        terminate = TRUE;
                        break;
                    case GST_MESSAGE_STATE_CHANGED:
                        /* We are only interested in state-changed messages from the pipeline */
                        if (GST_MESSAGE_SRC (msg) == GST_OBJECT (data.pipeline)) {
                            GstState old_state, new_state, pending_state;
                            gst_message_parse_state_changed (msg, &old_state, &new_state, &pending_state);
                            g_print ("Pipeline state changed from %s to %s:\n",
                                gst_element_state_get_name (old_state), gst_element_state_get_name (new_state));
                        }
                        break;
                    default:
                        /* We should not reach here */
                        g_printerr ("Unexpected message received.\n");
                        break;
                }
                gst_message_unref (msg);
            }
        } while (!terminate);
     
        /* Free resources */
        gst_object_unref (bus);
        gst_element_set_state (data.pipeline, GST_STATE_NULL);
        gst_object_unref (data.pipeline);
        return 0;
    }
     
    /* This function will be called by the pad-added signal */
    static void pad_added_handler (GstElement *src, GstPad *new_pad, CustomData *data) {
        GstPad *sink_pad = gst_element_get_static_pad (data->depay, "sink");
        GstPadLinkReturn ret;
        GstCaps *new_pad_caps = NULL;
        GstStructure *new_pad_struct = NULL;
        const gchar *new_pad_type = NULL;
     
        g_print ("Received new pad '%s' from '%s':\n", GST_PAD_NAME (new_pad), GST_ELEMENT_NAME (src));
     
        /* If our converter is already linked, we have nothing to do here */
        if (gst_pad_is_linked (sink_pad)) {
            g_print ("We are already linked. Ignoring.\n");
            goto exit;
        }
     
        /* Check the new pad's type */
        new_pad_caps = gst_pad_get_current_caps (new_pad);
        new_pad_struct = gst_caps_get_structure (new_pad_caps, 0);
        new_pad_type = gst_structure_get_name (new_pad_struct);
     
        /* Attempt the link */
        ret = gst_pad_link (new_pad, sink_pad);
        if (GST_PAD_LINK_FAILED (ret)) {
            g_print ("Type is '%s' but link failed.\n", new_pad_type);
        } else {
            g_print ("Link succeeded (type '%s').\n", new_pad_type);
        }
     
    exit:
        /* Unreference the new pad's caps, if we got them */
        if (new_pad_caps != NULL)
            gst_caps_unref (new_pad_caps);
     
        /* Unreference the sink pad */
        gst_object_unref (sink_pad);
    }
    
    • 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
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179

    编译:

    gcc demo.c -o demo `pkg-config --cflags --libs gstreamer-1.0`
    
    • 1

    运行:

    ./demo
    
    • 1
  • 相关阅读:
    LED灯实验
    使用Python的requests库采集充电桩LBS位置经纬度信息
    【Linux系统】第二篇、权限管理篇
    vscode windows mingw配置
    【算法题】LeetCode691、贴纸拼词(剪枝+记忆化搜索)
    Day17:C++ WITH Easyx
    常见项目管理中npm包操作总结
    Nacos安装指南(Windows环境)
    Python 爬虫项目实战:爬取某云热歌榜歌曲
    完成了一个小项目:修改了一个用PHP+MySQL写的建网站用的CMS原程序
  • 原文地址:https://blog.csdn.net/DeepCBW/article/details/126609571