AlexaClientSDK  3.0.0
A cross-platform, modular SDK for interacting with the Alexa Voice Service
FFmpegDecoder.h
Go to the documentation of this file.
1 /*
2  * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License").
5  * You may not use this file except in compliance with the License.
6  * A copy of the License is located at
7  *
8  * http://aws.amazon.com/apache2.0/
9  *
10  * or in the "license" file accompanying this file. This file is distributed
11  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12  * express or implied. See the License for the specific language governing
13  * permissions and limitations under the License.
14  */
15 #ifndef ALEXA_CLIENT_SDK_MEDIAPLAYER_ANDROIDSLESMEDIAPLAYER_INCLUDE_ANDROIDSLESMEDIAPLAYER_FFMPEGDECODER_H_
16 #define ALEXA_CLIENT_SDK_MEDIAPLAYER_ANDROIDSLESMEDIAPLAYER_INCLUDE_ANDROIDSLESMEDIAPLAYER_FFMPEGDECODER_H_
17 
18 #include <atomic>
19 #include <chrono>
20 #include <condition_variable>
21 #include <iostream>
22 #include <memory>
23 #include <mutex>
24 #include <string>
25 #include <utility>
26 
27 extern "C" {
28 #include <libavutil/samplefmt.h>
29 }
30 
35 
36 struct AVCodec;
37 struct AVCodecContext;
38 struct AVDictionary;
39 struct AVFormatContext;
40 struct AVFrame;
41 struct AVInputFormat;
42 struct AVIOContext;
43 struct SwrContext;
44 struct AVFilterGraph;
45 struct AVFilterContext;
46 
47 namespace alexaClientSDK {
48 namespace mediaPlayer {
49 namespace android {
50 
52 using LayoutMask = int64_t;
53 
65 public:
73  static std::unique_ptr<FFmpegDecoder> create(
74  std::unique_ptr<FFmpegInputControllerInterface> inputController,
75  const PlaybackConfiguration& outputConfig,
77 
80  std::pair<Status, size_t> read(Byte* buffer, size_t size) override;
81  void abort() override;
83 
97  bool shouldInterruptFFmpeg();
98 
99 private:
109  std::unique_ptr<FFmpegInputControllerInterface> inputController,
110  AVSampleFormat format,
111  LayoutMask layout,
112  int sampleRate,
114 
128  enum class DecodingState {
130  DECODING,
132  FLUSHING_DECODER,
134  FLUSHING_RESAMPLER,
136  INITIALIZING,
138  FINISHED,
140  INVALID
141  };
142 
144  friend std::ostream& operator<<(std::ostream& stream, const DecodingState state);
145 
151  void setState(DecodingState nextState);
152 
156  class UnreadData {
157  public:
165  UnreadData(AVSampleFormat format, LayoutMask layout, int sampleRate);
166 
172  AVFrame& getFrame();
173 
179  int getOffset() const;
180 
186  void resize(size_t minimumCapacity);
187 
193  bool isEmpty() const;
194 
200  void setOffset(int offset);
201 
202  private:
204  size_t m_capacity;
205 
207  int m_offset;
208 
210  std::shared_ptr<AVFrame> m_frame;
211  };
212 
222  size_t readData(Byte* buffer, size_t size, size_t bytesRead);
223 
229  void resample(std::shared_ptr<AVFrame> inputFrame);
230 
232  void decode();
233 
235  void next();
236 
238  void sleep();
239 
245  void readDecodedFrame(std::shared_ptr<AVFrame>& decodedFrame);
246 
252  void initialize();
253 
259  bool initializeFilters();
260 
273  bool transitionStateUsingStatus(int status, DecodingState nextState, const std::string& functionName);
274 
276  std::atomic<DecodingState> m_state;
277 
279  std::unique_ptr<FFmpegInputControllerInterface> m_inputController;
280 
282  const AVSampleFormat m_outputFormat;
283 
285  const LayoutMask m_outputLayout;
286 
288  const int m_outputRate;
289 
291  std::shared_ptr<AVFormatContext> m_formatContext;
292 
294  std::shared_ptr<AVCodecContext> m_codecContext;
295 
297  std::shared_ptr<SwrContext> m_swrContext;
298 
300  std::shared_ptr<AVFilterGraph> m_filterGraph;
301 
305  std::shared_ptr<AVFilterContext> m_filterInput;
306  std::shared_ptr<AVFilterContext> m_filterOutput;
307 
309  UnreadData m_unreadData;
310 
312  size_t m_retryCount;
313 
315  std::condition_variable m_abortCondition;
316 
318  std::chrono::time_point<std::chrono::steady_clock> m_initializeStartTime;
319 
322 
324  long long m_decodedSampleTime = 0;
325 };
326 
327 } // namespace android
328 } // namespace mediaPlayer
329 } // namespace alexaClientSDK
330 
331 #endif // ALEXA_CLIENT_SDK_MEDIAPLAYER_ANDROIDSLESMEDIAPLAYER_INCLUDE_ANDROIDSLESMEDIAPLAYER_FFMPEGDECODER_H_
SourceConfig emptySourceConfig()
Definition: SourceConfig.h:98
uint8_t Byte
Represents one byte of data.
Definition: DecoderInterface.h:38
::std::string string
Definition: gtest-port.h:1097
std::pair< Status, size_t > read(Byte *buffer, size_t size) override
int64_t LayoutMask
The layout mask representing which channels should be enabled.
Definition: FFmpegDecoder.h:52
Whether or not curl logs should be emitted.
Definition: AVSConnectionManager.h:36
static std::unique_ptr< FFmpegDecoder > create(std::unique_ptr< FFmpegInputControllerInterface > inputController, const PlaybackConfiguration &outputConfig, const avsCommon::utils::mediaPlayer::SourceConfig &config=avsCommon::utils::mediaPlayer::emptySourceConfig())
friend std::ostream & operator<<(std::ostream &stream, const DecodingState state)
Friend relationship to allow accessing State to convert it to a string for logging.

AlexaClientSDK 3.0.0 - Copyright 2016-2022 Amazon.com, Inc. or its affiliates. All Rights Reserved. Licensed under the Apache License, Version 2.0