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 shutdownBroadcasting() { if (broadcastController == null) { return; } broadcastController.shutdownTwitch(); broadcastController.setListener(null); }
private void initBroadcasting() { if (broadcastController != null) { return; } broadcastController = new BroadcastController(); broadcastController.setListener(this); broadcastController.setClientId(clientId); broadcastController.setClientSecret(clientSecret); broadcastController.initializeTwitch(); }
private void loop() { long lastFrameTime = 0; while (!Display.isCloseRequested()) { long now = System.nanoTime(); long renderFps = 30; long nanoPerFrame = 1000000000 / renderFps; // update the animation if (now - lastFrameTime >= nanoPerFrame) { lastFrameTime = now; angle += 2.0f; glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glPushMatrix(); glRotatef(view_rotx, 1.0f, 0.0f, 0.0f); glRotatef(view_roty, 0.0f, 1.0f, 0.0f); glRotatef(view_rotz, 0.0f, 0.0f, 1.0f); glPushMatrix(); glTranslatef(-3.0f, -2.0f, 0.0f); glRotatef(angle, 0.0f, 0.0f, 1.0f); glCallList(gear1); glPopMatrix(); glPushMatrix(); glTranslatef(3.1f, -2.0f, 0.0f); glRotatef(-2.0f * angle - 9.0f, 0.0f, 0.0f, 1.0f); glCallList(gear2); glPopMatrix(); glPushMatrix(); glTranslatef(-3.1f, 4.2f, 0.0f); glRotatef(-2.0f * angle - 25.0f, 0.0f, 0.0f, 1.0f); glCallList(gear3); glPopMatrix(); glPopMatrix(); Display.update(); } handleInput(); if (broadcastController != null) { submitFrame(); broadcastController.update(); } if (chatController != null) { chatController.update(); } } }
private void initChat() { if (chatController != null) { return; } chatController = new ChatController(); chatController.setListener(this); chatController.setAuthToken(broadcastController.getAuthToken()); if (!chatController.connect(username)) { chatController = null; } }
public void onIngestTestStateChanged(IngestTester source, IngestTester.TestState state) { String str = "[" + (int) (ingestTester.getTotalProgress() * 100) + "%] " + state.toString(); switch (state) { case ConnectingToServer: { str += ": " + source.getCurrentServer().serverName + "..."; break; } case DoneTestingServer: { str += ": " + source.getCurrentServer().serverName + "... " + source.getCurrentServer().bitrateKbps + " kbps"; break; } case Finished: case Cancelled: { ingestTester.setListener(null); ingestTester = null; break; } default: { break; } } if (state == IngestTester.TestState.Finished) { if (source.getIngestList().getBestServer() != null) { str += ": Selecting best server - " + source.getIngestList().getBestServer().serverName; broadcastController.setIngestServer(source.getIngestList().getBestServer()); } } System.out.println(str); }
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()); } } } } }