1
0
Fork 0
mirror of https://github.com/ossrs/srs.git synced 2025-02-24 06:54:22 +00:00
srs/trunk/src/main/srs_main_mp4_parser.cpp

126 lines
3.8 KiB
C++
Raw Normal View History

2017-03-25 09:21:39 +00:00
/**
* The MIT License (MIT)
*
2017-03-25 13:29:29 +00:00
* Copyright (c) 2013-2017 OSSRS(winlin)
2017-03-25 09:21:39 +00:00
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
2017-03-25 07:40:28 +00:00
#include <srs_core.hpp>
#include <srs_kernel_error.hpp>
2017-03-26 05:40:39 +00:00
#include <srs_service_log.hpp>
2017-05-13 14:37:46 +00:00
#include <srs_kernel_mp4.hpp>
#include <srs_kernel_file.hpp>
#include <srs_kernel_stream.hpp>
#include <srs_core_autofree.hpp>
2017-03-25 07:40:28 +00:00
2017-05-13 13:47:20 +00:00
#include <stdio.h>
#include <stdlib.h>
#include <string>
2017-05-13 14:37:46 +00:00
#include <sstream>
2017-05-13 13:47:20 +00:00
using namespace std;
2017-03-25 07:40:28 +00:00
// @global log and context.
2017-03-26 05:40:39 +00:00
ISrsLog* _srs_log = new SrsConsoleLog(SrsLogLevelTrace, false);
ISrsThreadContext* _srs_context = new SrsThreadContext();
2017-03-25 07:40:28 +00:00
2017-05-13 14:37:46 +00:00
int parse(std::string mp4_file)
2017-05-13 13:47:20 +00:00
{
2017-05-13 14:37:46 +00:00
int ret = ERROR_SUCCESS;
2017-05-13 13:47:20 +00:00
2017-05-13 14:37:46 +00:00
SrsFileReader fr;
if ((ret = fr.open(mp4_file)) != ERROR_SUCCESS) {
srs_error("Open MP4 file failed, ret=%d", ret);
return ret;
}
srs_trace("MP4 file open success");
SrsMp4BoxReader br;
if ((ret = br.initialize(&fr)) != ERROR_SUCCESS) {
srs_error("Open MP4 box reader failed, ret=%d", ret);
return ret;
}
srs_trace("MP4 box reader open success");
2017-05-14 14:16:15 +00:00
SrsSimpleStream* stream = new SrsSimpleStream();
SrsAutoFree(SrsSimpleStream, stream);
2017-05-13 14:37:46 +00:00
while (true) {
SrsMp4Box* box = NULL;
SrsAutoFree(SrsMp4Box, box);
2017-05-14 14:16:15 +00:00
if ((ret = br.read(stream, &box)) != ERROR_SUCCESS) {
2017-05-13 14:37:46 +00:00
if (ret != ERROR_SYSTEM_FILE_EOF) {
srs_error("Read MP4 box failed, ret=%d", ret);
2017-05-14 14:16:15 +00:00
} else {
fprintf(stderr, "\n");
2017-05-13 14:37:46 +00:00
}
return ret;
}
2017-05-14 14:16:15 +00:00
SrsBuffer* buffer = new SrsBuffer(stream->bytes(), stream->length());
SrsAutoFree(SrsBuffer, buffer);
if ((ret = box->decode(buffer)) != ERROR_SUCCESS) {
srs_error("Decode the box failed, ret=%d", ret);
return ret;
}
if ((ret = br.skip(box, stream)) != ERROR_SUCCESS) {
2017-05-13 14:37:46 +00:00
srs_error("Skip MP4 box failed, ret=%d", ret);
return ret;
}
stringstream ss;
2017-05-14 14:16:15 +00:00
fprintf(stderr, "%s", box->dumps(ss, 1).str().c_str());
2017-05-13 14:37:46 +00:00
}
return ret;
2017-05-13 13:47:20 +00:00
}
2017-03-25 07:40:28 +00:00
int main(int argc, char** argv)
{
2017-05-13 14:37:46 +00:00
int ret = ERROR_SUCCESS;
printf("SRS MP4 parser/%d.%d.%d, parse and show the mp4 boxes structure.\n",
2017-05-13 13:47:20 +00:00
VERSION_MAJOR, VERSION_MINOR, VERSION_REVISION);
if (argc < 2) {
2017-05-13 14:37:46 +00:00
printf("Usage: %s <mp4_file>\n"
" mp4_file The MP4 file path to parse.\n"
"For example:\n"
" %s doc/source.200kbps.768x320.mp4\n",
argv[0], argv[0]);
exit(-1);
2017-05-13 13:47:20 +00:00
}
string mp4_file = argv[1];
srs_trace("Parse MP4 file %s", mp4_file.c_str());
2017-05-13 14:37:46 +00:00
ret = parse(mp4_file);
if (ret == ERROR_SYSTEM_FILE_EOF) {
srs_trace("Parse complete");
return 0;
}
return ret;
2017-03-25 07:40:28 +00:00
}
2017-03-25 09:21:39 +00:00