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