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 "content/renderer/media/media_recorder_handler.h"
8 #include "base/location.h"
9 #include "base/logging.h"
10 #include "content/renderer/media/video_track_recorder.h"
11 #include "media/base/bind_to_current_loop.h"
12 #include "media/capture/webm_muxer.h"
13 #include "third_party/WebKit/public/platform/WebMediaRecorderHandlerClient.h"
14 #include "third_party/WebKit/public/platform/WebString.h"
18 MediaRecorderHandler::MediaRecorderHandler()
19 : recording_(false), client_(nullptr), weak_factory_(this) {
20 DVLOG(3) << __FUNCTION__
;
23 MediaRecorderHandler::~MediaRecorderHandler() {
24 DCHECK(main_render_thread_checker_
.CalledOnValidThread());
25 // Send a |last_in_slice| to our |client_|.
27 client_
->writeData(nullptr, 0u, true);
30 bool MediaRecorderHandler::canSupportMimeType(
31 const blink::WebString
& mimeType
) {
32 DCHECK(main_render_thread_checker_
.CalledOnValidThread());
33 // TODO(mcasas): For the time being only empty or "video/vp8" are supported.
34 return mimeType
.isEmpty() || mimeType
.utf8().compare("video/vp8") == 0;
37 bool MediaRecorderHandler::initialize(
38 blink::WebMediaRecorderHandlerClient
* client
,
39 const blink::WebMediaStream
& media_stream
,
40 const blink::WebString
& mimeType
) {
41 DCHECK(main_render_thread_checker_
.CalledOnValidThread());
42 if (!canSupportMimeType(mimeType
)) {
43 DLOG(ERROR
) << "Can't support type " << mimeType
.utf8();
46 media_stream_
= media_stream
;
53 bool MediaRecorderHandler::start() {
54 DCHECK(main_render_thread_checker_
.CalledOnValidThread());
59 bool MediaRecorderHandler::start(int timeslice
) {
60 DCHECK(main_render_thread_checker_
.CalledOnValidThread());
62 DCHECK(!media_stream_
.isNull());
64 webm_muxer_
.reset(new media::WebmMuxer(media::BindToCurrentLoop(base::Bind(
65 &MediaRecorderHandler::WriteData
, weak_factory_
.GetWeakPtr()))));
68 blink::WebVector
<blink::WebMediaStreamTrack
> video_tracks
;
69 media_stream_
.videoTracks(video_tracks
);
71 if (video_tracks
.isEmpty()) {
72 // TODO(mcasas): Add audio_tracks and update the code in this function
73 // correspondingly, see http://crbug.com/528519. As of now, only video
74 // tracks are supported.
75 LOG(WARNING
) << "Recording no video tracks is not implemented";
78 // TODO(mcasas): The muxer API supports only one video track. Extend it to
79 // several video tracks, see http://crbug.com/528523.
80 LOG_IF(WARNING
, video_tracks
.size() > 1u) << "Recording multiple video"
81 << " tracks is not implemented. Only recording first video track.";
82 const blink::WebMediaStreamTrack
& video_track
= video_tracks
[0];
83 if (video_track
.isNull())
86 const VideoTrackRecorder::OnEncodedVideoCB on_encoded_video_cb
=
87 base::Bind(&media::WebmMuxer::OnEncodedVideo
,
88 base::Unretained(webm_muxer_
.get()));
90 video_recorders_
.push_back(new VideoTrackRecorder(video_track
,
91 on_encoded_video_cb
));
97 void MediaRecorderHandler::stop() {
98 DCHECK(main_render_thread_checker_
.CalledOnValidThread());
102 video_recorders_
.clear();
103 webm_muxer_
.reset(NULL
);
106 void MediaRecorderHandler::pause() {
107 DCHECK(main_render_thread_checker_
.CalledOnValidThread());
113 void MediaRecorderHandler::resume() {
114 DCHECK(main_render_thread_checker_
.CalledOnValidThread());
120 void MediaRecorderHandler::WriteData(const base::StringPiece
& data
) {
121 DCHECK(main_render_thread_checker_
.CalledOnValidThread());
122 client_
->writeData(data
.data(), data
.size(), false /* lastInSlice */);
125 void MediaRecorderHandler::OnVideoFrameForTesting(
126 const scoped_refptr
<media::VideoFrame
>& frame
,
127 const base::TimeTicks
& timestamp
) {
128 for (auto* recorder
: video_recorders_
)
129 recorder
->OnVideoFrameForTesting(frame
, timestamp
);
132 } // namespace content