summaryrefslogtreecommitdiff
path: root/chromium/media/base/media.cc
blob: 9fd89ccc7f02773115a30e7f5f9d93da555c1919 (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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// 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/base/media.h"

#include "base/command_line.h"
#include "base/lazy_instance.h"
#include "base/macros.h"
#include "base/metrics/field_trial.h"
#include "base/trace_event/trace_event.h"
#include "media/base/media_switches.h"
#include "media/base/yuv_convert.h"

#if defined(OS_ANDROID)
#include "base/android/build_info.h"
#include "media/base/android/media_codec_util.h"
#endif

#if !defined(MEDIA_DISABLE_FFMPEG)
#include "media/ffmpeg/ffmpeg_common.h"
#endif

namespace media {

// Media must only be initialized once, so use a LazyInstance to ensure this.
class MediaInitializer {
 public:
  void enable_platform_decoder_support() {
    has_platform_decoder_support_ = true;
  }

  bool has_platform_decoder_support() { return has_platform_decoder_support_; }

 private:
  friend struct base::DefaultLazyInstanceTraits<MediaInitializer>;

  MediaInitializer() {
    TRACE_EVENT_WARMUP_CATEGORY("audio");
    TRACE_EVENT_WARMUP_CATEGORY("media");

    // Perform initialization of libraries which require runtime CPU detection.
    InitializeCPUSpecificYUVConversions();

#if !defined(MEDIA_DISABLE_FFMPEG)
    // Initialize CPU flags outside of the sandbox as this may query /proc for
    // details on the current CPU for NEON, VFP, etc optimizations.
    av_get_cpu_flags();

    // Disable logging as it interferes with layout tests.
    av_log_set_level(AV_LOG_QUIET);

#if defined(ALLOCATOR_SHIM)
    // Remove allocation limit from ffmpeg, so calls go down to shim layer.
    av_max_alloc(0);
#endif  // defined(ALLOCATOR_SHIM)

#endif  // !defined(MEDIA_DISABLE_FFMPEG)
  }

  ~MediaInitializer() {
    NOTREACHED() << "MediaInitializer should be leaky!";
  }

  bool has_platform_decoder_support_ = false;

  DISALLOW_COPY_AND_ASSIGN(MediaInitializer);
};

static base::LazyInstance<MediaInitializer>::Leaky g_media_library =
    LAZY_INSTANCE_INITIALIZER;

void InitializeMediaLibrary() {
  g_media_library.Get();
}

#if defined(OS_ANDROID)
void EnablePlatformDecoderSupport() {
  g_media_library.Pointer()->enable_platform_decoder_support();
}

bool HasPlatformDecoderSupport() {
  return g_media_library.Pointer()->has_platform_decoder_support();
}

bool PlatformHasOpusSupport() {
  return base::android::BuildInfo::GetInstance()->sdk_int() >= 21;
}

bool IsUnifiedMediaPipelineEnabled() {
  // TODO(dalecurtis): This experiment is temporary and should be removed once
  // we have enough data to support the primacy of the unified media pipeline;
  // see http://crbug.com/533190 for details.
  //
  // Note: It's important to query the field trial state first, to ensure that
  // UMA reports the correct group.
  const std::string group_name =
      base::FieldTrialList::FindFullName("UnifiedMediaPipelineTrial");
  const bool disabled_via_cli =
      base::CommandLine::ForCurrentProcess()->HasSwitch(
          switches::kDisableUnifiedMediaPipeline);
  // TODO(watk, dalecurtis): AVDA has bugs on API level 16 and 17 so it's
  // disabled for now. http://crbug.com/597467
  const bool api_level_supported =
      base::android::BuildInfo::GetInstance()->sdk_int() >= 18;

  return !disabled_via_cli && api_level_supported &&
         !base::StartsWith(group_name, "Disabled",
                           base::CompareCase::SENSITIVE);
}

bool ArePlatformDecodersAvailable() {
  return IsUnifiedMediaPipelineEnabled()
             ? HasPlatformDecoderSupport()
             : MediaCodecUtil::IsMediaCodecAvailable();
}
#endif

}  // namespace media