Zhou's profileSheva's TechSpacePhotosBlogLists Tools Help

Blog


    1/5/2009

    Streaming Media Content Over WCF RESTful Service

    WCF RESTful service API enables you to serve POX type of content over the http transport, and its default content-type header is application/xml if you use DataContractSerializer and application/json if you use DataContractJsonSerializer. If you need to serve up other contents for instance video/audio content, you need to explicitly control the content-type header, this could be achieved again by writing custom message formatter as demonstrated in the previous post, here is the code:

    public class ContentTypeMessageFormatter : IDispatchMessageFormatter
    {
        private IDispatchMessageFormatter formatter;
        private String contentType;
        public ContentTypeMessageFormatter(IDispatchMessageFormatter formatter, String contentType)
        {
            this.formatter = formatter;
            this.contentType = contentType;
        }

        public void DeserializeRequest(Message message, object[] parameters)
        {
            formatter.DeserializeRequest(message, parameters);
        }

        public Message SerializeReply(MessageVersion messageVersion, Object[] parameters, Object result)
        {
            if (!String.IsNullOrEmpty(contentType))
            {
                WebOperationContext.Current.OutgoingResponse.ContentType = contentType;
            }
            return formatter.SerializeReply(messageVersion, parameters, result);
        }
    }

    public class ContentTypeAttribute : Attribute, IOperationBehavior
    {
        public ContentTypeAttribute(String contentType)
        {
            this.ContentType = contentType;
        }

        public String ContentType
        {
            get;
            set;
        }

        public void AddBindingParameters(OperationDescription operationDescription, BindingParameterCollection bindingParameters)
        {
        }

        public void ApplyClientBehavior(OperationDescription operationDescription, ClientOperation clientOperation)
        {
        }

        public void ApplyDispatchBehavior(OperationDescription operationDescription, DispatchOperation dispatchOperation)
        {
            dispatchOperation.Formatter = new ContentTypeMessageFormatter(dispatchOperation.Formatter, ContentType);
        }

        public void Validate(OperationDescription operationDescription)
        {
        }
    }

    Then you could directly specify the ContentTypeAttribute at the service operation level, the following service could stream WMV video content over http by turning on the streamed transfer mode of WCF:

    [ServiceContract]
    public interface IMediaService
    {
        [OperationContract]
        [ContentType("audio/x-ms-wmv")]
        [WebGet(UriTemplate = "media/{name}")]
        Stream GetMedia(String name);
    }

    [AspNetCompatibilityRequirements(RequirementsMode=AspNetCompatibilityRequirementsMode.Required)]
    public class MediaService : IMediaService
    {
        public Stream GetMedia(String name)
        {
            var dir = HttpContext.Current.Server.MapPath("~");
            var file = String.Format("{0}.wmv", name);
            var filePath = Path.Combine(dir, file);
            return File.OpenRead(filePath);
        }
    }

    You turn on streaming in WCF, you need to configure the service as follows:

    <system.serviceModel>
      <
    serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
      <
    services>
        <
    service
         behaviorConfiguration="serviceBehavior"
         name="CustomContentTypeInRESTDemo.MediaService">
          <
    endpoint
           behaviorConfiguration="RestBehaviorConfig"
           binding="webHttpBinding"
           bindingConfiguration="HttpStreaming"
           contract="CustomContentTypeInRESTDemo.IMediaService"/>
        </
    service>
      </
    services>
      <
    bindings>
        <
    webHttpBinding>
          <
    binding name="HttpStreaming" maxReceivedMessageSize="67108864" transferMode="Streamed"/>
        </
    webHttpBinding>
      </
    bindings>
      <
    behaviors>
        <
    endpointBehaviors>
          <
    behavior name="RestBehaviorConfig">
            <
    webHttp/>
          </
    behavior>
        </
    endpointBehaviors>
      </
    behaviors>
    </
    system.serviceModel>

    Streaming audio/video content using RESTful service could be pretty useful, in particular if you need to use WPF’s MediaElement to playback content provided by your service, since MediaElement only allows you to specify a Uri of the media file, the DirectShow has build-in source filter to feed video/audio samples from HTTP transport or local file system, but doesn’t provide a built-in source filter to read samples from arbitrary stream.

    In WPF, you could directly have MediaElement’s Source property pointing to the templated REST Uri:

    <MediaElement Source="http://localhost:8080/MediaService.svc/media/testVideo"/>

    Actually, if you need VCR type of control over the streamed media content, then you need native protocol level support such as RTSP. Windows Media Services is Microsoft’s server implementation of RTSP protocol, and is available in Windows 2003 and Windows 2008. This link illustrates how to configure WMS in Windows 2003.

    Comments

    Please wait...
    Sorry, the comment you entered is too long. Please shorten it.
    You didn't enter anything. Please try again.
    Sorry, we can't add your comment right now. Please try again later.
    To add a comment, you need permission from your parent. Ask for permission
    Your parent has turned off comments.
    Sorry, we can't delete your comment right now. Please try again later.
    You've exceeded the maximum number of comments that can be left in one day. Please try again in 24 hours.
    Your account has had the ability to leave comments disabled because our systems indicate that you may be spamming other users. If you believe that your account has been disabled in error please contact Windows Live support.
    Complete the security check below to finish leaving your comment.
    The characters you type in the security check must match the characters in the picture or audio.
    Zhou Yong has turned off comments on this page.

    Trackbacks

    The trackback URL for this entry is:
    http://shevaspace.spaces.live.com/blog/cns!FD9A0F1F8DD06954!2014.trak
    Weblogs that reference this entry
    • None