ascii-chat
Binary v0.15.83-53-gb711d8720
Library lib/v0.3.2-701-gb711d8720
Video chat in your terminal
Loading...
Searching...
No Matches
OpusCodecBinding Class Reference

Public Member Functions

 OpusCodecBinding (int sample_rate, int channels, int bitrate, unsigned max_packet)
 ~OpusCodecBinding ()
 OpusCodecBinding (const OpusCodecBinding &)=delete
OpusCodecBinding & operator= (const OpusCodecBinding &)=delete
bool valid () const
NullableOpusBytes encode (PcmSamples samples)
NullablePcmSamples decode (OpusBytes packet, int frame_size)

Detailed Description

Definition at line 77 of file audio.cpp.

Constructor & Destructor Documentation

◆ OpusCodecBinding() [1/2]

OpusCodecBinding::OpusCodecBinding ( int sample_rate,
int channels,
int bitrate,
unsigned max_packet )
inline

Definition at line 79 of file audio.cpp.

79 : channels_(channels) {
80 if (opus_codec_in_use) {
81 SET_ERRNO(ERROR_AUDIO_IN_USE, "Browser Opus codec is already in use");
82 return;
83 }
84 if (sample_rate <= 0 || sample_rate > 48000 || (channels != 1 && channels != 2) || !max_packet) {
85 SET_ERRNO(ERROR_INVALID_PARAM, "Invalid Opus codec configuration");
86 return;
87 }
88 if (client_opus_encoder_init(sample_rate, channels, bitrate) != 0) {
89 SET_ERRNO(ERROR_AUDIO, "Cannot initialize browser Opus encoder");
90 return;
91 }
92 if (client_opus_decoder_init(sample_rate, channels) != 0) {
94 SET_ERRNO(ERROR_AUDIO, "Cannot initialize browser Opus decoder");
95 return;
96 }
97 const size_t frame = (static_cast<size_t>(sample_rate) * 12 + 99) / 100;
98 pcm_.resize(frame * channels);
99 encoded_.resize(max_packet);
100 input_.resize(65535);
101 initialized_ = true;
102 opus_codec_in_use = true;
103 }
#define SET_ERRNO(code,...)
@ ERROR_AUDIO
Definition error_codes.h:62
@ ERROR_INVALID_PARAM
@ ERROR_AUDIO_IN_USE
Definition error_codes.h:63
int client_opus_decoder_init(int sample_rate, int channels)
void client_opus_encoder_cleanup(void)
int client_opus_encoder_init(int sample_rate, int channels, int bitrate)

References client_opus_decoder_init(), client_opus_encoder_cleanup(), client_opus_encoder_init(), ERROR_AUDIO, ERROR_AUDIO_IN_USE, ERROR_INVALID_PARAM, and SET_ERRNO.

Referenced by operator=(), and OpusCodecBinding().

◆ ~OpusCodecBinding()

OpusCodecBinding::~OpusCodecBinding ( )
inline

Definition at line 104 of file audio.cpp.

104 {
105 if (initialized_) {
108 opus_codec_in_use = false;
109 }
110 }
void client_opus_decoder_cleanup(void)

References client_opus_decoder_cleanup(), and client_opus_encoder_cleanup().

◆ OpusCodecBinding() [2/2]

OpusCodecBinding::OpusCodecBinding ( const OpusCodecBinding & )
delete

References OpusCodecBinding().

Member Function Documentation

◆ decode()

NullablePcmSamples OpusCodecBinding::decode ( OpusBytes packet,
int frame_size )
inline

Definition at line 139 of file audio.cpp.

139 {
140 const size_t count = packet["length"].as<size_t>();
141 if (!initialized_) {
142 SET_ERRNO(ERROR_INVALID_STATE, "Opus codec is not initialized");
143 return NullablePcmSamples(emscripten::val::null());
144 }
145 if (count > input_.size() || frame_size <= 0 || static_cast<size_t>(frame_size) * channels_ > pcm_.size()) {
146 SET_ERRNO(ERROR_INVALID_PARAM, "Invalid Opus packet or output frame size");
147 return NullablePcmSamples(emscripten::val::null());
148 }
149 if (count)
150 emscripten::val(emscripten::typed_memory_view(count, input_.data())).call<void>("set", packet);
151 const int samples = client_opus_decode(input_.data(), static_cast<int>(count), pcm_.data(), frame_size, 0);
152 if (samples < 0) {
153 SET_ERRNO(ERROR_AUDIO, "Browser Opus decoding failed");
154 return NullablePcmSamples(emscripten::val::null());
155 }
156 const size_t output_count = static_cast<size_t>(samples) * channels_;
157 emscripten::val result = emscripten::val::global("Int16Array").new_(output_count);
158 result.call<void>("set", emscripten::val(emscripten::typed_memory_view(output_count, pcm_.data())));
159 return NullablePcmSamples(result);
160 }
@ ERROR_INVALID_STATE
int client_opus_decode(const uint8_t *opus_data, int opus_bytes, int16_t *pcm_data, int frame_size, int decode_fec)

References client_opus_decode(), ERROR_AUDIO, ERROR_INVALID_PARAM, ERROR_INVALID_STATE, and SET_ERRNO.

Referenced by EMSCRIPTEN_BINDINGS().

◆ encode()

NullableOpusBytes OpusCodecBinding::encode ( PcmSamples samples)
inline

Definition at line 117 of file audio.cpp.

117 {
118 const size_t count = samples["length"].as<size_t>();
119 if (!initialized_) {
120 SET_ERRNO(ERROR_INVALID_STATE, "Opus codec is not initialized");
121 return NullableOpusBytes(emscripten::val::null());
122 }
123 if (!count || count > pcm_.size() || count % channels_) {
124 SET_ERRNO(ERROR_INVALID_PARAM, "Invalid Opus input sample count: %zu", count);
125 return NullableOpusBytes(emscripten::val::null());
126 }
127 emscripten::val view = emscripten::val(emscripten::typed_memory_view(count, pcm_.data()));
128 view.call<void>("set", samples);
129 const int size = client_opus_encode(pcm_.data(), static_cast<int>(count / channels_), encoded_.data(),
130 static_cast<int>(encoded_.size()));
131 if (size < 0) {
132 SET_ERRNO(ERROR_AUDIO, "Browser Opus encoding failed");
133 return NullableOpusBytes(emscripten::val::null());
134 }
135 emscripten::val result = emscripten::val::global("Uint8Array").new_(size);
136 result.call<void>("set", emscripten::val(emscripten::typed_memory_view(size, encoded_.data())));
137 return NullableOpusBytes(result);
138 }
int client_opus_encode(const int16_t *pcm_data, int frame_size, uint8_t *opus_data, int max_opus_bytes)

References client_opus_encode(), ERROR_AUDIO, ERROR_INVALID_PARAM, ERROR_INVALID_STATE, and SET_ERRNO.

Referenced by EMSCRIPTEN_BINDINGS().

◆ operator=()

OpusCodecBinding & OpusCodecBinding::operator= ( const OpusCodecBinding & )
delete

References OpusCodecBinding().

◆ valid()

bool OpusCodecBinding::valid ( ) const
inline

Definition at line 113 of file audio.cpp.

113 {
114 return initialized_;
115 }

Referenced by EMSCRIPTEN_BINDINGS().


The documentation for this class was generated from the following file: