Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / media / blink / webmediasource_impl.cc
blob6e899568e2553a2bf36aa0eb330f998011e07bed
1 // Copyright 2013 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/blink/webmediasource_impl.h"
7 #include "base/guid.h"
8 #include "media/base/mime_util.h"
9 #include "media/blink/websourcebuffer_impl.h"
10 #include "media/filters/chunk_demuxer.h"
11 #include "third_party/WebKit/public/platform/WebCString.h"
12 #include "third_party/WebKit/public/platform/WebString.h"
14 using ::blink::WebString;
15 using ::blink::WebMediaSource;
17 namespace media {
19 #define STATIC_ASSERT_MATCHING_STATUS_ENUM(webkit_name, chromium_name) \
20 static_assert(static_cast<int>(WebMediaSource::webkit_name) == \
21 static_cast<int>(ChunkDemuxer::chromium_name), \
22 "mismatching status enum values: " #webkit_name)
23 STATIC_ASSERT_MATCHING_STATUS_ENUM(AddStatusOk, kOk);
24 STATIC_ASSERT_MATCHING_STATUS_ENUM(AddStatusNotSupported, kNotSupported);
25 STATIC_ASSERT_MATCHING_STATUS_ENUM(AddStatusReachedIdLimit, kReachedIdLimit);
26 #undef STATIC_ASSERT_MATCHING_STATUS_ENUM
28 WebMediaSourceImpl::WebMediaSourceImpl(ChunkDemuxer* demuxer,
29 const scoped_refptr<MediaLog>& media_log)
30 : demuxer_(demuxer), media_log_(media_log) {
31 DCHECK(demuxer_);
34 WebMediaSourceImpl::~WebMediaSourceImpl() {}
36 WebMediaSource::AddStatus WebMediaSourceImpl::addSourceBuffer(
37 const blink::WebString& type,
38 const blink::WebString& codecs,
39 blink::WebSourceBuffer** source_buffer) {
40 std::string id = base::GenerateGUID();
42 std::vector<std::string> parsed_codec_ids;
43 media::ParseCodecString(codecs.utf8().data(), &parsed_codec_ids, false);
45 WebMediaSource::AddStatus result =
46 static_cast<WebMediaSource::AddStatus>(
47 demuxer_->AddId(id, type.utf8().data(), parsed_codec_ids));
49 if (result == WebMediaSource::AddStatusOk)
50 *source_buffer = new WebSourceBufferImpl(id, demuxer_);
52 return result;
55 double WebMediaSourceImpl::duration() {
56 return demuxer_->GetDuration();
59 void WebMediaSourceImpl::setDuration(double new_duration) {
60 DCHECK_GE(new_duration, 0);
61 demuxer_->SetDuration(new_duration);
64 void WebMediaSourceImpl::markEndOfStream(
65 WebMediaSource::EndOfStreamStatus status) {
66 PipelineStatus pipeline_status = PIPELINE_OK;
68 switch (status) {
69 case WebMediaSource::EndOfStreamStatusNoError:
70 break;
71 case WebMediaSource::EndOfStreamStatusNetworkError:
72 pipeline_status = PIPELINE_ERROR_NETWORK;
73 break;
74 case WebMediaSource::EndOfStreamStatusDecodeError:
75 pipeline_status = PIPELINE_ERROR_DECODE;
76 break;
79 demuxer_->MarkEndOfStream(pipeline_status);
82 void WebMediaSourceImpl::unmarkEndOfStream() {
83 demuxer_->UnmarkEndOfStream();
86 } // namespace media