static int enc_release_h264_stream(int vin, int stream) { SDK_ENC_H264_STREAM_ATTR_t* stream_attr = &_sdk_enc.attr.video_stream_attr[vin][stream]; if(vin < HI_VENC_CH_BACKLOG_REF && stream < HI_VENC_STREAM_BACKLOG_REF){ if(ENC_H264_STREAM_ATTR_MAGIC == stream_attr->magic){ int const venc_group = vin; int const venc_ch = vin * HI_VENC_STREAM_BACKLOG_REF + stream; SOC_TRACE("Release video encode (%d,%d)", vin, stream); SOC_CHECK(HI_MPI_VENC_StopRecvPic(venc_ch)); SOC_CHECK(HI_MPI_VENC_UnRegisterChn(venc_ch)); SOC_CHECK(HI_MPI_VENC_DestroyChn(venc_ch)); if(0 == stream){ SOC_CHECK(HI_MPI_VENC_UnbindInput(venc_group)); SOC_CHECK(HI_MPI_VENC_DestroyGroup(venc_group)); } // clear the magic stream_attr->magic = 0; return SDK_SUCCESS; } } return SDK_FAILURE; }
static int enc_create_g711a_stream(int ain, int vin_ref, SDK_ENC_G711A_STREAM_ATTR_t* stream_attr) { if(ain < HI_AENC_CH_BACKLOG_REF){ if(vin_ref < HI_VENC_CH_BACKLOG_REF){ int const aenc_ch = ain; SDK_ENC_G711A_STREAM_ATTR_t* const this_stream_attr = &_sdk_enc.attr.audio_stream_attr[ain]; if(0 == this_stream_attr->magic){ AENC_ATTR_G711_S aenc_attr_g711 = {0}; AENC_CHN_ATTR_S aenc_ch_attr = {.enType = PT_G711A, .pValue = &aenc_attr_g711,}; MPP_CHN_S mpp_ch_ai, mpp_ch_aenc; aenc_ch_attr.enType = PT_G711A; aenc_ch_attr.pValue = &aenc_attr_g711; //aenc_ch_attr.u32BufSize = 8; //aenc_ch_attr.u32BufSize = MAX_AUDIO_FRAME_NUM; // ver 050 faq 1.6.3 aenc_ch_attr.u32BufSize = MAX_AUDIO_FRAME_NUM; memcpy(this_stream_attr, stream_attr, sizeof(SDK_ENC_G711A_STREAM_ATTR_t)); this_stream_attr->magic = ENC_H264_STREAM_ATTR_MAGIC; // mark the magic this_stream_attr->ain = ain; this_stream_attr->vin_ref = vin_ref; // FIXME: remember to check the sample rate and bitwidth this_stream_attr->sample_rate = SDK_ENC_AUDIO_SAMPLE_RATE_8KBPS; this_stream_attr->bit_width = SDK_ENC_AUDIO_BITWIDTH_8BITS; this_stream_attr->packet_size = 320; // sdk operate // enable vin SOC_CHECK(HI_MPI_AI_EnableChn(__HI_AIN_CH, ain)); // create aenc chn SOC_CHECK(HI_MPI_AENC_CreateChn(aenc_ch, &aenc_ch_attr)); #if 0 //enable resample // bind AENC to AI channel mpp_ch_ai.enModId = HI_ID_AI; mpp_ch_ai.s32DevId = __HI_AIN_CH; mpp_ch_ai.s32ChnId = ain; mpp_ch_aenc.enModId = HI_ID_AENC; mpp_ch_aenc.s32DevId = 0; mpp_ch_aenc.s32ChnId = aenc_ch; SOC_CHECK(HI_MPI_SYS_Bind(&mpp_ch_ai, &mpp_ch_aenc)); #endif _sdk_enc.attr.audio_msk |= 1<<ain; return SDK_SUCCESS; } }
/* FUNCTION: bsd_getsockopt() * * Get a socket option value * * PARAM1: s; IN - socket descriptor * PARAM2: level; IN - the level of the option to get * PARAM3: name; IN - the name of the option to get * PARAM4: opt; OUT - ptr to buffer for return of option value; may be * NULL * PARAM5: optlen; IN/OUT - ptr to int; on entry, specifies the length * in bytes of opt; on successful return, specifies the * length of the returned option; may be NULL if opt is * also NULL * RETURNS: 0 if successful, -1 if an error occurred. The error is * available via bsd_errno(s). */ int bsd_getsockopt(BSD_SOCKET s, int level, int name, void * opt, int * optlen) { struct socket * so; int loptlen; int e; so = LONG2SO(s); SOC_CHECK(so); /* make sure supplied option value is big enough for the * named option, else fail w/error EFAULT */ loptlen = bsd_i_sockoptlen(level, name); if ((optlen == NULL) || (*optlen < loptlen)) { so->so_error = EFAULT; return -1; } e = t_getsockopt(s, level, name, opt, loptlen); /* if it worked, copy the option length back for the caller's use */ if (e == 0) { *optlen = loptlen; } return e; }
int t_getsockopt(long s, int level, int name, void * arg, int arglen) { struct socket * so; int err; so = LONG2SO(s); SOC_CHECK(so); USE_ARG(level); USE_ARG(arglen); LOCK_NET_RESOURCE (NET_RESID); INET_TRACE (INETM_SOCKET, ("INET: getsockopt: name %x val %x valsize %d\n", name, val)); /* is it a level IP_OPTIONS call? */ if (level != IP_OPTIONS) { if ((err = sogetopt (so, name, arg)) != 0) { so->so_error = err; UNLOCK_NET_RESOURCE (NET_RESID); return SOCKET_ERROR; } } else { /* level 1 options are for the IP packet level. * the info is carried in the socket CB, then put * into the PACKET. */ if (name == IP_TTL_OPT) { if (!so->so_optsPack) *(int *)arg = IP_TTL; else *(int *)arg = (int)so->so_optsPack->ip_ttl; } else if (name == IP_TOS) { if (!so->so_optsPack) *(int *)arg = IP_TOS_DEFVAL; else *(int *)arg = (int)so->so_optsPack->ip_tos; } else { UNLOCK_NET_RESOURCE (NET_RESID); return SOCKET_ERROR; } } so->so_error = 0; UNLOCK_NET_RESOURCE (NET_RESID); return 0; }
/* FUNCTION: bsd_recvfrom() * * Receive data from a socket * * PARAM1: s; IN - socket descriptor * PARAM2: buf; OUT - ptr to buffer for return of received data * PARAM3: len; IN - length (in bytes) of buf * PARAM4: flags; IN - flags to be applied to the recvfrom() operation * PARAM5: from; OUT - ptr to buffer for return of sending peer's name; * may be NULL if return of name is not requested * PARAM6: fromlen; IN/OUT - ptr to int; on entry, specifies the length * in bytes of from; on successful return, specifies the * length of the returned name; may be NULL if * from is also NULL * RETURNS: the number of bytes received (>= 0), or -1 if an error * occurred. The error is available via bsd_errno(s). */ int bsd_recvfrom(BSD_SOCKET s, void * buf, BSD_SIZE_T len, int flags, struct sockaddr * from, int * fromlen) { struct socket * so; struct sockaddr lfrom; int lfromlen = 0; int lret; so = LONG2SO(s); SOC_CHECK(so); /* if we were given a buffer for the peer's address, also get the * buffer's length */ if (from != NULL) { if (fromlen == NULL) { so->so_error = EFAULT; return -1; } lfromlen = *fromlen; } lret = t_recvfrom(s, (char *)buf, len, flags, &lfrom, &lfromlen ); /* if we were successful, and we were given a buffer for the peer's * name: copy the peer's name back into the buffer, but limit * the copy to the lesser of the buffer's length and sizeof(struct * sockaddr_in), which is all that t_recvfrom() can return as a peer * name. * For IPV6 addresses or dual IPV4/IPV6 stack, the max size copied * is sizeof(struct sockaddr_in6) */ if ((lret != -1) && (from != NULL)) { #ifndef IP_V6 if (lfromlen > sizeof(struct sockaddr_in)) lfromlen = sizeof(struct sockaddr_in); #else if (lfromlen > sizeof(struct sockaddr_in6)) lfromlen = sizeof(struct sockaddr_in6); #endif MEMCPY(from, &lfrom, lfromlen); *fromlen = lfromlen; } return lret; }
int t_recv (long s, char * buf, int len, int flag) { #ifdef SOCKDEBUG char logbuf[10]; #endif struct socket * so; int err; int sendlen = len; so = LONG2SO(s); #ifdef SOC_CHECK_ALWAYS SOC_CHECK(so); #endif if ((so->so_state & SO_IO_OK) != SS_ISCONNECTED) { so->so_error = EPIPE; #ifdef SOCKDEBUG sprintf(logbuf, "t_recv: %d", so->so_error); glog_with_type(LOG_TYPE_DEBUG, logbuf, 1); #endif return SOCKET_ERROR; } so->so_error = 0; LOCK_NET_RESOURCE(NET_RESID); IN_PROFILER(PF_TCP, PF_ENTRY); /* measure time in TCP */ INET_TRACE (INETM_IO, ("INET:recv: so %x, len %d\n", so, len)); err = soreceive(so, NULL, buf, &len, flag); IN_PROFILER(PF_TCP, PF_EXIT); /* measure time in TCP */ UNLOCK_NET_RESOURCE(NET_RESID); if(err) { so->so_error = err; #ifdef SOCKDEBUG sprintf(logbuf, "t_recv: %d", so->so_error); glog_with_type(LOG_TYPE_DEBUG, logbuf, 1); #endif return SOCKET_ERROR; } /* return bytes we sent - the amount we wanted to send minus * the amount left in the buffer. */ return (sendlen - len); }
static int enc_release_h264_stream(int vin, int stream) { SDK_ENC_H264_STREAM_ATTR_t* stream_attr = &_sdk_enc.attr.video_stream_attr[vin][stream]; if(stream_attr && vin < HI_VENC_CH_BACKLOG_REF && stream < HI_VENC_STREAM_BACKLOG_REF){ if(ENC_H264_STREAM_ATTR_MAGIC == stream_attr->magic){ int const venc_ch = __HI_VENC_CH(vin, stream); int const venc_group = venc_ch; int const vpss_group = vin; int const vpss_ch = (venc_ch % HI_VENC_STREAM_BACKLOG_REF); MPP_CHN_S mpp_chn_vpss; MPP_CHN_S mpp_chn_venc; memset(&mpp_chn_vpss, 0, sizeof(mpp_chn_vpss)); memset(&mpp_chn_venc, 0, sizeof(mpp_chn_venc)); // unbind venc to vpss mpp_chn_vpss.enModId = HI_ID_VPSS; mpp_chn_vpss.s32DevId = vpss_group; mpp_chn_vpss.s32ChnId = vpss_ch; mpp_chn_venc.enModId = HI_ID_GROUP; mpp_chn_venc.s32DevId = venc_group; mpp_chn_venc.s32ChnId = venc_ch; SOC_TRACE("Stoping recv venc %d", venc_ch); SOC_CHECK(HI_MPI_VENC_StopRecvPic(venc_ch)); SOC_CHECK(HI_MPI_SYS_UnBind(&mpp_chn_vpss, &mpp_chn_venc)); SOC_CHECK(HI_MPI_VENC_UnRegisterChn(venc_ch)); SOC_CHECK(HI_MPI_VENC_DestroyChn(venc_ch)); SOC_CHECK(HI_MPI_VENC_DestroyGroup(venc_group)); // clear the magic stream_attr->magic = 0; return SDK_SUCCESS; } } return SDK_FAILURE; }
/* FUNCTION: bsd_getpeername() * * Get the name of the connected peer * * PARAM1: s; IN - socket descriptor * PARAM2: name; OUT - ptr to buffer for return of peer's address * PARAM3: addrlen; IN/OUT - ptr to int; on entry, specifies the length * in bytes of name; on successful return, specifies the * length of the returned name * RETURNS: 0 if successful, -1 if an error occurred. The error is * available via bsd_errno(s). */ int bsd_getpeername(BSD_SOCKET s, struct sockaddr * name, int * namelen) { struct socket * so; struct sockaddr lname; int lnamelen; int lret; so = LONG2SO(s); SOC_CHECK(so); /* if the buffer length is bogus, fail */ if (namelen == NULL) { so->so_error = EFAULT; return -1; } lnamelen = *namelen; lret = t_getpeername(s, &lname, &lnamelen); /* if we were successful: copy the peer's address back into the * buffer, but limit the copy to the lesser of the buffer's length * and sizeof(struct sockaddr_in), which is all that * t_getpeername() can return as a peer address, and pass the * copied length back to the caller. * For IPV6 addresses, or for dual IPV4/IPV6 stack, * the max size is sizeof(struct sockaddr_in6) */ if (lret != -1) { #ifndef IP_V6 if (lnamelen > sizeof(struct sockaddr_in)) lnamelen = sizeof(struct sockaddr_in); #else if (lnamelen > sizeof(struct sockaddr_in6)) lnamelen = sizeof(struct sockaddr_in6); #endif MEMCPY(name, &lname, lnamelen); *namelen = lnamelen; } return lret; }
int t_bind (long s, struct sockaddr * addr, int addrlen) { struct mbuf * nam; struct sockaddr sa; struct sockaddr * sap; struct socket * so; int err; so = LONG2SO(s); /* convert long to socket */ SOC_CHECK(so); DOMAIN_CHECK(so, addrlen); so->so_error = 0; if (addr == (struct sockaddr *)NULL) { MEMSET ((void *)&sa, 0, sizeof(sa)); addrlen = sizeof(sa); sa.sa_family = so->so_domain; sap = &sa; } else sap = addr; if ((nam = sockargs (sap, addrlen, MT_SONAME)) == NULL) { so->so_error = ENOMEM; return SOCKET_ERROR; } LOCK_NET_RESOURCE(NET_RESID); err = sobind (so, nam); m_freem(nam); UNLOCK_NET_RESOURCE(NET_RESID); if (err) { so->so_error = err; return SOCKET_ERROR; } return 0; }
/* FUNCTION: bsd_accept() * * Accept a connection on a socket * * PARAM1: s; IN - socket descriptor for the listening socket * PARAM2: addr; OUT - ptr to buffer for return of accepted peer's address; * may be NULL if return of address is not requested * PARAM3: addrlen; IN/OUT - ptr to int; on entry, specifies the length * in bytes of addr; on successful return, specifies the * length of the returned address; may be NULL if * addr is also NULL * RETURNS: A socket descriptor if successful, -1 if an error occurred. * The error is available via bsd_errno(s). */ BSD_SOCKET bsd_accept(BSD_SOCKET s, struct sockaddr * addr, int * addrlen) { struct socket * so; struct sockaddr laddr; long lret; so = LONG2SO(s); SOC_CHECK(so); /* if we were given a buffer for the peer's address, also get the * buffer's length */ if (addr != NULL) { if (addrlen == 0) { so->so_error = EFAULT; return -1; } } lret = t_accept(s, &laddr, addrlen); /* if we were successful, and we were given a buffer for the peer's * address: copy the peer's address back into the buffer, but limit * the copy to the lesser of the buffer's length and sizeof(struct * sockaddr_in), which is all that t_accept() can return as a peer * address. */ if ((lret != -1) && (addr != NULL)) { if (*addrlen > sizeof(struct sockaddr_in)) *addrlen = sizeof(struct sockaddr_in); MEMCPY(addr, &laddr, *addrlen); } return lret; }
/* FUNCTION: bsd_setsockopt() * * Set a socket option value * * PARAM1: s; IN - socket descriptor * PARAM2: level; IN - the level of the option to set * PARAM3: name; IN - the name of the option to set * PARAM4: opt; OUT - ptr to buffer containing option value; may be * NULL * PARAM5: optlen; IN - specifies the length in bytes of the option * value in opt * RETURNS: 0 if successful, -1 if an error occurred. The error is * available via bsd_errno(s). */ int bsd_setsockopt(BSD_SOCKET s, int level, int name, void * opt, int optlen) { struct socket * so; so = LONG2SO(s); SOC_CHECK(so); /* make sure supplied option value is big enough for the * named option, else fail w/error EFAULT */ if (optlen < bsd_i_sockoptlen(level, name)) { so->so_error = EFAULT; return -1; } return t_setsockopt(s, level, name, opt, optlen); }
int t_recvfrom(long s, char * buf, int len, int flags, struct sockaddr * from, int * fromlen) { struct socket * so; struct mbuf * sender = NULL; int err; int sendlen = len; so = LONG2SO(s); SOC_CHECK(so); so->so_error = 0; LOCK_NET_RESOURCE(NET_RESID); err = soreceive(so, &sender, buf, &len, flags); /* copy sender info from mbuf to sockaddr */ if (sender) { MEMCPY(from, (mtod(sender, struct sockaddr *)), *fromlen ); m_freem (sender); } UNLOCK_NET_RESOURCE(NET_RESID); if(err) { so->so_error = err; return SOCKET_ERROR; } /* OK return: amount of data actually sent */ return (sendlen - len); }
int t_shutdown(long s, int how) { struct socket *so; int err; so = LONG2SO(s); SOC_CHECK(so); so->so_error = 0; INET_TRACE (INETM_SOCKET, ("INET:shutdown so %x how %d\n", so, how)); LOCK_NET_RESOURCE(NET_RESID); err = soshutdown(so, how); UNLOCK_NET_RESOURCE(NET_RESID); if (err != 0) { so->so_error = err; return SOCKET_ERROR; } return 0; }
int t_listen(long s, int backlog) { struct socket * so; int err; so = LONG2SO(s); /* convert long to socket */ SOC_CHECK(so); so->so_error = 0; INET_TRACE (INETM_SOCKET, ("SOCK:listen:qlen %d\n", backlog)); LOCK_NET_RESOURCE(NET_RESID); err = solisten (so, backlog); UNLOCK_NET_RESOURCE(NET_RESID); if (err != 0) { so->so_error = err; return SOCKET_ERROR; } return 0; }
/* * FUNCTION: bsd_ioctl() * * Perform a control operation on a socket * * PARAM1: s; IN - socket descriptor * PARAM2: request; IN - requested control operation * PARAM3: arg; IN/OUT - arguments as required for control operation * RETURNS: 0 if successful, -1 if an error occurred. The error is * available via bsd_errno(s). */ int bsd_ioctl(BSD_SOCKET s, unsigned long request, ...) { struct socket * so; va_list argptr; int iarg; so = LONG2SO(s); SOC_CHECK(so); va_start(argptr, request); switch (request) { case FIONBIO: iarg = va_arg(argptr, int); va_end(argptr); return t_setsockopt(s, SOL_SOCKET, SO_NONBLOCK, &iarg, sizeof(iarg)); default: so->so_error = EINVAL; return -1; } }
int t_socketclose(long s) { struct socket * so; int err; so = LONG2SO(s); SOC_CHECK(so); so->so_error = 0; INET_TRACE ((INETM_CLOSE|INETM_SOCKET), ("INET:close, so %lx\n",so)); LOCK_NET_RESOURCE(NET_RESID); err = soclose(so); UNLOCK_NET_RESOURCE(NET_RESID); if (err != 0) { /* do not do the following assignment since the socket structure addressed by so has been freed by this point, jharan 12-10-98 */ /* so->so_error = err; */ return SOCKET_ERROR; } return 0; }
static int enc_create_h264_stream(const char* name, int vin, int stream, SDK_ENC_H264_STREAM_ATTR_t* stream_attr) { if(vin < HI_VENC_CH_BACKLOG_REF){ if(stream < HI_VENC_STREAM_BACKLOG_REF){ SDK_ENC_H264_STREAM_ATTR_t* const main_stream_attr = &_sdk_enc.attr.video_stream_attr[vin][0]; SDK_ENC_H264_STREAM_ATTR_t* const this_stream_attr = &_sdk_enc.attr.video_stream_attr[vin][stream]; if(0 == this_stream_attr->magic){ VENC_ATTR_H264_REF_MODE_E venc_attr_h264_ref_mode; VENC_CHN_ATTR_S venc_chn_attr; VENC_ATTR_H264_S venc_attr_h264; VENC_ATTR_H264_RC_S venc_attr_h264_rc = {0}; int const venc_group = vin; int const venc_ch = vin * HI_VENC_STREAM_BACKLOG_REF + stream; bool is_main = (0 == stream) ? true : false; // only magic is null could be init // init this stream attribute; memcpy(this_stream_attr, stream_attr, sizeof(SDK_ENC_H264_STREAM_ATTR_t)); strncpy(this_stream_attr->name, name, sizeof(this_stream_attr->name)); this_stream_attr->magic = ENC_H264_STREAM_ATTR_MAGIC; // mark it this_stream_attr->vin = vin; this_stream_attr->stream = stream; if(stream > 0){ // the size of stream depands on the main stream size if((this_stream_attr->width != main_stream_attr->width && this_stream_attr->width * 2 != main_stream_attr->width) || (this_stream_attr->height != main_stream_attr->height && this_stream_attr->height * 2 != main_stream_attr->height)){ this_stream_attr->width = main_stream_attr->width / 2; this_stream_attr->height = main_stream_attr->height / 2; } } this_stream_attr->start = false; // very important, declare the encode status SDK_ZERO_VAL(venc_chn_attr); venc_chn_attr.enType = PT_H264; // both h264 venc_chn_attr.pValue = &venc_attr_h264; SDK_ZERO_VAL(venc_attr_h264); venc_attr_h264.bMainStream = is_main ? HI_TRUE : HI_FALSE; venc_attr_h264.bField = HI_FALSE; venc_attr_h264.bVIField = HI_FALSE; venc_attr_h264.u32PicWidth = this_stream_attr->width; venc_attr_h264.u32PicHeight = this_stream_attr->height; venc_attr_h264.u32ViFramerate = this_stream_attr->vin_fps; venc_attr_h264.u32TargetFramerate = this_stream_attr->fps; venc_attr_h264.u32Gop = this_stream_attr->gop; venc_attr_h264.u32MaxDelay = 100; venc_attr_h264.u32Priority = 0; venc_attr_h264.bByFrame = HI_TRUE; venc_attr_h264.u32BufSize = venc_attr_h264.u32PicWidth * venc_attr_h264.u32PicHeight * 3 / 2; venc_attr_h264.enRcMode = RC_MODE_CBR; venc_attr_h264.u32PicLevel = 0; //venc_attr_h264.u32Bitrate = this_stream_attr->bps; venc_attr_h264.u32Bitrate = bitrate_regulate(this_stream_attr->width, this_stream_attr->height, this_stream_attr->bps); SOC_TRACE("Creating h264 %d,%d", vin, stream); // create video encode group if(is_main){ int vi_dev = 0; int vi_chn = 0; vimap_hi3515(vin, &vi_dev, &vi_chn); SOC_TRACE("Binding to (%d,%d)", vi_dev, vi_chn); SOC_CHECK(HI_MPI_VENC_CreateGroup(venc_group)); SOC_CHECK(HI_MPI_VENC_BindInput(venc_group, vi_dev, vi_chn)); } // create video encode channel and register it SOC_CHECK(HI_MPI_VENC_CreateChn(venc_ch, &venc_chn_attr, HI_NULL)); SOC_CHECK(HI_MPI_VENC_RegisterChn(venc_group, venc_ch)); // set h264 rc parameter SOC_CHECK(HI_MPI_VENC_GetH264eRcPara(venc_ch, &venc_attr_h264_rc)); venc_attr_h264_rc.s32IdrQpMax = 42; // 050 version description point 9 venc_attr_h264_rc.bFrameLostAllow = HI_TRUE; venc_attr_h264_rc.s32OsdProtectEn = HI_FALSE; venc_attr_h264_rc.bSceneChangeClip = HI_FALSE; // 050 version description point 13 venc_attr_h264_rc.bVbrQpDownAllowed = HI_TRUE; SOC_CHECK(HI_MPI_VENC_SetH264eRcPara(venc_ch, &venc_attr_h264_rc)); // set h264 reference mode SOC_CHECK(HI_MPI_VENC_GetH264eRefMode(venc_ch, &venc_attr_h264_ref_mode)); switch(stream_attr->ref_mode) { case SDK_ENC_H264_REF_MODE_1X: venc_attr_h264_ref_mode = H264E_REF_MODE_1X; break; case SDK_ENC_H264_REF_MODE_2X: venc_attr_h264_ref_mode = H264E_REF_MODE_2X; break; case SDK_ENC_H264_REF_MODE_4X: venc_attr_h264_ref_mode = H264E_REF_MODE_4X; break; } SOC_CHECK(HI_MPI_VENC_SetH264eRefMode(venc_ch, venc_attr_h264_ref_mode)); return SDK_SUCCESS; } } } return SDK_FAILURE; }
int t_setsockopt(long s, int level, int name, void * arg, int arglen) { struct socket * so; int err; so = LONG2SO(s); SOC_CHECK(so); USE_ARG(arglen); LOCK_NET_RESOURCE (NET_RESID); so->so_error = 0; INET_TRACE (INETM_SOCKET, ("INET: setsockopt: name %x val %x valsize %d\n", name, val)); /* is it a level IP_OPTIONS call? */ if (level != IP_OPTIONS) { if ((err = sosetopt (so, name, arg)) != 0) { so->so_error = err; UNLOCK_NET_RESOURCE (NET_RESID); return SOCKET_ERROR; } } else { /* level 1 options are for the IP packet level. * the info is carried in the socket CB, then put * into the PACKET. */ if (!so->so_optsPack) { so->so_optsPack = (struct ip_socopts *) SOCOPT_ALLOC (sizeof(struct ip_socopts *)); if (!so->so_optsPack) { so->so_error = ENOMEM; UNLOCK_NET_RESOURCE (NET_RESID); return SOCKET_ERROR; } } if (name == IP_TTL_OPT) so->so_optsPack->ip_ttl = (u_char)(*(int *)arg); else if (name == IP_TOS) so->so_optsPack->ip_tos = (u_char)(*(int *)arg); else if (name == IP_SCOPEID) so->so_optsPack->ip_scopeid = (u_char)(*(u_int *)arg); else { UNLOCK_NET_RESOURCE (NET_RESID); return SOCKET_ERROR; } } UNLOCK_NET_RESOURCE (NET_RESID); return 0; }
static int t_getname(long s, struct sockaddr * addr, int * addrlen, int opcode) { struct socket * so; struct mbuf * m; int err; so = LONG2SO(s); SOC_CHECK(so); so->so_error = 0; INET_TRACE (INETM_SOCKET, ("INET:get[sock|peer]name so %x\n", so)); if((opcode == PRU_PEERADDR) && (so->so_state & SS_ISCONNECTED) == 0) { so->so_error = ENOTCONN; return SOCKET_ERROR; } LOCK_NET_RESOURCE(NET_RESID); m = m_getwithdata (MT_SONAME, sizeof (struct sockaddr)); if (m == NULL) { so->so_error = ENOMEM; UNLOCK_NET_RESOURCE(NET_RESID); return SOCKET_ERROR; } so->so_req = opcode; if ((err = (*so->so_proto->pr_usrreq)(so, 0, m)) != 0) goto bad; #ifdef IP_V4 if(so->so_domain == AF_INET) { if(*addrlen < sizeof(struct sockaddr_in)) { dtrap(); /* programming error */ m_freem(m); UNLOCK_NET_RESOURCE(NET_RESID); return EINVAL; } MEMCPY(addr, m->m_data, sizeof(struct sockaddr_in)); *addrlen = sizeof(struct sockaddr_in); } #endif /* IP_V4 */ #ifdef IP_V6 if(so->so_domain == AF_INET6) { if(*addrlen < sizeof(struct sockaddr_in6)) { dtrap(); /* programming error */ m_freem(m); UNLOCK_NET_RESOURCE(NET_RESID); return EINVAL; } MEMCPY(addr, m->m_data, sizeof(struct sockaddr_in6)); *addrlen = sizeof(struct sockaddr_in6); } #endif /* IP_V6 */ bad: m_freem(m); UNLOCK_NET_RESOURCE(NET_RESID); if (err) { so->so_error = err; return SOCKET_ERROR; } return 0; }
int t_connect(long s, struct sockaddr * addr, int addrlen) { struct socket * so; struct mbuf * nam; so = LONG2SO(s); SOC_CHECK(so); DOMAIN_CHECK(so, addrlen); #ifdef NB_CONNECT /* need to test non blocking connect bits in case this is a poll of a previous request */ if (so->so_state & SS_NBIO) { if (so->so_state & SS_ISCONNECTING) /* still trying */ { so->so_error = EINPROGRESS; return SOCKET_ERROR; } if (so->so_state & SS_ISCONNECTED) /* connected OK */ { so->so_error = 0; return 0; } if (so->so_state & SS_WASCONNECTING) { so->so_state &= ~SS_WASCONNECTING; if (so->so_error) /* connect error - maybe timeout */ return SOCKET_ERROR; } } #endif /* NB_CONNECT */ so->so_error = 0; if ((nam = sockargs (addr, addrlen, MT_SONAME)) == NULL) { so->so_error = ENOMEM; return SOCKET_ERROR; } #ifdef TRACE_DEBUG { struct sockaddr_in *sin = (struct sockaddr_in *)uap->sap; INET_TRACE (INETM_SOCKET, ("INET: connect, port %d addr %lx\n", sin->sin_port, sin->sin_addr.s_addr)); } #endif /* TRACE_DEBUG */ LOCK_NET_RESOURCE(NET_RESID); if ((so->so_error = soconnect (so, nam)) != 0) goto bad; #ifdef NB_CONNECT /* need to test non blocking connect bits after soconnect() call */ if ((so->so_state & SS_NBIO)&& (so->so_state & SS_ISCONNECTING)) { so->so_error = EINPROGRESS; goto bad; } #endif /* NB_CONNECT */ INET_TRACE (INETM_SOCKET, ("INET: connect, so %x so_state %x so_error %d\n", so, so->so_state, so->so_error)); while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) { tcp_sleep ((char *)&so->so_timeo); } bad: if (so->so_error != EINPROGRESS) so->so_state &= ~(SS_ISCONNECTING|SS_WASCONNECTING); m_freem (nam); UNLOCK_NET_RESOURCE(NET_RESID); if (so->so_error) { /* printf("t_connect(): so_error = %d\n", so->so_error);*/ return SOCKET_ERROR; } return 0; }
long t_accept(long s, struct sockaddr * addr, int * addrlen) { #ifdef SOCKDEBUG char logbuf[10]; #endif struct socket * so; struct mbuf * nam; so = LONG2SO(s); SOC_CHECK(so); DOMAIN_CHECK(so, *addrlen); so->so_error = 0; INET_TRACE (INETM_SOCKET, ("INET:accept:so %x so_qlen %d so_state %x\n", so, so->so_qlen, so->so_state)); if ((so->so_options & SO_ACCEPTCONN) == 0) { so->so_error = EINVAL; #ifdef SOCKDEBUG sprintf(logbuf, "t_accept[%d]: %d", __LINE__, so->so_error); glog_with_type(LOG_TYPE_DEBUG, logbuf, 1); #endif return SOCKET_ERROR; } if ((so->so_state & SS_NBIO) && so->so_qlen == 0) { so->so_error = EWOULDBLOCK; #ifdef SOCKDEBUG sprintf(logbuf, "t_accept[%d]: %d", __LINE__, so->so_error); glog_with_type(LOG_TYPE_DEBUG, logbuf, 1); #endif return SOCKET_ERROR; } LOCK_NET_RESOURCE(NET_RESID); while (so->so_qlen == 0 && so->so_error == 0) { if (so->so_state & SS_CANTRCVMORE) { so->so_error = ECONNABORTED; UNLOCK_NET_RESOURCE(NET_RESID); return SOCKET_ERROR; } tcp_sleep ((char *)&so->so_timeo); } if (so->so_error) { #ifdef SOCKDEBUG sprintf(logbuf, "t_accept[%d]: %d", __LINE__, so->so_error); glog_with_type(LOG_TYPE_DEBUG, logbuf, 1); #endif UNLOCK_NET_RESOURCE(NET_RESID); return SOCKET_ERROR; } nam = m_getwithdata (MT_SONAME, sizeof (struct sockaddr)); if (nam == NULL) { UNLOCK_NET_RESOURCE(NET_RESID); so->so_error = ENOMEM; #ifdef SOCKDEBUG sprintf(logbuf, "t_accept[%d]: %d", __LINE__, so->so_error); glog_with_type(LOG_TYPE_DEBUG, logbuf, 1); #endif return SOCKET_ERROR; } { struct socket *aso = so->so_q; if (soqremque (aso, 1) == 0) panic("accept"); so = aso; } (void)soaccept (so, nam); #ifdef TRACE_DEBUG { struct sockaddr_in *sin; sin = mtod(nam, struct sockaddr_in *); INET_TRACE (INETM_SOCKET, ("INET:accept:done so %lx port %d addr %lx\n", so, sin->sin_port, sin->sin_addr.s_addr)); } #endif /* TRACE_INET */ /* return the addressing info in the passed structure */ if (addr != NULL) MEMCPY(addr, nam->m_data, *addrlen); m_freem (nam); UNLOCK_NET_RESOURCE(NET_RESID); SOC_RANGE(so); return SO2LONG(so); }
static int enc_create_h264_stream(const char* name, int vin, int stream, SDK_ENC_H264_STREAM_ATTR_t* stream_attr) { if(stream_attr && vin < HI_VENC_CH_BACKLOG_REF && stream < HI_VENC_STREAM_BACKLOG_REF){ SDK_ENC_H264_STREAM_ATTR_t* this_stream_attr = &_sdk_enc.attr.video_stream_attr[vin][stream]; if(0 == this_stream_attr->magic){ int const venc_group = vin * HI_VENC_STREAM_BACKLOG_REF + stream; int const venc_ch = venc_group; int const vpss_group = vin; int const vpss_ch = (0 == stream) ? VPSS_BSTR_CHN : VPSS_LSTR_CHN; int bps_regular = 0; // hisilicon structure //VPSS_CHN_MODE_S vpss_chn_mode; //VPSS_CHN_ATTR_S vpss_chn_attr; MPP_CHN_S mpp_chn_vpss; MPP_CHN_S mpp_chn_venc; VENC_CHN_ATTR_S venc_chn_attr; VENC_ATTR_H264_REF_MODE_E venc_attr_h264_ref_mode; VENC_ATTR_S* const p_venc_attr = &venc_chn_attr.stVeAttr; // the attribute of video encoder VENC_RC_ATTR_S* const p_venc_rc_attr = &venc_chn_attr.stRcAttr; // the attribute of rate ctrl VENC_ATTR_H264_S* const p_venc_attr_h264 = &p_venc_attr->stAttrH264e; VENC_ATTR_H264_CBR_S* const p_h264_cbr = &p_venc_rc_attr->stAttrH264Cbr; VENC_ATTR_H264_VBR_S* const p_h264_vbr = &p_venc_rc_attr->stAttrH264Vbr; VENC_ATTR_H264_FIXQP_S* const p_h264_fixqp = &p_venc_rc_attr->stAttrH264FixQp; VENC_ATTR_H264_ABR_S* const p_h264_abr = &p_venc_rc_attr->stAttrH264Abr; // only magic is null could be init // init this stream attribute; memcpy(this_stream_attr, stream_attr, sizeof(SDK_ENC_H264_STREAM_ATTR_t)); strncpy(this_stream_attr->name, name, sizeof(this_stream_attr->name)); this_stream_attr->magic = ENC_H264_STREAM_ATTR_MAGIC; this_stream_attr->vin = vin; this_stream_attr->stream = stream; this_stream_attr->start = false; // very important, declare the encode status // Greate Venc Group SOC_CHECK(HI_MPI_VENC_CreateGroup(venc_group)); // Create Venc Channel memset(&venc_chn_attr, 0, sizeof(venc_chn_attr)); p_venc_attr->enType = PT_H264; // must be h264 for this interface p_venc_attr_h264->u32MaxPicWidth = stream_attr->width; p_venc_attr_h264->u32MaxPicHeight = stream_attr->height; p_venc_attr_h264->u32PicWidth = stream_attr->width; // the picture width p_venc_attr_h264->u32PicHeight = stream_attr->height;// the picture height //p_venc_attr_h264->u32BufSize = p_venc_attr_h264->u32MaxPicWidth * p_venc_attr_h264->u32MaxPicHeight * 3 / 2; // stream buffer size p_venc_attr_h264->u32BufSize = p_venc_attr_h264->u32MaxPicWidth * p_venc_attr_h264->u32MaxPicHeight*3/4; // stream buffer size if(0 < (p_venc_attr_h264->u32BufSize%64)) { p_venc_attr_h264->u32BufSize += 64 - (p_venc_attr_h264->u32BufSize%64); } switch(stream_attr->profile){ default: case SDK_ENC_H264_PROFILE_BASELINE: p_venc_attr_h264->u32Profile = 0; break; case SDK_ENC_H264_PROFILE_DEFAULT: case SDK_ENC_H264_PROFILE_MAIN: p_venc_attr_h264->u32Profile = 1; break; } p_venc_attr_h264->bByFrame = HI_TRUE;// get stream mode is slice mode or frame mode p_venc_attr_h264->bField = HI_FALSE; // surpport frame code only for hi3516, bfield = HI_FALSE p_venc_attr_h264->bMainStream = HI_TRUE; // surpport main stream only for hi3516, bMainStream = HI_TRUE p_venc_attr_h264->u32Priority = 0; // channels precedence level. invalidate for hi3516 p_venc_attr_h264->bVIField = HI_FALSE; // the sign of the VI picture is field or frame. Invalidate for hi3516 switch(stream_attr->profile){ case SDK_ENC_H264_PROFILE_BASELINE: { p_venc_attr_h264->u32Profile = 0; break; } case SDK_ENC_H264_PROFILE_DEFAULT: case SDK_ENC_H264_PROFILE_MAIN: { p_venc_attr_h264->u32Profile = 1; break; } } bps_regular = bitrate_regulate(this_stream_attr->width, this_stream_attr->height, this_stream_attr->bps); switch(stream_attr->mode){ case SDK_ENC_H264_MODE_VBR: case SDK_ENC_H264_MODE_AUTO: { venc_chn_attr.stRcAttr.enRcMode = VENC_RC_MODE_H264VBRv2; p_h264_vbr->u32Gop = stream_attr->gop; p_h264_vbr->u32StatTime = (stream_attr->gop + stream_attr->fps - 1) / stream_attr->fps; p_h264_vbr->u32ViFrmRate = stream_attr->vin_fps; p_h264_vbr->fr32TargetFrmRate = (typeof(p_h264_vbr->fr32TargetFrmRate))stream_attr->fps; p_h264_vbr->u32MaxBitRate = bps_regular * 4 /3; p_h264_vbr->u32MinQp = 24; p_h264_vbr->u32MaxQp = 32; break; } case SDK_ENC_H264_MODE_CBR: case SDK_ENC_H264_MODE_ABR: { p_venc_rc_attr->enRcMode = VENC_RC_MODE_H264CBRv2; p_h264_cbr->u32Gop = stream_attr->gop; p_h264_cbr->u32StatTime = (stream_attr->gop + stream_attr->fps - 1) / stream_attr->fps; p_h264_cbr->u32ViFrmRate = stream_attr->vin_fps; p_h264_cbr->fr32TargetFrmRate = (typeof(p_h264_vbr->fr32TargetFrmRate))stream_attr->fps; p_h264_cbr->u32BitRate = bps_regular; p_h264_cbr->u32FluctuateLevel = 0; break; } case SDK_ENC_H264_MODE_FIXQP: { p_venc_rc_attr->enRcMode = VENC_RC_MODE_H264FIXQP; p_h264_fixqp->u32Gop = stream_attr->gop; p_h264_fixqp->u32ViFrmRate = stream_attr->vin_fps; p_h264_fixqp->fr32TargetFrmRate = (typeof(p_h264_vbr->fr32TargetFrmRate))stream_attr->fps; p_h264_fixqp->u32IQp = 20; p_h264_fixqp->u32PQp = 23; break; } default: { SOC_TRACE("H264 encode mode %d not supported!", stream_attr->mode); break; } } SOC_CHECK(HI_MPI_VENC_CreateChn(venc_ch, &venc_chn_attr)); SOC_CHECK(HI_MPI_VENC_RegisterChn(venc_ch, venc_group)); memset(&mpp_chn_vpss, 0, sizeof(mpp_chn_vpss)); memset(&mpp_chn_venc, 0, sizeof(mpp_chn_venc)); // binding venc to vpss mpp_chn_vpss.enModId = HI_ID_VPSS; mpp_chn_vpss.s32DevId = vpss_group; mpp_chn_vpss.s32ChnId = vpss_ch; mpp_chn_venc.enModId = HI_ID_GROUP; mpp_chn_venc.s32DevId = venc_group; mpp_chn_venc.s32ChnId = venc_ch; SOC_CHECK(HI_MPI_SYS_Bind(&mpp_chn_vpss, &mpp_chn_venc)); // set h264 reference mode SOC_CHECK(HI_MPI_VENC_GetH264eRefMode(venc_ch, &venc_attr_h264_ref_mode)); switch(stream_attr->ref_mode) { case SDK_ENC_H264_REF_MODE_1X: venc_attr_h264_ref_mode = H264E_REF_MODE_1X; break; case SDK_ENC_H264_REF_MODE_2X: venc_attr_h264_ref_mode = H264E_REF_MODE_2X; break; case SDK_ENC_H264_REF_MODE_4X: venc_attr_h264_ref_mode = H264E_REF_MODE_4X; break; } SOC_CHECK(HI_MPI_VENC_SetH264eRefMode(venc_ch, venc_attr_h264_ref_mode)); // create a new video encode engine return SDK_SUCCESS; } } return SDK_FAILURE; }
int t_sendto (long s, char * buf, int len, int flags, struct sockaddr * to, int tolen) { struct socket * so; int sendlen; int err; struct mbuf * name; so = LONG2SO(s); SOC_CHECK(so); so->so_error = 0; switch (so->so_type) { case SOCK_STREAM: /* this is a stream socket, so pass this request through * t_send() for its large-send support. */ return t_send(s, buf, len, flags); /*NOTREACHED*/ case SOCK_DGRAM: /* datagram (UDP) socket -- prepare to check length */ sendlen = udp_maxalloc(); break; #ifdef IP_RAW case SOCK_RAW: /* raw socket -- prepare to check length */ sendlen = ip_raw_maxalloc(so->so_options & SO_HDRINCL); break; #endif /* IP_RAW */ default: /* socket has unknown type */ dtrap(); so->so_error = EFAULT; return SOCKET_ERROR; /*NOTREACHED*/ } /* fall through for non-stream sockets: SOCK_DGRAM (UDP) and * SOCK_RAW (raw IP) */ /* check length against underlying stack's maximum */ if (len > sendlen) { so->so_error = EMSGSIZE; return SOCKET_ERROR; } /* if a sockaddr was passed, wrap it in an mbuf and pas it into the * bowels of the BSD code; else assume this is a bound UDP socket * and this call came from t_send() below. */ if (to) /* sockaddr was passed */ { name = sockargs(to, tolen, MT_SONAME); if(name == NULL) { so->so_error = ENOMEM; return SOCKET_ERROR; } } else /* hope user called bind() first... */ name = NULL; sendlen = len; LOCK_NET_RESOURCE(NET_RESID); err = sosend (so, name, buf, &sendlen, flags); if (name) m_freem(name); UNLOCK_NET_RESOURCE(NET_RESID); if (err != 0) { so->so_error = err; return SOCKET_ERROR; } return (len - sendlen); }
int t_send(long s, char * buf, int len, int flags) { struct socket * so; int e; /* error holder */ int total_sent = 0; int maxpkt; int sendlen; int sent; so = LONG2SO(s); #ifdef SOC_CHECK_ALWAYS SOC_CHECK(so); #endif if ((so->so_state & SO_IO_OK) != SS_ISCONNECTED) { so->so_error = EPIPE; return SOCKET_ERROR; } so->so_error = 0; /* If this is not a stream socket, assume it is bound and pass to * t_sendto() with a null sockaddr */ if (so->so_type != SOCK_STREAM) return(t_sendto(s, buf, len, flags, NULL, 0)); maxpkt = TCP_MSS; if(so->so_pcb) { struct tcpcb * tp; tp = intotcpcb(so->so_pcb); /* get tcp structure with mss */ if(tp->t_maxseg) /* Make sure it's set */ maxpkt = tp->t_maxseg; } IN_PROFILER(PF_TCP, PF_ENTRY); /* measure time in TCP */ while (len) { if (len > maxpkt) sendlen = maxpkt; /* take biggest block we can */ else sendlen = len; sent = sendlen; LOCK_NET_RESOURCE(NET_RESID); e = sosend (so, NULL, buf, &sendlen, flags); UNLOCK_NET_RESOURCE(NET_RESID); if (e != 0) /* sock_sendit failed? */ { /* if we simply ran out of bufs, report back to caller. */ if ((e == ENOBUFS) || (e == EWOULDBLOCK)) { /* if we actually sent something before running out * of buffers, report what we sent; * else, report the error and let the application * retry the call later */ if (total_sent != 0) { so->so_error = 0; break; /* break out of while(len) loop */ } } so->so_error = e; return SOCKET_ERROR; } /* if we can't send anymore, return now */ if (sendlen != 0) break; /* break out of while(len) loop */ /* adjust numbers & pointers, and go do next send loop */ sent -= sendlen; /* subtract anything that didn't get sent */ buf += sent; len -= sent; total_sent += sent; } IN_PROFILER(PF_TCP, PF_EXIT); /* measure time in TCP */ return total_sent; }
static int vin_capture(int vin, FILE* bitmap_stream) { int ret = 0; int api_ret = SDK_FAILURE; if(vin < HI3520A_VIN_CH_BACKLOG_REF){ int frame_depth = 0; VIDEO_FRAME_INFO_S video_frame_info; SOC_CHECK(HI_MPI_VI_GetFrameDepth(vin, &frame_depth)); if(0 == frame_depth){ HI_MPI_VI_SetFrameDepth(vin, 1); } if(HI_SUCCESS == HI_MPI_VI_GetFrame(vin, &video_frame_info)){ if(PIXEL_FORMAT_YUV_SEMIPLANAR_420 == video_frame_info.stVFrame.enPixelFormat){ int i = 0, ii = 0; int const width = video_frame_info.stVFrame.u32Width; int const height = video_frame_info.stVFrame.u32Height; size_t const stride = video_frame_info.stVFrame.u32Stride[0]; size_t const yuv420_size = stride * height * 3 / 2; const uint8_t* yuv420_data = HI_MPI_SYS_Mmap(video_frame_info.stVFrame.u32PhyAddr[0], yuv420_size); // allocate the bitmap data size_t bitmap24_size = width * height * 3; uint8_t* bitmap24_data = alloca(bitmap24_size); uint8_t* bitmap24_pixel = bitmap24_data; SOC_TRACE("Frame(%d) [%dx%d] stride %d", vin, width, height, stride); if(yuv420_data){ int y, u, v, yy, vr, ug, vg, ub; int r, g, b; const uint8_t *py = (uint8_t*)(yuv420_data); const uint8_t *puv = (uint8_t*)(py + width * height); // yuv420 to rgb888 for(i = 0; i < height; ++i){ for(ii = 0; ii < width; ++ii){ y = py[0]; yy = y * 256; u = puv[1] - 128; ug = 88 * u; ub = 454 * u; v = puv[0] - 128; vg = 183 * v; vr = 359 * v; /////////////////////////////////// // convert r = (yy + vr) >> 8; g = (yy - ug - vg) >> 8; b = (yy + ub) >> 8; if(r < 0){ r = 0; } if(r > 255){ r = 255; } if(g < 0){ g = 0; } if(g > 255){ g = 255; } if(b < 0){ b = 0; } if(b > 255){ b = 255; } *bitmap24_pixel++ = (uint8_t)b; *bitmap24_pixel++ = (uint8_t)g; *bitmap24_pixel++ = (uint8_t)r; //SOC_TRACE("RGB[%3d,%3d,%3d] @ (%3d,%3d)", r, g, b, ii, i); /////////////////////////////////// ++py; if(0 != (ii % 2)){ // even puv += 2; } } if(0 != (i % 2)){ // try twice puv -= width; } } SOC_CHECK(HI_MPI_SYS_Munmap(yuv420_data, yuv420_size)); if(0 == fseek(bitmap_stream, 0, SEEK_SET)){ BIT_MAP_FILE_HEADER_t bmp_header; memset(&bmp_header, 0, sizeof(bmp_header)); bmp_header.type[0] = 'B'; bmp_header.type[1] = 'M'; bmp_header.file_size = sizeof(bmp_header) + bitmap24_size; bmp_header.reserved_zero = 0; bmp_header.off_bits = sizeof(bmp_header); bmp_header.info_size = 40; bmp_header.width = width; bmp_header.height = height; bmp_header.planes = 1; bmp_header.bit_count = 24; bmp_header.compression = 0; bmp_header.size_image = bitmap24_size; bmp_header.xpels_per_meter = 0; bmp_header.ypels_per_meter = 0; bmp_header.clr_used = 0; bmp_header.clr_important = 0; fwrite(&bmp_header, 1, sizeof(bmp_header), bitmap_stream); for(i = 0; i < height; ++i){ void* bitmap_offset = bitmap24_data + (height - i - 1) * width * 3; fwrite(bitmap_offset, 1, width * 3, bitmap_stream); } api_ret = SDK_SUCCESS; } } } SOC_CHECK(HI_MPI_VI_ReleaseFrame(vin, &video_frame_info)); }