提交 f3313034 authored 作者: Paulo R. Panhoto's avatar Paulo R. Panhoto

mod_mp4 -- Supports playback of MP4 files.

* depends on libmp4v2 <http://code.google.com/p/mp4v2/>
(originally compiled against v1.6.1)

* File format details:
	- Files must be hinted (mpeg4ip can be used to create the hint
	  tracks).
	- Video track encoding must be supported by FS (e.g. H.263)
	- Audio track encoding must be PCMU/8000/Mono. The audio track
	  can be created with this tool:
	<https://asteriskvideo.svn.sourceforge.net/svnroot/asteriskvideo/tools/pcm2mp4.cpp>
上级 42edb9cf
LOCAL_LDFLAGS=-lmp4v2
LOCAL_SOURCES=mp4_helper.cpp
LOCAL_OBJS=mp4_helper.o
BASE=../../../..
include $(BASE)/build/modmake.rules
/*
The contents of this file are subject to the Mozilla Public License
Version 1.1 (the "License"); you may not use this file except in
compliance with the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL/
Software distributed under the License is distributed on an "AS IS"
basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
License for the specific language governing rights and limitations
under the License.
The Original Code is MP4 Helper Library to Freeswitch MP4 module.
The Initial Developer of the Original Code is
Paulo Rogério Panhoto <paulo@voicetechnology.com.br>.
Portions created by the Initial Developer are Copyright (C)
the Initial Developer. All Rights Reserved.
*/
#ifndef EXCEPTION_HPP_
#define EXCEPTION_HPP_
#include <exception>
#include <string>
class Exception: public std::exception {
public:
Exception()
{
}
Exception(const std::string & message): message_(message)
{
}
Exception(const std::exception & e): message_(e.what())
{
}
Exception(const Exception & e): message_(e.message_)
{
}
virtual ~Exception() throw()
{
}
const char * what() const throw()
{
return message_.c_str();
}
private:
std::string message_;
};
#endif
\ No newline at end of file
差异被折叠。
/*
The contents of this file are subject to the Mozilla Public License
Version 1.1 (the "License"); you may not use this file except in
compliance with the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL/
Software distributed under the License is distributed on an "AS IS"
basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
License for the specific language governing rights and limitations
under the License.
The Original Code is MP4 Helper Library to the Freeswitch MP4 Module.
The Initial Developer of the Original Code is
Paulo Rogério Panhoto <paulo@voicetechnology.com.br>.
Portions created by the Initial Developer are Copyright (C)
the Initial Developer. All Rights Reserved.
*/
#include "mp4_helper.hpp"
namespace MP4
{
Context::Context(const char * file, bool newFile)
{
if(newFile) create(file);
else open(file);
}
Context::~Context()
{
close();
}
void Context::open(const char * file)
{
fh = MP4Read(file, 0);
if (fh == MP4_INVALID_FILE_HANDLE) throw Exception(file, "Open failed");
getTracks(file);
}
void Context::create(const char * file)
{
fh = MP4Create(file);
if (fh == MP4_INVALID_FILE_HANDLE) throw Exception(file, "Create file failed");
}
void Context::close()
{
if (!isOpen()) return;
MP4Close(fh);
}
void Context::getTracks(const char * file)
{
int i = 0;
bool audioTrack = false, videoTrack = false;
if (!isOpen()) throw Exception(file, "File is closed.");
for (;;)
{
TrackProperties track;
if((track.hint = MP4FindTrackId(fh, i++, MP4_HINT_TRACK_TYPE, 0)) == MP4_INVALID_TRACK_ID) break;
MP4GetHintTrackRtpPayload(fh, track.hint, &track.codecName, &track.payload, NULL, NULL);
track.track = MP4GetHintTrackReferenceTrackId(fh, track.hint);
if(track.track == MP4_INVALID_TRACK_ID) continue;
track.clock = MP4GetTrackTimeScale(fh, track.hint);
if (!strcmp(MP4GetTrackType(fh, track.track), MP4_AUDIO_TRACK_TYPE)) {
audioTrack = true;
if(!strncmp(track.codecName, "PCM", 3))
track.packetLength = 20;
else
track.packetLength = track.clock = 0;
audio = track;
} else if (!strcmp(MP4GetTrackType(fh, track.track), MP4_VIDEO_TRACK_TYPE)) {
videoTrack = true;
const char * sdp = MP4GetHintTrackSdp(fh, track.hint);
const char * fmtp = strstr(sdp, "fmtp");
if (fmtp) {
// finds beginning of 'fmtp' value;
for(fmtp += 5; *fmtp != ' '; ++fmtp);
++fmtp;
const char * eol = fmtp;
for(;*eol != '\r' && *eol != '\n'; ++eol);
video.fmtp = std::string(fmtp, eol);
}
video.track = track;
}
}
if (!audioTrack || !videoTrack) throw Exception(file, "Missing audio/video track.");
}
bool Context::getVideoPacket(void * buffer, u_int & size, u_int & ts)
{
return getPacket(video.track.hint, video.track.runtime, true, buffer, size, ts);
}
bool Context::getAudioPacket(void * buffer, u_int & size, u_int & ts)
{
return getPacket(audio.hint, audio.runtime, false, buffer, size, ts);
}
bool Context::getPacket(MP4TrackId hint, RuntimeProperties & rt,
bool header, void * buffer, u_int & size, u_int & ts)
{
if (rt.frame == 0 || rt.packet == rt.packetsPerFrame) {
++rt.frame;
if(!MP4ReadRtpHint(fh, hint, rt.frame, &rt.packetsPerFrame))
return false;
rt.packet = 0;
rt.last_frame = MP4GetSampleTime(fh, hint, rt.frame);
}
ts = rt.last_frame;
if (!MP4ReadRtpPacket(fh, hint, rt.packet, (u_int8_t **) &buffer, &size, 0, header, true)) return false;
++rt.packet;
return true;
}
}
\ No newline at end of file
/*
The contents of this file are subject to the Mozilla Public License
Version 1.1 (the "License"); you may not use this file except in
compliance with the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL/
Software distributed under the License is distributed on an "AS IS"
basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
License for the specific language governing rights and limitations
under the License.
The Original Code is MP4 Helper Library to Freeswitch MP4 module.
The Initial Developer of the Original Code is
Paulo Rogério Panhoto <paulo@voicetechnology.com.br>.
Portions created by the Initial Developer are Copyright (C)
the Initial Developer. All Rights Reserved.
*/
#ifndef MP4_HELPER_HPP_
#define MP4_HELPER_HPP_
#include <mp4.h>
#include <string>
#include <exception>
#include <string>
namespace MP4
{
class Exception: public std::exception {
public:
Exception(const std::string & file, const std::string & error)
: description_(file + ':' + error)
{
}
const char * what() const throw()
{
return description_.c_str();
}
~Exception() throw()
{
}
private:
std::string description_;
};
struct RuntimeProperties {
u_int32_t frame; // sampleID
u_int16_t packetsPerFrame;
u_int16_t packet; // packetID
u_int32_t last_frame; // timestamp
RuntimeProperties(): frame(0), packetsPerFrame(0), packet(0)
{
}
};
struct TrackProperties {
MP4TrackId hint;
MP4TrackId track;
char * codecName;
u_int8_t payload;
u_int32_t clock;
u_int32_t packetLength; // packet Length in time (ms)
RuntimeProperties runtime;
TrackProperties(): hint(MP4_INVALID_TRACK_ID), track(MP4_INVALID_TRACK_ID),
codecName(NULL), payload(0), clock(0), packetLength(0)
{
}
};
typedef TrackProperties AudioProperties;
struct VideoProperties {
TrackProperties track;
std::string fmtp;
VideoProperties()
{
}
VideoProperties(const TrackProperties & rhs): track(rhs)
{
}
};
class Context {
public:
Context(const char * file, bool create = false);
~Context();
void open(const char * file);
void create(const char * file);
void close();
// returns: TRUE = has more data, FALSE = end-of-stream or failure
bool getVideoPacket(void * buffer, u_int & size, u_int & ts);
// returns: TRUE = has more data, FALSE = end-of-stream or failure
bool getAudioPacket(void * buffer, u_int & size, u_int & ts);
bool isOpen() const { return fh != MP4_INVALID_FILE_HANDLE; }
bool isSupported() const { return audio.track != MP4_INVALID_TRACK_ID && video.track.track != MP4_INVALID_TRACK_ID; }
const AudioProperties & audioTrack() const { return audio; }
const VideoProperties & videoTrack() const { return video; }
private:
MP4FileHandle fh;
AudioProperties audio;
VideoProperties video;
// Prevent copy construction.
Context(const Context &);
bool getPacket(MP4TrackId hint, RuntimeProperties & rt,
bool header, void * buffer, u_int & size, u_int & ts);
void getTracks(const char * file);
};
}
#endif
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论