private void PlayTranscodedWithSegments(string part, int bufferBlocks = 20, int quality = 10)
        {
            if (part.IndexOf("http://") != -1 && _isWebkit == false)
            {
                if (part.IndexOf(":32400") != -1)
                {
                    part = part.Substring(part.IndexOf(":32400/") + 6);
                }
                else
                {
            //                    PlayHttpWithDirectShow(part);
                    quality = 10;
                    bufferBlocks = 20;
                    return;
                }
            }

            string[] segments = TryTranscodeBySegments(part, quality);

            if (segments.Length == 0)
            {
                MessageBox.Show("transcoder failure");
                return;

            }

            string FILENAME = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "\\media.ts";

            AutoResetEvent ar = new AutoResetEvent(false);
            FileStream media = new FileStream(FILENAME, FileMode.Create, FileAccess.ReadWrite, FileShare.Read);
            int ctr = 0;

            Thread t = new Thread((arg) =>
            {

                Buffering bufferBox = new Buffering(bufferBlocks);
                bufferBox.Show();
                Application.DoEvents();

                foreach (string segment in segments)
                {

                    WebClient fetch = new WebClient();

                    if (_useAuth == true)
                    {
                        fetch.Headers["X-Plex-User"] = username;
                        fetch.Headers["X-Plex-Pass"] = passwordHash;
                    }

                    byte[] data = fetch.DownloadData(segment);
                    fetch.Dispose();

                    media.Write(data, 0, data.Length);
                    media.Flush();

                    if (ctr == bufferBlocks || ctr == segments.Length - 1)
                    {
                        try
                        {
                            bufferBox.Close();
                        }
                        catch
                        {
                        }

                        ar.Set();
                    }

                    if (bufferBox.Visible == true)
                    {
                        try
                        {
                            bufferBox.Increment();
                        }
                        catch
                        {
                        }
                    }

                    ctr++;

                }

            });

            t.Start();

            ar.WaitOne();

            System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo();
            psi.FileName = "ffplay.exe";
            psi.Arguments = "-sync audio -fs \"" + FILENAME + "\"";
            psi.UseShellExecute = false;

            Process proc = Process.Start(psi);

            proc.WaitForExit();

            try
            {
                t.Abort();
            }
            catch
            {
            }

            try
            {
                media.Close();
            }
            catch
            {
            }

            File.Delete(FILENAME);

            WebClient wc = new WebClient();
            wc.Headers[HttpRequestHeader.Cookie] = _sessionCookie;

            if (_useAuth == true)
            {
                wc.Headers["X-Plex-User"] = username;
                wc.Headers["X-Plex-Pass"] = passwordHash;
            }

            string url = FQDN() + "/video/:/transcode/segmented/stop";

            wc.DownloadStringAsync(new Uri(url));
        }
Beispiel #2
0
        private void PlayTranscodedWithSegments(string part, int bufferBlocks = 20, int quality = 10)
        {
            if (part.IndexOf("http://") != -1 && _isWebkit == false)
            {
                if (part.IndexOf(":32400") != -1)
                {
                    part = part.Substring(part.IndexOf(":32400/") + 6);
                }
                else
                {
//                    PlayHttpWithDirectShow(part);
                    quality      = 10;
                    bufferBlocks = 20;
                    return;
                }
            }


            string[] segments = TryTranscodeBySegments(part, quality);

            if (segments.Length == 0)
            {
                MessageBox.Show("transcoder failure");
                return;
            }

            string FILENAME = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "\\media.ts";

            AutoResetEvent ar    = new AutoResetEvent(false);
            FileStream     media = new FileStream(FILENAME, FileMode.Create, FileAccess.ReadWrite, FileShare.Read);
            int            ctr   = 0;

            Thread t = new Thread((arg) =>
            {
                Buffering bufferBox = new Buffering(bufferBlocks);
                bufferBox.Show();
                Application.DoEvents();

                foreach (string segment in segments)
                {
                    WebClient fetch = new WebClient();

                    if (_useAuth == true)
                    {
                        fetch.Headers["X-Plex-User"] = username;
                        fetch.Headers["X-Plex-Pass"] = passwordHash;
                    }

                    byte[] data = fetch.DownloadData(segment);
                    fetch.Dispose();

                    media.Write(data, 0, data.Length);
                    media.Flush();

                    if (ctr == bufferBlocks || ctr == segments.Length - 1)
                    {
                        try
                        {
                            bufferBox.Close();
                        }
                        catch
                        {
                        }

                        ar.Set();
                    }


                    if (bufferBox.Visible == true)
                    {
                        try
                        {
                            bufferBox.Increment();
                        }
                        catch
                        {
                        }
                    }

                    ctr++;
                }
            });

            t.Start();

            ar.WaitOne();


            System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo();
            psi.FileName        = "ffplay.exe";
            psi.Arguments       = "-sync audio -fs \"" + FILENAME + "\"";
            psi.UseShellExecute = false;

            Process proc = Process.Start(psi);

            proc.WaitForExit();

            try
            {
                t.Abort();
            }
            catch
            {
            }

            try
            {
                media.Close();
            }
            catch
            {
            }

            File.Delete(FILENAME);

            WebClient wc = new WebClient();

            wc.Headers[HttpRequestHeader.Cookie] = _sessionCookie;

            if (_useAuth == true)
            {
                wc.Headers["X-Plex-User"] = username;
                wc.Headers["X-Plex-Pass"] = passwordHash;
            }

            string url = FQDN() + "/video/:/transcode/segmented/stop";

            wc.DownloadStringAsync(new Uri(url));
        }