Shaka Player Embedded
text_track_public.cc
Go to the documentation of this file.
1 // Copyright 2019 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include <algorithm>
16 #include <cmath>
17 #include <unordered_set>
18 
19 #include "shaka/media/text_track.h"
20 #include "src/debug/mutex.h"
21 #include "src/util/utils.h"
22 
23 namespace shaka {
24 namespace media {
25 
27  public:
28  Impl() : mutex("TextTrack"), mode(TextTrackMode::Disabled) {}
29 
32  std::vector<std::shared_ptr<VTTCue>> cues;
33  std::unordered_set<Client*> clients;
34 };
35 
36 // \cond Doxygen_Skip
38 TextTrack::Client::~Client() {}
39 // \endcond Doxygen_Skip
40 
42  const std::string& language, const std::string& id)
43  : kind(kind), label(label), language(language), id(id), impl_(new Impl) {}
45 
47  std::unique_lock<Mutex> lock(impl_->mutex);
48  return impl_->mode;
49 }
50 
52  std::unique_lock<Mutex> lock(impl_->mutex);
53  impl_->mode = mode;
54 }
55 
56 
57 std::vector<std::shared_ptr<VTTCue>> TextTrack::cues() const {
58  std::unique_lock<Mutex> lock(impl_->mutex);
59  return impl_->cues;
60 }
61 
62 std::vector<std::shared_ptr<VTTCue>> TextTrack::active_cues(double time) const {
63  auto cues = this->cues();
64 
65  std::vector<std::shared_ptr<VTTCue>> ret;
66  for (auto& cue : cues) {
67  if (cue->start_time() <= time && cue->end_time() >= time)
68  ret.emplace_back(cue);
69  }
70  return ret;
71 }
72 
73 double TextTrack::NextCueChangeTime(double time) const {
74  auto cues = this->cues();
75 
76  double next_time = INFINITY;
77  for (auto& cue : cues) {
78  if (cue->start_time() > time)
79  next_time = std::min(next_time, cue->start_time());
80  else if (cue->end_time() > time)
81  next_time = std::min(next_time, cue->end_time());
82  }
83  return next_time;
84 }
85 
86 void TextTrack::AddCue(std::shared_ptr<VTTCue> cue) {
87  std::unique_lock<Mutex> lock(impl_->mutex);
88  impl_->cues.emplace_back(cue);
89 
90  for (Client* client : impl_->clients)
91  client->OnCueAdded(cue);
92 }
93 
94 void TextTrack::RemoveCue(std::shared_ptr<VTTCue> cue) {
95  std::unique_lock<Mutex> lock(impl_->mutex);
96  util::RemoveElement(&impl_->cues, cue);
97 
98  for (Client* client : impl_->clients)
99  client->OnCueRemoved(cue);
100 }
101 
102 
104  std::unique_lock<Mutex> lock(impl_->mutex);
105  impl_->clients.insert(client);
106 }
107 
109  std::unique_lock<Mutex> lock(impl_->mutex);
110  impl_->clients.erase(client);
111 }
112 
113 } // namespace media
114 } // namespace shaka
void RemoveClient(Client *client)
TextTrack(TextTrackKind kind, const std::string &label, const std::string &language, const std::string &id)
double NextCueChangeTime(double time) const
const std::string language
Definition: text_track.h:117
std::vector< std::shared_ptr< VTTCue > > cues
void AddClient(Client *client)
const TextTrackKind kind
Definition: text_track.h:111
virtual std::vector< std::shared_ptr< VTTCue > > active_cues(double time) const
std::unordered_set< Client * > clients
virtual TextTrackMode mode() const
const std::string label
Definition: text_track.h:114
void RemoveElement(List *list, Elem &&elem)
Definition: utils.h:95
virtual void OnCueRemoved(std::shared_ptr< VTTCue > cue)=0
virtual void RemoveCue(std::shared_ptr< VTTCue > cue)
virtual std::vector< std::shared_ptr< VTTCue > > cues() const
virtual void SetMode(TextTrackMode mode)
virtual void AddCue(std::shared_ptr< VTTCue > cue)
virtual void OnCueAdded(std::shared_ptr< VTTCue > cue)=0