DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Implementation

Mercurial (d38398e5144e)

VCS Links

OpusMetadata

OpusTrackEncoder

Macros

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-*/
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef OpusTrackEncoder_h_
#define OpusTrackEncoder_h_

#include <stdint.h>
#include <speex/speex_resampler.h>
#include "TrackEncoder.h"

struct OpusEncoder;

namespace mozilla {

// Opus meta data structure
class OpusMetadata : public TrackMetadataBase
{
public:
  // The ID Header of OggOpus. refer to http://wiki.xiph.org/OggOpus.
  nsTArray<uint8_t> mIdHeader;
  // The Comment Header of OggOpus.
  nsTArray<uint8_t> mCommentHeader;
  int32_t mChannels;
  float mSamplingFrequency;
  MetadataKind GetKind() const override { return METADATA_OPUS; }
};

class OpusTrackEncoder : public AudioTrackEncoder
{
public:
  OpusTrackEncoder();
  virtual ~OpusTrackEncoder();

  already_AddRefed<TrackMetadataBase> GetMetadata() override;

  nsresult GetEncodedTrack(EncodedFrameContainer& aData) override;

protected:
  int GetPacketDuration() override;

  nsresult Init(int aChannels, int aSamplingRate) override;

  /**
   * Get the samplerate of the data to be fed to the Opus encoder. This might be
   * different from the input samplerate if resampling occurs.
   */
  int GetOutputSampleRate();

private:
  /**
   * The Opus encoder from libopus.
   */
  OpusEncoder* mEncoder;

  /**
   * A local segment queue which takes the raw data out from mRawSegment in the
   * call of GetEncodedTrack(). Opus encoder only accepts GetPacketDuration()
   * samples from mSourceSegment every encoding cycle, thus it needs to be
   * global in order to store the leftover segments taken from mRawSegment.
   */
  AudioSegment mSourceSegment;

  /**
   * Total samples of delay added by codec, can be queried by the encoder. From
   * the perspective of decoding, real data begins this many samples late, so
   * the encoder needs to append this many null samples to the end of stream,
   * in order to align the time of input and output.
   */
  int mLookahead;

  /**
   * If the input sample rate does not divide 48kHz evenly, the input data are
   * resampled.
   */
  SpeexResamplerState* mResampler;

  /**
   * Store the resampled frames that don't fit into an Opus packet duration.
   * They will be prepended to the resampled frames next encoding cycle.
   */
  nsTArray<AudioDataValue> mResampledLeftover;

  // TimeStamp in microseconds.
  uint64_t mOutputTimeStamp;
};

} // namespace mozilla

#endif