Skip to content

Commit 304c174

Browse files
committed
add rtmp opus tests
1 parent fb090cd commit 304c174

6 files changed

Lines changed: 70 additions & 6 deletions

File tree

app/src/main/java/com/pedro/streamer/rotation/CameraFragment.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ class CameraFragment: Fragment(), ConnectChecker {
8686
val height = 480
8787
val vBitrate = 1200 * 1000
8888
private var rotation = 0
89-
private val sampleRate = 48000
89+
private val sampleRate = 32000
9090
private val isStereo = true
9191
private val aBitrate = 128 * 1000
9292
private var recordPath = ""
@@ -205,7 +205,7 @@ class CameraFragment: Fragment(), ConnectChecker {
205205

206206
override fun onConnectionFailed(reason: String) {
207207
if (genericStream.getStreamClient().reTry(5000, reason, null)) {
208-
toast("Retry, $reason")
208+
toast("Retry")
209209
} else {
210210
genericStream.stopStream()
211211
bStartStop.setImageResource(R.drawable.stream_icon)

rtmp/src/main/java/com/pedro/rtmp/flv/audio/config/OpusAudioSpecificConfig.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2024 pedroSG94.
2+
* Copyright (C) 2025 pedroSG94.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.

rtmp/src/main/java/com/pedro/rtmp/flv/audio/packet/OpusPacket.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2024 pedroSG94.
2+
* Copyright (C) 2025 pedroSG94.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.

rtmp/src/test/java/com/pedro/rtmp/flv/audio/AacPacketTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import java.nio.ByteBuffer
3030
class AacPacketTest {
3131

3232
@Test
33-
fun `GIVEN a aac buffer WHEN call create a aac packet 2 times THEN return config and expected buffer`() = runTest {
33+
fun `GIVEN an AAC buffer WHEN call create an AAC packet 2 times THEN return config and expected buffer`() = runTest {
3434
val timestamp = 123456789L
3535
val buffer = ByteArray(256) { 0x00 }
3636
val info = MediaFrame.Info(0, buffer.size, timestamp, false)

rtmp/src/test/java/com/pedro/rtmp/flv/audio/AudioConfigTest.kt

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,14 @@
1717
package com.pedro.rtmp.flv.audio
1818

1919
import com.pedro.rtmp.flv.audio.config.AacAudioSpecificConfig
20+
import com.pedro.rtmp.flv.audio.config.OpusAudioSpecificConfig
2021
import org.junit.Assert.assertArrayEquals
2122
import org.junit.Test
2223

2324
class AudioConfigTest {
2425

2526
@Test
26-
fun `GIVEN sps and pps WHEN create a video config for sequence packet THEN return a bytearray with the config`() {
27+
fun `GIVEN objectType, sampleRate and channels WHEN create an AAC audio config for sequence packet THEN return a bytearray with the config`() {
2728
val sampleRate = 44100
2829
val isStereo = true
2930
val objectType = AudioObjectType.AAC_LC
@@ -34,4 +35,16 @@ class AudioConfigTest {
3435
config.write(data, 0)
3536
assertArrayEquals(expectedConfig, data)
3637
}
38+
39+
@Test
40+
fun `GIVEN sampleRate and channels WHEN create an Opus audio config for sequence packet THEN return a bytearray with the config`() {
41+
val sampleRate = 48000
42+
val isStereo = true
43+
val expectedConfig = byteArrayOf(79, 112, 117, 115, 72, 101, 97, 100, 1, 2, 15, 0, 0, 0, -69, -128, 0, 0, 0)
44+
45+
val config = OpusAudioSpecificConfig(sampleRate, if (isStereo) 2 else 1)
46+
val data = ByteArray(config.size)
47+
config.write(data, 0)
48+
assertArrayEquals(expectedConfig, data)
49+
}
3750
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright (C) 2024 pedroSG94.
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+
17+
package com.pedro.rtmp.flv.audio
18+
19+
import com.pedro.common.frame.MediaFrame
20+
import com.pedro.rtmp.flv.FlvType
21+
import com.pedro.rtmp.flv.audio.packet.OpusPacket
22+
import kotlinx.coroutines.test.runTest
23+
import org.junit.Assert.assertEquals
24+
import org.junit.Test
25+
import java.nio.ByteBuffer
26+
27+
/**
28+
* Created by pedro on 9/9/23.
29+
*/
30+
class OpusPacketTest {
31+
32+
@Test
33+
fun `GIVEN an Opus buffer WHEN call create an Opus packet 2 times THEN return config and expected buffer`() = runTest {
34+
val timestamp = 123456789L
35+
val buffer = ByteArray(256) { 0x00 }
36+
val info = MediaFrame.Info(0, buffer.size, timestamp, false)
37+
val mediaFrame = MediaFrame(ByteBuffer.wrap(buffer), info, MediaFrame.Type.AUDIO)
38+
val opusPacket = OpusPacket()
39+
opusPacket.sendAudioInfo(48000, true)
40+
opusPacket.createFlvPacket(mediaFrame) { flvPacket ->
41+
assertEquals(FlvType.AUDIO, flvPacket.type)
42+
assertEquals(((AudioFormat.EX_HEADER.value shl 4) or (AudioFourCCPacketType.SEQUENCE_START.value and 0x0F)).toByte(), flvPacket.buffer[0])
43+
assertEquals(5 + 19, flvPacket.length)
44+
}
45+
opusPacket.createFlvPacket(mediaFrame) { flvPacket ->
46+
assertEquals(FlvType.AUDIO, flvPacket.type)
47+
assertEquals(((AudioFormat.EX_HEADER.value shl 4) or (AudioFourCCPacketType.CODED_FRAMES.value and 0x0F)).toByte(), flvPacket.buffer[0])
48+
assertEquals(5 + buffer.size, flvPacket.length)
49+
}
50+
}
51+
}

0 commit comments

Comments
 (0)