Skip to content

Commit

Permalink
* update rtmp & rtmp demo
Browse files Browse the repository at this point in the history
  • Loading branch information
lxowalle committed May 29, 2024
1 parent f3977b1 commit e527dbd
Show file tree
Hide file tree
Showing 11 changed files with 512 additions and 19 deletions.
6 changes: 6 additions & 0 deletions components/basic/include/maix_sys.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,12 @@ namespace maix::sys
*/
std::vector<std::map<std::string, std::string>> disk_partitions(bool only_disk = true);

/**
* register default signal handle
* @maixpy maix.sys.register_default_signal_handle
*/
void register_default_signal_handle();

/**
* Power off device
* @maixpy maix.sys.poweroff
Expand Down
22 changes: 22 additions & 0 deletions components/basic/port/linux/maix_sys_linux.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include "maix_util.hpp"
#include "maix_app.hpp"
#include "signal.h"

namespace maix::sys
{
static void signal_handle(int signal)
{
const char *signal_msg = NULL;
switch (signal) {
case SIGINT:
maix::app::set_exit_flag(true);
raise(SIGINT);
break;
default: signal_msg = "UNKNOWN"; break;
}
}

void register_default_signal_handle() {
signal(SIGILL, signal_handle);
}
}
22 changes: 22 additions & 0 deletions components/basic/port/maixcam/maix_sys_maixcam.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include "maix_sys.hpp"
#include "maix_app.hpp"
#include "signal.h"

namespace maix::sys
{
static void signal_handle(int signal)
{
const char *signal_msg = NULL;
switch (signal) {
case SIGINT:
maix::app::set_exit_flag(true);
raise(SIGINT);
break;
default: signal_msg = "UNKNOWN"; break;
}
}

void register_default_signal_handle() {
signal(SIGILL, signal_handle);
}
}
4 changes: 4 additions & 0 deletions components/maixcam_lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ if(CONFIG_MAIXCAM_LIB_COMPILE_FROM_SOURCE)
append_srcs_dir(ADD_SRCS "${source_dir}/nn")
list(APPEND ADD_REQUIREMENTS cvi_tpu)

# media_server
append_srcs_dir(ADD_SRCS "${source_dir}/media_server")
list(APPEND ADD_REQUIREMENTS media_server)

# middleware
set(middleware_include_dir .
${middleware_src_path}/v2/component/panel/${CONFIG_SOPHGO_MIDDLEWARE_CHIP}
${middleware_src_path}/v2/include
Expand Down
23 changes: 23 additions & 0 deletions components/maixcam_lib/include/maix_avc2flv.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#ifndef __MAIX_AVC2FLV
#define __MAIX_AVC2FLV

#ifdef __cplusplus
extern "C" {
#endif
#include "stdint.h"

int maix_avc2flv_init(int max_buff_size);
int maix_avc2flv_deinit();
int maix_avc2flv_prepare(uint8_t *data, int data_size);
int maix_avc2flv_iterate(void **nalu, int *size);
int maix_avc2flv(void *nalu, int nalu_size, uint32_t pts, uint32_t dts, uint8_t **flv, int *flv_size);

// need free data after used
int maix_flv_get_tail(uint8_t **data, int *size);
// need free data after used
int maix_flv_get_header(int audio, int video, uint8_t **data, int *size);
#ifdef __cplusplus
}
#endif

#endif // __MAIX_AVC2FLV
101 changes: 94 additions & 7 deletions components/vision/include/maix_rtmp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,29 @@

#include "maix_basic.hpp"
#include "maix_camera.hpp"
#include "maix_video.hpp"
#include <string>
#include <stdexcept>
#include <pthread.h>

/**
* @brief maix.rtmp module
* @maixpy maix.rtmp
*/
namespace maix::rtmp
{
/**
* Video type
* @maixpy maix.rtmp.TagType
*/
enum TagType
{
TAG_NONE,
TAG_VIDEO,
TAG_AUDIO,
TAG_SCRIPT,
};

/**
* Rtmp class
* @maixpy maix.rtmp.Rtmp
Expand All @@ -25,15 +42,20 @@ namespace maix::rtmp
std::string _app;
std::string _stream;
int _port;
int _bitrate;

int _socket;
void *_handler;

camera::Camera *_cam;
camera::Camera *_camera;
thread::Thread *_thread;
thread::Thread *_push_thread;
thread::Thread *_app_thread;
pthread_mutex_t _lock;
bool _start;
std::string _path;
image::Image *_capture_image;
bool _need_capture;
public:
/**
* @brief Construct a new Video object
Expand All @@ -45,29 +67,39 @@ namespace maix::rtmp
* @param port rtmp port, default is 1935.
* @param app rtmp app name
* @param stream rtmp stream name
* @param bitrate rtmp bitrate, default is 1000 * 1000
* @maixpy maix.rtmp.Rtmp.__init__
* @maixcdk maix.rtmp.Rtmp.Rtmp
*/
Rtmp(std::string host, int port = 1935, std::string app = std::string(), std::string stream = std::string());
Rtmp(std::string host = "localhost", int port = 1935, std::string app = std::string(), std::string stream = std::string(), int bitrate = 1000 * 1000);
~Rtmp();

/**
* @brief Get bitrate
* @return bitrate
* @maixpy maix.rtmp.Rtmp.push_video
*/
int bitrate() {
return _bitrate;
}

/**
* @brief Push rtmp video data
* @return error code, err::ERR_NONE means success, others means failed
* @return return 0 ok, other error
* @maixcdk maix.rtmp.Rtmp.push_video
*/
int push_video(void *data, size_t data_size, uint32_t timestamp);

/**
* @brief Push rtmp audio data
* @return error code, err::ERR_NONE means success, others means failed
* @return return 0 ok, other error
* @maixcdk maix.rtmp.Rtmp.push_audio
*/
int push_audio(void *data, size_t data_size, uint32_t timestamp);

/**
* @brief Push rtmp script data
* @return error code, err::ERR_NONE means success, others means failed
* @return return 0 ok, other error
* @maixcdk maix.rtmp.Rtmp.push_script
*/
int push_script(void *data, size_t data_size, uint32_t timestamp);
Expand All @@ -80,10 +112,56 @@ namespace maix::rtmp
* @maixpy maix.rtmp.Rtmp.bind_camera
*/
err::Err bind_camera(camera::Camera *cam) {
_cam = cam;
_camera = cam;
return err::ERR_NONE;
}

/**
* @brief If you bind a camera, return the camera object.
* @return Camera object
* @maixpy maix.rtmp.Rtmp.get_camera
*/
camera::Camera *get_camera() {
return _camera;
}

/**
* @brief If you bind a camera, capture the image of the camera
* @note The return value may be null, you must check whether the return value is null
* @return Image object
* @maixcdk maix.rtmp.Rtmp.capture
*/
image::Image *capture() {
err::check_raise(err::ERR_NOT_IMPL, "not support now!");
// lock(300);
// if (!_capture_image || !_capture_image->data()) {
// unlock();
// return NULL;
// }
// image::Image *new_image = new image::Image(_capture_image->width(), _capture_image->height(), _capture_image->format(),
// (uint8_t *)_capture_image->data(), _capture_image->data_size(), false);
// unlock();
// return new_image;
return NULL;
}

/**
* @brief Get pointer of capture image
* @return Image object
*/
image::Image *get_capture_image() {
return _capture_image;
}

/**
* @brief Set pointer of capture image
* @return Image object
*/
image::Image *set_capture_image(void *img) {
_capture_image = (image::Image *)img;
return _capture_image;
}

/**
* @brief Start push stream
* @note only support flv file now
Expand All @@ -94,7 +172,7 @@ namespace maix::rtmp
err::Err start(std::string path = std::string());

/**
* @brief Stop push
* @brief Stop push stream
* @return error code, err::ERR_NONE means success, others means failed
* @maixpy maix.rtmp.Rtmp.stop
*/
Expand Down Expand Up @@ -132,6 +210,15 @@ namespace maix::rtmp
bool is_started() {
return _start ? true : false;
}

/**
* @brief get handler
* @note DO NOT ADD TO MAIXPY
* @return rtmp handler
*/
void *get_handler() {
return _handler;
}
};
}

Expand Down
1 change: 1 addition & 0 deletions components/vision/port/maixcam/maix_camera_mmf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ namespace maix::camera
this->width = width;
this->height = height;
this->buffer_num = buff_num;
this->ch = -1;

if (0 != mmf_init()) {
err::check_raise(err::ERR_RUNTIME, "mmf init failed");
Expand Down
Loading

0 comments on commit e527dbd

Please sign in to comment.