コード例 #1
0
 private void logURIError(String url, MediaStreamAnalyzerResult result, HLSMediaFile file) {
   String message = String.format(ErrorType.INVALID_URI_FOUND.getMessageFormat(), url);
   ErrorLogEntry entry =
       new ErrorLogEntry(
           ErrorType.INVALID_URI_FOUND,
           file != null ? file.getFileName() : HLSConstants.APPLICATION,
           message,
           HLSConstants.FILE_LEVEL);
   result.addError(entry);
 }
コード例 #2
0
  private void analyzeTransportStream(HLSMediaFile file, String tsURL) {
    System.out.println(tsURL);
    IContainer container = IContainer.make();
    IContainerFormat format = IContainerFormat.make();
    format.setInputFormat(".ts");

    int returnCode = container.open(tsURL, IContainer.Type.READ, format);
    if (returnCode >= 0) {

      BigDecimal bitRate = BigDecimal.valueOf(container.getBitRate());
      BigDecimal duration = BigDecimal.valueOf(container.getDuration());
      BigDecimal inSeconds = duration.divide(BigDecimal.valueOf(1000000));
      System.out.println(bitRate);
      System.out.println(inSeconds.toString());
      TransportStreamFileInfo tsFileInfo = new TransportStreamFileInfo();
      tsFileInfo.setDuration(inSeconds);
      tsFileInfo.setCalculatedBitRate(bitRate);
      tsFileInfo.setFileName(tsURL);
      file.addTsFiles(tsFileInfo);
    }
    container.close();
  }
コード例 #3
0
  private void processFiles(String urlStr, MediaStreamAnalyzerResult result, boolean isAnalyzeTS) {
    HLSMediaFile file = new HLSMediaFile(urlStr);
    int lastIndex = urlStr.lastIndexOf('/');
    String baseUrl = urlStr.substring(0, lastIndex + 1);
    InputStreamReader inStreamReader = null;
    BufferedReader reader = null;
    LineNumberReader lineNumberReader = null;
    try {
      URLConnection connection = getConnection(urlStr, result);
      InputStream inStream = (InputStream) connection.getContent();
      inStreamReader = new InputStreamReader(inStream, Charset.forName("UTF-8"));
      reader = new BufferedReader(inStreamReader);
      lineNumberReader = new LineNumberReader(reader);
      String line = "";

      do {
        line = lineNumberReader.readLine();

        if (line != null && !line.trim().isEmpty()) {
          MediaFileTagType lineType = MediaFileTagType.findTagByLine(line);
          HLSMediaFileLineInfo lineInfo = new HLSMediaFileLineInfo();
          lineInfo.setLineData(line);
          lineInfo.setLineNumber(lineNumberReader.getLineNumber());
          lineInfo.setLineType(lineType);
          if (lineType.equals(MediaFileTagType.EXT_X_VERSION)) {
            file.setVersion(Integer.parseInt(HLSUtility.getTagValue(line)));
          }
          file.addFileLine(lineInfo);
          if (lineType.isURI() && !lineType.equals(MediaFileTagType.TRANSPORT_STREAM_URI)) {
            String nextURL = this.getNextURL(line, baseUrl, lineType);
            processFiles(nextURL, result, isAnalyzeTS);
          } else if (lineType.equals(MediaFileTagType.TRANSPORT_STREAM_URI)) {
            // verify TS file exists
            String tsURL = this.getNextURL(line, baseUrl, lineType);
            try {

              URL url = new URL(tsURL);
              HttpURLConnection tsConnection = (HttpURLConnection) url.openConnection();
              tsConnection.setRequestMethod(HTTPMethods.HEAD.value());
              tsConnection.connect();
              int responseCode = tsConnection.getResponseCode();
              if (responseCode == HttpURLConnection.HTTP_NOT_FOUND) {
                logURIError(tsURL, result, file);
              } else if (isAnalyzeTS) {
                //
                analyzeTransportStream(file, tsURL);
              }
              //
            } catch (MalformedURLException e) {
              logURIError(tsURL, result, file);
            } catch (IOException e) {
              logURIError(tsURL, result, file);
            }
          }
        }
        //
      } while (line != null);

      // After processing file, determine it's type.
      MediaFileType fileType = MediaFileType.matchFileTypeOnIdentifyingTag(file.getFileLines());
      file.setFileType(fileType);
      result.getFiles().add(file);

    } catch (MalformedURLException e) {
      logURIError(urlStr, result, file);
    } catch (IOException e) {
      logURIError(urlStr, result, file);
    } finally {
      try {
        if (inStreamReader != null) {
          inStreamReader.close();
        }
        if (lineNumberReader != null) {
          lineNumberReader.close();
        }
        if (lineNumberReader != null) {
          reader.close();
        }
      } catch (IOException io) {
        System.err.println("Unable to close readers!");
      }
    }
  }