~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): Mark Purcell
  • Date: 2014-01-28 18:23:36 UTC
  • mfrom: (1.1.11)
  • mto: This revision was merged to the branch mainline in revision 24.
  • Revision ID: package-import@ubuntu.com-20140128182336-3xenud1kbnwmf3mz
* New upstream release 
  - Fixes "New Upstream Release" (Closes: #735846)
  - Fixes "Ringtone does not stop" (Closes: #727164)
  - Fixes "[sflphone-kde] crash on startup" (Closes: #718178)
  - Fixes "sflphone GUI crashes when call is hung up" (Closes: #736583)
* Build-Depends: ensure GnuTLS 2.6
  - libucommon-dev (>= 6.0.7-1.1), libccrtp-dev (>= 2.0.6-3)
  - Fixes "FTBFS Build-Depends libgnutls{26,28}-dev" (Closes: #722040)
* Fix "boost 1.49 is going away" unversioned Build-Depends: (Closes: #736746)
* Add Build-Depends: libsndfile-dev, nepomuk-core-dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
 *  Copyright (C) 2004-2012 Savoir-Faire Linux Inc.
 
2
 *  Copyright (C) 2004-2013 Savoir-Faire Linux Inc.
3
3
 *  Author:  Emmanuel Lepage <emmanuel.lepage@savoirfairelinux.com>
 
4
 *  Author: Adrien Beraud <adrien.beraud@wisdomvibes.com>
4
5
 *
5
6
 *  This program is free software; you can redistribute it and/or modify
6
7
 *  it under the terms of the GNU General Public License as published by
27
28
 *  shall include the source code for the parts of OpenSSL used as well
28
29
 *  as that of the covered work.
29
30
 */
30
 
#include "opus.h"
 
31
#include "opus_wrapper.h"
 
32
#include "sfl_types.h"
31
33
#include <stdexcept>
32
34
#include <iostream>
33
35
 
34
 
static const int Opus_PAYLOAD_TYPE = 104; // dynamic payload type, out of range of video (96-99)
35
36
 
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),
37
38
    encoder_(0),
38
39
    decoder_(0)
39
40
{
40
 
   hasDynamicPayload_ = true;
41
 
 
42
 
   int err = 0;
43
 
   encoder_ = opus_encoder_create(CLOCK_RATE, CHANNELS, OPUS_APPLICATION_VOIP, &err);
44
 
   if (err)
45
 
       throw std::runtime_error("opus: could not create encoder");
46
 
 
47
 
   decoder_ = opus_decoder_create(CLOCK_RATE, CHANNELS, &err);
48
 
   if (err)
49
 
       throw std::runtime_error("opus: could not create decoder");
 
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");
50
53
}
51
54
 
52
55
Opus::~Opus()
53
56
{
54
57
    if (encoder_)
55
58
        opus_encoder_destroy(encoder_);
 
59
 
56
60
    if (decoder_)
57
61
        opus_decoder_destroy(decoder_);
58
62
}
59
63
 
60
 
int Opus::decode(short *dst, unsigned char *buf, size_t buffer_size)
61
 
{
62
 
   return opus_decode(decoder_, buf, buffer_size, dst, FRAME_SIZE, 0);
63
 
}
64
 
 
65
 
int Opus::encode(unsigned char *dst, short *src, size_t buffer_size)
66
 
{
67
 
   return opus_encode(encoder_, src, FRAME_SIZE, dst, buffer_size * 2);
68
 
}
 
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
 
69
122
 
70
123
// cppcheck-suppress unusedFunction
71
124
extern "C" sfl::AudioCodec* AUDIO_CODEC_ENTRY()