Improve evegreen configuration

Change-Id: I73594ab879915175e732d90c8d3ba53f2e74e9f2
Signed-off-by: Eugene Mutavchi <Ievgen_Mutavchi@comcast.com>
diff --git a/src/third_party/starboard/rdk/arm/gyp_configuration.gypi b/src/third_party/starboard/rdk/arm/gyp_configuration.gypi
index 789d0be..93878ac 100644
--- a/src/third_party/starboard/rdk/arm/gyp_configuration.gypi
+++ b/src/third_party/starboard/rdk/arm/gyp_configuration.gypi
@@ -40,7 +40,6 @@
     'sysroot%': '/',
     'gl_type': 'system_gles2',
     'has_ocdm%': '0',
-    'enable_evergreen_lite%': '0',
 
     # This is to create cobalt shared library
     'final_executable_type': 'shared_library',
@@ -146,13 +145,6 @@
       '-fno-exceptions',
     ],
     'conditions': [
-      ['<(enable_evergreen_lite)==1', {
-       'variables': {
-            'sb_evergreen_compatible': 1,
-            'sb_evergreen_compatible_lite': 1,
-            'sb_evergreen_compatible_libunwind': 1,
-       },
-      }],
       ['cobalt_fastbuild==0', {
         'compiler_flags_debug': [
           # '-g',
diff --git a/src/third_party/starboard/rdk/arm/gyp_configuration.py b/src/third_party/starboard/rdk/arm/gyp_configuration.py
index f40f33a..c189893 100644
--- a/src/third_party/starboard/rdk/arm/gyp_configuration.py
+++ b/src/third_party/starboard/rdk/arm/gyp_configuration.py
@@ -66,7 +66,9 @@
         'clang': 0,
         'sysroot': self.sysroot,
         'has_ocdm': self.has_ocdm,
-        'enable_evergreen_lite': self.enable_evergreen_lite,
+        'sb_evergreen_compatible': self.enable_evergreen_lite,
+        'sb_evergreen_compatible_lite': self.enable_evergreen_lite,
+        'sb_evergreen_compatible_libunwind': self.enable_evergreen_lite,
     })
     variables.update({
         'cobalt_font_package': 'limited',
diff --git a/src/third_party/starboard/rdk/arm/starboard_platform.gyp b/src/third_party/starboard/rdk/arm/starboard_platform.gyp
index 42d1755..1e0a250 100644
--- a/src/third_party/starboard/rdk/arm/starboard_platform.gyp
+++ b/src/third_party/starboard/rdk/arm/starboard_platform.gyp
@@ -77,7 +77,7 @@
              '<(DEPTH)/third_party/llvm-project/libunwind/libunwind.gyp:unwind_starboard',
            ],},
          ],
-       ],
+      ],
       'cflags': [
         '-Wno-reorder',
         '-Wno-sign-compare',
diff --git a/src/third_party/starboard/rdk/brcm/arm/gyp_configuration.gypi b/src/third_party/starboard/rdk/brcm/arm/gyp_configuration.gypi
index 006e6ee..6ef0a51 100644
--- a/src/third_party/starboard/rdk/brcm/arm/gyp_configuration.gypi
+++ b/src/third_party/starboard/rdk/brcm/arm/gyp_configuration.gypi
@@ -40,7 +40,6 @@
     'sysroot%': '/',
     'gl_type': 'system_gles2',
     'has_ocdm%': '0',
-    'enable_evergreen_lite%': '0',
 
     # This is to create cobalt shared library
     'final_executable_type': 'shared_library',
diff --git a/src/third_party/starboard/rdk/brcm/arm/gyp_configuration.py b/src/third_party/starboard/rdk/brcm/arm/gyp_configuration.py
index 8282124..0d99905 100644
--- a/src/third_party/starboard/rdk/brcm/arm/gyp_configuration.py
+++ b/src/third_party/starboard/rdk/brcm/arm/gyp_configuration.py
@@ -48,7 +48,6 @@
 
     self.has_ocdm = os.environ.get('COBALT_HAS_OCDM', '0')
     self.sabi_json_path = 'starboard/sabi/arm/%s/sabi-v13.json' % (os.environ.get('COBALT_ARM_CALLCONVENTION', 'hardfp'))
-    self.enable_evergreen_lite = os.environ.get('COBALT_EVERGREEN_LITE', '0')
     self.sysroot = os.path.realpath(os.environ.get('PKG_CONFIG_SYSROOT_DIR', '/'))
     self.AppendApplicationConfigurationPath(os.path.dirname(__file__))
     self.AppendApplicationConfigurationPath(os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..", "shared")))
@@ -66,7 +65,6 @@
         'clang': 0,
         'sysroot': self.sysroot,
         'has_ocdm': self.has_ocdm,
-        'enable_evergeen_lite': self.enable_evergreen_lite,
     })
     variables.update({
         'cobalt_font_package': 'limited',
diff --git a/src/third_party/starboard/rdk/shared/main_rdk.cc b/src/third_party/starboard/rdk/shared/main_rdk.cc
index f491e80..ae68188 100644
--- a/src/third_party/starboard/rdk/shared/main_rdk.cc
+++ b/src/third_party/starboard/rdk/shared/main_rdk.cc
@@ -39,6 +39,10 @@
 
 #include "third_party/starboard/rdk/shared/application_rdk.h"
 
+#if SB_IS(EVERGREEN_COMPATIBLE)
+#include "third_party/crashpad/wrapper/wrapper.h"
+#endif
+
 namespace third_party {
 namespace starboard {
 namespace rdk {
@@ -80,6 +84,10 @@
   starboard::shared::signal::InstallSuspendSignalHandlers();
   third_party::starboard::rdk::shared::InstallStopSignalHandlers();
 
+#if SB_IS(EVERGREEN_COMPATIBLE)
+  third_party::crashpad::wrapper::InstallCrashpadHandler();
+#endif
+
   int result = 0;
   {
     third_party::starboard::rdk::shared::Application application;
diff --git a/src/third_party/starboard/rdk/shared/player/player_internal.cc b/src/third_party/starboard/rdk/shared/player/player_internal.cc
index 5f3bdca..37f43db 100644
--- a/src/third_party/starboard/rdk/shared/player/player_internal.cc
+++ b/src/third_party/starboard/rdk/shared/player/player_internal.cc
@@ -111,6 +111,22 @@
   return flag->value;
 }
 
+bool enableNativeAudio() {
+  static bool enable_native_audio = false;
+  static volatile gsize init = 0;
+
+  if (g_once_init_enter (&init)) {
+    GstElementFactory* factory = gst_element_factory_find("brcmaudiosink");
+    if (factory) {
+      gst_object_unref(GST_OBJECT(factory));
+      enable_native_audio = true;
+    }
+    g_once_init_leave (&init, 1);
+  }
+
+  return enable_native_audio;
+}
+
 G_BEGIN_DECLS
 
 #define GST_COBALT_TYPE_SRC (gst_cobalt_src_get_type())
@@ -1370,10 +1386,8 @@
   unsigned flagAudio = getGstPlayFlag("audio");
   unsigned flagVideo = getGstPlayFlag("video");
   unsigned flagNativeVideo = getGstPlayFlag("native-video");
-  unsigned flagNativeAudio = 0;
-#if SB_HAS(NATIVE_AUDIO)
-  flagNativeAudio = getGstPlayFlag("native-audio");
-#endif
+  unsigned flagNativeAudio = enableNativeAudio() ? getGstPlayFlag("native-audio") : 0;
+
   g_object_set(pipeline_, "flags",
                flagAudio | flagVideo | flagNativeVideo | flagNativeAudio,
                nullptr);
diff --git a/src/third_party/starboard/rdk/shared/sources.gypi b/src/third_party/starboard/rdk/shared/sources.gypi
index e82cd0a..a3d9201 100644
--- a/src/third_party/starboard/rdk/shared/sources.gypi
+++ b/src/third_party/starboard/rdk/shared/sources.gypi
@@ -459,6 +459,8 @@
         '<(DEPTH)/starboard/shared/starboard/application.cc',
         '<(DEPTH)/starboard/shared/starboard/command_line.cc',
         '<(DEPTH)/starboard/shared/starboard/command_line.h',
+        '<(DEPTH)/starboard/shared/starboard/crash_handler.cc',
+        '<(DEPTH)/starboard/shared/starboard/crash_handler.h',
         '<(DEPTH)/starboard/shared/stub/cryptography_create_transformer.cc',
         '<(DEPTH)/starboard/shared/stub/cryptography_destroy_transformer.cc',
         '<(DEPTH)/starboard/shared/stub/cryptography_get_tag.cc',
diff --git a/src/third_party/starboard/rdk/shared/system/system_get_extensions.cc b/src/third_party/starboard/rdk/shared/system/system_get_extensions.cc
index d7666a8..9eaa625 100644
--- a/src/third_party/starboard/rdk/shared/system/system_get_extensions.cc
+++ b/src/third_party/starboard/rdk/shared/system/system_get_extensions.cc
@@ -33,10 +33,29 @@
 #include <cstring>
 
 #include "cobalt/extension/configuration.h"
+#include "cobalt/extension/crash_handler.h"
 #include "starboard/common/string.h"
 #include "third_party/starboard/rdk/shared/configuration.h"
+#if SB_IS(EVERGREEN_COMPATIBLE)
+#include "starboard/elf_loader/evergreen_config.h"
+#include "starboard/shared/starboard/crash_handler.h"
+#endif
 
 const void* SbSystemGetExtension(const char* name) {
+#if SB_IS(EVERGREEN_COMPATIBLE)
+  const starboard::elf_loader::EvergreenConfig* evergreen_config =
+      starboard::elf_loader::EvergreenConfig::GetInstance();
+  if (evergreen_config != NULL &&
+      evergreen_config->custom_get_extension_ != NULL) {
+    const void* ext = evergreen_config->custom_get_extension_(name);
+    if (ext != NULL) {
+      return ext;
+    }
+  }
+  if (strcmp(name, kCobaltExtensionCrashHandlerName) == 0) {
+    return starboard::common::GetCrashHandlerApi();
+  }
+#endif
   if (strcmp(name, kCobaltExtensionConfigurationName) == 0) {
     return third_party::starboard::rdk::shared::GetConfigurationApi();
   }
diff --git a/src/third_party/starboard/rdk/shared/system/system_get_path.cc b/src/third_party/starboard/rdk/shared/system/system_get_path.cc
index c6c84a3..01b6e81 100644
--- a/src/third_party/starboard/rdk/shared/system/system_get_path.cc
+++ b/src/third_party/starboard/rdk/shared/system/system_get_path.cc
@@ -29,13 +29,40 @@
 #include "starboard/directory.h"
 #include "starboard/file.h"
 #include "starboard/user.h"
+#if SB_IS(EVERGREEN_COMPATIBLE)
+#include "starboard/elf_loader/evergreen_config.h"
+#endif
 
 namespace {
 
+#if SB_IS(EVERGREEN_COMPATIBLE)
+// May override the content path if there is EvergreenConfig published.
+// The override allows for switching to different content paths based
+// on the Evergreen binary executed.
+// Returns false if it failed.
+bool GetEvergreenContentPathOverride(char* out_path, int path_size) {
+  const starboard::elf_loader::EvergreenConfig* evergreen_config =
+      starboard::elf_loader::EvergreenConfig::GetInstance();
+  if (!evergreen_config) {
+    return true;
+  }
+  if (evergreen_config->content_path_.empty()) {
+    return true;
+  }
+
+  if (starboard::strlcpy(out_path, evergreen_config->content_path_.c_str(),
+                         path_size) >= path_size) {
+    return false;
+  }
+  return true;
+}
+#endif
+
 bool GetContentDirectory(char* out_path, int path_size)
 {
   const char* paths = std::getenv("COBALT_CONTENT_DIR");
-  if(paths){ // Treat the environment variable as PATH-like search variable
+  if (paths) {
+    // Treat the environment variable as PATH-like search variable
     std::stringstream pathsStream(paths);
     const std::string testFilePath = "/fonts/fonts.xml";
     std::string contentPath;
@@ -46,10 +73,14 @@
         return (starboard::strlcat<char>(out_path, contentPath.c_str(), path_size) < path_size);
       }
     }
+#if !SB_IS(EVERGREEN_COMPATIBLE)
+    // Don't return false and let EvergreenConfig override the path
     return false;
-  } else { // Default to /usr/share/content/data if COBALT_CONTENT_PATH is not set
-    return (starboard::strlcat<char>(out_path, "/usr/share/content/data", path_size) < path_size);
+#endif
   }
+
+  // Default to /usr/share/content/data if COBALT_CONTENT_PATH is not set
+  return (starboard::strlcat<char>(out_path, "/usr/share/content/data", path_size) < path_size);
 }
 
 // Gets the path to the cache directory, using the user's home directory.
@@ -162,6 +193,11 @@
       if (!GetContentDirectory(path, kSbFileMaxPath)){
         return false;
       }
+#if SB_IS(EVERGREEN_COMPATIBLE)
+      if (!GetEvergreenContentPathOverride(path, kSbFileMaxPath)) {
+        return false;
+      }
+#endif
       break;
 
     case kSbSystemPathCacheDirectory:
@@ -202,7 +238,17 @@
 
     case kSbSystemPathFontConfigurationDirectory:
     case kSbSystemPathFontDirectory:
+#if SB_IS(EVERGREEN_COMPATIBLE)
+      if (!GetContentDirectory(path, kSbFileMaxPath)) {
+        return false;
+      }
+      if (starboard::strlcat(path, "/fonts", kSbFileMaxPath) >= kSbFileMaxPath) {
+        return false;
+      }
+      break;
+#else
       return false;
+#endif
 
     default:
       SB_NOTIMPLEMENTED() << "SbSystemGetPath not implemented for " << path_id;