[MediaController] Server native implementation
authorRafal Galka <r.galka@samsung.com>
Tue, 14 Apr 2015 07:26:00 +0000 (09:26 +0200)
committerRafal Galka <r.galka@samsung.com>
Tue, 28 Apr 2015 09:26:04 +0000 (18:26 +0900)
[Verification] Code compiles.

Change-Id: Ib76c47dfc1729b1741708046491c4023ccbaae0c

src/mediacontroller/mediacontroller.gyp
src/mediacontroller/mediacontroller_instance.cc
src/mediacontroller/mediacontroller_instance.h
src/mediacontroller/mediacontroller_server.cc [new file with mode: 0644]
src/mediacontroller/mediacontroller_server.h [new file with mode: 0644]

index 679d212072ae28077bc4e6cc27996b9d39756418..9e91e3254887c22bd8e16376ec564062e357725e 100755 (executable)
@@ -17,6 +17,8 @@
         'mediacontroller_extension.h',
         'mediacontroller_instance.cc',
         'mediacontroller_instance.h',
+        'mediacontroller_server.cc',
+        'mediacontroller_server.h',
         'mediacontroller_types.cc',
         'mediacontroller_types.h',
       ],
index ffca63edcbff0653d30bdf9671c245e7d6e7efa4..7ec9e1fcdab25c0b2e573cd0ee47893c7b4a8edd 100644 (file)
@@ -104,82 +104,134 @@ void MediaControllerInstance::MediaControllerManagerCreateServer(
     const picojson::value& args,
     picojson::object& out) {
 
-  // implement it
+  if (server_) {
+    ReportSuccess(out);
+    return;
+  }
 
-  // if success
-  // ReportSuccess(out);
-  // if error
-  // ReportError(out);
+  // TODO(r.galka) check privileges
+
+  server_ = std::make_shared<MediaControllerServer>();
+  const PlatformResult& result = server_->Init();
+  if (!result) {
+    server_.reset();
+    ReportError(result, &out);
+  }
+
+  ReportSuccess(out);
 }
 
 void MediaControllerInstance::MediaControllerServerUpdatePlaybackState(
     const picojson::value& args,
     picojson::object& out) {
+  CHECK_EXIST(args, "state", out)
 
-  // implement it
+  if (!server_) {
+    ReportError(PlatformResult(ErrorCode::INVALID_STATE_ERR,
+        "Server not initialized."), &out);
+    return;
+  }
 
-  // if success
-  // ReportSuccess(out);
-  // if error
-  // ReportError(out);
+  const std::string& state = args.get("state").get<std::string>();
+  const PlatformResult& result = server_->SetPlaybackState(state);
+  if (!result) {
+    ReportError(result, &out);
+    return;
+  }
+
+  ReportSuccess(out);
 }
 
 void MediaControllerInstance::MediaControllerServerUpdatePlaybackPosition(
     const picojson::value& args,
     picojson::object& out) {
+
+  if (!server_) {
+    ReportError(PlatformResult(ErrorCode::INVALID_STATE_ERR,
+        "Server not initialized."), &out);
+    return;
+  }
+
   CHECK_EXIST(args, "position", out)
 
   double position = args.get("position").get<double>();
+  const PlatformResult& result = server_->SetPlaybackPosition(position);
+  if (!result) {
+    ReportError(result, &out);
+    return;
+  }
 
-  // implement it
-
-  // if success
-  // ReportSuccess(out);
-  // if error
-  // ReportError(out);
+  ReportSuccess(out);
 }
 
 void MediaControllerInstance::MediaControllerServerUpdateShuffleMode(
     const picojson::value& args,
     picojson::object& out) {
+
+  if (!server_) {
+    ReportError(PlatformResult(ErrorCode::INVALID_STATE_ERR,
+        "Server not initialized."), &out);
+    return;
+  }
+
   CHECK_EXIST(args, "mode", out)
 
   bool mode = args.get("mode").get<bool>();
 
-  // implement it
+  const PlatformResult& result = server_->SetShuffleMode(mode);
+  if (!result) {
+    ReportError(result, &out);
+    return;
+  }
 
-  // if success
-  // ReportSuccess(out);
-  // if error
-  // ReportError(out);
+  ReportSuccess(out);
 }
 
 void MediaControllerInstance::MediaControllerServerUpdateRepeatMode(
     const picojson::value& args,
     picojson::object& out) {
 
+  if (!server_) {
+    ReportError(PlatformResult(ErrorCode::INVALID_STATE_ERR,
+        "Server not initialized."), &out);
+    return;
+  }
+
   CHECK_EXIST(args, "mode", out)
 
   bool mode = args.get("mode").get<bool>();
 
-  // implement it
+  const PlatformResult& result = server_->SetRepeatMode(mode);
+  if (!result) {
+    ReportError(result, &out);
+    return;
+  }
 
-  // if success
-  // ReportSuccess(out);
-  // if error
-  // ReportError(out);
+  ReportSuccess(out);
 }
 
 void MediaControllerInstance::MediaControllerServerUpdateMetadata(
     const picojson::value& args,
     picojson::object& out) {
 
-  // implement it
+  if (!server_) {
+    ReportError(PlatformResult(ErrorCode::INVALID_STATE_ERR,
+        "Server not initialized."), &out);
+    return;
+  }
 
-  // if success
-  // ReportSuccess(out);
-  // if error
-  // ReportError(out);
+  CHECK_EXIST(args, "metadata", out)
+
+  const picojson::object& metadata =
+      args.get("metadata").get<picojson::object>();
+
+  const PlatformResult& result = server_->SetMetadata(metadata);
+  if (!result) {
+    ReportError(result, &out);
+    return;
+  }
+
+  ReportSuccess(out);
 }
 
 void MediaControllerInstance::MediaControllerServerAddChangeRequestPlaybackInfoListener(
index 8751844ad45e4d697e0b15b23cedc2eb6fd2dbcd..f48db2f9ef6c64174c93da83d04703812c77ad74 100644 (file)
@@ -8,8 +8,8 @@
 #include <memory>
 
 #include "common/extension.h"
-
 #include "mediacontroller/mediacontroller_client.h"
+#include "mediacontroller/mediacontroller_server.h"
 
 namespace extension {
 namespace mediacontroller {
@@ -48,6 +48,7 @@ class MediaControllerInstance : public common::ParsedInstance {
   void MediaControllerServerInfoRemovePlaybackInfoChangeListener(const picojson::value& args, picojson::object& out);
 
   std::shared_ptr<MediaControllerClient> client_;
+  std::shared_ptr<MediaControllerServer> server_;
 };
 
 } // namespace mediacontroller
diff --git a/src/mediacontroller/mediacontroller_server.cc b/src/mediacontroller/mediacontroller_server.cc
new file mode 100644 (file)
index 0000000..434d8ab
--- /dev/null
@@ -0,0 +1,155 @@
+// Copyright 2015 Samsung Electronics Co, Ltd. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "mediacontroller/mediacontroller_server.h"
+
+#include "common/logger.h"
+
+#include "mediacontroller/mediacontroller_types.h"
+
+namespace extension {
+namespace mediacontroller {
+
+using common::PlatformResult;
+using common::ErrorCode;
+
+MediaControllerServer::MediaControllerServer() : handle_(nullptr) {
+}
+
+MediaControllerServer::~MediaControllerServer() {
+  LOGGER(DEBUG) << "entered";
+
+  if (handle_) {
+    int ret = mc_server_destroy(handle_);
+    if (ret != MEDIA_CONTROLLER_ERROR_NONE) {
+      LOGGER(ERROR) << "Unable to destroy media controller server";
+    }
+  }
+}
+
+common::PlatformResult MediaControllerServer::Init() {
+  PlatformResult result = PlatformResult(ErrorCode::NO_ERROR);
+
+  int ret = mc_server_create(&handle_);
+  if (ret != MEDIA_CONTROLLER_ERROR_NONE) {
+    LOGGER(ERROR) << "Unable to create media controller server, error: " << ret;
+    result = PlatformResult(ErrorCode::UNKNOWN_ERR,
+                            "Unable to create media controller server");
+  }
+
+  return result;
+}
+
+common::PlatformResult MediaControllerServer::SetPlaybackState(
+    const std::string& state) {
+
+  int state_int;
+  PlatformResult result = Types::StringToPlatformEnum(
+      Types::kMediaControllerPlaybackState, state, &state_int);
+
+  if (!result) {
+    return result;
+  }
+
+  int ret = mc_server_set_playback_state(
+      handle_, static_cast<mc_playback_states_e>(state_int));
+  if (ret != MEDIA_CONTROLLER_ERROR_NONE) {
+    LOGGER(ERROR) << "mc_server_set_playback_state failed, error: " << ret;
+    return PlatformResult(ErrorCode::UNKNOWN_ERR,
+                          "Error setting playback state");
+  }
+
+  ret = mc_server_update_playback_info(handle_);
+  if (ret != MEDIA_CONTROLLER_ERROR_NONE) {
+    LOGGER(ERROR) << "mc_server_update_playback_info failed, error: " << ret;
+    return PlatformResult(ErrorCode::UNKNOWN_ERR,
+                          "Error updating playback info");
+  }
+
+  return PlatformResult(ErrorCode::NO_ERROR);
+}
+
+common::PlatformResult MediaControllerServer::SetPlaybackPosition(
+    double position) {
+
+  int ret = mc_server_set_playback_position(
+      handle_, static_cast<unsigned long long>(position));
+  if (ret != MEDIA_CONTROLLER_ERROR_NONE) {
+    LOGGER(ERROR) << "mc_server_set_playback_position failed, error: " << ret;
+    return PlatformResult(ErrorCode::UNKNOWN_ERR,
+                          "Error setting playback position");
+  }
+
+  ret = mc_server_update_playback_info(handle_);
+  if (ret != MEDIA_CONTROLLER_ERROR_NONE) {
+    LOGGER(ERROR) << "mc_server_update_playback_info failed, error: " << ret;
+    return PlatformResult(ErrorCode::UNKNOWN_ERR,
+                          "Error updating playback info");
+  }
+
+  return PlatformResult(ErrorCode::NO_ERROR);
+}
+
+common::PlatformResult MediaControllerServer::SetShuffleMode(bool mode) {
+
+  int ret = mc_server_update_shuffle_mode(handle_,
+                                          mode ? SHUFFLE_MODE_ON
+                                               : SHUFFLE_MODE_OFF);
+  if (ret != MEDIA_CONTROLLER_ERROR_NONE) {
+    LOGGER(ERROR) << "mc_server_update_shuffle_mode failed, error: " << ret;
+    return PlatformResult(ErrorCode::UNKNOWN_ERR,
+                          "Error updating shuffle mode");
+  }
+
+  return PlatformResult(ErrorCode::NO_ERROR);
+}
+
+common::PlatformResult MediaControllerServer::SetRepeatMode(bool mode) {
+
+  int ret = mc_server_update_repeat_mode(handle_,
+                                         mode ? REPEAT_MODE_ON
+                                              : REPEAT_MODE_OFF);
+  if (ret != MEDIA_CONTROLLER_ERROR_NONE) {
+    LOGGER(ERROR) << "mc_server_update_repeat_mode failed, error: " << ret;
+    return PlatformResult(ErrorCode::UNKNOWN_ERR, "Error updating repeat mode");
+  }
+
+  return PlatformResult(ErrorCode::NO_ERROR);
+}
+
+common::PlatformResult MediaControllerServer::SetMetadata(
+    const picojson::object& metadata) {
+
+  int attribute_int, ret;
+  PlatformResult result(ErrorCode::NO_ERROR);
+  for (picojson::object::const_iterator i = metadata.begin();
+       i != metadata.end();
+       ++i) {
+
+    result = Types::StringToPlatformEnum(
+        Types::kMediaControllerMetadataAttribute, i->first, &attribute_int);
+    if (!result) {
+      return result;
+    }
+
+    ret = mc_server_set_metadata(handle_, static_cast<mc_meta_e>(attribute_int),
+                                 i->second.to_str().c_str());
+    if (ret != MEDIA_CONTROLLER_ERROR_NONE) {
+      LOGGER(ERROR) << "set_metadata failed for '" << i->first
+                    << "', error: " << ret;
+      return PlatformResult(ErrorCode::UNKNOWN_ERR, "Error setting metadata");
+    }
+  }
+
+  ret = mc_server_update_metadata(handle_);
+  if (ret != MEDIA_CONTROLLER_ERROR_NONE) {
+    LOGGER(ERROR) << "mc_server_update_metadata failed, error: " << ret;
+    return PlatformResult(ErrorCode::UNKNOWN_ERR, "Error updating metadata");
+  }
+
+  return result;
+}
+
+} // namespace mediacontroller
+} // namespace extension
diff --git a/src/mediacontroller/mediacontroller_server.h b/src/mediacontroller/mediacontroller_server.h
new file mode 100644 (file)
index 0000000..8ac75bc
--- /dev/null
@@ -0,0 +1,35 @@
+// Copyright 2015 Samsung Electronics Co, Ltd. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef MEDIACONTROLLER_MEDIACONTROLLER_SERVER_H_
+#define MEDIACONTROLLER_MEDIACONTROLLER_SERVER_H_
+
+#include <media_controller_server.h>
+#include <string>
+
+#include "common/platform_result.h"
+
+namespace extension {
+namespace mediacontroller {
+
+class MediaControllerServer {
+ public:
+  MediaControllerServer();
+  virtual ~MediaControllerServer();
+
+  common::PlatformResult Init();
+  common::PlatformResult SetPlaybackState(const std::string& state);
+  common::PlatformResult SetPlaybackPosition(double position);
+  common::PlatformResult SetShuffleMode(bool mode);
+  common::PlatformResult SetRepeatMode(bool mode);
+  common::PlatformResult SetMetadata(const picojson::object& metadata);
+
+ private:
+  mc_server_h handle_;
+};
+
+} // namespace mediacontroller
+} // namespace extension
+
+#endif  // MEDIACONTROLLER_MEDIACONTROLLER_SERVER_H_