void CX264Encoder::encode() { //MessageBox(NULL, TEXT("1"), TEXT("remote preview"), MB_OK); if (x264_picture_alloc(&pic, param.i_csp, param.i_width, param.i_height) < 0){ return; } //MessageBox(NULL, TEXT("2"), TEXT("remote preview"), MB_OK); while (!b_stop){ uv_mutex_lock(&queue_mutex); if (sample_queue.size() == 0){ //uv_mutex_unlock(&queue_mutex); int ret = uv_cond_timedwait(&queue_not_empty, &queue_mutex, (uint64_t)(5000 * 1e6)); if (ret == UV_ETIMEDOUT){ uv_mutex_unlock(&queue_mutex); MessageBox(NULL, TEXT("BUFFER EMPTY!"), TEXT("remote preview"), MB_OK); return; } } cc_src_sample_t buf = sample_queue.front(); pic.i_pts = i_frame++; int64_t ticks_per_frame = buf.end - buf.start; TCHAR m[248]; swprintf(m, TEXT("len: %d; width: %d; height: %d"), buf.len, param.i_width, param.i_height); //MessageBox(NULL, m, TEXT("remote preview"), MB_OK); if (param.i_csp == X264_CSP_RGB){ convert_rgb(pic.img.plane[0], buf.buf[0], param.i_width, param.i_height); //memcpy(pic.img.plane[0], buf.buf[0], buf.line[0]*param.i_height); } else { //for (int i = 0; i < buf.iplan; i++){ // memcpy(pic.img.plane[i], buf.buf[i], buf.line[i]); //} memcpy(pic.img.plane[0], buf.buf[0], buf.line[0] * param.i_height); memcpy(pic.img.plane[1], buf.buf[1], buf.line[1] * param.i_height / 2); memcpy(pic.img.plane[2], buf.buf[2], buf.line[2] * param.i_height / 2); } //MessageBox(NULL, TEXT("3"), TEXT("remote preview"), MB_OK); i_frame_size = x264_encoder_encode(h, &nal, &i_nal, &pic, &pic_out); //MessageBox(NULL, TEXT("4"), TEXT("remote preview"), MB_OK); if (i_frame_size < 0) break; else if (i_frame_size) { //TODO:encode success if (fn_cb){ int i; for (i = 0; i < i_nal; i++){ cc_src_sample_t out_smple; out_smple.sync = !!pic_out.b_keyframe; out_smple.start = pic_out.i_pts; out_smple.end = pic_out.i_pts + ticks_per_frame; out_smple.buf[0] = (uint8_t*)malloc(nal[i].i_payload); out_smple.len = nal[i].i_payload; out_smple.iplan = 1; memcpy(out_smple.buf[0], nal[i].p_payload, nal[i].i_payload); //MessageBox(NULL, TEXT("5"), TEXT("remote preview"), MB_OK); fn_cb(out_smple, p_user_data); if (!fd){ fd = fopen("src.264", "wb"); if (fd) ;// MessageBox(NULL, TEXT("create file!"), TEXT("x264 encoder"), MB_OK); else MessageBox(NULL, TEXT("create file false!"), TEXT("x264 encoder"), MB_OK); } if (fd){ fwrite(out_smple.buf[0], 1, out_smple.len, fd); } free(out_smple.buf[0]); } } } //MessageBox(NULL, TEXT("6"), TEXT("remote preview"), MB_OK); sample_queue.pop_front(); for (int i = 0; i < buf.iplan; i++){ free(buf.buf[i]); } uv_mutex_unlock(&queue_mutex); } while (encode_delay()){ i_frame_size = x264_encoder_encode(h, &nal, &i_nal, NULL, &pic_out); if (i_frame_size < 0) break; else if (i_frame_size) { //TODO:encode success } } }
void calculate_presses(int (*fn_cb)(void)) { int usb_pressed_count = 0; int pressed_fn = 0; int pressed_shift = 0; //memset(scancode_presses, 0, sizeof(scancode_presses)); for (int i=0; i<pressed_count; i++) { switch (layer[presses[i]]) { case KEYBOARD_FN: pressed_fn = 1; break; case KEYBOARD_LEFT_SHIFT: case KEYBOARD_RIGHT_SHIFT: pressed_shift = 1; break; } } if (pressed_fn) { // fn callbacks // - restart for firmware upload // - layout switching // - fn macros memset(scancode_presses, 0, sizeof(scancode_presses)); for (int i=0; i<pressed_count; i++) { int keycode = layer[presses[i]]; scancode_presses[keycode] = 1; } if (fn_cb()) { // if fn callback triggers, stop processing keys _delay_ms(50); return; } } for (int i=0; i<pressed_count; i++) { int keycode = 0; if (pressed_fn) { keycode = layer_fn[presses[i]]; } else if (pressed_shift) { keycode = layer_shift[presses[i]]; if (keycode == 0) { // no keycode found in shift layer, read keycode from the default layer keycode = layer[presses[i]]; } } else { keycode = layer[presses[i]]; } if (keycode == 0) { // no key assigned to the keypress continue; } if (keycode == KEYBOARD_FN) { // already handled, do nothing continue; } // process modifiers if (keycode == KEYBOARD_LEFT_CTRL) { keyboard_modifier_keys |= KEY_LEFT_CTRL; } else if (keycode == KEYBOARD_RIGHT_CTRL) { keyboard_modifier_keys |= KEY_RIGHT_CTRL; } else if (keycode == KEYBOARD_LEFT_SHIFT) { keyboard_modifier_keys |= KEY_LEFT_SHIFT; } else if (keycode == KEYBOARD_RIGHT_SHIFT) { keyboard_modifier_keys |= KEY_RIGHT_SHIFT; } else if (keycode == KEYBOARD_LEFT_ALT) { keyboard_modifier_keys |= KEY_LEFT_ALT; } else if (keycode == KEYBOARD_RIGHT_ALT) { keyboard_modifier_keys |= KEY_RIGHT_ALT; } else if (keycode == KEYBOARD_LEFT_GUI) { keyboard_modifier_keys |= KEY_LEFT_GUI; } else if (keycode == KEYBOARD_RIGHT_GUI) { keyboard_modifier_keys |= KEY_RIGHT_GUI; } else if (usb_pressed_count < 6) { // keypress keyboard_keys[usb_pressed_count++] = keycode; } } }