| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- #include "MediaSourceMain.h"
- #include <conio.h>
- #define DELETE_OBJECT(obj) if (obj) { delete obj; obj = NULL; }
- #define mygetch getch
- AppClass::AppClass(std::string src, std::string dst, std::string transport)
- {
- m_sSrc = src;
- m_sDst = dst;
- m_sTransport = transport;
- }
- void AppClass::Action()
- {
- CMediaSourceRtsp pusher;
- pusher.m_Input_rtsp = m_sSrc;
- pusher.m_Output_rtsp = m_sDst;
- if ((m_sTransport == "TCP") || (m_sTransport == "tcp"))
- pusher.m_TcpOrudp = "tcp";
- if ((m_sTransport == "UDP") || (m_sTransport == "udp"))
- pusher.m_TcpOrudp = "udp";
- if (!pusher.OpenInput(pusher.m_Input_rtsp, pusher.m_TcpOrudp)) {
- printf("open input_rtsp failed\n");
- exit(-1);
- }
- if (!pusher.OpenOutput(pusher.m_Output_rtsp)) {
- printf("open output_rtsp failed\n");
- exit(-1);
- }
- int ret = -1;
- while (!IsAborted()) {
- std::shared_ptr<AVPacket> packet = nullptr;
- packet = pusher.ReadPacket();
- if (!packet) {
- OutputDebugStringA("GetAvPacket ERROR\n");
- printf("GetAvPacket ERROR\n");
- pusher.ResetConnect();
- continue;
- }
- if ((packet) && (packet->stream_index == pusher.m_nVideoStream)) {
- ret = pusher.WritePacket(packet);
- if (ret < 0) {
- pusher.CheckOutputNet();
- }
- }
- }
- }
- std::string LoadConfigFile()
- {
- FILE* pf = fopen("rtsp_config.config", "rb+");
- if (!pf) {
- printf("open rtsp_config.config failed\n");
- return "";
- }
- fseek(pf, 0, SEEK_END);
- long lSize = ftell(pf);
- char* text = new char[lSize];
- rewind(pf);
- fread(text, sizeof(char), lSize, pf);
- std::string filedata = std::string(text, lSize);
- delete[] text;
- fclose(pf);
- return filedata;
- }
- int main(int argc, char** argv)
- {
- std::string filedata = LoadConfigFile();
- if (filedata == "") {
- printf("load rtsp_config.config failed\n");
- return -1;
- }
- nlohmann::json j = nlohmann::json::parse(filedata);
- std::vector<AppClass*> pApp;
- for (int i = 0; i < j["config"].size(); i++) {
- std::string src = j["config"][i]["src_rtsp"];
- std::string dst = j["config"][i]["dst_rtsp"];
- std::string transport = j["config"][i]["rtsp_transport"];
- AppClass* p = new AppClass(src,dst,transport);
- p->Start();
- pApp.emplace_back(p);
- }
- char c;
- while (1) {
- c = mygetch();
- if (c != 'q') {
- std::this_thread::sleep_for(std::chrono::milliseconds(100));
- }
- else {
- std::vector<AppClass*>::iterator iter = pApp.begin();
- for (; iter != pApp.end();) {
- DELETE_OBJECT((*iter));
- iter = pApp.erase(iter);
- }
- break;
- }
- }
- return 0;
- }
|