aboutsummaryrefslogtreecommitdiff
path: root/webrtc/modules/rtp_rtcp/test/BWEStandAlone/MatlabPlot.h
blob: 3ed89f8f9102fbc0f6907aa888dd5e3ba9ea3b1b (plain)
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
/*
 *  Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

#ifndef WEBRTC_MODULES_RTP_RTCP_TEST_BWESTANDALONE_MATLABPLOT_H_
#define WEBRTC_MODULES_RTP_RTCP_TEST_BWESTANDALONE_MATLABPLOT_H_

#include <list>
#include <string>
#include <vector>

#include "webrtc/typedefs.h"
#include "webrtc/system_wrappers/include/thread_wrapper.h"

namespace webrtc {
class CriticalSectionWrapper;
class EventWrapper;
}

//#define PLOT_TESTING

#ifdef MATLAB

typedef struct engine Engine;
typedef struct mxArray_tag mxArray;

class MatlabLine
{
    friend class MatlabPlot;

public:
    MatlabLine(int maxLen = -1, const char *plotAttrib = NULL, const char *name = NULL);
    ~MatlabLine();
    virtual void Append(double x, double y);
    virtual void Append(double y);
    void SetMaxLen(int maxLen);
    void SetAttribute(char *plotAttrib);
    void SetName(char *name);
    void Reset();
    virtual void PurgeOldData() {};

    void UpdateTrendLine(MatlabLine * sourceData, double slope, double offset);

    double xMin();
    double xMax();
    double yMin();
    double yMax();

protected:
    void GetPlotData(mxArray** xData, mxArray** yData);
    std::string GetXName();
    std::string GetYName();
    std::string GetPlotString();
    std::string GetRefreshString();
    std::string GetLegendString();
    bool hasLegend();
    std::list<double> _xData;
    std::list<double> _yData;
    mxArray* _xArray;
    mxArray* _yArray;
    int _maxLen;
    std::string _plotAttribute;
    std::string _name;
};


class MatlabTimeLine : public MatlabLine
{
public:
    MatlabTimeLine(int horizonSeconds = -1, const char *plotAttrib = NULL, const char *name = NULL,
        int64_t refTimeMs = -1);
    ~MatlabTimeLine() {};
    void Append(double y);
    void PurgeOldData();
    int64_t GetRefTime();

private:
    int64_t _refTimeMs;
    int _timeHorizon;
};


class MatlabPlot
{
    friend class MatlabEngine;

public:
    MatlabPlot();
    ~MatlabPlot();

    int AddLine(int maxLen = -1, const char *plotAttrib = NULL, const char *name = NULL);
    int AddTimeLine(int maxLen = -1, const char *plotAttrib = NULL, const char *name = NULL,
        int64_t refTimeMs = -1);
    int GetLineIx(const char *name);
    void Append(int lineIndex, double x, double y);
    void Append(int lineIndex, double y);
    int Append(const char *name, double x, double y);
    int Append(const char *name, double y);
    int Length(char *name);
    void SetPlotAttribute(char *name, char *plotAttrib);
    void Plot();
    void Reset();
    void SmartAxis(bool status = true) { _smartAxis = status; };
    void SetFigHandle(int handle);
    void EnableLegend(bool enable) { _legendEnabled = enable; };

    bool TimeToPlot();
    void Plotting();
    void DonePlotting();
    void DisablePlot();

    int MakeTrend(const char *sourceName, const char *trendName, double slope, double offset, const char *plotAttrib = NULL);

#ifdef PLOT_TESTING
    int64_t _plotStartTime;
    int64_t _plotDelay;
#endif

private:
    void UpdateData(Engine* ep);
    bool GetPlotCmd(std::ostringstream & cmd, Engine* ep);
    void GetPlotCmd(std::ostringstream & cmd); // call inside crit sect
    void GetRefreshCmd(std::ostringstream & cmd); // call inside crit sect
    void GetLegendCmd(std::ostringstream & cmd);
    bool DataAvailable();

    std::vector<MatlabLine *> _line;
    int _figHandle;
    bool _smartAxis;
    double _xlim[2];
    double _ylim[2];
    webrtc::CriticalSectionWrapper *_critSect;
    bool _timeToPlot;
    bool _plotting;
    bool _enabled;
    bool _firstPlot;
    bool _legendEnabled;
    webrtc::EventWrapper* _donePlottingEvent;
};


class MatlabEngine
{
public:
    MatlabEngine();
    ~MatlabEngine();

    MatlabPlot * NewPlot(MatlabPlot *newPlot);
    void DeletePlot(MatlabPlot *plot);

private:
    static bool PlotThread(void *obj);

    std::vector<MatlabPlot *> _plots;
    webrtc::CriticalSectionWrapper *_critSect;
    webrtc::EventWrapper *_eventPtr;
    rtc::scoped_ptr<webrtc::ThreadWrapper> _plotThread;
    bool _running;
    int _numPlots;
};

#endif //MATLAB

#endif // WEBRTC_MODULES_RTP_RTCP_TEST_BWESTANDALONE_MATLABPLOT_H_