how to hook ISmoothStreamCache object in SMFPlayer (Smooth Streaming Development kit) - smooth-streaming

I am using SMFPlayer in SilverLight Smooth Streaming Development Kit.
And I am able to play video content, however for some reason we want to have
control over data being downloaded and parsed. For that Purpose we want to start using
ISmoothStreamingCache interface.
I want to know what is the right approach to hook ISmoothStreamingCache object in SMFPlayer.
Thanks in advance
Big O

The ISmoothStreamingCache's implementation should also implement IPlugin interface. It should also be decorated with ExportAdaptiveCacheProvider attribute.
Then it will be automatically hooked to the SMFPlayer.
Below is skeleton code for class:
using System;
using System.Collections.Generic;
using System.IO.IsolatedStorage;
using System.Net;
using Microsoft.SilverlightMediaFramework.Plugins;
using Microsoft.SilverlightMediaFramework.Plugins.Metadata;
using Microsoft.Web.Media.SmoothStreaming;
namespace MyNamespace
{
[ExportAdaptiveCacheProvider(PluginName = "My Smooth Streaming Cache")]
public class MySmoothStreamingCache : ISmoothStreamingCache, IPlugin
{
public MySmoothStreamingCache()
{
// Your implementation
}
#region ISmoothStreamingCache members
public IAsyncResult BeginRetrieve(CacheRequest request, AsyncCallback callback, object state)
{
// Your implementation
}
public CacheResponse EndRetrieve(IAsyncResult ar)
{
// Your implementation
}
public IAsyncResult BeginPersist(CacheRequest request, CacheResponse response, AsyncCallback callback, object state)
{
// Your implementation
}
public bool EndPersist(IAsyncResult ar)
{
// Your implementation
}
public void OpenMedia(Uri manifestUri)
{
// Your implementation
}
public void CloseMedia(Uri manifestUri)
{
// Your implementation
}
#endregion
#region IPlugin members
public bool IsLoaded { get; private set; }
public void Load()
{
IsLoaded = true;
}
public event Action<IPlugin, Microsoft.SilverlightMediaFramework.Plugins.Primitives.LogEntry> LogReady;
public event Action<IPlugin, Exception> PluginLoadFailed;
public event Action<IPlugin> PluginLoaded;
public event Action<IPlugin, Exception> PluginUnloadFailed;
public event Action<IPlugin> PluginUnloaded;
public void Unload()
{
IsLoaded = false;
}
#endregion
}
}

Related

ninject with datacontext and need for static implementation in MVC

I am using a .net standard class library to implement Entity Framework core on an existing ASP.NET MVC web application.
I am injecting the data context into the classes I am using, but I also need a static implementation for some specific method calls. The obvious issue here is that in the static context, I don't have an injected data context. so i did the following.
What is the correct way to do this?
I am using a Ninject Module in the library to map dependencies to the kernel in the web project.
namespace AppealTrack.Logic.Classes
{
using System;
using System.Collections.Generic;
using System.Linq;
using Common.Entities;
using Data;
using Interfaces;
using Microsoft.EntityFrameworkCore;
public class LookupLogic : ILookupLogic, IDisposable
{
private readonly AppealTrackDataContext _context;
public LookupLogic(AppealTrackDataContext context)
{
_context = context;
}
public List<County> GetCountries(string state)
{
var list = _context.Countries.Where(x => state == null || x.State == state).Distinct().AsNoTracking().ToList();
return list;
}
public void Dispose()
{
}
}
public static class LookupLogicStatic
{
public static List<County> GetCountries(string state)
{
// this is the part that I don't think is correct:
using (var logic = new LookupLogic(new AppealTrackDataContext()))
{
var list = logic.GetCountries(state);
return list;
}
}
}
}
If you require a static GetCountries method, then there is not much you can do... you would need to manage the scope of DbContext for that method as static constructor does not take any arguments. Your LookupLogic does not have to be Disposable though (it can be if you want).
If you require a static method, then you don't have to add a new static class, you can add the static method to LookupLogic:
public class LookupLogic : ILookupLogic
{
private readonly AppealTrackDataContext _context;
public LookupLogic(AppealTrackDataContext context)
{
_context = context;
}
public List<Country> GetCountries(string state)
{
return GetCountries(_context, state);
}
public static List<Country> GetCountriesStatic(string state)
{
using (var dbContext = new AppealTrackDataContext())
{
return GetCountries(dbContext, state);
}
}
private static List<Country> GetCountries(AppealTrackDataContext dbContext, string state)
{
return dbContext.Countries.Where(x => state == null || x.State == state).Distinct().AsNoTracking().ToList();
}
}

Custom Repeat not breaking in IF

I am creating a CustomRepeat by extending CustomLoopTest. It never breaks in the IF condition.
Because break happens only for concrete class LoopTestStep, is it possible to modify LoopTestStep inside the IF condition by an interface? So we can implement that interface in our CustomLoopTestStep.
Another possiblity is to help to provide an alternative way.
public abstract class CustomLoopTestStep : CustomTestStep
{
protected CancellationTokenSource breakLoopToken { get; private set; }
[Browsable(false)]
protected CancellationToken BreakLoopRequested { get { return breakLoopToken.Token; } }
public CustomLoopTestStep()
{
breakLoopToken = new CancellationTokenSource();
}
public void BreakLoop()
{
breakLoopToken.Cancel();
}
/// <summary> Always call base.Run in LoopTestStep inheritors. </summary>
public override void Run()
{
breakLoopToken = new CancellationTokenSource();
}
}
At the moment (OpenTAP v9.4 and earlier), you need to inherit from LoopTestStep to interact with the If Verdict step. Otherwise if you prefer to achieve this by implementing an interface, you can submit a feature request here to add an interface in OpenTAP.

How should Dispose be coded in classes that implement IDependencyResolver and IDependencyScope?

I ran Code Analysis on my Web API project, in which I'm trying to implement IoC and DI using Castle Windsor, and it found four problems. The four things it found were all in WindsorDependencyResolver, and all four are "Implement IDisposable correctly" namely:
0)
CA1063 Implement IDisposable correctly Provide an overridable implementation of Dispose(bool) on 'WindsorDependencyResolver' or mark the type as sealed. A call to Dispose(false) should only clean up native resources. A call to Dispose(true) should clean up both managed and native resources.
That points to this line of code:
public class WindsorDependencyResolver : System.Web.Http.Dependencies.IDependencyResolver
1)
CA1063 Implement IDisposable correctly Modify 'WindsorDependencyResolver.Dispose()' so that it calls Dispose(true), then calls GC.SuppressFinalize on the current object instance ('this' or 'Me' in Visual Basic), and then returns.
That points to this line of code:
public void Dispose()
2) Same as O, but for the WindsorDependencyScope : IDependencyScope class.
3) Same as 1, but ""
I got the code I'm trying from Castle Windsor articles online, mainly from from this post. The entire code for this file is:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http.Dependencies;
using Castle.Windsor;
using Castle.MicroKernel.Registration;
using System.Web.Http;
using Castle.MicroKernel.Lifestyle;
using Castle.MicroKernel.SubSystems.Configuration;
using HandheldServer.Models;
namespace HandheldServer
{
public class WindsorDependencyResolver : System.Web.Http.Dependencies.IDependencyResolver
{
private readonly IWindsorContainer _container;
public WindsorDependencyResolver(IWindsorContainer container)
{
_container = container;
}
public IDependencyScope BeginScope()
{
return new WindsorDependencyScope(_container);
}
public object GetService(Type serviceType)
{
return _container.Kernel.HasComponent(serviceType) ? _container.Resolve(serviceType) : null;
}
public IEnumerable<object> GetServices(Type serviceType)
{
if (!_container.Kernel.HasComponent(serviceType))
{
return new object[0];
}
return _container.ResolveAll(serviceType).Cast<object>();
}
public void Dispose()
{
_container.Dispose();
}
}
public class WindsorDependencyScope : IDependencyScope
{
private readonly IWindsorContainer _container;
private readonly IDisposable _scope;
public WindsorDependencyScope(IWindsorContainer container)
{
this._container = container;
this._scope = container.BeginScope();
}
public object GetService(Type serviceType)
{
if (_container.Kernel.HasComponent(serviceType))
{
return _container.Resolve(serviceType);
}
else
{
return null;
}
}
public IEnumerable<object> GetServices(Type serviceType)
{
return this._container.ResolveAll(serviceType).Cast<object>();
}
public void Dispose()
{
this._scope.Dispose();
}
}
public class ApiControllersInstaller : IWindsorInstaller
{
public void Install(Castle.Windsor.IWindsorContainer container, Castle.MicroKernel.SubSystems.Configuration.IConfigurationStore store)
{
container.Register(Classes.FromThisAssembly() // should it be Types instead of Classes?
.BasedOn<ApiController>()
.LifestylePerWebRequest());
}
}
// This idea from https://github.com/argeset/set-locale/blob/master/src/client/SetLocale.Client.Web/Configurations/IocConfig.cs
public class ServiceInstaller : IWindsorInstaller
{
public void Install(IWindsorContainer container, IConfigurationStore store)
{
container.Register(
Component.For<IDeliveryItemRepository>().ImplementedBy<DeliveryItemRepository>().LifestylePerWebRequest(),
Component.For<IDeliveryRepository>().ImplementedBy<DeliveryRepository>().LifestylePerWebRequest(),
Component.For<IDepartmentRepository>().ImplementedBy<DepartmentRepository>().LifestylePerWebRequest(),
Component.For<IExpenseRepository>().ImplementedBy<ExpenseRepository>().LifestylePerWebRequest(),
Component.For<IInventoryItemRepository>().ImplementedBy<InventoryItemRepository>().LifestylePerWebRequest(),
Component.For<IInventoryRepository>().ImplementedBy<InventoryRepository>().LifestylePerWebRequest(),
Component.For<IItemGroupRepository>().ImplementedBy<ItemGroupRepository>().LifestylePerWebRequest());
}
}
}
What is the best way to mollify the Code Analysis tool?
Before answering your question please note that you should be very careful calling Dispose() on an object you didn't create and therefore are probably not responsible for. By this I mean the line
_container.Dispose();
the container was passed in so strictly speaking it is not yours to dispose of.
The easiest way to mollify the Code Analysis tool is to implement IDisposable as recommended
public class WindsorDependencyResolver
{
public WindsorDependencyResolver()
{
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
private bool disposed;
protected virtual void Dispose(bool disposing)
{
if (this.disposed) return;
if (disposing)
{
// call dispose on managed resources
// set to null
}
this.disposed = true;
}
}
See here for a thorough explanation of why, followed by many reasoned arguments as to why not! I would suggest that at the end of the day you should stick to your chosen standards and if that includes the code analysis tool then so be it.

With WPF DataBinding how do I have multiple clients for single source?

I have a data class that implements INotifyPropertyChanged and two WPF controls that DataBind to the same value. Only one WPF control is updated, why?
Here is my data class:
using System;
using System.ComponentModel;
using System.Windows.Threading;
namespace TestMultiBind
{
class DataSource : INotifyPropertyChanged
{
static int _DataValue;
static DispatcherTimer tmr = new DispatcherTimer();
static int _ClientCount = 0;
#region InotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
#endregion
public int DataValue
{
get { return _DataValue; }
}
public DataSource()
{
if (!DesignerProperties.GetIsInDesignMode(new System.Windows.DependencyObject()))
{
_ClientCount = _ClientCount + 1;
if (!tmr.IsEnabled)
{
tmr.Interval = TimeSpan.FromMilliseconds(10);
tmr.Tick += new EventHandler(tmr_Tick);
tmr.Start();
}
}
}
void tmr_Tick(object sender, EventArgs e)
{
_DataValue = DateTime.Now.Second;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("DataValue"));
}
}
}
}
and here is my XAML
Title="Window1" Height="300" Width="300">
<Grid>
<StackPanel>
<ProgressBar Height="20" Name="progressBar1" Value="{Binding Mode=OneWay, Path=DataValue}" Maximum="60">
<ProgressBar.DataContext>
<ds:DataSource/>
</ProgressBar.DataContext>
</ProgressBar>
<ProgressBar Height="30" Name="progressBar2" Value="{Binding Mode=OneWay, Path=DataValue}" Maximum="60">
<ProgressBar.DataContext>
<ds:DataSource/>
</ProgressBar.DataContext>
</ProgressBar>
</StackPanel>
</Grid>
I have tried to present the simplest example possible, thus I created a data class that uses a timer to update a value once per second. The real world problem I am trying to solve is data coming across a serial port that is to be displayed. Thus I need to store static data for the incoming data within the class as well as handling instance specific events for each of the clients (WPF controls) that are data bound to the control.
It would appear that my tmr_Tick routine is somehow instance specific to the first client as opposed to being static to the class and raising the multiple events required for each of the clients.
What am I missing here or doing wrong?
I'm not sure if this is the best way to do this, but it works. The problem with my initial code was that I needed both a static and a instance constructor for the class. The timer is created in the static constructor (which is only run once) while there needs to be one event handler per binding so this is handled in the instance constructor.
Here is the code that works:
using System;
using System.ComponentModel;
using System.Windows.Threading;
namespace TestMultiBind
{
class DataSource : INotifyPropertyChanged
{
static int _DataValue;
static DispatcherTimer tmr = new DispatcherTimer();
#region InotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
#endregion
public int DataValue
{
get { return _DataValue; }
}
static DataSource()
{
if (!DesignerProperties.GetIsInDesignMode(new System.Windows.DependencyObject()))
{
tmr.Interval = TimeSpan.FromMilliseconds(10);
tmr.Start();
}
}
public DataSource()
{
if (!DesignerProperties.GetIsInDesignMode(new System.Windows.DependencyObject()))
{
tmr.Tick += new EventHandler(tmr_Tick);
}
}
void tmr_Tick(object sender, EventArgs e)
{
_DataValue = DateTime.Now.Second;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("DataValue"));
}
}
}
}

Registering a Generics Implementation of a Generic Interface in StructureMap

i have a generic interface
public interface IDomainDataRepository<T>
{
T[] GetAll();
}
with a generic implementation
public class DomainDataRepository<T> : IDomainDataRepository<T>
{
public virtual T[] GetAll()
{
return GetSession().Linq<T>().ToArray();
}
}
how do I register it in StructureMap so that if I request IDomainDataRepository<State> then it will new up a DomainDataRepository<State>. Furthermore if I decide to implement a CountryDomainDataRepository and I request a IDomainDataRepository<Country> I want to get the specific implementation.
public class CountryDomainDataRepository : IDomainDataRepository<State>
{
public virtual Country[] GetAll()
{
return GetSession().Linq<Country>().ToArray();
}
}
You can accomplish this by configuring the generic open type to use a concrete open type:
[TestFixture]
public class open_generic_registration
{
[Test]
public void should_resolve_to_the_configured_concrete_instance_of_T()
{
var container = new Container(cfg =>
{
cfg.For(typeof (IDomainDataRepository<>)).Use(typeof (DomainDataRepository<>));
});
container.GetInstance<IDomainDataRepository<string>>().ShouldBeOfType<DomainDataRepository<string>>();
container.GetInstance<IDomainDataRepository<int>>().ShouldBeOfType<DomainDataRepository<int>>();
container.GetInstance<IDomainDataRepository<DateTime>>().ShouldBeOfType<DomainDataRepository<DateTime>>();
}
}

Resources