static void appExit() { printf("##appExit\n"); //if (bHasAudio) { audio_stop(); audio_close(); printf("audio_close\n"); } close_encode(); audio_dec_exit(); video_process_stop(); usleep(200 * 1000); camera_close(); printf("camera_close\n"); encode_close(); printf("encode_close\n"); mux_exit(); printf("mux_close\n"); encode_destroy(); akuio_pmem_fini(); setled_off(); PTZControlDeinit(); printf("akuio_pmem_fini\n"); record_rename_file(); }
/** * @brief open vedio encoder * * @author dengzhou * @date 2013-04-07 * @param[in] * @return T_S32 * @retval if return 0 success, otherwise failed */ int encode_open(T_ENC_INPUT *pEncIn1, T_ENC_INPUT *pEncIn2) { T_U32 temp; if (pEncIn1) { videoEnc[eCHAN_UNI].pBuff = akuio_alloc_pmem(ENCMEM); if (AK_NULL == videoEnc[eCHAN_UNI].pBuff) { return -1; } temp = akuio_vaddr2paddr(videoEnc[eCHAN_UNI].pBuff) & 7; //编码buffer 起始地址必须8字节对齐 videoEnc[eCHAN_UNI].pPhysBuff = ((T_U8 *)videoEnc[eCHAN_UNI].pBuff) + ((8-temp)&7); if (!(videoEnc[eCHAN_UNI].hVS = encode_openLib(pEncIn1))) { encode_close(eCHAN_UNI); printf("Open EncodeLib Failure\n"); return -1; } } if (pEncIn2) { videoEnc[eCHAN_DUAL].pBuff = akuio_alloc_pmem(ENCMEM); if (AK_NULL == videoEnc[eCHAN_DUAL].pBuff) { return -1; } temp = akuio_vaddr2paddr(videoEnc[eCHAN_DUAL].pBuff) & 7; //编码buffer 起始地址必须8字节对齐 videoEnc[eCHAN_DUAL].pPhysBuff = ((T_U8 *)videoEnc[eCHAN_DUAL].pBuff) + ((8-temp)&7); if (!(videoEnc[eCHAN_DUAL].hVS = encode_openLib(pEncIn2))) { encode_close(eCHAN_DUAL); encode_close(eCHAN_UNI); return -1; } } return 0; }
int main(int argc, char *argv[]) { if (argc < 3) { printf("Usage: #simple_demo ip port\n"); return -1; } struct cap_handle *caphandle = NULL; struct cvt_handle *cvthandle = NULL; struct enc_handle *enchandle = NULL; struct pac_handle *pachandle = NULL; struct net_handle *nethandle = NULL; struct cap_param capp; struct cvt_param cvtp; struct enc_param encp; struct pac_param pacp; struct net_param netp; // set paraments U32 vfmt = V4L2_PIX_FMT_YUYV; U32 ofmt = V4L2_PIX_FMT_YUV420; capp.dev_name = "/dev/video0"; capp.width = WIDTH; capp.height = HEIGHT; capp.pixfmt = vfmt; capp.rate = FRAMERATE; cvtp.inwidth = WIDTH; cvtp.inheight = HEIGHT; cvtp.inpixfmt = vfmt; cvtp.outwidth = WIDTH; cvtp.outheight = HEIGHT; cvtp.outpixfmt = ofmt; encp.src_picwidth = WIDTH; encp.src_picheight = HEIGHT; encp.enc_picwidth = WIDTH; encp.enc_picheight = HEIGHT; encp.chroma_interleave = 0; encp.fps = FRAMERATE; encp.gop = 30; encp.bitrate = 800; pacp.max_pkt_len = 1400; pacp.ssrc = 10; netp.type = UDP; netp.serip = argv[1]; netp.serport = atoi(argv[2]); caphandle = capture_open(capp); if (!caphandle) { printf("--- Open capture failed\n"); return -1; } cvthandle = convert_open(cvtp); if (!cvthandle) { printf("--- Open convert failed\n"); return -1; } enchandle = encode_open(encp); if (!enchandle) { printf("--- Open encode failed\n"); return -1; } pachandle = pack_open(pacp); if (!pachandle) { printf("--- Open pack failed\n"); return -1; } nethandle = net_open(netp); if (!nethandle) { printf("--- Open network failed\n"); return -1; } // start stream loop int ret; void *cap_buf, *cvt_buf, *hd_buf, *enc_buf, *pac_buf; int cap_len, cvt_len, hd_len, enc_len, pac_len; enum pic_t ptype; unsigned long framecount = 0; capture_start(caphandle); // !!! need to start capture stream! while (1) { ret = capture_get_data(caphandle, &cap_buf, &cap_len); if (ret != 0) { if (ret < 0) // error { printf("--- capture_get_data failed\n"); break; } else // again { usleep(10000); continue; } } if (cap_len <= 0) { printf("!!! No capture data\n"); continue; } // else ret = convert_do(cvthandle, cap_buf, cap_len, &cvt_buf, &cvt_len); if (ret < 0) { printf("--- convert_do failed\n"); break; } if (cvt_len <= 0) { printf("!!! No convert data\n"); continue; } // else // fetch h264 headers first! while ((ret = encode_get_headers(enchandle, &hd_buf, &hd_len, &ptype)) == 1) { //fwrite(hd_buf, 1, hd_len, dumpfile); pack_put(pachandle, hd_buf, hd_len); while (pack_get(pachandle, &pac_buf, &pac_len) == 1) { ret = net_send(nethandle, pac_buf, pac_len); if (ret != pac_len) { printf("send pack data failed, size: %d, err: %s\n", pac_len, strerror(errno)); } } } ret = encode_do(enchandle, cvt_buf, cvt_len, &enc_buf, &enc_len, &ptype); if (ret < 0) { printf("--- encode_do failed\n"); break; } if (enc_len <= 0) { printf("!!! No encode data\n"); continue; } // else //fwrite(enc_buf, 1, enc_len, dumpfile); // RTP pack and send pack_put(pachandle, enc_buf, enc_len); while (pack_get(pachandle, &pac_buf, &pac_len) == 1) { ret = net_send(nethandle, pac_buf, pac_len); if (ret != pac_len) { printf("send pack failed, size: %d, err: %s\n", pac_len, strerror(errno)); } } framecount++; } capture_stop(caphandle); net_close(nethandle); pack_close(pachandle); encode_close(enchandle); convert_close(cvthandle); capture_close(caphandle); return 0; }
int main(int argc, char **argv) { char logpath[FILENAME_MAX]; int log; if (argc != 2) { fprintf(stderr, PACKAGE_STRING "\n" " (c) Copyright 2001-2004 The IceS Development Team <*****@*****.**>\n" " Michael Smith <*****@*****.**>\n" " Karl Heyes <*****@*****.**>\n" " and others\n" "\n" "Usage: \"ices config.xml\"\n"); return 1; } config_initialize(); if (config_read(argv[1]) <= 0) { fprintf(stderr, "Failed to read config file \"%s\"\n", argv[1]); goto fail; } if (ices_config->background) { #ifndef _WIN32 int ret = 0; /* Start up new session, to lose old session and process group */ switch (fork()) { case 0: break; /* child continues */ case -1: perror ("fork"); ret = -1; default: exit (ret); } /* Disassociate process group and controlling terminal */ setsid(); /* Become a NON-session leader so that a */ /* control terminal can't be reacquired */ switch (fork()) { case 0: break; /* child continues */ case -1: perror ("fork"); ret = -1; default: exit (ret); } #else FreeConsole(); #endif } log_initialize(); thread_initialize(); shout_init(); encode_init(); #ifndef _WIN32 signals_setup(); #endif snprintf(logpath, FILENAME_MAX, "%s/%s", ices_config->logpath, ices_config->logfile); if(ices_config->log_stderr) log = log_open_file(stderr); else { log = log_open(logpath); if (log < 0) fprintf (stderr, "unable to open log %s\n", logpath); log_set_trigger (log, ices_config->logsize); } /* Set the log level, if requested - defaults to 2 (WARN) otherwise */ if (ices_config->loglevel) log_set_level(log, ices_config->loglevel); ices_config->log_id = log; LOG_INFO0(PACKAGE_STRING " started..."); if (ices_config->pidfile != NULL) { FILE *f = fopen (ices_config->pidfile, "w"); if (f) { fprintf (f, "%i", getpid()); fclose (f); } else { LOG_WARN1("pidfile \"%s\" cannot be written to", ices_config->pidfile); xmlFree (ices_config->pidfile); ices_config->pidfile = NULL; } } /* Start the core streaming loop */ input_loop(); if (ices_config->pidfile) remove (ices_config->pidfile); LOG_INFO0("Shutdown complete"); log_close(log); fail: encode_close(); shout_shutdown(); config_shutdown(); thread_shutdown(); log_shutdown(); return 0; }
int main(int argc, char *argv[]) { struct cap_handle *caphandle = NULL; struct cvt_handle *cvthandle = NULL; struct enc_handle *enchandle = NULL; struct pac_handle *pachandle = NULL; struct net_handle *nethandle = NULL; struct cap_param capp; struct cvt_param cvtp; struct enc_param encp; struct pac_param pacp; struct net_param netp; int stage = 0b00000011; U32 vfmt = V4L2_PIX_FMT_YUYV; U32 ofmt = V4L2_PIX_FMT_YUV420; // set default values capp.dev_name = "/dev/video0"; capp.width = 640; capp.height = 480; capp.pixfmt = vfmt; capp.rate = 15; cvtp.inwidth = 640; cvtp.inheight = 480; cvtp.inpixfmt = vfmt; cvtp.outwidth = 640; cvtp.outheight = 480; cvtp.outpixfmt = ofmt; encp.src_picwidth = 640; encp.src_picheight = 480; encp.enc_picwidth = 640; encp.enc_picheight = 480; encp.chroma_interleave = 0; encp.fps = 15; encp.gop = 12; encp.bitrate = 1000; pacp.max_pkt_len = 1400; pacp.ssrc = 1234; netp.serip = NULL; netp.serport = -1; netp.type = UDP; char *outfile = NULL; // options int opt = 0; static const char *optString = "?di:o:a:p:w:h:r:f:t:g:s:c:"; opt = getopt(argc, argv, optString); while (opt != -1) { int fmt; switch (opt) { case '?': display_usage(); return 0; case 'd': debug = 1; break; case 's': stage = atoi(optarg); break; case 'i': capp.dev_name = optarg; break; case 'o': outfile = optarg; break; case 'a': netp.serip = optarg; break; case 'p': netp.serport = atoi(optarg); break; case 'c': fmt = atoi(optarg); if (fmt == 1) capp.pixfmt = V4L2_PIX_FMT_YUV420; else capp.pixfmt = V4L2_PIX_FMT_YUYV; break; case 'w': capp.width = cvtp.inwidth = cvtp.outwidth = encp.src_picwidth = encp.enc_picwidth = atoi(optarg); break; case 'h': capp.height = cvtp.inheight = cvtp.outheight = encp.src_picheight = encp.enc_picheight = atoi(optarg); break; case 'r': encp.bitrate = atoi(optarg); break; case 'f': capp.rate = encp.fps = atoi(optarg); break; case 't': encp.chroma_interleave = atoi(optarg); break; case 'g': encp.gop = atoi(optarg); break; default: printf("Unknown option: %s\n", optarg); display_usage(); return -1; } opt = getopt(argc, argv, optString); } if (outfile) outfd = fopen(outfile, "wb"); signal(SIGINT, quit_func); caphandle = capture_open(capp); if (!caphandle) { printf("--- Open capture failed\n"); return -1; } if ((stage & 0b00000001) != 0) { cvthandle = convert_open(cvtp); if (!cvthandle) { printf("--- Open convert failed\n"); return -1; } } if ((stage & 0b00000010) != 0) { enchandle = encode_open(encp); if (!enchandle) { printf("--- Open encode failed\n"); return -1; } } if ((stage & 0b00000100) != 0) { pachandle = pack_open(pacp); if (!pachandle) { printf("--- Open pack failed\n"); return -1; } } if ((stage & 0b00001000) != 0) { if (netp.serip == NULL || netp.serport == -1) { printf( "--- Server ip and port must be specified when using network\n"); return -1; } nethandle = net_open(netp); if (!nethandle) { printf("--- Open network failed\n"); return -1; } } // start capture encode loop int ret; void *cap_buf, *cvt_buf, *hd_buf, *enc_buf; char *pac_buf = (char *) malloc(MAX_RTP_SIZE); int cap_len, cvt_len, hd_len, enc_len, pac_len; enum pic_t ptype; struct timeval ctime, ltime; unsigned long fps_counter = 0; int sec, usec; double stat_time = 0; capture_start(caphandle); // !!! need to start capture stream! quit = 0; gettimeofday(<ime, NULL); while (!quit) { if (debug) // print fps { gettimeofday(&ctime, NULL); sec = ctime.tv_sec - ltime.tv_sec; usec = ctime.tv_usec - ltime.tv_usec; if (usec < 0) { sec--; usec = usec + 1000000; } stat_time = (sec * 1000000) + usec; // diff in microsecond if (stat_time >= 1000000) // >= 1s { printf("\n*** FPS: %ld\n", fps_counter); fps_counter = 0; ltime = ctime; } fps_counter++; } ret = capture_get_data(caphandle, &cap_buf, &cap_len); if (ret != 0) { if (ret < 0) // error { printf("--- capture_get_data failed\n"); break; } else // again { usleep(10000); continue; } } if (cap_len <= 0) { printf("!!! No capture data\n"); continue; } if (debug) fputc('.', stdout); if ((stage & 0b00000001) == 0) // no convert, capture only { if (outfd) fwrite(cap_buf, 1, cap_len, outfd); continue; } // convert if (capp.pixfmt == V4L2_PIX_FMT_YUV420) // no need to convert { cvt_buf = cap_buf; cvt_len = cap_len; } else // do convert: YUYV => YUV420 { ret = convert_do(cvthandle, cap_buf, cap_len, &cvt_buf, &cvt_len); if (ret < 0) { printf("--- convert_do failed\n"); break; } if (cvt_len <= 0) { printf("!!! No convert data\n"); continue; } } if (debug) fputc('-', stdout); if ((stage & 0b00000010) == 0) // no encode { if (outfd) fwrite(cvt_buf, 1, cvt_len, outfd); continue; } // encode // fetch h264 headers first! while ((ret = encode_get_headers(enchandle, &hd_buf, &hd_len, &ptype)) != 0) { if (debug) fputc('S', stdout); if ((stage & 0b00000100) == 0) // no pack { if (outfd) fwrite(hd_buf, 1, hd_len, outfd); continue; } // pack headers pack_put(pachandle, hd_buf, hd_len); while (pack_get(pachandle, pac_buf, MAX_RTP_SIZE, &pac_len) == 1) { if (debug) fputc('#', stdout); if ((stage & 0b00001000) == 0) // no network { if (outfd) fwrite(pac_buf, 1, pac_len, outfd); continue; } // network ret = net_send(nethandle, pac_buf, pac_len); if (ret != pac_len) { printf("send pack failed, size: %d, err: %s\n", pac_len, strerror(errno)); } if (debug) fputc('>', stdout); } } ret = encode_do(enchandle, cvt_buf, cvt_len, &enc_buf, &enc_len, &ptype); if (ret < 0) { printf("--- encode_do failed\n"); break; } if (enc_len <= 0) { printf("!!! No encode data\n"); continue; } if (debug) { char c; switch (ptype) { case PPS: c = 'S'; break; case SPS: c = 'S'; break; case I: c = 'I'; break; case P: c = 'P'; break; case B: c = 'B'; break; default: c = 'N'; break; } fputc(c, stdout); } if ((stage & 0b00000100) == 0) // no pack { if (outfd) fwrite(enc_buf, 1, enc_len, outfd); continue; } // pack pack_put(pachandle, enc_buf, enc_len); while (pack_get(pachandle, pac_buf, MAX_RTP_SIZE, &pac_len) == 1) { if (debug) fputc('#', stdout); if ((stage & 0b00001000) == 0) // no network { if (outfd) fwrite(pac_buf, 1, pac_len, outfd); continue; } // network ret = net_send(nethandle, pac_buf, pac_len); if (ret != pac_len) { printf("send pack failed, size: %d, err: %s\n", pac_len, strerror(errno)); } if (debug) fputc('>', stdout); } } capture_stop(caphandle); free(pac_buf); if ((stage & 0b00001000) != 0) net_close(nethandle); if ((stage & 0b00000100) != 0) pack_close(pachandle); if ((stage & 0b00000010) != 0) encode_close(enchandle); if ((stage & 0b00000001) != 0) convert_close(cvthandle); capture_close(caphandle); if (outfd) fclose(outfd); return 0; }