static PyObject * c_set_instance(PyObject *self, PyObject *args, PyObject *kwargs) { struct pack *p; int ret; PyObject *key, *value; char *str; size_t strlen; int pos = 0; proc_check_shutdown(); p = pack_new(512); if (!p) return NULL; while(PyDict_Next(kwargs, &pos, &key, &value)) { PyString_AsStringAndSize(key, &str, &strlen); pack_put(p, str, strlen); PyString_AsStringAndSize(value, &str, &strlen); pack_put(p, str, strlen); } ret = db_put_profile(bk_node, bk_app, p); if (ret != 0) return NULL; Py_INCREF(Py_None); return Py_None; }
static int send_result(int cmd, const char *data, size_t size) { struct ipc_struct ipc; struct pack *p; memset(&ipc, 0, sizeof(struct ipc_struct)); ipc.source = bk_channel; p = pack_new(128); if (CMD_RESULT == cmd) { if (bk_app) pack_put(p, bk_app, strlen(bk_app)); else pack_put(p, "comar", 5); } if (data) { if (size == 0) size = strlen(data); pack_put(p, data, size); } proc_put(TO_PARENT, cmd, &ipc, p); return 0; }
static PyObject * c_call(PyObject *self, PyObject *args) { struct ipc_struct ipc; struct pack *p; char *node; char *pak; int nd; if (!PyArg_ParseTuple(args, "ss", &node, &pak)) return NULL; nd = model_lookup_method(node); if (nd == -1) return NULL; memset(&ipc, 0, sizeof(struct ipc_struct)); ipc.node = nd; p = pack_new(128); pack_put(p, pak, strlen(pak)); job_start(CMD_CALL_PACKAGE, &ipc, p); while (1) { struct ProcChild *sender; int cmd; int size; if (1 == proc_listen(&sender, &cmd, &size, 1)) { switch (cmd) { case CMD_RESULT: case CMD_FAIL: case CMD_ERROR: case CMD_NONE: proc_get(sender, &ipc, p, size); break; case CMD_NOTIFY: proc_get(sender, &ipc, p, size); proc_put(TO_PARENT, cmd, &ipc, p); break; } if (cmd == CMD_FINISH) break; } } Py_INCREF(Py_None); return Py_None; }
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[]) { 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; }