Autonomy Software C++ 24.5.1
Welcome to the Autonomy Software repository of the Mars Rover Design Team (MRDT) at Missouri University of Science and Technology (Missouri S&T)! API reference contains the source code and other resources for the development of the autonomy software for our Mars rover. The Autonomy Software project aims to compete in the University Rover Challenge (URC) by demonstrating advanced autonomous capabilities and robust navigation algorithms.
Loading...
Searching...
No Matches
WebRTC.h
1
11#ifndef WEBRTC_H
12#define WEBRTC_H
13
15#include <nlohmann/json.hpp>
16#include <opencv2/opencv.hpp>
17#include <rtc/rtc.hpp>
18#include <shared_mutex>
19
20extern "C"
21{
22#include <libavcodec/avcodec.h>
23#include <libavformat/avformat.h>
24#include <libavutil/avutil.h>
25#include <libavutil/error.h>
26#include <libavutil/frame.h>
27#include <libavutil/imgutils.h>
28#include <libavutil/mem.h>
29#include <libavutil/opt.h>
30#include <libswscale/swscale.h>
31}
32
34
35
43class WebRTC
44{
45 public:
47 // Declare public methods and member variables.
49 WebRTC(const std::string& szSignallingServerURL, const std::string& szStreamerID);
50 ~WebRTC();
51
52 // Setter for the frame received callback.
53 void SetOnFrameReceivedCallback(std::function<void(cv::Mat&)> fnOnFrameReceivedCallback, const AVPixelFormat eOutputPixelFormat = AV_PIX_FMT_BGR24);
54 bool GetIsConnected() const;
55
56 private:
58 // Declare private methods.
60 bool ConnectToSignallingServer(const std::string& szSignallingServerURL);
62 bool DecodeH264BytesToCVMat(const std::vector<uint8_t>& vH264EncodedBytes, cv::Mat& cvDecodedFrame, const AVPixelFormat eOutputPixelFormat);
63 bool RequestKeyFrame();
64 bool SendCommandToStreamer(const std::string& szCommand);
65
67 // Declare private member variables.
69
70 // Normal member variables.
71 std::string m_szSignallingServerURL;
72 std::string m_szStreamerID;
73
74 // WebRTC connection to RoveSoSimulator Pixel Streamer.
75 std::shared_ptr<rtc::WebSocket> m_pWebSocket;
76 std::shared_ptr<rtc::PeerConnection> m_pPeerConnection;
77 std::shared_ptr<rtc::DataChannel> m_pDataChannel;
78 std::shared_ptr<rtc::Track> m_pVideoTrack1;
79 std::shared_ptr<rtc::H264RtpDepacketizer> m_pTrack1H264DepacketizationHandler;
80 std::shared_ptr<rtc::RtcpReceivingSession> m_pTrack1RtcpReceivingSession;
81 std::chrono::system_clock::time_point m_tmLastKeyFrameRequestTime;
82
83 // AV codec context for decoding H264.
84 AVCodecContext* m_pAVCodecContext;
85 AVFrame* m_pFrame;
86 AVPacket* m_pPacket;
87 SwsContext* m_pSWSContext;
88 AVPixelFormat m_eOutputPixelFormat;
89 std::shared_mutex m_muDecoderMutex;
90
91 // OpenCV Mat for storing the frame.
92 cv::Mat m_cvFrame;
93
94 // Callback function for when a new frame is received.
95 std::function<void(cv::Mat&)> m_fnOnFrameReceivedCallback;
96};
97#endif
This class is used to establish a connection with the RoveSoSimulator and retrieve video streams from...
Definition WebRTC.h:44
bool RequestKeyFrame()
Requests a key frame from the given video track. This is useful for when the video track is out of sy...
Definition WebRTC.cpp:772
~WebRTC()
Destroy the Web RTC::WebRTC object.
Definition WebRTC.cpp:61
bool InitializeH264Decoder()
Initialize the H264 decoder. Creates the AVCodecContext, AVFrame, and AVPacket.
Definition WebRTC.cpp:563
bool ConnectToSignallingServer(const std::string &szSignallingServerURL)
Connected to the Unreal Engine 5 hosted Signalling Server for WebRTC negotiation.
Definition WebRTC.cpp:167
void SetOnFrameReceivedCallback(std::function< void(cv::Mat &)> fnOnFrameReceivedCallback, const AVPixelFormat eOutputPixelFormat=AV_PIX_FMT_BGR24)
Set the callback function for when a new frame is received.
Definition WebRTC.cpp:127
bool SendCommandToStreamer(const std::string &szCommand)
This method sends a command to the streamer via the data channel. The command is a JSON string that i...
Definition WebRTC.cpp:805
bool DecodeH264BytesToCVMat(const std::vector< uint8_t > &vH264EncodedBytes, cv::Mat &cvDecodedFrame, const AVPixelFormat eOutputPixelFormat)
Decodes H264 encoded bytes to a cv::Mat using FFmpeg.
Definition WebRTC.cpp:625
bool GetIsConnected() const
Get the connection status of the WebRTC object.
Definition WebRTC.cpp:144