File: VideoTestSubscriber.hpp

package info (click to toggle)
fastdds 3.1.2%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 58,132 kB
  • sloc: cpp: 779,516; xml: 15,119; python: 4,356; sh: 190; makefile: 93; ansic: 12
file content (269 lines) | stat: -rw-r--r-- 7,540 bytes parent folder | download | duplicates (2)
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
// Copyright 2016 Proyectos y Sistemas de Mantenimiento SL (eProsima).
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/**
 * @file VideoTestSubscriber.h
 *
 */

#ifndef VIDEOTESTSUBSCRIBER_H_
#define VIDEOTESTSUBSCRIBER_H_

#include <asio.hpp>
#include <condition_variable>

#include "VideoTestTypes.hpp"
#include <gstreamer-1.0/gst/app/gstappsrc.h>
#include <gstreamer-1.0/gst/app/gstappsink.h>
#include <gstreamer-1.0/gst/gst.h>

class TimeStats
{
public:

    TimeStats()
        : received(0)
        , m_minAvg(0)
        , m_maxAvg(0)
        , pDrop50(0)
        , pDrop90(0)
        , pDrop99(0)
        , pDrop9999(0)
        , pDropMean(0)
        , pDropStdev(0)
        , pAvg50(0)
        , pAvg90(0)
        , pAvg99(0)
        , pAvg9999(0)
        , pAvgMean(0)
        , pAvgStdev(0)
    {
    }

    ~TimeStats()
    {
    }

    unsigned int received;
    double m_minDrop, m_maxDrop, m_minAvg, m_maxAvg;
    double pDrop50, pDrop90, pDrop99, pDrop9999, pDropMean, pDropStdev;
    double pAvg50, pAvg90, pAvg99, pAvg9999, pAvgMean, pAvgStdev;

};

class VideoTestSubscriber
{
public:

    VideoTestSubscriber();
    virtual ~VideoTestSubscriber();

    eprosima::fastdds::dds::DomainParticipant* mp_participant;
    eprosima::fastdds::dds::Publisher* mp_commandpub;
    eprosima::fastdds::dds::Subscriber* mp_datasub;
    eprosima::fastdds::dds::Subscriber* mp_commandsub;
    eprosima::fastdds::dds::Topic* mp_video_topic;
    eprosima::fastdds::dds::Topic* mp_command_pub_topic;
    eprosima::fastdds::dds::Topic* mp_command_sub_topic;
    std::mutex mutex_;
    int disc_count_;
    std::condition_variable disc_cond_;
    int comm_count_;
    std::condition_variable comm_cond_;
    int data_count_;
    std::condition_variable data_cond_;
    int m_status;
    int n_received;
    int n_samples;
    void init(
            int nsam,
            bool reliable,
            uint32_t pid,
            bool hostname,
            const eprosima::fastdds::rtps::PropertyPolicy& part_property_policy,
            const eprosima::fastdds::rtps::PropertyPolicy& property_policy,
            bool large_data,
            const std::string& sXMLConfigFile,
            bool export_csv,
            const std::string& export_file,
            int forced_domain,
            int video_width,
            int video_height,
            int frame_rate);

    void run();
    bool test();

    class DataSubListener : public eprosima::fastdds::dds::DataReaderListener
    {
    public:

        DataSubListener(
                VideoTestSubscriber* up)
            : mp_up(up)
        {
        }

        ~DataSubListener()
        {
        }

        void on_subscription_matched(
                eprosima::fastdds::dds::DataReader* /*datareader*/,
                const eprosima::fastdds::dds::SubscriptionMatchedStatus& info) override;
        void on_data_available(
                eprosima::fastdds::dds::DataReader* datareader) override;
        VideoTestSubscriber* mp_up;
    }
    m_datasublistener;

    class CommandPubListener : public eprosima::fastdds::dds::DataWriterListener
    {
    public:

        CommandPubListener(
                VideoTestSubscriber* up)
            : mp_up(up)
        {
        }

        ~CommandPubListener()
        {
        }

        void on_publication_matched(
                eprosima::fastdds::dds::DataWriter* /*datawriter*/,
                const eprosima::fastdds::dds::PublicationMatchedStatus& info) override;
        VideoTestSubscriber* mp_up;
    }
    m_commandpublistener;

    class CommandSubListener : public eprosima::fastdds::dds::DataReaderListener
    {
    public:

        CommandSubListener(
                VideoTestSubscriber* up)
            : mp_up(up)
        {
        }

        ~CommandSubListener()
        {
        }

        void on_subscription_matched(
                eprosima::fastdds::dds::DataReader* /*datareader*/,
                const eprosima::fastdds::dds::SubscriptionMatchedStatus& info) override;
        void on_data_available(
                eprosima::fastdds::dds::DataReader* datareader) override;
        VideoTestSubscriber* mp_up;
    }
    m_commandsublistener;

    std::string m_sXMLConfigFile;
    bool m_bRunning;

    GstElement* pipeline;
    GstElement* appsrc;
    GstElement* sink;
    GstElement* videoconvert;
    guint source_id_;          // To control the GSource
    GMainLoop* gmain_loop_;    // GLib's Main Loop
    guint64 g_servertimestamp, g_clienttimestamp;
    gint64 g_framesDropped;
    bool m_bReliable;
    bool m_bExportCsv;
    std::string m_sExportPrefix;
    int m_forcedDomain;
    int m_videoWidth;
    int m_videoHeight;
    int m_videoFrameRate;

    std::thread thread_;
    std::deque<VideoType> packet_deque_;
    std::mutex stats_mutex_;
    std::mutex deque_mutex_;
    std::condition_variable deque_cond_;
    std::mutex gst_mutex_;

    std::chrono::steady_clock::time_point t_start_, t_end_;
    std::chrono::steady_clock::time_point t_drop_start_, t_drop_end_;
    std::vector<std::chrono::duration<double, std::micro>> samples_;
    std::vector<double> drops_;
    std::vector<double> avgs_;
    std::vector<TimeStats> m_stats;

protected:

    static void start_feed_cb(
            GstElement* source,
            guint size,
            VideoTestSubscriber* sub);
    static void stop_feed_cb(
            GstElement* source,
            VideoTestSubscriber* sub);
    static gboolean push_data_cb(
            VideoTestSubscriber* sub);
    static void fps_stats_cb(
            GstElement* source,
            gdouble fps,
            gdouble droprate,
            gdouble avgfps,
            VideoTestSubscriber* sub);
    void InitGStreamer();
    void stop();
    void analyzeTimes();
    void printStat(
            TimeStats& TS);
    eprosima::fastdds::dds::DataReaderQos datareader_qos_data;
    eprosima::fastdds::dds::DataReaderQos datareader_qos_cmd;
    eprosima::fastdds::dds::DataWriterQos datawriter_qos;
    eprosima::fastdds::dds::DataWriter*   mp_dw;
    eprosima::fastdds::dds::DataReader*   mp_data_dr;
    eprosima::fastdds::dds::DataReader*   mp_commanhd_dr;

    void push_video_packet(
            VideoType& packet)
    {
        std::unique_lock<std::mutex> lock(deque_mutex_);
        packet_deque_.push_back(std::move(packet));
        deque_cond_.notify_one();
    }

    VideoType pop_video_packet()
    {
        std::unique_lock<std::mutex> lock(deque_mutex_);
        deque_cond_.wait(lock, [&]()
                {
                    return !m_bRunning || !packet_deque_.empty();
                });
        VideoType vpacket;
        if (!packet_deque_.empty())
        {
            vpacket = std::move(packet_deque_[0]);
            packet_deque_.pop_front();
        }
        return vpacket;
    }

    static void gst_run(
            VideoTestSubscriber* sub);
    static void message_cb(
            GstBus* bus,
            GstMessage* message,
            gpointer user_data);
};

#endif /* VIDEOTESTSUBSCRIBER_H_ */