|
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 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 |
|
26 MetadataKind GetKind() const MOZ_OVERRIDE { return METADATA_OPUS; } |
|
27 }; |
|
28 |
|
29 class OpusTrackEncoder : public AudioTrackEncoder |
|
30 { |
|
31 public: |
|
32 OpusTrackEncoder(); |
|
33 virtual ~OpusTrackEncoder(); |
|
34 |
|
35 already_AddRefed<TrackMetadataBase> GetMetadata() MOZ_OVERRIDE; |
|
36 |
|
37 nsresult GetEncodedTrack(EncodedFrameContainer& aData) MOZ_OVERRIDE; |
|
38 |
|
39 protected: |
|
40 int GetPacketDuration(); |
|
41 |
|
42 nsresult Init(int aChannels, int aSamplingRate) MOZ_OVERRIDE; |
|
43 |
|
44 /** |
|
45 * Get the samplerate of the data to be fed to the Opus encoder. This might be |
|
46 * different from the input samplerate if resampling occurs. |
|
47 */ |
|
48 int GetOutputSampleRate(); |
|
49 |
|
50 private: |
|
51 /** |
|
52 * The Opus encoder from libopus. |
|
53 */ |
|
54 OpusEncoder* mEncoder; |
|
55 |
|
56 /** |
|
57 * A local segment queue which takes the raw data out from mRawSegment in the |
|
58 * call of GetEncodedTrack(). Opus encoder only accepts GetPacketDuration() |
|
59 * samples from mSourceSegment every encoding cycle, thus it needs to be |
|
60 * global in order to store the leftover segments taken from mRawSegment. |
|
61 */ |
|
62 AudioSegment mSourceSegment; |
|
63 |
|
64 /** |
|
65 * Total samples of delay added by codec, can be queried by the encoder. From |
|
66 * the perspective of decoding, real data begins this many samples late, so |
|
67 * the encoder needs to append this many null samples to the end of stream, |
|
68 * in order to align the time of input and output. |
|
69 */ |
|
70 int mLookahead; |
|
71 |
|
72 /** |
|
73 * If the input sample rate does not divide 48kHz evenly, the input data are |
|
74 * resampled. |
|
75 */ |
|
76 SpeexResamplerState* mResampler; |
|
77 |
|
78 /** |
|
79 * Store the resampled frames that don't fit into an Opus packet duration. |
|
80 * They will be prepended to the resampled frames next encoding cycle. |
|
81 */ |
|
82 nsTArray<AudioDataValue> mResampledLeftover; |
|
83 }; |
|
84 |
|
85 } |
|
86 #endif |