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-11-12 20:37:58 +00:00
|
|
|
|
2014-04-10 21:42:38 +00:00
|
|
|
#ifndef MEDIA_FORMATS_MP4_FRAGMENTER_H_
|
|
|
|
#define MEDIA_FORMATS_MP4_FRAGMENTER_H_
|
2013-11-12 20:37:58 +00:00
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "base/memory/ref_counted.h"
|
|
|
|
#include "base/memory/scoped_ptr.h"
|
|
|
|
#include "media/base/status.h"
|
|
|
|
|
2014-09-19 20:41:13 +00:00
|
|
|
namespace edash_packager {
|
2013-11-12 20:37:58 +00:00
|
|
|
namespace media {
|
|
|
|
|
|
|
|
class BufferWriter;
|
|
|
|
class MediaSample;
|
|
|
|
|
|
|
|
namespace mp4 {
|
|
|
|
|
2014-03-21 17:26:49 +00:00
|
|
|
struct SegmentReference;
|
|
|
|
struct TrackFragment;
|
2013-11-12 20:37:58 +00:00
|
|
|
|
2014-05-08 20:53:08 +00:00
|
|
|
/// Fragmenter is responsible for the generation of MP4 fragments, i.e. 'traf'
|
|
|
|
/// box and corresponding 'mdat' box.
|
2014-04-08 20:21:07 +00:00
|
|
|
class Fragmenter {
|
2013-11-12 20:37:58 +00:00
|
|
|
public:
|
2014-01-23 22:34:39 +00:00
|
|
|
/// @param traf points to a TrackFragment box.
|
2014-07-16 23:41:54 +00:00
|
|
|
Fragmenter(TrackFragment* traf);
|
2014-04-18 18:49:49 +00:00
|
|
|
|
|
|
|
virtual ~Fragmenter();
|
2013-11-12 20:37:58 +00:00
|
|
|
|
2014-01-23 22:34:39 +00:00
|
|
|
/// Add a sample to the fragmenter.
|
2014-05-08 20:53:08 +00:00
|
|
|
/// @param sample points to the sample to be added.
|
|
|
|
/// @return OK on success, an error status otherwise.
|
|
|
|
virtual Status AddSample(scoped_refptr<MediaSample> sample);
|
2013-11-12 20:37:58 +00:00
|
|
|
|
2014-01-23 22:34:39 +00:00
|
|
|
/// Initialize the fragment with default data.
|
2014-06-30 16:32:06 +00:00
|
|
|
/// @param first_sample_dts specifies the decoding timestamp for the first
|
|
|
|
/// sample for this fragment.
|
2014-04-18 18:49:49 +00:00
|
|
|
/// @return OK on success, an error status otherwise.
|
2014-09-30 21:52:21 +00:00
|
|
|
virtual Status InitializeFragment(int64_t first_sample_dts);
|
2013-11-12 20:37:58 +00:00
|
|
|
|
2014-01-23 22:34:39 +00:00
|
|
|
/// Finalize and optimize the fragment.
|
2014-05-08 20:53:08 +00:00
|
|
|
virtual void FinalizeFragment();
|
2013-11-12 20:37:58 +00:00
|
|
|
|
2014-01-23 22:34:39 +00:00
|
|
|
/// Fill @a reference with current fragment information.
|
2013-11-12 20:37:58 +00:00
|
|
|
void GenerateSegmentReference(SegmentReference* reference);
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
uint64_t fragment_duration() const { return fragment_duration_; }
|
|
|
|
uint64_t first_sap_time() const { return first_sap_time_; }
|
|
|
|
uint64_t earliest_presentation_time() const {
|
2013-11-12 20:37:58 +00:00
|
|
|
return earliest_presentation_time_;
|
|
|
|
}
|
2014-06-30 16:32:06 +00:00
|
|
|
bool fragment_initialized() const { return fragment_initialized_; }
|
2013-11-12 20:37:58 +00:00
|
|
|
bool fragment_finalized() const { return fragment_finalized_; }
|
|
|
|
BufferWriter* data() { return data_.get(); }
|
|
|
|
BufferWriter* aux_data() { return aux_data_.get(); }
|
|
|
|
|
2014-04-18 18:49:49 +00:00
|
|
|
protected:
|
|
|
|
TrackFragment* traf() { return traf_; }
|
|
|
|
|
2014-05-08 20:53:08 +00:00
|
|
|
/// Optimize sample entries table. If all values in @a entries are identical,
|
|
|
|
/// then @a entries is cleared and the value is assigned to @a default_value;
|
|
|
|
/// otherwise it is a NOP. Return true if the table is optimized.
|
|
|
|
template <typename T>
|
|
|
|
bool OptimizeSampleEntries(std::vector<T>* entries, T* default_value);
|
2014-04-18 18:49:49 +00:00
|
|
|
|
2013-11-12 20:37:58 +00:00
|
|
|
private:
|
|
|
|
// Check if the current fragment starts with SAP.
|
|
|
|
bool StartsWithSAP();
|
|
|
|
|
2014-04-18 18:49:49 +00:00
|
|
|
TrackFragment* traf_;
|
2014-06-30 16:32:06 +00:00
|
|
|
bool fragment_initialized_;
|
2013-11-12 20:37:58 +00:00
|
|
|
bool fragment_finalized_;
|
2014-09-30 21:52:21 +00:00
|
|
|
uint64_t fragment_duration_;
|
|
|
|
int64_t presentation_start_time_;
|
|
|
|
int64_t earliest_presentation_time_;
|
|
|
|
int64_t first_sap_time_;
|
2013-11-12 20:37:58 +00:00
|
|
|
scoped_ptr<BufferWriter> data_;
|
|
|
|
scoped_ptr<BufferWriter> aux_data_;
|
|
|
|
|
2014-04-08 20:21:07 +00:00
|
|
|
DISALLOW_COPY_AND_ASSIGN(Fragmenter);
|
2013-11-12 20:37:58 +00:00
|
|
|
};
|
|
|
|
|
2014-05-08 20:53:08 +00:00
|
|
|
template <typename T>
|
|
|
|
bool Fragmenter::OptimizeSampleEntries(std::vector<T>* entries,
|
|
|
|
T* default_value) {
|
|
|
|
DCHECK(entries);
|
|
|
|
DCHECK(default_value);
|
|
|
|
DCHECK(!entries->empty());
|
|
|
|
|
|
|
|
typename std::vector<T>::const_iterator it = entries->begin();
|
|
|
|
T value = *it;
|
|
|
|
for (; it < entries->end(); ++it)
|
|
|
|
if (value != *it)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Clear |entries| if it contains only one value.
|
|
|
|
entries->clear();
|
|
|
|
*default_value = value;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-11-12 20:37:58 +00:00
|
|
|
} // namespace mp4
|
|
|
|
} // namespace media
|
2014-09-19 20:41:13 +00:00
|
|
|
} // namespace edash_packager
|
2013-11-12 20:37:58 +00:00
|
|
|
|
2014-04-10 21:42:38 +00:00
|
|
|
#endif // MEDIA_FORMATS_MP4_FRAGMENTER_H_
|