001/*
002 *   Copyright 2020 Vonage
003 *
004 *   Licensed under the Apache License, Version 2.0 (the "License");
005 *   you may not use this file except in compliance with the License.
006 *   You may obtain a copy of the License at
007 *
008 *        http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *   Unless required by applicable law or agreed to in writing, software
011 *   distributed under the License is distributed on an "AS IS" BASIS,
012 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *   See the License for the specific language governing permissions and
014 *   limitations under the License.
015 */
016package com.vonage.client.voice;
017
018import com.fasterxml.jackson.annotation.JsonProperty;
019import com.fasterxml.jackson.core.JsonProcessingException;
020import com.fasterxml.jackson.databind.ObjectMapper;
021import com.vonage.client.VonageUnexpectedException;
022
023/**
024 * The JSON payload that will be sent in a {@link StreamRequest}.
025 * <p>
026 * {@code streamUrl}: An array containing a single URL to an mp3 or wav (16-bit) audio file.
027 * {@code loop}: The number of times the audio file at {@code streamUrl} is repeated before the stream ends. Set to 0 to loop infinitely
028 */
029
030public class StreamPayload {
031    private String[] streamUrl;
032    private int loop;
033
034    public StreamPayload(String streamUrl, int loop) {
035        this.streamUrl = new String[]{streamUrl};
036        this.loop = loop;
037    }
038
039    public int getLoop() {
040        return loop;
041    }
042
043    @JsonProperty("stream_url")
044    public String[] getStreamUrl() {
045        return streamUrl;
046    }
047
048    public String toJson() {
049        try {
050            ObjectMapper mapper = new ObjectMapper();
051            return mapper.writeValueAsString(this);
052        } catch (JsonProcessingException jpe) {
053            throw new VonageUnexpectedException("Failed to produce json from StreamPayload object.", jpe);
054        }
055    }
056}