2
* Copyright (C) 2004-2013 Savoir-Faire Linux Inc.
3
* Author: Emmanuel Lepage <emmanuel.lepage@savoirfairelinux.com>
4
* Author: Adrien Beraud <adrien.beraud@wisdomvibes.com>
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 3 of the License, or
9
* (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20
* Additional permission under GNU GPL version 3 section 7:
22
* If you modify this program, or any covered work, by linking or
23
* combining it with the OpenSSL project's OpenSSL library (or a
24
* modified version of that library), containing parts covered by the
25
* terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc.
26
* grants you additional permission to convey the resulting work.
27
* Corresponding Source for a non-source form of such a combination
28
* shall include the source code for the parts of OpenSSL used as well
29
* as that of the covered work.
31
#include "opus_wrapper.h"
32
#include "sfl_types.h"
37
Opus::Opus() : sfl::AudioCodec(PAYLOAD_TYPE, "opus", CLOCK_RATE, FRAME_SIZE, CHANNELS),
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");
58
opus_encoder_destroy(encoder_);
61
opus_decoder_destroy(decoder_);
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;
123
// cppcheck-suppress unusedFunction
124
extern "C" sfl::AudioCodec* AUDIO_CODEC_ENTRY()
128
} catch (const std::runtime_error &e) {
129
std::cerr << e.what() << std::endl;
134
// cppcheck-suppress unusedFunction
135
extern "C" void destroy(sfl::AudioCodec* a)