Exemplo n.º 1
0
 private void ButtonBrowseManifest_Click(object sender, RoutedEventArgs e)
 {
     try
     {
         if (_BrowseManifestOpenFileDialog.ShowDialog() == true)
         {
             _DoWorkException = null;
             var backgroundWorker = new BackgroundWorker();
             backgroundWorker.DoWork += BrowseManifest_DoWork;
             var progressWindow = new ProgressWindow(backgroundWorker)
             {
                 Owner = this,
             };
             progressWindow.ShowDialog();
             if (_DoWorkException == null)
             {
                 labelStatus.Content       = "OK";
                 labelStatus.Background    = Brushes.LightGreen;
                 buttonCalculate.IsEnabled = true;
             }
             else
             {
                 labelStatus.Content       = "Error";
                 labelStatus.Background    = Brushes.Red;
                 buttonCalculate.IsEnabled = false;
                 throw _DoWorkException;
             }
         }
     }
     catch (Exception ex)
     {
         var errorWin = new ErrorWindow("Error reading the video set of files.", ex)
         {
             Owner = this
         };
         errorWin.ShowDialog();
     }
 }
        /// <summary>
        ///
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonBrowseFov_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                OpenFileDialog openFileDialog = new OpenFileDialog();

                openFileDialog.Filter      = "Fixed Offset Files (*.fov)|*.fov|All Files (*.*)|*.*";
                openFileDialog.FilterIndex = 1;

                if (openFileDialog.ShowDialog() == true)
                {
                    fovFilename = openFileDialog.FileName;

                    string directoryName = new FileInfo(fovFilename).Directory.FullName;

                    string manifestFilename = Path.Combine(directoryName, Path.GetFileNameWithoutExtension(fovFilename) + ".ismc");

                    manifestInfo = new ManifestParser().Parse(manifestFilename);

                    textBoxFovPath.Text    = fovFilename;
                    textBoxFovPath.ToolTip = fovFilename;

                    buttonUpload.IsEnabled = true;
                }
                else
                {
                    buttonUpload.IsEnabled = false;
                }
            }
            catch (Exception ex)
            {
                buttonUpload.IsEnabled = false;

                ErrorWindow errorWin = new ErrorWindow("Error loading video data.", ex);
                errorWin.Owner = this;
                errorWin.ShowDialog();
            }
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonBrowseManifest_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                OpenFileDialog openFileDialog = new OpenFileDialog();

                openFileDialog.Filter      = "Manifest Files (*.ismc)|*.ismc|All Files (*.*)|*.*";
                openFileDialog.FilterIndex = 1;

                if (openFileDialog.ShowDialog() == true)
                {
                    BackgroundWorker backgroundWorker = new BackgroundWorker();

                    Exception exceptionError = null;

                    backgroundWorker.DoWork += delegate(object s, DoWorkEventArgs args)
                    {
                        try
                        {
                            string fileName = openFileDialog.FileName;

                            string directoryName = new FileInfo(fileName).Directory.FullName;

                            string smilFilename = Path.Combine(directoryName, Path.GetFileNameWithoutExtension(fileName) + ".ism");

                            manifestInfo = new ManifestParser().Parse(fileName);

                            smilDocument = SmilDocument.Load(smilFilename);

                            foreach (QualityLevelInfo qualityLevel in manifestInfo.Streams.Find(stream => stream.MediaType == MediaType.Video).QualityLevels)
                            {
                                SmilVideo smilVideo = smilDocument.Body.Switch.Video.First(stream => stream.SystemBitrate == qualityLevel.Bitrate.ToString());

                                qualityLevel.TrackId  = Convert.ToInt32(smilVideo.Param.First(p => p.Name == "trackID").Value, CultureInfo.InvariantCulture);
                                qualityLevel.Filename = Path.Combine(directoryName, smilVideo.Src);

                                if (!File.Exists(qualityLevel.Filename))
                                {
                                    throw new FileNotFoundException("Video file not found", qualityLevel.Filename);
                                }
                            }

                            foreach (QualityLevelInfo qualityLevel in manifestInfo.Streams.Find(stream => stream.MediaType == MediaType.Audio).QualityLevels)
                            {
                                SmilAudio smilAudio = smilDocument.Body.Switch.Audio.First(stream => stream.SystemBitrate == qualityLevel.Bitrate.ToString());

                                qualityLevel.TrackId  = Convert.ToInt32(smilAudio.Param.First(p => p.Name == "trackID").Value, CultureInfo.InvariantCulture);
                                qualityLevel.Filename = Path.Combine(directoryName, smilAudio.Src);

                                if (!File.Exists(qualityLevel.Filename))
                                {
                                    throw new FileNotFoundException("Video file not found", qualityLevel.Filename);
                                }
                            }

                            Dispatcher.Invoke(new Action(delegate
                            {
                                textBoxManifestPath.Text    = fileName;
                                textBoxManifestPath.ToolTip = fileName;

                                GridView grid = new GridView();

                                GridViewColumn c1       = new GridViewColumn();
                                c1.DisplayMemberBinding = new Binding("Type");
                                c1.Header = "Type";
                                grid.Columns.Add(c1);

                                GridViewColumn c2       = new GridViewColumn();
                                c2.DisplayMemberBinding = new Binding("File");
                                c2.Header = "File";
                                grid.Columns.Add(c2);

                                GridViewColumn c3       = new GridViewColumn();
                                c3.DisplayMemberBinding = new Binding("Path");
                                c3.Header = "Path";
                                grid.Columns.Add(c3);

                                ObservableCollection <object> coll = new ObservableCollection <object>();

                                coll.Add(new { Type = "SMIL", File = Path.GetFileName(smilFilename), Path = smilFilename });
                                coll.Add(new { Type = "Manifest", File = Path.GetFileName(fileName), Path = fileName });

                                foreach (QualityLevelInfo qualityLevel in manifestInfo.Streams.Find(stream => stream.MediaType == MediaType.Video).QualityLevels)
                                {
                                    coll.Add(new { Type = "Quality level", File = Path.GetFileNameWithoutExtension(qualityLevel.Filename), Path = qualityLevel.Filename });
                                }

                                listViewFiles.ItemsSource = coll;
                                listViewFiles.View        = grid;
                            }));
                        }
                        catch (Exception ex)
                        {
                            exceptionError = ex;
                        }
                    };

                    ProgressWindow progressWindow = new ProgressWindow();

                    progressWindow.Owner  = this;
                    progressWindow.Worker = backgroundWorker;

                    progressWindow.ShowDialog();

                    if (exceptionError == null)
                    {
                        labelStatus.Content       = "OK";
                        labelStatus.Background    = Brushes.LightGreen;
                        buttonCalculate.IsEnabled = true;
                    }
                    else
                    {
                        labelStatus.Content       = "Error";
                        labelStatus.Background    = Brushes.Red;
                        buttonCalculate.IsEnabled = false;

                        throw exceptionError;
                    }
                }
            }
            catch (Exception ex)
            {
                ErrorWindow errorWin = new ErrorWindow("Error reading the video set of files.", ex);
                errorWin.Owner = this;
                errorWin.ShowDialog();
            }
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonbuttonCalculate_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                SaveFileDialog safeFileDialog = new SaveFileDialog();

                safeFileDialog.Filter      = "Fixed offset video file (*.fov)|*.fov";
                safeFileDialog.FilterIndex = 1;
                safeFileDialog.FileName    = Path.Combine(Path.GetDirectoryName(manifestInfo.Filename), manifestInfo.Name + ".fov");

                if (safeFileDialog.ShowDialog() == true)
                {
                    BackgroundWorker backgroundWorker = new BackgroundWorker();

                    Exception exceptionError = null;

                    backgroundWorker.DoWork += delegate(object s, DoWorkEventArgs args)
                    {
                        try
                        {
                            string fileName = safeFileDialog.FileName;

                            TextWriter output = new StreamWriter(fileName, false, Encoding.UTF8);

                            try
                            {
                                foreach (StreamInfo streamInfo in manifestInfo.Streams)
                                {
                                    foreach (QualityLevelInfo qualityLevel in streamInfo.QualityLevels)
                                    {
                                        Mp4Stream stream = new Mp4FileStream(qualityLevel.Filename, FileAccess.Read);

                                        try
                                        {
                                            Mp4MfraBox mfra = GetMfra(stream);

                                            Mp4TfraBox tfra = (Mp4TfraBox)mfra.Children.First(b => b.Type == Mp4BoxType.TFRA && ((Mp4TfraBox)b).TrackId == qualityLevel.TrackId);

                                            foreach (MediaChunk chunk in streamInfo.Chunks)
                                            {
                                                Mp4TfraEntry entry = tfra.Entries[chunk.ChunkId];

                                                stream.Position = (long)entry.MoofOffset;

                                                uint size = 0;

                                                // moof
                                                size             = stream.ReadUInt32();
                                                stream.Position += (size - 4);

                                                // mdat
                                                size += stream.ReadUInt32();

                                                string line = string.Format("mediatype={0},bitrate={1},starttime={2},file={3},offset={4},size={5}", streamInfo.MediaType.ToString().ToLower(), qualityLevel.Bitrate, entry.Time, Path.GetFileName(qualityLevel.Filename), entry.MoofOffset, size);

                                                output.WriteLine(line);
                                            }
                                        }
                                        finally
                                        {
                                            stream.Close();
                                        }
                                    }
                                }
                            }
                            finally
                            {
                                output.Close();
                            }
                        }
                        catch (Exception ex)
                        {
                            exceptionError = ex;
                        }
                    };

                    ProgressWindow progressWindow = new ProgressWindow();

                    progressWindow.Owner  = this;
                    progressWindow.Worker = backgroundWorker;

                    progressWindow.ShowDialog();

                    if (exceptionError == null)
                    {
                        MessageBox.Show("Fixed offset file created.", "Success", MessageBoxButton.OK, MessageBoxImage.Information);
                    }
                    else
                    {
                        throw exceptionError;
                    }
                }
            }
            catch (Exception ex)
            {
                ErrorWindow errorWindow = new ErrorWindow("Error reading the video set of files.", ex);
                errorWindow.Owner = this;
                errorWindow.ShowDialog();
            }
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonbuttonUpload_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                BackgroundWorker backgroundWorker = new BackgroundWorker();

                Exception exceptionError = null;

                string account       = textBoxStorateAccount.Text.Trim();
                string key           = passwordBoxSharedKey.Password.Trim();
                string containerName = textBoxContainerName.Text.Trim();

                backgroundWorker.DoWork += delegate(object s, DoWorkEventArgs args)
                {
                    try
                    {
                        StorageCredentialsAccountAndKey credentials = new StorageCredentialsAccountAndKey(account, key);

                        CloudBlobClient client = new CloudBlobClient("http://" + account + ".blob.core.windows.net", credentials);

                        CloudBlobContainer container;
                        CloudBlob          blob;

                        container = new CloudBlobContainer(containerName, client);
                        BlobContainerPermissions containerPermissions = new BlobContainerPermissions();
                        containerPermissions.PublicAccess = BlobContainerPublicAccessType.Container;
                        container.CreateIfNotExist();
                        container.SetPermissions(containerPermissions);

                        string directoryName = new FileInfo(fovFilename).Directory.FullName;

                        blob = new CloudBlob(containerName + "/" + manifestInfo.Name + ".ism/Manifest", client);
                        blob.DeleteIfExists();
                        blob.Properties.ContentType = "text/xml";
                        blob.UploadFile(manifestInfo.Filename);

                        foreach (string line in File.ReadAllLines(fovFilename))
                        {
                            string[] arr = line.Split(',');

                            string mediatype = arr[0].Split('=')[1];
                            string bitrate   = arr[1].Split('=')[1];
                            string starttime = arr[2].Split('=')[1];
                            string file      = arr[3].Split('=')[1];
                            string offset    = arr[4].Split('=')[1];
                            string size      = arr[5].Split('=')[1];

                            string path = string.Format("{0}/{1}.ism/QualityLevels({2})/Fragments({3}={4})", containerName, manifestInfo.Name, bitrate, mediatype, starttime);

                            FileStream stream = File.OpenRead(Path.Combine(directoryName, file));

                            byte[] buffer = new byte[int.Parse(size)];
                            stream.Position = int.Parse(offset);
                            stream.Read(buffer, 0, buffer.Length);

                            stream.Close();

                            blob = new CloudBlob(path, client);
                            blob.DeleteIfExists();
                            blob.Properties.ContentType = "video/mp4";
                            blob.UploadByteArray(buffer);
                        }
                    }
                    catch (Exception ex)
                    {
                        exceptionError = ex;
                    }
                };

                ProgressWindow progressWindow = new ProgressWindow();

                progressWindow.Owner  = this;
                progressWindow.Worker = backgroundWorker;

                progressWindow.ShowDialog();

                if (exceptionError == null)
                {
                    MessageBox.Show("Video uploaded to Azure Storage account.", "Success", MessageBoxButton.OK, MessageBoxImage.Information);
                }
                else
                {
                    throw exceptionError;
                }
            }
            catch (Exception ex)
            {
                ErrorWindow errorWin = new ErrorWindow("Error uploading to azure.", ex);
                errorWin.Owner = this;
                errorWin.ShowDialog();
            }
        }