summaryrefslogtreecommitdiff
path: root/chromium/media/blink/webmediasource_impl.cc
blob: 67ef90307adc2f2bb25ac1959987bfc4645d60e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "media/blink/webmediasource_impl.h"

#include "base/guid.h"
#include "media/base/mime_util.h"
#include "media/blink/websourcebuffer_impl.h"
#include "media/filters/chunk_demuxer.h"
#include "third_party/blink/public/platform/web_string.h"

using ::blink::WebString;
using ::blink::WebMediaSource;

namespace media {

#define STATIC_ASSERT_MATCHING_STATUS_ENUM(webkit_name, chromium_name) \
  static_assert(static_cast<int>(WebMediaSource::webkit_name) == \
                static_cast<int>(ChunkDemuxer::chromium_name),  \
                "mismatching status enum values: " #webkit_name)
STATIC_ASSERT_MATCHING_STATUS_ENUM(kAddStatusOk, kOk);
STATIC_ASSERT_MATCHING_STATUS_ENUM(kAddStatusNotSupported, kNotSupported);
STATIC_ASSERT_MATCHING_STATUS_ENUM(kAddStatusReachedIdLimit, kReachedIdLimit);
#undef STATIC_ASSERT_MATCHING_STATUS_ENUM

WebMediaSourceImpl::WebMediaSourceImpl(ChunkDemuxer* demuxer)
    : demuxer_(demuxer) {
  DCHECK(demuxer_);
}

WebMediaSourceImpl::~WebMediaSourceImpl() = default;

WebMediaSource::AddStatus WebMediaSourceImpl::AddSourceBuffer(
    const blink::WebString& content_type,
    const blink::WebString& codecs,
    blink::WebSourceBuffer** source_buffer) {
  std::string id = base::GenerateGUID();

  WebMediaSource::AddStatus result = static_cast<WebMediaSource::AddStatus>(
      demuxer_->AddId(id, content_type.Utf8(), codecs.Utf8()));

  if (result == WebMediaSource::kAddStatusOk)
    *source_buffer = new WebSourceBufferImpl(id, demuxer_);

  return result;
}

double WebMediaSourceImpl::Duration() {
  return demuxer_->GetDuration();
}

void WebMediaSourceImpl::SetDuration(double new_duration) {
  DCHECK_GE(new_duration, 0);
  demuxer_->SetDuration(new_duration);
}

void WebMediaSourceImpl::MarkEndOfStream(
    WebMediaSource::EndOfStreamStatus status) {
  PipelineStatus pipeline_status = PIPELINE_OK;

  switch (status) {
    case WebMediaSource::kEndOfStreamStatusNoError:
      break;
    case WebMediaSource::kEndOfStreamStatusNetworkError:
      pipeline_status = CHUNK_DEMUXER_ERROR_EOS_STATUS_NETWORK_ERROR;
      break;
    case WebMediaSource::kEndOfStreamStatusDecodeError:
      pipeline_status = CHUNK_DEMUXER_ERROR_EOS_STATUS_DECODE_ERROR;
      break;
  }

  demuxer_->MarkEndOfStream(pipeline_status);
}

void WebMediaSourceImpl::UnmarkEndOfStream() {
  demuxer_->UnmarkEndOfStream();
}

}  // namespace media