OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef MEDIA_FILTERS_LIBWEBM_MUXER_H_ | 5 #ifndef MEDIA_FILTERS_LIBWEBM_MUXER_H_ |
6 #define MEDIA_FILTERS_LIBWEBM_MUXER_H_ | 6 #define MEDIA_FILTERS_LIBWEBM_MUXER_H_ |
7 | 7 |
8 #include <set> | 8 #include <set> |
9 | 9 |
10 #include "base/callback.h" | 10 #include "base/callback.h" |
(...skipping 18 matching lines...) Expand all Loading... |
29 // containing a single encoded video frame. WebM container has no Trailer. | 29 // containing a single encoded video frame. WebM container has no Trailer. |
30 // Clients will push encoded VPx video frames one by one via OnEncodedVideo(). | 30 // Clients will push encoded VPx video frames one by one via OnEncodedVideo(). |
31 // libwebm will eventually ping the WriteDataCB passed on contructor with the | 31 // libwebm will eventually ping the WriteDataCB passed on contructor with the |
32 // wrapped encoded data. | 32 // wrapped encoded data. |
33 // WebmMuxer is designed for single thread use throughout. | 33 // WebmMuxer is designed for single thread use throughout. |
34 // [1] http://www.webmproject.org/docs/container/ | 34 // [1] http://www.webmproject.org/docs/container/ |
35 // [2] http://www.matroska.org/technical/specs/index.html | 35 // [2] http://www.matroska.org/technical/specs/index.html |
36 // TODO(mcasas): Add support for Audio muxing. | 36 // TODO(mcasas): Add support for Audio muxing. |
37 class MEDIA_EXPORT WebmMuxer : public NON_EXPORTED_BASE(mkvmuxer::IMkvWriter) { | 37 class MEDIA_EXPORT WebmMuxer : public NON_EXPORTED_BASE(mkvmuxer::IMkvWriter) { |
38 public: | 38 public: |
| 39 using EncodedVideoCB = |
| 40 base::Callback<void(const scoped_refptr<VideoFrame>& video_frame, |
| 41 scoped_ptr<std::string> encoded_data, |
| 42 base::TimeTicks timestamp, |
| 43 bool is_key_frame)>; |
| 44 |
39 // Callback to be called when WebmMuxer is ready to write a chunk of data, | 45 // Callback to be called when WebmMuxer is ready to write a chunk of data, |
40 // either any file header or a SingleBlock. | 46 // either any file header or a SingleBlock. |
41 using WriteDataCB = base::Callback<void(base::StringPiece)>; | 47 using WriteDataCB = base::Callback<void(base::StringPiece)>; |
42 | 48 |
43 explicit WebmMuxer(const WriteDataCB& write_data_callback); | 49 explicit WebmMuxer(const WriteDataCB& write_data_callback); |
44 ~WebmMuxer() override; | 50 ~WebmMuxer() override; |
45 | 51 |
| 52 EncodedVideoCB GenerateOnEncodedVideoCallback(); |
| 53 |
| 54 private: |
| 55 friend class WebmMuxerTest; |
| 56 struct VideoMuxerArg { |
| 57 VideoMuxerArg( |
| 58 const base::TimeTicks& first_frame_timestamp, uint64 track_number) |
| 59 : first_frame_timestamp(first_frame_timestamp), |
| 60 track_number(track_number) {} |
| 61 |
| 62 base::TimeTicks first_frame_timestamp; |
| 63 uint64 track_number; |
| 64 }; |
| 65 |
46 // Adds a |video_frame| with |encoded_data.data()| to WebM Segment. | 66 // Adds a |video_frame| with |encoded_data.data()| to WebM Segment. |
47 void OnEncodedVideo(const scoped_refptr<VideoFrame>& video_frame, | 67 void OnEncodedVideo(int track_index, |
| 68 const scoped_refptr<VideoFrame>& video_frame, |
48 scoped_ptr<std::string> encoded_data, | 69 scoped_ptr<std::string> encoded_data, |
49 base::TimeTicks timestamp, | 70 base::TimeTicks timestamp, |
50 bool is_key_frame); | 71 bool is_key_frame); |
51 | 72 |
52 private: | 73 int GetNextVideoTrackIndex() const; |
53 friend class WebmMuxerTest; | |
54 | 74 |
55 // Creates and adds a new video track. Called upon receiving the first | 75 // Creates and adds a new video track. Called upon receiving the first |
56 // frame of a given Track, adds |frame_size| and |frame_rate| to the Segment | 76 // frame of a given Track, adds |frame_size| and |frame_rate| to the Segment |
57 // info, although individual frames passed to OnEncodedVideo() can have any | 77 // info, although individual frames passed to OnEncodedVideo() can have any |
58 // frame size. | 78 // frame size. |
59 void AddVideoTrack(const gfx::Size& frame_size, double frame_rate); | 79 uint64 AddVideoTrack(const gfx::Size& frame_size, double frame_rate); |
60 | 80 |
61 // IMkvWriter interface. | 81 // IMkvWriter interface. |
62 mkvmuxer::int32 Write(const void* buf, mkvmuxer::uint32 len) override; | 82 mkvmuxer::int32 Write(const void* buf, mkvmuxer::uint32 len) override; |
63 mkvmuxer::int64 Position() const override; | 83 mkvmuxer::int64 Position() const override; |
64 mkvmuxer::int32 Position(mkvmuxer::int64 position) override; | 84 mkvmuxer::int32 Position(mkvmuxer::int64 position) override; |
65 bool Seekable() const override; | 85 bool Seekable() const override; |
66 void ElementStartNotify(mkvmuxer::uint64 element_id, | 86 void ElementStartNotify(mkvmuxer::uint64 element_id, |
67 mkvmuxer::int64 position) override; | 87 mkvmuxer::int64 position) override; |
68 | 88 |
69 // Used to DCHECK that we are called on the correct thread. | 89 // Used to DCHECK that we are called on the correct thread. |
70 base::ThreadChecker thread_checker_; | 90 base::ThreadChecker thread_checker_; |
71 | 91 |
72 // A caller-side identifier to interact with |segment_|, initialised upon | 92 // Arguments for each tracks to mkvmuxer. |
73 // first frame arrival by AddVideoTrack(). | 93 std::vector<VideoMuxerArg> video_muxer_args_; |
74 uint64_t track_index_; | |
75 | |
76 // Origin of times for frame timestamps. | |
77 base::TimeTicks first_frame_timestamp_; | |
78 | 94 |
79 // Callback to dump written data as being called by libwebm. | 95 // Callback to dump written data as being called by libwebm. |
80 const WriteDataCB write_data_callback_; | 96 const WriteDataCB write_data_callback_; |
81 | 97 |
82 // Rolling counter of the position in bytes of the written goo. | 98 // Rolling counter of the position in bytes of the written goo. |
83 base::CheckedNumeric<mkvmuxer::int64> position_; | 99 base::CheckedNumeric<mkvmuxer::int64> position_; |
84 | 100 |
85 // The MkvMuxer active element. | 101 // The MkvMuxer active element. |
86 mkvmuxer::Segment segment_; | 102 mkvmuxer::Segment segment_; |
87 | 103 |
88 DISALLOW_COPY_AND_ASSIGN(WebmMuxer); | 104 DISALLOW_COPY_AND_ASSIGN(WebmMuxer); |
89 }; | 105 }; |
90 | 106 |
91 } // namespace media | 107 } // namespace media |
92 | 108 |
93 #endif // MEDIA_FILTERS_LIBWEBM_MUXER_H_ | 109 #endif // MEDIA_FILTERS_LIBWEBM_MUXER_H_ |
OLD | NEW |