Skip to content

Commit

Permalink
feat: Audio stream endpoints in Voice
Browse files Browse the repository at this point in the history
  • Loading branch information
SMadani committed Jun 27, 2024
1 parent f358d47 commit ddc5c23
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 2 deletions.
7 changes: 7 additions & 0 deletions src/main/kotlin/com/vonage/client/kt/Voice.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ class Voice(private val voiceClient: VoiceClient) {
fun transfer(nccoUrl: URI) = transfer(nccoUrl.toString())

fun sendDtmf(digits: String): DtmfResponse = voiceClient.sendDtmf(callId, digits)

fun streamAudio(streamUrl: String, loop: Int? = null, level: Double? = null): StreamResponse =
if (loop != null && level != null) voiceClient.startStream(callId, streamUrl, loop, level)
else if (loop != null) voiceClient.startStream(callId, streamUrl, loop)
else voiceClient.startStream(callId, streamUrl)

fun stopStream(): StreamResponse = voiceClient.stopStream(callId)
}

fun listCalls(filter: (CallsFilter.Builder.() -> Unit)? = null): CallInfoPage =
Expand Down
6 changes: 4 additions & 2 deletions src/test/kotlin/com/vonage/client/kt/AbstractTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,10 @@ abstract class AbstractTest {
expectedResponseParams: Map<String, Any>? = null) =
mockP(HttpMethod.PUT, expectedUrl, expectedRequestParams, status, authType, expectedResponseParams)

protected fun mockDelete(expectedUrl: String, authType: AuthType? = null) =
mockRequest(HttpMethod.DELETE, expectedUrl, authType = authType).mockReturn(204)
protected fun mockDelete(expectedUrl: String, authType: AuthType? = null,
expectedResponseParams: Map<String, Any>? = null) =
mockRequest(HttpMethod.DELETE, expectedUrl, authType = authType)
.mockReturn(if (expectedResponseParams == null) 204 else 200, expectedResponseParams)

protected fun mockGet(expectedUrl: String,
expectedQueryParams: Map<String, Any>? = null,
Expand Down
48 changes: 48 additions & 0 deletions src/test/kotlin/com/vonage/client/kt/VoiceTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class VoiceTest : AbstractTest() {
private val pageSize = 25
private val recordIndex = 14
private val dtmf = "p*123#"
private val streamUrl = "https://example.com/waiting.mp3"
private val callResponseMap = mapOf(
"_links" to mapOf(
"self" to mapOf(
Expand Down Expand Up @@ -113,6 +114,30 @@ class VoiceTest : AbstractTest() {
invocation.invoke()
}

private fun testStream(loop: Int = 1, level: Double = 0.0, invocation: (() -> StreamResponse)? = null) {

val message = "Stream ${if (invocation == null) "stopped" else "started"}"
val expectedResponseParams = mapOf("message" to message, "uuid" to callIdStr)
val expectedUrl = "$callUrl/stream"
val response = if (invocation == null) {
mockDelete(expectedUrl, expectedResponseParams = expectedResponseParams)
callObj.stopStream()
}
else {
mockPut(expectedUrl, status = 200,
expectedRequestParams = mapOf(
"stream_url" to listOf(streamUrl),
"loop" to loop, "level" to level
),
expectedResponseParams = expectedResponseParams
)
invocation.invoke()
}
assertNotNull(response)
assertEquals(message, response.message)
assertEquals(callIdStr, response.uuid)
}

@Test
fun `terminate call`() {
testModifyCall("hangup", callObj::hangup)
Expand Down Expand Up @@ -173,6 +198,29 @@ class VoiceTest : AbstractTest() {
assertEquals(callIdStr, response.uuid)
}

@Test
fun `stream audio into call`() {
val loop = 3
val level = 0.45
testStream {
callObj.streamAudio(streamUrl)
}
testStream(loop = loop) {
callObj.streamAudio(streamUrl, loop)
}
testStream(level = level) {
callObj.streamAudio(streamUrl, level = level)
}
testStream(loop = loop, level = level) {
callObj.streamAudio(streamUrl, loop = loop, level = level)
}
}

@Test
fun `stop audio stream`() {
testStream()
}

@Test
fun `list calls all filter parameters`() {
mockGet(callsBaseUrl, expectedQueryParams = mapOf(
Expand Down

0 comments on commit ddc5c23

Please sign in to comment.