Skip to content

Commit

Permalink
[chromecast] Add cast_shell --accept-resource-provider switch and a u…
Browse files Browse the repository at this point in the history
…tility function, GetSwitchValueBoolean.

Also has the cast_shell flag --accept-resource-provider imply
--alsa-check-close-timeout=0 unless the close timeout is explicitly
overridden.

BUG=internal b/26091363
TEST=pepperoni build
     cast_base_unittests --gtest_filter=ChromecastSwitchesTest.*

Review URL: https://codereview.chromium.org/1767603003

Cr-Commit-Position: refs/heads/master@{#380024}
  • Loading branch information
jyw authored and Commit bot committed Mar 9, 2016
1 parent c13b8c0 commit b92b0b2
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 3 deletions.
1 change: 1 addition & 0 deletions chromecast/base/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ source_set("test_support") {
test("cast_base_unittests") {
sources = [
"bind_to_task_runner_unittest.cc",
"chromecast_switches_unittest.cc",
"device_capabilities_impl_unittest.cc",
"error_codes_unittest.cc",
"path_utils_unittest.cc",
Expand Down
40 changes: 39 additions & 1 deletion chromecast/base/chromecast_switches.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,16 @@

#include "chromecast/base/chromecast_switches.h"

#include "base/command_line.h"

namespace switches {

// Value indicating whether flag from command line switch is true.
const char kSwitchValueTrue[] = "true";

// Value indicating whether flag from command line switch is false.
const char kSwitchValueFalse[] = "false";

// Enable the CMA media pipeline.
const char kEnableCmaMediaPipeline[] = "enable-cma-media-pipeline";

Expand All @@ -32,6 +40,11 @@ const char kLastLaunchedApp[] = "last-launched-app";
// started.
const char kPreviousApp[] = "previous-app";

// Flag indicating that a resource provider must be set up to provide cast
// receiver with resources. Apps cannot start until provided resources.
// This flag implies --alsa-check-close-timeout=0.
const char kAcceptResourceProvider[] = "accept-resource-provider";

// Size of the ALSA output buffer in frames. This directly sets the latency of
// the output device. Latency can be calculated by multiplying the sample rate
// by the output buffer size.
Expand All @@ -48,11 +61,36 @@ const char kAlsaOutputStartThreshold[] = "alsa-output-start-threshold";
const char kAlsaOutputAvailMin[] = "alsa-output-avail-min";

// Time in ms to wait before closing the PCM handle when no more mixer inputs
// remain.
// remain. Assumed to be 0 if --accept-resource-provider is present.
const char kAlsaCheckCloseTimeout[] = "alsa-check-close-timeout";

// Number of channels on the alsa output device that the stream mixer uses.
// Default is 2 channels.
const char kAlsaNumOutputChannels[] = "alsa-num-output-channels";

} // namespace switches

namespace chromecast {

bool GetSwitchValueBoolean(const std::string& switch_string,
const bool default_value) {
const base::CommandLine* command_line =
base::CommandLine::ForCurrentProcess();
if (command_line->HasSwitch(switch_string)) {
if (command_line->GetSwitchValueASCII(switch_string) !=
switches::kSwitchValueTrue &&
command_line->GetSwitchValueASCII(switch_string) !=
switches::kSwitchValueFalse &&
command_line->GetSwitchValueASCII(switch_string) != "") {
LOG(WARNING) << "Invalid switch value " << switch_string << "="
<< command_line->GetSwitchValueASCII(switch_string)
<< "; assuming default value of " << default_value;
return default_value;
}
return command_line->GetSwitchValueASCII(switch_string) !=
switches::kSwitchValueFalse;
}
return default_value;
}

} // namespace chromecast
21 changes: 21 additions & 0 deletions chromecast/base/chromecast_switches.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,16 @@
#ifndef CHROMECAST_BASE_CHROMECAST_SWITCHES_H_
#define CHROMECAST_BASE_CHROMECAST_SWITCHES_H_

#include <string>

#include "build/build_config.h"

namespace switches {

// Switch values
extern const char kSwitchValueTrue[];
extern const char kSwitchValueFalse[];

// Media switches
extern const char kEnableCmaMediaPipeline[];
extern const char kHdmiSinkSupportedCodecs[];
Expand All @@ -29,6 +35,9 @@ extern const char kAllowHiddenMediaPlayback[];
extern const char kLastLaunchedApp[];
extern const char kPreviousApp[];

// Cast Receiver switches
extern const char kAcceptResourceProvider[];

// ALSA-based CMA switches. (Only valid for audio products.)
extern const char kAlsaOutputBufferSize[];
extern const char kAlsaOutputPeriodSize[];
Expand All @@ -39,4 +48,16 @@ extern const char kAlsaNumOutputChannels[];

} // namespace switches

namespace chromecast {

// Gets boolean value from switch |switch_string|.
// --|switch_string| -> true
// --|switch_string|="true" -> true
// --|switch_string|="false" -> false
// no switch named |switch_string| -> |default_value|
bool GetSwitchValueBoolean(const std::string& switch_string,
const bool default_value);

} // namespace chromecast

#endif // CHROMECAST_BASE_CHROMECAST_SWITCHES_H_
61 changes: 61 additions & 0 deletions chromecast/base/chromecast_switches_unittest.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright 2016 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 "base/command_line.h"
#include "chromecast/base/chromecast_switches.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace chromecast {

TEST(ChromecastSwitchesTest, NoSwitch) {
if (base::CommandLine::CommandLine::InitializedForCurrentProcess())
base::CommandLine::Reset();
base::CommandLine::Init(0, nullptr);
EXPECT_TRUE(GetSwitchValueBoolean(switches::kEnableCmaMediaPipeline, true));
EXPECT_FALSE(GetSwitchValueBoolean(switches::kEnableCmaMediaPipeline, false));
}

TEST(ChromecastSwitchesTest, NoSwitchValue) {
if (base::CommandLine::CommandLine::InitializedForCurrentProcess())
base::CommandLine::Reset();
base::CommandLine::Init(0, nullptr);
base::CommandLine* cl = base::CommandLine::ForCurrentProcess();
cl->AppendSwitch(switches::kEnableCmaMediaPipeline);
EXPECT_TRUE(GetSwitchValueBoolean(switches::kEnableCmaMediaPipeline, true));
EXPECT_TRUE(GetSwitchValueBoolean(switches::kEnableCmaMediaPipeline, false));
}

TEST(ChromecastSwitchesTest, SwitchValueTrue) {
if (base::CommandLine::CommandLine::InitializedForCurrentProcess())
base::CommandLine::Reset();
base::CommandLine::Init(0, nullptr);
base::CommandLine* cl = base::CommandLine::ForCurrentProcess();
cl->AppendSwitchASCII(switches::kEnableCmaMediaPipeline,
switches::kSwitchValueTrue);
EXPECT_TRUE(GetSwitchValueBoolean(switches::kEnableCmaMediaPipeline, true));
EXPECT_TRUE(GetSwitchValueBoolean(switches::kEnableCmaMediaPipeline, false));
}

TEST(ChromecastSwitchesTest, SwitchValueFalse) {
if (base::CommandLine::CommandLine::InitializedForCurrentProcess())
base::CommandLine::Reset();
base::CommandLine::Init(0, nullptr);
base::CommandLine* cl = base::CommandLine::ForCurrentProcess();
cl->AppendSwitchASCII(switches::kEnableCmaMediaPipeline,
switches::kSwitchValueFalse);
EXPECT_FALSE(GetSwitchValueBoolean(switches::kEnableCmaMediaPipeline, true));
EXPECT_FALSE(GetSwitchValueBoolean(switches::kEnableCmaMediaPipeline, false));
}

TEST(ChromecastSwitchesTest, SwitchValueNonsense) {
if (base::CommandLine::CommandLine::InitializedForCurrentProcess())
base::CommandLine::Reset();
base::CommandLine::Init(0, nullptr);
base::CommandLine* cl = base::CommandLine::ForCurrentProcess();
cl->AppendSwitchASCII(switches::kEnableCmaMediaPipeline, "silverware");
EXPECT_TRUE(GetSwitchValueBoolean(switches::kEnableCmaMediaPipeline, true));
EXPECT_FALSE(GetSwitchValueBoolean(switches::kEnableCmaMediaPipeline, false));
}

} // namespace chromecast
1 change: 1 addition & 0 deletions chromecast/chromecast_tests.gypi
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
],
'sources': [
'base/bind_to_task_runner_unittest.cc',
'base/chromecast_switches_unittest.cc',
'base/component/component_unittest.cc',
'base/device_capabilities_impl_unittest.cc',
'base/error_codes_unittest.cc',
Expand Down
8 changes: 6 additions & 2 deletions chromecast/media/cma/backend/alsa/stream_mixer_alsa.cc
Original file line number Diff line number Diff line change
Expand Up @@ -256,9 +256,13 @@ void StreamMixerAlsa::DefineAlsaParameters() {
}
alsa_avail_min_ = avail_min;

// --accept-resource-provider should imply a check close timeout of 0.
int default_close_timeout = chromecast::GetSwitchValueBoolean(
switches::kAcceptResourceProvider, false)
? 0
: kDefaultCheckCloseTimeoutMs;
GetSwitchValueAsNonNegativeInt(switches::kAlsaCheckCloseTimeout,
kDefaultCheckCloseTimeoutMs,
&check_close_timeout_);
default_close_timeout, &check_close_timeout_);
}

int StreamMixerAlsa::SetAlsaPlaybackParams() {
Expand Down

0 comments on commit b92b0b2

Please sign in to comment.