private void submitFrame() { if (!broadcastController.getIsBroadcasting() || broadcastController.getIsPaused()) { return; } long curTime = System.nanoTime(); long nanoPerFrame = 1000000000 / broadcastFramesPerSecond; // If you send frames too quickly to the SDK (based on the broadcast FPS you configured) it will // not be able // to make use of them all. In that case, it will simply release buffers without using them // which means the // game wasted time doing the capture. To mitigate this, the app should pace the captures to // the broadcast FPS. long captureDelta = curTime - lastCaptureTime; boolean isTimeForNextCapture = captureDelta >= nanoPerFrame; if (!isTimeForNextCapture) { return; } FrameBuffer buffer = broadcastController.getNextFreeBuffer(); broadcastController.captureFrameBuffer_ReadPixels(buffer); broadcastController.submitFrame(buffer); lastCaptureTime = curTime; }
private void handleInput() { while (Keyboard.next()) { if (Keyboard.isRepeatEvent()) { continue; } if (!Keyboard.getEventKeyState()) { continue; } if (Keyboard.getEventKey() == Keyboard.KEY_A) { broadcastController.requestAuthToken(username, password); } else if (Keyboard.getEventKey() == Keyboard.KEY_S) { broadcastController.setStreamInfo(username, "Java Game", "Fun times"); } else if (Keyboard.getEventKey() == Keyboard.KEY_P) { if (broadcastController.getIsPaused()) { broadcastController.resumeBroadcasting(); } else { broadcastController.pauseBroadcasting(); } } else if (Keyboard.getEventKey() == Keyboard.KEY_SPACE) { if (broadcastController.getIsBroadcasting()) { broadcastController.stopBroadcasting(); } else { VideoParams videoParams = broadcastController.getRecommendedVideoParams( Display.getDisplayMode().getWidth(), Display.getDisplayMode().getHeight(), broadcastFramesPerSecond); videoParams.verticalFlip = true; broadcastController.startBroadcasting(videoParams); } } else if (Keyboard.getEventKey() == Keyboard.KEY_R) { broadcastController.runCommercial(); } else if (Keyboard.getEventKey() == Keyboard.KEY_I) { if (ingestTester != null) { broadcastController.cancelIngestTest(); ingestTester = null; } else { ingestTester = broadcastController.startIngestTest(); ingestTester.setListener(this); } } else if (Keyboard.getEventKey() == Keyboard.KEY_G) { broadcastController.requestGameNameList("final"); } else if (Keyboard.getEventKey() == Keyboard.KEY_1) { broadcastController.sendActionMetaData( "TestAction", broadcastController.getCurrentBroadcastTime(), "Something cool happened", "{ \"MyValue\" : \"42\" }"); } else if (Keyboard.getEventKey() == Keyboard.KEY_2) { if (metaDataSpanSequenceId == -1) { metaDataSpanSequenceId = broadcastController.startSpanMetaData( "TestSpan", broadcastController.getCurrentBroadcastTime(), "Something cool just started happening", "{ \"MyValue\" : \"42\" }"); } else { broadcastController.endSpanMetaData( "TestSpan", broadcastController.getCurrentBroadcastTime(), metaDataSpanSequenceId, "Something cool just stopped happening", "{ \"MyValue\" : \"42\" }"); metaDataSpanSequenceId = -1; } } else if (Keyboard.getEventKey() == Keyboard.KEY_C) { if (chatController != null) { if (chatController.getIsConnected()) { chatController.disconnect(); } else { chatController.connect(username); } } } else if (Keyboard.getEventKey() == Keyboard.KEY_V) { if (chatController != null) { if (chatController.getIsConnected()) { chatController.disconnect(); } else { chatController.connectAnonymous(username); } } } else if (Keyboard.getEventKey() == Keyboard.KEY_M) { if (chatController != null) { if (chatController.getIsConnected()) { chatController.sendChatMessage("Test chat message: " + System.currentTimeMillis()); } } } } }