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.
5 #ifndef MEDIA_FILTERS_LIBWEBM_MUXER_H_
6 #define MEDIA_FILTERS_LIBWEBM_MUXER_H_
10 #include "base/callback.h"
11 #include "base/gtest_prod_util.h"
12 #include "base/numerics/safe_math.h"
13 #include "base/strings/string_piece.h"
14 #include "base/threading/thread_checker.h"
15 #include "base/time/time.h"
16 #include "media/base/media_export.h"
17 #include "third_party/libwebm/source/mkvmuxer.hpp"
18 #include "ui/gfx/geometry/size.h"
24 // Adapter class to manage a WebM container [1], a simplified version of a
25 // Matroska container [2], composed of an EBML header, and a single Segment
26 // including at least a Track Section and a number of SimpleBlocks each
27 // containing a single encoded video frame. WebM container has no Trailer.
28 // Clients will push encoded VPx video frames one by one via the
29 // OnEncodedVideo(). libwebm will eventually ping the WriteDataCB passed on
30 // contructor with the wrapped encoded data. All operations must happen in a
31 // single thread, where WebmMuxer is created and destroyed.
32 // [1] http://www.webmproject.org/docs/container/
33 // [2] http://www.matroska.org/technical/specs/index.html
34 // TODO(mcasas): Add support for Audio muxing.
35 class MEDIA_EXPORT WebmMuxer
: public NON_EXPORTED_BASE(mkvmuxer::IMkvWriter
) {
37 // Callback to be called when WebmMuxer is ready to write a chunk of data,
38 // either any file header or a SingleBlock. The chunk is described as a byte
39 // array and a byte length.
40 using WriteDataCB
= base::Callback
<void(const base::StringPiece
&)>;
42 explicit WebmMuxer(const WriteDataCB
& write_data_callback
);
43 ~WebmMuxer() override
;
45 // Adds a |video_frame| with |encoded_data.data()| to WebM Segment.
46 // TODO(mcasas): Revisit how |encoded_data| is passed once the whole recording
47 // chain is setup (http://crbug.com/262211).
48 void OnEncodedVideo(const scoped_refptr
<VideoFrame
>& video_frame
,
49 const base::StringPiece
& encoded_data
,
50 base::TimeTicks timestamp
,
54 friend class WebmMuxerTest
;
56 // Creates and adds a new video track. Called upon receiving the first
57 // frame of a given Track, adds |frame_size| and |frame_rate| to the Segment
58 // info, although individual frames passed to OnEncodedVideo() can have any
59 // frame size. Returns OnEncodedVideo() callback with a |track_number| bound
61 void AddVideoTrack(const gfx::Size
& frame_size
, double frame_rate
);
63 // IMkvWriter interface.
64 mkvmuxer::int32
Write(const void* buf
, mkvmuxer::uint32 len
) override
;
65 mkvmuxer::int64
Position() const override
;
66 mkvmuxer::int32
Position(mkvmuxer::int64 position
) override
;
67 bool Seekable() const override
;
68 void ElementStartNotify(mkvmuxer::uint64 element_id
,
69 mkvmuxer::int64 position
) override
;
71 // Used to DCHECK that we are called on the correct thread.
72 base::ThreadChecker thread_checker_
;
74 // A caller-side identifier to interact with |segment_|, initialised upon
75 // first frame arrival by AddVideoTrack().
76 uint64_t track_index_
;
78 // Origin of times for frame timestamps.
79 base::TimeTicks first_frame_timestamp_
;
81 // Callback to dump written data as being called by libwebm.
82 const WriteDataCB write_data_callback_
;
84 // Rolling counter of the position in bytes of the written goo.
85 base::CheckedNumeric
<mkvmuxer::int64
> position_
;
87 // The MkvMuxer active element.
88 mkvmuxer::Segment segment_
;
90 DISALLOW_COPY_AND_ASSIGN(WebmMuxer
);
95 #endif // MEDIA_FILTERS_LIBWEBM_MUXER_H_