Android Open Source - Amphitheatre Streamer






From Project

Back to project page Amphitheatre.

License

The source code is released under:

Apache License

If you think the Android project Amphitheatre listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright (C) 2014 Michell Bak//ww w . jav  a  2 s  .  co  m
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.jerrellmardis.amphitheatre.server;

import android.util.Log;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Properties;

import jcifs.smb.SmbFile;

public class Streamer extends StreamServer {

  public static final int PORT = 7871;
  public static final String URL = "http://127.0.0.1:" + PORT;
  private SmbFile file;
  protected List<SmbFile> extras; // those can be subtitles
  private static Streamer instance;
    private OnStreamListener onStreamListener;

    public interface OnStreamListener {
        void onStream(int percentStreamed);
        void onPlay();
    }

    public void setOnStreamListener(OnStreamListener onStreamListener) {
        this.onStreamListener = onStreamListener;
    }

    protected Streamer(int port) throws IOException {
    super(port, new File("."));
  }

  public static Streamer getInstance() {
    if (instance == null)
      try {
        instance = new Streamer(PORT);
      } catch (IOException e) {
        e.printStackTrace();
      }
    return instance;
  }

  public void setStreamSrc(SmbFile file,List<SmbFile> extraFiles) {
    this.file = file;
    this.extras = extraFiles;
  }

  @Override
  public Response serve(String uri, String method, Properties header, Properties parms, Properties files) {
    Response res = null;
    try {
      SmbFile sourceFile = null;
      String name = getNameFromPath(uri);
      if(file!=null && file.getName().equals(name))
        sourceFile = file;
      else if(extras!=null){
        for(SmbFile i : extras){
          if(i!=null && i.getName().equals(name)){
            sourceFile = i;
            break;
          }
        }
      }
      if (sourceFile==null)
        res= new Response(HTTP_NOTFOUND, MIME_PLAINTEXT, null);
      else {
        long startFrom = 0;
        long endAt = -1;
        String range = header.getProperty("range");
        if (range != null) {
          if (range.startsWith("bytes=")) {
            range = range.substring("bytes=".length());
            int minus = range.indexOf('-');
            try {
              if (minus > 0) {
                startFrom = Long.parseLong(range.substring(0, minus));
                endAt = Long.parseLong(range.substring(minus + 1));
              }
            } catch (NumberFormatException nfe) {}
          }
        }
        Log.d("Streamer", "Request: " + range + " from: " + startFrom + ", to: " + endAt);

        // Change return code and add Content-Range header when skipping is requested
        final StreamSource source = new StreamSource(sourceFile);
        long fileLen = source.length();
        if (range != null && startFrom > 0) {
          if (startFrom >= fileLen) {
            res = new Response(HTTP_RANGE_NOT_SATISFIABLE, MIME_PLAINTEXT, null);
            res.addHeader("Content-Range", "bytes 0-0/" + fileLen);
          } else {
            if (endAt < 0)
              endAt = fileLen - 1;
            long newLen = fileLen - startFrom;
            if (newLen < 0)
              newLen = 0;
            Log.d("Streamer", "start=" + startFrom + ", endAt=" + endAt + ", newLen=" + newLen);
            final long dataLen = newLen;
            source.moveTo(startFrom);
            Log.d("Streamer", "Skipped " + startFrom + " bytes");

                        if (onStreamListener != null) {
                            int percentStreamed = Math.round((startFrom / (float) endAt) * 100);
                            onStreamListener.onStream(percentStreamed);
                        }

            res = new Response(HTTP_PARTIALCONTENT, source.getMimeType(), source);
            res.addHeader("Content-length", "" + dataLen);
            res.addHeader("Content-Range", "bytes " + startFrom + "-" + endAt + "/" + fileLen);
          }
        } else {
          source.reset();
          res = new Response(HTTP_OK, source.getMimeType(), source);
          res.addHeader("Content-Length", "" + fileLen);
                    if (onStreamListener != null) {
                        onStreamListener.onPlay();
                    }
        }
      }
    } catch (IOException ioe) {
      ioe.printStackTrace();
      res = new Response(HTTP_FORBIDDEN, MIME_PLAINTEXT, null);
    }

    // Announce that the file server accepts partial content requestes
    res.addHeader("Accept-Ranges", "bytes");
    return res;
  }

  public static String getNameFromPath(String path){
    if(path == null || path.length() < 2)
      return null;
    int slash = path.lastIndexOf('/');
    if(slash == -1)
      return path;
    return path.substring(slash+1);
  }

}




Java Source Code List

com.jerrellmardis.amphitheatre.activity.BootupActivity.java
com.jerrellmardis.amphitheatre.activity.BrowseActivity.java
com.jerrellmardis.amphitheatre.activity.DetailsActivity.java
com.jerrellmardis.amphitheatre.activity.GridViewActivity.java
com.jerrellmardis.amphitheatre.activity.SearchActivity.java
com.jerrellmardis.amphitheatre.api.ApiClient.java
com.jerrellmardis.amphitheatre.api.GuessItClient.java
com.jerrellmardis.amphitheatre.api.MediaClientFactory.java
com.jerrellmardis.amphitheatre.api.MediaClient.java
com.jerrellmardis.amphitheatre.api.TMDbClient.java
com.jerrellmardis.amphitheatre.api.TVDBClientTest.java
com.jerrellmardis.amphitheatre.api.TVDBClient.java
com.jerrellmardis.amphitheatre.api.TVDBService.java
com.jerrellmardis.amphitheatre.fragment.AddSourceDialogFragment.java
com.jerrellmardis.amphitheatre.fragment.BrowseFragment.java
com.jerrellmardis.amphitheatre.fragment.CustomizeDialogFragment.java
com.jerrellmardis.amphitheatre.fragment.SearchFragment.java
com.jerrellmardis.amphitheatre.fragment.VerticalGridFragment.java
com.jerrellmardis.amphitheatre.fragment.VideoDetailsFragment.java
com.jerrellmardis.amphitheatre.listeners.RowBuilderTaskListener.java
com.jerrellmardis.amphitheatre.listeners.TaskListener.java
com.jerrellmardis.amphitheatre.model.GridGenre.java
com.jerrellmardis.amphitheatre.model.Source.java
com.jerrellmardis.amphitheatre.model.VideoGroup.java
com.jerrellmardis.amphitheatre.model.Video.java
com.jerrellmardis.amphitheatre.model.guessit.Guess.java
com.jerrellmardis.amphitheatre.model.tmdb.Config.java
com.jerrellmardis.amphitheatre.model.tmdb.Episode.java
com.jerrellmardis.amphitheatre.model.tmdb.Genre.java
com.jerrellmardis.amphitheatre.model.tmdb.Movie.java
com.jerrellmardis.amphitheatre.model.tmdb.ProductionCompany.java
com.jerrellmardis.amphitheatre.model.tmdb.SearchResult.java
com.jerrellmardis.amphitheatre.model.tmdb.TvShow.java
com.jerrellmardis.amphitheatre.model.tmdb.Videos.java
com.jerrellmardis.amphitheatre.model.tvdb.BaseResponse.java
com.jerrellmardis.amphitheatre.model.tvdb.EpisodeResponse.java
com.jerrellmardis.amphitheatre.model.tvdb.Episode.java
com.jerrellmardis.amphitheatre.model.tvdb.Language.java
com.jerrellmardis.amphitheatre.model.tvdb.SeriesResult.java
com.jerrellmardis.amphitheatre.model.tvdb.Series.java
com.jerrellmardis.amphitheatre.server.StreamServer.java
com.jerrellmardis.amphitheatre.server.StreamSource.java
com.jerrellmardis.amphitheatre.server.Streamer.java
com.jerrellmardis.amphitheatre.service.LibraryUpdateService.java
com.jerrellmardis.amphitheatre.service.RecommendationsService.java
com.jerrellmardis.amphitheatre.task.DetailRowBuilderTask.java
com.jerrellmardis.amphitheatre.task.DownloadMovieTask.java
com.jerrellmardis.amphitheatre.task.DownloadTaskHelper.java
com.jerrellmardis.amphitheatre.task.DownloadTvShowTask.java
com.jerrellmardis.amphitheatre.task.DownloadVideoTask.java
com.jerrellmardis.amphitheatre.task.GetFilesTask.java
com.jerrellmardis.amphitheatre.task.NetworkSearchTask.java
com.jerrellmardis.amphitheatre.util.ApiConstants.java
com.jerrellmardis.amphitheatre.util.Base64.java
com.jerrellmardis.amphitheatre.util.BlurTransform.java
com.jerrellmardis.amphitheatre.util.Constants.java
com.jerrellmardis.amphitheatre.util.Enums.java
com.jerrellmardis.amphitheatre.util.PicassoBackgroundManagerTarget.java
com.jerrellmardis.amphitheatre.util.RecommendationBuilder.java
com.jerrellmardis.amphitheatre.util.SecurePreferences.java
com.jerrellmardis.amphitheatre.util.Utils.java
com.jerrellmardis.amphitheatre.util.VideoUtils.java
com.jerrellmardis.amphitheatre.widget.CardPresenter.java
com.jerrellmardis.amphitheatre.widget.DetailsDescriptionPresenter.java
com.jerrellmardis.amphitheatre.widget.GridItemPresenter.java
com.jerrellmardis.amphitheatre.widget.SeasonCardPresenter.java
com.jerrellmardis.amphitheatre.widget.SortedObjectAdapter.java
com.jerrellmardis.amphitheatre.widget.TvShowsCardPresenter.java