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