private void createPlaylist() {
    String showName = MediaFileAPI.GetMediaTitle(mediaFile);
    int numberOfMediaFileSegments = MediaFileAPI.GetNumberOfSegments(mediaFile);
    Log.debug("Number of media file segments: " + numberOfMediaFileSegments);

    for (int i = 0; i < numberOfMediaFileSegments; i++) {
      // get length of current media file segment
      long mediaFileSegmentDurationInMillis = MediaFileAPI.GetDurationForSegment(mediaFile, i);
      long mediaFileSegmentDurationInSeconds =
          mediaFileSegmentDurationInMillis / 1000; // milliseconds to seconds
      Log.debug(
          "mediaFileSegmentDurationInMillis (" + i + "): " + mediaFileSegmentDurationInMillis);
      Log.debug(
          "mediaFileSegmentDurationInSeconds (" + i + "): " + mediaFileSegmentDurationInSeconds);

      int sequence = 0;
      for (int j = 0; j < mediaFileSegmentDurationInSeconds; j += SegmentPlaylist.TARGET_DURATION) {
        long currentDuration =
            Math.min(SegmentPlaylist.TARGET_DURATION, mediaFileSegmentDurationInSeconds - j);

        Segment newSegment = new Segment(currentDuration, sequence, i, showName);

        segmentList.add(newSegment);

        sequence++;
      }
    }
    if (segmentList != null) {
      Log.debug("segmentList size: " + segmentList.size());
    }
  }
  public SegmentPlaylist(
      HttpServletRequest req, Object mediaFile, String conversionId, String quality) {
    this.req = req;
    this.mediaFile = mediaFile;
    this.mediaFileId = MediaFileAPI.GetMediaFileID(mediaFile);
    this.segmentList = new ArrayList<Segment>();
    this.conversionId = conversionId;
    this.quality = quality;
    this.isFileCurrentlyRecording = MediaFileAPI.IsFileCurrentlyRecording(mediaFile);

    if (!this.isFileCurrentlyRecording) {
      createPlaylist();
    } else {
      // retry until there's at least one full segment recorded
      while (this.isFileCurrentlyRecording && segmentList.size() <= 1) {
        this.segmentList.clear();

        createPlaylist();

        long mediaFileSegmentDurationInMillis = MediaFileAPI.GetDurationForSegment(mediaFile, 0);
        long mediaFileSegmentDurationInSeconds =
            mediaFileSegmentDurationInMillis / 1000; // milliseconds to seconds

        if (this.isFileCurrentlyRecording
            && (mediaFileSegmentDurationInSeconds < SegmentPlaylist.TARGET_DURATION)) {
          try {
            // wait until we'll have a full first segment to return
            Thread.sleep(
                (SegmentPlaylist.TARGET_DURATION - mediaFileSegmentDurationInSeconds + 1) * 1000);
          } catch (InterruptedException e) {
            Log.warn(e.getMessage());
          }
        }

        this.isFileCurrentlyRecording = MediaFileAPI.IsFileCurrentlyRecording(mediaFile);
      }
    }
  }