|
| 1 | +/* |
| 2 | + * Copyright 2024 Vonage |
| 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 | +package com.vonage.client.kt |
| 17 | + |
| 18 | +import com.vonage.client.video.* |
| 19 | +import java.util.* |
| 20 | + |
| 21 | +class Video(private val client: VideoClient) { |
| 22 | + |
| 23 | + fun createSession(properties: CreateSessionRequest.Builder.() -> Unit = {}): CreateSessionResponse = |
| 24 | + client.createSession(CreateSessionRequest.builder().apply(properties).build()) |
| 25 | + |
| 26 | + fun session(sessionId: String): ExistingSession = ExistingSession(sessionId) |
| 27 | + |
| 28 | + inner class ExistingSession internal constructor(val id: String) { |
| 29 | + |
| 30 | + fun stream(streamId: String): ExistingStream = ExistingStream(streamId) |
| 31 | + |
| 32 | + inner class ExistingStream internal constructor(val streamId: String) { |
| 33 | + |
| 34 | + fun info(): GetStreamResponse = client.getStream(id, streamId) |
| 35 | + |
| 36 | + fun mute(): Unit = client.muteStream(id, streamId) |
| 37 | + |
| 38 | + fun setLayout(vararg layoutClasses: String): Unit = |
| 39 | + client.setStreamLayout(id, |
| 40 | + SessionStream.builder(streamId).layoutClassList(layoutClasses.toList()).build() |
| 41 | + ) |
| 42 | + } |
| 43 | + |
| 44 | + fun connection(connectionId: String): ExistingConnection = ExistingConnection(connectionId) |
| 45 | + |
| 46 | + inner class ExistingConnection internal constructor(val id: String) { |
| 47 | + |
| 48 | + fun disconnect(): Unit = client.forceDisconnect(this@ExistingSession.id, id) |
| 49 | + |
| 50 | + fun signal(type: String, data: String): Unit = |
| 51 | + client.signal(this@ExistingSession.id, id, signalRequest(type, data)) |
| 52 | + |
| 53 | + fun sendDtmf(digits: String): Unit = client.sendDtmf(this@ExistingSession.id, id, digits) |
| 54 | + } |
| 55 | + |
| 56 | + fun muteStreams(active: Boolean = true, vararg excludedStreamIds: String): ProjectDetails = |
| 57 | + client.muteSession(id, active, |
| 58 | + if (excludedStreamIds.isNotEmpty()) excludedStreamIds.toList() else null |
| 59 | + ) |
| 60 | + |
| 61 | + fun listStreams(): List<GetStreamResponse> = client.listStreams(id) |
| 62 | + |
| 63 | + fun listArchives(count: Int = 1000, offset: Int = 0): List<Archive> = |
| 64 | + client.listArchives(listCompositionsFilter(count, offset, id)) |
| 65 | + |
| 66 | + fun listBroadcasts(count: Int = 1000, offset: Int = 0): List<Broadcast> = |
| 67 | + client.listBroadcasts(listCompositionsFilter(count, offset, id)) |
| 68 | + |
| 69 | + fun signalAll(type: String, data: String): Unit = |
| 70 | + client.signalAll(id, signalRequest(type, data)) |
| 71 | + |
| 72 | + fun sendDtmf(digits: String): Unit = client.sendDtmf(id, digits) |
| 73 | + |
| 74 | + fun startCaptions(token: String, properties: CaptionsRequest.Builder.() -> Unit): UUID = |
| 75 | + client.startCaptions(CaptionsRequest.builder() |
| 76 | + .apply(properties).sessionId(id).token(token).build() |
| 77 | + ).captionsId |
| 78 | + |
| 79 | + fun stopCaptions(captionsId: String): Unit = client.stopCaptions(captionsId) |
| 80 | + |
| 81 | + fun createArchive(properties: Archive.Builder.() -> Unit = {}): Archive = |
| 82 | + client.createArchive(Archive.builder(id).apply(properties).build()) |
| 83 | + |
| 84 | + fun startBroadcast(properties: Broadcast.Builder.() -> Unit): Broadcast = |
| 85 | + client.createBroadcast(Broadcast.builder(id).apply(properties).build()) |
| 86 | + |
| 87 | + fun generateToken(options: TokenOptions.Builder.() -> Unit = {}): String = |
| 88 | + client.generateToken(id, TokenOptions.builder().apply(options).build()) |
| 89 | + } |
| 90 | + |
| 91 | + fun sipDial(properties: SipDialRequest.Builder.() -> Unit): SipDialResponse = |
| 92 | + client.sipDial(SipDialRequest.builder().apply(properties).build()) |
| 93 | + |
| 94 | + fun connectToWebsocket(properties: ConnectRequest.Builder.() -> Unit): ConnectResponse = |
| 95 | + client.connectToWebsocket(ConnectRequest.builder().apply(properties).build()) |
| 96 | + |
| 97 | + fun listArchives(count: Int = 1000, offset: Int = 0): List<Archive> = |
| 98 | + client.listArchives(listCompositionsFilter(count, offset)) |
| 99 | + |
| 100 | + fun archive(archiveId: String): ExistingArchive = ExistingArchive(archiveId) |
| 101 | + |
| 102 | + inner class ExistingArchive internal constructor(val id: String) { |
| 103 | + |
| 104 | + fun info(): Archive = client.getArchive(id) |
| 105 | + |
| 106 | + fun stop(): Archive = client.stopArchive(id) |
| 107 | + |
| 108 | + fun delete(): Unit = client.deleteArchive(id) |
| 109 | + |
| 110 | + fun setLayout(initialLayout: ScreenLayoutType, |
| 111 | + screenshareType: ScreenLayoutType? = null, |
| 112 | + stylesheet: String? = null): Unit = |
| 113 | + client.updateArchiveLayout(id, |
| 114 | + StreamCompositionLayout.builder(initialLayout) |
| 115 | + .screenshareType(screenshareType) |
| 116 | + .stylesheet(stylesheet) |
| 117 | + .build() |
| 118 | + ) |
| 119 | + |
| 120 | + fun addStream(streamId: String, audio: Boolean = true, video: Boolean = true) = |
| 121 | + client.addArchiveStream(id, streamId, audio, video) |
| 122 | + |
| 123 | + fun removeStream(streamId: String): Unit = client.removeArchiveStream(id, streamId) |
| 124 | + } |
| 125 | + |
| 126 | + fun listBroadcasts(count: Int = 1000, offset: Int = 0): List<Broadcast> = |
| 127 | + client.listBroadcasts(listCompositionsFilter(count, offset)) |
| 128 | + |
| 129 | + fun broadcast(broadcastId: String): ExistingBroadcast = ExistingBroadcast(broadcastId) |
| 130 | + |
| 131 | + inner class ExistingBroadcast internal constructor(val id: String) { |
| 132 | + |
| 133 | + fun info(): Broadcast = client.getBroadcast(id) |
| 134 | + |
| 135 | + fun stop(): Broadcast = client.stopBroadcast(id) |
| 136 | + |
| 137 | + fun setLayout(initialLayout: ScreenLayoutType, |
| 138 | + screenshareType: ScreenLayoutType? = null, |
| 139 | + stylesheet: String? = null): Unit = |
| 140 | + client.updateBroadcastLayout(id, |
| 141 | + StreamCompositionLayout.builder(initialLayout) |
| 142 | + .screenshareType(screenshareType) |
| 143 | + .stylesheet(stylesheet) |
| 144 | + .build() |
| 145 | + ) |
| 146 | + |
| 147 | + fun addStream(streamId: String, audio: Boolean = true, video: Boolean = true) = |
| 148 | + client.addBroadcastStream(id, streamId, audio, video) |
| 149 | + |
| 150 | + fun removeStream(streamId: String): Unit = client.removeBroadcastStream(id, streamId) |
| 151 | + } |
| 152 | + |
| 153 | + fun startRender(properties: RenderRequest.Builder.() -> Unit): RenderResponse = |
| 154 | + client.startRender(RenderRequest.builder().apply(properties).build()) |
| 155 | + |
| 156 | + fun listRenders(count: Int = 1000, offset: Int = 0): List<RenderResponse> = |
| 157 | + client.listRenders(ListStreamCompositionsRequest.builder().count(count).offset(offset).build()) |
| 158 | + |
| 159 | + fun render(renderId: String): ExistingRender = ExistingRender(renderId) |
| 160 | + |
| 161 | + inner class ExistingRender internal constructor(val id: String) { |
| 162 | + |
| 163 | + fun info(): RenderResponse = client.getRender(id) |
| 164 | + |
| 165 | + fun stop(): Unit = client.stopRender(id) |
| 166 | + } |
| 167 | +} |
| 168 | + |
| 169 | +private fun listCompositionsFilter(count: Int, offset: Int, sessionId: String? = null): |
| 170 | + ListStreamCompositionsRequest = ListStreamCompositionsRequest.builder() |
| 171 | + .count(count).offset(offset).sessionId(sessionId).build() |
| 172 | + |
| 173 | +private fun signalRequest(type: String, data: String): SignalRequest = |
| 174 | + SignalRequest.builder().type(type).data(data).build() |
| 175 | + |
| 176 | +private fun streamCompositionLayout(initialLayout: ScreenLayoutType, |
| 177 | + screenshareType: ScreenLayoutType?, |
| 178 | + stylesheet: String?): StreamCompositionLayout = |
| 179 | + StreamCompositionLayout.builder(initialLayout) |
| 180 | + .screenshareType(screenshareType).stylesheet(stylesheet).build() |
| 181 | + |
| 182 | +fun Broadcast.Builder.addRtmpStream(properties: Rtmp.Builder.() -> Unit): Broadcast.Builder = |
| 183 | + addRtmpStream(Rtmp.builder().apply(properties).build()) |
| 184 | + |
| 185 | +fun Broadcast.Builder.hls(hls: Hls.Builder.() -> Unit = {}): Broadcast.Builder = |
| 186 | + hls(Hls.builder().apply(hls).build()) |
| 187 | + |
| 188 | +fun Archive.Builder.layout(initialLayout: ScreenLayoutType, |
| 189 | + screenshareType: ScreenLayoutType? = null, |
| 190 | + stylesheet: String? = null): Archive.Builder = |
| 191 | + layout(streamCompositionLayout(initialLayout, screenshareType, stylesheet)) |
| 192 | + |
| 193 | +fun Broadcast.Builder.layout(initialLayout: ScreenLayoutType, |
| 194 | + screenshareType: ScreenLayoutType? = null, |
| 195 | + stylesheet: String? = null): Broadcast.Builder = |
| 196 | + layout(streamCompositionLayout(initialLayout, screenshareType, stylesheet)) |
0 commit comments