static int handle_send(struct genl_info *info) { char *filename; struct nlattr *attr; int error; if (verify_superpriv()) return -EPERM; attr = info->attrs[ATTR_FILENAME]; if (!attr) { log_err("Request lacks a file name."); return -EINVAL; } filename = nla_data(attr); attr = info->attrs[ATTR_PKT]; if (!attr) { log_err("Request lacks a packet."); return -EINVAL; } error = sender_send(filename, nla_data(attr), nla_len(attr)); return genl_respond(info, error); }
/** * Used to handle keyboard input. * Implement any additional commands here. */ void l5_handle_keyboard(void) { char buffer[1024]; char *retval; retval = fgets(buffer, sizeof(buffer), stdin); if(retval != 0) { buffer[strlen(buffer)-1] = 0; /* debug */ fprintf(stderr, "The buffer contains: >>%s<<\n", buffer); if(strstr(buffer, "CONNECT") != NULL) { char hostname[1024] = {0}; int port; int device; /* * sscanf tries to find the pattern in the buffer, and extracts * the parts into the given variables. */ int ret = sscanf(buffer, "CONNECT %s %d", hostname, &port); if(ret == 2) { /* two matches, we got our hostname and port */ device = l1_connect(hostname, port); if(device < 0) { fprintf(stderr, "Error connecting to host:port %s:%d.\n", hostname, port); return; } fprintf(stderr, "Physical connection to host:port %s:%d has device number %d\n", hostname, port, device); } }else if(strstr(buffer, "SEND") != NULL) { char filename[100]; int device; int ret = sscanf(buffer, "SEND %s %d", filename, &device); if(ret == 2) { //We got our filename and device to send the file //read file sender_init(filename, device); sender_send(); } }else if(strstr(buffer, "QUIT") != NULL) { // Gracefully shut down l2_shutdown(); delayed_send_shutdown(); exit(EXIT_SUCCESS); } } }
int main (int argc, char **argv) { void *capture = capture_open("/dev/video0", VIDEO_WIDTH, VIDEO_HEIGHT, PIX_FMT_YUV420P); if (!capture) { fprintf(stderr, "ERR: can't open '/dev/video0'\n"); exit(-1); } void *encoder = vc_open(VIDEO_WIDTH, VIDEO_HEIGHT, VIDEO_FPS); if (!encoder) { fprintf(stderr, "ERR: can't open x264 encoder\n"); exit(-1); } void *sender = sender_open(TARGET_IP, TARGET_PORT); if (!sender) { fprintf(stderr, "ERR: can't open sender for %s:%d\n", TARGET_IP, TARGET_PORT); exit(-1); } int tosleep = 1000000 / VIDEO_FPS; FILE *fp_save = fopen("./my1.264", "wb"); for (; ; ) { // 抓 Picture pic; capture_get_picture(capture, &pic); // 压 const void *outdata; int outlen; int rc = vc_compress(encoder, pic.data, pic.stride, &outdata, &outlen); if (rc < 0) continue; // 发 sender_send(sender, outdata, outlen); fwrite(outdata, 1, outlen, fp_save); // 等 //usleep(tosleep); } fclose(fp_save); sender_close(sender); vc_close(encoder); capture_close(capture); return 0; }