| OLD | NEW |
| (Empty) |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef MEDIA_FILTERS_LIBWEBM_MUXER_H_ | |
| 6 #define MEDIA_FILTERS_LIBWEBM_MUXER_H_ | |
| 7 | |
| 8 #include <stdint.h> | |
| 9 | |
| 10 #include <deque> | |
| 11 | |
| 12 #include "base/callback.h" | |
| 13 #include "base/macros.h" | |
| 14 #include "base/numerics/safe_math.h" | |
| 15 #include "base/strings/string_piece.h" | |
| 16 #include "base/threading/thread_checker.h" | |
| 17 #include "base/time/time.h" | |
| 18 #include "media/base/media_export.h" | |
| 19 #include "media/base/video_codecs.h" | |
| 20 #include "third_party/libwebm/source/mkvmuxer.hpp" | |
| 21 | |
| 22 namespace gfx { | |
| 23 class Size; | |
| 24 } // namespace gfx | |
| 25 | |
| 26 namespace media { | |
| 27 | |
| 28 class VideoFrame; | |
| 29 class AudioParameters; | |
| 30 | |
| 31 // Adapter class to manage a WebM container [1], a simplified version of a | |
| 32 // Matroska container [2], composed of an EBML header, and a single Segment | |
| 33 // including at least a Track Section and a number of SimpleBlocks each | |
| 34 // containing a single encoded video or audio frame. WebM container has no | |
| 35 // Trailer. | |
| 36 // Clients will push encoded VPx video frames and Opus audio frames one by one | |
| 37 // via OnEncoded{Video|Audio}(). libwebm will eventually ping the WriteDataCB | |
| 38 // passed on contructor with the wrapped encoded data. | |
| 39 // WebmMuxer is designed for use on a single thread. | |
| 40 // [1] http://www.webmproject.org/docs/container/ | |
| 41 // [2] http://www.matroska.org/technical/specs/index.html | |
| 42 class MEDIA_EXPORT WebmMuxer : public NON_EXPORTED_BASE(mkvmuxer::IMkvWriter) { | |
| 43 public: | |
| 44 // Callback to be called when WebmMuxer is ready to write a chunk of data, | |
| 45 // either any file header or a SingleBlock. | |
| 46 using WriteDataCB = base::Callback<void(base::StringPiece)>; | |
| 47 | |
| 48 // |codec| can be VP8 or VP9 and should coincide with whatever is sent in | |
| 49 // OnEncodedVideo(). | |
| 50 WebmMuxer(VideoCodec codec, | |
| 51 bool has_video_, | |
| 52 bool has_audio_, | |
| 53 const WriteDataCB& write_data_callback); | |
| 54 ~WebmMuxer() override; | |
| 55 | |
| 56 // Functions to add video and audio frames with |encoded_data.data()| | |
| 57 // to WebM Segment. | |
| 58 void OnEncodedVideo(const scoped_refptr<VideoFrame>& video_frame, | |
| 59 scoped_ptr<std::string> encoded_data, | |
| 60 base::TimeTicks timestamp, | |
| 61 bool is_key_frame); | |
| 62 void OnEncodedAudio(const media::AudioParameters& params, | |
| 63 scoped_ptr<std::string> encoded_data, | |
| 64 base::TimeTicks timestamp); | |
| 65 | |
| 66 private: | |
| 67 friend class WebmMuxerTest; | |
| 68 | |
| 69 // Methods for creating and adding video and audio tracks, called upon | |
| 70 // receiving the first frame of a given Track. | |
| 71 // AddVideoTrack adds |frame_size| and |frame_rate| to the Segment | |
| 72 // info, although individual frames passed to OnEncodedVideo() can have any | |
| 73 // frame size. | |
| 74 void AddVideoTrack(const gfx::Size& frame_size, double frame_rate); | |
| 75 void AddAudioTrack(const media::AudioParameters& params); | |
| 76 | |
| 77 // IMkvWriter interface. | |
| 78 mkvmuxer::int32 Write(const void* buf, mkvmuxer::uint32 len) override; | |
| 79 mkvmuxer::int64 Position() const override; | |
| 80 mkvmuxer::int32 Position(mkvmuxer::int64 position) override; | |
| 81 bool Seekable() const override; | |
| 82 void ElementStartNotify(mkvmuxer::uint64 element_id, | |
| 83 mkvmuxer::int64 position) override; | |
| 84 | |
| 85 // Helper to simplify saving frames. | |
| 86 void AddFrame(scoped_ptr<std::string> encoded_data, | |
| 87 uint8_t track_index, | |
| 88 base::TimeTicks timestamp, | |
| 89 bool is_key_frame); | |
| 90 | |
| 91 // Used to DCHECK that we are called on the correct thread. | |
| 92 base::ThreadChecker thread_checker_; | |
| 93 | |
| 94 // Video Codec configured: VP9 if true, otherwise VP8 is used by default. | |
| 95 const bool use_vp9_; | |
| 96 | |
| 97 // Caller-side identifiers to interact with |segment_|, initialised upon | |
| 98 // first frame arrival to Add{Video, Audio}Track(). | |
| 99 uint8_t video_track_index_; | |
| 100 uint8_t audio_track_index_; | |
| 101 | |
| 102 // Origin of times for frame timestamps. | |
| 103 base::TimeTicks first_frame_timestamp_; | |
| 104 base::TimeDelta most_recent_timestamp_; | |
| 105 | |
| 106 // TODO(ajose): Change these when support is added for multiple tracks. | |
| 107 // http://crbug.com/528523 | |
| 108 const bool has_video_; | |
| 109 const bool has_audio_; | |
| 110 | |
| 111 // Callback to dump written data as being called by libwebm. | |
| 112 const WriteDataCB write_data_callback_; | |
| 113 | |
| 114 // Rolling counter of the position in bytes of the written goo. | |
| 115 base::CheckedNumeric<mkvmuxer::int64> position_; | |
| 116 | |
| 117 // The MkvMuxer active element. | |
| 118 mkvmuxer::Segment segment_; | |
| 119 | |
| 120 // Hold on to all encoded video frames to dump them with and when audio is | |
| 121 // received, if expected, since WebM headers can only be written once. | |
| 122 struct EncodedVideoFrame { | |
| 123 EncodedVideoFrame(scoped_ptr<std::string> data, | |
| 124 base::TimeTicks timestamp, | |
| 125 bool is_keyframe); | |
| 126 ~EncodedVideoFrame(); | |
| 127 | |
| 128 scoped_ptr<std::string> data; | |
| 129 base::TimeTicks timestamp; | |
| 130 bool is_keyframe; | |
| 131 | |
| 132 private: | |
| 133 DISALLOW_IMPLICIT_CONSTRUCTORS(EncodedVideoFrame); | |
| 134 }; | |
| 135 std::deque<scoped_ptr<EncodedVideoFrame>> encoded_frames_queue_; | |
| 136 | |
| 137 DISALLOW_COPY_AND_ASSIGN(WebmMuxer); | |
| 138 }; | |
| 139 | |
| 140 } // namespace media | |
| 141 | |
| 142 #endif // MEDIA_FILTERS_LIBWEBM_MUXER_H_ | |
| OLD | NEW |