代码拉取完成,页面将自动刷新
/*
* 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);
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。