~ubuntu-branches/ubuntu/wily/sflphone/wily

« back to all changes in this revision

Viewing changes to daemon/src/audio/codecs/opus.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2015-01-07 14:51:16 UTC
  • mfrom: (4.3.5 sid)
  • Revision ID: package-import@ubuntu.com-20150107145116-yxnafinf4lrdvrmx
Tags: 1.4.1-0.1ubuntu1
* Merge with Debian, remaining changes:
 - Drop soprano, nepomuk build-dep
* Drop ubuntu patches, now upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
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>
5
 
 *
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.
10
 
 *
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.
15
 
 *
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.
19
 
 *
20
 
 *  Additional permission under GNU GPL version 3 section 7:
21
 
 *
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.
30
 
 */
31
 
#include "opus_wrapper.h"
32
 
#include "sfl_types.h"
33
 
#include <stdexcept>
34
 
#include <iostream>
35
 
 
36
 
 
37
 
Opus::Opus() : sfl::AudioCodec(PAYLOAD_TYPE, "opus", CLOCK_RATE, FRAME_SIZE, CHANNELS),
38
 
    encoder_(0),
39
 
    decoder_(0)
40
 
{
41
 
    hasDynamicPayload_ = true;
42
 
 
43
 
    int err = 0;
44
 
    encoder_ = opus_encoder_create(CLOCK_RATE, CHANNELS, OPUS_APPLICATION_VOIP, &err);
45
 
 
46
 
    if (err)
47
 
        throw std::runtime_error("opus: could not create encoder");
48
 
 
49
 
    decoder_ = opus_decoder_create(CLOCK_RATE, CHANNELS, &err);
50
 
 
51
 
    if (err)
52
 
        throw std::runtime_error("opus: could not create decoder");
53
 
}
54
 
 
55
 
Opus::~Opus()
56
 
{
57
 
    if (encoder_)
58
 
        opus_encoder_destroy(encoder_);
59
 
 
60
 
    if (decoder_)
61
 
        opus_decoder_destroy(decoder_);
62
 
}
63
 
 
64
 
sfl::AudioCodec *
65
 
Opus::clone()
66
 
{
67
 
    return new Opus;
68
 
}
69
 
 
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
73
 
{
74
 
    return 48000;
75
 
}
76
 
 
77
 
// "...and the number of channels MUST be 2."
78
 
const char *
79
 
Opus::getSDPChannels() const
80
 
{
81
 
    return "2";
82
 
}
83
 
 
84
 
int Opus::decode(SFLAudioSample *dst, unsigned char *buf, size_t buffer_size)
85
 
{
86
 
    const int ret = opus_decode(decoder_, buf, buffer_size, dst, FRAME_SIZE, 0);
87
 
    if (ret < 0)
88
 
        std::cerr << opus_strerror(ret) << std::endl;
89
 
    return ret;
90
 
}
91
 
 
92
 
int Opus::encode(unsigned char *dst, SFLAudioSample *src, size_t buffer_size)
93
 
{
94
 
    const int ret = opus_encode(encoder_, src, FRAME_SIZE, dst, buffer_size * 2);
95
 
    if (ret < 0)
96
 
        std::cerr << opus_strerror(ret) << std::endl;
97
 
    return ret;
98
 
}
99
 
 
100
 
int Opus::decode(std::vector<std::vector<SFLAudioSample> > &dst, unsigned char *buf, size_t buffer_size)
101
 
{
102
 
    if (buf == NULL) return 0;
103
 
 
104
 
    const int ret = opus_decode(decoder_, buf, buffer_size, dst[0].data(), sizeof(SFLAudioSample) * FRAME_SIZE, 0);
105
 
    if (ret < 0)
106
 
        std::cerr << opus_strerror(ret) << std::endl;
107
 
 
108
 
    return ret;
109
 
}
110
 
 
111
 
int Opus::encode(unsigned char *dst, std::vector<std::vector<SFLAudioSample> > &src, size_t buffer_size)
112
 
{
113
 
    if (dst == NULL) return 0;
114
 
 
115
 
    const int ret = opus_encode(encoder_, src[0].data(), FRAME_SIZE, dst, buffer_size * sizeof(SFLAudioSample));
116
 
    if (ret < 0)
117
 
        std::cerr << opus_strerror(ret) << std::endl;
118
 
 
119
 
    return ret;
120
 
}
121
 
 
122
 
 
123
 
// cppcheck-suppress unusedFunction
124
 
extern "C" sfl::AudioCodec* AUDIO_CODEC_ENTRY()
125
 
{
126
 
    try {
127
 
        return new Opus;
128
 
    } catch (const std::runtime_error &e) {
129
 
        std::cerr << e.what() << std::endl;
130
 
        return 0;
131
 
    }
132
 
}
133
 
 
134
 
// cppcheck-suppress unusedFunction
135
 
extern "C" void destroy(sfl::AudioCodec* a)
136
 
{
137
 
    delete a;
138
 
}