2014-02-14 23:21:05 +00:00
|
|
|
// Copyright 2014 Google Inc. All rights reserved.
|
|
|
|
//
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file or at
|
|
|
|
// https://developers.google.com/open-source/licenses/bsd
|
2013-10-11 21:44:55 +00:00
|
|
|
|
|
|
|
#ifndef MEDIA_BASE_DEMUXER_H_
|
|
|
|
#define MEDIA_BASE_DEMUXER_H_
|
|
|
|
|
2016-01-21 18:30:29 +00:00
|
|
|
#include <deque>
|
2016-08-17 17:41:40 +00:00
|
|
|
#include <memory>
|
2013-10-11 21:44:55 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2016-01-21 18:30:29 +00:00
|
|
|
#include "packager/base/compiler_specific.h"
|
2014-10-01 22:10:21 +00:00
|
|
|
#include "packager/media/base/container_names.h"
|
|
|
|
#include "packager/media/base/status.h"
|
2013-10-11 21:44:55 +00:00
|
|
|
|
2016-05-20 21:19:33 +00:00
|
|
|
namespace shaka {
|
2013-10-11 21:44:55 +00:00
|
|
|
namespace media {
|
|
|
|
|
|
|
|
class Decryptor;
|
|
|
|
class File;
|
2014-08-25 22:51:19 +00:00
|
|
|
class KeySource;
|
2013-10-11 21:44:55 +00:00
|
|
|
class MediaParser;
|
|
|
|
class MediaSample;
|
|
|
|
class MediaStream;
|
|
|
|
class StreamInfo;
|
|
|
|
|
2014-01-24 18:46:46 +00:00
|
|
|
/// Demuxer is responsible for extracting elementary stream samples from a
|
|
|
|
/// media file, e.g. an ISO BMFF file.
|
2013-10-11 21:44:55 +00:00
|
|
|
class Demuxer {
|
|
|
|
public:
|
2014-01-24 18:46:46 +00:00
|
|
|
/// @param file_name specifies the input source. It uses prefix matching to
|
|
|
|
/// create a proper File object. The user can extend File to support
|
|
|
|
/// a custom File object with its own prefix.
|
2014-08-25 22:51:19 +00:00
|
|
|
explicit Demuxer(const std::string& file_name);
|
2013-10-11 21:44:55 +00:00
|
|
|
~Demuxer();
|
|
|
|
|
2014-08-25 22:51:19 +00:00
|
|
|
/// Set the KeySource for media decryption.
|
|
|
|
/// @param key_source points to the source of decryption keys. The key
|
|
|
|
/// source must support fetching of keys for the type of media being
|
|
|
|
/// demuxed.
|
2016-08-17 17:41:40 +00:00
|
|
|
void SetKeySource(std::unique_ptr<KeySource> key_source);
|
2014-08-25 22:51:19 +00:00
|
|
|
|
2014-01-24 18:46:46 +00:00
|
|
|
/// Initialize the Demuxer. Calling other public methods of this class
|
|
|
|
/// without this method returning OK, results in an undefined behavior.
|
|
|
|
/// This method primes the demuxer by parsing portions of the media file to
|
|
|
|
/// extract stream information.
|
|
|
|
/// @return OK on success.
|
2013-10-11 21:44:55 +00:00
|
|
|
Status Initialize();
|
|
|
|
|
2014-01-24 18:46:46 +00:00
|
|
|
/// Drive the remuxing from demuxer side (push). Read the file and push
|
|
|
|
/// the Data to Muxer until Eof.
|
2013-10-11 21:44:55 +00:00
|
|
|
Status Run();
|
|
|
|
|
2014-01-24 18:46:46 +00:00
|
|
|
/// Read from the source and send it to the parser.
|
2013-10-11 21:44:55 +00:00
|
|
|
Status Parse();
|
|
|
|
|
2015-02-09 18:22:28 +00:00
|
|
|
/// Cancel a demuxing job in progress. Will cause @a Run to exit with an error
|
|
|
|
/// status of type CANCELLED.
|
|
|
|
void Cancel();
|
|
|
|
|
2014-01-24 18:46:46 +00:00
|
|
|
/// @return Streams in the media container being demuxed. The caller cannot
|
|
|
|
/// add or remove streams from the returned vector, but the caller is
|
|
|
|
/// allowed to change the internal state of the streams in the vector
|
|
|
|
/// through MediaStream APIs.
|
2016-08-30 23:01:19 +00:00
|
|
|
const std::vector<std::unique_ptr<MediaStream>>& streams() {
|
|
|
|
return streams_;
|
|
|
|
}
|
2013-10-11 21:44:55 +00:00
|
|
|
|
2015-06-02 21:41:49 +00:00
|
|
|
/// @return Container name (type). Value is CONTAINER_UNKNOWN if the demuxer
|
|
|
|
/// is not initialized.
|
|
|
|
MediaContainerName container_name() { return container_name_; }
|
|
|
|
|
2013-10-11 21:44:55 +00:00
|
|
|
private:
|
2017-01-24 00:55:02 +00:00
|
|
|
Demuxer(const Demuxer&) = delete;
|
|
|
|
Demuxer& operator=(const Demuxer&) = delete;
|
|
|
|
|
2016-01-21 18:30:29 +00:00
|
|
|
struct QueuedSample {
|
2017-01-24 00:55:02 +00:00
|
|
|
QueuedSample(uint32_t track_id, std::shared_ptr<MediaSample> sample);
|
2016-01-21 18:30:29 +00:00
|
|
|
~QueuedSample();
|
|
|
|
|
|
|
|
uint32_t track_id;
|
2017-01-24 00:55:02 +00:00
|
|
|
std::shared_ptr<MediaSample> sample;
|
2016-01-21 18:30:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Parser init event.
|
2017-01-24 00:55:02 +00:00
|
|
|
void ParserInitEvent(const std::vector<std::shared_ptr<StreamInfo>>& streams);
|
2016-01-21 18:30:29 +00:00
|
|
|
// Parser new sample event handler. Queues the samples if init event has not
|
|
|
|
// been received, otherwise calls PushSample() to push the sample to
|
|
|
|
// corresponding stream.
|
2014-09-30 21:52:21 +00:00
|
|
|
bool NewSampleEvent(uint32_t track_id,
|
2017-01-24 00:55:02 +00:00
|
|
|
const std::shared_ptr<MediaSample>& sample);
|
2016-01-21 18:30:29 +00:00
|
|
|
// Helper function to push the sample to corresponding stream.
|
2017-01-24 00:55:02 +00:00
|
|
|
bool PushSample(uint32_t track_id,
|
|
|
|
const std::shared_ptr<MediaSample>& sample);
|
2013-10-11 21:44:55 +00:00
|
|
|
|
|
|
|
std::string file_name_;
|
|
|
|
File* media_file_;
|
|
|
|
bool init_event_received_;
|
2014-10-14 02:34:29 +00:00
|
|
|
Status init_parsing_status_;
|
2016-01-21 18:30:29 +00:00
|
|
|
// Queued samples received in NewSampleEvent() before ParserInitEvent().
|
|
|
|
std::deque<QueuedSample> queued_samples_;
|
2016-08-17 17:41:40 +00:00
|
|
|
std::unique_ptr<MediaParser> parser_;
|
2016-08-30 23:01:19 +00:00
|
|
|
std::vector<std::unique_ptr<MediaStream>> streams_;
|
2015-06-02 21:41:49 +00:00
|
|
|
MediaContainerName container_name_;
|
2016-08-17 17:41:40 +00:00
|
|
|
std::unique_ptr<uint8_t[]> buffer_;
|
|
|
|
std::unique_ptr<KeySource> key_source_;
|
2015-02-09 18:22:28 +00:00
|
|
|
bool cancelled_;
|
2013-10-11 21:44:55 +00:00
|
|
|
};
|
2013-11-21 22:33:07 +00:00
|
|
|
|
|
|
|
} // namespace media
|
2016-05-20 21:19:33 +00:00
|
|
|
} // namespace shaka
|
2013-10-11 21:44:55 +00:00
|
|
|
|
|
|
|
#endif // MEDIA_BASE_DEMUXER_H_
|