27
28
* shall include the source code for the parts of OpenSSL used as well
28
29
* as that of the covered work.
31
#include "opus_wrapper.h"
32
#include "sfl_types.h"
31
33
#include <stdexcept>
32
34
#include <iostream>
34
static const int Opus_PAYLOAD_TYPE = 104; // dynamic payload type, out of range of video (96-99)
36
Opus::Opus() : sfl::AudioCodec(Opus_PAYLOAD_TYPE, "OPUS", CLOCK_RATE, FRAME_SIZE, CHANNELS),
37
Opus::Opus() : sfl::AudioCodec(PAYLOAD_TYPE, "opus", CLOCK_RATE, FRAME_SIZE, CHANNELS),
40
hasDynamicPayload_ = true;
43
encoder_ = opus_encoder_create(CLOCK_RATE, CHANNELS, OPUS_APPLICATION_VOIP, &err);
45
throw std::runtime_error("opus: could not create encoder");
47
decoder_ = opus_decoder_create(CLOCK_RATE, CHANNELS, &err);
49
throw std::runtime_error("opus: could not create decoder");
41
hasDynamicPayload_ = true;
44
encoder_ = opus_encoder_create(CLOCK_RATE, CHANNELS, OPUS_APPLICATION_VOIP, &err);
47
throw std::runtime_error("opus: could not create encoder");
49
decoder_ = opus_decoder_create(CLOCK_RATE, CHANNELS, &err);
52
throw std::runtime_error("opus: could not create decoder");
55
58
opus_encoder_destroy(encoder_);
57
61
opus_decoder_destroy(decoder_);
60
int Opus::decode(short *dst, unsigned char *buf, size_t buffer_size)
62
return opus_decode(decoder_, buf, buffer_size, dst, FRAME_SIZE, 0);
65
int Opus::encode(unsigned char *dst, short *src, size_t buffer_size)
67
return opus_encode(encoder_, src, FRAME_SIZE, dst, buffer_size * 2);
70
// Reference: http://tools.ietf.org/html/draft-spittka-payload-rtp-opus-03#section-6.2
71
// "The RTP clock rate in "a=rtpmap" MUST be 48000..."
72
uint32_t Opus::getSDPClockRate() const
77
// "...and the number of channels MUST be 2."
79
Opus::getSDPChannels() const
84
int Opus::decode(SFLAudioSample *dst, unsigned char *buf, size_t buffer_size)
86
const int ret = opus_decode(decoder_, buf, buffer_size, dst, FRAME_SIZE, 0);
88
std::cerr << opus_strerror(ret) << std::endl;
92
int Opus::encode(unsigned char *dst, SFLAudioSample *src, size_t buffer_size)
94
const int ret = opus_encode(encoder_, src, FRAME_SIZE, dst, buffer_size * 2);
96
std::cerr << opus_strerror(ret) << std::endl;
100
int Opus::decode(std::vector<std::vector<SFLAudioSample> > &dst, unsigned char *buf, size_t buffer_size)
102
if (buf == NULL) return 0;
104
const int ret = opus_decode(decoder_, buf, buffer_size, dst[0].data(), sizeof(SFLAudioSample) * FRAME_SIZE, 0);
106
std::cerr << opus_strerror(ret) << std::endl;
111
int Opus::encode(unsigned char *dst, std::vector<std::vector<SFLAudioSample> > &src, size_t buffer_size)
113
if (dst == NULL) return 0;
115
const int ret = opus_encode(encoder_, src[0].data(), FRAME_SIZE, dst, buffer_size * sizeof(SFLAudioSample));
117
std::cerr << opus_strerror(ret) << std::endl;
70
123
// cppcheck-suppress unusedFunction
71
124
extern "C" sfl::AudioCodec* AUDIO_CODEC_ENTRY()