Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit e63f20b5 authored by Gil Dobjanschi's avatar Gil Dobjanschi Committed by Android (Google) Code Review
Browse files

Merge "Save overlays to XML"

parents 5553044c 5665fd6a
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -57,6 +57,10 @@ public abstract class Effect {
            throw new IllegalArgumentException("Media item cannot be null");
        }

        if (startTimeMs + durationMs > mediaItem.getTimelineDuration()) {
            throw new IllegalArgumentException("Invalid start time and duration");
        }

        mMediaItem = mediaItem;
        mUniqueId = effectId;
        mStartTimeMs = startTimeMs;
+28 −4
Original line number Diff line number Diff line
@@ -16,6 +16,9 @@

package android.media.videoeditor;

import java.util.HashMap;
import java.util.Map;


/**
 * This is the super class for all Overlay classes.
@@ -26,6 +29,8 @@ public abstract class Overlay {
    private final String mUniqueId;
    // The overlay owner
    private final MediaItem mMediaItem;
    // user attributes
    private final Map<String, String> mUserAttributes;

    protected long mStartTimeMs;
    protected long mDurationMs;
@@ -36,10 +41,7 @@ public abstract class Overlay {
     */
    @SuppressWarnings("unused")
    private Overlay() {
        mUniqueId = null;
        mMediaItem = null;
        mStartTimeMs = 0;
        mDurationMs = 0;
        this(null, null, 0, 0);
    }

    /**
@@ -58,10 +60,15 @@ public abstract class Overlay {
            throw new IllegalArgumentException("Media item cannot be null");
        }

        if (startTimeMs + durationMs > mediaItem.getTimelineDuration()) {
            throw new IllegalArgumentException("Invalid start time and duration");
        }

        mMediaItem = mediaItem;
        mUniqueId = overlayId;
        mStartTimeMs = startTimeMs;
        mDurationMs = durationMs;
        mUserAttributes = new HashMap<String, String>();
    }

    /**
@@ -125,6 +132,23 @@ public abstract class Overlay {
        return mMediaItem;
    }

    /**
     * Set a user attribute
     *
     * @param name The attribute name
     * @param value The attribute value
     */
    public void setUserAttribute(String name, String value) {
        mUserAttributes.put(name, value);
    }

    /**
     * @return The user attributes
     */
    public Map<String, String> getUserAttributes() {
        return mUserAttributes;
    }

    /*
     * {@inheritDoc}
     */
+17 −1
Original line number Diff line number Diff line
@@ -32,7 +32,7 @@ import android.graphics.Bitmap.CompressFormat;
 */
public class OverlayFrame extends Overlay {
    // Instance variables
    private final Bitmap mBitmap;
    private Bitmap mBitmap;
    private String mFilename;

    /**
@@ -92,6 +92,22 @@ public class OverlayFrame extends Overlay {
        return mBitmap;
    }

    /**
     * @param bitmap The overlay bitmap
     */
    public void setBitmap(Bitmap bitmap) {
        mBitmap = bitmap;
        if (mFilename != null) {
            // Delete the file
            new File(mFilename).delete();
            // Invalidate the filename
            mFilename = null;
        }

        // Invalidate the transitions if necessary
        getMediaItem().invalidateTransitions(this);
    }

    /**
     * Get the file name of this overlay
     */
+94 −4
Original line number Diff line number Diff line
@@ -25,6 +25,7 @@ import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
@@ -50,6 +51,10 @@ public class VideoEditorTestImpl implements VideoEditor {
    private static final String TAG_MEDIA_ITEM = "media_item";
    private static final String TAG_TRANSITIONS = "transitions";
    private static final String TAG_TRANSITION = "transition";
    private static final String TAG_OVERLAYS = "overlays";
    private static final String TAG_OVERLAY = "overlay";
    private static final String TAG_OVERLAY_USER_ATTRIBUTES = "overlay_user_attributes";

    private static final String ATTR_ID = "id";
    private static final String ATTR_FILENAME = "filename";
    private static final String ATTR_AUDIO_WAVEFORM_FILENAME = "wavefoem";
@@ -572,6 +577,40 @@ public class VideoEditorTestImpl implements VideoEditor {
                        Long.toString(mediaItem.getTimelineDuration()));
            }

            final List<Overlay> overlays = mediaItem.getAllOverlays();
            if (overlays.size() > 0) {
                serializer.startTag("", TAG_OVERLAYS);
                for (Overlay overlay : overlays) {
                    serializer.startTag("", TAG_OVERLAY);
                    serializer.attribute("", ATTR_ID, overlay.getId());
                    serializer.attribute("", ATTR_TYPE, overlay.getClass().getSimpleName());
                    serializer.attribute("", ATTR_BEGIN_TIME,
                            Long.toString(overlay.getStartTime()));
                    serializer.attribute("", ATTR_DURATION, Long.toString(overlay.getDuration()));
                    if (overlay instanceof OverlayFrame) {
                        final OverlayFrame overlayFrame = (OverlayFrame)overlay;
                        overlayFrame.save(this);
                        if (overlayFrame.getFilename() != null) {
                            serializer.attribute("", ATTR_FILENAME, overlayFrame.getFilename());
                        }
                    }

                    // Save the user attributes
                    serializer.startTag("", TAG_OVERLAY_USER_ATTRIBUTES);
                    final Map<String, String> userAttributes = overlay.getUserAttributes();
                    for (String name : userAttributes.keySet()) {
                        final String value = userAttributes.get(name);
                        if (value != null) {
                            serializer.attribute("", name, value);
                        }
                    }
                    serializer.endTag("", TAG_OVERLAY_USER_ATTRIBUTES);

                    serializer.endTag("", TAG_OVERLAY);
                }
                serializer.endTag("", TAG_OVERLAYS);
            }

            serializer.endTag("", TAG_MEDIA_ITEM);
        }
        serializer.endTag("", TAG_MEDIA_ITEMS);
@@ -629,21 +668,22 @@ public class VideoEditorTestImpl implements VideoEditor {
        parser.setInput(new FileInputStream(file), "UTF-8");
        int eventType = parser.getEventType();
        String name;
        MediaItem currentMediaItem = null;
        Overlay currentOverlay = null;
        while (eventType != XmlPullParser.END_DOCUMENT) {
            switch (eventType) {
                case XmlPullParser.START_TAG: {
                    name = parser.getName();
                    if (name.equals(TAG_PROJECT)) {
                    if (TAG_PROJECT.equals(name)) {
                        mAspectRatio = Integer.parseInt(parser.getAttributeValue("",
                                ATTR_ASPECT_RATIO));
                    } else if (name.equals(TAG_MEDIA_ITEM)) {
                    } else if (TAG_MEDIA_ITEM.equals(name)) {
                        final String mediaItemId = parser.getAttributeValue("", ATTR_ID);
                        final String type = parser.getAttributeValue("", ATTR_TYPE);
                        final String filename = parser.getAttributeValue("", ATTR_FILENAME);
                        final int renderingMode = Integer.parseInt(parser.getAttributeValue("",
                                ATTR_RENDERING_MODE));

                        MediaItem currentMediaItem;
                        if (MediaImageItem.class.getSimpleName().equals(type)) {
                            final long durationMs = Long.parseLong(parser.getAttributeValue("",
                                    ATTR_DURATION));
@@ -673,11 +713,36 @@ public class VideoEditorTestImpl implements VideoEditor {
                        if (currentMediaItem != null) {
                            mMediaItems.add(currentMediaItem);
                        }
                    } else if (name.equals(TAG_TRANSITION)) {
                    } else if (TAG_TRANSITION.equals(name)) {
                        final Transition transition = parseTransition(parser);
                        if (transition != null) {
                            mTransitions.add(transition);
                        }
                    } else if (TAG_OVERLAY.equals(name)) {
                        if (currentMediaItem != null) {
                            currentOverlay = parseOverlay(parser, currentMediaItem);
                            if (currentOverlay != null) {
                                currentMediaItem.addOverlay(currentOverlay);
                            }
                        }
                    } else if (TAG_OVERLAY_USER_ATTRIBUTES.equals(name)) {
                        if (currentOverlay != null) {
                            final int attributesCount = parser.getAttributeCount();
                            for (int i = 0; i < attributesCount; i++) {
                                currentOverlay.setUserAttribute(parser.getAttributeName(i),
                                        parser.getAttributeValue(i));
                            }
                        }
                    }
                    break;
                }

                case XmlPullParser.END_TAG: {
                    name = parser.getName();
                    if (TAG_MEDIA_ITEM.equals(name)) {
                        currentMediaItem = null;
                    } else if (TAG_OVERLAY.equals(name)) {
                        currentOverlay = null;
                    }
                    break;
                }
@@ -764,6 +829,31 @@ public class VideoEditorTestImpl implements VideoEditor {
        return transition;
    }

    /**
     * Parse the overlay
     *
     * @param parser The parser
     * @param mediaItem The media item owner
     *
     * @return The overlay
     */
    private Overlay parseOverlay(XmlPullParser parser, MediaItem mediaItem) {
        final String overlayId = parser.getAttributeValue("", ATTR_ID);
        final String type = parser.getAttributeValue("", ATTR_TYPE);
        final long durationMs = Long.parseLong(parser.getAttributeValue("", ATTR_DURATION));
        final long startTimeMs = Long.parseLong(parser.getAttributeValue("", ATTR_BEGIN_TIME));

        final Overlay overlay;
        if (OverlayFrame.class.getSimpleName().equals(type)) {
            final String filename = parser.getAttributeValue("", ATTR_FILENAME);
            overlay = new OverlayFrame(mediaItem, overlayId, filename, startTimeMs, durationMs);
        } else {
            overlay = null;
        }

        return overlay;
    }

    public void cancelExport(String filename) {
    }