/**
   * This method waits until the pipeline has had an opportunity to shutdown and if it surpasses the
   * maximum timeout value it will be manually stopped.
   */
  public void stop(long timeout) {
    // We must stop the capture as soon as possible, then check whatever needed
    for (CaptureDeviceBin captureDeviceBin : captureDeviceBins) {
      captureDeviceBin.shutdown();
    }

    long startWait = System.currentTimeMillis();

    while (pipeline != null
        && (pipeline.getState() != State.PAUSED || pipeline.getState() != State.NULL)) {
      try {
        Thread.sleep(WAIT_FOR_NULL_SLEEP_TIME);
      } catch (InterruptedException e) {
      }
      // If we've timed out then force kill the pipeline
      if (System.currentTimeMillis() - startWait >= timeout) {
        if (pipeline != null) {
          logger.debug("The pipeline took too long to shut down, now sending State.NULL.");
          pipeline.setState(State.NULL);
        }
        pipeline = null;
      }
    }

    if (pipeline != null) {
      pipeline.setState(State.NULL);
    }
    pipeline = null;
  }
  public void debugGST() {

    List<Element> sinks = pipe.getSinks();
    List<Element> elements = pipe.getElementsRecursive();
    List<Element> sources = pipe.getSources();

    State state = pipe.getState();

    int a = 1;
  }
  /**
   * Creates the gStreamer pipeline and blocks until it starts successfully
   *
   * @param newRec The RecordingImpl of the capture we wish to perform.
   * @return The recording ID (equal to newRec.getID()) or null in the case of an error
   */
  public void start(RecordingImpl newRec) {
    // Create the pipeline
    try {
      pipeline = create(newRec.getProperties(), false);
    } catch (UnsatisfiedLinkError e) {
      throw new UnableToStartCaptureException(
          e.getMessage() + " : please add libjv4linfo.so to /usr/lib to correct this issue.");
    }

    // Check if the pipeline came up ok
    if (pipeline == null) {
      // logger.error("Capture {} could not start, pipeline was null!", newRec.getID());
      captureFailureHandler.resetOnFailure(newRec.getID());
      throw new UnableToStartCaptureException(
          "Capture " + newRec.getID() + " could not start, pipeline was null!");
    }

    logger.info("Initializing devices for capture.");

    hookUpBus();

    // Grab time to wait for pipeline to start
    int wait;
    String waitProp = newRec.getProperty(CaptureParameters.CAPTURE_START_WAIT);
    if (waitProp != null) {
      wait = Integer.parseInt(waitProp);
    } else {
      wait = 5; // Default taken from gstreamer docs
    }

    pipeline.debugToDotFile(Pipeline.DEBUG_GRAPH_SHOW_ALL, pipeline.getName());
    // Try and start the pipeline
    pipeline.play();
    if (pipeline.getState(wait * GStreamerPipeline.GST_SECOND) != State.PLAYING) {
      // In case of an error call stop to clean up the pipeline.
      logger.debug("Pipeline was unable to start after " + wait + " seconds.");
      stop(GStreamerPipeline.DEFAULT_PIPELINE_SHUTDOWN_TIMEOUT);
      throw new UnableToStartCaptureException(
          "Unable to start pipeline after " + wait + " seconds.  Aborting!");
    }
    logger.info("{} started.", pipeline.getName());
  }