1
0
mirror of https://github.com/spl0k/supysonic.git synced 2024-12-23 01:16:18 +00:00
supysonic/tests/api/test_transcoding.py

131 lines
4.3 KiB
Python
Raw Normal View History

2017-12-07 22:33:32 +00:00
# This file is part of Supysonic.
# Supysonic is a Python implementation of the Subsonic server API.
#
# Copyright (C) 2017-2020 Alban 'spl0k' Féron
2017-12-07 22:33:32 +00:00
#
# Distributed under terms of the GNU AGPLv3 license.
import unittest
import sys
2017-12-07 22:33:32 +00:00
2020-11-14 18:01:39 +00:00
from flask import current_app
from pony.orm import db_session
from supysonic.db import Folder, Track
2017-12-07 22:33:32 +00:00
from supysonic.managers.folder import FolderManager
from supysonic.scanner import Scanner
from .apitestbase import ApiTestBase
2019-06-29 15:25:44 +00:00
2017-12-07 22:33:32 +00:00
class TranscodingTestCase(ApiTestBase):
def setUp(self):
2020-11-22 15:12:14 +00:00
super().setUp()
2017-12-07 22:33:32 +00:00
2017-12-23 21:59:04 +00:00
with db_session:
2019-06-29 15:25:44 +00:00
folder = FolderManager.add("Folder", "tests/assets/folder")
scanner = Scanner()
2019-06-29 15:25:44 +00:00
scanner.queue_folder("Folder")
2019-05-11 15:13:58 +00:00
scanner.run()
2017-12-07 22:33:32 +00:00
2017-12-23 21:59:04 +00:00
self.trackid = Track.get().id
2017-12-07 22:33:32 +00:00
def _stream(self, **kwargs):
2019-06-29 15:25:44 +00:00
kwargs.update(
{"u": "alice", "p": "Alic3", "c": "tests", "v": "1.9.0", "id": self.trackid}
)
2017-12-07 22:33:32 +00:00
2019-06-29 15:25:44 +00:00
rv = self.client.get("/rest/stream.view", query_string=kwargs)
2017-12-07 22:33:32 +00:00
self.assertEqual(rv.status_code, 200)
2019-06-29 15:25:44 +00:00
self.assertFalse(rv.mimetype.startswith("text/"))
2017-12-07 22:33:32 +00:00
return rv
def test_no_transcoding_available(self):
2019-06-29 15:25:44 +00:00
self._make_request("stream", {"id": self.trackid, "format": "wat"}, error=0)
2017-12-07 22:33:32 +00:00
@unittest.skipIf(
sys.platform == "win32",
"Can't test transcoding on Windows because of a lack of simple commandline tools",
)
2017-12-07 22:33:32 +00:00
def test_direct_transcode(self):
2019-06-29 15:25:44 +00:00
rv = self._stream(maxBitRate=96, estimateContentLength="true")
2020-11-14 18:01:39 +00:00
self.assertIn(b"tests/assets/folder/silence.mp3", rv.data)
self.assertTrue(rv.data.endswith(b"96"))
self.assertIn("Content-Length", rv.headers)
self.assertEqual(rv.content_length, 48000) # 4s at 96kbps
2017-12-07 22:33:32 +00:00
@unittest.skipIf(
sys.platform == "win32",
"Can't test transcoding on Windows because of a lack of simple commandline tools",
)
2017-12-07 22:33:32 +00:00
def test_decode_encode(self):
2019-06-29 15:25:44 +00:00
rv = self._stream(format="cat")
2020-11-14 18:01:39 +00:00
self.assertEqual(rv.data, b"Pushing out some mp3 data...")
2017-12-07 22:33:32 +00:00
2019-06-29 15:25:44 +00:00
rv = self._stream(format="md5")
2020-11-14 18:01:39 +00:00
self.assertTrue(rv.data.startswith(b"dbb16c0847e5d8c3b1867604828cb50b"))
@unittest.skipIf(
sys.platform == "win32",
"Can't test transcoding on Windows because of a lack of simple commandline tools",
)
def test_mostly_transcoded_cached(self):
# See https://github.com/spl0k/supysonic/issues/202
rv = self._stream(maxBitRate=96, estimateContentLength="true", format="rnd")
read = 0
it = iter(rv.response)
# Read up to the estimated length
2020-11-14 18:01:39 +00:00
while read < 48000:
read += len(next(it))
rv.response.close()
rv.close()
key = "{}-96.rnd".format(self.trackid)
with self.app_context():
self.assertTrue(current_app.transcode_cache.has(key))
self.assertEqual(current_app.transcode_cache.size, 52000)
@unittest.skipIf(
sys.platform == "win32",
"Can't test transcoding on Windows because of a lack of simple commandline tools",
)
def test_partly_transcoded_cached(self):
rv = self._stream(maxBitRate=96, estimateContentLength="true", format="rnd")
# read one check of data then close the connection
next(iter(rv.response))
rv.response.close()
rv.close()
key = "{}-96.rnd".format(self.trackid)
with self.app_context():
self.assertFalse(current_app.transcode_cache.has(key))
self.assertEqual(current_app.transcode_cache.size, 0)
2017-12-07 22:33:32 +00:00
@unittest.skipIf(
sys.platform == "win32",
"Can't test transcoding on Windows because of a lack of simple commandline tools",
)
def test_last_chunk_close_transcoded_cached(self):
rv = self._stream(maxBitRate=96, estimateContentLength="true", format="rnd")
read = 0
it = iter(rv.response)
# Read up to the last chunk of data but keep the generator "alive"
while read < 52000:
read += len(next(it))
rv.response.close()
rv.close()
key = "{}-96.rnd".format(self.trackid)
with self.app_context():
self.assertTrue(current_app.transcode_cache.has(key))
self.assertEqual(current_app.transcode_cache.size, 52000)
2017-12-07 22:33:32 +00:00
2019-06-29 15:25:44 +00:00
if __name__ == "__main__":
unittest.main()