Bug 1219047 - Call rust mp4parser with logging. r=kinetik
Add a helper for passing the initialization segments
of mp4 streams to the rust parser and log the result.
This runs real data through the new parser for testing
but doesn't use the results.
Code is conditional on MOZ_RUST_MP4PARSE to be defined
in confvars.sh. See
bug 1219530.
--- a/media/libstagefright/binding/MP4Metadata.cpp
+++ b/media/libstagefright/binding/MP4Metadata.cpp
@@ -8,16 +8,17 @@
#include "media/stagefright/MediaSource.h"
#include "media/stagefright/MetaData.h"
#include "mozilla/Monitor.h"
#include "mp4_demuxer/MoofParser.h"
#include "mp4_demuxer/MP4Metadata.h"
#include <limits>
#include <stdint.h>
+#include <vector>
using namespace stagefright;
namespace mp4_demuxer
{
struct StageFrightPrivate
{
@@ -101,19 +102,53 @@ MP4Metadata::MP4Metadata(Stream* aSource
UpdateCrypto(metaData.get());
}
MP4Metadata::~MP4Metadata()
{
}
+#ifdef MOZ_RUST_MP4PARSE
+#include "mp4parse.h"
+
+// Helper to test the rust parser on a data source.
+static bool try_rust(RefPtr<Stream> aSource)
+{
+ int64_t length;
+ if (!aSource->Length(&length)) {
+ fprintf(stderr, "Couldn't get source length\n");
+ return false;
+ }
+ fprintf(stderr, "Source length %d bytes\n", (long long int)length);
+ if (length <= 0) {
+ return false;
+ }
+ size_t bytes_read = 0;
+ auto buffer = std::vector<uint8_t>(length);
+ bool rv = aSource->ReadAt(0, buffer.data(), length, &bytes_read);
+ if (!rv || bytes_read != size_t(length)) {
+ fprintf(stderr, "Error copying mp4 data\n");
+ return false;
+ }
+ auto context = mp4parse_new();
+ int32_t tracks = mp4parse_read(context, buffer.data(), bytes_read);
+ mp4parse_free(context);
+ fprintf(stderr, "rust parser found %d tracks\n", int(tracks));
+ return true;
+}
+#endif
+
uint32_t
MP4Metadata::GetNumberTracks(mozilla::TrackInfo::TrackType aType) const
{
+#ifdef MOZ_RUST_MP4PARSE
+ // Try in rust first.
+ try_rust(mSource);
+#endif
size_t tracks = mPrivate->mMetadataExtractor->countTracks();
uint32_t total = 0;
for (size_t i = 0; i < tracks; i++) {
sp<MetaData> metaData = mPrivate->mMetadataExtractor->getTrackMetaData(i);
const char* mimeType;
if (metaData == nullptr || !metaData->findCString(kKeyMIMEType, &mimeType)) {
continue;