All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.twilio.rest.video.v1.CompositionHookCreator Maven / Gradle / Ivy

There is a newer version: 10.6.4
Show newest version
/**
 * This code was generated by
 * \ / _    _  _|   _  _
 *  | (_)\/(_)(_|\/| |(/_  v1.0.0
 *       /       /
 */

package com.twilio.rest.video.v1;

import com.twilio.base.Creator;
import com.twilio.converter.Converter;
import com.twilio.converter.Promoter;
import com.twilio.exception.ApiConnectionException;
import com.twilio.exception.ApiException;
import com.twilio.exception.RestException;
import com.twilio.http.HttpMethod;
import com.twilio.http.Request;
import com.twilio.http.Response;
import com.twilio.http.TwilioRestClient;
import com.twilio.rest.Domains;

import java.net.URI;
import java.util.List;
import java.util.Map;

/**
 * PLEASE NOTE that this class contains preview products that are subject to
 * change. Use them with caution. If you currently do not have developer preview
 * access, please contact [email protected].
 */
public class CompositionHookCreator extends Creator {
    private final String friendlyName;
    private Boolean enabled;
    private Map videoLayout;
    private List audioSources;
    private List audioSourcesExcluded;
    private String resolution;
    private CompositionHook.Format format;
    private URI statusCallback;
    private HttpMethod statusCallbackMethod;
    private Boolean trim;

    /**
     * Construct a new CompositionHookCreator.
     * 
     * @param friendlyName Friendly name of the Composition Hook to be shown in the
     *                     console.
     */
    public CompositionHookCreator(final String friendlyName) {
        this.friendlyName = friendlyName;
    }

    /**
     * When activated, the Composition Hook is enabled and a composition will be
     * triggered for every Video room completed by this account from this point
     * onwards; `false` indicates the Composition Hook is left inactive..
     * 
     * @param enabled Boolean flag for activating the Composition Hook.
     * @return this
     */
    public CompositionHookCreator setEnabled(final Boolean enabled) {
        this.enabled = enabled;
        return this;
    }

    /**
     * A JSON object defining the video layout of the Composition Hook in terms of
     * regions. See the section [Managing Video Layouts](#managing-video-layouts)
     * below for further information..
     * 
     * @param videoLayout The JSON video layout description.
     * @return this
     */
    public CompositionHookCreator setVideoLayout(final Map videoLayout) {
        this.videoLayout = videoLayout;
        return this;
    }

    /**
     * An array of audio sources to merge. All the specified sources must belong to
     * the same Group Room. It can include: 
     * * Zero or more Track names. These can be specified using wildcards (e.g.
     * `student*`). The use of `[*]` has semantics "all if any" meaning zero or more
     * (i.e. all) depending on whether the Group Room had audio tracks..
     * 
     * @param audioSources A list of audio sources related to this Composition Hook.
     * @return this
     */
    public CompositionHookCreator setAudioSources(final List audioSources) {
        this.audioSources = audioSources;
        return this;
    }

    /**
     * An array of audio sources to merge. All the specified sources must belong to
     * the same Group Room. It can include: 
     * * Zero or more Track names. These can be specified using wildcards (e.g.
     * `student*`). The use of `[*]` has semantics "all if any" meaning zero or more
     * (i.e. all) depending on whether the Group Room had audio tracks..
     * 
     * @param audioSources A list of audio sources related to this Composition Hook.
     * @return this
     */
    public CompositionHookCreator setAudioSources(final String audioSources) {
        return setAudioSources(Promoter.listOfOne(audioSources));
    }

    /**
     * An array of audio sources to exclude from the Composition Hook. Any new
     * Composition triggered by the Composition Hook shall include all audio sources
     * specified in `AudioSources` except for the ones specified in
     * `AudioSourcesExcluded`. This parameter may include: 
     * * Zero or more Track names. These can be specified using wildcards (e.g.
     * `student*`).
     * 
     * @param audioSourcesExcluded A list of audio sources excluded related to this
     *                             Composition Hook.
     * @return this
     */
    public CompositionHookCreator setAudioSourcesExcluded(final List audioSourcesExcluded) {
        this.audioSourcesExcluded = audioSourcesExcluded;
        return this;
    }

    /**
     * An array of audio sources to exclude from the Composition Hook. Any new
     * Composition triggered by the Composition Hook shall include all audio sources
     * specified in `AudioSources` except for the ones specified in
     * `AudioSourcesExcluded`. This parameter may include: 
     * * Zero or more Track names. These can be specified using wildcards (e.g.
     * `student*`).
     * 
     * @param audioSourcesExcluded A list of audio sources excluded related to this
     *                             Composition Hook.
     * @return this
     */
    public CompositionHookCreator setAudioSourcesExcluded(final String audioSourcesExcluded) {
        return setAudioSourcesExcluded(Promoter.listOfOne(audioSourcesExcluded));
    }

    /**
     * A string representing the numbers of pixels for rows (width) and columns
     * (height) of the generated composed video. This string must have the format
     * `{width}x{height}`. This parameter must comply with the following
     * constraints: 
     * * `width >= 16 && width <= 1280`
     * * `height >= 16 && height <= 1280`
     * * `width * height <= 921,600`
     * Typical values are: 
     * * HD = `1280x720`
     * * PAL = `1024x576`
     * * VGA = `640x480`
     * * CIF = `320x240`
     * Note that the `Resolution` implicitly imposes an aspect ratio to the
     * resulting composition. When the original video tracks get constrained by this
     * aspect ratio they are scaled-down to fit. You can find detailed information
     * in the [Managing Video Layouts](#managing-video-layouts) section. Defaults to
     * `640x480`..
     * 
     * @param resolution Pixel resolution of the composed video.
     * @return this
     */
    public CompositionHookCreator setResolution(final String resolution) {
        this.resolution = resolution;
        return this;
    }

    /**
     * Container format of the Composition media files created by the Composition
     * Hook. Can be any of the following: `mp4`, `webm`. The use of `mp4` or `webm`
     * makes mandatory the specification of `AudioSources` and/or one `VideoLayout`
     * element containing a valid `video_sources` list, otherwise an error is fired.
     * Defaults to `webm`..
     * 
     * @param format Container format of the Composition Hook media file. Any of
     *               the following: `mp4`, `webm`.
     * @return this
     */
    public CompositionHookCreator setFormat(final CompositionHook.Format format) {
        this.format = format;
        return this;
    }

    /**
     * A URL that Twilio sends asynchronous webhook requests to on every composition
     * event. If not provided, status callback events will not be dispatched..
     * 
     * @param statusCallback A URL that Twilio sends asynchronous webhook requests
     *                       to on every composition event.
     * @return this
     */
    public CompositionHookCreator setStatusCallback(final URI statusCallback) {
        this.statusCallback = statusCallback;
        return this;
    }

    /**
     * A URL that Twilio sends asynchronous webhook requests to on every composition
     * event. If not provided, status callback events will not be dispatched..
     * 
     * @param statusCallback A URL that Twilio sends asynchronous webhook requests
     *                       to on every composition event.
     * @return this
     */
    public CompositionHookCreator setStatusCallback(final String statusCallback) {
        return setStatusCallback(Promoter.uriFromString(statusCallback));
    }

    /**
     * HTTP method Twilio should use when requesting the above URL. Defaults to
     * `POST`..
     * 
     * @param statusCallbackMethod HTTP method Twilio should use when requesting
     *                             the above URL.
     * @return this
     */
    public CompositionHookCreator setStatusCallbackMethod(final HttpMethod statusCallbackMethod) {
        this.statusCallbackMethod = statusCallbackMethod;
        return this;
    }

    /**
     * When activated, clips all the intervals where there is no active media in the
     * Compositions triggered by the Composition Hook. This results in shorter
     * compositions in cases when the Room was created but no Participant joined for
     * some time, or if all the Participants left the room and joined at a later
     * stage, as those gaps will be removed. You can find further information in the
     * [Managing Video Layouts](#managing-video-layouts) section. Defaults to
     * `true`..
     * 
     * @param trim Boolean flag for clipping intervals that have no media.
     * @return this
     */
    public CompositionHookCreator setTrim(final Boolean trim) {
        this.trim = trim;
        return this;
    }

    /**
     * Make the request to the Twilio API to perform the create.
     * 
     * @param client TwilioRestClient with which to make the request
     * @return Created CompositionHook
     */
    @Override
    @SuppressWarnings("checkstyle:linelength")
    public CompositionHook create(final TwilioRestClient client) {
        Request request = new Request(
            HttpMethod.POST,
            Domains.VIDEO.toString(),
            "/v1/CompositionHooks",
            client.getRegion()
        );

        addPostParams(request);
        Response response = client.request(request);

        if (response == null) {
            throw new ApiConnectionException("CompositionHook creation failed: Unable to connect to server");
        } else if (!TwilioRestClient.SUCCESS.apply(response.getStatusCode())) {
            RestException restException = RestException.fromJson(response.getStream(), client.getObjectMapper());
            if (restException == null) {
                throw new ApiException("Server Error, no content");
            }

            throw new ApiException(
                restException.getMessage(),
                restException.getCode(),
                restException.getMoreInfo(),
                restException.getStatus(),
                null
            );
        }

        return CompositionHook.fromJson(response.getStream(), client.getObjectMapper());
    }

    /**
     * Add the requested post parameters to the Request.
     * 
     * @param request Request to add post params to
     */
    private void addPostParams(final Request request) {
        if (friendlyName != null) {
            request.addPostParam("FriendlyName", friendlyName);
        }

        if (enabled != null) {
            request.addPostParam("Enabled", enabled.toString());
        }

        if (videoLayout != null) {
            request.addPostParam("VideoLayout", Converter.mapToJson(videoLayout));
        }

        if (audioSources != null) {
            for (String prop : audioSources) {
                request.addPostParam("AudioSources", prop);
            }
        }

        if (audioSourcesExcluded != null) {
            for (String prop : audioSourcesExcluded) {
                request.addPostParam("AudioSourcesExcluded", prop);
            }
        }

        if (resolution != null) {
            request.addPostParam("Resolution", resolution);
        }

        if (format != null) {
            request.addPostParam("Format", format.toString());
        }

        if (statusCallback != null) {
            request.addPostParam("StatusCallback", statusCallback.toString());
        }

        if (statusCallbackMethod != null) {
            request.addPostParam("StatusCallbackMethod", statusCallbackMethod.toString());
        }

        if (trim != null) {
            request.addPostParam("Trim", trim.toString());
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy