Shaka Player Embedded
audio_renderer_common.h
Go to the documentation of this file.
1 // Copyright 2020 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 #ifndef SHAKA_EMBEDDED_MEDIA_AUDIO_RENDERER_COMMON_H_
16 #define SHAKA_EMBEDDED_MEDIA_AUDIO_RENDERER_COMMON_H_
17 
18 #include <memory>
19 
20 #include "shaka/media/frames.h"
21 #include "shaka/media/renderer.h"
22 #include "src/debug/mutex.h"
23 #include "src/debug/thread.h"
24 #include "src/debug/thread_event.h"
25 #include "src/util/buffer_writer.h"
26 #include "src/util/clock.h"
27 
28 namespace shaka {
29 namespace media {
30 
57  public:
59  ~AudioRendererCommon() override;
60 
61  void SetPlayer(const MediaPlayer* player) override;
62  void Attach(const DecodedStream* stream) override;
63  void Detach() override;
64 
65  double Volume() const override;
66  void SetVolume(double volume) override;
67  bool Muted() const override;
68  void SetMuted(bool muted) override;
69 
70  protected:
75  void Stop();
76 
77  private:
78  enum class SyncStatus {
79  Success,
80  NoFrame,
81  FatalError,
82  };
83 
96  virtual bool InitDevice(std::shared_ptr<DecodedFrame> frame,
97  double volume) = 0;
98 
107  virtual bool AppendBuffer(const uint8_t* data, size_t size) = 0;
108 
110  virtual void ClearBuffer() = 0;
111 
117  virtual size_t GetBytesBuffered() const = 0;
118 
120  virtual void SetDeviceState(bool is_playing) = 0;
121 
127  virtual void UpdateVolume(double volume) = 0;
128 
130  bool FillSilence(size_t bytes);
131 
137  bool IsFrameSimilar(std::shared_ptr<DecodedFrame> frame1,
138  std::shared_ptr<DecodedFrame> frame2) const;
139 
140  bool WriteFrame(std::shared_ptr<DecodedFrame> frame, size_t sync_bytes);
141 
142  void SetClock(const util::Clock* clock);
143 
144  void ThreadMain();
145 
146 
147  void OnPlaybackStateChanged(VideoPlaybackState old_state,
148  VideoPlaybackState new_state) override;
149  void OnPlaybackRateChanged(double old_rate, double new_rate) override;
150  void OnSeeking() override;
151 
152 
154  mutable Mutex mutex_;
155  ThreadEvent<void> on_play_;
156 
157  const util::Clock* clock_;
158  const MediaPlayer* player_;
159  const DecodedStream* input_;
160 
161  std::shared_ptr<DecodedFrame> cur_frame_;
162  double sync_time_;
163  uint64_t bytes_written_;
164  double volume_;
165  bool muted_;
166  bool needs_resync_;
167  bool shutdown_;
168 
169  Thread thread_;
170 };
171 
172 } // namespace media
173 } // namespace shaka
174 
175 #endif // SHAKA_EMBEDDED_MEDIA_AUDIO_RENDERER_COMMON_H_
void SetPlayer(const MediaPlayer *player) override
std::shared_ptr< shaka::media::DecodedFrame > frame
void SetVolume(double volume) override
void Attach(const DecodedStream *stream) override