3 Star 7 Fork 10

474807898/rv1106_1103_rtsp模块

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
rkadk_rtsp.cpp 25.12 KB
一键复制 编辑 原始数据 按行查看 历史
474807898 提交于 2024-03-13 11:50 . 第一次提交
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875
/*
* Copyright (c) 2021 Rockchip, Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "rkadk_rtsp.h"
#include "rkadk_log.h"
#include "rkadk_media_comm.h"
#include "rkadk_param.h"
#include "rtsp_demo.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <pthread.h>
#include <signal.h>
#ifndef USE_DEFUALT_RTSP
#ifdef __cplusplus
extern "C" {
#endif
#include "rtspservice.h"
#include "rtputils.h"
#include "ringfifo.h"
extern void RTP_port_pool_init(int port);
extern void EventLoop(int s32MainFd);
extern int HisiPutH264DataToBuffer(uint8_t *frame_data, int frame_len);
extern int g_s32Quit ;
extern int s32MainFd;
#ifdef __cplusplus
}
#endif
#endif//#ifndef USE_DEFUALT_RTSP
#if 1
//add tcpserver
//创建tcp server
#define PORT 11603//10099
static uint8_t tcp_server_running = 1;
//列表
static volatile int client_list[20];
static volatile int client_num;
void push_client_socket(int sockfd){
if (client_num >= (int)sizeof(client_list)){
return;
}
client_list[client_num] = sockfd;
client_num++;
}
void pop_client_socket(int socketfd){
int pos = -1;
printf("pop_client_socket client_num:%d, socketfd:%d\r\n", client_num, socketfd);
if (client_num == 0){
return;
}
for (int i=0; i<client_num; i++){
if (socketfd == client_list[i]){
client_list[i] = 0;
client_num--;
break;
}
}
if (client_num < 0){
client_num = 0;
}
printf("pop_client_socket end\r\n");
}
#if 0
static uint8_t last_sps_data[1024];
static int sps_data_len;
static uint8_t last_pps_data[256];
static int pps_data_len;
#endif
void send_data_2_client(uint8_t *data, int len){
#if 0
if ((*data == 0x00 && *(data+1)==0x00 && *(data+2) == 0x01 && *(data+3) == 0x67) ||
(*data == 0x00 && *(data+1)==0x00 && *(data+2) == 0x00 && *(data+3) == 0x01 && *(data+4) == 0x67)){
//sps
sps_data_len = len;
memcpy(last_sps_data, data, len);
}
if ((*data == 0x00 && *(data+1)==0x00 && *(data+2) == 0x01 && *(data+3) == 0x68) ||
(*data == 0x00 && *(data+1)==0x00 && *(data+2) == 0x00 && *(data+3) == 0x01 && *(data+4) == 0x68)){
//pps
pps_data_len = len;
memcpy(last_pps_data, data, len);
}
#endif//
int num = client_num;
for (int i=0; i<num; i++){
uint8_t type = 2;
if ((*data == 0x00 && *(data+1)==0x00 && *(data+2) == 0x01 && *(data+3) == 0x65) ||
(*data == 0x00 && *(data+1)==0x00 && *(data+2) == 0x00 && *(data+3) == 0x01 && *(data+4) == 0x65)){
type = 1;//0 ppsinfo 1:iframe 2: pframe
}
if ((*data == 0x00 && *(data+1)==0x00 && *(data+2) == 0x01 && *(data+3) == 0x67) ||
(*data == 0x00 && *(data+1)==0x00 && *(data+2) == 0x00 && *(data+3) == 0x01 && *(data+4) == 0x67)){
type = 1;//0 ppsinfo 1:iframe 2: pframe
}
//send type
int ret = write(client_list[i], &type, 1);
//send length
uint32_t length = htonl(len);
ret = write(client_list[i], &length, 4);
//send data
ret = write(client_list[i],data,len);
if (ret <= 0){
printf("write to sock:%d failed,ret:%d\r\n", client_list[i], ret);
}
}
}
void *keep_alive_func(void* arg)
{
int client_sock = (int)arg;
char buf[1024] = {0};
if (client_sock <= 0){
return NULL;
}
while(1){
ssize_t recv_size = recv(client_sock,buf,sizeof(buf)-1,0);
if(recv_size == 0)
{
printf("peer closed\r\n");
break;
}
else
{
printf("recv failed \r\n");
continue;
}
}
pop_client_socket(client_sock);
close(client_sock);
printf("keep_alive_func end client_sock:%d\r\n",client_sock);
return NULL;
}
#define UNUSED(x) (void)(x)
static void* create_tcp_server(void *arg){
int server_sockfd,client_sockfd;
socklen_t server_len,client_len;
struct sockaddr_in server_sockaddr,client_sockaddr;
UNUSED(arg);
/*create a socket.type is AF_INET,sock_stream*/
server_sockfd = socket(AF_INET,SOCK_STREAM,0);
server_sockaddr.sin_family = AF_INET;
server_sockaddr.sin_port = htons(PORT);
server_sockaddr.sin_addr.s_addr = htonl(INADDR_ANY);
server_len = sizeof(server_sockaddr);
int on;
setsockopt(server_sockfd, SOL_SOCKET, SO_REUSEADDR,&on,sizeof(on));
/*bind a socket or rename a sockt*/
if(bind(server_sockfd, (struct sockaddr*)&server_sockaddr, server_len)==-1){
printf("bind error");
return NULL;
}
if(listen(server_sockfd, 5)==-1){
printf("listen error");
return NULL;
}
pthread_t thread_id;
while(tcp_server_running) {
if((client_sockfd = accept(server_sockfd, (struct sockaddr*)&client_sockaddr, &client_len)) == -1){
printf("connect error");
//exit(1);
continue;//
} else {
char buf_ip[INET_ADDRSTRLEN];
memset(buf_ip,'\0',sizeof(buf_ip));
inet_ntop(AF_INET,&client_sockaddr.sin_addr,buf_ip,sizeof(buf_ip));
printf("get connect,ip is%s,client_sockfd:%d,server_sock:%d\n",buf_ip, client_sockfd, server_sockfd);
printf("port=%d\n",ntohs(client_sockaddr.sin_port));
push_client_socket(client_sockfd);
pthread_create(&thread_id, NULL, keep_alive_func, (void *)client_sockfd);
pthread_detach(thread_id);
}
}
return NULL;
}
void start_tcp_server(){
pthread_t thread_id;
tcp_server_running = 1;
pthread_create(&thread_id, NULL, create_tcp_server, (void *)NULL);
pthread_detach(thread_id);
}
void stop_tcp_server(){
tcp_server_running = 0;
}
static int rtsp_server_running = 0;
static void* rtsp_server(void *arg){
struct timespec ts = { 2, 0 };
RKADK_LOGE("g_s32Quit:%d, s32MainFd:%d", g_s32Quit, s32MainFd);
while (!g_s32Quit)
{
nanosleep(&ts, NULL);
EventLoop(s32MainFd);
}
return NULL;
}
void start_rtsp_server(){
ringmalloc(720*576);
pthread_t thread_id;
rtsp_server_running = 1;
pthread_create(&thread_id, NULL, rtsp_server, (void *)NULL);
pthread_detach(thread_id);
}
//add end.
#endif//
typedef struct {
bool start;
bool bRequestIDR;
bool bWaitIDR;
bool bVencChnMux;
bool bFirstKeyFrame;
RKADK_U32 u32CamId;
rtsp_demo_handle stRtspHandle;
rtsp_session_handle stRtspSession;
} RKADK_RTSP_HANDLE_S;
static void RKADK_RTSP_SetVideoChn(RKADK_PARAM_STREAM_CFG_S *pstLiveCfg, RKADK_U32 u32CamId,
MPP_CHN_S *pstViChn, MPP_CHN_S *pstVencChn,
MPP_CHN_S *pstSrcVpssChn, MPP_CHN_S *pstDstVpssChn) {
pstViChn->enModId = RK_ID_VI;
pstViChn->s32DevId = u32CamId;
pstViChn->s32ChnId = pstLiveCfg->vi_attr.u32ViChn;
pstSrcVpssChn->enModId = RK_ID_VPSS;
pstSrcVpssChn->s32DevId = pstLiveCfg->attribute.vpss_grp;
pstSrcVpssChn->s32ChnId = pstLiveCfg->attribute.vpss_chn;
pstDstVpssChn->enModId = RK_ID_VPSS;
pstDstVpssChn->s32DevId = pstLiveCfg->attribute.vpss_grp;
pstDstVpssChn->s32ChnId = 0; //When vpss is dst, chn is equal to 0
pstVencChn->enModId = RK_ID_VENC;
pstVencChn->s32DevId = 0;
pstVencChn->s32ChnId = pstLiveCfg->attribute.venc_chn;
}
static int RKADK_RTSP_SetVencAttr(RKADK_U32 u32CamId,
RKADK_PARAM_STREAM_CFG_S *pstLiveCfg,
VENC_CHN_ATTR_S *pstVencAttr) {
int ret;
RKADK_PARAM_SENSOR_CFG_S *pstSensorCfg = NULL;
RKADK_U32 u32DstFrameRateNum;
RKADK_PARAM_FPS_S stFps;
RKADK_CHECK_POINTER(pstVencAttr, RKADK_FAILURE);
memset(pstVencAttr, 0, sizeof(VENC_CHN_ATTR_S));
pstSensorCfg = RKADK_PARAM_GetSensorCfg(u32CamId);
if (!pstSensorCfg) {
RKADK_LOGE("RKADK_PARAM_GetSensorCfg failed");
return -1;
}
pstVencAttr->stRcAttr.enRcMode = RKADK_PARAM_GetRcMode(
pstLiveCfg->attribute.rc_mode, pstLiveCfg->attribute.codec_type);
u32DstFrameRateNum = pstLiveCfg->attribute.framerate;
if (u32DstFrameRateNum > pstSensorCfg->framerate) {
RKADK_LOGW("CamId[%d] rtsp fps[%d] > sensor fps[%d], use sensor fps",
u32CamId, u32DstFrameRateNum, pstSensorCfg->framerate);
u32DstFrameRateNum = pstSensorCfg->framerate;
stFps.u32Framerate = pstSensorCfg->framerate;
stFps.enStreamType = RKADK_STREAM_TYPE_LIVE;
RKADK_PARAM_SetCamParam(u32CamId, RKADK_PARAM_TYPE_FPS, &stFps);
}
ret = RKADK_MEDIA_SetRcAttr(&pstVencAttr->stRcAttr, pstLiveCfg->attribute.gop,
pstLiveCfg->attribute.bitrate,
pstSensorCfg->framerate, u32DstFrameRateNum);
if (ret) {
RKADK_LOGE("RKADK_MEDIA_SetRcAttr failed");
return -1;
}
pstVencAttr->stVencAttr.enType =
RKADK_MEDIA_GetRkCodecType(pstLiveCfg->attribute.codec_type);
pstVencAttr->stVencAttr.enPixelFormat = pstLiveCfg->vi_attr.stChnAttr.enPixelFormat;
pstVencAttr->stVencAttr.u32MaxPicWidth = pstLiveCfg->attribute.width;
pstVencAttr->stVencAttr.u32MaxPicHeight = pstLiveCfg->attribute.height;
pstVencAttr->stVencAttr.u32PicWidth = pstLiveCfg->attribute.width;
pstVencAttr->stVencAttr.u32PicHeight = pstLiveCfg->attribute.height;
pstVencAttr->stVencAttr.u32VirWidth = pstLiveCfg->attribute.width;
pstVencAttr->stVencAttr.u32VirHeight = pstLiveCfg->attribute.height;
pstVencAttr->stVencAttr.u32Profile = pstLiveCfg->attribute.profile;
pstVencAttr->stVencAttr.u32StreamBufCnt = RKADK_PARAM_GetStreamBufCnt(u32CamId, false);
pstVencAttr->stVencAttr.u32BufSize = pstLiveCfg->attribute.bufsize;
return 0;
}
static RKADK_S32 RKADK_RTSP_InitService(RKADK_CODEC_TYPE_E enCodecType,
RKADK_U32 port, const char *path,
RKADK_RTSP_HANDLE_S *pHandle) {
int ret = 0;
RKADK_LOGE("RKADK_RTSP_InitService enter.");
#ifndef USE_DEFUALT_RTSP
//use customer rtsp
RKADK_LOGE("RKADK_RTSP_InitService enter create port:%d", SERVER_RTSP_PORT_DEFAULT);
signal(SIGINT, IntHandl);
s32MainFd = tcp_listen(SERVER_RTSP_PORT_DEFAULT);
if (ScheduleInit() != 0)
{
RKADK_LOGE("Fatal: Can't start scheduler %s, %i \nServer is aborting.\n", __FILE__, __LINE__);
return 0;
}
RTP_port_pool_init(RTP_DEFAULT_PORT);
start_rtsp_server();
return 0;
#else
pHandle->stRtspHandle = create_rtsp_demo(port);
pHandle->stRtspSession = rtsp_new_session(pHandle->stRtspHandle, path);
if (enCodecType == RKADK_CODEC_TYPE_H264) {
ret = rtsp_set_video(pHandle->stRtspSession, RTSP_CODEC_ID_VIDEO_H264, NULL,
0);
if (ret) {
RKADK_LOGE("rtsp_set_video failed(%d)", ret);
return -1;
}
} else if (enCodecType == RKADK_CODEC_TYPE_H265) {
ret = rtsp_set_video(pHandle->stRtspSession, RTSP_CODEC_ID_VIDEO_H265, NULL,
0);
if (ret) {
RKADK_LOGE("rtsp_set_video failed(%d)", ret);
return -1;
}
} else {
RKADK_LOGE("Unsupport enCodecType: %d", enCodecType);
return -1;
}
return rtsp_sync_video_ts(pHandle->stRtspSession, rtsp_get_reltime(),
rtsp_get_ntptime());
#endif
}
static void RKADK_RTSP_DeInitService(RKADK_RTSP_HANDLE_S *pHandle) {
#ifdef USE_DEFUALT_RTSP
if (pHandle->stRtspHandle)
rtsp_del_demo(pHandle->stRtspHandle);
#endif
}
static RKADK_S32 RKADK_RTSP_RequestIDR(RKADK_U32 u32CamId, RKADK_U32 u32ChnId) {
int ret = 0;
RKADK_STREAM_TYPE_E enType = RKADK_PARAM_VencChnMux(u32CamId, u32ChnId);
if (enType == RKADK_STREAM_TYPE_VIDEO_MAIN ||
enType == RKADK_STREAM_TYPE_VIDEO_SUB) {
int i;
RKADK_PARAM_REC_CFG_S *pstRecCfg = RKADK_PARAM_GetRecCfg(u32CamId);
if (!pstRecCfg) {
RKADK_LOGE("RKADK_PARAM_GetRecCfg failed");
return false;
}
for (i = 0; i < (int)pstRecCfg->file_num; i++) {
RKADK_LOGI("chn mux, venc_chn[%d] request IDR",
pstRecCfg->attribute[i].venc_chn);
ret |= RK_MPI_VENC_RequestIDR(pstRecCfg->attribute[i].venc_chn, RK_FALSE);
}
} else {
ret = RK_MPI_VENC_RequestIDR(u32ChnId, RK_FALSE);
}
return ret;
}
static void RKADK_RTSP_VencOutCb(RKADK_MEDIA_VENC_DATA_S mb, RKADK_VOID *handle) {
RKADK_MEDIA_VENC_DATA_S stData = mb;
RKADK_PARAM_STREAM_CFG_S *pstLiveCfg;
RKADK_RTSP_HANDLE_S *pHandle = (RKADK_RTSP_HANDLE_S *)handle;
RKADK_VOID *data;
if (!pHandle) {
RKADK_LOGE("Can't find rtsp handle");
RK_MPI_VENC_ReleaseStream(stData.u32ChnId, &stData.stFrame);
return;
}
pstLiveCfg = RKADK_PARAM_GetStreamCfg(pHandle->u32CamId, RKADK_STREAM_TYPE_LIVE);
if (!pstLiveCfg) {
RKADK_LOGE("Live RKADK_PARAM_GetStreamCfg Live failed");
return;
}
if (!pHandle->start)
return;
if (!pHandle->bWaitIDR) {
if (!RKADK_MEDIA_CheckIdrFrame(pstLiveCfg->attribute.codec_type,
stData.stFrame.pstPack->DataType)) {
if (!pHandle->bRequestIDR) {
RKADK_LOGD("requst idr frame");
RKADK_RTSP_RequestIDR(pHandle->u32CamId, pstLiveCfg->attribute.venc_chn);
pHandle->bRequestIDR = true;
} else {
RKADK_LOGD("wait first idr frame");
}
return;
}
pHandle->bWaitIDR = true;
if (pHandle->bFirstKeyFrame) {
RKADK_KLOG("Rtsp first key frame pts: %lld", stData.stFrame.pstPack->u64PTS);
pHandle->bFirstKeyFrame = false;
}
}
data = RK_MPI_MB_Handle2VirAddr(stData.stFrame.pstPack->pMbBlk);
#ifdef USE_DEFUALT_RTSP
if (pHandle->stRtspHandle && pHandle->stRtspSession) {
rtsp_tx_video(pHandle->stRtspSession, (uint8_t *)data,
stData.stFrame.pstPack->u32Len, stData.stFrame.pstPack->u64PTS);
rtsp_do_event(pHandle->stRtspHandle);
}
#else
HisiPutH264DataToBuffer((uint8_t *)data, stData.stFrame.pstPack->u32Len);
#endif
//lyz add for voip
send_data_2_client((uint8_t *)data, stData.stFrame.pstPack->u32Len);
//add end for voip
}
static RKADK_S32 RKADK_RTSP_VencGetData(RKADK_U32 u32CamId,
MPP_CHN_S *pstVencChn,
RKADK_RTSP_HANDLE_S *pHandle) {
int ret = 0;
VENC_RECV_PIC_PARAM_S stRecvParam;
stRecvParam.s32RecvPicNum = -1;
ret = RK_MPI_VENC_StartRecvFrame(pstVencChn->s32ChnId, &stRecvParam);
if (ret) {
RKADK_LOGE("RK_MPI_VENC_StartRecvFrame failed = %d", ret);
return ret;
}
ret = RKADK_MEDIA_GetVencBuffer(pstVencChn, RKADK_RTSP_VencOutCb,
(RKADK_VOID *)pHandle);
if (ret) {
RKADK_LOGE("RKADK_MEDIA_GetVencBuffer failed = %d", ret);
return ret;
}
return ret;
}
static bool RKADK_RTSP_IsUseVpss(RKADK_U32 u32CamId, RKADK_PARAM_STREAM_CFG_S *pstLiveCfg) {
bool bUseVpss = false;
RKADK_U32 u32SrcWidth, u32SrcHeight;
RKADK_U32 u32DstWidth, u32DstHeight;
RKADK_PARAM_SENSOR_CFG_S *pstSensorCfg;
pstSensorCfg = RKADK_PARAM_GetSensorCfg(u32CamId);
if (!pstSensorCfg) {
RKADK_LOGE("RKADK_PARAM_GetSensorCfg failed");
return false;
}
u32SrcWidth = pstLiveCfg->vi_attr.stChnAttr.stSize.u32Width;
u32SrcHeight = pstLiveCfg->vi_attr.stChnAttr.stSize.u32Height;
u32DstWidth = pstLiveCfg->attribute.width;
u32DstHeight = pstLiveCfg->attribute.height;
if (u32DstWidth != u32SrcWidth || u32DstHeight != u32SrcHeight) {
RKADK_LOGD("In[%d, %d], Out[%d, %d]", u32SrcWidth, u32SrcHeight,
u32DstWidth, u32DstHeight);
bUseVpss = true;
}
if (!pstSensorCfg->used_isp) {
#ifdef RV1126_1109
if (pstSensorCfg->flip || pstSensorCfg->mirror)
#endif
bUseVpss = true;
}
return bUseVpss;
}
RKADK_S32 RKADK_RTSP_Init(RKADK_U32 u32CamId, RKADK_U32 port, const char *path,
RKADK_MW_PTR *ppHandle) {
int ret = 0;
bool bSysInit = false;
bool bUseVpss = false;
MPP_CHN_S stViChn, stVencChn, stSrcVpssChn, stDstVpssChn;
RKADK_STREAM_TYPE_E enType;
VPSS_GRP_ATTR_S stGrpAttr;
VPSS_CHN_ATTR_S stChnAttr;
RKADK_RTSP_HANDLE_S *pHandle;
RKADK_CHECK_CAMERAID(u32CamId, RKADK_FAILURE);
RKADK_CHECK_POINTER(path, RKADK_FAILURE);
RKADK_LOGI("Rtsp[%d, %d, %s] Init...", u32CamId, port, path);
if (*ppHandle) {
RKADK_LOGE("rtsp handle has been created");
return -1;
}
bSysInit = RKADK_MPI_SYS_CHECK();
if (!bSysInit) {
RKADK_LOGE("System is not initialized");
return -1;
}
RKADK_PARAM_STREAM_CFG_S *pstLiveCfg =
RKADK_PARAM_GetStreamCfg(u32CamId, RKADK_STREAM_TYPE_LIVE);
if (!pstLiveCfg) {
RKADK_LOGE("Live RKADK_PARAM_GetStreamCfg Live failed");
return -1;
}
RKADK_PARAM_SENSOR_CFG_S *pstSensorCfg = RKADK_PARAM_GetSensorCfg(u32CamId);
if (!pstSensorCfg) {
RKADK_LOGE("RKADK_PARAM_GetSensorCfg failed");
return -1;
}
pHandle = (RKADK_RTSP_HANDLE_S *)malloc(sizeof(RKADK_RTSP_HANDLE_S));
if (!pHandle) {
RKADK_LOGE("malloc pHandle failed");
return -1;
}
memset(pHandle, 0, sizeof(RKADK_RTSP_HANDLE_S));
pHandle->u32CamId = u32CamId;
pHandle->bFirstKeyFrame = true;
ret = RKADK_RTSP_InitService(pstLiveCfg->attribute.codec_type, port, path,
pHandle);
if (ret) {
RKADK_LOGE("RKADK_RTSP_InitService failed");
free(pHandle);
return -1;
}
RKADK_RTSP_SetVideoChn(pstLiveCfg, u32CamId, &stViChn, &stVencChn, &stSrcVpssChn, &stDstVpssChn);
enType = RKADK_PARAM_VencChnMux(u32CamId, stVencChn.s32ChnId);
if (enType != RKADK_STREAM_TYPE_BUTT && enType != RKADK_STREAM_TYPE_LIVE) {
switch (enType) {
case RKADK_STREAM_TYPE_VIDEO_MAIN:
RKADK_LOGI("Live and Record main venc[%d] mux", stVencChn.s32ChnId);
break;
case RKADK_STREAM_TYPE_VIDEO_SUB:
RKADK_LOGI("Live and Record sub venc[%d] mux", stVencChn.s32ChnId);
break;
case RKADK_STREAM_TYPE_PREVIEW:
RKADK_LOGI("Live and Preview venc[%d] mux", stVencChn.s32ChnId);
break;
default:
RKADK_LOGW("Invaild venc[%d] mux, enType[%d]", stVencChn.s32ChnId,
enType);
break;
}
pHandle->bVencChnMux = true;
}
// Create VI
ret = RKADK_MPI_VI_Init(u32CamId, stViChn.s32ChnId,
&(pstLiveCfg->vi_attr.stChnAttr));
if (ret) {
RKADK_LOGE("RKADK_MPI_VI_Init faled %d", ret);
goto failed;
}
bUseVpss = RKADK_RTSP_IsUseVpss(u32CamId, pstLiveCfg);
if (bUseVpss) {
memset(&stGrpAttr, 0, sizeof(VPSS_GRP_ATTR_S));
memset(&stChnAttr, 0, sizeof(VPSS_CHN_ATTR_S));
stGrpAttr.u32MaxW = pstSensorCfg->max_width;
stGrpAttr.u32MaxH = pstSensorCfg->max_height;
stGrpAttr.enPixelFormat = pstLiveCfg->vi_attr.stChnAttr.enPixelFormat;
stGrpAttr.enCompressMode = COMPRESS_MODE_NONE;
stGrpAttr.stFrameRate.s32SrcFrameRate = -1;
stGrpAttr.stFrameRate.s32DstFrameRate = -1;
stChnAttr.enChnMode = VPSS_CHN_MODE_USER;
stChnAttr.enCompressMode = COMPRESS_MODE_NONE;
stChnAttr.enDynamicRange = DYNAMIC_RANGE_SDR8;
stChnAttr.enPixelFormat = pstLiveCfg->vi_attr.stChnAttr.enPixelFormat;
stChnAttr.stFrameRate.s32SrcFrameRate = -1;
stChnAttr.stFrameRate.s32DstFrameRate = -1;
stChnAttr.u32Width = pstLiveCfg->attribute.width;
stChnAttr.u32Height = pstLiveCfg->attribute.height;
stChnAttr.bMirror = (RK_BOOL)pstSensorCfg->mirror;
stChnAttr.bFlip = (RK_BOOL)pstSensorCfg->flip;
stChnAttr.u32Depth = 0;
ret = RKADK_MPI_VPSS_Init(stSrcVpssChn.s32DevId, stSrcVpssChn.s32ChnId,
&stGrpAttr, &stChnAttr);
if (ret) {
RKADK_LOGE("RKADK_MPI_VPSS_Init falied[%d]",ret);
goto failed;
}
}
// Create VENC
VENC_CHN_ATTR_S stVencChnAttr;
ret = RKADK_RTSP_SetVencAttr(u32CamId, pstLiveCfg, &stVencChnAttr);
if (ret) {
RKADK_LOGE("RKADK_RTSP_SetVencAttr failed");
goto failed;
}
ret = RKADK_MPI_VENC_Init(u32CamId, stVencChn.s32ChnId, &stVencChnAttr);
if (ret) {
RKADK_LOGE("RKADK_MPI_VENC_Init failed(%d)", ret);
goto failed;
}
RK_MPI_VENC_SetSceneMode(stVencChn.s32ChnId, RKADK_ENCODE_SENSE_CVR);
RKADK_PARAM_SetVAdvancedParam(pstLiveCfg->attribute);
ret = RKADK_RTSP_VencGetData(u32CamId, &stVencChn, pHandle);
if (ret) {
RKADK_LOGE("RKADK_RTSP_VencGetData failed(%d)", ret);
goto failed;
}
if (bUseVpss) {
// VPSS Bind VENC
ret = RKADK_MPI_SYS_Bind(&stSrcVpssChn, &stVencChn);
if (ret) {
RKADK_LOGE("Bind VPSS[%d] to VENC[%d] failed[%x]", stSrcVpssChn.s32ChnId,
stVencChn.s32ChnId, ret);
goto failed;
}
// VI Bind VPSS
ret = RKADK_MPI_SYS_Bind(&stViChn, &stDstVpssChn);
if (ret) {
RKADK_LOGE("Bind VI[%d] to VPSS[%d] failed[%x]", stViChn.s32ChnId,
stDstVpssChn.s32DevId, ret);
RKADK_MPI_SYS_UnBind(&stSrcVpssChn, &stVencChn);
goto failed;
}
} else {
// VI Bind VENC
ret = RKADK_MPI_SYS_Bind(&stViChn, &stVencChn);
if (ret) {
RKADK_LOGE("Bind VI[%d] to VENC[%d] failed[%x]", stViChn.s32ChnId,
stVencChn.s32ChnId, ret);
goto failed;
}
}
*ppHandle = (RKADK_MW_PTR)pHandle;
//liyizhang add for voip
start_tcp_server();
//add end.
RKADK_LOGI("Rtsp[%d, %d, %s] Init End...", u32CamId, port, path);
return 0;
failed:
RKADK_LOGE("failed");
RKADK_MPI_VENC_DeInit(stVencChn.s32ChnId);
RKADK_MPI_VPSS_DeInit(stSrcVpssChn.s32DevId, stSrcVpssChn.s32ChnId);
RKADK_MPI_VI_DeInit(u32CamId, stViChn.s32ChnId);
if (pHandle) {
RKADK_RTSP_DeInitService((RKADK_RTSP_HANDLE_S *)pHandle);
free(pHandle);
}
return ret;
}
RKADK_S32 RKADK_RTSP_DeInit(RKADK_MW_PTR pHandle) {
int ret = 0;
bool bUseVpss = false;
MPP_CHN_S stViChn, stVencChn, stSrcVpssChn, stDstVpssChn;
RKADK_CHECK_POINTER(pHandle, RKADK_FAILURE);
RKADK_RTSP_HANDLE_S *pstHandle = (RKADK_RTSP_HANDLE_S *)pHandle;
RKADK_LOGI("Rtsp[%d] DeInit...", pstHandle->u32CamId);
RKADK_PARAM_STREAM_CFG_S *pstLiveCfg =
RKADK_PARAM_GetStreamCfg(pstHandle->u32CamId, RKADK_STREAM_TYPE_LIVE);
if (!pstLiveCfg) {
RKADK_LOGE("RKADK_PARAM_GetStreamCfg failed");
return -1;
}
RKADK_RTSP_SetVideoChn(pstLiveCfg, pstHandle->u32CamId, &stViChn, &stVencChn,
&stSrcVpssChn, &stDstVpssChn);
// exit get media buffer
if (pstHandle->bVencChnMux)
RKADK_MEDIA_StopGetVencBuffer(&stVencChn, RKADK_RTSP_VencOutCb, pstHandle);
bUseVpss = RKADK_RTSP_IsUseVpss(pstHandle->u32CamId, pstLiveCfg);
if (bUseVpss){
// VPSS UnBind VENC
ret = RKADK_MPI_SYS_UnBind(&stSrcVpssChn, &stVencChn);
if (ret) {
RKADK_LOGE("UnBind VPSS[%d] to VENC[%d] failed[%x]", stSrcVpssChn.s32ChnId,
stVencChn.s32ChnId, ret);
return ret;
}
// VI UnBind VPSS
ret = RKADK_MPI_SYS_UnBind(&stViChn, &stDstVpssChn);
if (ret) {
RKADK_LOGE("UnBind VI[%d] to VPSS[%d] failed[%x]", stViChn.s32ChnId,
stDstVpssChn.s32DevId, ret);
return ret;
}
} else {
// VI UnBind VENC
ret = RKADK_MPI_SYS_UnBind(&stViChn, &stVencChn);
if (ret) {
RKADK_LOGE("UnBind VI[%d] to VENC[%d] failed[%x]", stViChn.s32ChnId,
stVencChn.s32ChnId, ret);
return ret;
}
}
// destroy venc before vi
ret = RKADK_MPI_VENC_DeInit(stVencChn.s32ChnId);
if (ret) {
RKADK_LOGE("RKADK_MPI_VENC_DeInit failed(%d)", ret);
return ret;
}
// Destory VPSS
if (bUseVpss) {
ret = RKADK_MPI_VPSS_DeInit(stSrcVpssChn.s32DevId, stSrcVpssChn.s32ChnId);
if (ret) {
RKADK_LOGE("DeInit VPSS[%d] failed[%x]", stSrcVpssChn.s32ChnId, ret);
return ret;
}
}
// destroy vi
ret = RKADK_MPI_VI_DeInit(pstHandle->u32CamId, stViChn.s32ChnId);
if (ret) {
RKADK_LOGE("RKADK_MPI_VI_DeInit failed %d", ret);
return ret;
}
RKADK_RTSP_DeInitService((RKADK_RTSP_HANDLE_S *)pHandle);
RKADK_LOGI("Rtsp[%d] DeInit End...", pstHandle->u32CamId);
free(pHandle);
return 0;
}
RKADK_S32 RKADK_RTSP_Start(RKADK_MW_PTR pHandle) {
RKADK_CHECK_POINTER(pHandle, RKADK_FAILURE);
RKADK_RTSP_HANDLE_S *pstHandle = (RKADK_RTSP_HANDLE_S *)pHandle;
if (pstHandle->start)
return 0;
RKADK_PARAM_STREAM_CFG_S *pstLiveCfg =
RKADK_PARAM_GetStreamCfg(pstHandle->u32CamId, RKADK_STREAM_TYPE_LIVE);
if (!pstLiveCfg) {
RKADK_LOGE("RKADK_PARAM_GetStreamCfg failed");
return -1;
}
pstHandle->start = true;
pstHandle->bRequestIDR = false;
pstHandle->bWaitIDR = false;
// multiplex venc chn, thread get mediabuffer
if (pstHandle->bVencChnMux)
return 0;
VENC_RECV_PIC_PARAM_S stRecvParam;
stRecvParam.s32RecvPicNum = -1;
return RK_MPI_VENC_StartRecvFrame(pstLiveCfg->attribute.venc_chn,
&stRecvParam);
}
RKADK_S32 RKADK_RTSP_Stop(RKADK_MW_PTR pHandle) {
RKADK_CHECK_POINTER(pHandle, RKADK_FAILURE);
RKADK_RTSP_HANDLE_S *pstHandle = (RKADK_RTSP_HANDLE_S *)pHandle;
if (!pstHandle->start)
return 0;
RKADK_PARAM_STREAM_CFG_S *pstLiveCfg =
RKADK_PARAM_GetStreamCfg(pstHandle->u32CamId, RKADK_STREAM_TYPE_LIVE);
if (!pstLiveCfg) {
RKADK_LOGE("RKADK_PARAM_GetStreamCfg failed");
return -1;
}
pstHandle->start = false;
// multiplex venc chn, thread get mediabuffer
if (pstHandle->bVencChnMux)
return 0;
VENC_RECV_PIC_PARAM_S stRecvParam;
stRecvParam.s32RecvPicNum = 0;
return RK_MPI_VENC_StartRecvFrame(pstLiveCfg->attribute.venc_chn,
&stRecvParam);
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/aiprogrammer/rv11061103rtsp-module.git
[email protected]:aiprogrammer/rv11061103rtsp-module.git
aiprogrammer
rv11061103rtsp-module
rv1106_1103_rtsp模块
master

搜索帮助