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
17
18package android.media.videoeditor;
19
20/**
21 * This is the super class for all effects. An effect can only be applied to a
22 * single media item.
23 * {@hide}
24 */
25public abstract class Effect {
26    /**
27     *  Instance variables
28     */
29    private final String mUniqueId;
30    /**
31     *  The effect owner
32     */
33    private final MediaItem mMediaItem;
34
35    protected long mDurationMs;
36    /**
37     *  The start time of the effect relative to the beginning
38     *  of the media item
39     */
40    protected long mStartTimeMs;
41
42    /**
43     * Default constructor
44     */
45    @SuppressWarnings("unused")
46    private Effect() {
47        mMediaItem = null;
48        mUniqueId = null;
49        mStartTimeMs = 0;
50        mDurationMs = 0;
51    }
52
53    /**
54     * Constructor
55     *
56     * @param mediaItem The media item owner
57     * @param effectId The effect id
58     * @param startTimeMs The start time relative to the media item to which it
59     *            is applied
60     * @param durationMs The effect duration in milliseconds
61     */
62    public Effect(MediaItem mediaItem, String effectId, long startTimeMs,
63                  long durationMs) {
64        if (mediaItem == null) {
65            throw new IllegalArgumentException("Media item cannot be null");
66        }
67
68        if ((startTimeMs < 0) || (durationMs < 0)) {
69             throw new IllegalArgumentException("Invalid start time Or/And Duration");
70        }
71        if (startTimeMs + durationMs > mediaItem.getDuration()) {
72            throw new IllegalArgumentException("Invalid start time and duration");
73        }
74
75        mMediaItem = mediaItem;
76        mUniqueId = effectId;
77        mStartTimeMs = startTimeMs;
78        mDurationMs = durationMs;
79    }
80
81    /**
82     * Get the id of the effect.
83     *
84     * @return The id of the effect
85     */
86    public String getId() {
87        return mUniqueId;
88    }
89
90    /**
91     * Set the duration of the effect. If a preview or export is in progress,
92     * then this change is effective for next preview or export session.
93     *
94     * @param durationMs of the effect in milliseconds
95     */
96    public void setDuration(long durationMs) {
97        if (durationMs <0) {
98            throw new IllegalArgumentException("Invalid duration");
99        }
100
101        if (mStartTimeMs + durationMs > mMediaItem.getDuration()) {
102            throw new IllegalArgumentException("Duration is too large");
103        }
104
105        getMediaItem().getNativeContext().setGeneratePreview(true);
106
107        final long oldDurationMs = mDurationMs;
108        mDurationMs = durationMs;
109
110        mMediaItem.invalidateTransitions(mStartTimeMs, oldDurationMs, mStartTimeMs, mDurationMs);
111    }
112
113    /**
114     * Get the duration of the effect
115     *
116     * @return The duration of the effect in milliseconds
117     */
118    public long getDuration() {
119        return mDurationMs;
120    }
121
122    /**
123     * Set start time of the effect. If a preview or export is in progress, then
124     * this change is effective for next preview or export session.
125     *
126     * @param startTimeMs The start time of the effect relative to the beginning
127     *            of the media item in milliseconds
128     */
129    public void setStartTime(long startTimeMs) {
130        if (startTimeMs + mDurationMs > mMediaItem.getDuration()) {
131            throw new IllegalArgumentException("Start time is too large");
132        }
133
134        getMediaItem().getNativeContext().setGeneratePreview(true);
135        final long oldStartTimeMs = mStartTimeMs;
136        mStartTimeMs = startTimeMs;
137
138        mMediaItem.invalidateTransitions(oldStartTimeMs, mDurationMs, mStartTimeMs, mDurationMs);
139    }
140
141    /**
142     * Get the start time of the effect
143     *
144     * @return The start time in milliseconds
145     */
146    public long getStartTime() {
147        return mStartTimeMs;
148    }
149
150    /**
151     * Set the start time and duration
152     *
153     * @param startTimeMs start time in milliseconds
154     * @param durationMs The duration in milliseconds
155     */
156    public void setStartTimeAndDuration(long startTimeMs, long durationMs) {
157        if (startTimeMs + durationMs > mMediaItem.getDuration()) {
158            throw new IllegalArgumentException("Invalid start time or duration");
159        }
160
161        getMediaItem().getNativeContext().setGeneratePreview(true);
162        final long oldStartTimeMs = mStartTimeMs;
163        final long oldDurationMs = mDurationMs;
164
165        mStartTimeMs = startTimeMs;
166        mDurationMs = durationMs;
167
168        mMediaItem.invalidateTransitions(oldStartTimeMs, oldDurationMs, mStartTimeMs, mDurationMs);
169    }
170
171    /**
172     * Get the media item owner.
173     *
174     * @return The media item owner
175     */
176    public MediaItem getMediaItem() {
177        return mMediaItem;
178    }
179
180    /*
181     * {@inheritDoc}
182     */
183    @Override
184    public boolean equals(Object object) {
185        if (!(object instanceof Effect)) {
186            return false;
187        }
188        return mUniqueId.equals(((Effect)object).mUniqueId);
189    }
190
191    /*
192     * {@inheritDoc}
193     */
194    @Override
195    public int hashCode() {
196        return mUniqueId.hashCode();
197    }
198}
199