Effect.java revision f8b04868e6fa1f7ca9c1fe3f39ae1f46a530b6df
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.videoeditor;
18
19/**
20 * This is the super class for all effects. An effect can only be applied to a
21 * single media item. If one wants to apply the same effect to multiple media
22 * items, multiple @{MediaItem.addEffect(Effect)} call must be invoked on each
23 * of the MediaItem objects.
24 * {@hide}
25 */
26public abstract class Effect {
27    // Instance variables
28    private final String mUniqueId;
29    // The effect owner
30    private final MediaItem mMediaItem;
31    protected long mDurationMs;
32    // The start time of the effect relative to the media item timeline
33    protected long mStartTimeMs;
34
35    /**
36     * Default constructor
37     */
38    @SuppressWarnings("unused")
39    private Effect() {
40        mMediaItem = null;
41        mUniqueId = null;
42        mStartTimeMs = 0;
43        mDurationMs = 0;
44    }
45
46    /**
47     * Constructor
48     *
49     * @param mediaItem The media item owner
50     * @param effectId The effect id
51     * @param startTimeMs The start time relative to the media item to which it
52     *            is applied
53     * @param durationMs The effect duration in milliseconds
54     */
55    public Effect(MediaItem mediaItem, String effectId, long startTimeMs, long durationMs) {
56        if (mediaItem == null) {
57            throw new IllegalArgumentException("Media item cannot be null");
58        }
59
60        mMediaItem = mediaItem;
61        mUniqueId = effectId;
62        mStartTimeMs = startTimeMs;
63        mDurationMs = durationMs;
64    }
65
66    /**
67     * @return The id of the effect
68     */
69    public String getId() {
70        return mUniqueId;
71    }
72
73    /**
74     * Set the duration of the effect. If a preview or export is in progress,
75     * then this change is effective for next preview or export session. s
76     *
77     * @param durationMs of the effect in milliseconds
78     */
79    public void setDuration(long durationMs) {
80        if (mStartTimeMs + durationMs > mMediaItem.getTimelineDuration()) {
81            throw new IllegalArgumentException("Duration is too large");
82        }
83
84        mDurationMs = durationMs;
85
86        mMediaItem.invalidateTransitions(this);
87    }
88
89    /**
90     * Get the duration of the effect
91     *
92     * @return The duration of the effect in milliseconds
93     */
94    public long getDuration() {
95        return mDurationMs;
96    }
97
98    /**
99     * Set start time of the effect. If a preview or export is in progress, then
100     * this change is effective for next preview or export session.
101     *
102     * @param startTimeMs The start time of the effect relative to the beginning
103     *            of the media item in milliseconds
104     */
105    public void setStartTime(long startTimeMs) {
106        if (startTimeMs + mDurationMs > mMediaItem.getTimelineDuration()) {
107            throw new IllegalArgumentException("Start time is too large");
108        }
109
110        mStartTimeMs = startTimeMs;
111
112        mMediaItem.invalidateTransitions(this);
113    }
114
115    /**
116     * @return The start time in milliseconds
117     */
118    public long getStartTime() {
119        return mStartTimeMs;
120    }
121
122    /**
123     * @return The media item owner
124     */
125    public MediaItem getMediaItem() {
126        return mMediaItem;
127    }
128
129    /*
130     * {@inheritDoc}
131     */
132    @Override
133    public boolean equals(Object object) {
134        if (!(object instanceof Effect)) {
135            return false;
136        }
137        return mUniqueId.equals(((Effect)object).mUniqueId);
138    }
139
140    /*
141     * {@inheritDoc}
142     */
143    @Override
144    public int hashCode() {
145        return mUniqueId.hashCode();
146    }
147}
148