W3C

Mediastream Image Capture

W3C Editor's Draft 19 March 2013

This version:
http://gmandyam.github.com/image-capture
Latest published version:
http://www.w3.org/TR/image-capture/
Latest editor's draft:
http://gmandyam.github.com/image-capture
Editor:
Giridhar Mandyam, Qualcomm Innovation Center, Inc

Abstract

This document specific the takePhoto() method and corresponding camera settings or use with MediaStreams as defined in Media Capture and Streams [GETUSERMEDIA].

Status of This Document

This section describes the status of this document at the time of its publication. Other documents may supersede this document. A list of current W3C publications and the latest revision of this technical report can be found in the W3C technical reports index at http://www.w3.org/TR/.

Comments on this document are welcomed.

This document was published by the Media Capture Task Force as an Editor's Draft. If you wish to make comments regarding this document, please send them to public-media-capture@w3.org (subscribe, archives). All feedback is welcome.

Publication as an Editor's Draft does not imply endorsement by the W3C Membership. This is a draft document and may be updated, replaced or obsoleted by other documents at any time. It is inappropriate to cite this document as other than work in progress.

This document was produced by a group operating under the 5 February 2004 W3C Patent Policy. W3C maintains a public list of any patent disclosures made in connection with the deliverables of the group; that page also includes instructions for disclosing a patent. An individual who has actual knowledge of a patent which the individual believes contains Essential Claim(s) must disclose the information in accordance with section 6 of the W3C Patent Policy.

Table of Contents

1. Introduction

The API defined in this document taks a valid MediaStream and returns an encoded image in the form of a Blob (as defined in [FILE-API]). The image is provided by the capture device that provides the MediaStream. Moreover, picture-specific settings can be optionally provided as arguments that can be applied to the image being captured.

2. Image Capture API

[Constructor(MediaStreamTrack track)]
interface ImageCapture : EventTarget {
    readonly attribute PhotoSettingsOptions photoSettingsOptions;
    readonly attribute MediaStreamTrack     videoStreamTrack;
             attribute EventHandler         onphoto;
             attribute EventHandler         onphotoerror;
             attribute EventHandler         onphotosettingschange;
             attribute EventHandler         onphotosettingserror;
             attribute EventHandler         onframegrab;
             attribute EventHandler         onframegraberror;
    void setPhotoSettings (PhotoSettings? photoSettings);
    void takePhoto ();
    void clearFrameGrabber ();
    void getFrame ();
    void frameGrabber ();
};

2.1 Attributes

onframegrab of type EventHandler
Register/unregister for frame capture events of type FrameGrabEvent. The handler should expect to get a FrameGrabEvent object as its first parameter.
onframegraberror of type EventHandler
Register/unregister for frame capture errors of type FrameGrabErrorEvent.
onphoto of type EventHandler
Register/unregister for photo events of type BlobEvent. The handler should expect to get a BlobEvent object as its first parameter.
onphotoerror of type EventHandler
Register/unregister for photo error events of type PhotoErrorEvent. The handler should expect to get a PhotoError object as its first parameter.
onphotosettingschange of type EventHandler
Register/unregister for photo settings change events of type SettingsChangeEvent.
onphotosettingserror of type EventHandler
Register/unregister for photo settings error events of type SettingsErrorEvent.
photoSettingsOptions of type PhotoSettingsOptions, readonly
Describes current photo settings
videoStreamTrack of type MediaStreamTrack, readonly
The MediaStream passed into the constructor

2.2 Methods

clearFrameGrabber
If the frameGrabber() method has been successfully invoked previously, the UA must immediately terminate the corresponding process. Otherwise the method must immediately return without further action taking place.
No parameters.
Return type: void
frameGrabber
When the frameGrabber() method of an ImageCapture object is invoked, then if the MediaStreamTrack provided in the contructor does not have the kind attribute set to "video" or if the MediaStreamTrack's readyState is not "live", the UA must fire a FrameGrabErrorEvent event at the ImageCapture object with a new FrameError object whose code is set to INVALID_TRACK. If the UA is unable to execute the frameGrabber() method for any other reason, then the UA must fire a FrameGrabErrorEvent event at the ImageCapture object with a new FrameError object whose code is set to FRAME_GRAB_ERROR. Otherwise it must queue a task, using the DOM manipulation task source, that starts a process where the following steps are executed every time a new frame of image data is available:
  1. Gather data from the MediaStreamTrack into a ImageData object (as defined in [CANVAS-2D]) containing a single still frame in RGBA format. The width and height of the ImageData object are derived from the constraints of the MediaStreamTrack.
  2. Raise a FrameGrabEvent event containing the ImageData to the onframegrab event handler (if specified).
No parameters.
Return type: void
getFrame
When the getFrame() method of an ImageCapture object is invoked, then if the MediaStreamTrack provided in the contructor does not have the kind attribute set to "video" or if the MediaStreamTrack's readyState is not "live", the UA must fire a FrameGrabErrorEvent event at the ImageCapture object with a new FrameError object whose code is set to INVALID_TRACK. If the UA is unable to execute the getFrame() method for any other reason, then the UA must fire a FrameGrabErrorEvent event at the ImageCapture object with a new FrameError object whose code is set to FRAME_GRAB_ERROR. Otherwise it must queue a task, using the DOM manipulation task source, that runs the following steps:
  1. Gather data from the MediaStreamTrack into a ImageData object (as defined in [CANVAS-2D]) containing a single still frame in RGBA format. The width and height of the ImageData object are derived from the constraints of the MediaStreamTrack.
  2. Raise a FrameGrabEvent event containing the ImageData to the onframegrab event handler (if specified). {Note: Unlike frameGrabber(), getFrame() returns data only once upon being invoked.}
No parameters.
Return type: void
setPhotoSettings
When the setPhotoSettings() method of an ImageCapture object is invoked, then then a valid PhotoSettings object must be passed in the method to the ImageCapture object. If the UA can successfully apply the settings, then the UA must fire a SettingsChangeEvent event at the onphotosettingschange event handler (if specified). If the UA cannot successfully apply the settings, then the UA must fire a SettingsErrorEvent at the ImageCapture object.
ParameterTypeNullableOptionalDescription
photoSettingsPhotoSettings
Return type: void
takePhoto
When the takePhoto() method of an ImageCapture object is invoked, then if the MediaStreamTrack provided in the contructor does not have the kind attribute set to "video" or if the MediaStreamTrack's readyState is not "live", the UA must fire a PhotoErrorEvent event at the ImageCapture object with a new PhotoError object whose code is set to INVALID_TRACK. If the UA is unable to execute the takePhoto() method for any other reason, then the UA must fire a PhotoErrorEvent event at the ImageCapture object with a new PhotoError object whose code is set to PHOTO_ERROR. Otherwise it must queue a task, using the DOM manipulation task source, that runs the following steps:
  1. Gather data from the MediaStreamTrack into a Blob containing a single still image. The method of doing this will depend on the underlying device. Devices may temporarily stop streaming data, reconfigure themselves with the appropriate photo settings, take the photo, and then resume streaming. In this case, the stopping and restarting of streaming should cause mute and unmute events to fire on the Track in question.
  2. Raise a BlobEvent event containing the Blob to the onphoto event handler (if specified).
No parameters.
Return type: void

3. FrameGrabEvent

[Constructor(DOMString type, optional FrameGrabEventInit frameGrabInitDict)]
interface FrameGrabEvent : Event {
    readonly attribute ImageData imageData;
};

3.1 Attributes

imageData of type ImageData, readonly
Returns an ImageData object whose length and height attributes indicates the dimensions of the captured frame.

3.2 FrameGrabEventInit Dictionary

dictionary FrameGrabEventInit : EventInit {
    ImageData imageData;
};

3.2.1 Dictionary FrameGrabEventInit Members

imageData of type ImageData
An ImageData object containing the data to deliver via this event.

4. FrameGrabErrorEvent

[Constructor(DOMString type, optional FrameGrabErrorEventInit frameGrabErrorInitDict)]
interface FrameGrabErrorEvent : Event {
    readonly attribute FrameError frameError;
};

4.1 Attributes

frameError of type FrameError, readonly
Returns a FrameError object whose code attribute indicates the type of error occurrence.

4.2 FrameGrabErrorEventInit Dictionary

dictionary FrameGrabErrorEventInit : EventInit {
    FrameError frameError;
};

4.2.1 Dictionary FrameGrabErrorEventInit Members

frameError of type FrameError
A FrameError object containing the data to deliver via this event.

5. BlobEvent

[Constructor(DOMString type, optional BlobEventInit blobInitDict)]
interface BlobEvent : Event {
    readonly attribute Blob data;
};

5.1 Attributes

data of type Blob, readonly
Returns a Blob object whose type attribute indicates the encoding of the blob data. An implementation must return a Blob in a format that is capable of being viewed in an HTML <img> tag.

5.2 BlobEventInit Dictionary

dictionary BlobEventInit : EventInit {
    Blob data;
};

5.2.1 Dictionary BlobEventInit Members

data of type Blob
A Blob object containing the data to deliver via this event.

6. PhotoErrorEvent

[Constructor(DOMString type, optional PhotoErrorEventInit photoErrorInitDict)]
interface PhotoErrorEvent : Event {
    readonly attribute PhotoError photoError;
};

6.1 Attributes

photoError of type PhotoError, readonly
Returns a PhotoError object whose code attribute indicates the type of error occurrence.

6.2 PhotoErrorEventInit Dictionary

dictionary PhotErrorEventInit : EventInit {
    PhotoError photoError;
};

6.2.1 Dictionary PhotErrorEventInit Members

photoError of type PhotoError
A photoError object containing the data to deliver via this event.

7. SettingsChangeEvent

[Constructor(DOMString type, optional SettingsChangeEventInit photoSettingsInitDict)]
interface PhotoSettingsEvent : Event {
    readonly attribute PhotoSettings photoSettings;
};

7.1 Attributes

photoSettings of type PhotoSettings, readonly
Returns a PhotoSettings object whose type attribute indicates the current photo settings.

7.2 SettingsChangeEventInit Dictionary

dictionary SettingsChangeEventInit : EventInit {
    PhotoSettings photoSettings;
};

7.2.1 Dictionary SettingsChangeEventInit Members

photoSettings of type PhotoSettings
A PhotoSettings object containing the data to deliver via this event.

8. SettingsErrorEvent

[Constructor(DOMString type, optional SettingsErrorEventInit photoSettingsInitDict)]
interface SettingsErrorEvent : Event {
    readonly attribute PhotoSettings photoSettings;
};

8.1 Attributes

photoSettings of type PhotoSettings, readonly
Returns a PhotoSettings object whose type attribute indicates the current photo settings.

8.2 SettingsErrorEventInit Dictionary

dictionary SettingsErrorEventInit : EventInit {
    PhotoSettings photoSettings;
};

8.2.1 Dictionary SettingsErrorEventInit Members

photoSettings of type PhotoSettings
A PhotoSettings object containing the data to deliver via this event.

9. MediaSettingsRange

interface MediaSettingsRange {
    readonly attribute unsigned long max;
    readonly attribute unsigned long min;
    readonly attribute unsigned long initial;
};

9.1 Attributes

initial of type unsigned long, readonly
The current value of this setting
max of type unsigned long, readonly
The maximum value of this setting
min of type unsigned long, readonly
The minimum value of this setting

10. MediaSettingsItem

The MediaSettingsItem interface is now defined, which allows for a single setting to be managed.

interface MediaSettingsItem {
    readonly attribute any value;
};

10.1 Attributes

value of type any, readonly
Value of current setting.

11. PhotoSettingsOptions

The PhotoSettingsOptions attribute of the ImageCapture object provides the photo-specific settings options and current settings values.

interface PhotoSettingsOptions {
             attribute MediaSettingsItem  whiteBalanceMode;
             attribute MediaSettingsItem  autoExposureMode;
             attribute MediaSettingsRange exposureCompensation;
             attribute MediaSettingsRange iso;
             attribute MediaSettingsItem  redEyeReduction;
             attribute MediaSettingsRange brightness;
             attribute MediaSettingsRange constrast;
             attribute MediaSettingsRange saturation;
             attribute MediaSettingsRange sharpness;
             attribute MediaSettingsRange imageHeight;
             attribute MediaSettingsRange imageWidth;
};

11.1 Attributes

autoExposureMode of type MediaSettingsItem
This reflects the current auto exposure mode setting. Values are of type ExposureModeEnum.
brightness of type MediaSettingsRange
This reflects the current brightness setting of the camera and permitted range. Values are numeric.
constrast of type MediaSettingsRange
This reflects the current contrast setting of the camera and permitted range. Values are numeric.
exposureCompensation of type MediaSettingsRange
This reflects the current exposure compensation setting and permitted range. Values are numeric.
imageHeight of type MediaSettingsRange
This reflects the image height range supported by the UA and the current height setting.
imageWidth of type MediaSettingsRange
This reflects the image width range supported by the UA and the current width setting.
iso of type MediaSettingsRange
This reflects the current camera ISO setting and permitted range. Values are numeric.
redEyeReduction of type MediaSettingsItem
This reflects whether camera red eye reduction is on or off, and is boolean - on is true
saturation of type MediaSettingsRange
This reflects the current saturation setting of the camera and permitted range. Values are numeric.
sharpness of type MediaSettingsRange
This reflects the current sharpness setting of the camera and permitted range. Values are numeric.
whiteBalanceMode of type MediaSettingsItem
This reflects the current white balance mode setting. Values are of type WhiteBalanceModeEnum.

12. WhiteBalanceModeEnum

enum WhiteBalanceModeEnum {
    "auto",
    "incandescent",
    "fluorescent",
    "warm-fluorescent",
    "daylight",
    "cloudy-daylight",
    "twilight",
    "shade"
};
Enumeration description
autoauto
incandescent2500-3500 Kelvin
fluorescent4000-5000 Kelvin
warm-fluorescent5000-5500 Kelvin
daylight5000-6500 Kelvin
cloudy-daylight6500-8000 Kelvin
twilight8000-9000 Kelvin
shade9000-10000 Kelvin

13. ExposureModeEnum

enum ExposureModeEnum {
    "frame-average",
    "center-weighted",
    "spot-metering"
};
Enumeration description
frame-averageAverage of light information from entire scene
center-weightedSensitivity concentrated towards center of viewfinder
spot-meteringSpot-centered weighting

14. PhotoSettings

The PhotoSettings object is optionally passed into the ImageCapture.setPhotoSettings() method in order to modify capture device settings specific to still imagery. Each of the attributes in this object are optional.

dictionary PhotoSettings {
    attribute any           whiteBalanceModeSetting;
    attribute any           autoExposureModeSetting;
    attribute unsigned long exposureCompensationSetting;
    attribute unsigned long isoSetting;
    attribute boolean       redEyeReductionSetting;
    attribute unsigned long brightnessSetting;
    attribute unsigned long constrastSetting;
    attribute unsigned long saturationSetting;
    attribute unsigned long sharpnessSetting;
    attribute unsigned long imageHeightSetting;
    attribute unsigned long imageWidthSetting;
};

14.1 Dictionary PhotoSettings Members

autoExposureModeSetting of type attribute any
This reflects the desired auto exposure mode setting. Acceptable values are of type ExposureModeEnum.
brightnessSetting of type attribute unsigned long
This reflects the desired brightness setting of the camera.
constrastSetting of type attribute unsigned long
This reflects the desired contrast setting of the camera.
exposureCompensationSetting of type attribute unsigned long
This reflects the desired exposure compensation setting.
imageHeightSetting of type attribute unsigned long
This reflects the desired image height. The UA must select the closest height value this setting if it supports a discrete set of height options.
imageWidthSetting of type attribute unsigned long
This reflects the desired image width. The UA must select the closest width value this setting if it supports a discrete set of width options.
isoSetting of type attribute unsigned long
This reflects the desired camera ISO setting.
redEyeReductionSetting of type attribute boolean
This reflects whether camera red eye reduction is desired
saturationSetting of type attribute unsigned long
This reflects the desired saturation setting of the camera.
sharpnessSetting of type attribute unsigned long
This reflects the desired sharpness setting of the camera.
whiteBalanceModeSetting of type attribute any
This reflects the desired white balance mode setting. Acceptable values are of type WhiteBalanceModeEnum.

15. FrameError

The FrameError object is passed to an onframegraberror event handler of an ImageCapture object if an error occurred when the getFrame() method was invoked.

interface FrameError {
    readonly attribute unsigned short code;
    const unsigned short FRAME_GRAB_ERROR = 0;
    const unsigned short INVALID_TRACK = 1;
};

15.1 Attributes

code of type unsigned short, readonly
Returns appropriate error code derived from list of pre-defined constants

15.2 Constants

FRAME_GRAB_ERROR of type unsigned short
value of code for when an error occurred while capturing the frame
INVALID_TRACK of type unsigned short
value of code for when the MediaStreamTrack is not suitable for frame capture

16. PhotoError

The PhotoError object is passed to an onphotoerror event handler of an ImageCapture object if an error occurred when the takePhoto() method was invoked.

interface PhotoError {
    readonly attribute unsigned short code;
    const unsigned short PHOTO_ERROR = 0;
    const unsigned short INVALID_TRACK = 1;
};

16.1 Attributes

code of type unsigned short, readonly
Returns appropriate error code derived from list of pre-defined constants

16.2 Constants

INVALID_TRACK of type unsigned short
value of code for when the MediaStreamTrack is not suitable for image capture
PHOTO_ERROR of type unsigned short
value of code for when an error occurred while capturing the image

17. Examples

17.1 Taking a picture if Red Eye Reduction is activated

Example 1
navigator.getUserMedia({video: true}, gotMedia, failedToGetMedia);

function gotMedia(mediastream) {
   //Extract video track.  'kind' attribute not checked because stream was created with video option only.
   var videoDevice = mediastream.getTrackByID()[0];
   // Check if this device supports a picture mode...
   var pictureDevice = new ImageCapture(videoDevice);
   if (pictureDevice) {
         pictureDevice.onphoto = showPicture;
         if (pictureDevice.photoSettingsOptions.redEyeReduction) {
            pictureDevice.setPhotoSettings({redEyeReductionSetting:true});
            }
         else
            console.log('No red eye reduction');
         pictureDevice.onphotosettingschange = function(){
            if (pictureDevice.photoSettingsOptions.redEyeReduction.value)
                pictureDevice.takePhoto();
            }
         }
     }

 function showPicture(e) {
    var img = document.querySelector("img");
    img.src = URL.createObjectURL(e.data);
    }

 function failedToGetMedia{
    console.log('Stream failure');
    }

17.2 Grabbing a Frame for Post-Processing

Example 2
navigator.getUserMedia({video: true}, gotMedia, failedToGetMedia);

function gotMedia(mediastream) {
   //Extract video track.  'kind' attribute not checked because stream was created with video option only.
   var videoDevice = mediastream.getTrackByID()[0];
   // Check if this device supports a picture mode...
   var pictureDevice = new ImageCapture(videoDevice);
   if (pictureDevice) {
         pictureDevice.onframegrab = processFrame;
         pictureDevice.getFrame();
         }
     }

 function processFrame(e) {
    imgData = e.imageData;
    width = imgData.width;
    height = imgData.height;
    for (j=3; j < imgData.length; j+=4)
         {
         // Set all alpha values to medium opacity
         imgData.data[j] = 128;
         }
    // Create new ImageObject with the modified pixel values
    var canvas = document.createElement('canvas');
    ctx = canvas.getContext("2d");
    newImg = ctx.createImageData(width,height);
    for (j=0; j < imgData.length; j++)
         {
         newImg.data[j] = imgData.data[j];
         }
    // ... and do something with the modified image ...
    }

 function failedToGetMedia{
    console.log('Stream failure');
    }

A. References

A.1 Normative references

[CANVAS-2D]
Ian Hickson. HTML Canvas 2D Context. 25 May 2011. W3C Working Draft. (Work in progress.) URL: http://www.w3.org/TR/2dcontext/
[FILE-API]
Arun Ranganathan; Jonas Sicking. File API. 20 October 2011. W3C Working Draft. (Work in progress.) URL: http://www.w3.org/TR/2011/WD-FileAPI-20111020/
[GETUSERMEDIA]
D. Burnett, A. Narayanan. getusermedia: Getting access to local devices that can generate multimedia streams 22 December 2011. W3C Editors draft (Work in progress.) URL: http://dev.w3.org/2011/webrtc/editor/getusermedia.html