2013-06-04 23:29:12 -07: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/. */
|
|
|
|
#include "MediaEncoder.h"
|
|
|
|
#include "MediaDecoder.h"
|
2013-09-05 13:25:17 -07:00
|
|
|
#include "nsIPrincipal.h"
|
2013-12-09 23:24:51 -08:00
|
|
|
#include "nsMimeTypes.h"
|
|
|
|
#include "prlog.h"
|
2014-01-13 23:26:04 -08:00
|
|
|
#include "mozilla/Preferences.h"
|
2013-10-24 19:59:52 -07:00
|
|
|
|
2013-06-04 23:29:12 -07:00
|
|
|
#ifdef MOZ_OGG
|
|
|
|
#include "OggWriter.h"
|
|
|
|
#endif
|
|
|
|
#ifdef MOZ_OPUS
|
|
|
|
#include "OpusTrackEncoder.h"
|
2014-01-14 22:21:14 -08:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef MOZ_VORBIS
|
|
|
|
#include "VorbisTrackEncoder.h"
|
2013-06-04 23:29:12 -07:00
|
|
|
#endif
|
2013-12-09 23:24:51 -08:00
|
|
|
#ifdef MOZ_WEBM_ENCODER
|
|
|
|
#include "VorbisTrackEncoder.h"
|
|
|
|
#include "VP8TrackEncoder.h"
|
|
|
|
#include "WebMWriter.h"
|
|
|
|
#endif
|
|
|
|
#ifdef MOZ_OMX_ENCODER
|
|
|
|
#include "OmxTrackEncoder.h"
|
|
|
|
#include "ISOMediaWriter.h"
|
|
|
|
#endif
|
2013-06-04 23:29:12 -07:00
|
|
|
|
2013-12-09 23:24:51 -08:00
|
|
|
#ifdef LOG
|
|
|
|
#undef LOG
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef PR_LOGGING
|
|
|
|
PRLogModuleInfo* gMediaEncoderLog;
|
|
|
|
#define LOG(type, msg) PR_LOG(gMediaEncoderLog, type, msg)
|
2013-06-04 23:29:12 -07:00
|
|
|
#else
|
2013-12-09 23:24:51 -08:00
|
|
|
#define LOG(type, msg)
|
2013-06-04 23:29:12 -07:00
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaEncoder::NotifyQueuedTrackChanges(MediaStreamGraph* aGraph,
|
|
|
|
TrackID aID,
|
|
|
|
TrackRate aTrackRate,
|
|
|
|
TrackTicks aTrackOffset,
|
|
|
|
uint32_t aTrackEvents,
|
|
|
|
const MediaSegment& aQueuedMedia)
|
|
|
|
{
|
|
|
|
// Process the incoming raw track data from MediaStreamGraph, called on the
|
|
|
|
// thread of MediaStreamGraph.
|
2013-12-09 23:24:51 -08:00
|
|
|
if (mAudioEncoder && aQueuedMedia.GetType() == MediaSegment::AUDIO) {
|
2013-06-04 23:29:12 -07:00
|
|
|
mAudioEncoder->NotifyQueuedTrackChanges(aGraph, aID, aTrackRate,
|
|
|
|
aTrackOffset, aTrackEvents,
|
|
|
|
aQueuedMedia);
|
|
|
|
|
2013-12-09 23:24:51 -08:00
|
|
|
} else if (mVideoEncoder && aQueuedMedia.GetType() == MediaSegment::VIDEO) {
|
|
|
|
mVideoEncoder->NotifyQueuedTrackChanges(aGraph, aID, aTrackRate,
|
|
|
|
aTrackOffset, aTrackEvents,
|
|
|
|
aQueuedMedia);
|
2013-06-04 23:29:12 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaEncoder::NotifyRemoved(MediaStreamGraph* aGraph)
|
|
|
|
{
|
|
|
|
// In case that MediaEncoder does not receive a TRACK_EVENT_ENDED event.
|
2013-12-09 23:24:51 -08:00
|
|
|
LOG(PR_LOG_DEBUG, ("NotifyRemoved in [MediaEncoder]."));
|
|
|
|
if (mAudioEncoder) {
|
|
|
|
mAudioEncoder->NotifyRemoved(aGraph);
|
|
|
|
}
|
|
|
|
if (mVideoEncoder) {
|
|
|
|
mVideoEncoder->NotifyRemoved(aGraph);
|
|
|
|
}
|
|
|
|
|
2013-06-04 23:29:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* static */
|
|
|
|
already_AddRefed<MediaEncoder>
|
2013-12-09 23:24:51 -08:00
|
|
|
MediaEncoder::CreateEncoder(const nsAString& aMIMEType, uint8_t aTrackTypes)
|
2013-06-04 23:29:12 -07:00
|
|
|
{
|
2013-12-09 23:24:51 -08:00
|
|
|
#ifdef PR_LOGGING
|
|
|
|
if (!gMediaEncoderLog) {
|
|
|
|
gMediaEncoderLog = PR_NewLogModule("MediaEncoder");
|
|
|
|
}
|
|
|
|
#endif
|
2013-06-04 23:29:12 -07:00
|
|
|
nsAutoPtr<ContainerWriter> writer;
|
|
|
|
nsAutoPtr<AudioTrackEncoder> audioEncoder;
|
|
|
|
nsAutoPtr<VideoTrackEncoder> videoEncoder;
|
|
|
|
nsRefPtr<MediaEncoder> encoder;
|
2013-12-09 23:24:51 -08:00
|
|
|
nsString mimeType;
|
|
|
|
if (!aTrackTypes) {
|
|
|
|
LOG(PR_LOG_ERROR, ("NO TrackTypes!!!"));
|
|
|
|
return nullptr;
|
2013-06-04 23:29:12 -07:00
|
|
|
}
|
2013-12-09 23:24:51 -08:00
|
|
|
#ifdef MOZ_WEBM_ENCODER
|
2014-01-13 23:26:04 -08:00
|
|
|
else if (MediaEncoder::IsWebMEncoderEnabled() &&
|
2013-12-09 23:24:51 -08:00
|
|
|
(aMIMEType.EqualsLiteral(VIDEO_WEBM) ||
|
|
|
|
(aTrackTypes & ContainerWriter::HAS_VIDEO))) {
|
|
|
|
if (aTrackTypes & ContainerWriter::HAS_AUDIO) {
|
|
|
|
audioEncoder = new VorbisTrackEncoder();
|
|
|
|
NS_ENSURE_TRUE(audioEncoder, nullptr);
|
|
|
|
}
|
|
|
|
videoEncoder = new VP8TrackEncoder();
|
|
|
|
writer = new WebMWriter(aTrackTypes);
|
|
|
|
NS_ENSURE_TRUE(writer, nullptr);
|
|
|
|
NS_ENSURE_TRUE(videoEncoder, nullptr);
|
|
|
|
mimeType = NS_LITERAL_STRING(VIDEO_WEBM);
|
|
|
|
}
|
|
|
|
#endif //MOZ_WEBM_ENCODER
|
|
|
|
#ifdef MOZ_OMX_ENCODER
|
2014-01-13 23:26:04 -08:00
|
|
|
else if (MediaEncoder::IsOMXEncoderEnabled() &&
|
|
|
|
(aMIMEType.EqualsLiteral(VIDEO_MP4) ||
|
|
|
|
(aTrackTypes & ContainerWriter::HAS_VIDEO))) {
|
2013-12-09 23:24:51 -08:00
|
|
|
if (aTrackTypes & ContainerWriter::HAS_AUDIO) {
|
|
|
|
audioEncoder = new OmxAudioTrackEncoder();
|
|
|
|
NS_ENSURE_TRUE(audioEncoder, nullptr);
|
|
|
|
}
|
|
|
|
videoEncoder = new OmxVideoTrackEncoder();
|
|
|
|
writer = new ISOMediaWriter(aTrackTypes);
|
|
|
|
NS_ENSURE_TRUE(writer, nullptr);
|
|
|
|
NS_ENSURE_TRUE(videoEncoder, nullptr);
|
|
|
|
mimeType = NS_LITERAL_STRING(VIDEO_MP4);
|
|
|
|
}
|
|
|
|
#endif // MOZ_OMX_ENCODER
|
2013-06-04 23:29:12 -07:00
|
|
|
#ifdef MOZ_OGG
|
2013-12-09 23:24:51 -08:00
|
|
|
else if (MediaDecoder::IsOggEnabled() && MediaDecoder::IsOpusEnabled() &&
|
|
|
|
(aMIMEType.EqualsLiteral(AUDIO_OGG) ||
|
|
|
|
(aTrackTypes & ContainerWriter::HAS_AUDIO))) {
|
2013-06-04 23:29:12 -07:00
|
|
|
writer = new OggWriter();
|
|
|
|
audioEncoder = new OpusTrackEncoder();
|
2013-12-09 23:24:51 -08:00
|
|
|
NS_ENSURE_TRUE(writer, nullptr);
|
|
|
|
NS_ENSURE_TRUE(audioEncoder, nullptr);
|
|
|
|
mimeType = NS_LITERAL_STRING(AUDIO_OGG);
|
2013-06-04 23:29:12 -07:00
|
|
|
}
|
2013-12-09 23:24:51 -08:00
|
|
|
#endif // MOZ_OGG
|
|
|
|
else {
|
|
|
|
LOG(PR_LOG_ERROR, ("Can not find any encoder to record this media stream"));
|
|
|
|
return nullptr;
|
2013-06-04 23:29:12 -07:00
|
|
|
}
|
2013-12-09 23:24:51 -08:00
|
|
|
LOG(PR_LOG_DEBUG, ("Create encoder result:a[%d] v[%d] w[%d] mimeType = %s.",
|
|
|
|
audioEncoder != nullptr, videoEncoder != nullptr,
|
|
|
|
writer != nullptr, mimeType.get()));
|
2013-06-04 23:29:12 -07:00
|
|
|
encoder = new MediaEncoder(writer.forget(), audioEncoder.forget(),
|
2013-12-09 23:24:51 -08:00
|
|
|
videoEncoder.forget(), mimeType);
|
2013-06-04 23:29:12 -07:00
|
|
|
return encoder.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* GetEncodedData() runs as a state machine, starting with mState set to
|
2013-10-24 19:59:52 -07:00
|
|
|
* GET_METADDATA, the procedure should be as follow:
|
2013-06-04 23:29:12 -07:00
|
|
|
*
|
|
|
|
* While non-stop
|
2013-10-24 19:59:52 -07:00
|
|
|
* If mState is GET_METADDATA
|
|
|
|
* Get the meta data from audio/video encoder
|
|
|
|
* If a meta data is generated
|
|
|
|
* Get meta data from audio/video encoder
|
2013-06-04 23:29:12 -07:00
|
|
|
* Set mState to ENCODE_TRACK
|
2013-10-24 19:59:52 -07:00
|
|
|
* Return the final container data
|
2013-06-04 23:29:12 -07:00
|
|
|
*
|
|
|
|
* If mState is ENCODE_TRACK
|
|
|
|
* Get encoded track data from audio/video encoder
|
|
|
|
* If a packet of track data is generated
|
|
|
|
* Insert encoded track data into the container stream of writer
|
|
|
|
* If the final container data is copied to aOutput
|
|
|
|
* Return the copy of final container data
|
|
|
|
* If this is the last packet of input stream
|
|
|
|
* Set mState to ENCODE_DONE
|
|
|
|
*
|
2013-11-24 15:57:52 -08:00
|
|
|
* If mState is ENCODE_DONE or ENCODE_ERROR
|
2013-06-04 23:29:12 -07:00
|
|
|
* Stop the loop
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
MediaEncoder::GetEncodedData(nsTArray<nsTArray<uint8_t> >* aOutputBufs,
|
|
|
|
nsAString& aMIMEType)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
2013-12-19 02:13:05 -08:00
|
|
|
|
2013-06-04 23:29:12 -07:00
|
|
|
aMIMEType = mMIMEType;
|
|
|
|
|
|
|
|
bool reloop = true;
|
|
|
|
while (reloop) {
|
|
|
|
switch (mState) {
|
2013-10-24 19:59:52 -07:00
|
|
|
case ENCODE_METADDATA: {
|
2013-12-09 23:24:51 -08:00
|
|
|
LOG(PR_LOG_DEBUG, ("ENCODE_METADDATA TimeStamp = %f", GetEncodeTimeStamp()));
|
|
|
|
nsresult rv = CopyMetadataToMuxer(mAudioEncoder.get());
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
LOG(PR_LOG_ERROR, ("Error! Fail to Set Audio Metadata"));
|
2013-11-24 15:57:52 -08:00
|
|
|
break;
|
|
|
|
}
|
2013-12-09 23:24:51 -08:00
|
|
|
rv = CopyMetadataToMuxer(mVideoEncoder.get());
|
2013-06-04 23:29:12 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
2013-12-09 23:24:51 -08:00
|
|
|
LOG(PR_LOG_ERROR, ("Error! Fail to Set Video Metadata"));
|
|
|
|
break;
|
2013-06-04 23:29:12 -07:00
|
|
|
}
|
|
|
|
|
2013-10-24 19:59:52 -07:00
|
|
|
rv = mWriter->GetContainerData(aOutputBufs,
|
|
|
|
ContainerWriter::GET_HEADER);
|
|
|
|
if (NS_FAILED(rv)) {
|
2013-12-09 23:24:51 -08:00
|
|
|
LOG(PR_LOG_ERROR,("Error! writer fail to generate header!"));
|
2013-11-24 15:57:52 -08:00
|
|
|
mState = ENCODE_ERROR;
|
2013-10-24 19:59:52 -07:00
|
|
|
break;
|
2013-06-04 23:29:12 -07:00
|
|
|
}
|
2013-12-09 23:24:51 -08:00
|
|
|
LOG(PR_LOG_DEBUG, ("Finish ENCODE_METADDATA TimeStamp = %f", GetEncodeTimeStamp()));
|
2013-10-24 19:59:52 -07:00
|
|
|
mState = ENCODE_TRACK;
|
2013-06-04 23:29:12 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case ENCODE_TRACK: {
|
2013-12-09 23:24:51 -08:00
|
|
|
LOG(PR_LOG_DEBUG, ("ENCODE_TRACK TimeStamp = %f", GetEncodeTimeStamp()));
|
2013-10-24 19:59:52 -07:00
|
|
|
EncodedFrameContainer encodedData;
|
2013-12-09 23:24:51 -08:00
|
|
|
nsresult rv = NS_OK;
|
|
|
|
rv = WriteEncodedDataToMuxer(mAudioEncoder.get());
|
2013-06-04 23:29:12 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
2013-12-09 23:24:51 -08:00
|
|
|
LOG(PR_LOG_ERROR, ("Error! Fail to write audio encoder data to muxer"));
|
2013-06-04 23:29:12 -07:00
|
|
|
break;
|
|
|
|
}
|
2013-12-09 23:24:51 -08:00
|
|
|
LOG(PR_LOG_DEBUG, ("Audio encoded TimeStamp = %f", GetEncodeTimeStamp()));
|
|
|
|
rv = WriteEncodedDataToMuxer(mVideoEncoder.get());
|
2013-06-04 23:29:12 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
2013-12-09 23:24:51 -08:00
|
|
|
LOG(PR_LOG_ERROR, ("Fail to write video encoder data to muxer"));
|
2013-06-04 23:29:12 -07:00
|
|
|
break;
|
|
|
|
}
|
2013-12-09 23:24:51 -08:00
|
|
|
LOG(PR_LOG_DEBUG, ("Video encoded TimeStamp = %f", GetEncodeTimeStamp()));
|
|
|
|
// In audio only or video only case, let unavailable track's flag to be true.
|
|
|
|
bool isAudioCompleted = (mAudioEncoder && mAudioEncoder->IsEncodingComplete()) || !mAudioEncoder;
|
|
|
|
bool isVideoCompleted = (mVideoEncoder && mVideoEncoder->IsEncodingComplete()) || !mVideoEncoder;
|
2013-06-04 23:29:12 -07:00
|
|
|
rv = mWriter->GetContainerData(aOutputBufs,
|
2013-12-09 23:24:51 -08:00
|
|
|
isAudioCompleted && isVideoCompleted ?
|
2013-06-04 23:29:12 -07:00
|
|
|
ContainerWriter::FLUSH_NEEDED : 0);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
// Successfully get the copy of final container data from writer.
|
|
|
|
reloop = false;
|
|
|
|
}
|
2013-12-09 23:24:51 -08:00
|
|
|
mState = (mWriter->IsWritingComplete()) ? ENCODE_DONE : ENCODE_TRACK;
|
|
|
|
LOG(PR_LOG_DEBUG, ("END ENCODE_TRACK TimeStamp = %f "
|
|
|
|
"mState = %d aComplete %d vComplete %d",
|
|
|
|
GetEncodeTimeStamp(), mState, isAudioCompleted, isVideoCompleted));
|
2013-06-04 23:29:12 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case ENCODE_DONE:
|
2013-11-24 15:57:52 -08:00
|
|
|
case ENCODE_ERROR:
|
2013-12-09 23:24:51 -08:00
|
|
|
LOG(PR_LOG_DEBUG, ("MediaEncoder has been shutdown."));
|
2013-11-24 15:57:52 -08:00
|
|
|
mShutdown = true;
|
|
|
|
reloop = false;
|
|
|
|
break;
|
2013-06-04 23:29:12 -07:00
|
|
|
default:
|
2013-06-28 18:38:30 -07:00
|
|
|
MOZ_CRASH("Invalid encode state");
|
2013-06-04 23:29:12 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-09 23:24:51 -08:00
|
|
|
nsresult
|
|
|
|
MediaEncoder::WriteEncodedDataToMuxer(TrackEncoder *aTrackEncoder)
|
|
|
|
{
|
|
|
|
if (aTrackEncoder == nullptr) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
if (aTrackEncoder->IsEncodingComplete()) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
EncodedFrameContainer encodedVideoData;
|
|
|
|
nsresult rv = aTrackEncoder->GetEncodedTrack(encodedVideoData);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
// Encoding might be canceled.
|
|
|
|
LOG(PR_LOG_ERROR, ("Error! Fail to get encoded data from video encoder."));
|
|
|
|
mState = ENCODE_ERROR;
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
rv = mWriter->WriteEncodedTrack(encodedVideoData,
|
|
|
|
aTrackEncoder->IsEncodingComplete() ?
|
|
|
|
ContainerWriter::END_OF_STREAM : 0);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
LOG(PR_LOG_ERROR, ("Error! Fail to write encoded video track to the media container."));
|
|
|
|
mState = ENCODE_ERROR;
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
MediaEncoder::CopyMetadataToMuxer(TrackEncoder *aTrackEncoder)
|
|
|
|
{
|
|
|
|
if (aTrackEncoder == nullptr) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
nsRefPtr<TrackMetadataBase> meta = aTrackEncoder->GetMetadata();
|
|
|
|
if (meta == nullptr) {
|
|
|
|
LOG(PR_LOG_ERROR, ("Error! metadata = null"));
|
|
|
|
mState = ENCODE_ERROR;
|
|
|
|
return NS_ERROR_ABORT;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv = mWriter->SetMetadata(meta);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
LOG(PR_LOG_ERROR, ("Error! SetMetadata fail"));
|
|
|
|
mState = ENCODE_ERROR;
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2014-01-13 23:26:04 -08:00
|
|
|
#ifdef MOZ_WEBM_ENCODER
|
|
|
|
bool
|
|
|
|
MediaEncoder::IsWebMEncoderEnabled()
|
|
|
|
{
|
|
|
|
return Preferences::GetBool("media.encoder.webm.enabled");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef MOZ_OMX_ENCODER
|
|
|
|
bool
|
|
|
|
MediaEncoder::IsOMXEncoderEnabled()
|
|
|
|
{
|
|
|
|
return Preferences::GetBool("media.encoder.omx.enabled");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-06-04 23:29:12 -07:00
|
|
|
}
|