private void CreatePlaylist()
        {
            var playlistItem = new Core.Media.PlaylistItem();
            playlistItem.MediaSource = new Uri("http://ecn.channel9.msdn.com/o9/content/smf/smoothcontent/elephantsdream/Elephants_Dream_1024-h264-st-aac.ism/manifest");
            playlistItem.DeliveryMethod = DeliveryMethods.AdaptiveStreaming;

            var region = new CaptionRegion();
            playlistItem.Captions.Add(region);

            var caption1 = new CaptionElement
            {
                Begin = TimeSpan.Zero,
                End = TimeSpan.FromSeconds(10),
                Content = "This marker should display from 00:00:00 to 00:00:10 and the text should be red."
            };
            caption1.Style.Color = Colors.Red;
            caption1.Style.FontSize.Unit = LengthUnit.Pixel;
            caption1.Style.FontSize.Value = 15;
            region.Children.Add(caption1);

            var caption2 = new CaptionElement
            {
                Begin = TimeSpan.FromSeconds(20),
                End = TimeSpan.FromSeconds(30),
                Content = "This marker should display from 00:00:20 to 00:00:30 and the text should be yellow."
            };
            caption2.Style.Color = Colors.Yellow;
            caption2.Style.FontSize.Unit = LengthUnit.Pixel;
            caption2.Style.FontSize.Value = 15;
            region.Children.Add(caption2);

            player.Playlist.Add(playlistItem);
        }
 private void ApplyCaptionOverrideSettings(CaptionElement caption)
 {
     if (this.CaptionsOverrideSettings != null)
     {
         this.ApplyTextOverrides(caption);
         this.ApplyWindowOverrides(caption);
         foreach (TimedTextElement element in caption.Children)
         {
             CaptionElement element2 = element as CaptionElement;
             if (element2 != null)
             {
                 this.ApplyCaptionOverrideSettings(element2);
             }
         }
     }
 }
        private void Player_DataReceived(object sender, DataReceivedInfo e)
        {
            if (IsCaptionStream(e.StreamAttributes))
            {
                try
                {
                    var stream = new MemoryStream(e.Data);
                    var text = new StreamReader(stream).ReadToEnd();
                    var duration = e.DataChunk.Duration != TimeSpan.Zero
                                        ? e.DataChunk.Duration
                                        : TimeSpan.FromMilliseconds(2002);

                    if (_captionRegion.Begin == TimeSpan.MinValue || _captionRegion.Begin > e.DataChunk.Timestamp)
                    {
                        _captionRegion.Begin = e.DataChunk.Timestamp;
                    }

#if SILVERLIGHT3
                    if (!SystemExtensions.IsNullOrWhiteSpace(text))
#else
                    if (!string.IsNullOrWhiteSpace(text))
#endif
                    {
                        var caption = new CaptionElement
                        {
                            Content = text,
                            Begin = e.DataChunk.Timestamp,
                            End = e.DataChunk.Timestamp.Add(duration)
                        };

                        _captionRegion.Children.Add(caption);
                    }
                }
                catch (Exception err)
                {
                    Debug.WriteLine(err.Message);
                }
            }
        }
 private void RenderElementRecurse(StackPanel parent, ref StackPanel p, CaptionElement element, ref double offset, TextAlignment align)
 {
     if (element.IsActiveAtPosition(this._mediaPosition) && (element.CurrentStyle.Display == Visibility.Visible))
     {
         if (element.CaptionElementType == TimedTextElementType.Text)
         {
             string text = (element.Content != null) ? element.Content.ToString() : string.Empty;
             offset = this.WrapElement(parent, ref p, offset, text, element, align, false);
         }
         else if (element.CaptionElementType == TimedTextElementType.Container)
         {
             foreach (CaptionElement element2 in element.Children)
             {
                 this.RenderElementRecurse(parent, ref p, element2, ref offset, align);
             }
         }
         else if (element.CaptionElementType == TimedTextElementType.LineBreak)
         {
             p = this.NewPanel(parent, ref offset, element, align);
         }
     }
 }
        private double WrapElement(StackPanel parent, ref StackPanel p, double offset, string text, CaptionElement element, TextAlignment align, bool directionApplied = false)
        {
            if ((text == null) || (text == ""))
            {
                return offset;
            }
            Size effectiveSize = this.GetEffectiveSize();
            TimedTextStyle currentStyle = element.CurrentStyle;
            Size size2 = currentStyle.Extent.ToPixelSize(new Size?(effectiveSize));
            double width = size2.Width;
            double height = size2.Height;
            if ((currentStyle.Direction == Direction.RightToLeft) && !directionApplied)
            {
                text = new string(text.ToCharArray().Reverse<char>().ToArray<char>());
            }
            double num3 = ((currentStyle.FontSize.Unit == LengthUnit.PixelProportional) || (currentStyle.FontSize.Unit == LengthUnit.Cell)) ? effectiveSize.Height : height;
            TextBlock textblock = this.GetStyledTextblock(currentStyle, width, num3, false);
            this.SetContent(textblock, text);
            Border border = new Border {
                Background = GetCachedBrush(currentStyle.BackgroundColor)
            };
            FrameworkElement contentElement = null;
            double num4 = currentStyle.OutlineWidth.ToPixelLength(effectiveSize.Height);
            if (this.CaptionsOverrideSettings == null)
            {
                return offset;
            }
            switch (this.CaptionsOverrideSettings.CharacterEdgeAttribute)
            {
                case CaptionsOverrideCharacterEdges.Default:
                    if (num4 <= 0.0)
                    {
                        contentElement = textblock;
                        break;
                    }
                    contentElement = this.ApplyUniformEdge(currentStyle, width, num3, text, textblock);
                    break;

                case CaptionsOverrideCharacterEdges.NoAttribute:
                    contentElement = textblock;
                    break;

                case CaptionsOverrideCharacterEdges.Raised:
                    contentElement = this.ApplyRaisedEdge(currentStyle, width, num3, text, textblock);
                    break;

                case CaptionsOverrideCharacterEdges.Depressed:
                    contentElement = this.ApplyDepressedEdge(currentStyle, width, num3, text, textblock);
                    break;

                case CaptionsOverrideCharacterEdges.Uniform:
                    contentElement = this.ApplyUniformEdge(currentStyle, width, num3, text, textblock);
                    break;

                case CaptionsOverrideCharacterEdges.DropShadow:
                    contentElement = this.ApplyDropShadowEdge(currentStyle, width, num3, text, textblock);
                    break;
            }
            contentElement.Opacity = (currentStyle.Visibility == Visibility.Visible) ? currentStyle.Opacity : 0.0;
            border.Child = contentElement;
            p.Children.Add(border);
            string str = text;
            string str2 = string.Empty;
            double effectiveWidth = textblock.GetEffectiveWidth();
            if (((offset + effectiveWidth) <= size2.Width) || (currentStyle.WrapOption != TextWrapping.Wrap))
            {
                offset += effectiveWidth;
                return offset;
            }
            if ((text.Length <= 0) || (text.IndexOf(' ') >= 0))
            {
                while ((offset + textblock.GetEffectiveWidth()) > size2.Width)
                {
                    int num7 = str.LastIndexOf(' ');
                    if (num7 < 0)
                    {
                        this.SetAllContent(contentElement, text);
                        return 0.0;
                    }
                    str2 = text.Substring(num7 + 1);
                    str = text.Substring(0, num7);
                    this.SetAllContent(contentElement, str);
                }
                p = this.NewPanel(parent, ref offset, element, align);
                return this.WrapElement(parent, ref p, offset, str2, element, align, true);
            }
            if ((offset != 0.0) && (effectiveWidth < size2.Width))
            {
                p.Children.Remove(border);
                p = this.NewPanel(parent, ref offset, element, align);
                return this.WrapElement(parent, ref p, 0.0, text, element, align, true);
            }
            int length = text.Length - 1;
            str = text.Substring(0, length);
            str2 = text.Substring(length);
            this.SetAllContent(contentElement, str);
            while ((offset + textblock.GetEffectiveWidth()) > size2.Width)
            {
                length--;
                str = text.Substring(0, length);
                str2 = text.Substring(length);
                this.SetAllContent(contentElement, str);
                p.UpdateLayout();
            }
            p = this.NewPanel(parent, ref offset, element, align);
            return this.WrapElement(parent, ref p, offset, str2, element, align, true);
        }
        private StackPanel NewPanel(StackPanel parent, ref double offset, CaptionElement element, TextAlignment align)
        {
            StackPanel panel = new StackPanel {
                Orientation = Orientation.Horizontal
            };
            switch (align)
            {
                case TextAlignment.Center:
                    panel.HorizontalAlignment = HorizontalAlignment.Center;
                    break;

                case TextAlignment.Left:
                    panel.HorizontalAlignment = HorizontalAlignment.Left;
                    break;

                case TextAlignment.Right:
                    panel.HorizontalAlignment = HorizontalAlignment.Right;
                    break;

                case TextAlignment.Justify:
                    panel.HorizontalAlignment = HorizontalAlignment.Stretch;
                    break;
            }
            parent.Children.Add(panel);
            offset = 0.0;
            return panel;
        }
 private UIElement RenderElement(CaptionElement element)
 {
     StackPanel parent = null;
     try
     {
         parent = new StackPanel {
             Background = GetCachedBrush(element.CurrentStyle.BackgroundColor),
             HorizontalAlignment = HorizontalAlignment.Stretch
         };
         TextAlignment textAlign = element.CurrentStyle.TextAlign;
         double offset = 0.0;
         StackPanel p = this.NewPanel(parent, ref offset, element, textAlign);
         this.RenderElementRecurse(parent, ref p, element, ref offset, textAlign);
     }
     catch (Exception)
     {
     }
     return parent;
 }
 private void ApplyWindowOverrides(CaptionElement caption)
 {
     if (caption.CaptionElementType == TimedTextElementType.Container)
     {
         if (this.CaptionsOverrideSettings.WindowColor != CaptionsOverrideColors.Default)
         {
             Color color = this.CaptionsOverrideSettings.WindowColor.ToColor();
             color.A = caption.CurrentStyle.BackgroundColor.A;
             caption.CurrentStyle.BackgroundColor = color;
         }
         if (this.CaptionsOverrideSettings.WindowOpacity != CaptionsOverrideOpacities.Default)
         {
             Color backgroundColor = caption.CurrentStyle.BackgroundColor;
             backgroundColor.A = (byte) (this.CaptionsOverrideSettings.WindowOpacity.ToDouble() * 255.0);
             caption.CurrentStyle.BackgroundColor = backgroundColor;
         }
     }
 }
 private void GetTextRecursive(CaptionElement captionElement, ref string text)
 {
     if (captionElement.IsActiveAtPosition(this._mediaPosition) && (captionElement.CurrentStyle.Display == Visibility.Visible))
     {
         if (captionElement.CaptionElementType == TimedTextElementType.Container)
         {
             foreach (CaptionElement element in captionElement.Children.Cast<CaptionElement>())
             {
                 this.GetTextRecursive(element, ref text);
             }
         }
         else if (captionElement.CaptionElementType == TimedTextElementType.Text)
         {
             text = text + captionElement.Content + Environment.NewLine;
         }
     }
 }
 private void ApplyTextOverrides(CaptionElement caption)
 {
     if (caption.CaptionElementType == TimedTextElementType.Text)
     {
         if (this.CaptionsOverrideSettings.CharacterColor != CaptionsOverrideColors.Default)
         {
             caption.CurrentStyle.Color = this.CaptionsOverrideSettings.CharacterColor.ToColor();
         }
         if (this.CaptionsOverrideSettings.CharacterOpacity != CaptionsOverrideOpacities.Default)
         {
             caption.CurrentStyle.Opacity = this.CaptionsOverrideSettings.CharacterOpacity.ToDouble();
         }
         if (this.CaptionsOverrideSettings.CharacterBackgroundColor != CaptionsOverrideColors.Default)
         {
             Color color = this.CaptionsOverrideSettings.CharacterBackgroundColor.ToColor();
             color.A = caption.CurrentStyle.BackgroundColor.A;
             caption.CurrentStyle.BackgroundColor = color;
         }
         if (this.CaptionsOverrideSettings.CharacterBackgroundOpacity != CaptionsOverrideOpacities.Default)
         {
             Color backgroundColor = caption.CurrentStyle.BackgroundColor;
             backgroundColor.A = (byte) (this.CaptionsOverrideSettings.CharacterBackgroundOpacity.ToDouble() * 255.0);
             caption.CurrentStyle.BackgroundColor = backgroundColor;
         }
         if (this.CaptionsOverrideSettings.CharacterFont != CaptionsOverrideFonts.Default)
         {
             caption.CurrentStyle.FontFamily = this.CaptionsOverrideSettings.CharacterFont.ToFontFamily();
         }
     }
 }
        private StackPanel NewPanel(StackPanel parent, ref double offset, CaptionElement element, TextAlignment align)
        {
            StackPanel p = new StackPanel
            {
                Orientation = Orientation.Horizontal
            };

            switch (align)
            {
                case TextAlignment.Center:
                    p.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
                    break;
                case TextAlignment.Right:
                    p.HorizontalAlignment = System.Windows.HorizontalAlignment.Right;
                    break;
                case TextAlignment.Left:
                    p.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
                    break;
                default:    //TextAlignment.Justify:
                    p.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
                    break;
            }
            parent.Children.Add(p);
            offset = 0;
            return p;
        }
        private double WrapElement(StackPanel parent, ref StackPanel p, double offset, string text, CaptionElement element, TextAlignment align, bool directionApplied = false)
        {
            if (text == null || text == "") return offset;

            var effectiveSize = this.GetEffectiveSize();
            var style = element.CurrentStyle;
            var panelSize = style.Extent.ToPixelSize(effectiveSize);
            double panelWidth = panelSize.Width;
            double panelHeight = panelSize.Height;

            if (style.Direction == Direction.RightToLeft && !directionApplied)
            {
                text = new string(text.ToCharArray().Reverse().ToArray());
            }

            double height = style.FontSize.Unit == LengthUnit.PixelProportional || style.FontSize.Unit == LengthUnit.Cell ? effectiveSize.Height : panelHeight;
            TextBlock textblock = GetStyledTextblock(style, panelWidth, height, false);
            SetContent(textblock, text);

            Border border = new Border();
            border.Background = GetCachedBrush(style.BackgroundColor);
            FrameworkElement contentElement;

            double outlineWidth = style.OutlineWidth.ToPixelLength(effectiveSize.Height);
            if (outlineWidth > 0)
            {
                Grid cnv = new Grid();

                // do outline image up and to left
                TextBlock tb2 = GetStyledTextblock(style, panelWidth, height, true);
                SetContent(tb2, text);
                cnv.Children.Add(tb2);
                tb2.RenderTransform = new TranslateTransform() { X = -1, Y = -1 };

                // do outline image down and to right
                tb2 = GetStyledTextblock(style, panelWidth, height, true);
                SetContent(tb2, text);
                cnv.Children.Add(tb2);
                tb2.RenderTransform = new TranslateTransform() { X = 1, Y = 1 };

                // do outline image up and to right
                tb2 = GetStyledTextblock(style, panelWidth, height, true);
                SetContent(tb2, text);
                cnv.Children.Add(tb2);
                tb2.RenderTransform = new TranslateTransform() { X = 1, Y = -1 };

                // do outline image down and to left
                tb2 = GetStyledTextblock(style, panelWidth, height, true);
                SetContent(tb2, text);
                cnv.Children.Add(tb2);
                tb2.RenderTransform = new TranslateTransform() { X = -1, Y = 1 };

                // add the main text
                cnv.Children.Add(textblock);

                // add the border
                contentElement = cnv;
            }
            else
            {
                contentElement = textblock;
            }

            border.Child = contentElement;
            p.Children.Add(border);

            string head = text;
            string tail = string.Empty;
            double elementWidth = textblock.GetEffectiveWidth();
            if (offset + elementWidth > panelSize.Width && style.WrapOption == TextWrapping.Wrap)
            {
                if (text.Length > 0 && text.IndexOf(' ') < 0)
                {
                    if (offset != 0 && elementWidth < panelSize.Width)
                    {
                        p.Children.Remove(border);
                        p = NewPanel(parent, ref offset, element, align);
                        return WrapElement(parent, ref p, 0, text, element, align, true);
                    }
                    int idx = text.Length - 1;
                    head = text.Substring(0, idx);
                    tail = text.Substring(idx);
                    SetAllContent(contentElement, head);
                    while (offset + textblock.GetEffectiveWidth() > panelSize.Width)
                    {
                        idx--;
                        head = text.Substring(0, idx);
                        tail = text.Substring(idx);
                        SetAllContent(contentElement, head);
                        p.UpdateLayout();
                    }
                    p = NewPanel(parent, ref offset, element, align);
                    return WrapElement(parent, ref p, offset, tail, element, align, true);
                }
                while (offset + textblock.GetEffectiveWidth() > panelSize.Width)
                {
                    int idx = head.LastIndexOf(' ');
                    if (idx < 0)
                    {
                        SetAllContent(contentElement, text);
                        return 0;
                    }
                    else
                    {
                        tail = text.Substring(idx + 1);
                        head = text.Substring(0, idx);
                    }
                    SetAllContent(contentElement, head);
                }
                p = NewPanel(parent, ref offset, element, align);
                return WrapElement(parent, ref p, offset, tail, element, align, true);
            }
            else
            {
                offset += elementWidth;
                return offset;
            }
        }
 private void RenderElementRecurse(StackPanel parent, ref StackPanel p, CaptionElement element, ref double offset, TextAlignment align)
 {
     if (element.IsActiveAtPosition(_mediaPosition) && element.CurrentStyle.Display == System.Windows.Visibility.Visible)
     {
         if (element.CaptionElementType == TimedTextElementType.Text)
         {
             var text = element.Content != null ? element.Content.ToString() : string.Empty;
             offset = WrapElement(parent, ref p, offset, text, element, align);
         }
         else if (element.CaptionElementType == TimedTextElementType.Container)
         {
             foreach (CaptionElement child in element.Children)
             {
                 RenderElementRecurse(parent, ref p, child, ref offset, align);
             }
         }
         else if (element.CaptionElementType == TimedTextElementType.LineBreak)
         {
             p = NewPanel(parent, ref offset, element, align);
         }
     }
 }
        private UIElement RenderElement(CaptionElement element)
        {
            StackPanel parent = null;

            try
            {
                parent = new StackPanel
                {
                    Background = GetCachedBrush(element.CurrentStyle.BackgroundColor),
                    HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch
                };


                var textAlignment = element.CurrentStyle.TextAlign;

                double offset = 0;
                StackPanel p = NewPanel(parent, ref offset, element, textAlignment);
                RenderElementRecurse(parent, ref p, element, ref offset, textAlignment);

#if !WINDOWS_PHONE && !SILVERLIGHT3 && NOTUSED
                foreach (StackPanel stack in parent.Children)
                {
                    double baseline = 0;
                    foreach (Border b in stack.Children)
                    {
                        TextBlock tb = b.Child as TextBlock;
                        if (tb != null && tb.BaselineOffset > baseline)
                        {
                            baseline = tb.ActualHeight - tb.BaselineOffset;
                        }
                    }
                    foreach (Border b in stack.Children)
                    {
                        TextBlock tb = b.Child as TextBlock;
                        if (tb != null)
                        {
                            tb.Margin = new Thickness(0, 0, 0, baseline - (tb.ActualHeight - tb.BaselineOffset));
                        }
                    }
                }
#endif
            }
            catch (Exception)
            {
                //TODO: Respond to errors
            }

            return parent;
        }