Shaka Packager SDK
cue_alignment_handler.h
1 // Copyright 2018 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_CHUNKING_CUE_ALIGNMENT_HANDLER_
8 #define PACKAGER_MEDIA_CHUNKING_CUE_ALIGNMENT_HANDLER_
9 
10 #include <list>
11 
12 #include "packager/media/base/media_handler.h"
13 #include "packager/media/chunking/sync_point_queue.h"
14 
15 namespace shaka {
16 namespace media {
17 
26  public:
27  explicit CueAlignmentHandler(SyncPointQueue* sync_points);
28  ~CueAlignmentHandler() = default;
29 
30  private:
32  CueAlignmentHandler& operator=(const CueAlignmentHandler&) = delete;
33 
34  struct StreamState {
35  // Information for the stream.
36  std::shared_ptr<const StreamInfo> info;
37  // Cached samples that cannot be dispatched. All the samples should be at or
38  // after |hint|.
39  std::list<std::unique_ptr<StreamData>> samples;
40  // If set, the stream is pending to be flushed.
41  bool to_be_flushed = false;
42  // Only set for text stream.
43  double max_text_sample_end_time_seconds = 0;
44 
45  // A list of cues that the stream should inject between media samples. When
46  // there are no cues, the stream should run up to the hint.
47  std::list<std::unique_ptr<StreamData>> cues;
48  };
49 
50  // MediaHandler overrides.
51  Status InitializeInternal() override;
52  Status Process(std::unique_ptr<StreamData> data) override;
53  Status OnFlushRequest(size_t stream_index) override;
54 
55  // Internal handling functions for different stream data.
56  Status OnStreamInfo(std::unique_ptr<StreamData> data);
57 
58  Status OnVideoSample(std::unique_ptr<StreamData> sample);
59  Status OnNonVideoSample(std::unique_ptr<StreamData> sample);
60  Status OnSample(std::unique_ptr<StreamData> sample);
61 
62  // Update stream states with new sync point.
63  Status UseNewSyncPoint(std::shared_ptr<const CueEvent> new_sync);
64 
65  // Check if everyone is waiting for new hint points.
66  bool EveryoneWaitingAtHint() const;
67 
68  // Dispatch or save incoming sample.
69  Status AcceptSample(std::unique_ptr<StreamData> sample,
70  StreamState* stream_state);
71 
72  // Dispatch all samples and cues (in the correct order) for the given stream.
73  Status RunThroughSamples(StreamState* stream);
74 
75  SyncPointQueue* const sync_points_ = nullptr;
76  std::vector<StreamState> stream_states_;
77 
78  // A common hint used by all streams. When a new cue is given to all streams,
79  // the hint will be updated. The hint will always be larger than any cue. The
80  // hint represents the min time in seconds for the next cue appear. The hints
81  // are based off the un-promoted cue event times in |sync_points_|.
82  //
83  // When a video stream passes the hint, it will promote the corresponding cue
84  // event. If all streams get to the hint and there are no video streams, the
85  // thread will block until |sync_points_| gives back a promoted cue event.
86  double hint_;
87 };
88 
89 } // namespace media
90 } // namespace shaka
91 
92 #endif // PACKAGER_MEDIA_CHUNKING_CUE_ALIGNMENT_HANDLER_
All the methods that are virtual are virtual for mocking.
A synchronized queue for cue points.