Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(46)

Side by Side Diff: content/renderer/media/media_recorder_handler.cc

Issue 1534553003: MediaRecorder: correct MIME type parsing (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: tommi@s comments Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 #include "content/renderer/media/media_recorder_handler.h" 5 #include "content/renderer/media/media_recorder_handler.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/location.h" 8 #include "base/location.h"
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "base/strings/string_tokenizer.h" 10 #include "base/strings/string_tokenizer.h"
11 #include "base/strings/string_util.h" 11 #include "base/strings/string_util.h"
12 #include "content/renderer/media/audio_track_recorder.h" 12 #include "content/renderer/media/audio_track_recorder.h"
13 #include "content/renderer/media/media_stream_track.h" 13 #include "content/renderer/media/media_stream_track.h"
14 #include "content/renderer/media/video_track_recorder.h" 14 #include "content/renderer/media/video_track_recorder.h"
15 #include "content/renderer/media/webrtc_uma_histograms.h" 15 #include "content/renderer/media/webrtc_uma_histograms.h"
16 #include "media/audio/audio_parameters.h" 16 #include "media/audio/audio_parameters.h"
17 #include "media/base/audio_bus.h" 17 #include "media/base/audio_bus.h"
18 #include "media/base/bind_to_current_loop.h" 18 #include "media/base/bind_to_current_loop.h"
19 #include "media/base/mime_util.h"
19 #include "media/base/video_frame.h" 20 #include "media/base/video_frame.h"
20 #include "media/capture/webm_muxer.h" 21 #include "media/capture/webm_muxer.h"
21 #include "third_party/WebKit/public/platform/WebMediaRecorderHandlerClient.h" 22 #include "third_party/WebKit/public/platform/WebMediaRecorderHandlerClient.h"
22 #include "third_party/WebKit/public/platform/WebString.h" 23 #include "third_party/WebKit/public/platform/WebString.h"
23 24
24 using base::TimeDelta; 25 using base::TimeDelta;
25 using base::TimeTicks; 26 using base::TimeTicks;
26 27
27 namespace content { 28 namespace content {
28 29
29 MediaRecorderHandler::MediaRecorderHandler() 30 MediaRecorderHandler::MediaRecorderHandler()
30 : use_vp9_(false), 31 : use_vp9_(false),
31 recording_(false), 32 recording_(false),
32 client_(nullptr), 33 client_(nullptr),
33 weak_factory_(this) {} 34 weak_factory_(this) {}
34 35
35 MediaRecorderHandler::~MediaRecorderHandler() { 36 MediaRecorderHandler::~MediaRecorderHandler() {
36 DCHECK(main_render_thread_checker_.CalledOnValidThread()); 37 DCHECK(main_render_thread_checker_.CalledOnValidThread());
37 // Send a |last_in_slice| to our |client_|. 38 // Send a |last_in_slice| to our |client_|.
38 if (client_) 39 if (client_)
39 client_->writeData(nullptr, 0u, true); 40 client_->writeData(nullptr, 0u, true);
40 } 41 }
41 42
42 bool MediaRecorderHandler::canSupportMimeType( 43 bool MediaRecorderHandler::canSupportMimeType(
43 const blink::WebString& mimeType) { 44 const blink::WebString& web_type, const blink::WebString& web_codecs) {
44 DCHECK(main_render_thread_checker_.CalledOnValidThread()); 45 DCHECK(main_render_thread_checker_.CalledOnValidThread());
45 // Ensure we can support each passed MIME type. 46 // An empty |web_type| means MediaRecorderHandler can choose its preferred
46 const std::string input = mimeType.utf8(); // Must outlive tokenizer! 47 // codecs.
47 base::StringTokenizer tokenizer(input, ","); 48 if (web_type.isEmpty())
48 while (tokenizer.GetNext()) { 49 return true;
49 // Strip whitespace. 50
50 const std::string token(base::CollapseWhitespaceASCII( 51 const std::string type(web_type.utf8());
51 tokenizer.token(), true /* trim sequences with line breaks*/)); 52 const bool video = base::EqualsCaseInsensitiveASCII(type, "video/webm");
52 if (!token.empty() && 53 const bool audio =
53 !base::EqualsCaseInsensitiveASCII(token, "video/vp8") && 54 video ? false : base::EqualsCaseInsensitiveASCII(type, "audio/webm");
54 !base::EqualsCaseInsensitiveASCII(token, "video/vp9") && 55 if (!video && !audio)
55 !base::EqualsCaseInsensitiveASCII(token, "audio/opus") && 56 return false;
56 !base::EqualsCaseInsensitiveASCII(token, "video/webm") && 57
57 !base::EqualsCaseInsensitiveASCII(token, "audio/webm")) { 58 // Both |video| and |audio| support empty |codecs|; |type| == "video" supports
59 // vp8, vp9 or opus; |type| = "audio", supports only opus.
60 // http://www.webmproject.org/docs/container Sec:"HTML5 Video Type Parameters"
61 static const char* kVideoCodecs[] = { "vp8", "vp9", "opus" };
62 static const char* kAudioCodecs[] = { "opus" };
63 const char** codecs = video ? &kVideoCodecs[0] : &kAudioCodecs[0];
64 int codecs_count = video ? arraysize(kVideoCodecs) : arraysize(kAudioCodecs);
65
66 std::vector<std::string> codecs_list;
67 media::ParseCodecString(web_codecs.utf8(), &codecs_list, true /* strip */);
68 for (const auto& codec : codecs_list) {
69 const auto found = std::find_if(
70 &codecs[0], &codecs[codecs_count], [&codec](const char* name) {
71 return base::EqualsCaseInsensitiveASCII(codec, name);
72 });
73 if (found == &codecs[codecs_count])
58 return false; 74 return false;
59 }
60 } 75 }
61
62 return true; 76 return true;
63 } 77 }
64 78
65 bool MediaRecorderHandler::initialize( 79 bool MediaRecorderHandler::initialize(
66 blink::WebMediaRecorderHandlerClient* client, 80 blink::WebMediaRecorderHandlerClient* client,
67 const blink::WebMediaStream& media_stream, 81 const blink::WebMediaStream& media_stream,
68 const blink::WebString& mimeType) { 82 const blink::WebString& type,
83 const blink::WebString& codecs) {
69 DCHECK(main_render_thread_checker_.CalledOnValidThread()); 84 DCHECK(main_render_thread_checker_.CalledOnValidThread());
70 // Save histogram data so we can see how much MediaStream Recorder is used. 85 // Save histogram data so we can see how much MediaStream Recorder is used.
71 // The histogram counts the number of calls to the JS API. 86 // The histogram counts the number of calls to the JS API.
72 UpdateWebRTCMethodCount(WEBKIT_MEDIA_STREAM_RECORDER); 87 UpdateWebRTCMethodCount(WEBKIT_MEDIA_STREAM_RECORDER);
73 88
74 if (!canSupportMimeType(mimeType)) { 89 if (!canSupportMimeType(type, codecs)) {
75 DLOG(ERROR) << "Can't support type " << mimeType.utf8(); 90 DLOG(ERROR) << "Can't support " << type.utf8()
91 << ";codecs=" << codecs.utf8();
76 return false; 92 return false;
77 } 93 }
78 use_vp9_ = mimeType.utf8().compare("video/vp9") == 0; 94 use_vp9_ = base::ToLowerASCII(codecs.utf8()).find("vp9") != std::string::npos;
79 media_stream_ = media_stream; 95 media_stream_ = media_stream;
80 DCHECK(client); 96 DCHECK(client);
81 client_ = client; 97 client_ = client;
82 98
83 return true; 99 return true;
84 } 100 }
85 101
86 bool MediaRecorderHandler::start() { 102 bool MediaRecorderHandler::start() {
87 DCHECK(main_render_thread_checker_.CalledOnValidThread()); 103 DCHECK(main_render_thread_checker_.CalledOnValidThread());
88 DCHECK(!recording_); 104 DCHECK(!recording_);
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after
239 recorder->OnData(audio_bus, timestamp); 255 recorder->OnData(audio_bus, timestamp);
240 } 256 }
241 257
242 void MediaRecorderHandler::SetAudioFormatForTesting( 258 void MediaRecorderHandler::SetAudioFormatForTesting(
243 const media::AudioParameters& params) { 259 const media::AudioParameters& params) {
244 for (auto* recorder : audio_recorders_) 260 for (auto* recorder : audio_recorders_)
245 recorder->OnSetFormat(params); 261 recorder->OnSetFormat(params);
246 } 262 }
247 263
248 } // namespace content 264 } // namespace content
OLDNEW
« no previous file with comments | « content/renderer/media/media_recorder_handler.h ('k') | content/renderer/media/media_recorder_handler_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698