1 /* 2 * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved. 3 * 4 * Use of this source code is governed by a BSD-style license 5 * that can be found in the LICENSE file in the root of the source 6 * tree. An additional intellectual property rights grant can be found 7 * in the file PATENTS. All contributing project authors may 8 * be found in the AUTHORS file in the root of the source tree. 9 */ 10 11 #ifndef WEBRTC_MODULES_RTP_RTCP_TEST_BWESTANDALONE_MATLABPLOT_H_ 12 #define WEBRTC_MODULES_RTP_RTCP_TEST_BWESTANDALONE_MATLABPLOT_H_ 13 14 #include <list> 15 #include <string> 16 #include <vector> 17 18 #include "webrtc/typedefs.h" 19 20 namespace webrtc { 21 class CriticalSectionWrapper; 22 class EventWrapper; 23 class ThreadWrapper; 24 } 25 26 //#define PLOT_TESTING 27 28 #ifdef MATLAB 29 30 typedef struct engine Engine; 31 typedef struct mxArray_tag mxArray; 32 33 class MatlabLine 34 { 35 friend class MatlabPlot; 36 37 public: 38 MatlabLine(int maxLen = -1, const char *plotAttrib = NULL, const char *name = NULL); 39 ~MatlabLine(); 40 virtual void Append(double x, double y); 41 virtual void Append(double y); 42 void SetMaxLen(int maxLen); 43 void SetAttribute(char *plotAttrib); 44 void SetName(char *name); 45 void Reset(); 46 virtual void PurgeOldData() {}; 47 48 void UpdateTrendLine(MatlabLine * sourceData, double slope, double offset); 49 50 double xMin(); 51 double xMax(); 52 double yMin(); 53 double yMax(); 54 55 protected: 56 void GetPlotData(mxArray** xData, mxArray** yData); 57 std::string GetXName(); 58 std::string GetYName(); 59 std::string GetPlotString(); 60 std::string GetRefreshString(); 61 std::string GetLegendString(); 62 bool hasLegend(); 63 std::list<double> _xData; 64 std::list<double> _yData; 65 mxArray* _xArray; 66 mxArray* _yArray; 67 int _maxLen; 68 std::string _plotAttribute; 69 std::string _name; 70 }; 71 72 73 class MatlabTimeLine : public MatlabLine 74 { 75 public: 76 MatlabTimeLine(int horizonSeconds = -1, const char *plotAttrib = NULL, const char *name = NULL, 77 int64_t refTimeMs = -1); 78 ~MatlabTimeLine() {}; 79 void Append(double y); 80 void PurgeOldData(); 81 int64_t GetRefTime(); 82 83 private: 84 int64_t _refTimeMs; 85 int _timeHorizon; 86 }; 87 88 89 class MatlabPlot 90 { 91 friend class MatlabEngine; 92 93 public: 94 MatlabPlot(); 95 ~MatlabPlot(); 96 97 int AddLine(int maxLen = -1, const char *plotAttrib = NULL, const char *name = NULL); 98 int AddTimeLine(int maxLen = -1, const char *plotAttrib = NULL, const char *name = NULL, 99 int64_t refTimeMs = -1); 100 int GetLineIx(const char *name); 101 void Append(int lineIndex, double x, double y); 102 void Append(int lineIndex, double y); 103 int Append(const char *name, double x, double y); 104 int Append(const char *name, double y); 105 int Length(char *name); 106 void SetPlotAttribute(char *name, char *plotAttrib); 107 void Plot(); 108 void Reset(); 109 void SmartAxis(bool status = true) { _smartAxis = status; }; 110 void SetFigHandle(int handle); 111 void EnableLegend(bool enable) { _legendEnabled = enable; }; 112 113 bool TimeToPlot(); 114 void Plotting(); 115 void DonePlotting(); 116 void DisablePlot(); 117 118 int MakeTrend(const char *sourceName, const char *trendName, double slope, double offset, const char *plotAttrib = NULL); 119 120 #ifdef PLOT_TESTING 121 int64_t _plotStartTime; 122 int64_t _plotDelay; 123 #endif 124 125 private: 126 void UpdateData(Engine* ep); 127 bool GetPlotCmd(std::ostringstream & cmd, Engine* ep); 128 void GetPlotCmd(std::ostringstream & cmd); // call inside crit sect 129 void GetRefreshCmd(std::ostringstream & cmd); // call inside crit sect 130 void GetLegendCmd(std::ostringstream & cmd); 131 bool DataAvailable(); 132 133 std::vector<MatlabLine *> _line; 134 int _figHandle; 135 bool _smartAxis; 136 double _xlim[2]; 137 double _ylim[2]; 138 webrtc::CriticalSectionWrapper *_critSect; 139 bool _timeToPlot; 140 bool _plotting; 141 bool _enabled; 142 bool _firstPlot; 143 bool _legendEnabled; 144 webrtc::EventWrapper* _donePlottingEvent; 145 }; 146 147 148 class MatlabEngine 149 { 150 public: 151 MatlabEngine(); 152 ~MatlabEngine(); 153 154 MatlabPlot * NewPlot(MatlabPlot *newPlot); 155 void DeletePlot(MatlabPlot *plot); 156 157 private: 158 static bool PlotThread(void *obj); 159 160 std::vector<MatlabPlot *> _plots; 161 webrtc::CriticalSectionWrapper *_critSect; 162 webrtc::EventWrapper *_eventPtr; 163 webrtc::ThreadWrapper* _plotThread; 164 bool _running; 165 int _numPlots; 166 }; 167 168 #endif //MATLAB 169 170 #endif // WEBRTC_MODULES_RTP_RTCP_TEST_BWESTANDALONE_MATLABPLOT_H_ 171