001package net.bramp.ffmpeg.info;
002
003import java.util.ArrayList;
004import java.util.Arrays;
005import java.util.Collections;
006import java.util.List;
007import net.bramp.ffmpeg.shared.CodecType;
008import org.apache.commons.lang3.builder.EqualsBuilder;
009import org.apache.commons.lang3.builder.HashCodeBuilder;
010
011/** Represents the input or output stream pattern of an FFmpeg filter. */
012public class FilterPattern {
013  /**
014   * Indicates whether this pattern represents a source or a sink and therefore has no other
015   * options.
016   */
017  private final boolean sinkOrSource;
018
019  /** Indicates whether this pattern accepts a variable number of streams. */
020  private final boolean variableStreams;
021
022  /** Contains a pattern matching the stream types supported. */
023  private final List<CodecType> streams;
024
025  /** Parses a filter pattern string describing the supported stream types. */
026  public FilterPattern(String pattern) {
027    this.sinkOrSource = pattern.contains("|");
028    this.variableStreams = pattern.contains("N");
029    List<CodecType> streams = new ArrayList<>();
030
031    for (int i = 0; i < pattern.length(); i++) {
032      final char c = pattern.charAt(i);
033
034      if (c == '|' || c == 'N') {
035        // These symbols are handled separately
036        continue;
037      }
038      if (c == 'A') {
039        streams.add(CodecType.AUDIO);
040      } else if (c == 'V') {
041        streams.add(CodecType.VIDEO);
042      } else {
043        throw new IllegalStateException("Unsupported character in filter pattern " + c);
044      }
045    }
046
047    this.streams = Collections.unmodifiableList(streams);
048  }
049
050  public boolean isSinkOrSource() {
051    return sinkOrSource;
052  }
053
054  public boolean isVariableStreams() {
055    return variableStreams;
056  }
057
058  public List<CodecType> getStreams() {
059    return streams;
060  }
061
062  @Override
063  public boolean equals(Object obj) {
064    return EqualsBuilder.reflectionEquals(this, obj);
065  }
066
067  @Override
068  public String toString() {
069    if (isSinkOrSource()) {
070      return "|";
071    }
072
073    if (isVariableStreams()) {
074      return "N";
075    }
076
077    return Arrays.toString(this.streams.toArray());
078  }
079
080  @Override
081  public int hashCode() {
082    return HashCodeBuilder.reflectionHashCode(this);
083  }
084}