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