W3C

The Capture API

W3C Working Draft 01 April 2010

This Version:
http://www.w3.org/TR/2010/WD-capture-api-20100401/
Latest Published Version:
http://www.w3.org/TR/capture-api/
Latest Editor's Draft:
http://dev.w3.org/2009/dap/camera/
Editors:
Dzung D Tran, Intel
Ilkka Oksanen, Nokia
Ingmar Kliche, Deutsche Telekom

Abstract

This specification defines an Application Programming Interface (API) that provides access to the audio, image and video capture capabilities of the device.

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/.

This document represents the early consensus of the group on the scope and features of the proposed Capture API. Issues and editors notes in the document highlight some of the points on which the group is still working and would particularly like to get feedback.

This document was published by the Device APIs and Policy Working Group as a First Public Working Draft. This document is intended to become a W3C Recommendation. If you wish to make comments regarding this document, please send them to public-device-apis@w3.org (subscribe, archives). All feedback is welcome.

Publication as a Working 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 Capture API defines a high-level interface for accessing the microphone and camera of a hosting device.

1.1 Usage Examples

The following code extracts illustrate how to work with a camera service in the hosting device:

Launching a device camera application and retrieving the pictures taken.

function success(data) {
  var container = document.createElement("div");
  document.body.appendChild(container);
  
  for (var i in data) {
    var img = document.createElement("img");
    img.src = data[i].uri;
    container.appendChild(img);
  }
}
 
function error(err) {
  alert(err.message + " (" + err.code + ")"); 
}
 
navigator.device.captureImage(success, error, { maxNumberOfMediaFiles: 1 });

Example of retrieving image sizes and formats supported by hosting device camera.

var summary,

formats = navigator.device.supportedImageFormats;

for (var key in formats) {
  summary += key + ": " + formats[key] + "\n";
}

alert(summary);

2. Security and Privacy Considerations

This section is under development.

The API defined in this specification launches the capture application which allows the user to take pictures, record voice or record video and provides a handle to the content. This information can potentially compromise user privacy and a conforming implementation of this specification must provide a mechanism that protects the user's privacy and this mechanism should ensure that such operations must be authenticated.

Privacy considerations for implementers of Capture API

A conforming implementation of this specification must provide a mechanism that protects the user's privacy and this mechanism should ensure that privacy information is not revealed without user's informed consent.

3. API Description

3.1 Capture interface

The Capture interface exposes an interface to the camera and microphone of the hosting device.

Objects implementing the NavigatorDevice interface (e.g. the window.navigator.device object in Web browsers [NAVIGATOR]) provide access to the Capture interface through the Capture interface. An instance of Capture would be then obtained by using binding-specific casting methods on an instance of NavigatorDevice.

[Supplemental, NoInterfaceObject]
interface Capture {
    readonly attribute FormatData[] supportedImageFormats;
    readonly attribute FormatData[] supportedVideoFormats;
    readonly attribute FormatData[] supportedAudioFormats;
    PendingOperation captureImage (in CaptureCB successCB, in optional CaptureErrorCB? errorCB, in optional CaptureImageOptions options);
    PendingOperation captureVideo (in CaptureCB successCB, in optional CaptureErrorCB? errorCB, in optional CaptureVideoOptions options);
    PendingOperation captureAudio (in CaptureCB successCB, in optional CaptureErrorCB? errorCB, in optional CaptureAudioOptions options);
    PendingOperation getViewFinder (in ViewFinderCB successCB, in optional ViewFinderErrorCB? errorCB);
};

3.1.1 Attributes

supportedAudioFormats of type array of FormatData, readonly
An array of FormatData objects which contains audio formats supported by the hosting device microphone.
No exceptions.
supportedImageFormats of type array of FormatData, readonly
An array of FormatData objects which contains image sizes and formats supported by the hosting device camera.
No exceptions.
supportedVideoFormats of type array of FormatData, readonly
An array of FormatData objects which contains video resolutions and formats supported by the hosting device camera.
No exceptions.

3.1.2 Methods

captureAudio

Launch device native audio recorder application for recording audio clip(s).

This method takes two or three arguments. When called, it immediately returns a PendingOperation object and then asynchronously start a capture audio process defined as follows:

  1. Start native audio recorder application. Allow end user to record audio clip(s) and return.
  2. If successful, invoke the associated successCB with a MediaArray argument. If the attempt fails, and the method was invoked with a non-null errorCallback argument, this method must invoke the errorCallback with a CaptureError object as an argument.
  3. If a CaptureAudioOptions parameter was present, and its maxNumberOfMediaFiles attribute was defined, successCB must be invoked after the user has captured a number of audio clips defined in maxNumberOfMediaFiles. If user exited the native camera application prematurely, errorCB must be invoked.

ParameterTypeNullableOptionalDescription
successCBCaptureCBFunction to call when the asynchronous operation completes
errorCBCaptureErrorCBFunction to call when the asynchronous operation fails. This parameter is optional.
optionsCaptureAudioOptionsAudio capture options. This parameter is optional.
No exceptions.
Return type: PendingOperation
captureImage

Launch device native camera application for taking image(s).

This method takes two or three arguments. When called, it immediately returns a PendingOperation object and then asynchronously start a capture image process defined as follows:

  1. Start native camera application. Allow end user to take picture(s).
  2. If successful, invoke the associated successCB with a MediaArray argument. If the attempt fails, and the method was invoked with a non-null errorCallback argument, this method must invoke the errorCallback with a CaptureError object as an argument.
  3. If a CaptureImageOptions parameter was present, and its maxNumberOfMediaFiles attribute was defined, successCB must be invoked after the user has captured a number of images defined in maxNumberOfMediaFiles. If user exited the native camera application prematurely, errorCB must be invoked.

ParameterTypeNullableOptionalDescription
successCBCaptureCBFunction to call when the asynchronous operation completes
errorCBCaptureErrorCBFunction to call when the asynchronous operation fails. This parameter is optional.
optionsCaptureImageOptionsImage capture options. This parameter is optional.
No exceptions.
Return type: PendingOperation
captureVideo

Launch device native camera application for recording video(s).

This method takes three or four arguments. When called, it immediately returns a PendingOperation object and then asynchronously start a capture video process defined as follows:

  1. Start native video camera application. Allow end user to take video(s) and return.
  2. If successful, invoke the associated successCB with a MediaArray argument. If the attempt fails, and the method was invoked with a non-null errorCallback argument, this method must invoke the errorCallback with a CaptureError object as an argument.
  3. If a CaptureVideoOptions parameter was present, and its maxNumberOfMediaFiles attribute was defined, successCB must be invoked after the user has captured a number of video clips defined in maxNumberOfMediaFiles. If user exited the native camera application prematurely, errorCB must be invoked.
  4. If a CaptureVideoOptions parameter was present, and its duration attribute was defined, successCB must be invoked after the video has been captured for the number of milliseconds defined in duration. If user exited the native camera application prematurely, errorCB must be invoked.

ParameterTypeNullableOptionalDescription
successCBCaptureCBFunction to call when the asynchronous operation completes
errorCBCaptureErrorCBFunction to call when the asynchronous operation fails. This parameter is optional.
optionsCaptureVideoOptionsImage capture options. This parameter is optional.
No exceptions.
Return type: PendingOperation
getViewFinder

This method takes one or two arguments. When called, it immediately returns a PendingOperation object and then tries asynchronously to return the ViewFinder object of the active capture device selected by the system or user.

User consent must be verified.

If successful, invokes the associated ViewFinderCB with a ViewFinder argument. If the attempt fails, and the method was invoked with a non-null errorCallback argument, this method must invoke the errorCB with a CaptureError object as an argument.

ParameterTypeNullableOptionalDescription
successCBViewFinderCBFunction to call when the asynchronous operation completes
errorCBViewFinderErrorCBFunction to call when the asynchronous operation fails. This parameter is optional.
No exceptions.
Return type: PendingOperation

3.2 MediaFile interface

MediaFile captures a single photo, video or sound captured by the device native capture application. It inherits from File [FILE-API]

[NoInterfaceObject]
interface MediaFile : File {
    attribute FormatData format;
};

3.2.1 Attributes

format of type FormatData
The format attribute represents the format information of MediaFile object.
No exceptions.

3.3 FormatData interface

FormatData captures format information of a media file captured by the device native capture application.

[NoInterfaceObject]
interface FormatData {
    attribute DOMString     type;
    attribute unsigned long height;
    attribute double        duration;
    attribute unsigned long width;
};

3.3.1 Attributes

duration of type double
The duration attribute represents length of the video or sound clip in milliseconds. In the case of a image this attribute has value 0.
No exceptions.
height of type unsigned long
The height attribute represents height of the image or video in pixels. In the case of a sound clip this attribute has value 0.
No exceptions.
type of type DOMString
The type attribute represents the MIME type of the captured image, video or sound. For example, a valid MIME type for JPEG images is image/jpeg. A valid MIME type for WAV sound file is audio/x-wav.
No exceptions.
width of type unsigned long
The width attribute represents width of the image or video in pixels. In the case of a sound clip this attribute has value 0.
No exceptions.

3.4 CaptureCB interface

[Callback=FunctionOnly, NoInterfaceObject]
interface CaptureCB {
    void onSuccess (in MediaArray capturedMedia);
};

3.4.1 Methods

onSuccess
ParameterTypeNullableOptionalDescription
capturedMediaMediaArraySequence of MediaFile successfully captured by the device
No exceptions.
Return type: void

3.5 MediaArray typedef

The MediaArray typedef represents a sequence of MediaFile objects.

typedef sequence<MediaFile> MediaArray;
Throughout this specification, the identifier MediaArray is used to refer to the sequence<MediaFile> type.

3.6 CaptureErrorCB interface

[Callback=FunctionOnly, NoInterfaceObject]
interface CaptureErrorCB {
    void onError (in CaptureError error);
};

3.6.1 Methods

onError
ParameterTypeNullableOptionalDescription
errorCaptureErrorThe error object of an unsuccessful capture asynchronous operation.
No exceptions.
Return type: void

3.7 ViewFinderCB interface

[Callback=FunctionOnly, NoInterfaceObject]
interface ViewFinderCB {
    void onSuccess (in ViewFinder viewfinder);
};

3.7.1 Methods

onSuccess
ParameterTypeNullableOptionalDescription
viewfinderViewFinderThe ViewFinder object of the selected capture device.
No exceptions.
Return type: void

3.8 ViewFinderErrorCB interface

[Callback=FunctionOnly, NoInterfaceObject]
interface ViewFinderErrorCB {
    void onError (in CaptureError error);
};

3.8.1 Methods

onError
ParameterTypeNullableOptionalDescription
errorCaptureError The error object of an unsuccessful asynchronous viewfinder acquisition operation.
No exceptions.
Return type: void

3.9 CaptureError interface

The CaptureError interface encapsulates all errors in the Capture API.

More error codes to be defined here.

[NoInterfaceObject]
interface CaptureError {
    const unsigned short CAPTURE_INTERNAL_ERR = 0;
    readonly attribute unsigned short code;
};

3.9.1 Attributes

code of type unsigned short, readonly
An error code assigned by an implementation when an error has occurred in Capture API processing.
No exceptions.

3.9.2 Constants

CAPTURE_INTERNAL_ERR of type unsigned short
Camera or microphone failed to capture image or sound.

3.10 CaptureImageOptions interface

The CaptureImageOptions interface encapsulates all image capture operation configuration options.

Additional attributes proposed: width and height.

[NoInterfaceObject]
interface CaptureImageOptions {
    attribute unsigned long maxNumberOfMediaFiles;
};

3.10.1 Attributes

maxNumberOfMediaFiles of type unsigned long
Upper limit of images user can take.
No exceptions.

3.11 CaptureVideoOptions interface

[NoInterfaceObject]
interface CaptureVideoOptions {
    attribute unsigned long maxNumberOfMediaFiles;
    attribute double        duration;
};

3.11.1 Attributes

duration of type double
Maximum duration of a single video clip in milliseconds.
No exceptions.
maxNumberOfMediaFiles of type unsigned long
Upper limit of videos user can record.
No exceptions.

3.12 CaptureAudioOptions interface

Additional attributes proposed: duration.

[NoInterfaceObject]
interface CaptureAudioOptions {
    attribute unsigned long maxNumberOfMediaFiles;
};

3.12.1 Attributes

maxNumberOfMediaFiles of type unsigned long
Upper limit of sound clips user can record.
No exceptions.

3.13 PendingOperation interface

This may be a general interface for use throughout all APIs. Included here for now for completion.

[NoInterfaceObject]
interface PendingOperation {
    void cancel ();
};

3.13.1 Methods

cancel
Cancel/clear the pending asynchronous operation.
No parameters.
No exceptions.
Return type: void

3.14 ViewFinder interface

This interface represents a camera viewfinder. It inherits from File [FILE-API] and provides a method to embed a viewfinder to web content.

[NoInterfaceObject]
interface ViewFinder : File {
    void startCapture (in Blob data);
    void stopCapture ();
};

3.14.1 Methods

startCapture

Starts the capture process.

ParameterTypeNullableOptionalDescription
dataBlob The blob to write.
No exceptions.
Return type: void
stopCapture

Stops the capture process.

No parameters.
No exceptions.
Return type: void

4. Capture Input Selection

In addition to programmable API, content can be acquired from capture devices through a file-select control.

4.1 Capture aware file-select control

This section is non-normative.

If input element in the File Upload state [HTML5] contains accept attribute with values image/*, sound/*, or video/*, the user agent can invoke a file picker that allows respectively the user to take a picture, record a sound file, or record a video in addition to selecting an existing file from the file system.

See the User Interface Examples appendix for the illustration.

In case the user chooses to capture video, audio, or image content, the user agent creates media files on the fly as specified in [HTML5].

<input id="cameraInput" type="file" accept="image/*"> 

In trusted environments the click event of file input element can be synthesised as follows:

<input id="cameraInput" onchange="successCB();" type="file" accept="image/*"> 

<script>

function successCB() {
  // Read the image using file-reader or submit the form
}

document.getElementById('cameraInput').click();

</script>

4.2 ViewFinder aware file-select control

Conforming user agents must provide the user a file picker from which it is possible to select any of the available capture devices in addition to possibility to select one or more files from the file system.

If a user selects camera device, the FileList object that represents the currently selected files in the input element in the File Upload state must contain one ViewFinder object instead of File objects as in [FILE-API].

<script>

  function getVideo () {
    var inp = document.createElement("input");
    inp.type = "file";
    inp.accept = "video/*";
    inp.onchange = function () { setupVideo(this.files[0]); };
    // this is only possible in trusted environments
    // the click event cannot be synthetized by default
    inp.click();
  }

  var blob;

  function setupVideo (vf) {
    document.getElementById("my-video").src = vf.URL;
	var bb = new BlobBuilder();
    blob = bb.getBlob();
    document.getElementById("start").onclick = function () { vf.startCapture(bb.getBlob()); }
    document.getElementById("stop").onclick = function () { vf.stopCapture(); }
    document.getElementById("send").onclick = function () { send(); }
  }

  function send () {
    if (!blob) {
       alert("You didn't capture anything!")
       return;
    }
    var xhr = new XMLHttpRequest();
    xhr.open("GET", "http://my-server/video-upload");
    xhr.onreadystatechange = function () {
      if (xhr.readyState == 4 && xhr.status == 200) alert("Video uploaded!");
    }
    xhr.send(blob);
  }
</script>

<button onclick='getVideo();'>Film your face!</button>
<video id='my-video'></video>
<button id='start'>Start</button>
<button id='stop'>Stop</button>
<button id='send'>Upload</button>

B. User Interface Examples

Capture API aware file picker might render as:

A File picker with camera support

C. Features for Future Consideration

This is a list of features that have been discussed with respect to this version of the API but for which it has been decided that if they are included it will be in a future revision.

D. Acknowledgements

Many thanks to Google, Nokia, and OMTP BONDI who provided the initial input into this specification.

E. References

E.1 Normative references

No normative references.

E.2 Informative references

[FILE-API]
Arun Ranganathan. File API. 17 November 2009. W3C Working Draft. (Work in progress.) URL: http://www.w3.org/TR/2009/WD-FileAPI-20091117/
[HTML5]
Ian Hickson; David Hyatt. HTML 5. 25 August 2009. W3C Working Draft. (Work in progress.) URL: http://www.w3.org/TR/2009/WD-html5-20090825/
[NAVIGATOR]
Ian Hickson, David Hyatt. Navigator interface in HTML5, Editors draft (Work in progress). URL: http://dev.w3.org/html5/spec/webappapis.html#navigator