Effect.java revision 6ea92ecabbb53a2997eb5835c11945fecc177b91
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        if (startTimeMs + durationMs > mediaItem.getDuration()) {
61            throw new IllegalArgumentException("Invalid start time and duration");
62        }
63
64        mMediaItem = mediaItem;
65        mUniqueId = effectId;
66        mStartTimeMs = startTimeMs;
67        mDurationMs = durationMs;
68    }
69
70    /**
71     * @return The id of the effect
72     */
73    public String getId() {
74        return mUniqueId;
75    }
76
77    /**
78     * Set the duration of the effect. If a preview or export is in progress,
79     * then this change is effective for next preview or export session. s
80     *
81     * @param durationMs of the effect in milliseconds
82     */
83    public void setDuration(long durationMs) {
84        if (mStartTimeMs + durationMs > mMediaItem.getDuration()) {
85            throw new IllegalArgumentException("Duration is too large");
86        }
87
88        mDurationMs = durationMs;
89
90        mMediaItem.invalidateTransitions(this);
91    }
92
93    /**
94     * Get the duration of the effect
95     *
96     * @return The duration of the effect in milliseconds
97     */
98    public long getDuration() {
99        return mDurationMs;
100    }
101
102    /**
103     * Set start time of the effect. If a preview or export is in progress, then
104     * this change is effective for next preview or export session.
105     *
106     * @param startTimeMs The start time of the effect relative to the beginning
107     *            of the media item in milliseconds
108     */
109    public void setStartTime(long startTimeMs) {
110        if (startTimeMs + mDurationMs > mMediaItem.getDuration()) {
111            throw new IllegalArgumentException("Start time is too large");
112        }
113
114        mStartTimeMs = startTimeMs;
115
116        mMediaItem.invalidateTransitions(this);
117    }
118
119    /**
120     * @return The start time in milliseconds
121     */
122    public long getStartTime() {
123        return mStartTimeMs;
124    }
125
126    /**
127     * Set the start time and duration
128     *
129     * @param startTimeMs start time in milliseconds
130     * @param durationMs The duration in milliseconds
131     */
132    public void setStartTimeAndDuration(long startTimeMs, long durationMs) {
133        if (startTimeMs + durationMs > mMediaItem.getDuration()) {
134            throw new IllegalArgumentException("Invalid start time or duration");
135        }
136
137        mStartTimeMs = startTimeMs;
138        mDurationMs = durationMs;
139
140        mMediaItem.invalidateTransitions(this);
141    }
142
143    /**
144     * @return The media item owner
145     */
146    public MediaItem getMediaItem() {
147        return mMediaItem;
148    }
149
150    /*
151     * {@inheritDoc}
152     */
153    @Override
154    public boolean equals(Object object) {
155        if (!(object instanceof Effect)) {
156            return false;
157        }
158        return mUniqueId.equals(((Effect)object).mUniqueId);
159    }
160
161    /*
162     * {@inheritDoc}
163     */
164    @Override
165    public int hashCode() {
166        return mUniqueId.hashCode();
167    }
168}
169