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 #include "media/capture/webm_muxer.h"
11 WebmMuxer::WebmMuxer(const WriteDataCB
& write_data_callback
)
12 : write_data_callback_(write_data_callback
),
14 DCHECK(thread_checker_
.CalledOnValidThread());
16 segment_
.set_mode(mkvmuxer::Segment::kLive
);
17 segment_
.OutputCues(false);
19 mkvmuxer::SegmentInfo
* const info
= segment_
.GetSegmentInfo();
20 info
->set_writing_app("Chrome");
21 info
->set_muxing_app("Chrome");
24 WebmMuxer::~WebmMuxer() {
25 DCHECK(thread_checker_
.CalledOnValidThread());
29 uint64_t WebmMuxer::AddVideoTrack(const gfx::Size
& frame_size
,
31 DCHECK(thread_checker_
.CalledOnValidThread());
33 const uint64_t track_index
=
34 segment_
.AddVideoTrack(frame_size
.width(), frame_size
.height(), 0);
36 mkvmuxer::VideoTrack
* const video_track
=
37 reinterpret_cast<mkvmuxer::VideoTrack
*>(
38 segment_
.GetTrackByNumber(track_index
));
40 video_track
->set_codec_id(mkvmuxer::Tracks::kVp8CodecId
);
41 DCHECK_EQ(video_track
->crop_right(), 0ull);
42 DCHECK_EQ(video_track
->crop_left(), 0ull);
43 DCHECK_EQ(video_track
->crop_top(), 0ull);
44 DCHECK_EQ(video_track
->crop_bottom(), 0ull);
46 video_track
->set_frame_rate(frame_rate
);
47 video_track
->set_default_duration(base::Time::kMicrosecondsPerSecond
/
49 // Segment's timestamps should be in milliseconds, DCHECK it. See
50 // http://www.webmproject.org/docs/container/#muxer-guidelines
51 DCHECK_EQ(segment_
.GetSegmentInfo()->timecode_scale(), 1000000ull);
56 void WebmMuxer::OnEncodedVideo(uint64_t track_number
,
57 const base::StringPiece
& encoded_data
,
58 base::TimeDelta timestamp
,
60 DCHECK(thread_checker_
.CalledOnValidThread());
61 // |track_number|, a caller-side identifier, cannot be zero (!), see
62 // http://www.matroska.org/technical/specs/index.html#Tracks
63 DCHECK_GT(track_number
, 0u);
64 DCHECK(segment_
.GetTrackByNumber(track_number
));
66 segment_
.AddFrame(reinterpret_cast<const uint8_t*>(encoded_data
.data()),
67 encoded_data
.size(), track_number
, timestamp
.InMilliseconds(), keyframe
);
70 mkvmuxer::int32
WebmMuxer::Write(const void* buf
, mkvmuxer::uint32 len
) {
71 DCHECK(thread_checker_
.CalledOnValidThread());
72 write_data_callback_
.Run(base::StringPiece(reinterpret_cast<const char*>(buf
),
78 mkvmuxer::int64
WebmMuxer::Position() const {
79 return position_
.ValueOrDie();
82 mkvmuxer::int32
WebmMuxer::Position(mkvmuxer::int64 position
) {
83 // The stream is not Seekable() so indicate we cannot set the position.
87 bool WebmMuxer::Seekable() const {
91 void WebmMuxer::ElementStartNotify(mkvmuxer::uint64 element_id
,
92 mkvmuxer::int64 position
) {
93 // This method gets pinged before items are sent to |write_data_callback_|.
94 DCHECK_GE(position
, position_
.ValueOrDefault(0))
95 << "Can't go back in a live WebM stream.";