Shaka Player Embedded
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
ffmpeg_demuxer.h
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 #ifndef SHAKA_EMBEDDED_MEDIA_FFMPEG_FFMPEG_DEMUXER_H_
16 #define SHAKA_EMBEDDED_MEDIA_FFMPEG_FFMPEG_DEMUXER_H_
17 
18 extern "C" {
19 #include <libavformat/avformat.h>
20 }
21 
22 #include <memory>
23 #include <string>
24 #include <vector>
25 
26 #include "shaka/media/demuxer.h"
28 #include "src/debug/mutex.h"
29 #include "src/debug/thread.h"
30 #include "src/debug/thread_event.h"
31 
32 namespace shaka {
33 namespace media {
34 namespace ffmpeg {
35 
40 class FFmpegDemuxer : public Demuxer {
41  public:
42  FFmpegDemuxer(Demuxer::Client* client, const std::string& mime_type,
43  const std::string& container);
44  ~FFmpegDemuxer() override;
45 
46  void Reset() override;
47 
48  bool Demux(double timestamp_offset, const uint8_t* data, size_t size,
49  std::vector<std::shared_ptr<EncodedFrame>>* frames) override;
50 
51  private:
52  enum class State {
53  Waiting,
54  Parsing,
55  Errored,
56  Stopping,
57  };
58  template <typename T, void (*Free)(T**)>
59  struct AVFree {
60  void operator()(T* obj) {
61  Free(&obj);
62  }
63  };
64  using CodecParameters =
65  std::unique_ptr<AVCodecParameters,
66  AVFree<AVCodecParameters, &avcodec_parameters_free>>;
67  using FormatContext =
68  std::unique_ptr<AVFormatContext,
69  AVFree<AVFormatContext, &avformat_close_input>>;
70 
71  static int OnRead(void* user, uint8_t* buffer, int size);
72 
73  void ThreadMain();
74 
75  bool ReinitDemuxer();
76  void UpdateEncryptionInfo();
77  void OnError();
78 
79  ThreadEvent<void> signal_;
80  Mutex mutex_;
81  const std::string mime_type_;
82  const std::string container_;
83 
84  // These fields can only be used from the background thread and aren't
85  // protected by the mutex.
86  std::shared_ptr<const StreamInfo> cur_stream_info_;
87  AVIOContext* io_;
88  FormatContext demuxer_ctx_;
89  Demuxer::Client* client_;
90 
91  // These fields are protected by the mutex.
92  std::vector<std::shared_ptr<EncodedFrame>>* output_;
93  double timestamp_offset_;
94  const uint8_t* input_;
95  size_t input_size_;
96  size_t input_pos_;
97  State state_;
98 
99  // Needs to be last so fields are initialized when the thread starts.
100  Thread thread_;
101 };
102 
104  public:
105  bool IsTypeSupported(const std::string& mime_type) const override;
106  bool IsCodecVideo(const std::string& codec) const override;
107 
108  std::unique_ptr<Demuxer> Create(const std::string& mime_type,
109  Demuxer::Client* client) const override;
110 };
111 
112 } // namespace ffmpeg
113 } // namespace media
114 } // namespace shaka
115 
116 #endif // SHAKA_EMBEDDED_MEDIA_FFMPEG_FFMPEG_DEMUXER_H_
FFmpegDemuxer(Demuxer::Client *client, const std::string &mime_type, const std::string &container)
std::list< std::shared_ptr< BaseFrame > > frames
Definition: streams.cc:128
bool Demux(double timestamp_offset, const uint8_t *data, size_t size, std::vector< std::shared_ptr< EncodedFrame >> *frames) override