Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for MacOS #7

Open
wants to merge 22 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
127 changes: 76 additions & 51 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,59 +1,84 @@
cmake_minimum_required(VERSION 3.5.0)
project(RtspServer)
project(RTSPServer)

include_directories("${CMAKE_CURRENT_SOURCE_DIR}/src/net/" "${CMAKE_CURRENT_SOURCE_DIR}/src/xop/")
# Cmake needs some version information
CMAKE_MINIMUM_REQUIRED(VERSION 3.16)

file(GLOB rtspserver_sources
"${CMAKE_CURRENT_SOURCE_DIR}/src/net/*.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/src/net/*.h"
"${CMAKE_CURRENT_SOURCE_DIR}/src/xop/*.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/src/xop/*.h"
)
set(CMAKE_CXX_STANDARD 14)

SET(ARCHSTR ${ANDROID_ABI})
message(STATUS "Architecture: ${ARCHSTR}")

set(CMAKE_CONFIGURATION_TYPES "Debug;Release" CACHE STRING "Configs" FORCE)
#=======================================
# USER DEFINED VARIABLES

IF(NOT CMAKE_BUILD_TYPE)
MESSAGE(STATUS "CMAKE_BUILD_TYPE was not specified. Building Debug Version.")
SET(CMAKE_BUILD_TYPE Debug)
ENDIF(NOT CMAKE_BUILD_TYPE)

#==========================================================

INCLUDE_DIRECTORIES(
${CMAKE_CURRENT_SOURCE_DIR}/src/
${CMAKE_CURRENT_SOURCE_DIR}/src/3rdpart
)

FILE(GLOB NetSources ${CMAKE_CURRENT_SOURCE_DIR}/src/net/*.cpp)
FILE(GLOB NetHeaders ${CMAKE_CURRENT_SOURCE_DIR}/src/net/*.h)
ADD_LIBRARY(net SHARED ${NetSources} ${NetHeaders})
SET_TARGET_PROPERTIES(net PROPERTIES
DEBUG_POSTFIX d
ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/lib/${ARCHSTR}"
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/lib/${ARCHSTR}"
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/bin/${ARCHSTR}"
)

set(RTSPSERVER_SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/src/net/Acceptor.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/net/BufferReader.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/net/BufferWriter.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/net/EpollTaskScheduler.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/net/EventLoop.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/net/Logger.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/net/MemoryManager.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/net/NetInterface.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/net/Pipe.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/net/SelectTaskScheduler.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/net/SocketUtil.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/net/Socket.h
${CMAKE_CURRENT_SOURCE_DIR}/src/net/TcpConnection.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/net/TcpServer.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/net/Timer.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/net/Timestamp.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/net/TaskScheduler.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/net/TcpSocket.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/xop/AACSource.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/xop/DigestAuthentication.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/xop/G711ASource.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/xop/H264Source.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/xop/MediaSession.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/xop/H264Parser.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/xop/H265Source.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/xop/RtpConnection.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/xop/RtspConnection.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/xop/RtspMessage.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/xop/RtspPusher.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/xop/RtspServer.cpp
FILE(GLOB XopSources ${CMAKE_CURRENT_SOURCE_DIR}/src/xop/*.cpp)
FILE(GLOB XopHeaders ${CMAKE_CURRENT_SOURCE_DIR}/src/xop/*.h)
ADD_LIBRARY(xop SHARED ${XopSources} ${XopHeaders})
ADD_DEPENDENCIES(xop net)
TARGET_LINK_LIBRARIES(xop net)
SET_TARGET_PROPERTIES(xop PROPERTIES
DEBUG_POSTFIX d
ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/lib/${ARCHSTR}"
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/lib/${ARCHSTR}"
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/bin/${ARCHSTR}"
)

add_library(RtspServer STATIC ${rtspserver_sources})
add_library(RtspServer::RtspServer ALIAS RtspServer)
IF(ANDROID)
TARGET_LINK_LIBRARIES(xop log)
TARGET_LINK_LIBRARIES(net log)
ENDIF(ANDROID)

target_include_directories(RtspServer
PUBLIC
"${CMAKE_CURRENT_SOURCE_DIR}/src/"
"${CMAKE_CURRENT_SOURCE_DIR}/src/xop/"
"${CMAKE_CURRENT_SOURCE_DIR}/src/3rdpart/"
)
IF(BUILD_EXECUTABLE)
ADD_EXECUTABLE(rtsp_pusher ${CMAKE_CURRENT_SOURCE_DIR}/example/rtsp_pusher.cpp )
ADD_DEPENDENCIES(rtsp_pusher net xop)
TARGET_LINK_LIBRARIES(rtsp_pusher net xop)
SET_TARGET_PROPERTIES(rtsp_pusher PROPERTIES
DEBUG_POSTFIX d
ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/lib/${ARCHSTR}"
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/lib/${ARCHSTR}"
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/bin/${ARCHSTR}"
)

target_link_libraries(RtspServer
PRIVATE
zm-dependency-interface)
ADD_EXECUTABLE(rtsp_h264_file ${CMAKE_CURRENT_SOURCE_DIR}/example/rtsp_h264_file.cpp )
ADD_DEPENDENCIES(rtsp_h264_file net xop)
TARGET_LINK_LIBRARIES(rtsp_h264_file net xop)
SET_TARGET_PROPERTIES(rtsp_h264_file PROPERTIES
DEBUG_POSTFIX d
ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/lib/${ARCHSTR}"
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/lib/${ARCHSTR}"
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/bin/${ARCHSTR}"
)

ADD_EXECUTABLE(rtsp_server ${CMAKE_CURRENT_SOURCE_DIR}/example/rtsp_server.cpp )
ADD_DEPENDENCIES(rtsp_server net xop)
TARGET_LINK_LIBRARIES(rtsp_server net xop)
SET_TARGET_PROPERTIES(rtsp_server PROPERTIES
DEBUG_POSTFIX d
ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/lib/${ARCHSTR}"
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/lib/${ARCHSTR}"
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/bin/${ARCHSTR}"
)
ENDIF(BUILD_EXECUTABLE)
61 changes: 32 additions & 29 deletions example/rtsp_h264_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class H264File
{ return (m_file != NULL); }

int ReadFrame(char* in_buf, int in_buf_size, bool* end);

private:
FILE *m_file = NULL;
char *m_buf = NULL;
Expand All @@ -32,7 +32,7 @@ class H264File
void SendFrameThread(xop::RtspServer* rtsp_server, xop::MediaSessionId session_id, H264File* h264_file);

int main(int argc, char **argv)
{
{
if(argc != 2) {
printf("Usage: %s test.h264 \n", argv[0]);
return 0;
Expand All @@ -48,7 +48,7 @@ int main(int argc, char **argv)
std::string ip = "127.0.0.1";
std::string port = "554";
std::string rtsp_url = "rtsp://" + ip + ":" + port + "/" + suffix;

std::shared_ptr<xop::EventLoop> event_loop(new xop::EventLoop());
std::shared_ptr<xop::RtspServer> server = xop::RtspServer::Create(event_loop.get());

Expand All @@ -61,21 +61,21 @@ int main(int argc, char **argv)
server->SetAuthConfig("-_-", "admin", "12345");
#endif

xop::MediaSession *session = xop::MediaSession::CreateNew("live");
session->AddSource(xop::channel_0, xop::H264Source::CreateNew());
//session->StartMulticast();
xop::MediaSession *session = xop::MediaSession::CreateNew("live");
session->AddSource(xop::channel_0, xop::H264Source::CreateNew());
//session->StartMulticast();
session->AddNotifyConnectedCallback([] (xop::MediaSessionId sessionId, std::string peer_ip, uint16_t peer_port){
printf("RTSP client connect, ip=%s, port=%hu \n", peer_ip.c_str(), peer_port);
});

session->AddNotifyDisconnectedCallback([](xop::MediaSessionId sessionId, std::string peer_ip, uint16_t peer_port) {
printf("RTSP client disconnect, ip=%s, port=%hu \n", peer_ip.c_str(), peer_port);
});

xop::MediaSessionId session_id = server->AddSession(session);

std::thread t1(SendFrameThread, server.get(), session_id, &h264_file);
t1.detach();
t1.detach();

std::cout << "Play URL: " << rtsp_url << std::endl;

Expand All @@ -88,27 +88,28 @@ int main(int argc, char **argv)
}

void SendFrameThread(xop::RtspServer* rtsp_server, xop::MediaSessionId session_id, H264File* h264_file)
{
{
int buf_size = 2000000;
std::unique_ptr<uint8_t> frame_buf(new uint8_t[buf_size]);

while(1) {
bool end_of_frame = false;
int frame_size = h264_file->ReadFrame((char*)frame_buf.get(), buf_size, &end_of_frame);
if(frame_size > 0) {
xop::AVFrame videoFrame = {0};
videoFrame.type = 0;
videoFrame.size = frame_size;
xop::AVFrame videoFrame;
videoFrame.type = 0;
videoFrame.timestamp = xop::H264Source::GetTimestamp();
videoFrame.buffer.reset(new uint8_t[videoFrame.size]);
memcpy(videoFrame.buffer.get(), frame_buf.get(), videoFrame.size);
//videoFrame.buffer.reset(new uint8_t[videoFrame.size]);
//memcpy(videoFrame.buffer.get(), frame_buf.get(), videoFrame.size);
videoFrame.buffer.resize(frame_size);
memcpy(videoFrame.buffer.data(), frame_buf.get(), frame_size);
rtsp_server->PushFrame(session_id, xop::channel_0, videoFrame);
}
else {
break;
}
xop::Timer::Sleep(40);

xop::Timer::Sleep(40);
};
}

Expand All @@ -125,8 +126,12 @@ H264File::~H264File()

bool H264File::Open(const char *path)
{
#ifdef WIN32
fopen_s(&m_file, path, "rb");
#else
m_file = fopen(path, "rb");
if(m_file == NULL) {
#endif
if(m_file == NULL) {
return false;
}

Expand All @@ -151,11 +156,11 @@ int H264File::ReadFrame(char* in_buf, int in_buf_size, bool* end)

int bytes_read = (int)fread(m_buf, 1, m_buf_size, m_file);
if(bytes_read == 0) {
fseek(m_file, 0, SEEK_SET);
fseek(m_file, 0, SEEK_SET);
m_count = 0;
m_bytes_used = 0;
bytes_read = (int)fread(m_buf, 1, m_buf_size, m_file);
if(bytes_read == 0) {
if(bytes_read == 0) {
this->Close();
return -1;
}
Expand All @@ -175,8 +180,8 @@ int H264File::ReadFrame(char* in_buf, int in_buf_size, bool* end)
else {
continue;
}
if (((m_buf[i+start_code]&0x1F) == 0x5 || (m_buf[i+start_code]&0x1F) == 0x1)

if (((m_buf[i+start_code]&0x1F) == 0x5 || (m_buf[i+start_code]&0x1F) == 0x1)
&& ((m_buf[i+start_code+1]&0x80) == 0x80)) {
is_find_start = true;
i += 4;
Expand All @@ -195,17 +200,17 @@ int H264File::ReadFrame(char* in_buf, int in_buf_size, bool* end)
else {
continue;
}
if (((m_buf[i+start_code]&0x1F) == 0x7) || ((m_buf[i+start_code]&0x1F) == 0x8)
|| ((m_buf[i+start_code]&0x1F) == 0x6)|| (((m_buf[i+start_code]&0x1F) == 0x5

if (((m_buf[i+start_code]&0x1F) == 0x7) || ((m_buf[i+start_code]&0x1F) == 0x8)
|| ((m_buf[i+start_code]&0x1F) == 0x6)|| (((m_buf[i+start_code]&0x1F) == 0x5
|| (m_buf[i+start_code]&0x1F) == 0x1) &&((m_buf[i+start_code+1]&0x80) == 0x80))) {
is_find_end = true;
break;
}
}

bool flag = false;
if(is_find_start && !is_find_end && m_count>0) {
if(is_find_start && !is_find_end && m_count>0) {
flag = is_find_end = true;
i = bytes_read;
*end = true;
Expand All @@ -217,7 +222,7 @@ int H264File::ReadFrame(char* in_buf, int in_buf_size, bool* end)
}

int size = (i<=in_buf_size ? i : in_buf_size);
memcpy(in_buf, m_buf, size);
memcpy(in_buf, m_buf, size);

if(!flag) {
m_count += 1;
Expand All @@ -231,5 +236,3 @@ int H264File::ReadFrame(char* in_buf, int in_buf_size, bool* end)
fseek(m_file, m_bytes_used, SEEK_SET);
return size;
}


Loading