All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.teamapps.ux.component.webrtc.apiclient.MediaSoupV3RestClient Maven / Gradle / Ivy

There is a newer version: 0.9.194
Show newest version
/*-
 * ========================LICENSE_START=================================
 * TeamApps
 * ---
 * Copyright (C) 2014 - 2024 TeamApps.org
 * ---
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * =========================LICENSE_END==================================
 */

package org.teamapps.ux.component.webrtc.apiclient;

import com.fasterxml.jackson.databind.JsonNode;
import org.glassfish.jersey.client.JerseyClientBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import jakarta.ws.rs.client.Entity;
import jakarta.ws.rs.client.InvocationCallback;
import jakarta.ws.rs.client.WebTarget;
import jakarta.ws.rs.core.MediaType;
import java.lang.invoke.MethodHandles;
import java.time.Duration;
import java.util.List;
import java.util.Set;
import java.util.concurrent.CompletableFuture;

public class MediaSoupV3RestClient implements MediaSoupV3ApiClient {

	private static final Logger LOGGER = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());

	private final WebTarget webTarget;
	private final String serverSecret;

	public MediaSoupV3RestClient(String serverUrl, String serverSecret) {
		webTarget = JerseyClientBuilder.createClient()
				.target(serverUrl)
				.path("api");
		this.serverSecret = serverSecret;
	}

	@Override
	public CompletableFuture startFileStreaming(int workerId, StreamFileRequest streamFileRequest) {
		CompletableFuture future = new CompletableFuture<>();
		webTarget.path(workerId + "/fileStreaming")
				.request(MediaType.APPLICATION_JSON)
				.header("Authorization", "Bearer " + MediaSoupV3TokenGenerator.generateStreamingJwtToken(streamFileRequest.getStreamUuid(), serverSecret, Duration.ofDays(365)))
				.async()
				.post(Entity.json(streamFileRequest), new InvocationCallback() {
					@Override
					public void completed(Void s) {
						future.complete(s);
					}

					@Override
					public void failed(Throwable throwable) {
						future.completeExceptionally(throwable);
					}
				});
		return future;
	}


	public CompletableFuture stopFileStreaming(int workerId, String streamUuid) {
		CompletableFuture future = new CompletableFuture<>();
		webTarget.path(workerId + "/stopFileStreaming")
				.request(MediaType.APPLICATION_JSON)
				.header("Authorization", "Bearer " + MediaSoupV3TokenGenerator.generateStreamingJwtToken(streamUuid, serverSecret, Duration.ofDays(365)))
				.async()
				.post(Entity.json(new StreamData(streamUuid)),
						new InvocationCallback() {
							@Override
							public void completed(Void s) {
								future.complete(s);
							}

							@Override
							public void failed(Throwable throwable) {
								future.completeExceptionally(throwable);
							}
						});
		return future;
	}

	public CompletableFuture startRecording(int workerId, String streamUuid) {
		return startRecording(workerId, streamUuid, null);
	}

	public CompletableFuture startRecording(int workerId, String streamUuid, Set kinds) {
		CompletableFuture future = new CompletableFuture<>();
		webTarget.path(workerId + "/startRecording")
				.request(MediaType.APPLICATION_JSON)
				.header("Authorization", "Bearer " + MediaSoupV3TokenGenerator.generateRecordingJwtToken(serverSecret, Duration.ofDays(365)))
				.async()
				.post(Entity.json(new StreamAndKinds(streamUuid, kinds)), new InvocationCallback() {
					@Override
					public void completed(Void s) {
						future.complete(s);
					}

					@Override
					public void failed(Throwable throwable) {
						future.completeExceptionally(throwable);
					}
				});
		return future;
	}

	public CompletableFuture stopRecording(int workerId, String streamUuid) {
		CompletableFuture future = new CompletableFuture<>();
		webTarget.path(workerId + "/stopRecording")
				.request(MediaType.APPLICATION_JSON)
				.header("Authorization", "Bearer " + MediaSoupV3TokenGenerator.generateRecordingJwtToken(serverSecret, Duration.ofDays(365)))
				.async()
				.post(Entity.json(new StreamAndKinds(streamUuid, Set.of(MediaKind.AUDIO, MediaKind.VIDEO))), new InvocationCallback() {
					@Override
					public void completed(Void s) {
						future.complete(s);
					}

					@Override
					public void failed(Throwable throwable) {
						future.completeExceptionally(throwable);
					}
				});
		return future;
	}

	public CompletableFuture transportStats(List ids) {
		CompletableFuture future = new CompletableFuture<>();
		webTarget
				.path("0/transportStats")
				.request(MediaType.APPLICATION_JSON)
				.header("Authorization", "Bearer " + MediaSoupV3TokenGenerator.generateGeneralApiToken(serverSecret, Duration.ofDays(365)))
				.async()
				.post(Entity.json(new StatsInput(ids)), new InvocationCallback() {
					@Override
					public void completed(JsonNode s) {
						future.complete(s);
					}

					@Override
					public void failed(Throwable throwable) {
						future.completeExceptionally(throwable);
					}
				});
		return future;
	}

	@Override
	public CompletableFuture getWorkerLoad(int workerId) {
		CompletableFuture future = new CompletableFuture<>();
		webTarget.path("0/workerLoad")
				.request(MediaType.APPLICATION_JSON)
				.header("Authorization", "Bearer " + MediaSoupV3TokenGenerator.generateGeneralApiToken(serverSecret, Duration.ofDays(365)))
				.async()
				.post(Entity.json(null), new InvocationCallback() {
					@Override
					public void completed(WorkerLoadData s) {
						future.complete(s.getCurrentLoad());
					}

					@Override
					public void failed(Throwable throwable) {
						future.completeExceptionally(throwable);
					}
				});
		return future;
	}

	@Override
	public CompletableFuture getNumberOfWorkers() {
		CompletableFuture future = new CompletableFuture<>();
		webTarget.path("0/numWorkers")
				.request(MediaType.APPLICATION_JSON)
				.header("Authorization", "Bearer " + MediaSoupV3TokenGenerator.generateGeneralApiToken(serverSecret, Duration.ofDays(365)))
				.async()
				.post(Entity.json(null), new InvocationCallback() {
					@Override
					public void completed(NumWorkersData s) {
						future.complete(s.getNum());
					}

					@Override
					public void failed(Throwable throwable) {
						future.completeExceptionally(throwable);
					}
				});
		return future;
	}

	@Override
	public CompletableFuture> getRecordedStreamUuids() {
		CompletableFuture> future = new CompletableFuture<>();
		webTarget.path("0/recordedStreams")
				.request(MediaType.APPLICATION_JSON)
				.header("Authorization", "Bearer " + MediaSoupV3TokenGenerator.generateRecordingJwtToken(serverSecret, Duration.ofDays(365)))
				.async()
				.post(Entity.json(null), new InvocationCallback() {
					@Override
					public void completed(ListData s) {
						future.complete(s.getList());
					}

					@Override
					public void failed(Throwable throwable) {
						future.completeExceptionally(throwable);
					}
				});
		return future;
	}

	@Override
	public CompletableFuture> getStreamRecordingsForUuid(String streamUuid) {
		CompletableFuture> future = new CompletableFuture<>();
		webTarget.path("0/streamRecordings")
				.request(MediaType.APPLICATION_JSON)
				.header("Authorization", "Bearer " + MediaSoupV3TokenGenerator.generateRecordingJwtToken(serverSecret, Duration.ofDays(365)))
				.async()
				.post(Entity.json(new StreamData(streamUuid)),
						new InvocationCallback() {
							@Override
							public void completed(ListData s) {
								future.complete(s.getList());
							}

							@Override
							public void failed(Throwable throwable) {
								future.completeExceptionally(throwable);
							}
						});
		return future;
	}

	public CompletableFuture deleteStreamRecordings(String streamUuid) {
		CompletableFuture future = new CompletableFuture<>();
		webTarget.path("0/deleteStreamRecordings")
				.request(MediaType.APPLICATION_JSON)
				.header("Authorization", "Bearer " + MediaSoupV3TokenGenerator.generateRecordingJwtToken(serverSecret, Duration.ofDays(365)))
				.async()
				.post(Entity.json(new StreamData(streamUuid)),
						new InvocationCallback() {
							@Override
							public void completed(Void s) {
								future.complete(s);
							}

							@Override
							public void failed(Throwable throwable) {
								future.completeExceptionally(throwable);
							}
						});
		return future;
	}

	public CompletableFuture deleteRecording(String filePathInput) {
		CompletableFuture future = new CompletableFuture<>();
		webTarget.path("0/deleteRecording")
				.request(MediaType.APPLICATION_JSON)
				.header("Authorization", "Bearer " + MediaSoupV3TokenGenerator.generateRecordingJwtToken(serverSecret, Duration.ofDays(365)))
				.async()
				.post(Entity.json(new FilePathInput(filePathInput)),
						new InvocationCallback() {
							@Override
							public void completed(Void s) {
								future.complete(s);
							}

							@Override
							public void failed(Throwable throwable) {
								future.completeExceptionally(throwable);
							}
						});
		return future;
	}


	public CompletableFuture kindsByFile(String filePath, boolean relativePath) {
		CompletableFuture future = new CompletableFuture<>();
		webTarget.path("0/kindsByFile")
				.request(MediaType.APPLICATION_JSON)
				.header("Authorization", "Bearer " + MediaSoupV3TokenGenerator.generateGeneralApiToken(serverSecret, Duration.ofDays(365)))
				.async()
				.post(Entity.json(new KindsByFileInput(filePath, relativePath)),
						new InvocationCallback() {
							@Override
							public void completed(KindsOptionsData s) {
								future.complete(s);
							}

							@Override
							public void failed(Throwable throwable) {
								future.completeExceptionally(throwable);
							}
						});
		return future;
	}


	// public CompletableFuture resumeConsumer(ConsumerData consumerData) {
	// 	CompletableFuture future = new CompletableFuture<>();
	// 	webTarget.path("resumeConsumer").request(MediaType.APPLICATION_JSON).async().post(Entity.json(consumerData), new InvocationCallback() {
	// 		@Override
	// 		public void completed(Void s) {
	// 			future.complete(s);
	// 		}
	//
	// 		@Override
	// 		public void failed(Throwable throwable) {
	// 			future.completeExceptionally(throwable);
	// 		}
	// 	});
	// 	return future;
	// }
	//
	// public CompletableFuture pauseConsumer(ConsumerData consumerData) {
	// 	CompletableFuture future = new CompletableFuture<>();
	// 	webTarget.path("pauseConsumer").request(MediaType.APPLICATION_JSON).async().post(Entity.json(consumerData), new InvocationCallback() {
	// 		@Override
	// 		public void completed(Void s) {
	// 			future.complete(s);
	// 		}
	//
	// 		@Override
	// 		public void failed(Throwable throwable) {
	// 			future.completeExceptionally(throwable);
	// 		}
	// 	});
	// 	return future;
	// }
	//
	// public CompletableFuture setPreferredLayers(ConsumerPreferredLayers consumerPreferredLayers) {
	// 	CompletableFuture future = new CompletableFuture<>();
	// 	webTarget.path("setPreferredLayers").request(MediaType.APPLICATION_JSON).async().post(Entity.json(consumerPreferredLayers), new InvocationCallback() {
	// 		@Override
	// 		public void completed(Void s) {
	// 			future.complete(s);
	// 		}
	//
	// 		@Override
	// 		public void failed(Throwable throwable) {
	// 			future.completeExceptionally(throwable);
	// 		}
	// 	});
	// 	return future;
	// }
	//
	// public CompletableFuture resumeProducer(ProducerData producerData) {
	// 	CompletableFuture future = new CompletableFuture<>();
	// 	webTarget.path("resumeProducer").request(MediaType.APPLICATION_JSON).async().post(Entity.json(producerData), new InvocationCallback() {
	// 		@Override
	// 		public void completed(Void s) {
	// 			future.complete(s);
	// 		}
	//
	// 		@Override
	// 		public void failed(Throwable throwable) {
	// 			future.completeExceptionally(throwable);
	// 		}
	// 	});
	// 	return future;
	// }
	//
	// public CompletableFuture pauseProducer(ProducerData producerData) {
	// 	CompletableFuture future = new CompletableFuture<>();
	// 	webTarget.path("pauseProducer").request(MediaType.APPLICATION_JSON).async().post(Entity.json(producerData), new InvocationCallback() {
	// 		@Override
	// 		public void completed(Void s) {
	// 			future.complete(s);
	// 		}
	//
	// 		@Override
	// 		public void failed(Throwable throwable) {
	// 			future.completeExceptionally(throwable);
	// 		}
	// 	});
	// 	return future;
	// }
	//
	// public CompletableFuture closeProducer(ProducerData producerData) {
	// 	CompletableFuture future = new CompletableFuture<>();
	// 	webTarget.path("closeProducer").request(MediaType.APPLICATION_JSON).async().post(Entity.json(producerData), new InvocationCallback() {
	// 		@Override
	// 		public void completed(Void s) {
	// 			future.complete(s);
	// 		}
	//
	// 		@Override
	// 		public void failed(Throwable throwable) {
	// 			future.completeExceptionally(throwable);
	// 		}
	// 	});
	// 	return future;
	// }
	//
	// public CompletableFuture liveStreaming(LiveStreamRequest liveStreamRequest) {
	// 	CompletableFuture future = new CompletableFuture<>();
	// 	webTarget.path("liveStreaming").request(MediaType.APPLICATION_JSON).async().post(Entity.json(liveStreamRequest), new InvocationCallback() {
	// 		@Override
	// 		public void completed(Void s) {
	// 			future.complete(s);
	// 		}
	//
	// 		@Override
	// 		public void failed(Throwable throwable) {
	// 			future.completeExceptionally(throwable);
	// 		}
	// 	});
	// 	return future;
	// }
	//
	// public CompletableFuture liveToHls(LiveToHlsRequest liveToHlsRequest) {
	// 	CompletableFuture future = new CompletableFuture<>();
	// 	webTarget.path("liveToHls").request(MediaType.APPLICATION_JSON).async().post(Entity.json(liveToHlsRequest), new InvocationCallback() {
	// 		@Override
	// 		public void completed(Void s) {
	// 			future.complete(s);
	// 		}
	//
	// 		@Override
	// 		public void failed(Throwable throwable) {
	// 			future.completeExceptionally(throwable);
	// 		}
	// 	});
	// 	return future;
	// }
	// public CompletableFuture pushToServerInputs(PushStreamInputsRequest pushStreamInputsRequest) {
	// 	CompletableFuture future = new CompletableFuture<>();
	// 	webTarget.path("pushToServerInputs").request(MediaType.APPLICATION_JSON).async().post(Entity.json(pushStreamInputsRequest),
	// 			new InvocationCallback() {
	// 				@Override
	// 				public void completed(PushStreamInputsResponse s) {
	// 					future.complete(s);
	// 				}
	//
	// 				@Override
	// 				public void failed(Throwable throwable) {
	// 					future.completeExceptionally(throwable);
	// 				}
	// 			});
	// 	return future;
	// }
	//
	// public CompletableFuture pushToServerOptions(PushStreamOptionsRequest pushStreamOptionsRequest) {
	// 	CompletableFuture future = new CompletableFuture<>();
	// 	webTarget.path("pushToServerOptions").request(MediaType.APPLICATION_JSON).async().post(Entity.json(pushStreamOptionsRequest),
	// 			new InvocationCallback() {
	// 				@Override
	// 				public void completed(PushStreamOptionsResponse s) {
	// 					future.complete(s);
	// 				}
	//
	// 				@Override
	// 				public void failed(Throwable throwable) {
	// 					future.completeExceptionally(throwable);
	// 				}
	// 			});
	// 	return future;
	// }
	//
	// public CompletableFuture pushToServer(PushStreamRequest pushStreamRequest) {
	// 	CompletableFuture future = new CompletableFuture<>();
	// 	webTarget.path("pushToServer").request(MediaType.APPLICATION_JSON).async().post(Entity.json(pushStreamRequest), new InvocationCallback() {
	// 		@Override
	// 		public void completed(Void s) {
	// 			future.complete(s);
	// 		}
	//
	// 		@Override
	// 		public void failed(Throwable throwable) {
	// 			future.completeExceptionally(throwable);
	// 		}
	// 	});
	// 	return future;
	// }
	//
	// public CompletableFuture pullFromServerInputs(PullStreamInputsRequest pullStreamInputsRequest) {
	// 	CompletableFuture future = new CompletableFuture<>();
	// 	webTarget.path("pullFromServerInputs").request(MediaType.APPLICATION_JSON).async().post(Entity.json(pullStreamInputsRequest),
	// 			new InvocationCallback() {
	// 				@Override
	// 				public void completed(PullStreamInputsResponse s) {
	// 					future.complete(s);
	// 				}
	//
	// 				@Override
	// 				public void failed(Throwable throwable) {
	// 					future.completeExceptionally(throwable);
	// 				}
	// 			});
	// 	return future;
	// }
	// public CompletableFuture requestKeyframe(ConsumerData consumerData) {
	// 	CompletableFuture future = new CompletableFuture<>();
	// 	webTarget.path("requestKeyframe").request(MediaType.APPLICATION_JSON).async().post(Entity.json(consumerData), new InvocationCallback() {
	// 		@Override
	// 		public void completed(Void s) {
	// 			future.complete(s);
	// 		}
	//
	// 		@Override
	// 		public void failed(Throwable throwable) {
	// 			future.completeExceptionally(throwable);
	// 		}
	// 	});
	// 	return future;
	// }

	// public CompletableFuture mixerStart(MixerCreateOptions mixerCreateOptions) {
	// 	CompletableFuture future = new CompletableFuture<>();
	// 	webTarget.path("mixerStart").request(MediaType.APPLICATION_JSON).async().post(Entity.json(mixerCreateOptions), new InvocationCallback() {
	// 		@Override
	// 		public void completed(MixerInput s) {
	// 			future.complete(s);
	// 		}
	//
	// 		@Override
	// 		public void failed(Throwable throwable) {
	// 			future.completeExceptionally(throwable);
	// 		}
	// 	});
	// 	return future;
	// }
	//
	// public CompletableFuture mixerClose(MixerInput mixerInput) {
	// 	CompletableFuture future = new CompletableFuture<>();
	// 	webTarget.path("mixerClose").request(MediaType.APPLICATION_JSON).async().post(Entity.json(mixerInput), new InvocationCallback() {
	// 		@Override
	// 		public void completed(Void s) {
	// 			future.complete(s);
	// 		}
	//
	// 		@Override
	// 		public void failed(Throwable throwable) {
	// 			future.completeExceptionally(throwable);
	// 		}
	// 	});
	// 	return future;
	// }
	//
	// public CompletableFuture mixerAdd(MixerAddAudioData mixerAddAudioData|MixerAddVideoData) {
	// 	CompletableFuture future = new CompletableFuture<>();
	// 	webTarget.path("mixerAdd").request(MediaType.APPLICATION_JSON).async().post(Entity.json(mixerAddAudioData), new InvocationCallback() {
	// 		@Override
	// 		public void completed(Void s) {
	// 			future.complete(s);
	// 		}
	//
	// 		@Override
	// 		public void failed(Throwable throwable) {
	// 			future.completeExceptionally(throwable);
	// 		}
	// 	});
	// 	return future;
	// }
	//
	// public CompletableFuture mixerUpdate(MixerUpdateData mixerUpdateData) {
	// 	CompletableFuture future = new CompletableFuture<>();
	// 	webTarget.path("mixerUpdate").request(MediaType.APPLICATION_JSON).async().post(Entity.json(mixerUpdateData), new InvocationCallback() {
	// 		@Override
	// 		public void completed(Void s) {
	// 			future.complete(s);
	// 		}
	//
	// 		@Override
	// 		public void failed(Throwable throwable) {
	// 			future.completeExceptionally(throwable);
	// 		}
	// 	});
	// 	return future;
	// }
	//
	// public CompletableFuture mixerRemove(MixerRemoveData mixerRemoveData) {
	// 	CompletableFuture future = new CompletableFuture<>();
	// 	webTarget.path("mixerRemove").request(MediaType.APPLICATION_JSON).async().post(Entity.json(mixerRemoveData), new InvocationCallback() {
	// 		@Override
	// 		public void completed(Void s) {
	// 			future.complete(s);
	// 		}
	//
	// 		@Override
	// 		public void failed(Throwable throwable) {
	// 			future.completeExceptionally(throwable);
	// 		}
	// 	});
	// 	return future;
	// }
	//
	// public CompletableFuture mixerPipeStart(MixerPipeLiveData|MixerPipeRecordingData|MixerPipeRtmpData|MixerPipeHlsData mixerPipeLiveData) {
	// 	CompletableFuture future = new CompletableFuture<>();
	// 	webTarget.path("mixerPipeStart").request(MediaType.APPLICATION_JSON).async().post(Entity.json(mixerPipeLiveData), new InvocationCallback() {
	// 		@Override
	// 		public void completed(MixerPipeInput s) {
	// 			future.complete(s);
	// 		}
	//
	// 		@Override
	// 		public void failed(Throwable throwable) {
	// 			future.completeExceptionally(throwable);
	// 		}
	// 	});
	// 	return future;
	// }
	//
	// public CompletableFuture mixerPipeStop(MixerPipeStopInput mixerPipeStopInput) {
	// 	CompletableFuture future = new CompletableFuture<>();
	// 	webTarget.path("mixerPipeStop").request(MediaType.APPLICATION_JSON).async().post(Entity.json(mixerPipeStopInput), new InvocationCallback() {
	// 		@Override
	// 		public void completed(Void s) {
	// 			future.complete(s);
	// 		}
	//
	// 		@Override
	// 		public void failed(Throwable throwable) {
	// 			future.completeExceptionally(throwable);
	// 		}
	// 	});
	// 	return future;
	// }
	// public CompletableFuture setMaxIncomingBitrate(TransportBitrateData transportBitrateData) {
	// 	CompletableFuture future = new CompletableFuture<>();
	// 	webTarget.path("setMaxIncomingBitrate").request(MediaType.APPLICATION_JSON).async().post(Entity.json(transportBitrateData), new InvocationCallback() {
	// 		@Override
	// 		public void completed(Void s) {
	// 			future.complete(s);
	// 		}
	//
	// 		@Override
	// 		public void failed(Throwable throwable) {
	// 			future.completeExceptionally(throwable);
	// 		}
	// 	});
	// 	return future;
	// }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy