Line data Source code
1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-*/
2 : /* This Source Code Form is subject to the terms of the Mozilla Public
3 : * License, v. 2.0. If a copy of the MPL was not distributed with this file,
4 : * You can obtain one at http://mozilla.org/MPL/2.0/. */
5 :
6 : #ifndef OpusTrackEncoder_h_
7 : #define OpusTrackEncoder_h_
8 :
9 : #include <stdint.h>
10 : #include <speex/speex_resampler.h>
11 : #include "TrackEncoder.h"
12 :
13 : struct OpusEncoder;
14 :
15 : namespace mozilla {
16 :
17 : // Opus meta data structure
18 0 : class OpusMetadata : public TrackMetadataBase
19 : {
20 : public:
21 : // The ID Header of OggOpus. refer to http://wiki.xiph.org/OggOpus.
22 : nsTArray<uint8_t> mIdHeader;
23 : // The Comment Header of OggOpus.
24 : nsTArray<uint8_t> mCommentHeader;
25 : int32_t mChannels;
26 : float mSamplingFrequency;
27 0 : MetadataKind GetKind() const override { return METADATA_OPUS; }
28 : };
29 :
30 : class OpusTrackEncoder : public AudioTrackEncoder
31 : {
32 : public:
33 : OpusTrackEncoder();
34 : virtual ~OpusTrackEncoder();
35 :
36 : already_AddRefed<TrackMetadataBase> GetMetadata() override;
37 :
38 : nsresult GetEncodedTrack(EncodedFrameContainer& aData) override;
39 :
40 : protected:
41 : int GetPacketDuration() override;
42 :
43 : nsresult Init(int aChannels, int aSamplingRate) override;
44 :
45 : /**
46 : * Get the samplerate of the data to be fed to the Opus encoder. This might be
47 : * different from the input samplerate if resampling occurs.
48 : */
49 : int GetOutputSampleRate();
50 :
51 : private:
52 : /**
53 : * The Opus encoder from libopus.
54 : */
55 : OpusEncoder* mEncoder;
56 :
57 : /**
58 : * A local segment queue which takes the raw data out from mRawSegment in the
59 : * call of GetEncodedTrack(). Opus encoder only accepts GetPacketDuration()
60 : * samples from mSourceSegment every encoding cycle, thus it needs to be
61 : * global in order to store the leftover segments taken from mRawSegment.
62 : */
63 : AudioSegment mSourceSegment;
64 :
65 : /**
66 : * Total samples of delay added by codec, can be queried by the encoder. From
67 : * the perspective of decoding, real data begins this many samples late, so
68 : * the encoder needs to append this many null samples to the end of stream,
69 : * in order to align the time of input and output.
70 : */
71 : int mLookahead;
72 :
73 : /**
74 : * If the input sample rate does not divide 48kHz evenly, the input data are
75 : * resampled.
76 : */
77 : SpeexResamplerState* mResampler;
78 :
79 : /**
80 : * Store the resampled frames that don't fit into an Opus packet duration.
81 : * They will be prepended to the resampled frames next encoding cycle.
82 : */
83 : nsTArray<AudioDataValue> mResampledLeftover;
84 :
85 : // TimeStamp in microseconds.
86 : uint64_t mOutputTimeStamp;
87 : };
88 :
89 : } // namespace mozilla
90 :
91 : #endif
|