CamcorderProfile.java revision f7badff8bbcbd45939c2f60cfcc88c6d3b07c6b2
1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.media;
18
19import android.hardware.Camera;
20import android.hardware.Camera.CameraInfo;
21
22/**
23 * Retrieves the
24 * predefined camcorder profile settings for camcorder applications.
25 * These settings are read-only.
26 *
27 * <p>The compressed output from a recording session with a given
28 * CamcorderProfile contains two tracks: one for audio and one for video.
29 *
30 * <p>Each profile specifies the following set of parameters:
31 * <ul>
32 * <li> The file output format
33 * <li> Video codec format
34 * <li> Video bit rate in bits per second
35 * <li> Video frame rate in frames per second
36 * <li> Video frame width and height,
37 * <li> Audio codec format
38 * <li> Audio bit rate in bits per second,
39 * <li> Audio sample rate
40 * <li> Number of audio channels for recording.
41 * </ul>
42 */
43public class CamcorderProfile
44{
45    // Do not change these values/ordinals without updating their counterpart
46    // in include/media/MediaProfiles.h!
47
48    /**
49     * Quality level corresponding to the lowest available resolution.
50     */
51    public static final int QUALITY_LOW  = 0;
52
53    /**
54     * Quality level corresponding to the highest available resolution.
55     */
56    public static final int QUALITY_HIGH = 1;
57
58    /**
59     * Quality level corresponding to the qcif (176 x 144) resolution.
60     */
61    public static final int QUALITY_QCIF = 2;
62
63    /**
64     * Quality level corresponding to the cif (352 x 288) resolution.
65     */
66    public static final int QUALITY_CIF = 3;
67
68    /**
69     * Quality level corresponding to the 480p (720 x 480) resolution.
70     * Note that the horizontal resolution for 480p can also be other
71     * values, such as 640 or 704, instead of 720.
72     */
73    public static final int QUALITY_480P = 4;
74
75    /**
76     * Quality level corresponding to the 720p (1280 x 720) resolution.
77     */
78    public static final int QUALITY_720P = 5;
79
80    /**
81     * Quality level corresponding to the 1080p (1920 x 1080) resolution.
82     * Note that the vertical resolution for 1080p can also be 1088,
83     * instead of 1080 (used by some vendors to avoid cropping during
84     * video playback).
85     */
86    public static final int QUALITY_1080P = 6;
87
88    /**
89     * Quality level corresponding to the QVGA (320x240) resolution.
90     */
91    public static final int QUALITY_QVGA = 7;
92
93    /**
94     * Quality level corresponding to the 2160p (3840x2160) resolution.
95     */
96    public static final int QUALITY_2160P = 8;
97
98    // Start and end of quality list
99    private static final int QUALITY_LIST_START = QUALITY_LOW;
100    private static final int QUALITY_LIST_END = QUALITY_2160P;
101
102    /**
103     * Time lapse quality level corresponding to the lowest available resolution.
104     */
105    public static final int QUALITY_TIME_LAPSE_LOW  = 1000;
106
107    /**
108     * Time lapse quality level corresponding to the highest available resolution.
109     */
110    public static final int QUALITY_TIME_LAPSE_HIGH = 1001;
111
112    /**
113     * Time lapse quality level corresponding to the qcif (176 x 144) resolution.
114     */
115    public static final int QUALITY_TIME_LAPSE_QCIF = 1002;
116
117    /**
118     * Time lapse quality level corresponding to the cif (352 x 288) resolution.
119     */
120    public static final int QUALITY_TIME_LAPSE_CIF = 1003;
121
122    /**
123     * Time lapse quality level corresponding to the 480p (720 x 480) resolution.
124     */
125    public static final int QUALITY_TIME_LAPSE_480P = 1004;
126
127    /**
128     * Time lapse quality level corresponding to the 720p (1280 x 720) resolution.
129     */
130    public static final int QUALITY_TIME_LAPSE_720P = 1005;
131
132    /**
133     * Time lapse quality level corresponding to the 1080p (1920 x 1088) resolution.
134     */
135    public static final int QUALITY_TIME_LAPSE_1080P = 1006;
136
137    /**
138     * Time lapse quality level corresponding to the QVGA (320 x 240) resolution.
139     */
140    public static final int QUALITY_TIME_LAPSE_QVGA = 1007;
141
142    /**
143     * Time lapse quality level corresponding to the 2160p (3840 x 2160) resolution.
144     */
145    public static final int QUALITY_TIME_LAPSE_2160P = 1008;
146
147    // Start and end of timelapse quality list
148    private static final int QUALITY_TIME_LAPSE_LIST_START = QUALITY_TIME_LAPSE_LOW;
149    private static final int QUALITY_TIME_LAPSE_LIST_END = QUALITY_TIME_LAPSE_2160P;
150
151    /**
152     * High speed ( >= 100fps) quality level corresponding to the lowest available resolution.
153     */
154    public static final int QUALITY_HIGH_SPEED_LOW = 2000;
155
156    /**
157     * High speed ( >= 100fps) quality level corresponding to the highest available resolution.
158     */
159    public static final int QUALITY_HIGH_SPEED_HIGH = 2001;
160
161    /**
162     * High speed ( >= 100fps) quality level corresponding to the 480p (720 x 480) resolution.
163     *
164     * Note that the horizontal resolution for 480p can also be other
165     * values, such as 640 or 704, instead of 720.
166     */
167    public static final int QUALITY_HIGH_SPEED_480P = 2002;
168
169    /**
170     * High speed ( >= 100fps) quality level corresponding to the 720p (1280 x 720) resolution.
171     */
172    public static final int QUALITY_HIGH_SPEED_720P = 2003;
173
174    /**
175     * High speed ( >= 100fps) quality level corresponding to the 1080p (1920 x 1080 or 1920x1088)
176     * resolution.
177     */
178    public static final int QUALITY_HIGH_SPEED_1080P = 2004;
179
180    // Start and end of high speed quality list
181    private static final int QUALITY_HIGH_SPEED_LIST_START = QUALITY_HIGH_SPEED_LOW;
182    private static final int QUALITY_HIGH_SPEED_LIST_END = QUALITY_HIGH_SPEED_1080P;
183
184    /**
185     * Default recording duration in seconds before the session is terminated.
186     * This is useful for applications like MMS has limited file size requirement.
187     */
188    public int duration;
189
190    /**
191     * The quality level of the camcorder profile
192     */
193    public int quality;
194
195    /**
196     * The file output format of the camcorder profile
197     * @see android.media.MediaRecorder.OutputFormat
198     */
199    public int fileFormat;
200
201    /**
202     * The video encoder being used for the video track
203     * @see android.media.MediaRecorder.VideoEncoder
204     */
205    public int videoCodec;
206
207    /**
208     * The target video output bit rate in bits per second
209     */
210    public int videoBitRate;
211
212    /**
213     * The target video frame rate in frames per second
214     */
215    public int videoFrameRate;
216
217    /**
218     * The target video frame width in pixels
219     */
220    public int videoFrameWidth;
221
222    /**
223     * The target video frame height in pixels
224     */
225    public int videoFrameHeight;
226
227    /**
228     * The audio encoder being used for the audio track.
229     * @see android.media.MediaRecorder.AudioEncoder
230     */
231    public int audioCodec;
232
233    /**
234     * The target audio output bit rate in bits per second
235     */
236    public int audioBitRate;
237
238    /**
239     * The audio sampling rate used for the audio track
240     */
241    public int audioSampleRate;
242
243    /**
244     * The number of audio channels used for the audio track
245     */
246    public int audioChannels;
247
248    /**
249     * Returns the camcorder profile for the first back-facing camera on the
250     * device at the given quality level. If the device has no back-facing
251     * camera, this returns null.
252     * @param quality the target quality level for the camcorder profile
253     * @see #get(int, int)
254     */
255    public static CamcorderProfile get(int quality) {
256        int numberOfCameras = Camera.getNumberOfCameras();
257        CameraInfo cameraInfo = new CameraInfo();
258        for (int i = 0; i < numberOfCameras; i++) {
259            Camera.getCameraInfo(i, cameraInfo);
260            if (cameraInfo.facing == CameraInfo.CAMERA_FACING_BACK) {
261                return get(i, quality);
262            }
263        }
264        return null;
265    }
266
267    /**
268     * Returns the camcorder profile for the given camera at the given
269     * quality level.
270     *
271     * Quality levels QUALITY_LOW, QUALITY_HIGH are guaranteed to be supported, while
272     * other levels may or may not be supported. The supported levels can be checked using
273     * {@link #hasProfile(int, int)}.
274     * QUALITY_LOW refers to the lowest quality available, while QUALITY_HIGH refers to
275     * the highest quality available.
276     * QUALITY_LOW/QUALITY_HIGH have to match one of qcif, cif, 480p, 720p, 1080p or 2160p.
277     * E.g. if the device supports 480p, 720p, 1080p and 2160p, then low is 480p and high is
278     * 2160p.
279     *
280     * The same is true for time lapse quality levels, i.e. QUALITY_TIME_LAPSE_LOW,
281     * QUALITY_TIME_LAPSE_HIGH are guaranteed to be supported and have to match one of
282     * qcif, cif, 480p, 720p, 1080p, or 2160p.
283     *
284     * For high speed quality levels, they may or may not be supported. If a subset of the levels
285     * are supported, QUALITY_HIGH_SPEED_LOW and QUALITY_HIGH_SPEED_HIGH are guaranteed to be
286     * supported and have to match one of 480p, 720p, or 1080p.
287     *
288     * A camcorder recording session with higher quality level usually has higher output
289     * bit rate, better video and/or audio recording quality, larger video frame
290     * resolution and higher audio sampling rate, etc, than those with lower quality
291     * level.
292     *
293     * @param cameraId the id for the camera
294     * @param quality the target quality level for the camcorder profile.
295     * @see #QUALITY_LOW
296     * @see #QUALITY_HIGH
297     * @see #QUALITY_QCIF
298     * @see #QUALITY_CIF
299     * @see #QUALITY_480P
300     * @see #QUALITY_720P
301     * @see #QUALITY_1080P
302     * @see #QUALITY_2160P
303     * @see #QUALITY_TIME_LAPSE_LOW
304     * @see #QUALITY_TIME_LAPSE_HIGH
305     * @see #QUALITY_TIME_LAPSE_QCIF
306     * @see #QUALITY_TIME_LAPSE_CIF
307     * @see #QUALITY_TIME_LAPSE_480P
308     * @see #QUALITY_TIME_LAPSE_720P
309     * @see #QUALITY_TIME_LAPSE_1080P
310     * @see #QUALITY_TIME_LAPSE_2160P
311     * @see #QUALITY_HIGH_SPEED_LOW
312     * @see #QUALITY_HIGH_SPEED_HIGH
313     * @see #QUALITY_HIGH_SPEED_480P
314     * @see #QUALITY_HIGH_SPEED_720P
315     * @see #QUALITY_HIGH_SPEED_1080P
316    */
317    public static CamcorderProfile get(int cameraId, int quality) {
318        if (!((quality >= QUALITY_LIST_START &&
319               quality <= QUALITY_LIST_END) ||
320              (quality >= QUALITY_TIME_LAPSE_LIST_START &&
321               quality <= QUALITY_TIME_LAPSE_LIST_END) ||
322               (quality >= QUALITY_HIGH_SPEED_LIST_START &&
323               quality <= QUALITY_HIGH_SPEED_LIST_END))) {
324            String errMessage = "Unsupported quality level: " + quality;
325            throw new IllegalArgumentException(errMessage);
326        }
327        return native_get_camcorder_profile(cameraId, quality);
328    }
329
330    /**
331     * Returns true if camcorder profile exists for the first back-facing
332     * camera at the given quality level.
333     * @param quality the target quality level for the camcorder profile
334     */
335    public static boolean hasProfile(int quality) {
336        int numberOfCameras = Camera.getNumberOfCameras();
337        CameraInfo cameraInfo = new CameraInfo();
338        for (int i = 0; i < numberOfCameras; i++) {
339            Camera.getCameraInfo(i, cameraInfo);
340            if (cameraInfo.facing == CameraInfo.CAMERA_FACING_BACK) {
341                return hasProfile(i, quality);
342            }
343        }
344        return false;
345    }
346
347    /**
348     * Returns true if camcorder profile exists for the given camera at
349     * the given quality level.
350     * @param cameraId the id for the camera
351     * @param quality the target quality level for the camcorder profile
352     */
353    public static boolean hasProfile(int cameraId, int quality) {
354        return native_has_camcorder_profile(cameraId, quality);
355    }
356
357    static {
358        System.loadLibrary("media_jni");
359        native_init();
360    }
361
362    // Private constructor called by JNI
363    private CamcorderProfile(int duration,
364                             int quality,
365                             int fileFormat,
366                             int videoCodec,
367                             int videoBitRate,
368                             int videoFrameRate,
369                             int videoWidth,
370                             int videoHeight,
371                             int audioCodec,
372                             int audioBitRate,
373                             int audioSampleRate,
374                             int audioChannels) {
375
376        this.duration         = duration;
377        this.quality          = quality;
378        this.fileFormat       = fileFormat;
379        this.videoCodec       = videoCodec;
380        this.videoBitRate     = videoBitRate;
381        this.videoFrameRate   = videoFrameRate;
382        this.videoFrameWidth  = videoWidth;
383        this.videoFrameHeight = videoHeight;
384        this.audioCodec       = audioCodec;
385        this.audioBitRate     = audioBitRate;
386        this.audioSampleRate  = audioSampleRate;
387        this.audioChannels    = audioChannels;
388    }
389
390    // Methods implemented by JNI
391    private static native final void native_init();
392    private static native final CamcorderProfile native_get_camcorder_profile(
393            int cameraId, int quality);
394    private static native final boolean native_has_camcorder_profile(
395            int cameraId, int quality);
396}
397