001package net.bramp.ffmpeg;
002
003import static com.google.common.base.Preconditions.checkArgument;
004import static com.google.common.base.Preconditions.checkNotNull;
005
006import com.google.common.base.CharMatcher;
007import com.google.common.base.Strings;
008import com.google.common.collect.ImmutableList;
009import java.net.URI;
010import java.util.List;
011import javax.annotation.Nullable;
012
013public final class Preconditions {
014
015  private static final List<String> rtps = ImmutableList.of("rtsp", "rtp", "rtmp");
016  private static final List<String> udpTcp = ImmutableList.of("udp", "tcp");
017
018  Preconditions() {
019    throw new AssertionError("No instances for you!");
020  }
021
022  /**
023   * Ensures the argument is not null, empty string, or just whitespace.
024   *
025   * @param arg The argument
026   * @param errorMessage The exception message to use if the check fails
027   * @return The passed in argument if it is not blank
028   */
029  public static String checkNotEmpty(String arg, @Nullable Object errorMessage) {
030    boolean empty = Strings.isNullOrEmpty(arg) || CharMatcher.whitespace().matchesAllOf(arg);
031    checkArgument(!empty, errorMessage);
032    return arg;
033  }
034
035  /**
036   * Checks if the URI is valid for streaming to.
037   *
038   * @param uri The URI to check
039   * @return The passed in URI if it is valid
040   * @throws IllegalArgumentException if the URI is not valid.
041   */
042  public static URI checkValidStream(URI uri) throws IllegalArgumentException {
043    String scheme = checkNotNull(uri).getScheme();
044    scheme = checkNotNull(scheme, "URI is missing a scheme").toLowerCase();
045
046    if (rtps.contains(scheme)) {
047      return uri;
048    }
049
050    if (udpTcp.contains(scheme)) {
051      if (uri.getPort() == -1) {
052        throw new IllegalArgumentException("must set port when using udp or tcp scheme");
053      }
054      return uri;
055    }
056
057    throw new IllegalArgumentException("not a valid output URL, must use rtp/tcp/udp scheme");
058  }
059}