2013-05-17 15:16:36 +04:00
|
|
|
/* -*- 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 TrackEncoder_h_
|
|
|
|
#define TrackEncoder_h_
|
|
|
|
|
|
|
|
#include "mozilla/ReentrantMonitor.h"
|
|
|
|
|
|
|
|
#include "AudioSegment.h"
|
2013-11-12 06:29:09 +04:00
|
|
|
#include "EncodedFrameContainer.h"
|
2016-01-26 05:49:01 +03:00
|
|
|
#include "StreamTracks.h"
|
2013-10-25 06:59:52 +04:00
|
|
|
#include "TrackMetadataBase.h"
|
2013-12-02 11:52:54 +04:00
|
|
|
#include "VideoSegment.h"
|
2014-07-14 09:47:56 +04:00
|
|
|
#include "MediaStreamGraph.h"
|
2013-05-17 15:16:36 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Base class of AudioTrackEncoder and VideoTrackEncoder. Lifetimes managed by
|
|
|
|
* MediaEncoder. Most methods can only be called on the MediaEncoder's thread,
|
|
|
|
* but some subclass methods can be called on other threads when noted.
|
|
|
|
*
|
|
|
|
* NotifyQueuedTrackChanges is called on subclasses of this class from the
|
|
|
|
* MediaStreamGraph thread, and AppendAudioSegment/AppendVideoSegment is then
|
|
|
|
* called to store media data in the TrackEncoder. Later on, GetEncodedTrack is
|
|
|
|
* called on MediaEncoder's thread to encode and retrieve the encoded data.
|
|
|
|
*/
|
|
|
|
class TrackEncoder
|
|
|
|
{
|
|
|
|
public:
|
2014-07-07 13:33:47 +04:00
|
|
|
TrackEncoder();
|
2013-11-12 06:29:09 +04:00
|
|
|
|
2013-05-17 15:16:36 +04:00
|
|
|
virtual ~TrackEncoder() {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Notified by the same callbcak of MediaEncoder when it has received a track
|
|
|
|
* change from MediaStreamGraph. Called on the MediaStreamGraph thread.
|
|
|
|
*/
|
|
|
|
virtual void NotifyQueuedTrackChanges(MediaStreamGraph* aGraph, TrackID aID,
|
2014-09-18 09:20:43 +04:00
|
|
|
StreamTime aTrackOffset,
|
2013-05-17 15:16:36 +04:00
|
|
|
uint32_t aTrackEvents,
|
|
|
|
const MediaSegment& aQueuedMedia) = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Notified by the same callback of MediaEncoder when it has been removed from
|
|
|
|
* MediaStreamGraph. Called on the MediaStreamGraph thread.
|
|
|
|
*/
|
2014-07-14 09:47:56 +04:00
|
|
|
void NotifyEvent(MediaStreamGraph* aGraph,
|
2016-06-30 10:07:48 +03:00
|
|
|
MediaStreamGraphEvent event);
|
2013-05-17 15:16:36 +04:00
|
|
|
|
|
|
|
/**
|
2013-11-12 06:29:09 +04:00
|
|
|
* Creates and sets up meta data for a specific codec, called on the worker
|
|
|
|
* thread.
|
2013-05-17 15:16:36 +04:00
|
|
|
*/
|
2013-11-10 02:30:01 +04:00
|
|
|
virtual already_AddRefed<TrackMetadataBase> GetMetadata() = 0;
|
2013-05-17 15:16:36 +04:00
|
|
|
|
|
|
|
/**
|
2013-11-12 06:29:09 +04:00
|
|
|
* Encodes raw segments. Result data is returned in aData, and called on the
|
|
|
|
* worker thread.
|
2013-05-17 15:16:36 +04:00
|
|
|
*/
|
2013-10-25 06:59:52 +04:00
|
|
|
virtual nsresult GetEncodedTrack(EncodedFrameContainer& aData) = 0;
|
2013-11-12 06:29:09 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* True if the track encoder has encoded all source segments coming from
|
|
|
|
* MediaStreamGraph. Call on the worker thread.
|
|
|
|
*/
|
|
|
|
bool IsEncodingComplete() { return mEncodingComplete; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Notifies from MediaEncoder to cancel the encoding, and wakes up
|
|
|
|
* mReentrantMonitor if encoder is waiting on it.
|
|
|
|
*/
|
|
|
|
void NotifyCancel()
|
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter mon(mReentrantMonitor);
|
|
|
|
mCanceled = true;
|
|
|
|
mReentrantMonitor.NotifyAll();
|
|
|
|
}
|
|
|
|
|
2015-06-08 06:47:28 +03:00
|
|
|
virtual void SetBitrate(const uint32_t aBitrate) {}
|
|
|
|
|
2013-11-12 06:29:09 +04:00
|
|
|
protected:
|
|
|
|
/**
|
|
|
|
* Notifies track encoder that we have reached the end of source stream, and
|
|
|
|
* wakes up mReentrantMonitor if encoder is waiting for any source data.
|
|
|
|
*/
|
|
|
|
virtual void NotifyEndOfStream() = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A ReentrantMonitor to protect the pushing and pulling of mRawSegment which
|
|
|
|
* is declared in its subclasses, and the following flags: mInitialized,
|
|
|
|
* EndOfStream and mCanceled. The control of protection is managed by its
|
|
|
|
* subclasses.
|
|
|
|
*/
|
|
|
|
ReentrantMonitor mReentrantMonitor;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* True if the track encoder has encoded all source data.
|
|
|
|
*/
|
|
|
|
bool mEncodingComplete;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* True if flag of EOS or any form of indicating EOS has set in the codec-
|
|
|
|
* encoder.
|
|
|
|
*/
|
|
|
|
bool mEosSetInEncoder;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* True if the track encoder has initialized successfully, protected by
|
|
|
|
* mReentrantMonitor.
|
|
|
|
*/
|
|
|
|
bool mInitialized;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* True if the TrackEncoder has received an event of TRACK_EVENT_ENDED from
|
|
|
|
* MediaStreamGraph, or the MediaEncoder is removed from its source stream,
|
|
|
|
* protected by mReentrantMonitor.
|
|
|
|
*/
|
|
|
|
bool mEndOfStream;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* True if a cancellation of encoding is sent from MediaEncoder, protected by
|
|
|
|
* mReentrantMonitor.
|
|
|
|
*/
|
|
|
|
bool mCanceled;
|
2014-07-07 13:33:47 +04:00
|
|
|
|
|
|
|
// How many times we have tried to initialize the encoder.
|
2016-01-06 10:42:08 +03:00
|
|
|
uint32_t mInitCounter;
|
|
|
|
StreamTime mNotInitDuration;
|
2013-05-17 15:16:36 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
class AudioTrackEncoder : public TrackEncoder
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
AudioTrackEncoder()
|
|
|
|
: TrackEncoder()
|
|
|
|
, mChannels(0)
|
|
|
|
, mSamplingRate(0)
|
2015-06-08 06:47:28 +03:00
|
|
|
, mAudioBitrate(0)
|
2013-05-17 15:16:36 +04:00
|
|
|
{}
|
|
|
|
|
2016-01-18 06:50:29 +03:00
|
|
|
void NotifyQueuedTrackChanges(MediaStreamGraph* aGraph, TrackID aID,
|
|
|
|
StreamTime aTrackOffset,
|
|
|
|
uint32_t aTrackEvents,
|
|
|
|
const MediaSegment& aQueuedMedia) override;
|
2013-05-17 15:16:36 +04:00
|
|
|
|
2015-07-29 19:25:34 +03:00
|
|
|
template<typename T>
|
|
|
|
static
|
|
|
|
void InterleaveTrackData(nsTArray<const T*>& aInput,
|
|
|
|
int32_t aDuration,
|
|
|
|
uint32_t aOutputChannels,
|
|
|
|
AudioDataValue* aOutput,
|
|
|
|
float aVolume)
|
|
|
|
{
|
|
|
|
if (aInput.Length() < aOutputChannels) {
|
|
|
|
// Up-mix. This might make the mChannelData have more than aChannels.
|
|
|
|
AudioChannelsUpMix(&aInput, aOutputChannels, SilentChannel::ZeroChannel<T>());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aInput.Length() > aOutputChannels) {
|
|
|
|
DownmixAndInterleave(aInput, aDuration,
|
|
|
|
aVolume, aOutputChannels, aOutput);
|
|
|
|
} else {
|
|
|
|
InterleaveAndConvertBuffer(aInput.Elements(), aDuration, aVolume,
|
|
|
|
aOutputChannels, aOutput);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-07 14:05:05 +04:00
|
|
|
/**
|
|
|
|
* Interleaves the track data and stores the result into aOutput. Might need
|
|
|
|
* to up-mix or down-mix the channel data if the channels number of this chunk
|
|
|
|
* is different from aOutputChannels. The channel data from aChunk might be
|
|
|
|
* modified by up-mixing.
|
|
|
|
*/
|
|
|
|
static void InterleaveTrackData(AudioChunk& aChunk, int32_t aDuration,
|
|
|
|
uint32_t aOutputChannels,
|
|
|
|
AudioDataValue* aOutput);
|
|
|
|
|
2014-01-15 10:21:14 +04:00
|
|
|
/**
|
|
|
|
* De-interleaves the aInput data and stores the result into aOutput.
|
|
|
|
* No up-mix or down-mix operations inside.
|
|
|
|
*/
|
|
|
|
static void DeInterleaveTrackData(AudioDataValue* aInput, int32_t aDuration,
|
|
|
|
int32_t aChannels, AudioDataValue* aOutput);
|
2014-07-01 09:25:29 +04:00
|
|
|
/**
|
|
|
|
* Measure size of mRawSegment
|
|
|
|
*/
|
|
|
|
size_t SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const;
|
2014-01-15 10:21:14 +04:00
|
|
|
|
2016-01-18 06:50:29 +03:00
|
|
|
void SetBitrate(const uint32_t aBitrate) override
|
2015-06-08 06:47:28 +03:00
|
|
|
{
|
|
|
|
mAudioBitrate = aBitrate;
|
|
|
|
}
|
2013-05-17 15:16:36 +04:00
|
|
|
protected:
|
|
|
|
/**
|
|
|
|
* Number of samples per channel in a pcm buffer. This is also the value of
|
|
|
|
* frame size required by audio encoder, and mReentrantMonitor will be
|
|
|
|
* notified when at least this much data has been added to mRawSegment.
|
|
|
|
*/
|
2013-11-12 06:29:09 +04:00
|
|
|
virtual int GetPacketDuration() { return 0; }
|
2013-05-17 15:16:36 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Initializes the audio encoder. The call of this method is delayed until we
|
|
|
|
* have received the first valid track from MediaStreamGraph, and the
|
|
|
|
* mReentrantMonitor will be notified if other methods is waiting for encoder
|
|
|
|
* to be completely initialized. This method is called on the MediaStreamGraph
|
|
|
|
* thread.
|
|
|
|
*/
|
|
|
|
virtual nsresult Init(int aChannels, int aSamplingRate) = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Appends and consumes track data from aSegment, this method is called on
|
|
|
|
* the MediaStreamGraph thread. mReentrantMonitor will be notified when at
|
|
|
|
* least GetPacketDuration() data has been added to mRawSegment, wake up other
|
|
|
|
* method which is waiting for more data from mRawSegment.
|
|
|
|
*/
|
2013-11-12 06:29:09 +04:00
|
|
|
nsresult AppendAudioSegment(const AudioSegment& aSegment);
|
2013-05-17 15:16:36 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Notifies the audio encoder that we have reached the end of source stream,
|
|
|
|
* and wakes up mReentrantMonitor if encoder is waiting for more track data.
|
|
|
|
*/
|
2016-01-18 06:50:29 +03:00
|
|
|
void NotifyEndOfStream() override;
|
2013-05-17 15:16:36 +04:00
|
|
|
|
|
|
|
/**
|
2013-11-25 03:57:52 +04:00
|
|
|
* The number of channels are used for processing PCM data in the audio encoder.
|
|
|
|
* This value comes from the first valid audio chunk. If encoder can't support
|
|
|
|
* the channels in the chunk, downmix PCM stream can be performed.
|
|
|
|
* This value also be used to initialize the audio encoder.
|
2013-05-17 15:16:36 +04:00
|
|
|
*/
|
|
|
|
int mChannels;
|
|
|
|
|
|
|
|
/**
|
2013-11-12 06:29:09 +04:00
|
|
|
* The sampling rate of source audio data.
|
2013-05-17 15:16:36 +04:00
|
|
|
*/
|
2013-11-12 06:29:09 +04:00
|
|
|
int mSamplingRate;
|
2013-05-17 15:16:36 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A segment queue of audio track data, protected by mReentrantMonitor.
|
|
|
|
*/
|
2013-11-12 06:29:09 +04:00
|
|
|
AudioSegment mRawSegment;
|
2015-06-08 06:47:28 +03:00
|
|
|
|
|
|
|
uint32_t mAudioBitrate;
|
2013-05-17 15:16:36 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
class VideoTrackEncoder : public TrackEncoder
|
|
|
|
{
|
2013-12-02 11:52:54 +04:00
|
|
|
public:
|
2016-06-15 18:48:44 +03:00
|
|
|
explicit VideoTrackEncoder(TrackRate aTrackRate)
|
2013-12-02 11:52:54 +04:00
|
|
|
: TrackEncoder()
|
|
|
|
, mFrameWidth(0)
|
|
|
|
, mFrameHeight(0)
|
2014-02-21 10:27:41 +04:00
|
|
|
, mDisplayWidth(0)
|
|
|
|
, mDisplayHeight(0)
|
2016-06-15 18:48:44 +03:00
|
|
|
, mTrackRate(aTrackRate)
|
2013-12-02 11:52:54 +04:00
|
|
|
, mTotalFrameDuration(0)
|
2016-04-13 12:10:55 +03:00
|
|
|
, mLastFrameDuration(0)
|
2015-06-08 06:47:28 +03:00
|
|
|
, mVideoBitrate(0)
|
2016-06-15 18:48:44 +03:00
|
|
|
, mLastFrameTimeStamp(TimeStamp::Now())
|
|
|
|
, mFirstFrame(true)
|
2013-12-02 11:52:54 +04:00
|
|
|
{}
|
|
|
|
|
|
|
|
/**
|
2014-09-18 03:51:13 +04:00
|
|
|
* Notified by the same callback of MediaEncoder when it has received a track
|
2013-12-02 11:52:54 +04:00
|
|
|
* change from MediaStreamGraph. Called on the MediaStreamGraph thread.
|
|
|
|
*/
|
2016-01-18 06:50:29 +03:00
|
|
|
void NotifyQueuedTrackChanges(MediaStreamGraph* aGraph, TrackID aID,
|
|
|
|
StreamTime aTrackOffset,
|
|
|
|
uint32_t aTrackEvents,
|
|
|
|
const MediaSegment& aQueuedMedia) override;
|
2014-07-01 09:25:29 +04:00
|
|
|
/**
|
|
|
|
* Measure size of mRawSegment
|
|
|
|
*/
|
|
|
|
size_t SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const;
|
2013-12-02 11:52:54 +04:00
|
|
|
|
2016-01-18 06:50:29 +03:00
|
|
|
void SetBitrate(const uint32_t aBitrate) override
|
2015-06-08 06:47:28 +03:00
|
|
|
{
|
|
|
|
mVideoBitrate = aBitrate;
|
|
|
|
}
|
2016-06-15 18:48:44 +03:00
|
|
|
|
|
|
|
void Init(const VideoSegment& aSegment);
|
|
|
|
|
|
|
|
void SetCurrentFrames(const VideoSegment& aSegment);
|
|
|
|
|
|
|
|
StreamTime SecondsToMediaTime(double aS) const
|
|
|
|
{
|
|
|
|
NS_ASSERTION(0 <= aS && aS <= TRACK_TICKS_MAX/TRACK_RATE_MAX,
|
|
|
|
"Bad seconds");
|
|
|
|
return mTrackRate * aS;
|
|
|
|
}
|
|
|
|
|
2013-12-02 11:52:54 +04:00
|
|
|
protected:
|
|
|
|
/**
|
|
|
|
* Initialized the video encoder. In order to collect the value of width and
|
|
|
|
* height of source frames, this initialization is delayed until we have
|
|
|
|
* received the first valid video frame from MediaStreamGraph;
|
|
|
|
* mReentrantMonitor will be notified after it has successfully initialized,
|
|
|
|
* and this method is called on the MediaStramGraph thread.
|
|
|
|
*/
|
2014-02-21 10:27:41 +04:00
|
|
|
virtual nsresult Init(int aWidth, int aHeight, int aDisplayWidth,
|
2016-06-15 18:48:44 +03:00
|
|
|
int aDisplayHeight) = 0;
|
2013-12-02 11:52:54 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Appends source video frames to mRawSegment. We only append the source chunk
|
|
|
|
* if it is unique to mLastChunk. Called on the MediaStreamGraph thread.
|
|
|
|
*/
|
|
|
|
nsresult AppendVideoSegment(const VideoSegment& aSegment);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tells the video track encoder that we've reached the end of source stream,
|
|
|
|
* and wakes up mReentrantMonitor if encoder is waiting for more track data.
|
|
|
|
* Called on the MediaStreamGraph thread.
|
|
|
|
*/
|
2016-01-18 06:50:29 +03:00
|
|
|
void NotifyEndOfStream() override;
|
2013-12-02 11:52:54 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The width of source video frame, ceiled if the source width is odd.
|
|
|
|
*/
|
|
|
|
int mFrameWidth;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The height of source video frame, ceiled if the source height is odd.
|
|
|
|
*/
|
|
|
|
int mFrameHeight;
|
|
|
|
|
2014-02-21 10:27:41 +04:00
|
|
|
/**
|
|
|
|
* The display width of source video frame.
|
|
|
|
*/
|
|
|
|
int mDisplayWidth;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The display height of source video frame.
|
|
|
|
*/
|
|
|
|
int mDisplayHeight;
|
|
|
|
|
2013-12-02 11:52:54 +04:00
|
|
|
/**
|
|
|
|
* The track rate of source video.
|
|
|
|
*/
|
|
|
|
TrackRate mTrackRate;
|
|
|
|
|
|
|
|
/**
|
2014-09-18 09:20:43 +04:00
|
|
|
* The total duration of frames in encoded video in StreamTime, kept track of
|
2013-12-02 11:52:54 +04:00
|
|
|
* in subclasses.
|
|
|
|
*/
|
2014-09-18 09:20:43 +04:00
|
|
|
StreamTime mTotalFrameDuration;
|
2013-12-02 11:52:54 +04:00
|
|
|
|
|
|
|
/**
|
2016-04-13 12:10:55 +03:00
|
|
|
* The last unique frame and duration we've sent to track encoder,
|
|
|
|
* kept track of in subclasses.
|
2013-12-02 11:52:54 +04:00
|
|
|
*/
|
|
|
|
VideoFrame mLastFrame;
|
2016-04-13 12:10:55 +03:00
|
|
|
StreamTime mLastFrameDuration;
|
2013-05-17 15:16:36 +04:00
|
|
|
|
2013-12-02 11:52:54 +04:00
|
|
|
/**
|
|
|
|
* A segment queue of audio track data, protected by mReentrantMonitor.
|
|
|
|
*/
|
|
|
|
VideoSegment mRawSegment;
|
2015-06-08 06:47:28 +03:00
|
|
|
|
|
|
|
uint32_t mVideoBitrate;
|
2016-06-15 18:48:44 +03:00
|
|
|
|
|
|
|
private:
|
|
|
|
TimeStamp mLastFrameTimeStamp;
|
|
|
|
bool mFirstFrame;
|
2013-05-17 15:16:36 +04:00
|
|
|
};
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace mozilla
|
|
|
|
|
2013-05-17 15:16:36 +04:00
|
|
|
#endif
|