Exemple #1
0
  protected void revertFileName(TranscodeFileImpl tf) throws TranscodeException {

    File cache_file = tf.getCacheFile();

    if (cache_file.exists()) {

      Debug.out("Cache file already allocated, can't rename");

      return;
    }

    File source_file = tf.getSourceFile().getFile(true);

    String original_name = source_file.getName();

    int pos = original_name.indexOf('.');

    if (pos == -1) {

      return;
    }

    String cf_name = cache_file.getName();

    if (cf_name.endsWith(original_name.substring(pos))) {

      return;
    }

    try {
      synchronized (this) {
        if (device_files == null) {

          loadDeviceFile();
        }

        String reverted_name = allocateUniqueFileName(original_name);

        tf.setCacheFile(new File(cache_file.getParentFile(), reverted_name));
      }
    } catch (Throwable e) {

      throw (new TranscodeException("File name revertion failed", e));
    }
  }
Exemple #2
0
  public void generateTT(IndentWriter writer) {
    TranscodeFileImpl[] files = getFiles();

    int complete = 0;
    int copied = 0;
    int deleted = 0;
    int template = 0;

    for (TranscodeFileImpl f : files) {

      if (f.isComplete()) {

        complete++;
      }

      if (f.isCopiedToDevice()) {

        copied++;
      }

      if (f.isDeleted()) {

        deleted++;
      }

      if (f.isTemplate()) {

        template++;
      }
    }

    writer.println(
        "files="
            + files.length
            + ", comp="
            + complete
            + ", copied="
            + copied
            + ", deleted="
            + deleted
            + ", template="
            + template);
  }
Exemple #3
0
  public TranscodeFileImpl allocateFile(
      TranscodeProfile profile, boolean no_xcode, DiskManagerFileInfo file, boolean for_job)
      throws TranscodeException {
    TranscodeFileImpl result = null;

    setError(KEY_FILE_ALLOC_ERROR, null);

    try {
      synchronized (this) {
        if (device_files == null) {

          loadDeviceFile();
        }

        String key =
            ByteFormatter.encodeString(file.getDownloadHash())
                + ":"
                + file.getIndex()
                + ":"
                + profile.getUID();

        if (device_files.containsKey(key)) {

          try {
            result = new TranscodeFileImpl(this, key, device_files);

          } catch (Throwable e) {

            device_files.remove(key);

            log("Failed to deserialise transcode file", e);
          }
        }

        if (result == null) {

          String ext = profile.getFileExtension();

          String target_file = file.getFile(true).getName();

          if (ext != null && !no_xcode) {

            int pos = target_file.lastIndexOf('.');

            if (pos != -1) {

              target_file = target_file.substring(0, pos);
            }

            target_file += ext;
          }

          target_file = allocateUniqueFileName(target_file);

          File output_file = getWorkingDirectory(true);

          if (!output_file.canWrite()) {

            throw (new TranscodeException(
                "Can't write to transcode folder '" + output_file.getAbsolutePath() + "'"));
          }

          output_file = new File(output_file.getAbsoluteFile(), target_file);

          result =
              new TranscodeFileImpl(
                  this, key, profile.getName(), device_files, output_file, for_job);

          result.setSourceFile(file);

          device_files_last_mod = SystemTime.getMonotonousTime();

          device_files_dirty = true;

        } else {

          result.setSourceFile(file);

          result.setProfileName(profile.getName());
        }
      }
    } catch (Throwable e) {

      setError(KEY_FILE_ALLOC_ERROR, Debug.getNestedExceptionMessage(e));

      throw (new TranscodeException("File allocation failed", e));
    }

    for (TranscodeTargetListener l : listeners) {

      try {
        l.fileAdded(result);

      } catch (Throwable e) {

        Debug.out(e);
      }
    }

    return (result);
  }
Exemple #4
0
  protected void deleteFile(TranscodeFileImpl file, boolean delete_contents, boolean remove)
      throws TranscodeException {
    if (file.isDeleted()) {

      return;
    }

    if (delete_contents) {

      File f = file.getCacheFile();

      int time = 0;

      while (f.exists() && !f.delete()) {

        if (time > 3000) {

          log("Failed to remove file '" + f.getAbsolutePath() + "'");

          break;

        } else {

          try {
            Thread.sleep(500);

          } catch (Throwable e) {

          }

          time += 500;
        }
      }
    }

    if (remove) {

      try {
        // fire the listeners FIRST as this gives listeners a chance to extract data
        // from the file before it is deleted (otherwise operations fail with 'file has been
        // deleted'

        for (TranscodeTargetListener l : listeners) {

          try {
            l.fileRemoved(file);

          } catch (Throwable e) {

            Debug.out(e);
          }
        }

        synchronized (this) {
          if (device_files == null) {

            loadDeviceFile();

          } else {

            device_files_last_mod = SystemTime.getMonotonousTime();
          }

          device_files.remove(file.getKey());

          device_files_dirty = true;
        }

      } catch (Throwable e) {

        throw (new TranscodeException("Delete failed", e));
      }
    }
  }
  protected boolean generate(TrackerWebPageRequest request, TrackerWebPageResponse response)
      throws IOException {
    InetSocketAddress local_address = request.getLocalAddress();

    if (local_address == null) {

      return (false);
    }

    String host = local_address.getAddress().getHostAddress();

    String url = request.getURL();

    if (TRACE) {
      System.out.println("url: " + url);
    }

    if (!url.startsWith("/TiVoConnect?")) {

      return (false);
    }

    int pos = url.indexOf('?');

    if (pos == -1) {

      return (false);
    }

    String[] bits = url.substring(pos + 1).split("&");

    Map<String, String> args = new HashMap<String, String>();

    for (String bit : bits) {

      String[] x = bit.split("=");

      args.put(x[0], URLDecoder.decode(x[1], "UTF-8"));
    }

    if (TRACE) {
      System.out.println("args: " + args);
    }

    // root folder /TiVoConnect?Command=QueryContainer&Container=%2F

    String command = args.get("Command");

    if (command == null) {

      return (false);
    }

    String reply = null;

    if (command.equals("QueryContainer")) {

      String container = args.get("Container");

      if (container == null) {

        return (false);
      }

      if (container.equals("/")) {

        reply =
            "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>"
                + NL
                + "<TiVoContainer>"
                + NL
                + "    <Details>"
                + NL
                + "        <Title>"
                + server_name
                + "</Title>"
                + NL
                + "        <ContentType>x-container/tivo-server</ContentType>"
                + NL
                + "        <SourceFormat>x-container/folder</SourceFormat>"
                + NL
                + "        <TotalItems>1</TotalItems>"
                + NL
                + "    </Details>"
                + NL
                + "    <Item>"
                + NL
                + "        <Details>"
                + NL
                + "            <Title>"
                + server_name
                + "</Title>"
                + NL
                + "            <ContentType>x-container/tivo-videos</ContentType>"
                + NL
                + "            <SourceFormat>x-container/folder</SourceFormat>"
                + NL
                + "        </Details>"
                + NL
                + "        <Links>"
                + NL
                + "            <Content>"
                + NL
                + "                <Url>/TiVoConnect?Command=QueryContainer&amp;Container="
                + urlencode("/Content")
                + "</Url>"
                + NL
                + "                <ContentType>x-container/tivo-videos</ContentType>"
                + NL
                + "            </Content>"
                + NL
                + "        </Links>"
                + NL
                + "    </Item>"
                + NL
                + "    <ItemStart>0</ItemStart>"
                + NL
                + "    <ItemCount>1</ItemCount>"
                + NL
                + "</TiVoContainer>";

      } else if (container.startsWith("/Content")) {

        boolean show_categories = getShowCategories();

        String recurse = args.get("Recurse");

        if (recurse != null && recurse.equals("Yes")) {

          show_categories = false;
        }

        TranscodeFileImpl[] tfs = getFiles();

        String category_or_tag = null;

        Map<String, ContainerInfo> categories_or_tags = null;

        if (show_categories) {

          if (container.startsWith("/Content/")) {

            category_or_tag = container.substring(container.lastIndexOf('/') + 1);

          } else {

            categories_or_tags = new HashMap<String, ContainerInfo>();
          }
        }

        // build list of applicable items

        List<ItemInfo> items = new ArrayList<ItemInfo>(tfs.length);

        for (TranscodeFileImpl file : tfs) {

          if (!file.isComplete()) {

            // see if we can set up a stream xcode for this but only if we
            // know the duration and the transcode is in progress (done in setup)

            if (!setupStreamXCode(file)) {

              continue;
            }
          }

          if (category_or_tag != null) {

            boolean hit = false;

            String[] cats = file.getCategories();
            String[] tags = file.getTags(true);

            for (String[] strs : new String[][] {cats, tags}) {

              for (String c : strs) {

                if (c.equals(category_or_tag)) {

                  hit = true;
                }
              }
            }

            if (!hit) {

              continue;
            }
          }

          FileInfo info = new FileInfo(file, host);

          if (info.isOK()) {

            boolean skip = false;

            if (categories_or_tags != null) {

              String[] cats = file.getCategories();
              String[] tags = file.getTags(true);

              if (cats.length > 0 || tags.length > 0) {

                skip = true;

                for (String[] strs : new String[][] {cats, tags}) {

                  for (String s : strs) {

                    ContainerInfo cont = categories_or_tags.get(s);

                    if (cont == null) {

                      items.add(cont = new ContainerInfo(s));

                      categories_or_tags.put(s, cont);
                    }

                    cont.addChild();
                  }
                }
              }
            }

            if (!skip) {

              items.add(info);
            }
          }
        }

        // sort

        String sort_order = args.get("SortOrder");

        if (sort_order != null) {

          String[] keys = Constants.PAT_SPLIT_COMMA.split(sort_order);

          final List<Comparator<ItemInfo>> comparators = new ArrayList<Comparator<ItemInfo>>();
          final List<Boolean> reverses = new ArrayList<Boolean>();

          for (String key : keys) {

            boolean reverse = false;

            if (key.startsWith("!")) {

              reverse = true;

              key = key.substring(1);
            }

            Comparator<ItemInfo> comp = sort_comparators.get(key);

            if (comp != null) {

              comparators.add(comp);
              reverses.add(reverse);
            }
          }

          if (comparators.size() > 0) {

            Collections.sort(
                items,
                new Comparator<ItemInfo>() {
                  public int compare(ItemInfo i1, ItemInfo i2) {
                    for (int i = 0; i < comparators.size(); i++) {

                      Comparator<ItemInfo> comp = comparators.get(i);

                      int res = comp.compare(i1, i2);

                      if (res != 0) {

                        if (reverses.get(i)) {

                          if (res < 0) {

                            res = 1;

                          } else {

                            res = -1;
                          }
                        }

                        return (res);
                      }
                    }

                    return (0);
                  }
                });
          }
        }

        // select items to return

        String item_count = args.get("ItemCount");
        String anchor_offset = args.get("AnchorOffset");
        String anchor = args.get("AnchorItem");

        int num_items;

        if (item_count == null) {

          num_items = items.size();

        } else {

          // can be negative if X items from end

          num_items = Integer.parseInt(item_count);
        }

        int
            anchor_index; // either one before or one after item to be returned depending on count
                          // +ve/-ve

        if (num_items < 0) {

          anchor_index = items.size();

        } else {

          anchor_index = -1;
        }

        if (anchor != null) {

          for (int i = 0; i < items.size(); i++) {

            ItemInfo info = items.get(i);

            if (anchor.equals(info.getLinkURL())) {

              anchor_index = i;
            }
          }
        }

        if (anchor_offset != null) {

          anchor_index += Integer.parseInt(anchor_offset);

          if (anchor_index < -1) {

            anchor_index = -1;

          } else if (anchor_index > items.size()) {

            anchor_index = items.size();
          }
        }

        int start_index;
        int end_index;

        if (num_items > 0) {

          start_index = anchor_index + 1;

          end_index = anchor_index + num_items;

        } else {

          start_index = anchor_index + num_items;

          end_index = anchor_index - 1;
        }

        if (start_index < 0) {

          start_index = 0;
        }

        if (end_index >= items.size()) {

          end_index = items.size() - 1;
        }

        int num_to_return = end_index - start_index + 1;

        if (num_to_return < 0) {

          num_to_return = 0;
        }

        String machine = getMachineName();

        if (machine == null) {

          // default until we find out what it is - can't see any way to get it apart from wait for
          // broadcast

          machine = "TivoHDDVR";
        }

        String header =
            "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>"
                + NL
                + "<TiVoContainer>"
                + NL
                + "    <Tivos>"
                + NL
                + "      <Tivo>"
                + machine
                + "</Tivo>"
                + NL
                + "    </Tivos>"
                + NL
                + "    <ItemStart>"
                + start_index
                + "</ItemStart>"
                + NL
                + "    <ItemCount>"
                + num_to_return
                + "</ItemCount>"
                + NL
                + "    <Details>"
                + NL
                + "        <Title>"
                + escape(container)
                + "</Title>"
                + NL
                + "        <ContentType>x-container/tivo-videos</ContentType>"
                + NL
                + "        <SourceFormat>x-container/folder</SourceFormat>"
                + NL
                + "        <TotalItems>"
                + items.size()
                + "</TotalItems>"
                + NL
                + "    </Details>"
                + NL;

        reply = header;

        for (int i = start_index; i <= end_index; i++) {

          ItemInfo item = items.get(i);

          if (item instanceof FileInfo) {

            FileInfo file = (FileInfo) item;

            long file_size = file.getTargetSize();

            String title = escape(file.getName());
            String desc = title;

            int MAX_TITLE_LENGTH = 30;

            if (title.length() > MAX_TITLE_LENGTH) {

              // TiVo has problems displaying a truncated title if it has
              // no spaces in it

              String temp = "";

              for (int j = 0; j < title.length(); j++) {

                char c = title.charAt(j);

                if (Character.isLetterOrDigit(c)) {

                  temp += c;
                } else {

                  temp += ' ';
                }
              }

              int space_pos = temp.indexOf(' ');

              if (space_pos == -1 || space_pos > MAX_TITLE_LENGTH) {

                temp = temp.substring(0, 30) + "...";
              }

              title = temp;
            }

            reply +=
                "    <Item>"
                    + NL
                    + "        <Details>"
                    + NL
                    + "            <Title>"
                    + title
                    + "</Title>"
                    + NL
                    + "            <ContentType>video/x-tivo-mpeg</ContentType>"
                    + NL
                    + "            <SourceFormat>video/x-ms-wmv</SourceFormat>"
                    + NL;

            if (file_size > 0) {
              reply += "            <SourceSize>" + file_size + "</SourceSize>" + NL;
            } else {
              long est_size = file.getEstimatedTargetSize();

              if (est_size > 0) {
                reply += "            <SourceSize>" + est_size + "</SourceSize>" + NL;
              }
            }

            reply +=
                "            <Duration>"
                    + file.getDurationMillis()
                    + "</Duration>"
                    + NL
                    + "            <Description>"
                    + desc
                    + "</Description>"
                    + NL
                    + "            <SourceChannel>0</SourceChannel>"
                    + NL
                    + "            <SourceStation></SourceStation>"
                    + NL
                    + "            <SeriesId></SeriesId>"
                    + NL
                    + "            <CaptureDate>"
                    + file.getCaptureDate()
                    + "</CaptureDate>"
                    + NL
                    + "        </Details>"
                    + NL
                    + "        <Links>"
                    + NL
                    + "            <Content>"
                    + NL
                    + "                <ContentType>video/x-tivo-mpeg</ContentType>"
                    + NL
                    + "                    <AcceptsParams>No</AcceptsParams>"
                    + NL
                    + "                    <Url>"
                    + file.getLinkURL()
                    + "</Url>"
                    + NL
                    + "                </Content>"
                    + NL
                    + "                <CustomIcon>"
                    + NL
                    + "                    <ContentType>video/*</ContentType>"
                    + NL
                    + "                    <AcceptsParams>No</AcceptsParams>"
                    + NL
                    + "                    <Url>urn:tivo:image:save-until-i-delete-recording</Url>"
                    + NL
                    + "                </CustomIcon>"
                    + NL
                    + "        </Links>"
                    + NL
                    + "    </Item>"
                    + NL;

          } else {

            ContainerInfo cont = (ContainerInfo) item;

            reply +=
                "    <Item>"
                    + NL
                    + "        <Details>"
                    + NL
                    + "            <Title>"
                    + cont.getName()
                    + "</Title>"
                    + NL
                    + "            <ContentType>x-container/tivo-videos</ContentType>"
                    + NL
                    + "            <SourceFormat>x-container/folder</SourceFormat>"
                    + NL
                    + "            <TotalItems>"
                    + cont.getChildCount()
                    + "</TotalItems>"
                    + NL
                    + "        </Details>"
                    + NL
                    + "        <Links>"
                    + NL
                    + "            <Content>"
                    + NL
                    + "                <Url>"
                    + cont.getLinkURL()
                    + "</Url>"
                    + NL
                    + "                <ContentType>x-container/tivo-videos</ContentType>"
                    + NL
                    + "            </Content>"
                    + NL
                    + "        </Links>"
                    + NL
                    + "    </Item>"
                    + NL;
          }
        }

        String footer = "</TiVoContainer>";

        reply += footer;
      }

    } else if (command.equals("QueryFormats")) {

      String source_format = args.get("SourceFormat");

      if (source_format != null && source_format.startsWith("video")) {

        // /TiVoConnect?Command=QueryFormats&SourceFormat=video%2Fx-tivo-mpeg

        reply =
            "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
                + NL
                + "<TiVoFormats><Format>"
                + NL
                + "<ContentType>video/x-tivo-mpeg</ContentType><Description/>"
                + NL
                + "</Format></TiVoFormats>";
      }
    }

    if (reply == null) {

      return (false);
    }

    if (TRACE) {
      System.out.println("->" + reply);
    }

    response.setContentType("text/xml");

    response.getOutputStream().write(reply.getBytes("UTF-8"));

    return (true);
  }