Shaka Packager SDK
segmenter_test_base.h
1 // Copyright 2015 Google Inc. All rights reserved.
2 //
3 // Use of this source code is governed by a BSD-style
4 // license that can be found in the LICENSE file or at
5 // https://developers.google.com/open-source/licenses/bsd
6 
7 #ifndef PACKAGER_MEDIA_FORMATS_WEBM_SEGMENTER_TEST_UTILS_H_
8 #define PACKAGER_MEDIA_FORMATS_WEBM_SEGMENTER_TEST_UTILS_H_
9 
10 #include <gtest/gtest.h>
11 
12 #include "packager/file/file_closer.h"
13 #include "packager/file/file_test_util.h"
14 #include "packager/file/memory_file.h"
15 #include "packager/media/base/media_sample.h"
16 #include "packager/media/base/muxer_options.h"
17 #include "packager/media/base/stream_info.h"
18 #include "packager/media/base/video_stream_info.h"
19 #include "packager/media/formats/webm/mkv_writer.h"
20 #include "packager/media/formats/webm/segmenter.h"
21 #include "packager/media/formats/webm/webm_parser.h"
22 #include "packager/status.h"
23 #include "packager/status_test_util.h"
24 
25 namespace shaka {
26 namespace media {
27 
28 class SegmentTestBase : public ::testing::Test {
29  public:
30  enum KeyFrameFlag {
31  kKeyFrame,
32  kNotKeyFrame,
33  };
34  enum SideDataFlag {
35  kGenerateSideData,
36  kNoSideData,
37  };
38 
39  protected:
41 
42  void SetUp() override;
43  void TearDown() override;
44 
46  template <typename S>
48  const MuxerOptions& options,
49  const StreamInfo& info,
50  std::unique_ptr<webm::Segmenter>* result) const {
51  std::unique_ptr<S> segmenter(new S(options));
52 
53  ASSERT_OK(segmenter->Initialize(info, nullptr /* progress_listener */,
54  nullptr /* muxer_listener */));
55  *result = std::move(segmenter);
56  }
57 
59  std::shared_ptr<MediaSample> CreateSample(KeyFrameFlag key_frame_flag,
60  uint64_t duration,
61  SideDataFlag side_data_flag);
65  VideoStreamInfo* CreateVideoStreamInfo(uint32_t time_scale) const;
66 
68  std::string OutputFileName() const;
70  std::string TemplateFileName(int number) const;
71 
72  protected:
73  // A helper class used to determine the number of clusters and frames for a
74  // given WebM file.
75  class ClusterParser : private WebMParserClient {
76  public:
77  ClusterParser();
78  ~ClusterParser() override;
79 
80  // Make sure to use ASSERT_NO_FATAL_FAILURE.
81  void PopulateFromCluster(const std::string& file_name);
82  void PopulateFromSegment(const std::string& file_name);
83 
84  size_t GetFrameCountForCluster(size_t cluster_index) const;
85  int64_t GetFrameTimecode(size_t cluster_index, size_t frame_index) const;
86 
87  size_t cluster_count() const;
88 
89  private:
90  // WebMParserClient overrides.
91  WebMParserClient* OnListStart(int id) override;
92  bool OnListEnd(int id) override;
93  bool OnUInt(int id, int64_t val) override;
94  bool OnFloat(int id, double val) override;
95  bool OnBinary(int id, const uint8_t* data, int size) override;
96  bool OnString(int id, const std::string& str) override;
97 
98  private:
99  int64_t cluster_timecode_ = -1;
100  // frame_timecodes_[cluster_index][frame_index].
101  std::vector<std::vector<int64_t>> frame_timecodes_;
102  bool in_cluster_ = false;
103  };
104 
105  protected:
106  void set_cur_timestamp(uint64_t timestamp) { cur_timestamp_ = timestamp; }
107 
108  std::string output_file_name_;
109  std::string segment_template_;
110  uint64_t cur_timestamp_;
111  bool single_segment_;
112 };
113 
114 } // namespace media
115 } // namespace shaka
116 
117 #endif // PACKAGER_MEDIA_FORMATS_WEBM_SEGMENTER_TEST_UTILS_H_
std::string TemplateFileName(int number) const
Gets the file name of the given template file.
MuxerOptions CreateMuxerOptions() const
Creates a Muxer options object for testing.
void CreateAndInitializeSegmenter(const MuxerOptions &options, const StreamInfo &info, std::unique_ptr< webm::Segmenter > *result) const
Creates a Segmenter of the given type and initializes it.
std::string OutputFileName() const
Gets the file name of the current output file.
VideoStreamInfo * CreateVideoStreamInfo(uint32_t time_scale) const
Creates a video stream info object for testing.
std::shared_ptr< MediaSample > CreateSample(KeyFrameFlag key_frame_flag, uint64_t duration, SideDataFlag side_data_flag)
Creates a new media sample.
Abstract class holds stream information.
Definition: stream_info.h:65
Holds video stream information.
All the methods that are virtual are virtual for mocking.
This structure contains the list of configuration options for Muxer.
Definition: muxer_options.h:20