2014-10-16 11:26:24 +00:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
|
|
|
|
|
*
|
|
|
|
|
* Use of this source code is governed by a BSD-style license
|
|
|
|
|
* that can be found in the LICENSE file in the root of the source
|
|
|
|
|
* tree. An additional intellectual property rights grant can be found
|
|
|
|
|
* in the file PATENTS. All contributing project authors may
|
|
|
|
|
* be found in the AUTHORS file in the root of the source tree.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#ifndef WEBRTC_MODULES_AUDIO_CODING_CODECS_AUDIO_ENCODER_H_
|
|
|
|
|
#define WEBRTC_MODULES_AUDIO_CODING_CODECS_AUDIO_ENCODER_H_
|
|
|
|
|
|
|
|
|
|
#include <algorithm>
|
2014-12-16 13:41:36 +00:00
|
|
|
#include <vector>
|
2014-10-16 11:26:24 +00:00
|
|
|
|
2015-11-06 01:21:35 -08:00
|
|
|
#include "webrtc/base/array_view.h"
|
2016-03-01 00:41:31 -08:00
|
|
|
#include "webrtc/base/buffer.h"
|
|
|
|
|
#include "webrtc/base/deprecation.h"
|
2014-10-16 11:26:24 +00:00
|
|
|
#include "webrtc/typedefs.h"
|
|
|
|
|
|
|
|
|
|
namespace webrtc {
|
|
|
|
|
|
2016-10-06 07:13:54 -07:00
|
|
|
class Clock;
|
|
|
|
|
|
2014-10-16 11:26:24 +00:00
|
|
|
// This is the interface class for encoders in AudioCoding module. Each codec
|
2014-12-16 13:41:36 +00:00
|
|
|
// type must have an implementation of this class.
|
2014-10-16 11:26:24 +00:00
|
|
|
class AudioEncoder {
|
|
|
|
|
public:
|
2016-05-16 07:34:24 -07:00
|
|
|
// Used for UMA logging of codec usage. The same codecs, with the
|
|
|
|
|
// same values, must be listed in
|
|
|
|
|
// src/tools/metrics/histograms/histograms.xml in chromium to log
|
|
|
|
|
// correct values.
|
|
|
|
|
enum class CodecType {
|
|
|
|
|
kOther = 0, // Codec not specified, and/or not listed in this enum
|
|
|
|
|
kOpus = 1,
|
|
|
|
|
kIsac = 2,
|
|
|
|
|
kPcmA = 3,
|
|
|
|
|
kPcmU = 4,
|
|
|
|
|
kG722 = 5,
|
|
|
|
|
kIlbc = 6,
|
|
|
|
|
|
|
|
|
|
// Number of histogram bins in the UMA logging of codec types. The
|
|
|
|
|
// total number of different codecs that are logged cannot exceed this
|
|
|
|
|
// number.
|
|
|
|
|
kMaxLoggedAudioCodecTypes
|
|
|
|
|
};
|
|
|
|
|
|
2014-12-16 13:41:36 +00:00
|
|
|
struct EncodedInfoLeaf {
|
2015-09-08 05:57:53 -07:00
|
|
|
size_t encoded_bytes = 0;
|
|
|
|
|
uint32_t encoded_timestamp = 0;
|
|
|
|
|
int payload_type = 0;
|
|
|
|
|
bool send_even_if_empty = false;
|
|
|
|
|
bool speech = true;
|
2016-05-16 07:34:24 -07:00
|
|
|
CodecType encoder_type = CodecType::kOther;
|
2014-12-01 14:44:50 +00:00
|
|
|
};
|
|
|
|
|
|
2014-12-16 13:41:36 +00:00
|
|
|
// This is the main struct for auxiliary encoding information. Each encoded
|
|
|
|
|
// packet should be accompanied by one EncodedInfo struct, containing the
|
|
|
|
|
// total number of |encoded_bytes|, the |encoded_timestamp| and the
|
|
|
|
|
// |payload_type|. If the packet contains redundant encodings, the |redundant|
|
|
|
|
|
// vector will be populated with EncodedInfoLeaf structs. Each struct in the
|
|
|
|
|
// vector represents one encoding; the order of structs in the vector is the
|
|
|
|
|
// same as the order in which the actual payloads are written to the byte
|
|
|
|
|
// stream. When EncoderInfoLeaf structs are present in the vector, the main
|
|
|
|
|
// struct's |encoded_bytes| will be the sum of all the |encoded_bytes| in the
|
|
|
|
|
// vector.
|
|
|
|
|
struct EncodedInfo : public EncodedInfoLeaf {
|
|
|
|
|
EncodedInfo();
|
2016-04-19 03:03:23 -07:00
|
|
|
EncodedInfo(const EncodedInfo&);
|
2016-04-22 04:59:31 -07:00
|
|
|
EncodedInfo(EncodedInfo&&);
|
2014-12-16 13:41:36 +00:00
|
|
|
~EncodedInfo();
|
2016-04-22 04:59:31 -07:00
|
|
|
EncodedInfo& operator=(const EncodedInfo&);
|
|
|
|
|
EncodedInfo& operator=(EncodedInfo&&);
|
2014-12-16 13:41:36 +00:00
|
|
|
|
|
|
|
|
std::vector<EncodedInfoLeaf> redundant;
|
|
|
|
|
};
|
|
|
|
|
|
2015-09-08 05:57:53 -07:00
|
|
|
virtual ~AudioEncoder() = default;
|
2014-10-16 11:26:24 +00:00
|
|
|
|
2015-09-08 05:57:53 -07:00
|
|
|
// Returns the input sample rate in Hz and the number of input channels.
|
|
|
|
|
// These are constants set at instantiation time.
|
|
|
|
|
virtual int SampleRateHz() const = 0;
|
Convert channel counts to size_t.
IIRC, this was originally requested by ajm during review of the other size_t conversions I did over the past year, and I agreed it made sense, but wanted to do it separately since those changes were already gargantuan.
BUG=chromium:81439
TEST=none
R=henrik.lundin@webrtc.org, henrika@webrtc.org, kjellander@webrtc.org, minyue@webrtc.org, perkj@webrtc.org, solenberg@webrtc.org, stefan@webrtc.org, tina.legrand@webrtc.org
Review URL: https://codereview.webrtc.org/1316523002 .
Cr-Commit-Position: refs/heads/master@{#11229}
2016-01-12 16:26:35 -08:00
|
|
|
virtual size_t NumChannels() const = 0;
|
2015-09-08 05:57:53 -07:00
|
|
|
|
|
|
|
|
// Returns the rate at which the RTP timestamps are updated. The default
|
|
|
|
|
// implementation returns SampleRateHz().
|
2015-02-18 12:00:32 +00:00
|
|
|
virtual int RtpTimestampRateHz() const;
|
2015-01-27 18:24:45 +00:00
|
|
|
|
2014-10-29 08:38:50 +00:00
|
|
|
// Returns the number of 10 ms frames the encoder will put in the next
|
|
|
|
|
// packet. This value may only change when Encode() outputs a packet; i.e.,
|
|
|
|
|
// the encoder may vary the number of 10 ms frames from packet to packet, but
|
|
|
|
|
// it must decide the length of the next packet no later than when outputting
|
|
|
|
|
// the preceding packet.
|
Update a ton of audio code to use size_t more correctly and in general reduce
use of int16_t/uint16_t.
This is the upshot of a recommendation by henrik.lundin and kwiberg on an original small change ( https://webrtc-codereview.appspot.com/42569004/#ps1 ) to stop using int16_t just because values could fit in it, and is similar in nature to a previous "mass change to use size_t more" ( https://webrtc-codereview.appspot.com/23129004/ ) which also needed to be split up for review but to land all at once, since, like adding "const", such changes tend to cause a lot of transitive effects.
This was be reviewed and approved in pieces:
https://codereview.webrtc.org/1224093003
https://codereview.webrtc.org/1224123002
https://codereview.webrtc.org/1224163002
https://codereview.webrtc.org/1225133003
https://codereview.webrtc.org/1225173002
https://codereview.webrtc.org/1227163003
https://codereview.webrtc.org/1227203003
https://codereview.webrtc.org/1227213002
https://codereview.webrtc.org/1227893002
https://codereview.webrtc.org/1228793004
https://codereview.webrtc.org/1228803003
https://codereview.webrtc.org/1228823002
https://codereview.webrtc.org/1228823003
https://codereview.webrtc.org/1228843002
https://codereview.webrtc.org/1230693002
https://codereview.webrtc.org/1231713002
The change is being landed as TBR to all the folks who reviewed the above.
BUG=chromium:81439
TEST=none
R=andrew@webrtc.org, pbos@webrtc.org
TBR=aluebs, andrew, asapersson, henrika, hlundin, jan.skoglund, kwiberg, minyue, pbos, pthatcher
Review URL: https://codereview.webrtc.org/1230503003 .
Cr-Commit-Position: refs/heads/master@{#9768}
2015-08-24 14:52:23 -07:00
|
|
|
virtual size_t Num10MsFramesInNextPacket() const = 0;
|
2014-10-16 11:26:24 +00:00
|
|
|
|
2014-12-08 21:15:55 +00:00
|
|
|
// Returns the maximum value that can be returned by
|
|
|
|
|
// Num10MsFramesInNextPacket().
|
Update a ton of audio code to use size_t more correctly and in general reduce
use of int16_t/uint16_t.
This is the upshot of a recommendation by henrik.lundin and kwiberg on an original small change ( https://webrtc-codereview.appspot.com/42569004/#ps1 ) to stop using int16_t just because values could fit in it, and is similar in nature to a previous "mass change to use size_t more" ( https://webrtc-codereview.appspot.com/23129004/ ) which also needed to be split up for review but to land all at once, since, like adding "const", such changes tend to cause a lot of transitive effects.
This was be reviewed and approved in pieces:
https://codereview.webrtc.org/1224093003
https://codereview.webrtc.org/1224123002
https://codereview.webrtc.org/1224163002
https://codereview.webrtc.org/1225133003
https://codereview.webrtc.org/1225173002
https://codereview.webrtc.org/1227163003
https://codereview.webrtc.org/1227203003
https://codereview.webrtc.org/1227213002
https://codereview.webrtc.org/1227893002
https://codereview.webrtc.org/1228793004
https://codereview.webrtc.org/1228803003
https://codereview.webrtc.org/1228823002
https://codereview.webrtc.org/1228823003
https://codereview.webrtc.org/1228843002
https://codereview.webrtc.org/1230693002
https://codereview.webrtc.org/1231713002
The change is being landed as TBR to all the folks who reviewed the above.
BUG=chromium:81439
TEST=none
R=andrew@webrtc.org, pbos@webrtc.org
TBR=aluebs, andrew, asapersson, henrika, hlundin, jan.skoglund, kwiberg, minyue, pbos, pthatcher
Review URL: https://codereview.webrtc.org/1230503003 .
Cr-Commit-Position: refs/heads/master@{#9768}
2015-08-24 14:52:23 -07:00
|
|
|
virtual size_t Max10MsFramesInAPacket() const = 0;
|
2015-06-18 14:58:34 +02:00
|
|
|
|
|
|
|
|
// Returns the current target bitrate in bits/s. The value -1 means that the
|
|
|
|
|
// codec adapts the target automatically, and a current target cannot be
|
|
|
|
|
// provided.
|
|
|
|
|
virtual int GetTargetBitrate() const = 0;
|
2014-12-08 21:15:55 +00:00
|
|
|
|
2015-09-08 05:57:53 -07:00
|
|
|
// Accepts one 10 ms block of input audio (i.e., SampleRateHz() / 100 *
|
|
|
|
|
// NumChannels() samples). Multi-channel audio must be sample-interleaved.
|
2016-03-01 00:41:31 -08:00
|
|
|
// The encoder appends zero or more bytes of output to |encoded| and returns
|
|
|
|
|
// additional encoding information. Encode() checks some preconditions, calls
|
2016-03-04 00:54:32 -08:00
|
|
|
// EncodeImpl() which does the actual work, and then checks some
|
2016-03-01 00:41:31 -08:00
|
|
|
// postconditions.
|
2015-09-08 05:57:53 -07:00
|
|
|
EncodedInfo Encode(uint32_t rtp_timestamp,
|
2015-11-06 01:21:35 -08:00
|
|
|
rtc::ArrayView<const int16_t> audio,
|
2016-03-01 00:41:31 -08:00
|
|
|
rtc::Buffer* encoded);
|
|
|
|
|
|
2015-09-08 05:57:53 -07:00
|
|
|
// Resets the encoder to its starting state, discarding any input that has
|
|
|
|
|
// been fed to the encoder but not yet emitted in a packet.
|
2015-05-07 12:35:12 +02:00
|
|
|
virtual void Reset() = 0;
|
|
|
|
|
|
2015-09-08 05:57:53 -07:00
|
|
|
// Enables or disables codec-internal FEC (forward error correction). Returns
|
|
|
|
|
// true if the codec was able to comply. The default implementation returns
|
|
|
|
|
// true when asked to disable FEC and false when asked to enable it (meaning
|
|
|
|
|
// that FEC isn't supported).
|
|
|
|
|
virtual bool SetFec(bool enable);
|
|
|
|
|
|
|
|
|
|
// Enables or disables codec-internal VAD/DTX. Returns true if the codec was
|
|
|
|
|
// able to comply. The default implementation returns true when asked to
|
|
|
|
|
// disable DTX and false when asked to enable it (meaning that DTX isn't
|
|
|
|
|
// supported).
|
|
|
|
|
virtual bool SetDtx(bool enable);
|
|
|
|
|
|
2016-07-27 04:53:47 -07:00
|
|
|
// Returns the status of codec-internal DTX. The default implementation always
|
|
|
|
|
// returns false.
|
|
|
|
|
virtual bool GetDtx() const;
|
|
|
|
|
|
2015-09-08 05:57:53 -07:00
|
|
|
// Sets the application mode. Returns true if the codec was able to comply.
|
|
|
|
|
// The default implementation just returns false.
|
|
|
|
|
enum class Application { kSpeech, kAudio };
|
|
|
|
|
virtual bool SetApplication(Application application);
|
|
|
|
|
|
|
|
|
|
// Tells the encoder about the highest sample rate the decoder is expected to
|
|
|
|
|
// use when decoding the bitstream. The encoder would typically use this
|
|
|
|
|
// information to adjust the quality of the encoding. The default
|
2015-12-15 14:20:24 -08:00
|
|
|
// implementation does nothing.
|
2015-09-08 23:15:33 -07:00
|
|
|
virtual void SetMaxPlaybackRate(int frequency_hz);
|
2015-09-08 05:57:53 -07:00
|
|
|
|
2016-11-30 01:18:58 -08:00
|
|
|
// Tells the encoder what the projected packet loss rate is. The rate is in
|
|
|
|
|
// the range [0.0, 1.0]. The encoder would typically use this information to
|
|
|
|
|
// adjust channel coding efforts, such as FEC. The default implementation
|
|
|
|
|
// does nothing.
|
|
|
|
|
virtual void SetProjectedPacketLossRate(double fraction);
|
|
|
|
|
|
|
|
|
|
// Tells the encoder what average bitrate we'd like it to produce. The
|
|
|
|
|
// encoder is free to adjust or disregard the given bitrate (the default
|
|
|
|
|
// implementation does the latter).
|
|
|
|
|
virtual void SetTargetBitrate(int target_bps);
|
|
|
|
|
|
2016-06-23 03:58:36 -07:00
|
|
|
// Causes this encoder to let go of any other encoders it contains, and
|
|
|
|
|
// returns a pointer to an array where they are stored (which is required to
|
|
|
|
|
// live as long as this encoder). Unless the returned array is empty, you may
|
|
|
|
|
// not call any methods on this encoder afterwards, except for the
|
|
|
|
|
// destructor. The default implementation just returns an empty array.
|
|
|
|
|
// NOTE: This method is subject to change. Do not call or override it.
|
|
|
|
|
virtual rtc::ArrayView<std::unique_ptr<AudioEncoder>>
|
|
|
|
|
ReclaimContainedEncoders();
|
|
|
|
|
|
2016-10-06 07:13:54 -07:00
|
|
|
// Enables audio network adaptor. Returns true if successful.
|
|
|
|
|
virtual bool EnableAudioNetworkAdaptor(const std::string& config_string,
|
|
|
|
|
const Clock* clock);
|
|
|
|
|
|
|
|
|
|
// Disables audio network adaptor.
|
|
|
|
|
virtual void DisableAudioNetworkAdaptor();
|
|
|
|
|
|
|
|
|
|
// Provides uplink bandwidth to this encoder to allow it to adapt.
|
|
|
|
|
virtual void OnReceivedUplinkBandwidth(int uplink_bandwidth_bps);
|
|
|
|
|
|
|
|
|
|
// Provides uplink packet loss fraction to this encoder to allow it to adapt.
|
|
|
|
|
virtual void OnReceivedUplinkPacketLossFraction(
|
|
|
|
|
float uplink_packet_loss_fraction);
|
|
|
|
|
|
|
|
|
|
// Provides target audio bitrate to this encoder to allow it to adapt.
|
|
|
|
|
virtual void OnReceivedTargetAudioBitrate(int target_audio_bitrate_bps);
|
|
|
|
|
|
|
|
|
|
// Provides RTT to this encoder to allow it to adapt.
|
|
|
|
|
virtual void OnReceivedRtt(int rtt_ms);
|
|
|
|
|
|
|
|
|
|
// To allow encoder to adapt its frame length, it must be provided the frame
|
2016-10-31 04:08:32 -07:00
|
|
|
// length range that receivers can accept.
|
2016-10-06 07:13:54 -07:00
|
|
|
virtual void SetReceiverFrameLengthRange(int min_frame_length_ms,
|
|
|
|
|
int max_frame_length_ms);
|
|
|
|
|
|
2016-03-01 00:41:31 -08:00
|
|
|
protected:
|
|
|
|
|
// Subclasses implement this to perform the actual encoding. Called by
|
2016-04-18 06:14:33 -07:00
|
|
|
// Encode().
|
2016-03-04 00:54:32 -08:00
|
|
|
virtual EncodedInfo EncodeImpl(uint32_t rtp_timestamp,
|
|
|
|
|
rtc::ArrayView<const int16_t> audio,
|
2016-04-18 06:14:33 -07:00
|
|
|
rtc::Buffer* encoded) = 0;
|
2015-05-07 12:35:12 +02:00
|
|
|
};
|
2014-10-16 11:26:24 +00:00
|
|
|
} // namespace webrtc
|
|
|
|
|
#endif // WEBRTC_MODULES_AUDIO_CODING_CODECS_AUDIO_ENCODER_H_
|