Skip to content

Commit

Permalink
* add rtmp demo
Browse files Browse the repository at this point in the history
  • Loading branch information
lxowalle committed May 15, 2024
1 parent 2d6b338 commit 9e4c6e4
Show file tree
Hide file tree
Showing 25 changed files with 2,396 additions and 50 deletions.
88 changes: 53 additions & 35 deletions components/3rd_party/media_server/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,38 @@ set(media_server_unzip_path "${DL_EXTRACTED_PATH}/media_server")
set(src_path "${media_server_unzip_path}/media_server-${media_server_version_str}")

############### Add include ###################
set(media_server_private_include_dir
"${src_path}/librtsp/source/server"
"${src_path}/sdk/include"
"${src_path}/sdk/libhttp/include"
"${src_path}/librtp/include"
"${src_path}/libmpeg/include"
"${src_path}/libflv/include"
"${src_path}/libmkv/include"
"${src_path}/avcodec/avcodec/include"
"${src_path}/avcodec/avbsf/include"
"${src_path}/libmov/include"
"${src_path}/include"
"${src_path}/media")
# list(APPEND ADD_PRIVATE_INCLUDE ${media_server_private_include_dir})

set(media_server_include_dir "${src_path}"
".")

list(APPEND ADD_INCLUDE ${media_server_include_dir}
${media_server_private_include_dir})

set(media_server_private_include_dir "${src_path}/include/avcodec/avbsf/include"
"${src_path}/include/avcodec/avcodec/include"
"${src_path}/include/avcodec/h264/include"
"${src_path}/include/avcodec/h265/include"

"${src_path}/include/sdk/include"
"${src_path}/include/sdk/libaio/include"
"${src_path}/include/sdk/libhttp/include"
"${src_path}/include/sdk/libice/include"

"${src_path}/include/media-server/libdash/include"
"${src_path}/include/media-server/libice/include"
"${src_path}/include/media-server/libflv/include"
"${src_path}/include/media-server/libhls/include"
"${src_path}/include/media-server/libmkv/include"
"${src_path}/include/media-server/libmov/include"
"${src_path}/include/media-server/libmpeg/include"
"${src_path}/include/media-server/librtmp/include"
"${src_path}/include/media-server/librtp/include"
"${src_path}/include/media-server/librtsp/include"
"${src_path}/include/media-server/libsip/include")
set(media_server_include_dir "inc" "${src_path}")
list(APPEND ADD_INCLUDE ${media_server_include_dir} ${media_server_private_include_dir})
set_property(SOURCE ${media_server_include_dir} PROPERTY GENERATED 1)
set_property(SOURCE ${media_server_private_include_dir} PROPERTY GENERATED 1)
###############################################

############ Add source files #################
list(APPEND ADD_SRCS
)
aux_source_directory("src" ADD_SRCS)
# append_srcs_dir(ADD_SRCS ".") # append source file in src dir to var ADD_SRCS
# set_property(SOURCE ${ADD_SRCS} PROPERTY GENERATED 1)

append_srcs_dir(ADD_SRCS "${src_path}"
"${src_path}/media") # append source file in src dir to var ADD_SRCS
# list(REMOVE_ITEM COMPONENT_SRCS "src/test2.c")
# FILE(GLOB_RECURSE EXTRA_SRC "src/*.c")
# FILE(GLOB EXTRA_SRC "src/*.c")
Expand Down Expand Up @@ -63,16 +66,31 @@ append_srcs_dir(ADD_SRCS "${src_path}"
###############################################

############ Add static libs ##################
list(APPEND ADD_STATIC_LIB "${src_path}/libs/librtsp.a"
"${src_path}/libs/libhttp.a"
"${src_path}/libs/libflv.a"
"${src_path}/libs/libmov.a"
"${src_path}/libs/librtp.a"
"${src_path}/libs/libmpeg.a"
"${src_path}/libs/libsdk.a"
"${src_path}/libs/libavcodec.a"
"${src_path}/libs/libavbsf.a")
list(APPEND ADD_STATIC_LIB

"${src_path}/lib/libdash.a"
"${src_path}/lib/libhls.a"
"${src_path}/lib/libmkv.a"
"${src_path}/lib/libmov.a"
"${src_path}/lib/libmpeg.a"
"${src_path}/lib/librtmp.a"
"${src_path}/lib/libflv.a"
"${src_path}/lib/librtp.a"
"${src_path}/lib/librtsp.a"
"${src_path}/lib/libsip.a"

"${src_path}/lib/libavbsf.a"
"${src_path}/lib/libavcodec.a"
"${src_path}/lib/libh264.a"
"${src_path}/lib/libh265.a"

"${src_path}/lib/libhttp.a"
"${src_path}/lib/libice.a"
"${src_path}/lib/libsdk.a"


)
set_property(SOURCE ${ADD_STATIC_LIB} PROPERTY GENERATED 1)
###############################################

############ Add dynamic libs ##################
Expand All @@ -87,7 +105,7 @@ list(APPEND ADD_STATIC_LIB "${src_path}/libs/librtsp.a"

#### Add compile option for this component
#### and components denpend on this component
list(APPEND ADD_DEFINITIONS -D__ERROR__=00*10000000+__LINE__*1000)
list(APPEND ADD_DEFINITIONS -D__ERROR__=00*10000000+__LINE__*1000 -DOS_LINUX)

###############################################

Expand Down
2 changes: 1 addition & 1 deletion components/3rd_party/media_server/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ config MEDIA_SERVER_VERSION_MINOR

config MEDIA_SERVER_VERSION_PATCH
int "media_server package patch version"
default 1
default 2
help
media_server package patch version
endmenu
3 changes: 3 additions & 0 deletions components/3rd_party/media_server/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ def add_file_downloads(confs : dict) -> list:
elif version == "1.0.1":
url = "https://files.catbox.moe/acqhv1.zip"
sha256sum = "ce06dc3d03b6036165956e60afd5eec5bfd37e7746e4b427f2099732478ecc22"
elif version == "1.0.2":
url = "https://github.com/sipeed/MaixCDK/releases/download/v0.0.0/media_server-1.0.2.zip"
sha256sum = "b9872dbe52fae4d4b60a0db827533877b20186e9457525345407fdad8e187704"
else:
raise Exception(f"version {version} not support")
filename = f"media_server-{version}.zip"
Expand Down
76 changes: 76 additions & 0 deletions components/3rd_party/media_server/inc/h265-camera-source.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#ifndef _h265_camera_source_h_
#define _h265_camera_source_h_

#include "media-source.h"
#include "sys/process.h"
#include "time64.h"
#include "rtp.h"
#include <string>
#include "pthread.h"
#include <list>

class H265CameraSource : public IMediaSource
{
public:
H265CameraSource(const char *file);
virtual ~H265CameraSource();

public:
virtual int Play();
virtual int Pause();
virtual int Seek(int64_t pos);
virtual int SetSpeed(double speed);
virtual int GetDuration(int64_t& duration) const;
virtual int GetSDPMedia(std::string& sdp) const;
virtual int GetRTPInfo(const char* uri, char *rtpinfo, size_t bytes) const;
virtual int SetTransport(const char* track, std::shared_ptr<IRTPTransport> transport);
int SetNextFrame(const uint8_t* ptr, size_t bytes);

private:
int GetNextFrame(int64_t &dts, const uint8_t* &ptr, size_t &bytes);
int FreeNextFrame();

static void OnRTCPEvent(void* param, const struct rtcp_msg_t* msg);
void OnRTCPEvent(const struct rtcp_msg_t* msg);
int SendRTCP();

static void* RTPAlloc(void* param, int bytes);
static void RTPFree(void* param, void *packet);
static int RTPPacket(void* param, const void *packet, int bytes, uint32_t timestamp, int flags);

private:
void* m_rtp;
uint32_t m_timestamp;
time64_t m_rtp_clock;
time64_t m_rtcp_clock;
// H265CameraReader m_reader;
std::shared_ptr<IRTPTransport> m_transport;

struct vframe_t
{
const uint8_t* nalu;
int64_t time;
long bytes;
bool idr; // IDR frame

bool operator < (const struct vframe_t &v) const
{
return time < v.time;
}
};
typedef std::list<vframe_t> lframes_t;
lframes_t m_videos_list;
lframes_t::iterator m_video;
pthread_mutex_t m_lock;

std::list<std::pair<const uint8_t*, size_t> > m_sps;

int m_status;
int64_t m_pos;
double m_speed;

void *m_rtppacker;
unsigned char m_packet[MAX_UDP_PACKET+14];
};

#endif /* !_h265_camera_source_h_ */
30 changes: 30 additions & 0 deletions components/3rd_party/media_server/inc/media-source.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#ifndef _media_source_h_
#define _media_source_h_

#include <string>
#include <memory>

#ifndef MAX_UDP_PACKET
#define MAX_UDP_PACKET (1450-16)
#endif

struct IRTPTransport
{
virtual int Send(bool rtcp, const void* data, size_t bytes) = 0;
};

struct IMediaSource
{
virtual ~IMediaSource(){}

virtual int Play() = 0;
virtual int Pause() = 0;
virtual int Seek(int64_t pos) = 0;
virtual int SetSpeed(double speed) = 0;
virtual int GetDuration(int64_t& duration) const = 0;
virtual int GetSDPMedia(std::string& sdp) const = 0;
virtual int GetRTPInfo(const char* uri, char *rtpinfo, size_t bytes) const = 0;
virtual int SetTransport(const char* track, std::shared_ptr<IRTPTransport> transport) = 0;
};

#endif /* !_media_source_h_ */
36 changes: 36 additions & 0 deletions components/3rd_party/media_server/inc/rtp-tcp-transport.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#ifndef _rtp_tcp_transport_h_
#define _rtp_tcp_transport_h_

#include "media-source.h"
#include "rtsp-server-internal.h"

class RTPTcpTransport : public IRTPTransport
{
public:
RTPTcpTransport(rtsp_server_t* rtsp, uint8_t rtp, uint8_t rtcp): m_rtp(rtp), m_rtcp(rtcp), m_rtsp(rtsp) {}
virtual ~RTPTcpTransport() {}

public:
virtual int Send(bool rtcp, const void* data, size_t bytes)
{
assert(bytes < (1 << 16));
if (bytes >= (1 << 16))
return -E2BIG;

m_packet[0] = '$';
m_packet[1] = rtcp ? m_rtcp : m_rtp;
m_packet[2] = (bytes >> 8) & 0xFF;
m_packet[3] = bytes & 0xff;
memcpy(m_packet + 4, data, bytes);
int r = rtsp_server_send_interleaved_data(m_rtsp, m_packet, bytes + 4);
return 0 == r ? bytes : r;
}

private:
uint8_t m_rtp;
uint8_t m_rtcp;
rtsp_server_t* m_rtsp;
uint8_t m_packet[4 + (1 << 16)];
};

#endif /* !_rtp_tcp_transport_h_ */
26 changes: 26 additions & 0 deletions components/3rd_party/media_server/inc/rtp-udp-transport.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#ifndef _rtp_udp_transport_h_
#define _rtp_udp_transport_h_

#include "sys/sock.h"
#include "media-source.h"

class RTPUdpTransport : public IRTPTransport
{
public:
RTPUdpTransport();
virtual ~RTPUdpTransport();

public:
virtual int Send(bool rtcp, const void* data, size_t bytes);

public:
int Init(const char* ip, unsigned short port[2]);
int Init(socket_t socket[2], const char* peer, unsigned short port[2]);

private:
socket_t m_socket[2];
socklen_t m_addrlen[2];
struct sockaddr_storage m_addr[2];
};

#endif /* !_rtp_udp_transport_h_ */
49 changes: 49 additions & 0 deletions components/3rd_party/media_server/inc/rtsp-server-internal.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#ifndef _rtsp_server_internal_h_
#define _rtsp_server_internal_h_

#include "rtsp-server.h"
#include "http-parser.h"
#include "rtsp-header-session.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

#if defined(OS_WINDOWS)
#define strcasecmp _stricmp
#endif

#define MAX_UDP_PACKAGE 1024

#define USER_AGENT "ireader/media-server"

struct rtsp_server_t
{
struct rtsp_handler_t handler;
void *param, *sendparam;

http_parser_t* parser;
struct rtsp_header_session_t session;

unsigned int cseq;
char reply[MAX_UDP_PACKAGE];

char ip[65]; // IPv4/IPv6
unsigned short port;
};

int rtsp_server_handle(struct rtsp_server_t *rtsp);
int rtsp_server_options(struct rtsp_server_t *rtsp, const char* uri);
int rtsp_server_announce(struct rtsp_server_t *rtsp, const char* uri);
int rtsp_server_describe(struct rtsp_server_t *rtsp, const char* uri);
int rtsp_server_setup(struct rtsp_server_t *rtsp, const char* uri);
int rtsp_server_play(struct rtsp_server_t *rtsp, const char* uri);
int rtsp_server_pause(struct rtsp_server_t *rtsp, const char* uri);
int rtsp_server_teardown(struct rtsp_server_t *rtsp, const char* uri);
int rtsp_server_get_parameter(struct rtsp_server_t *rtsp, const char* uri);
int rtsp_server_set_parameter(struct rtsp_server_t *rtsp, const char* uri);
int rtsp_server_record(struct rtsp_server_t *rtsp, const char* uri);
int rtsp_server_reply(struct rtsp_server_t *rtsp, int code);
int rtsp_server_reply2(struct rtsp_server_t *rtsp, int code, const char* header, const void* data, int bytes);

#endif /* !_rtsp_server_internal_h_ */
23 changes: 23 additions & 0 deletions components/3rd_party/media_server/inc/rtsp_server.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#ifndef __RTSP_SERVER_H
#define __RTSP_SERVER_H

#ifdef __cplusplus
extern "C" {
#endif

#include "stdlib.h"
#include "stdint.h"

int rtsp_server_init(char *ip, int port);
int rtsp_server_deinit(void);
char *rtsp_get_server_ip(void);
int rtsp_get_server_port(void);
int rtsp_server_start(void);
int rtsp_server_stop(void);
void rtsp_send_h265_data(uint8_t *asddata, size_t data_len);

#ifdef __cplusplus
}
#endif

#endif // __RTSP_SERVER_H
13 changes: 0 additions & 13 deletions components/3rd_party/media_server/readme.md
Original file line number Diff line number Diff line change
@@ -1,14 +1 @@
# readme

对openmv库进行了代码裁剪,保证代码量尽量的小,以方便在出现问题时可以快速定位到是否是移植带来的问题。

其中主要修改的部分:
1. alloc文件用于内存管理,由于openmv的内存管理与板级强相关,所以这里的实现与源码有差异
2. common文件用于存放一些通用函数。这里删除掉了大部分用不到的文件,只保留了一些方便imlib编译通过的文件。删除大部分文件的优势是可以更快的理解imlib依赖的代码,缺点是如果源码库中common文件有更新,需要手动同步到这里
3. imlib文件用于存放imlib的核心代码
4. ports文件保存了配置文件、与板级相关的代码、与第三方库相关的代码


# TODO:

1. 未实现文件操作功能。在ports/linux中添加了oofatfs只是为了编译通过,实际上并没有实现文件操作功能
Loading

0 comments on commit 9e4c6e4

Please sign in to comment.