Esempio n. 1
0
    protected void Page_Load(object sender, EventArgs e)
    {
        string filename = "";

        List<EncoderConfig> cfgs = Utils.LoadConfig();
        EncoderConfig cfg = cfgs[Int32.Parse(Request.QueryString["idProfile"])];

        ServiceInterface server = new ServiceInterface();
        if (Request.QueryString["idChannel"] != null)
        {
            if (server.GetChannel(Int32.Parse(Request.QueryString["idChannel"])).isRadio)
                bufferSize = 2560;
            WebTvResult res = server.StartTimeShifting(Int32.Parse(Request.QueryString["idChannel"]));
            if (res.result != 0)
            {
                Utils.Log("Streamer.aspx: ERROR: StartTimeShifting failed with error: " + res.result.ToString());
                Response.Output.WriteLine("ERROR: StartTimeShifting failed with error: " + res.result.ToString());
                Response.End();
                return;
            }
            usedCard = res.user.idCard;
            usedChannel = res.user.idChannel;
            tvServerUsername = res.user.name;
            if (cfg.inputMethod == TransportMethod.Filename)
                filename = res.rtspURL;
            else
                filename = res.timeshiftFile;
        }
        else if (Request.QueryString["idRecording"] != null)
        {
            WebRecording rec = server.GetRecording(Int32.Parse(Request.QueryString["idRecording"]));
            filename = rec.fileName;
        }
        else if (Request.QueryString["idMovie"] != null)
        {
            WebMovie movie = server.GetMovie(Int32.Parse(Request.QueryString["idMovie"]));
            filename = movie.file;
        }
        else if (Request.QueryString["idMusicTrack"] != null)
        {
            WebMusicTrack track = server.GetMusicTrack(Int32.Parse(Request.QueryString["idMusicTrack"]));
            filename = track.file;
        }
        else if (Request.QueryString["idTvSeries"] != null)
        {
            WebSeries series = server.GetTvSeries(Request.QueryString["idTvSeries"]);
            filename = series.filename;
        }
        else if (Request.QueryString["idMovingPicture"] != null)
        {
            WebMovingPicture pic = server.GetMovingPicture(Int32.Parse(Request.QueryString["idMovingPicture"]));
            filename = pic.filename;
        }
        if (!File.Exists(filename) && !filename.StartsWith("rtsp://"))
        {
            Utils.Log("Streamer.aspx: Requested file " + filename + " does not exist.");
            return;
        }
        Response.Clear();
        Response.Buffer = false;
        Response.BufferOutput = false;
        Response.AppendHeader("Connection", "Keep-Alive");
        Response.ContentType = "video/x-msvideo";
        Response.StatusCode = 200;

        Stream mediaStream = null;
        EncoderWrapper encoder = null;
        Stream outStream = null;

        if (cfg.inputMethod != TransportMethod.Filename)
        {
            if (Request.QueryString["idChannel"] != null)
            {
                mediaStream = new TsBuffer(filename);
            }
            else
                mediaStream = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
            encoder = new EncoderWrapper(mediaStream, cfg);
        }
        else
            encoder = new EncoderWrapper(filename, cfg);

        if (cfg.useTranscoding)
            outStream = encoder;
        else
            outStream = mediaStream;

        byte[] buffer = new byte[bufferSize];
        int read;
        try
        {
            while ((read = outStream.Read(buffer, 0, buffer.Length)) > 0)
            {
                try
                {
                    Response.OutputStream.Write(buffer, 0, read);
                }
                catch (Exception)
                {
                    break; // stream is closed
                }
                if (Request.QueryString["idChannel"] != null)
                    server.SendHeartBeat(usedChannel, usedCard, tvServerUsername);
            }
        }
        catch (Exception ex)
        {
            Utils.Log("Streamer.aspx: Exception raised=" + ex.Message + Environment.NewLine + ex.StackTrace);
        }
        if (mediaStream != null)
            mediaStream.Close();
        if (Request.QueryString["idChannel"] != null)
            server.StopTimeShifting(usedChannel, usedCard, tvServerUsername);
        encoder.StopProcess();
        Response.End();
    }
Esempio n. 2
0
        public static void Stream(MediaType mediatype, string id)
        {
            StopStreaming();

            string fileName = string.Empty;
            string userName = string.Empty;
            int card = 0;
            EncoderConfig cfg = null;

            switch (mediatype)
            {
                case Streamer.MediaType.Tv:
                case Streamer.MediaType.Radio:

                    if (mediatype == Streamer.MediaType.Tv)
                    {
                        Log.Info("iPiMPWeb - TV stream requested");
                        cfg = Utils.LoadConfig()[0];
                    }
                    else
                    {
                        Log.Info("iPiMPWeb - Radio stream requested");
                        cfg = Utils.LoadConfig()[3];
                    }

                    WebTvResult res = uWiMP.TVServer.Cards.StartTimeshifting(Convert.ToInt32(id));
                    Log.Info("iPiMPWeb - StartTimeshifting result is {0}", res.result.ToString());
                    if (res.result != 0)
                        return;

                    card = res.user.idCard;
                    userName = res.user.name;
                    fileName = res.timeshiftFile;

                    UpdateStreamTracker(mediatype.ToString(), id, card.ToString(), userName);
                    break;

                case Streamer.MediaType.Recording:
                    cfg = Utils.LoadConfig()[1];
                    Recording recording = uWiMP.TVServer.Recordings.GetRecordingById(Convert.ToInt32(id));
                    fileName = recording.FileName;
                    UpdateStreamTracker(mediatype.ToString(), id.ToString(), "", "");
                    break;

                case Streamer.MediaType.TvSeries:
                    cfg = Utils.LoadConfig()[2];
                    fileName = id;
                    UpdateStreamTracker(mediatype.ToString(), id.ToString(), "", "");
                    break;

                default:
                    return;
            }

            if (!(File.Exists(fileName) || fileName.StartsWith("rtsp://")))
            {
                Log.Info("iPiMPWeb - StartTimeshifting StopStreaming file does not exist or starts with rtsp {0}", fileName);
                StopStreaming();
                return;
            }

            try
            {
                if ((cfg.inputMethod != TransportMethod.Filename))
                {
                    if ((mediatype == Streamer.MediaType.Tv) | (mediatype == Streamer.MediaType.Radio))
                    {
                        _mediaStream = new TsBuffer(fileName);
                    }
                    else
                    {
                        _mediaStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                    }
                    _encoder = new EncoderWrapper(_mediaStream, cfg);
                }
                else
                {
                    _encoder = new EncoderWrapper(fileName, cfg);
                }
            }
            catch (Exception ex)
            {
                Log.Info("iPiMPWeb - StartTimeshifting exception {0}", ex.Message);
            }
        }