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 
43  // A list of cues that the stream should inject between media samples. When
44  // there are no cues, the stream should run up to the hint.
45  std::list<std::unique_ptr<StreamData>> cues;
46  };
47 
48  // MediaHandler overrides.
49  Status InitializeInternal() override;
50  Status Process(std::unique_ptr<StreamData> data) override;
51  Status OnFlushRequest(size_t stream_index) override;
52 
53  // Internal handling functions for different stream data.
54  Status OnStreamInfo(std::unique_ptr<StreamData> data);
55 
56  Status OnVideoSample(std::unique_ptr<StreamData> sample);
57  Status OnNonVideoSample(std::unique_ptr<StreamData> sample);
58  Status OnSample(std::unique_ptr<StreamData> sample);
59 
60  // Update stream states with new sync point.
61  Status UseNewSyncPoint(std::shared_ptr<const CueEvent> new_sync);
62 
63  // Check if everyone is waiting for new hint points.
64  bool EveryoneWaitingAtHint() const;
65 
66  // Dispatch or save incoming sample.
67  Status AcceptSample(std::unique_ptr<StreamData> sample,
68  StreamState* stream_state);
69 
70  // Dispatch all samples and cues (in the correct order) for the given stream.
71  Status RunThroughSamples(StreamState* stream);
72 
73  SyncPointQueue* const sync_points_ = nullptr;
74  std::vector<StreamState> stream_states_;
75 
76  // A common hint used by all streams. When a new cue is given to all streams,
77  // the hint will be updated. The hint will always be larger than any cue. The
78  // hint represents the min time in seconds for the next cue appear. The hints
79  // are based off the un-promoted cue event times in |sync_points_|.
80  //
81  // When a video stream passes the hint, it will promote the corresponding cue
82  // event. If all streams get to the hint and there are no video streams, the
83  // thread will block until |sync_points_| gives back a promoted cue event.
84  double hint_;
85 };
86 
87 } // namespace media
88 } // namespace shaka
89 
90 #endif // PACKAGER_MEDIA_CHUNKING_CUE_ALIGNMENT_HANDLER_
All the methods that are virtual are virtual for mocking.
A synchronized queue for cue points.