1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
|
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# pylint: disable=missing-docstring
#
import ctypes # type: ignore
import sys
import unittest
import opuslib.api
import opuslib.api.encoder
import opuslib.api.ctl
__author__ = 'Никита Кузнецов <self@svartalf.info>'
__copyright__ = 'Copyright (c) 2012, SvartalF'
__license__ = 'BSD 3-Clause License'
class EncoderTest(unittest.TestCase):
"""Encoder basic API tests
From the `tests/test_opus_api.c`
"""
def _test_unsupported_sample_rates(self):
for cxx in range(0, 4):
for ixx in range(-7, 96000 + 1):
if ixx in (8000, 12000, 16000, 24000, 48000) and cxx in (1, 2):
continue
if ixx == -5:
fsx = -8000
elif ixx == -6:
fsx = sys.maxsize # TODO: Must be an INT32_MAX
elif ixx == -7:
fsx = -1 * (sys.maxsize - 1) # TODO: Must be an INT32_MIN
else:
fsx = ixx
try:
opuslib.api.encoder.create_state(
fsx, cxx, opuslib.APPLICATION_VOIP)
except opuslib.OpusError as exc:
self.assertEqual(exc.code, opuslib.BAD_ARG)
def test_create(self):
try:
opuslib.api.encoder.create_state(48000, 2, opuslib.AUTO)
except opuslib.OpusError as exc:
self.assertEqual(exc.code, opuslib.BAD_ARG)
enc = opuslib.api.encoder.create_state(
48000, 2, opuslib.APPLICATION_VOIP)
opuslib.api.encoder.destroy(enc)
enc = opuslib.api.encoder.create_state(
48000, 2, opuslib.APPLICATION_RESTRICTED_LOWDELAY)
# TODO: rewrite that code
# i = opuslib.api.encoder.encoder_ctl(
# enc, opuslib.api.ctl.get_lookahead)
# if(err!=OPUS_OK || i<0 || i>32766)test_failed();
opuslib.api.encoder.destroy(enc)
enc = opuslib.api.encoder.create_state(
48000, 2, opuslib.APPLICATION_AUDIO)
# TODO: rewrite that code
# i = opuslib.api.encoder.encoder_ctl(
# enc, opuslib.api.ctl.get_lookahead)
# err=opus_encoder_ctl(enc,OPUS_GET_LOOKAHEAD(&i));
# if(err!=OPUS_OK || i<0 || i>32766)test_failed();
opuslib.api.encoder.destroy(enc)
@classmethod
def test_encode(cls):
enc = opuslib.api.encoder.create_state(
48000, 2, opuslib.APPLICATION_AUDIO)
data = chr(0) * ctypes.sizeof(ctypes.c_short) * 2 * 960
opuslib.api.encoder.encode(enc, data, 960, len(data))
opuslib.api.encoder.destroy(enc)
@classmethod
def test_encode_float(cls):
enc = opuslib.api.encoder.create_state(
48000, 2, opuslib.APPLICATION_AUDIO)
data = chr(0) * ctypes.sizeof(ctypes.c_float) * 2 * 960
opuslib.api.encoder.encode_float(enc, data, 960, len(data))
opuslib.api.encoder.destroy(enc)
def test_unimplemented(self):
enc = opuslib.api.encoder.create_state(
48000, 2, opuslib.APPLICATION_AUDIO)
try:
opuslib.api.encoder.encoder_ctl(enc, opuslib.api.ctl.unimplemented)
except opuslib.OpusError as exc:
self.assertEqual(exc.code, opuslib.UNIMPLEMENTED)
opuslib.api.encoder.destroy(enc)
def test_application(self):
self.check_setget(
opuslib.api.ctl.set_application,
opuslib.api.ctl.get_application,
(-1, opuslib.AUTO),
(opuslib.APPLICATION_AUDIO,
opuslib.APPLICATION_RESTRICTED_LOWDELAY)
)
def test_bitrate(self):
enc = opuslib.api.encoder.create_state(
48000, 2, opuslib.APPLICATION_AUDIO)
opuslib.api.encoder.encoder_ctl(
enc, opuslib.api.ctl.set_bitrate, 1073741832)
value = opuslib.api.encoder.encoder_ctl(
enc, opuslib.api.ctl.get_bitrate)
self.assertLess(value, 700000)
self.assertGreater(value, 256000)
opuslib.api.encoder.destroy(enc)
self.check_setget(
opuslib.api.ctl.set_bitrate,
opuslib.api.ctl.get_bitrate,
(-12345, 0),
(500, 256000)
)
def test_force_channels(self):
self.check_setget(
opuslib.api.ctl.set_force_channels,
opuslib.api.ctl.get_force_channels,
(-1, 3),
(1, opuslib.AUTO)
)
def test_bandwidth(self):
enc = opuslib.api.encoder.create_state(
48000, 2, opuslib.APPLICATION_AUDIO)
# Set bandwidth
ixx = -2
self.assertRaises(
opuslib.OpusError,
lambda: opuslib.api.encoder.encoder_ctl(
enc, opuslib.api.ctl.set_bandwidth, ixx)
)
ix1 = opuslib.BANDWIDTH_FULLBAND + 1
self.assertRaises(
opuslib.OpusError,
lambda: opuslib.api.encoder.encoder_ctl(
enc, opuslib.api.ctl.set_bandwidth, ix1)
)
ix2 = opuslib.BANDWIDTH_NARROWBAND
opuslib.api.encoder.encoder_ctl(
enc, opuslib.api.ctl.set_bandwidth, ix2)
ix3 = opuslib.BANDWIDTH_FULLBAND
opuslib.api.encoder.encoder_ctl(
enc, opuslib.api.ctl.set_bandwidth, ix3)
ix4 = opuslib.BANDWIDTH_WIDEBAND
opuslib.api.encoder.encoder_ctl(
enc, opuslib.api.ctl.set_bandwidth, ix4)
ix5 = opuslib.BANDWIDTH_MEDIUMBAND
opuslib.api.encoder.encoder_ctl(
enc, opuslib.api.ctl.set_bandwidth, ix5)
# Get bandwidth
value = opuslib.api.encoder.encoder_ctl(
enc, opuslib.api.ctl.get_bandwidth)
self.assertIn(
value,
(opuslib.BANDWIDTH_FULLBAND,
opuslib.BANDWIDTH_MEDIUMBAND,
opuslib.BANDWIDTH_WIDEBAND,
opuslib.BANDWIDTH_NARROWBAND,
opuslib.AUTO)
)
opuslib.api.encoder.encoder_ctl(
enc, opuslib.api.ctl.set_bandwidth, opuslib.AUTO)
opuslib.api.encoder.destroy(enc)
def test_max_bandwidth(self):
enc = opuslib.api.encoder.create_state(
48000, 2, opuslib.APPLICATION_AUDIO)
i = -2
self.assertRaises(
opuslib.OpusError,
lambda: opuslib.api.encoder.encoder_ctl(
enc, opuslib.api.ctl.set_max_bandwidth, i)
)
i = opuslib.BANDWIDTH_FULLBAND + 1
self.assertRaises(
opuslib.OpusError,
lambda: opuslib.api.encoder.encoder_ctl(
enc, opuslib.api.ctl.set_max_bandwidth, i)
)
i = opuslib.BANDWIDTH_NARROWBAND
opuslib.api.encoder.encoder_ctl(
enc, opuslib.api.ctl.set_max_bandwidth, i)
i = opuslib.BANDWIDTH_FULLBAND
opuslib.api.encoder.encoder_ctl(
enc, opuslib.api.ctl.set_max_bandwidth, i)
i = opuslib.BANDWIDTH_WIDEBAND
opuslib.api.encoder.encoder_ctl(
enc, opuslib.api.ctl.set_max_bandwidth, i)
i = opuslib.BANDWIDTH_MEDIUMBAND
opuslib.api.encoder.encoder_ctl(
enc, opuslib.api.ctl.set_max_bandwidth, i)
i = -12345
value = opuslib.api.encoder.encoder_ctl(
enc, opuslib.api.ctl.get_max_bandwidth)
self.assertIn(
value,
(opuslib.BANDWIDTH_FULLBAND,
opuslib.BANDWIDTH_MEDIUMBAND,
opuslib.BANDWIDTH_WIDEBAND,
opuslib.BANDWIDTH_NARROWBAND,
opuslib.AUTO)
)
opuslib.api.encoder.destroy(enc)
def test_dtx(self):
self.check_setget(
opuslib.api.ctl.set_dtx, opuslib.api.ctl.get_dtx, (-1, 2), (1, 0))
def test_complexity(self):
self.check_setget(
opuslib.api.ctl.set_complexity,
opuslib.api.ctl.get_complexity,
(-1, 11),
(0, 10)
)
def test_inband_fec(self):
self.check_setget(
opuslib.api.ctl.set_inband_fec,
opuslib.api.ctl.get_inband_fec,
(-1, 2),
(1, 0)
)
def test_packet_loss_perc(self):
self.check_setget(
opuslib.api.ctl.set_packet_loss_perc,
opuslib.api.ctl.get_packet_loss_perc,
(-1, 101),
(100, 0)
)
def test_vbr(self):
self.check_setget(
opuslib.api.ctl.set_vbr, opuslib.api.ctl.get_vbr, (-1, 2), (1, 0))
def test_vbr_constraint(self):
self.check_setget(
opuslib.api.ctl.set_vbr_constraint,
opuslib.api.ctl.get_vbr_constraint,
(-1, 2),
(1, 0)
)
def test_signal(self):
self.check_setget(
opuslib.api.ctl.set_signal,
opuslib.api.ctl.get_signal,
(-12345, 0x7FFFFFFF),
(opuslib.SIGNAL_MUSIC, opuslib.AUTO)
)
def test_lsb_depth(self):
self.check_setget(
opuslib.api.ctl.set_lsb_depth,
opuslib.api.ctl.get_lsb_depth,
(7, 25),
(16, 24)
)
def check_setget(self, v_set, v_get, bad, good):
enc = opuslib.api.encoder.create_state(
48000, 2, opuslib.APPLICATION_AUDIO)
for value in bad:
self.assertRaises(
opuslib.OpusError,
lambda: opuslib.api.encoder.encoder_ctl(enc, v_set, value)
)
for valuex in good:
opuslib.api.encoder.encoder_ctl(enc, v_set, valuex)
result = opuslib.api.encoder.encoder_ctl(enc, v_get)
self.assertEqual(valuex, result)
opuslib.api.encoder.destroy(enc)
|