Java Code Examples for org.kurento.client.PlayerEndpoint

The following examples show how to use org.kurento.client.PlayerEndpoint. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: kurento-java   Source File: TransactionTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test(expected = TransactionNotCommitedException.class)
public void usePlainMethodsInNewObjectsInsideTx() throws InterruptedException, ExecutionException {

  // Pipeline creation (no transaction)
  MediaPipeline pipeline = kurentoClient.createMediaPipeline();
  PlayerEndpoint player =
      new PlayerEndpoint.Builder(pipeline, "http://" + getTestFilesHttpPath()
          + "/video/format/small.webm").build();

  // Creation in explicit transaction
  Transaction tx = pipeline.beginTransaction();
  HttpPostEndpoint httpEndpoint = new HttpPostEndpoint.Builder(pipeline).build(tx);

  // TransactionNotExecutedExcetion should be thrown
  httpEndpoint.connect(player);

}
 
Example 2
Source Project: kurento-java   Source File: TransactionTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test(expected = TransactionNotCommitedException.class)
public void usePlainMethodsWithNewObjectsAsParamsInsideTx() throws InterruptedException,
    ExecutionException {

  // Pipeline creation (no transaction)
  MediaPipeline pipeline = kurentoClient.createMediaPipeline();
  PlayerEndpoint player =
      new PlayerEndpoint.Builder(pipeline, "http://" + getTestFilesHttpPath()
          + "/video/format/small.webm").build();

  // Creation in explicit transaction
  Transaction tx = pipeline.beginTransaction();
  HttpPostEndpoint httpEndpoint = new HttpPostEndpoint.Builder(pipeline).build(tx);

  // TransactionNotExecutedExcetion should be thrown
  player.connect(httpEndpoint);

}
 
Example 3
Source Project: kurento-java   Source File: RtpEndpointAsyncTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testRtpEndpointSimulatingAndroidSdp() throws InterruptedException {

  PlayerEndpoint player = new PlayerEndpoint.Builder(pipeline, URL_BARCODES).build();

  RtpEndpoint rtpEndpoint = new RtpEndpoint.Builder(pipeline).build();

  String requestSdp = "v=0\r\n" + "o=- 12345 12345 IN IP4 95.125.31.136\r\n" + "s=-\r\n"
      + "c=IN IP4 95.125.31.136\r\n" + "t=0 0\r\n" + "m=video 52126 RTP/AVP 96 97 98\r\n"
      + "a=rtpmap:96 H264/90000\r\n" + "a=rtpmap:97 MP4V-ES/90000\r\n"
      + "a=rtpmap:98 H263-1998/90000\r\n" + "a=recvonly\r\n" + "b=AS:384\r\n";

  rtpEndpoint.processOffer(requestSdp);
  player.connect(rtpEndpoint, MediaType.VIDEO);
  player.play();

  // just a little bit of time before destroying
  Thread.sleep(2000);
}
 
Example 4
Source Project: kurento-java   Source File: SdpBaseTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testRtpEndpointSimulatingAndroidSdp() throws InterruptedException {

  PlayerEndpoint player = new PlayerEndpoint.Builder(pipeline, URL_BARCODES).build();

  String requestSdp = "v=0\r\n" + "o=- 12345 12345 IN IP4 95.125.31.136\r\n" + "s=-\r\n"
      + "c=IN IP4 95.125.31.136\r\n" + "t=0 0\r\n" + "m=video 52126 RTP/AVP 96 97 98\r\n"
      + "a=rtpmap:96 H264/90000\r\n" + "a=rtpmap:97 MP4V-ES/90000\r\n"
      + "a=rtpmap:98 H263-1998/90000\r\n" + "a=recvonly\r\n" + "b=AS:384\r\n";

  player.connect(sdp, MediaType.VIDEO);
  sdp.processOffer(requestSdp);
  player.play();

  // just a little bit of time before destroying
  Thread.sleep(2000);
}
 
Example 5
Source Project: kurento-tutorial-java   Source File: PlayMediaPipeline.java    License: Apache License 2.0 6 votes vote down vote up
public PlayMediaPipeline(KurentoClient kurento, String user, final WebSocketSession session) {
  // Media pipeline
  pipeline = kurento.createMediaPipeline();

  // Media Elements (WebRtcEndpoint, PlayerEndpoint)
  webRtc = new WebRtcEndpoint.Builder(pipeline).build();
  player = new PlayerEndpoint.Builder(pipeline, RECORDING_PATH + user + RECORDING_EXT).build();

  // Connection
  player.connect(webRtc);

  // Player listeners
  player.addErrorListener(new EventListener<ErrorEvent>() {
    @Override
    public void onEvent(ErrorEvent event) {
      log.info("ErrorEvent: {}", event.getDescription());
      sendPlayEnd(session);
    }
  });
}
 
Example 6
Source Project: kurento-tutorial-java   Source File: PlayMediaPipeline.java    License: Apache License 2.0 6 votes vote down vote up
public PlayMediaPipeline(KurentoClient kurento, String user, final WebSocketSession session) {
  // Media pipeline
  pipeline = kurento.createMediaPipeline();

  // Media Elements (WebRtcEndpoint, PlayerEndpoint)
  webRtc = new WebRtcEndpoint.Builder(pipeline).build();
  player = new PlayerEndpoint.Builder(pipeline, RECORDING_PATH + user + RECORDING_EXT).build();

  // Connection
  player.connect(webRtc);

  // Player listeners
  player.addErrorListener(new EventListener<ErrorEvent>() {
    @Override
    public void onEvent(ErrorEvent event) {
      log.info("ErrorEvent: {}", event.getDescription());
      sendPlayEnd(session);
    }
  });
}
 
Example 7
Source Project: openmeetings   Source File: TestSetupFlowMocked.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testMsgTestRecord1() throws Exception {
	doReturn(mock(MediaPipeline.class)).when(client).createMediaPipeline(any(Transaction.class));
	WebRtcEndpoint.Builder builder = mock(WebRtcEndpoint.Builder.class);
	whenNew(WebRtcEndpoint.Builder.class).withArguments(any(MediaPipeline.class)).thenReturn(builder);
	doReturn(mock(WebRtcEndpoint.class)).when(builder).build();

	RecorderEndpoint.Builder recBuilder = mock(RecorderEndpoint.Builder.class);
	whenNew(RecorderEndpoint.Builder.class).withArguments(any(MediaPipeline.class), anyString()).thenReturn(recBuilder);
	doReturn(recBuilder).when(recBuilder).stopOnEndOfStream();
	doReturn(recBuilder).when(recBuilder).withMediaProfile(any(MediaProfileSpecType.class));
	doReturn(mock(RecorderEndpoint.class)).when(recBuilder).build();

	WsClient c = new WsClient("sessionId", 0);
	for (boolean audio : new boolean[] {true, false}) {
		for (boolean video : new boolean[] {true, false}) {
			JSONObject msg = new JSONObject(MSG_BASE.toString())
					.put("id", "record")
					.put("sdpOffer", "")
					.put("audio", audio)
					.put("video", video);
			handler.onMessage(c, msg);
		}
	}
	JSONObject iceMsg = new JSONObject(MSG_BASE.toString())
			.put("id", "iceCandidate")
			.put(PARAM_CANDIDATE, new JSONObject()
					.put(PARAM_CANDIDATE, "candidate")
					.put("sdpMid", "sdpMid")
					.put("sdpMLineIndex", 1));
	handler.onMessage(c, iceMsg);
	PlayerEndpoint.Builder playBuilder = mock(PlayerEndpoint.Builder.class);
	whenNew(PlayerEndpoint.Builder.class).withArguments(any(MediaPipeline.class), anyString()).thenReturn(playBuilder);
	doReturn(mock(PlayerEndpoint.class)).when(playBuilder).build();
	handler.onMessage(c, new JSONObject(MSG_BASE.toString())
			.put("id", "play")
			.put("sdpOffer", "sdpOffer"));
	testProcessor.destroy();
}
 
Example 8
Source Project: kurento-java   Source File: EventTagTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testEventWithoutTag() throws Exception {
  MediaPipeline mp = kurentoClient.createMediaPipeline();
  final CountDownLatch eventReceived = new CountDownLatch(1);

  PlayerEndpoint player =
      new PlayerEndpoint.Builder(mp, "http://" + getTestFilesHttpPath() + "/video/10sec/red.webm")
          .build();

  player.addTag("test_1", "value_1");
  player.addTag("test_2", "value_2");
  player.addTag("test_3", "value_3");

  player.addEndOfStreamListener(new EventListener<EndOfStreamEvent>() {
    @Override
    public void onEvent(EndOfStreamEvent event) {
      List<Tag> tags = event.getTags();

      if (tags.size() == 0) {
        eventReceived.countDown();
      }
    }
  });

  player.play();
  // Guard time to reproduce the whole video
  if (!eventReceived.await(TIMEOUT, TimeUnit.SECONDS)) {
    Assert.fail("Event not received");
  }
}
 
Example 9
Source Project: kurento-java   Source File: RepositoryRecorderTest.java    License: Apache License 2.0 5 votes vote down vote up
private void launchBrowser(WebRtcEndpoint webRtcEp, PlayerEndpoint playerEp,
    RecorderEndpoint recorderEp) throws InterruptedException {

  getPage().subscribeEvents("playing");
  getPage().initWebRtc(webRtcEp, WebRtcChannel.AUDIO_AND_VIDEO, WebRtcMode.RCV_ONLY);
  playerEp.play();
  final CountDownLatch eosLatch = new CountDownLatch(1);
  playerEp.addEndOfStreamListener(new EventListener<EndOfStreamEvent>() {
    @Override
    public void onEvent(EndOfStreamEvent event) {
      eosLatch.countDown();
    }
  });

  if (recorderEp != null) {
    recorderEp.record();
  }

  // Assertions
  Assert.assertTrue("Not received media (timeout waiting playing event)",
      getPage().waitForEvent("playing"));
  Assert.assertTrue("The color of the video should be black",
      getPage().similarColor(Color.BLACK));
  Assert.assertTrue("Not received EOS event in player",
      eosLatch.await(getPage().getTimeout(), TimeUnit.SECONDS));
  double currentTime = getPage().getCurrentTime();
  Assert.assertTrue(
      "Error in play time (expected: " + PLAYTIME + " sec, real: " + currentTime + " sec)",
      getPage().compare(PLAYTIME, currentTime));
}
 
Example 10
Source Project: kurento-java   Source File: CompositeWebRtcRecorderTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testCompositeRecorder() throws Exception {

  // MediaPipeline
  MediaPipeline mp = kurentoClient.createMediaPipeline();

  Composite composite = new Composite.Builder(mp).build();

  HubPort hubPort1 = new HubPort.Builder(composite).build();
  WebRtcEndpoint webRtcEpRed = new WebRtcEndpoint.Builder(mp).build();
  webRtcEpRed.connect(hubPort1);

  HubPort hubPort2 = new HubPort.Builder(composite).build();
  WebRtcEndpoint webRtcEpGreen = new WebRtcEndpoint.Builder(mp).build();
  webRtcEpGreen.connect(hubPort2, MediaType.AUDIO);

  HubPort hubPort3 = new HubPort.Builder(composite).build();
  WebRtcEndpoint webRtcEpBlue = new WebRtcEndpoint.Builder(mp).build();
  webRtcEpBlue.connect(hubPort3, MediaType.AUDIO);

  HubPort hubPort4 = new HubPort.Builder(composite).build();
  WebRtcEndpoint webRtcEpWhite = new WebRtcEndpoint.Builder(mp).build();
  webRtcEpWhite.connect(hubPort4, MediaType.AUDIO);

  String recordingFile = getDefaultOutputFile(EXTENSION_WEBM);
  RecorderEndpoint recorderEp =
      new RecorderEndpoint.Builder(mp, Protocol.FILE + recordingFile).build();
  HubPort hubPort5 = new HubPort.Builder(composite).build();
  hubPort5.connect(recorderEp);

  // WebRTC browsers
  getPage(BROWSER2).initWebRtc(webRtcEpRed, WebRtcChannel.AUDIO_AND_VIDEO, WebRtcMode.SEND_ONLY);
  getPage(BROWSER3).initWebRtc(webRtcEpGreen, WebRtcChannel.AUDIO_AND_VIDEO,
      WebRtcMode.SEND_ONLY);
  getPage(BROWSER4).initWebRtc(webRtcEpBlue, WebRtcChannel.AUDIO_AND_VIDEO, WebRtcMode.SEND_ONLY);
  getPage(BROWSER5).initWebRtc(webRtcEpWhite, WebRtcChannel.AUDIO_AND_VIDEO,
      WebRtcMode.SEND_ONLY);

  recorderEp.record();

  Thread.sleep(PLAYTIME * 1000);

  final CountDownLatch recorderLatch = new CountDownLatch(1);
  recorderEp.stopAndWait(new Continuation<Void>() {

    @Override
    public void onSuccess(Void result) throws Exception {
      recorderLatch.countDown();
    }

    @Override
    public void onError(Throwable cause) throws Exception {
      recorderLatch.countDown();
    }
  });

  Assert.assertTrue("Not stop properly",
      recorderLatch.await(getPage(BROWSER1).getTimeout(), TimeUnit.SECONDS));

  mp.release();

  // Media Pipeline #2
  MediaPipeline mp2 = kurentoClient.createMediaPipeline();
  PlayerEndpoint playerEp2 =
      new PlayerEndpoint.Builder(mp2, Protocol.FILE + recordingFile).build();
  WebRtcEndpoint webRtcEp2 = new WebRtcEndpoint.Builder(mp2).build();
  playerEp2.connect(webRtcEp2);

  // Playing the recorded file
  launchBrowser(mp2, webRtcEp2, playerEp2, null, EXPECTED_VIDEO_CODEC_WEBM,
      EXPECTED_AUDIO_CODEC_WEBM, recordingFile, Color.RED, 0, 0, PLAYTIME);

  // Release Media Pipeline #2
  mp2.release();

  success = true;
}
 
Example 11
Source Project: kurento-java   Source File: PlayerMultiplePauseTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testPlayerMultiplePause() throws Exception {
  // Test data
  final String mediaUrl = "http://" + getTestFilesHttpPath() + "/video/60sec/red.webm";
  final Color expectedColor = Color.RED;
  final int playTimeSeconds = 2;
  final int pauseTimeSeconds = 2;
  final int numPauses = 30;

  // Media Pipeline
  MediaPipeline mp = kurentoClient.createMediaPipeline();
  PlayerEndpoint playerEp = new PlayerEndpoint.Builder(mp, mediaUrl).build();
  WebRtcEndpoint webRtcEp = new WebRtcEndpoint.Builder(mp).build();
  playerEp.connect(webRtcEp);

  // WebRTC in receive-only mode
  getPage().subscribeEvents("playing");
  getPage().initWebRtc(webRtcEp, WebRtcChannel.AUDIO_AND_VIDEO, WebRtcMode.RCV_ONLY);
  playerEp.play();
  Assert.assertTrue("Not received media (timeout waiting playing event)",
      getPage().waitForEvent("playing"));

  for (int i = 0; i < numPauses; i++) {
    // Assert color
    Assert.assertTrue("The color of the video should be " + expectedColor,
        getPage().similarColor(expectedColor));

    // Pause and wait
    playerEp.pause();
    Thread.sleep(TimeUnit.SECONDS.toMillis(pauseTimeSeconds));

    // Resume video
    playerEp.play();
    Thread.sleep(TimeUnit.SECONDS.toMillis(playTimeSeconds));
  }

  // Release Media Pipeline
  mp.release();
}
 
Example 12
Source Project: kurento-java   Source File: MetaTestMountedVolumeTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void test() throws InterruptedException {

  // Media Pipeline
  MediaPipeline mp = kurentoClient.createMediaPipeline();

  String videoPath = "file://" + getTestFilesDiskPath() + "/video/filter/barcodes.webm";

  PlayerEndpoint p = new PlayerEndpoint.Builder(mp, videoPath).build();

  final CountDownLatch latch = new CountDownLatch(1);

  p.addErrorListener(new EventListener<ErrorEvent>() {
    @Override
    public void onEvent(ErrorEvent event) {
      log.warn("Error un player: " + event.getDescription());
      latch.countDown();
    }
  });

  p.play();

  if (latch.await(5, TimeUnit.SECONDS)) {
    fail("Player error");
  }

  // Release Media Pipeline
  mp.release();
}
 
Example 13
Source Project: kurento-java   Source File: TransactionTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void transactionTest() throws InterruptedException, ExecutionException {

  // Pipeline creation (no transaction)
  MediaPipeline pipeline = kurentoClient.createMediaPipeline();

  PlayerEndpoint player =
      new PlayerEndpoint.Builder(pipeline, "http://" + getTestFilesHttpPath()
          + "/video/format/small.webm").useEncodedMedia().build();

  HttpPostEndpoint httpEndpoint = new HttpPostEndpoint.Builder(pipeline).build();

  player.connect(httpEndpoint);

  String url = httpEndpoint.getUrl();
  // End pipeline creation

  // Explicit transaction
  Transaction tx = pipeline.beginTransaction();
  player.play(tx);
  TFuture<String> fUrl = httpEndpoint.getUrl(tx);
  pipeline.release(tx);
  tx.commit();
  // End explicit transaction

  assertThat(url, is(fUrl.get()));
}
 
Example 14
Source Project: kurento-java   Source File: TransactionTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void creationInTransaction() throws InterruptedException, ExecutionException {

  // Pipeline creation (transaction)
  Transaction tx1 = kurentoClient.beginTransaction();

  MediaPipeline pipeline = kurentoClient.createMediaPipeline(tx1);

  PlayerEndpoint player =
      new PlayerEndpoint.Builder(pipeline, "http://" + getTestFilesHttpPath()
          + "/video/format/small.webm").useEncodedMedia().build(tx1);

  HttpPostEndpoint httpEndpoint = new HttpPostEndpoint.Builder(pipeline).build(tx1);

  player.connect(tx1, httpEndpoint);
  TFuture<String> url1 = httpEndpoint.getUrl(tx1);
  tx1.commit();
  // End pipeline creation

  // Explicit transaction
  Transaction tx2 = pipeline.beginTransaction();
  player.play(tx2);
  TFuture<String> url2 = httpEndpoint.getUrl(tx2);
  pipeline.release(tx2);
  tx2.commit();
  // End explicit transaction

  assertThat(url1.get(), is(url2.get()));
}
 
Example 15
Source Project: kurento-java   Source File: TransactionTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void waitCommitedTest() throws InterruptedException, ExecutionException {

  // Pipeline creation (transaction)

  Transaction tx = kurentoClient.beginTransaction();

  MediaPipeline pipeline = kurentoClient.createMediaPipeline(tx);

  final PlayerEndpoint player =
      new PlayerEndpoint.Builder(pipeline, "http://" + getTestFilesHttpPath()
          + "/video/format/small.webm").build(tx);

  HttpPostEndpoint httpEndpoint = new HttpPostEndpoint.Builder(pipeline).build(tx);

  player.connect(tx, httpEndpoint);

  final CountDownLatch readyLatch = new CountDownLatch(1);

  new Thread() {
    @Override
    public void run() {
      try {
        player.waitCommited();
        readyLatch.countDown();
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
    }
  }.start();

  assertThat(readyLatch.getCount(), is(1l));

  tx.commit();

  if (!readyLatch.await(5000, TimeUnit.SECONDS)) {
    fail("waitForReady not unblocked in 5s");
  }
}
 
Example 16
Source Project: kurento-tutorial-java   Source File: Pipeline.java    License: Apache License 2.0 5 votes vote down vote up
public void setFeedUrl(String feedUrl) {
  this.feedUrl = feedUrl;

  if (this.playerEndpoint != null) {
    log.debug("Releasing previous elements");
    this.crowdDetectorFilter.release();
    this.playerEndpoint.release();
  }

  log.debug("Creating new elements");

  this.crowdDetectorFilter = new CrowdDetectorFilter.Builder(this.pipe, this.rois).build();
  this.crowdDetectorFilter.setProcessingWidth(640);

  addOrionListeners();

  this.playerEndpoint = new PlayerEndpoint.Builder(this.pipe, this.feedUrl).build();
  addPlayerListeners();
  this.playing = true;

  this.playerEndpoint.connect(this.crowdDetectorFilter);
  this.playerEndpoint.play();

  log.debug("New player is now runing");
  log.debug("Connecting " + this.webRtcEndpoints.size() + " webrtcendpoints");
  // change the feed for all the webrtc clients connected.
  for (Entry<String, WebRtcEndpoint> ep : this.webRtcEndpoints.entrySet()) {
    this.crowdDetectorFilter.connect(ep.getValue());
  }
}
 
Example 17
Source Project: openvidu   Source File: MediaEndpoint.java    License: Apache License 2.0 4 votes vote down vote up
public PlayerEndpoint getPlayerEndpoint() {
	return playerEndpoint;
}
 
Example 18
Source Project: openmeetings   Source File: AbstractStream.java    License: Apache License 2.0 4 votes vote down vote up
public PlayerEndpoint createPlayerEndpoint(MediaPipeline pipeline, String path) {
	return new PlayerEndpoint.Builder(pipeline, path).build();
}
 
Example 19
Source Project: kurento-java   Source File: DispatcherPlayerTest.java    License: Apache License 2.0 4 votes vote down vote up
@Test
public void testDispatcherPlayer() throws Exception {
  // Media Pipeline
  MediaPipeline mp = kurentoClient.createMediaPipeline();

  PlayerEndpoint playerEp =
      new PlayerEndpoint.Builder(mp, "http://" + getTestFilesHttpPath() + "/video/10sec/red.webm")
          .build();
  PlayerEndpoint playerEp2 = new PlayerEndpoint.Builder(mp,
      "http://" + getTestFilesHttpPath() + "/video/10sec/blue.webm").build();
  WebRtcEndpoint webRtcEp = new WebRtcEndpoint.Builder(mp).build();

  Dispatcher dispatcher = new Dispatcher.Builder(mp).build();
  HubPort hubPort1 = new HubPort.Builder(dispatcher).build();
  HubPort hubPort2 = new HubPort.Builder(dispatcher).build();
  HubPort hubPort3 = new HubPort.Builder(dispatcher).build();

  playerEp.connect(hubPort1);
  playerEp2.connect(hubPort3);
  hubPort2.connect(webRtcEp);
  dispatcher.connect(hubPort1, hubPort2);

  final CountDownLatch eosLatch = new CountDownLatch(1);
  playerEp2.addEndOfStreamListener(new EventListener<EndOfStreamEvent>() {
    @Override
    public void onEvent(EndOfStreamEvent event) {
      eosLatch.countDown();
    }
  });

  // Test execution
  getPage().subscribeEvents("playing");
  getPage().initWebRtc(webRtcEp, WebRtcChannel.AUDIO_AND_VIDEO, WebRtcMode.RCV_ONLY);
  playerEp.play();

  // Assertions
  Assert.assertTrue("Not received media (timeout waiting playing event)",
      getPage().waitForEvent("playing"));
  Assert.assertTrue("The color of the video should be red", getPage().similarColor(Color.RED));

  Thread.sleep(5000);
  playerEp2.play();
  dispatcher.connect(hubPort3, hubPort2);
  Assert.assertTrue("The color of the video should be blue", getPage().similarColor(Color.BLUE));

  Assert.assertTrue("Not received EOS event in player",
      eosLatch.await(getPage().getTimeout(), TimeUnit.SECONDS));
  double currentTime = getPage().getCurrentTime();
  Assert.assertTrue(
      "Error in play time (expected: " + PLAYTIME + " sec, real: " + currentTime + " sec)",
      getPage().compare(PLAYTIME, currentTime));

  // Release Media Pipeline
  mp.release();
}
 
Example 20
Source Project: kurento-java   Source File: AlphaBlendingPlayerTest.java    License: Apache License 2.0 4 votes vote down vote up
@Test
public void testAlphaBlendingPlayer() throws Exception {
  // Media Pipeline
  MediaPipeline mp = kurentoClient.createMediaPipeline();

  PlayerEndpoint playerRed =
      new PlayerEndpoint.Builder(mp, "http://" + getTestFilesHttpPath() + "/video/30sec/red.webm")
          .build();
  PlayerEndpoint playerGreen = new PlayerEndpoint.Builder(mp,
      "http://" + getTestFilesHttpPath() + "/video/30sec/green.webm").build();
  PlayerEndpoint playerBlue = new PlayerEndpoint.Builder(mp,
      "http://" + getTestFilesHttpPath() + "/video/30sec/blue.webm").build();

  AlphaBlending alphaBlending = new AlphaBlending.Builder(mp).build();
  HubPort hubPort1 = new HubPort.Builder(alphaBlending).build();
  HubPort hubPort2 = new HubPort.Builder(alphaBlending).build();
  HubPort hubPort3 = new HubPort.Builder(alphaBlending).build();

  playerRed.connect(hubPort1);
  playerGreen.connect(hubPort2);
  playerBlue.connect(hubPort3);

  HubPort hubPort4 = new HubPort.Builder(alphaBlending).build();
  WebRtcEndpoint webRtcEp = new WebRtcEndpoint.Builder(mp).build();
  hubPort4.connect(webRtcEp);

  alphaBlending.setMaster(hubPort1, 1);

  alphaBlending.setPortProperties(0F, 0F, 8, 0.2F, 0.2F, hubPort2);
  alphaBlending.setPortProperties(0.4F, 0.4F, 7, 0.2F, 0.2F, hubPort3);

  getPage().subscribeEvents("playing");
  getPage().initWebRtc(webRtcEp, WebRtcChannel.VIDEO_ONLY, WebRtcMode.RCV_ONLY);

  playerRed.play();
  playerGreen.play();
  playerBlue.play();

  Thread.sleep(2000);
  Assert.assertTrue("Not received media (timeout waiting playing event)",
      getPage().waitForEvent("playing"));

  Thread.sleep(2000);

  // Assertions
  Assert.assertTrue("Upper left part of the video must be blue",
      getPage().similarColorAt(Color.GREEN, 0, 0));
  Assert.assertTrue("Lower right part of the video must be red",
      getPage().similarColorAt(Color.RED, 315, 235));
  Assert.assertTrue("Center of the video must be blue",
      getPage().similarColorAt(Color.BLUE, 160, 120));

  // alphaBlending.setMaster(hubPort3, 1);
  alphaBlending.setPortProperties(0.8F, 0.8F, 7, 0.2F, 0.2F, hubPort3);

  Assert.assertTrue("Lower right part of the video must be blue",
      getPage().similarColorAt(Color.BLUE, 315, 235));
  Assert.assertTrue("Center of the video must be red",
      getPage().similarColorAt(Color.RED, 160, 120));

  Thread.sleep(PLAYTIME * 1000);
}
 
Example 21
Source Project: kurento-java   Source File: EventTagTest.java    License: Apache License 2.0 4 votes vote down vote up
@Test
public void testEventTag() throws Exception {
  MediaPipeline mp = kurentoClient.createMediaPipeline();
  final CountDownLatch eventReceived = new CountDownLatch(TAG_SIZE);

  PlayerEndpoint player =
      new PlayerEndpoint.Builder(mp, "http://" + getTestFilesHttpPath() + "/video/10sec/red.webm")
          .build();

  player.addTag("test_1", "value_1");
  player.addTag("test_2", "value_2");
  player.addTag("test_3", "value_3");

  player.setSendTagsInEvents(true);

  player.addEndOfStreamListener(new EventListener<EndOfStreamEvent>() {
    @Override
    public void onEvent(EndOfStreamEvent event) {
      List<Tag> tags = event.getTags();

      for (Tag tag : tags) {
        if (tag.getKey().compareTo("test_1") == 0) {
          if (tag.getValue().compareTo("value_1") == 0) {
            eventReceived.countDown();
          }
        } else if (tag.getKey().compareTo("test_2") == 0) {
          if (tag.getValue().compareTo("value_2") == 0) {
            eventReceived.countDown();
          }
        } else if (tag.getKey().compareTo("test_3") == 0) {
          if (tag.getValue().compareTo("value_3") == 0) {
            eventReceived.countDown();
          }
        }
      }
    }
  });

  player.play();
  // Guard time to reproduce the whole video
  if (!eventReceived.await(TIMEOUT, TimeUnit.SECONDS)) {
    Assert.fail("Event not received");
  }

}
 
Example 22
Source Project: kurento-java   Source File: RepositoryRecorderTest.java    License: Apache License 2.0 4 votes vote down vote up
@Test
public void testRepositoryRecorder() throws Exception {

  final CountDownLatch recorderLatch = new CountDownLatch(1);

  // Media Pipeline
  MediaPipeline mp = kurentoClient.createMediaPipeline();
  PlayerEndpoint playerEp = new PlayerEndpoint.Builder(mp,
      "http://" + getTestFilesHttpPath() + "/video/10sec/ball.webm").build();
  WebRtcEndpoint webRtcEp1 = new WebRtcEndpoint.Builder(mp).build();

  RepositoryItem repositoryItem = repository.createRepositoryItem();
  RepositoryHttpRecorder recorder = repositoryItem.createRepositoryHttpRecorder();

  RecorderEndpoint recorderEp = new RecorderEndpoint.Builder(mp, recorder.getURL()).build();
  playerEp.connect(webRtcEp1);
  playerEp.connect(recorderEp);

  final CountDownLatch eosLatch = new CountDownLatch(1);
  playerEp.addEndOfStreamListener(new EventListener<EndOfStreamEvent>() {
    @Override
    public void onEvent(EndOfStreamEvent event) {
      eosLatch.countDown();
    }
  });

  // Test execution #1. Play the video while it is recorded
  launchBrowser(webRtcEp1, playerEp, recorderEp);

  // Wait for EOS
  Assert.assertTrue("Not received EOS event in player",
      eosLatch.await(getPage().getTimeout(), TimeUnit.SECONDS));

  // Release Media Pipeline #1
  recorderEp.stopAndWait(new Continuation<Void>() {

    @Override
    public void onSuccess(Void result) throws Exception {
      recorderLatch.countDown();
    }

    @Override
    public void onError(Throwable cause) throws Exception {
      recorderLatch.countDown();
    }
  });

  Assert.assertTrue("Not stop properly",
      recorderLatch.await(getPage().getTimeout(), TimeUnit.SECONDS));

  mp.release();
  Thread.sleep(500);
}
 
Example 23
Source Project: kurento-java   Source File: PlayerEndTest.java    License: Apache License 2.0 4 votes vote down vote up
public void doTest(PlayerOperation playerOperation) throws Exception {
  // Test data
  final String mediaUrl = "http://" + getTestFilesHttpPath() + "/video/format/small.webm";
  final int guardTimeSeconds = 10;

  // Media Pipeline
  MediaPipeline mp = kurentoClient.createMediaPipeline();
  PlayerEndpoint playerEp = new PlayerEndpoint.Builder(mp, mediaUrl).build();
  WebRtcEndpoint webRtcEp = new WebRtcEndpoint.Builder(mp).build();
  playerEp.connect(webRtcEp);

  // Subscription to EOS event
  final boolean[] eos = new boolean[1];
  eos[0] = false;
  playerEp.addEndOfStreamListener(new EventListener<EndOfStreamEvent>() {
    @Override
    public void onEvent(EndOfStreamEvent event) {
      log.error("EOS event received: {} {}", event.getType(), event.getTimestamp());
      eos[0] = true;
    }
  });

  // WebRTC in receive-only mode
  getPage().subscribeEvents("playing");
  getPage().initWebRtc(webRtcEp, WebRtcChannel.AUDIO_AND_VIDEO, WebRtcMode.RCV_ONLY);
  playerEp.play();
  Assert.assertTrue("Not received media (timeout waiting playing event)",
      getPage().waitForEvent("playing"));

  // Stop/release stream and wait x seconds
  switch (playerOperation) {
    case STOP:
      playerEp.stop();
      break;
    case RELEASE:
      playerEp.release();
      break;
  }
  Thread.sleep(TimeUnit.SECONDS.toMillis(guardTimeSeconds));

  // Verify that EOS event has not being received
  Assert.assertFalse("EOS event has been received. "
      + "This should not be happenning because the stream has been stopped", eos[0]);

  // Release Media Pipeline
  mp.release();
}
 
Example 24
Source Project: kurento-java   Source File: PlayerFaceOverlayTest.java    License: Apache License 2.0 4 votes vote down vote up
@Test
public void testPlayerFaceOverlay() throws Exception {
  // Test data
  final int playTimeSeconds = 30;
  final String mediaUrl = "http://" + getTestFilesHttpPath() + "/video/filter/fiwarecut.mp4";
  final Color expectedColor = Color.RED;
  final int xExpectedColor = 420;
  final int yExpectedColor = 45;
  final String imgOverlayUrl = "http://" + getTestFilesHttpPath() + "/img/red-square.png";
  final float offsetXPercent = -0.2F;
  final float offsetYPercent = -1.2F;
  final float widthPercent = 1.6F;
  final float heightPercent = 1.6F;

  // Media Pipeline
  MediaPipeline mp = kurentoClient.createMediaPipeline();
  PlayerEndpoint playerEp = new PlayerEndpoint.Builder(mp, mediaUrl).build();
  WebRtcEndpoint webRtcEp = new WebRtcEndpoint.Builder(mp).build();
  FaceOverlayFilter filter = new FaceOverlayFilter.Builder(mp).build();
  filter.setOverlayedImage(imgOverlayUrl, offsetXPercent, offsetYPercent, widthPercent,
      heightPercent);
  playerEp.connect(filter);
  filter.connect(webRtcEp);

  final CountDownLatch eosLatch = new CountDownLatch(1);
  playerEp.addEndOfStreamListener(new EventListener<EndOfStreamEvent>() {
    @Override
    public void onEvent(EndOfStreamEvent event) {
      eosLatch.countDown();
    }
  });

  // Test execution
  getPage().subscribeEvents("playing");
  getPage().initWebRtc(webRtcEp, WebRtcChannel.AUDIO_AND_VIDEO, WebRtcMode.RCV_ONLY);
  playerEp.play();

  // Assertions
  Assert.assertTrue("Not received media (timeout waiting playing event)",
      getPage().waitForEvent("playing"));
  Assert.assertTrue(
      "Color at coordinates " + xExpectedColor + "," + yExpectedColor + " must be "
          + expectedColor,
      getPage().similarColorAt(expectedColor, xExpectedColor, yExpectedColor));
  Assert.assertTrue("Not received EOS event in player",
      eosLatch.await(getPage().getTimeout(), TimeUnit.SECONDS));
  double currentTime = getPage().getCurrentTime();
  Assert.assertTrue(
      "Error in play time (expected: " + playTimeSeconds + " sec, real: " + currentTime + " sec)",
      getPage().compare(playTimeSeconds, currentTime));

  // Release Media Pipeline
  mp.release();
}
 
Example 25
Source Project: kurento-java   Source File: PlayerSwitchTest.java    License: Apache License 2.0 4 votes vote down vote up
@Test
public void testPlayerSwitch() throws Exception {
  // Media Pipeline
  MediaPipeline mp = kurentoClient.createMediaPipeline();
  PlayerEndpoint playerRed = new PlayerEndpoint.Builder(mp,
      "http://" + getTestFilesHttpPath() + "/video/format/chrome.mp4").build();

  WebRtcEndpoint webRtcEndpoint = new WebRtcEndpoint.Builder(mp).build();

  // Test execution
  getPage().subscribeEvents("playing");
  getPage().initWebRtc(webRtcEndpoint, WebRtcChannel.AUDIO_AND_VIDEO, WebRtcMode.RCV_ONLY);

  // red
  playerRed.connect(webRtcEndpoint);
  playerRed.play();
  getPage().subscribeEvents("playing");
  Thread.sleep(TimeUnit.SECONDS.toMillis(PLAYTIME) / N_PLAYER);

  PlayerEndpoint playerGreen = new PlayerEndpoint.Builder(mp,
      "http://" + getTestFilesHttpPath() + "/video/format/fiware.mkv").build();
  // green
  playerGreen.connect(webRtcEndpoint);
  playerGreen.play();
  Thread.sleep(TimeUnit.SECONDS.toMillis(PLAYTIME) / N_PLAYER);

  // blue
  PlayerEndpoint playerBlue = new PlayerEndpoint.Builder(mp,
      "http://" + getTestFilesHttpPath() + "/video/format/sintel.webm").build();
  playerBlue.connect(webRtcEndpoint);
  playerBlue.play();
  Thread.sleep(TimeUnit.SECONDS.toMillis(PLAYTIME) / N_PLAYER);

  // ball
  PlayerEndpoint playerBall = new PlayerEndpoint.Builder(mp,
      "http://" + getTestFilesHttpPath() + "/video/format/rabbit.mov").build();
  playerBall.connect(webRtcEndpoint);
  playerBall.play();
  Thread.sleep(TimeUnit.SECONDS.toMillis(PLAYTIME) / N_PLAYER);

  // rtsp
  PlayerEndpoint playerRtsp =
      new PlayerEndpoint.Builder(mp, "rtsp://195.55.223.100/axis-media/media.amp").build();
  playerRtsp.connect(webRtcEndpoint);
  playerRtsp.play();
  Thread.sleep(TimeUnit.SECONDS.toMillis(PLAYTIME) / N_PLAYER);

  // Assertions
  Assert.assertTrue("Not received media (timeout waiting playing event)",
      getPage().waitForEvent("playing"));
  double currentTime = getPage().getCurrentTime();
  Assert.assertTrue(
      "Error in play time (expected: " + PLAYTIME + " sec, real: " + currentTime + " sec)",
      getPage().compare(PLAYTIME, currentTime));

  // Release Media Pipeline
  mp.release();
}
 
Example 26
Source Project: kurento-java   Source File: RecorderPlayerTest.java    License: Apache License 2.0 4 votes vote down vote up
public void doTest(MediaProfileSpecType mediaProfileSpecType, String expectedVideoCodec,
    String expectedAudioCodec, String extension) throws Exception {

  // Media Pipeline #1
  MediaPipeline mp = kurentoClient.createMediaPipeline();
  PlayerEndpoint playerEp =
      new PlayerEndpoint.Builder(mp, getPlayerUrl("/video/10sec/green.webm")).build();
  WebRtcEndpoint webRtcEp1 = new WebRtcEndpoint.Builder(mp).build();

  String recordingFile = getRecordUrl(extension);

  RecorderEndpoint recorderEp = new RecorderEndpoint.Builder(mp, recordingFile)
      .withMediaProfile(mediaProfileSpecType).build();
  playerEp.connect(webRtcEp1);

  playerEp.connect(recorderEp);

  final CountDownLatch eosLatch = new CountDownLatch(1);
  playerEp.addEndOfStreamListener(new EventListener<EndOfStreamEvent>() {
    @Override
    public void onEvent(EndOfStreamEvent event) {
      eosLatch.countDown();
    }
  });

  // Test execution #1. Play the video while it is recorded
  launchBrowser(mp, webRtcEp1, playerEp, recorderEp, expectedVideoCodec, expectedAudioCodec,
      recordingFile, EXPECTED_COLOR, 0, 0, PLAYTIME);

  // Wait for EOS
  Assert.assertTrue("No EOS event", eosLatch.await(getPage().getTimeout(), TimeUnit.SECONDS));

  // Release Media Pipeline #1
  mp.release();

  // Reloading browser
  getPage().reload();

  // Media Pipeline #2
  MediaPipeline mp2 = kurentoClient.createMediaPipeline();
  PlayerEndpoint playerEp2 = new PlayerEndpoint.Builder(mp2, recordingFile).build();
  WebRtcEndpoint webRtcEp2 = new WebRtcEndpoint.Builder(mp2).build();
  playerEp2.connect(webRtcEp2);

  // Playing the recording
  launchBrowser(null, webRtcEp2, playerEp2, null, expectedVideoCodec, expectedAudioCodec,
      recordingFile, EXPECTED_COLOR, 0, 0, PLAYTIME);

  // Release Media Pipeline #2
  mp2.release();

  success = true;
}
 
Example 27
Source Project: kurento-java   Source File: RecorderFaceOverlayTest.java    License: Apache License 2.0 4 votes vote down vote up
public void doTest(MediaProfileSpecType mediaProfileSpecType, String expectedVideoCodec,
    String expectedAudioCodec, String extension) throws Exception {

  // Media Pipeline #1
  MediaPipeline mp = kurentoClient.createMediaPipeline();
  PlayerEndpoint playerEp =
      new PlayerEndpoint.Builder(mp, getPlayerUrl("/video/filter/fiwarecut.mp4")).build();
  WebRtcEndpoint webRtcEp1 = new WebRtcEndpoint.Builder(mp).build();

  FaceOverlayFilter filter = new FaceOverlayFilter.Builder(mp).build();
  filter.setOverlayedImage("http://" + getTestFilesHttpPath() + "/img/red-square.png", -0.2F,
      -1.2F, 1.6F, 1.6F);

  String recordingFile = getRecordUrl(extension);
  RecorderEndpoint recorderEp = new RecorderEndpoint.Builder(mp, recordingFile)
      .withMediaProfile(mediaProfileSpecType).build();
  playerEp.connect(filter);
  filter.connect(webRtcEp1);
  filter.connect(recorderEp);

  // Test execution #1. Play and record
  getPage().setThresholdTime(THRESHOLD);
  launchBrowser(mp, webRtcEp1, playerEp, recorderEp, expectedVideoCodec, expectedAudioCodec,
      recordingFile, EXPECTED_COLOR, EXPECTED_COLOR_X, EXPECTED_COLOR_Y, PLAYTIME);

  // Release Media Pipeline #1
  mp.release();

  // Reloading browser
  getPage().reload();

  // Media Pipeline #2
  MediaPipeline mp2 = kurentoClient.createMediaPipeline();
  PlayerEndpoint playerEp2 = new PlayerEndpoint.Builder(mp2, recordingFile).build();
  WebRtcEndpoint webRtcEp2 = new WebRtcEndpoint.Builder(mp2).build();
  playerEp2.connect(webRtcEp2);

  // Playing the recording
  launchBrowser(mp, webRtcEp2, playerEp2, null, expectedVideoCodec, expectedAudioCodec,
      recordingFile, EXPECTED_COLOR, EXPECTED_COLOR_X, EXPECTED_COLOR_Y, PLAYTIME);

  // Release Media Pipeline #2
  mp2.release();

  success = true;
}
 
Example 28
Source Project: kurento-java   Source File: RecorderSwitchPlayerTest.java    License: Apache License 2.0 4 votes vote down vote up
public void doTest(MediaProfileSpecType mediaProfileSpecType, String expectedVideoCodec,
    String expectedAudioCodec, String extension, String[] mediaUrls, Color[] expectedColors)
        throws Exception {

  // Media Pipeline #1
  MediaPipeline mp = kurentoClient.createMediaPipeline();
  final CountDownLatch errorPipelinelatch = new CountDownLatch(1);

  mp.addErrorListener(new EventListener<ErrorEvent>() {

    @Override
    public void onEvent(ErrorEvent event) {
      msgError = "Description:" + event.getDescription() + "; Error code:" + event.getType();
      errorPipelinelatch.countDown();
    }
  });

  int numPlayers = mediaUrls.length;
  PlayerEndpoint[] players = new PlayerEndpoint[numPlayers];

  for (int i = 0; i < numPlayers; i++) {
    players[i] = new PlayerEndpoint.Builder(mp, mediaUrls[i]).build();
  }

  WebRtcEndpoint webRtcEp = new WebRtcEndpoint.Builder(mp).build();

  final CountDownLatch recorderLatch = new CountDownLatch(1);
  String recordingFile = getRecordUrl(extension);
  RecorderEndpoint recorderEp = new RecorderEndpoint.Builder(mp, recordingFile)
      .withMediaProfile(mediaProfileSpecType).build();

  // Test execution
  getPage().subscribeEvents("playing");
  getPage().initWebRtc(webRtcEp, WebRtcChannel.AUDIO_AND_VIDEO, WebRtcMode.RCV_ONLY);

  boolean startRecord = false;
  for (int i = 0; i < numPlayers; i++) {
    players[i].connect(webRtcEp);
    players[i].connect(recorderEp);
    players[i].play();

    if (!startRecord) {

      Assert.assertTrue("Not received media (timeout waiting playing event)",
          getPage().waitForEvent("playing"));
      recorderEp.record();
      startRecord = true;
    }

    waitSeconds(PLAYTIME / numPlayers);
  }

  // Release Media Pipeline #1
  saveGstreamerDot(mp);
  recorderEp.stopAndWait(new Continuation<Void>() {

    @Override
    public void onSuccess(Void result) throws Exception {
      recorderLatch.countDown();
    }

    @Override
    public void onError(Throwable cause) throws Exception {
      recorderLatch.countDown();
    }
  });

  Assert.assertTrue("Not stop properly",
      recorderLatch.await(getPage().getTimeout(), TimeUnit.SECONDS));

  mp.release();

  Assert.assertTrue(msgError, errorPipelinelatch.getCount() == 1);

  // Reloading browser
  getPage().reload();

  checkRecordingFile(recordingFile, "browser", expectedColors, PLAYTIME, expectedVideoCodec,
      expectedAudioCodec);
  success = true;
}
 
Example 29
public void doTestWithPlayer(MediaProfileSpecType mediaProfileSpecType, String expectedVideoCodec,
    String expectedAudioCodec, String extension, String mediaUrlPlayer) throws Exception {
  // Media Pipeline #1
  getPage(BROWSER2).close();
  MediaPipeline mp = kurentoClient.createMediaPipeline();
  final CountDownLatch errorPipelinelatch = new CountDownLatch(1);

  mp.addErrorListener(new EventListener<ErrorEvent>() {

    @Override
    public void onEvent(ErrorEvent event) {
      msgError = "Description:" + event.getDescription() + "; Error code:" + event.getType();
      errorPipelinelatch.countDown();
    }
  });

  WebRtcEndpoint webRtcEpRed = new WebRtcEndpoint.Builder(mp).build();
  PlayerEndpoint playerEp = new PlayerEndpoint.Builder(mp, mediaUrlPlayer).build();

  String recordingFile = getRecordUrl(extension);
  RecorderEndpoint recorderEp = new RecorderEndpoint.Builder(mp, recordingFile)
      .withMediaProfile(mediaProfileSpecType).build();

  PassThrough passThrough = new PassThrough.Builder(mp).build();
  passThrough.connect(recorderEp);

  // Test execution
  getPage(BROWSER1).subscribeLocalEvents("playing");
  long startWebrtc = System.currentTimeMillis();
  getPage(BROWSER1).initWebRtc(webRtcEpRed, WebRtcChannel.AUDIO_AND_VIDEO, WebRtcMode.SEND_ONLY);

  webRtcEpRed.connect(passThrough);
  recorderEp.record();

  Assert.assertTrue("Not received media (timeout waiting playing event)",
      getPage(BROWSER1).waitForEvent("playing"));
  long webrtcRedConnectionTime = System.currentTimeMillis() - startWebrtc;
  Thread.sleep(TimeUnit.SECONDS.toMillis(PLAYTIME) / N_PLAYER);

  startWebrtc = System.currentTimeMillis();

  playerEp.play();
  playerEp.connect(passThrough);
  long playerEpConnectionTime = System.currentTimeMillis() - startWebrtc;
  Thread.sleep(TimeUnit.SECONDS.toMillis(PLAYTIME) / N_PLAYER);

  webRtcEpRed.connect(passThrough);
  Thread.sleep(TimeUnit.SECONDS.toMillis(PLAYTIME) / N_PLAYER);

  // Release Media Pipeline #1
  saveGstreamerDot(mp);

  final CountDownLatch recorderLatch = new CountDownLatch(1);
  recorderEp.stopAndWait(new Continuation<Void>() {

    @Override
    public void onSuccess(Void result) throws Exception {
      recorderLatch.countDown();
    }

    @Override
    public void onError(Throwable cause) throws Exception {
      recorderLatch.countDown();
    }
  });

  Assert.assertTrue("Not stop properly",
      recorderLatch.await(getPage(BROWSER1).getTimeout(), TimeUnit.SECONDS));
  mp.release();

  Assert.assertTrue(msgError, errorPipelinelatch.getCount() == 1);

  final long playtime = PLAYTIME
      + TimeUnit.MILLISECONDS.toSeconds((2 * webrtcRedConnectionTime) + playerEpConnectionTime);

  checkRecordingFile(recordingFile, BROWSER3, EXPECTED_COLORS, playtime, expectedVideoCodec,
      expectedAudioCodec);
  success = true;
}
 
Example 30
Source Project: kurento-java   Source File: RecorderPlayerDisconnectTest.java    License: Apache License 2.0 4 votes vote down vote up
public void doTest(MediaProfileSpecType mediaProfileSpecType, String expectedVideoCodec,
    String expectedAudioCodec, String extension) throws Exception {

  final CountDownLatch recorderLatch = new CountDownLatch(1);

  // Media Pipeline #1
  MediaPipeline mp = kurentoClient.createMediaPipeline();
  PlayerEndpoint playerGreen =
      new PlayerEndpoint.Builder(mp, getPlayerUrl("/video/10sec/green.webm")).build();

  String recordingFile = getRecordUrl(extension);
  RecorderEndpoint recorderEp = new RecorderEndpoint.Builder(mp, recordingFile)
      .withMediaProfile(mediaProfileSpecType).build();

  playerGreen.play();
  recorderEp.record();
  for (int i = 0; i < NUM_SWAPS; i++) {
    if (i % 2 == 0) {
      playerGreen.connect(recorderEp);
    } else {
      playerGreen.disconnect(recorderEp);
    }

    Thread.sleep(TimeUnit.SECONDS.toMillis(PLAYTIME) / NUM_SWAPS);
  }

  // Release Media Pipeline #1
  saveGstreamerDot(mp);

  recorderEp.stopAndWait(new Continuation<Void>() {

    @Override
    public void onSuccess(Void result) throws Exception {
      recorderLatch.countDown();
    }

    @Override
    public void onError(Throwable cause) throws Exception {
      recorderLatch.countDown();
    }
  });

  Assert.assertTrue("Not stop properly",
      recorderLatch.await(getPage().getTimeout(), TimeUnit.SECONDS));

  mp.release();

  // Wait until file exists
  waitForFileExists(recordingFile);

  // Reloading browser
  getPage().reload();

  // Media Pipeline #2
  MediaPipeline mp2 = kurentoClient.createMediaPipeline();
  PlayerEndpoint playerEp2 = new PlayerEndpoint.Builder(mp2, recordingFile).build();
  WebRtcEndpoint webRtcEp2 = new WebRtcEndpoint.Builder(mp2).build();
  playerEp2.connect(webRtcEp2);

  // Playing the recording
  getPage().subscribeEvents("playing");
  getPage().initWebRtc(webRtcEp2, WebRtcChannel.AUDIO_AND_VIDEO, WebRtcMode.RCV_ONLY);
  final CountDownLatch eosLatch = new CountDownLatch(1);
  playerEp2.addEndOfStreamListener(new EventListener<EndOfStreamEvent>() {
    @Override
    public void onEvent(EndOfStreamEvent event) {
      eosLatch.countDown();
    }
  });
  playerEp2.play();

  // Assertions in recording
  final String messageAppend = "[played file with media pipeline]";
  final int playtime = PLAYTIME;

  Assert.assertTrue(
      "Not received media in the recording (timeout waiting playing event) " + messageAppend,
      getPage().waitForEvent("playing"));
  for (Color color : EXPECTED_COLORS) {
    Assert.assertTrue("The color of the recorded video should be " + color + " " + messageAppend,
        getPage().similarColor(color));
  }
  Assert.assertTrue("Not received EOS event in player",
      eosLatch.await(getPage().getTimeout(), TimeUnit.SECONDS));

  double currentTime = getPage().getCurrentTime();
  Assert.assertTrue("Error in play time in the recorded video (expected: " + playtime
      + " sec, real: " + currentTime + " sec) " + messageAppend,
      getPage().compare(playtime, currentTime));

  AssertMedia.assertCodecs(recordingFile, expectedVideoCodec, expectedAudioCodec);
  AssertMedia.assertDuration(recordingFile, TimeUnit.SECONDS.toMillis(playtime),
      TimeUnit.SECONDS.toMillis(getPage().getThresholdTime()));

  // Release Media Pipeline #2
  mp2.release();

  success = true;
}