Effect.java revision 9bcedf7cf3e9c981837f2d8ec98cd118efad3f01
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 final long oldDurationMs = mDurationMs; 106 mDurationMs = durationMs; 107 108 mMediaItem.invalidateTransitions(mStartTimeMs, oldDurationMs, 109 mStartTimeMs, mDurationMs); 110 } 111 112 /** 113 * Get the duration of the effect 114 * 115 * @return The duration of the effect in milliseconds 116 */ 117 public long getDuration() { 118 return mDurationMs; 119 } 120 121 /** 122 * Set start time of the effect. If a preview or export is in progress, then 123 * this change is effective for next preview or export session. 124 * 125 * @param startTimeMs The start time of the effect relative to the beginning 126 * of the media item in milliseconds 127 */ 128 public void setStartTime(long startTimeMs) { 129 if (startTimeMs + mDurationMs > mMediaItem.getDuration()) { 130 throw new IllegalArgumentException("Start time is too large"); 131 } 132 133 final long oldStartTimeMs = mStartTimeMs; 134 mStartTimeMs = startTimeMs; 135 136 mMediaItem.invalidateTransitions(oldStartTimeMs, mDurationMs, 137 mStartTimeMs, mDurationMs); 138 } 139 140 /** 141 * Get the start time of the effect 142 * 143 * @return The start time in milliseconds 144 */ 145 public long getStartTime() { 146 return mStartTimeMs; 147 } 148 149 /** 150 * Set the start time and duration 151 * 152 * @param startTimeMs start time in milliseconds 153 * @param durationMs The duration in milliseconds 154 */ 155 public void setStartTimeAndDuration(long startTimeMs, long durationMs) { 156 if (startTimeMs + durationMs > mMediaItem.getDuration()) { 157 throw new IllegalArgumentException("Invalid start time or duration"); 158 } 159 160 final long oldStartTimeMs = mStartTimeMs; 161 final long oldDurationMs = mDurationMs; 162 163 mStartTimeMs = startTimeMs; 164 mDurationMs = durationMs; 165 166 mMediaItem.invalidateTransitions(oldStartTimeMs, oldDurationMs, 167 mStartTimeMs, mDurationMs); 168 } 169 170 /** 171 * Get the media item owner. 172 * 173 * @return The media item owner 174 */ 175 public MediaItem getMediaItem() { 176 return mMediaItem; 177 } 178 179 /* 180 * {@inheritDoc} 181 */ 182 @Override 183 public boolean equals(Object object) { 184 if (!(object instanceof Effect)) { 185 return false; 186 } 187 return mUniqueId.equals(((Effect)object).mUniqueId); 188 } 189 190 /* 191 * {@inheritDoc} 192 */ 193 @Override 194 public int hashCode() { 195 return mUniqueId.hashCode(); 196 } 197} 198