Shaka Player Embedded
media_player.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 
16 
17 #include <atomic>
18 #include <vector>
19 
20 #include "src/debug/mutex.h"
21 #include "src/util/utils.h"
22 
23 namespace shaka {
24 namespace media {
25 
26 namespace {
27 
28 std::atomic<const MediaPlayer*> player_{nullptr};
29 
30 } // namespace
31 
32 std::string to_string(VideoReadyState state) {
33 #define TO_STRING(E) \
34  case VideoReadyState::E: \
35  return #E
36 
37  switch (state) {
44  default:
45  return "Unknown";
46  }
47 #undef TO_STRING
48 }
49 
50 std::string to_string(VideoPlaybackState state) {
51 #define TO_STRING(E) \
52  case VideoPlaybackState::E: \
53  return #E
54 
55  switch (state) {
65  default:
66  return "Unknown";
67  }
68 #undef TO_STRING
69 }
70 
71 
72 // \cond Doxygen_Skip
74 MediaPlayer::Client::~Client() {}
75 // \endcond Doxygen_Skip
76 
77 void MediaPlayer::Client::OnAddAudioTrack(std::shared_ptr<MediaTrack> track) {}
78 
80  std::shared_ptr<MediaTrack> track) {}
81 
82 void MediaPlayer::Client::OnAddVideoTrack(std::shared_ptr<MediaTrack> track) {}
83 
85  std::shared_ptr<MediaTrack> track) {}
86 
87 void MediaPlayer::Client::OnAddTextTrack(std::shared_ptr<TextTrack> track) {}
88 
89 void MediaPlayer::Client::OnRemoveTextTrack(std::shared_ptr<TextTrack> track) {}
90 
92  VideoReadyState new_state) {}
93 
95  VideoPlaybackState new_state) {
96 }
97 
99  double new_rate) {}
100 
101 void MediaPlayer::Client::OnError(const std::string& error) {}
102 
104 
106 
108 
110 
112 
114 
115 void MediaPlayer::Client::OnUserEvent(const std::string& name,
116  void* user_data) {}
117 
118 
120  public:
121  template <typename Func, typename... Args>
122  void CallClientMethod(Func func, Args&&... args) {
123  util::shared_lock<SharedMutex> lock(mutex);
124  for (auto* client : clients)
125  (client->*func)(std::forward<Args>(args)...);
126  }
127 
128  SharedMutex mutex{"ClientList"};
129  std::vector<Client*> clients;
130 };
131 
134 
136  std::unique_lock<SharedMutex> lock(impl_->mutex);
137  if (!util::contains(impl_->clients, client))
138  impl_->clients.emplace_back(client);
139 }
140 
142  std::unique_lock<SharedMutex> lock(impl_->mutex);
143  util::RemoveElement(&impl_->clients, client);
144 }
145 
147  std::shared_ptr<MediaTrack> track) {
148  impl_->CallClientMethod(&Client::OnAddAudioTrack, track);
149 }
150 
152  std::shared_ptr<MediaTrack> track) {
153  impl_->CallClientMethod(&Client::OnRemoveAudioTrack, track);
154 }
155 
157  std::shared_ptr<MediaTrack> track) {
158  impl_->CallClientMethod(&Client::OnAddVideoTrack, track);
159 }
160 
162  std::shared_ptr<MediaTrack> track) {
163  impl_->CallClientMethod(&Client::OnRemoveVideoTrack, track);
164 }
165 
166 void MediaPlayer::ClientList::OnAddTextTrack(std::shared_ptr<TextTrack> track) {
167  impl_->CallClientMethod(&Client::OnAddTextTrack, track);
168 }
169 
171  std::shared_ptr<TextTrack> track) {
172  impl_->CallClientMethod(&Client::OnRemoveTextTrack, track);
173 }
174 
176  VideoReadyState new_state) {
177  impl_->CallClientMethod(&Client::OnReadyStateChanged, old_state, new_state);
178 }
179 
181  VideoPlaybackState old_state, VideoPlaybackState new_state) {
182  impl_->CallClientMethod(&Client::OnPlaybackStateChanged, old_state,
183  new_state);
184 }
185 
187  double new_rate) {
188  impl_->CallClientMethod(&Client::OnPlaybackRateChanged, old_rate, new_rate);
189 }
190 
191 void MediaPlayer::ClientList::OnError(const std::string& error) {
192  impl_->CallClientMethod(&Client::OnError, error);
193 }
194 
196  impl_->CallClientMethod(&Client::OnAttachMse);
197 }
198 
200  impl_->CallClientMethod(&Client::OnAttachSource);
201 }
202 
204  impl_->CallClientMethod(&Client::OnDetach);
205 }
206 
208  impl_->CallClientMethod(&Client::OnPlay);
209 }
210 
212  impl_->CallClientMethod(&Client::OnSeeking);
213 }
214 
216  impl_->CallClientMethod(&Client::OnWaitingForKey);
217 }
218 
220  void* user_data) {
221  impl_->CallClientMethod(&Client::OnUserEvent, name, user_data);
222 }
223 
224 
225 // \cond Doxygen_Skip
227 MediaPlayer::~MediaPlayer() {}
228 // \endcond Doxygen_Skip
229 
231  player_.store(player, std::memory_order_relaxed);
232 }
233 
235  return player_.load(std::memory_order_relaxed);
236 }
237 
238 } // namespace media
239 } // namespace shaka
static void SetMediaPlayerForSupportChecks(const MediaPlayer *player)
virtual void OnPlaybackRateChanged(double old_rate, double new_rate)
Definition: media_player.cc:98
virtual void OnAddVideoTrack(std::shared_ptr< MediaTrack > track)
Definition: media_player.cc:82
bool contains(const std::vector< T > &vec, U &&elem)
Definition: utils.h:87
void CallClientMethod(Func func, Args &&... args)
virtual void OnRemoveAudioTrack(std::shared_ptr< MediaTrack > track)
Definition: media_player.cc:79
const char * name
void OnPlaybackRateChanged(double old_rate, double new_rate) override
void OnReadyStateChanged(VideoReadyState old_state, VideoReadyState new_state) override
void OnRemoveAudioTrack(std::shared_ptr< MediaTrack > track) override
virtual void OnAddAudioTrack(std::shared_ptr< MediaTrack > track)
Definition: media_player.cc:77
std::string to_string(VideoReadyState state)
Definition: media_player.cc:32
virtual void OnRemoveVideoTrack(std::shared_ptr< MediaTrack > track)
Definition: media_player.cc:84
static const MediaPlayer * GetMediaPlayerForSupportChecks()
virtual void OnUserEvent(const std::string &name, void *user_data)
void OnRemoveTextTrack(std::shared_ptr< TextTrack > track) override
virtual void OnReadyStateChanged(VideoReadyState old_state, VideoReadyState new_state)
Definition: media_player.cc:91
void OnError(const std::string &error) override
void OnRemoveVideoTrack(std::shared_ptr< MediaTrack > track) override
void RemoveElement(List *list, Elem &&elem)
Definition: utils.h:95
void OnUserEvent(const std::string &name, void *user_data) override
void OnAddTextTrack(std::shared_ptr< TextTrack > track) override
void OnAddAudioTrack(std::shared_ptr< MediaTrack > track) override
virtual void OnAddTextTrack(std::shared_ptr< TextTrack > track)
Definition: media_player.cc:87
virtual void OnPlaybackStateChanged(VideoPlaybackState old_state, VideoPlaybackState new_state)
Definition: media_player.cc:94
#define TO_STRING(E)
void OnAddVideoTrack(std::shared_ptr< MediaTrack > track) override
virtual void OnRemoveTextTrack(std::shared_ptr< TextTrack > track)
Definition: media_player.cc:89
void OnPlaybackStateChanged(VideoPlaybackState old_state, VideoPlaybackState new_state) override
virtual void OnError(const std::string &error)