forked from roc-streaming/roc-toolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
roc-streaminggh-608 Implement rtp::parse_encoding()
- Loading branch information
Showing
5 changed files
with
207 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* Copyright (c) 2024 Roc Streaming authors | ||
* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
#include "roc_rtp/encoding.h" | ||
#include "roc_audio/sample_spec.h" | ||
|
||
namespace roc { | ||
namespace rtp { | ||
|
||
bool parse_encoding(const char* str, Encoding& result) { | ||
result = Encoding(); | ||
|
||
if (str == NULL) { | ||
roc_log(LogError, "parse encoding: string is null"); | ||
return false; | ||
} | ||
|
||
const char* sep = strchr(str, ':'); | ||
if (sep == NULL) { | ||
roc_log(LogError, | ||
"parse encoding: invalid format: missing separator, expected" | ||
" <id>:<format>/<rate>/<channels>"); | ||
return false; | ||
} | ||
|
||
if (!isdigit(*str)) { | ||
roc_log(LogError, | ||
"parse encoding: invalid format: not a number, expected" | ||
" <id>:<format>/<rate>/<channels>"); | ||
return false; | ||
} | ||
|
||
char* number_end = NULL; | ||
const unsigned long number = strtoul(str, &number_end, 10); | ||
|
||
if (number == ULONG_MAX || !number_end || number_end != sep) { | ||
roc_log(LogError, | ||
"parse encoding: invalid format: not a number, expected" | ||
" <id>:<format>/<rate>/<channels>"); | ||
return false; | ||
} | ||
|
||
if (number > UINT_MAX) { | ||
roc_log(LogError, | ||
"parse encoding: number out of range:" | ||
" value=%lu maximum=%u", | ||
number, UINT_MAX); | ||
return false; | ||
} | ||
|
||
if (!audio::parse_sample_spec(sep + 1, result.sample_spec)) { | ||
roc_log(LogError, | ||
"parse encoding: invalid format: invalid spec, expected" | ||
" <id>:<format>/<rate>/<channels>"); | ||
return false; | ||
} | ||
|
||
result.payload_type = (unsigned int)number; | ||
result.packet_flags = packet::Packet::FlagAudio; | ||
|
||
return true; | ||
} | ||
|
||
} // namespace rtp | ||
} // namespace roc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* Copyright (c) 2024 Roc Streaming authors | ||
* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
#include <CppUTest/TestHarness.h> | ||
|
||
#include "roc_rtp/encoding.h" | ||
|
||
namespace roc { | ||
namespace rtp { | ||
|
||
TEST_GROUP(encoding) {}; | ||
|
||
TEST(encoding, parse) { | ||
Encoding enc; | ||
CHECK(parse_encoding("101:s18/48000/surround4.1", enc)); | ||
|
||
CHECK_EQUAL(101, enc.payload_type); | ||
|
||
CHECK(enc.sample_spec.is_valid()); | ||
CHECK_EQUAL(audio::SampleFormat_Pcm, enc.sample_spec.sample_format()); | ||
CHECK_EQUAL(audio::PcmFormat_SInt18, enc.sample_spec.pcm_format()); | ||
CHECK_EQUAL(48000, enc.sample_spec.sample_rate()); | ||
CHECK_EQUAL(5, enc.sample_spec.num_channels()); | ||
|
||
CHECK_EQUAL(packet::Packet::FlagAudio, enc.packet_flags); | ||
CHECK(enc.new_encoder == NULL); | ||
CHECK(enc.new_decoder == NULL); | ||
} | ||
|
||
TEST(encoding, parse_errors) { | ||
Encoding enc; | ||
|
||
CHECK(!parse_encoding(":s16/44100/stereo", enc)); | ||
CHECK(!parse_encoding("101,s16/44100/stereo", enc)); | ||
CHECK(!parse_encoding("101:", enc)); | ||
CHECK(!parse_encoding("101:s16/44100/bad", enc)); | ||
CHECK(!parse_encoding(":", enc)); | ||
CHECK(!parse_encoding("", enc)); | ||
CHECK(!parse_encoding("::", enc)); | ||
CHECK(!parse_encoding("101::s16/44100/stereo", enc)); | ||
CHECK(!parse_encoding("xxx:s16/44100/stereo", enc)); | ||
CHECK(!parse_encoding("-101:s16/44100/stereo", enc)); | ||
CHECK(!parse_encoding("+101:s16/44100/stereo", enc)); | ||
CHECK(!parse_encoding("101.2:s16/44100/stereo", enc)); | ||
|
||
CHECK(parse_encoding("101:s16/44100/stereo", enc)); | ||
} | ||
|
||
} // namespace rtp | ||
} // namespace roc |