diff options
author | Jacob Keeler <jacob.keeler@livioradio.com> | 2016-09-20 11:44:51 -0400 |
---|---|---|
committer | Jacob Keeler <jacob.keeler@livioradio.com> | 2016-09-20 14:37:25 -0400 |
commit | 039581419749dbdd32590804e8b14a77fcaf9636 (patch) | |
tree | ced927fc4bef4686f7373a5f45dd692bd9fc9de0 /src/components/media_manager | |
parent | 1b96b7c02d2e338b846a7314c6fa0bdaa31f7c5e (diff) | |
parent | aff7f09a4ec07becfb64dc78992580061d3342ff (diff) | |
download | sdl_core-039581419749dbdd32590804e8b14a77fcaf9636.tar.gz |
Merge branch 'master'
Diffstat (limited to 'src/components/media_manager')
48 files changed, 1796 insertions, 1347 deletions
diff --git a/src/components/media_manager/CMakeLists.txt b/src/components/media_manager/CMakeLists.txt index 9a8618fa60..69af6e7841 100644 --- a/src/components/media_manager/CMakeLists.txt +++ b/src/components/media_manager/CMakeLists.txt @@ -45,11 +45,14 @@ set(default_sources ${COMPONENTS_DIR}/media_manager/src/audio/from_mic_to_file_recorder_thread.cc ${COMPONENTS_DIR}/media_manager/src/audio/socket_audio_streamer_adapter.cc ${COMPONENTS_DIR}/media_manager/src/audio/pipe_audio_streamer_adapter.cc + ${COMPONENTS_DIR}/media_manager/src/audio/file_audio_streamer_adapter.cc ${COMPONENTS_DIR}/media_manager/src/video/socket_video_streamer_adapter.cc ${COMPONENTS_DIR}/media_manager/src/video/pipe_video_streamer_adapter.cc - ${COMPONENTS_DIR}/media_manager/src/video/video_stream_to_file_adapter.cc + ${COMPONENTS_DIR}/media_manager/src/video/file_video_streamer_adapter.cc + ${COMPONENTS_DIR}/media_manager/src/streamer_adapter.cc ${COMPONENTS_DIR}/media_manager/src/pipe_streamer_adapter.cc ${COMPONENTS_DIR}/media_manager/src/socket_streamer_adapter.cc + ${COMPONENTS_DIR}/media_manager/src/file_streamer_adapter.cc ) set(LIBRARIES ${GSTREAMER_gstreamer_LIBRARY} @@ -60,17 +63,18 @@ set(LIBRARIES glib-2.0 ) else(EXTENDED_MEDIA_MODE) -set(default_includes -) + set(default_sources - ${COMPONENTS_DIR}/media_manager/src/video/video_stream_to_file_adapter.cc ${COMPONENTS_DIR}/media_manager/src/audio/socket_audio_streamer_adapter.cc ${COMPONENTS_DIR}/media_manager/src/audio/pipe_audio_streamer_adapter.cc + ${COMPONENTS_DIR}/media_manager/src/audio/file_audio_streamer_adapter.cc ${COMPONENTS_DIR}/media_manager/src/video/socket_video_streamer_adapter.cc ${COMPONENTS_DIR}/media_manager/src/video/pipe_video_streamer_adapter.cc - ${COMPONENTS_DIR}/media_manager/src/video/video_stream_to_file_adapter.cc + ${COMPONENTS_DIR}/media_manager/src/video/file_video_streamer_adapter.cc + ${COMPONENTS_DIR}/media_manager/src/streamer_adapter.cc ${COMPONENTS_DIR}/media_manager/src/pipe_streamer_adapter.cc ${COMPONENTS_DIR}/media_manager/src/socket_streamer_adapter.cc + ${COMPONENTS_DIR}/media_manager/src/file_streamer_adapter.cc ) set(LIBRARIES ProtocolLibrary @@ -91,9 +95,7 @@ include_directories ( ${COMPONENTS_DIR}/config_profile/include/ ${JSONCPP_INCLUDE_DIRECTORY} ${CMAKE_BINARY_DIR}/src/components/ - ${CMAKE_SOURCE_DIR}/src/components/policy/src/policy/include/ - ${CMAKE_SOURCE_DIR}/src/components/policy/src/policy/usage_statistics/include/ - ${default_includes} + ${COMPONENTS_DIR}/policy/include/ ${LOG4CXX_INCLUDE_DIRECTORY} ) @@ -109,5 +111,6 @@ add_library("MediaManager" ${SOURCES} ${default_sources}) target_link_libraries("MediaManager" ${LIBRARIES}) if(BUILD_TESTS) - add_subdirectory(test) + add_subdirectory(test) endif() + diff --git a/src/components/media_manager/include/media_manager/audio/a2dp_source_player_adapter.h b/src/components/media_manager/include/media_manager/audio/a2dp_source_player_adapter.h index 257564e81a..3f262b27bb 100644 --- a/src/components/media_manager/include/media_manager/audio/a2dp_source_player_adapter.h +++ b/src/components/media_manager/include/media_manager/audio/a2dp_source_player_adapter.h @@ -38,6 +38,10 @@ #include "media_manager/media_adapter_impl.h" #include "utils/threads/thread_delegate.h" +namespace protocol_handler { +class SessionObserver; +} + namespace threads { class Thread; } @@ -45,22 +49,28 @@ class Thread; namespace media_manager { class A2DPSourcePlayerAdapter : public MediaAdapterImpl { - public: - A2DPSourcePlayerAdapter(); - ~A2DPSourcePlayerAdapter(); - void SendData(int32_t application_key, - const ::protocol_handler::RawMessagePtr message) {} - void StartActivity(int32_t application_key); - void StopActivity(int32_t application_key); - bool is_app_performing_activity(int32_t application_key); + public: + A2DPSourcePlayerAdapter(protocol_handler::SessionObserver& session_observer); + ~A2DPSourcePlayerAdapter(); + void SendData(int32_t application_key, + const ::protocol_handler::RawMessagePtr message) {} + void StartActivity(int32_t application_key); + void StopActivity(int32_t application_key); + bool is_app_performing_activity(int32_t application_key) const; + + void set_connection_handler( + connection_handler::ConnectionHandlerImpl* connection_handler) { + connection_handler_ = connection_handler; + } - private: - class A2DPSourcePlayerThread; + private: + class A2DPSourcePlayerThread; - typedef std::pair<threads::Thread*, A2DPSourcePlayerThread*> Pair; - typedef std::map<int32_t, Pair> SourcesMap; - SourcesMap sources_; - DISALLOW_COPY_AND_ASSIGN(A2DPSourcePlayerAdapter); + typedef std::pair<threads::Thread*, A2DPSourcePlayerThread*> Pair; + typedef std::map<int32_t, Pair> SourcesMap; + SourcesMap sources_; + protocol_handler::SessionObserver& session_observer_; + DISALLOW_COPY_AND_ASSIGN(A2DPSourcePlayerAdapter); }; } // namespace media_manager diff --git a/src/components/media_manager/include/media_manager/audio/audio_stream_sender_thread.h b/src/components/media_manager/include/media_manager/audio/audio_stream_sender_thread.h index cb1c144706..b956f2c9e2 100644 --- a/src/components/media_manager/include/media_manager/audio/audio_stream_sender_thread.h +++ b/src/components/media_manager/include/media_manager/audio/audio_stream_sender_thread.h @@ -39,13 +39,16 @@ #include "utils/conditional_variable.h" #include "utils/lock.h" - namespace NsSmartDeviceLink { namespace NsSmartObjects { class SmartObject; } } +namespace application_manager { +class ApplicationManager; +} + namespace media_manager { typedef enum { @@ -62,68 +65,67 @@ typedef enum { ACQ_16_BIT = 1 } AudioCaptureQuality; -typedef enum { - AT_INVALID = -1, - AT_PCM = 0 -} AudioType; +typedef enum { AT_INVALID = -1, AT_PCM = 0 } AudioType; /* - * @brief AudioStreamSenderThread class used to read binary data written from microphone + * @brief AudioStreamSenderThread class used to read binary data written from + * microphone * and send it every 1 second to mobile device. */ class AudioStreamSenderThread : public threads::ThreadDelegate { - public: - /* - * @brief AudioStreamSenderThread class constructor - * - * @param session_key Session key of connection for Mobile side - * @param correlation_id Correlation id for response for Mobile side - */ - AudioStreamSenderThread(const std::string fileName, - uint32_t session_key); - - /* - * @brief AudioStreamSenderThread class destructor - */ - ~AudioStreamSenderThread(); - - /** - * @brief Thread procedure. - */ - void threadMain(); - - /* - * @brief Retrieve session key - * - * @return Stored session key - */ - uint32_t session_key() const; - - void exitThreadMain(); - - private: - /* - * @brief Sends AudioPassThru request - */ - bool SendEndAudioPassThru(); - - void sendAudioChunkToMobile(); - - - bool getShouldBeStopped(); - void setShouldBeStopped(bool should_stop); - - uint32_t session_key_; - const std::string fileName_; - int32_t offset_; - volatile bool shouldBeStoped_; - sync_primitives::Lock shouldBeStoped_lock_; - sync_primitives::ConditionalVariable shouldBeStoped_cv_; - - static const int32_t kAudioPassThruTimeout; - - - DISALLOW_COPY_AND_ASSIGN(AudioStreamSenderThread); + public: + /* + * @brief AudioStreamSenderThread class constructor + * + * @param session_key Session key of connection for Mobile side + * @param correlation_id Correlation id for response for Mobile side + */ + AudioStreamSenderThread(const std::string& fileName, + uint32_t session_key, + application_manager::ApplicationManager& app_mngr); + + /* + * @brief AudioStreamSenderThread class destructor + */ + ~AudioStreamSenderThread(); + + /** + * @brief Thread procedure. + */ + void threadMain(); + + /* + * @brief Retrieve session key + * + * @return Stored session key + */ + uint32_t session_key() const; + + void exitThreadMain(); + + private: + /* + * @brief Sends AudioPassThru request + */ + bool SendEndAudioPassThru(); + + void sendAudioChunkToMobile(); + + bool getShouldBeStopped(); + void setShouldBeStopped(bool should_stop); + + uint32_t session_key_; + const std::string fileName_; + int32_t offset_; + volatile bool shouldBeStoped_; + sync_primitives::Lock shouldBeStoped_lock_; + sync_primitives::ConditionalVariable shouldBeStoped_cv_; + + static const int32_t kAudioPassThruTimeout; + + application_manager::ApplicationManager& application_manager_; + + DISALLOW_COPY_AND_ASSIGN(AudioStreamSenderThread); }; } // namespace media_manager diff --git a/src/components/media_manager/include/media_manager/audio/file_audio_streamer_adapter.h b/src/components/media_manager/include/media_manager/audio/file_audio_streamer_adapter.h new file mode 100644 index 0000000000..dc0c001acf --- /dev/null +++ b/src/components/media_manager/include/media_manager/audio/file_audio_streamer_adapter.h @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2014-2015, Ford Motor Company + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following + * disclaimer in the documentation and/or other materials provided with the + * distribution. + * + * Neither the name of the Ford Motor Company nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef SRC_COMPONENTS_MEDIA_MANAGER_INCLUDE_MEDIA_MANAGER_AUDIO_FILE_AUDIO_STREAMER_ADAPTER_H_ +#define SRC_COMPONENTS_MEDIA_MANAGER_INCLUDE_MEDIA_MANAGER_AUDIO_FILE_AUDIO_STREAMER_ADAPTER_H_ + +#include "media_manager/file_streamer_adapter.h" + +namespace media_manager { + +class FileAudioStreamerAdapter : public FileStreamerAdapter { + public: + FileAudioStreamerAdapter(const std::string& audio_stream_file, + const std::string& app_storage_folder); + ~FileAudioStreamerAdapter(); +}; + +} // namespace media_manager + +#endif // SRC_COMPONENTS_MEDIA_MANAGER_INCLUDE_MEDIA_MANAGER_AUDIO_FILE_AUDIO_STREAMER_ADAPTER_H_ diff --git a/src/components/media_manager/include/media_manager/audio/from_mic_recorder_adapter.h b/src/components/media_manager/include/media_manager/audio/from_mic_recorder_adapter.h index 3ea4581435..7daf9c7add 100644 --- a/src/components/media_manager/include/media_manager/audio/from_mic_recorder_adapter.h +++ b/src/components/media_manager/include/media_manager/audio/from_mic_recorder_adapter.h @@ -43,22 +43,23 @@ class Thread; namespace media_manager { class FromMicRecorderAdapter : public MediaAdapterImpl { - public: - FromMicRecorderAdapter(); - ~FromMicRecorderAdapter(); - void SendData(int32_t application_key, - const ::protocol_handler::RawMessagePtr message) {} - void StartActivity(int32_t application_key); - void StopActivity(int32_t application_key); - bool is_app_performing_activity(int32_t application_key); - void set_output_file(const std::string& output_file); - void set_duration(int32_t duration); - private: - threads::Thread* recorder_thread_; - std::string output_file_; - const int32_t kDefaultDuration; - int32_t duration_; - DISALLOW_COPY_AND_ASSIGN(FromMicRecorderAdapter); + public: + FromMicRecorderAdapter(); + ~FromMicRecorderAdapter(); + void SendData(int32_t application_key, + const ::protocol_handler::RawMessagePtr message) {} + void StartActivity(int32_t application_key); + void StopActivity(int32_t application_key); + bool is_app_performing_activity(int32_t application_key) const; + void set_output_file(const std::string& output_file); + void set_duration(int32_t duration); + + private: + threads::Thread* recorder_thread_; + std::string output_file_; + const int32_t kDefaultDuration; + int32_t duration_; + DISALLOW_COPY_AND_ASSIGN(FromMicRecorderAdapter); }; } // namespace media_manager diff --git a/src/components/media_manager/include/media_manager/audio/from_mic_recorder_listener.h b/src/components/media_manager/include/media_manager/audio/from_mic_recorder_listener.h index 7df2af7f4a..c615195f94 100644 --- a/src/components/media_manager/include/media_manager/audio/from_mic_recorder_listener.h +++ b/src/components/media_manager/include/media_manager/audio/from_mic_recorder_listener.h @@ -36,30 +36,33 @@ #include <string> #include "media_manager/media_adapter_listener.h" +namespace application_manager { +class ApplicationManager; +} + namespace threads { class Thread; } namespace media_manager { class FromMicRecorderListener : public MediaAdapterListener { - public: - explicit FromMicRecorderListener( - const std::string& file_name); - ~FromMicRecorderListener(); - virtual void OnDataReceived( - int32_t application_key, - const DataForListener& data); - virtual void OnErrorReceived( - int32_t application_key, - const DataForListener& data); - virtual void OnActivityStarted(int32_t application_key); - virtual void OnActivityEnded(int32_t application_key); + public: + FromMicRecorderListener(const std::string& file_name, + application_manager::ApplicationManager&); + ~FromMicRecorderListener(); + virtual void OnDataReceived(int32_t application_key, + const DataForListener& data); + virtual void OnErrorReceived(int32_t application_key, + const DataForListener& data); + virtual void OnActivityStarted(int32_t application_key); + virtual void OnActivityEnded(int32_t application_key); - private: - threads::Thread* reader_; - std::string file_name_; - int32_t current_application_; - DISALLOW_COPY_AND_ASSIGN(FromMicRecorderListener); + private: + threads::Thread* reader_; + std::string file_name_; + int32_t current_application_; + application_manager::ApplicationManager& application_manager_; + DISALLOW_COPY_AND_ASSIGN(FromMicRecorderListener); }; } // namespace media_manager diff --git a/src/components/media_manager/include/media_manager/audio/from_mic_to_file_recorder_thread.h b/src/components/media_manager/include/media_manager/audio/from_mic_to_file_recorder_thread.h index 7bb036fa42..f0c6d3c48f 100644 --- a/src/components/media_manager/include/media_manager/audio/from_mic_to_file_recorder_thread.h +++ b/src/components/media_manager/include/media_manager/audio/from_mic_to_file_recorder_thread.h @@ -44,53 +44,52 @@ namespace media_manager { class FromMicToFileRecorderThread : public threads::ThreadDelegate { - public: - FromMicToFileRecorderThread(const std::string& output_file, - int32_t duration); - ~FromMicToFileRecorderThread(); - void threadMain(); + public: + FromMicToFileRecorderThread(const std::string& output_file, int32_t duration); + ~FromMicToFileRecorderThread(); + void threadMain(); - void exitThreadMain(); + void exitThreadMain(); - void set_output_file(const std::string& output_file); - void set_record_duration(int32_t duration); + void set_output_file(const std::string& output_file); + void set_record_duration(int32_t duration); - private: - int32_t argc_; - gchar** argv_; + private: + int32_t argc_; + gchar** argv_; - const std::string oKey_; - const std::string tKey_; + const std::string oKey_; + const std::string tKey_; - static GMainLoop* loop; - threads::Thread* sleepThread_; - bool shouldBeStoped_; - sync_primitives::Lock stopFlagLock_; + static GMainLoop* loop; + threads::Thread* sleepThread_; + bool shouldBeStoped_; + sync_primitives::Lock stopFlagLock_; - std::string outputFileName_, durationString_; + std::string outputFileName_, durationString_; - typedef struct { - GstElement* pipeline; - gint duration; - } GstTimeout; + typedef struct { + GstElement* pipeline; + gint duration; + } GstTimeout; - void initArgs(); + void initArgs(); - void psleep(void* timeout); + void psleep(void* timeout); - class SleepThreadDelegate : public threads::ThreadDelegate { - public: - explicit SleepThreadDelegate(GstTimeout timeout); + class SleepThreadDelegate : public threads::ThreadDelegate { + public: + explicit SleepThreadDelegate(GstTimeout timeout); - void threadMain(); + void threadMain(); - private: - GstTimeout timeout_; + private: + GstTimeout timeout_; - DISALLOW_COPY_AND_ASSIGN(SleepThreadDelegate); - }; + DISALLOW_COPY_AND_ASSIGN(SleepThreadDelegate); + }; - DISALLOW_COPY_AND_ASSIGN(FromMicToFileRecorderThread); + DISALLOW_COPY_AND_ASSIGN(FromMicToFileRecorderThread); }; } // namespace media_manager diff --git a/src/components/media_manager/include/media_manager/audio/pipe_audio_streamer_adapter.h b/src/components/media_manager/include/media_manager/audio/pipe_audio_streamer_adapter.h index 437f34fa03..d0dbbebd38 100644 --- a/src/components/media_manager/include/media_manager/audio/pipe_audio_streamer_adapter.h +++ b/src/components/media_manager/include/media_manager/audio/pipe_audio_streamer_adapter.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2014, Ford Motor Company + * Copyright (c) 2014-2015, Ford Motor Company * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -33,17 +33,15 @@ #ifndef SRC_COMPONENTS_MEDIA_MANAGER_INCLUDE_MEDIA_MANAGER_AUDIO_PIPE_AUDIO_STREAMER_ADAPTER_H_ #define SRC_COMPONENTS_MEDIA_MANAGER_INCLUDE_MEDIA_MANAGER_AUDIO_PIPE_AUDIO_STREAMER_ADAPTER_H_ -#include <string> #include "media_manager/pipe_streamer_adapter.h" namespace media_manager { -class PipeAudioStreamerAdapter : public PipeStreamerAdapter { - public: - PipeAudioStreamerAdapter(); - ~PipeAudioStreamerAdapter(); - private: - DISALLOW_COPY_AND_ASSIGN(PipeAudioStreamerAdapter); +class PipeAudioStreamerAdapter : public PipeStreamerAdapter { + public: + PipeAudioStreamerAdapter(const std::string& named_audio_pipe_path, + const std::string& app_storage_folder); + ~PipeAudioStreamerAdapter(); }; } // namespace media_manager diff --git a/src/components/media_manager/include/media_manager/audio/socket_audio_streamer_adapter.h b/src/components/media_manager/include/media_manager/audio/socket_audio_streamer_adapter.h index be5951be3e..c0f3174496 100644 --- a/src/components/media_manager/include/media_manager/audio/socket_audio_streamer_adapter.h +++ b/src/components/media_manager/include/media_manager/audio/socket_audio_streamer_adapter.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2014, Ford Motor Company + * Copyright (c) 2014-2015, Ford Motor Company * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -33,18 +33,17 @@ #ifndef SRC_COMPONENTS_MEDIA_MANAGER_INCLUDE_MEDIA_MANAGER_AUDIO_SOCKET_AUDIO_STREAMER_ADAPTER_H_ #define SRC_COMPONENTS_MEDIA_MANAGER_INCLUDE_MEDIA_MANAGER_AUDIO_SOCKET_AUDIO_STREAMER_ADAPTER_H_ -#include <string> #include "media_manager/socket_streamer_adapter.h" namespace media_manager { + class SocketAudioStreamerAdapter : public SocketStreamerAdapter { - public: - SocketAudioStreamerAdapter(); - virtual ~SocketAudioStreamerAdapter(); - private: - DISALLOW_COPY_AND_ASSIGN(SocketAudioStreamerAdapter); + public: + SocketAudioStreamerAdapter(const std::string& server_address, + uint16_t audio_streaming_port); + virtual ~SocketAudioStreamerAdapter(); }; -} // namespace media_manager +} // namespace media_manager #endif // SRC_COMPONENTS_MEDIA_MANAGER_INCLUDE_MEDIA_MANAGER_AUDIO_SOCKET_AUDIO_STREAMER_ADAPTER_H_ diff --git a/src/components/media_manager/include/media_manager/file_streamer_adapter.h b/src/components/media_manager/include/media_manager/file_streamer_adapter.h new file mode 100644 index 0000000000..293c6721ee --- /dev/null +++ b/src/components/media_manager/include/media_manager/file_streamer_adapter.h @@ -0,0 +1,71 @@ +/* + * Copyright (c) 2014-2015, Ford Motor Company + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following + * disclaimer in the documentation and/or other materials provided with the + * distribution. + * + * Neither the name of the Ford Motor Company nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef SRC_COMPONENTS_MEDIA_MANAGER_INCLUDE_MEDIA_MANAGER_FILE_STREAMER_ADAPTER_H_ +#define SRC_COMPONENTS_MEDIA_MANAGER_INCLUDE_MEDIA_MANAGER_FILE_STREAMER_ADAPTER_H_ + +#include <string> +#include <fstream> +#include "media_manager/streamer_adapter.h" +#include "utils/threads/thread_delegate.h" + +namespace media_manager { + +class FileStreamerAdapter : public StreamerAdapter { + public: + FileStreamerAdapter(const std::string& file_name, + const std::string& app_storage_folder); + virtual ~FileStreamerAdapter(); + + protected: + class FileStreamer : public StreamerAdapter::Streamer { + public: + FileStreamer(FileStreamerAdapter* const adapter, + const std::string& file_name, + const std::string& app_storage_folder); + virtual ~FileStreamer(); + + protected: + virtual bool Connect(); + virtual void Disconnect(); + virtual bool Send(protocol_handler::RawMessagePtr msg); + + private: + std::string file_name_; + std::string app_storage_folder_; + std::ofstream* file_stream_; + }; +}; + +} // namespace media_manager + +#endif // SRC_COMPONENTS_MEDIA_MANAGER_INCLUDE_MEDIA_MANAGER_FILE_STREAMER_ADAPTER_H_ diff --git a/src/components/media_manager/include/media_manager/media_adapter.h b/src/components/media_manager/include/media_manager/media_adapter.h index a96ce03c87..3fe05cae33 100644 --- a/src/components/media_manager/include/media_manager/media_adapter.h +++ b/src/components/media_manager/include/media_manager/media_adapter.h @@ -33,7 +33,7 @@ #ifndef SRC_COMPONENTS_MEDIA_MANAGER_INCLUDE_MEDIA_MANAGER_MEDIA_ADAPTER_H_ #define SRC_COMPONENTS_MEDIA_MANAGER_INCLUDE_MEDIA_MANAGER_MEDIA_ADAPTER_H_ -#include <cstdint> +#include <stdint.h> #include "protocol/common.h" namespace media_manager { @@ -44,9 +44,11 @@ class MediaAdapter { const ::protocol_handler::RawMessagePtr message) = 0; virtual void StartActivity(int32_t application_key) = 0; virtual void StopActivity(int32_t application_key) = 0; - virtual bool is_app_performing_activity(int32_t application_key) = 0; + virtual bool is_app_performing_activity(int32_t application_key) const = 0; - virtual ~MediaAdapter() { ;} + virtual ~MediaAdapter() { + ; + } }; } // namespace media_manager #endif // SRC_COMPONENTS_MEDIA_MANAGER_INCLUDE_MEDIA_MANAGER_MEDIA_ADAPTER_H_ diff --git a/src/components/media_manager/include/media_manager/media_adapter_impl.h b/src/components/media_manager/include/media_manager/media_adapter_impl.h index a3a296150b..75c48c98b7 100644 --- a/src/components/media_manager/include/media_manager/media_adapter_impl.h +++ b/src/components/media_manager/include/media_manager/media_adapter_impl.h @@ -43,18 +43,18 @@ namespace media_manager { typedef utils::SharedPtr<MediaAdapterListener> MediaListenerPtr; class MediaAdapterImpl : public MediaAdapter { - public: - virtual ~MediaAdapterImpl(); - virtual void AddListener(const MediaListenerPtr& listener); - virtual void RemoveListener(const MediaListenerPtr& listener); + public: + virtual ~MediaAdapterImpl(); + virtual void AddListener(const MediaListenerPtr& listener); + virtual void RemoveListener(const MediaListenerPtr& listener); - protected: - MediaAdapterImpl(); - std::set<MediaListenerPtr> media_listeners_; - int32_t current_application_; + protected: + MediaAdapterImpl(); + std::set<MediaListenerPtr> media_listeners_; + int32_t current_application_; - private: - DISALLOW_COPY_AND_ASSIGN(MediaAdapterImpl); + private: + DISALLOW_COPY_AND_ASSIGN(MediaAdapterImpl); }; typedef utils::SharedPtr<MediaAdapterImpl> MediaAdapterImplPtr; diff --git a/src/components/media_manager/include/media_manager/media_adapter_listener.h b/src/components/media_manager/include/media_manager/media_adapter_listener.h index 423d41c89b..37c3925199 100644 --- a/src/components/media_manager/include/media_manager/media_adapter_listener.h +++ b/src/components/media_manager/include/media_manager/media_adapter_listener.h @@ -40,17 +40,14 @@ namespace media_manager { typedef int32_t DataForListener; class MediaAdapterListener { - public: - virtual ~MediaAdapterListener() { - } - virtual void OnDataReceived( - int32_t application_key, - const DataForListener& data) = 0; - virtual void OnErrorReceived( - int32_t application_key, - const DataForListener& data) = 0; - virtual void OnActivityStarted(int32_t application_key) = 0; - virtual void OnActivityEnded(int32_t application_key) = 0; + public: + virtual ~MediaAdapterListener() {} + virtual void OnDataReceived(int32_t application_key, + const DataForListener& data) = 0; + virtual void OnErrorReceived(int32_t application_key, + const DataForListener& data) = 0; + virtual void OnActivityStarted(int32_t application_key) = 0; + virtual void OnActivityEnded(int32_t application_key) = 0; }; } // namespace media_manager #endif // SRC_COMPONENTS_MEDIA_MANAGER_INCLUDE_MEDIA_MANAGER_MEDIA_ADAPTER_LISTENER_H_ diff --git a/src/components/media_manager/include/media_manager/media_manager.h b/src/components/media_manager/include/media_manager/media_manager.h index b4f5c9c28b..53e3690c13 100644 --- a/src/components/media_manager/include/media_manager/media_manager.h +++ b/src/components/media_manager/include/media_manager/media_manager.h @@ -34,26 +34,33 @@ #define SRC_COMPONENTS_MEDIA_MANAGER_INCLUDE_MEDIA_MANAGER_MEDIA_MANAGER_H_ #include <string> -#include "protocol_handler/protocol_handler.h" - +#include "protocol/service_type.h" +#include "media_manager/media_manager_settings.h" namespace media_manager { class MediaManager { - public: - virtual void PlayA2DPSource(int32_t application_key) = 0; - virtual void StopA2DPSource(int32_t application_key) = 0; + public: + virtual void PlayA2DPSource(int32_t application_key) = 0; + virtual void StopA2DPSource(int32_t application_key) = 0; - virtual void StartMicrophoneRecording(int32_t application_key, - const std::string& outputFileName, - int32_t duration) = 0; - virtual void StopMicrophoneRecording(int32_t application_key) = 0; + virtual void StartMicrophoneRecording(int32_t application_key, + const std::string& outputFileName, + int32_t duration) = 0; + virtual void StopMicrophoneRecording(int32_t application_key) = 0; - virtual void StartStreaming( - int32_t application_key, protocol_handler::ServiceType service_type) = 0; - virtual void StopStreaming( - int32_t application_key, protocol_handler::ServiceType service_type) = 0; + virtual void StartStreaming(int32_t application_key, + protocol_handler::ServiceType service_type) = 0; + virtual void StopStreaming(int32_t application_key, + protocol_handler::ServiceType service_type) = 0; + virtual void FramesProcessed(int32_t application_key, + int32_t frame_number) = 0; + /** + * \brief Media manager settings getter + * \return pointer to media manager settings class + */ + virtual const MediaManagerSettings& settings() const = 0; - virtual ~MediaManager(){} + virtual ~MediaManager() {} }; } // namespace media_manager diff --git a/src/components/media_manager/include/media_manager/media_manager_impl.h b/src/components/media_manager/include/media_manager/media_manager_impl.h index ebdae7b958..c84a5a884f 100644 --- a/src/components/media_manager/include/media_manager/media_manager_impl.h +++ b/src/components/media_manager/include/media_manager/media_manager_impl.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2014, Ford Motor Company + * Copyright (c) 2016, Ford Motor Company * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -35,58 +35,80 @@ #include <string> #include <map> -#include "utils/singleton.h" #include "protocol_handler/protocol_observer.h" #include "protocol_handler/protocol_handler.h" #include "media_manager/media_manager.h" #include "media_manager/media_adapter_impl.h" #include "media_manager/media_adapter_listener.h" +namespace application_manager { +class ApplicationManager; +} + +namespace connection_handler { +class ConnectionHandlerImpl; +} + namespace media_manager { class MediaManagerImpl : public MediaManager, - public protocol_handler::ProtocolObserver, - public utils::Singleton<MediaManagerImpl> { - public: - virtual ~MediaManagerImpl(); + public protocol_handler::ProtocolObserver { + public: + MediaManagerImpl(application_manager::ApplicationManager& application_manager, + const MediaManagerSettings& settings); + virtual ~MediaManagerImpl(); - virtual void PlayA2DPSource(int32_t application_key); - virtual void StopA2DPSource(int32_t application_key); + virtual void PlayA2DPSource(int32_t application_key); + virtual void StopA2DPSource(int32_t application_key); - virtual void StartMicrophoneRecording(int32_t application_key, - const std::string& outputFileName, - int32_t duration); - virtual void StopMicrophoneRecording(int32_t application_key); + virtual void StartMicrophoneRecording(int32_t application_key, + const std::string& outputFileName, + int32_t duration); + virtual void StopMicrophoneRecording(int32_t application_key); - virtual void StartStreaming( - int32_t application_key, protocol_handler::ServiceType service_type); - virtual void StopStreaming( - int32_t application_key, protocol_handler::ServiceType service_type); + virtual void StartStreaming(int32_t application_key, + protocol_handler::ServiceType service_type); + virtual void StopStreaming(int32_t application_key, + protocol_handler::ServiceType service_type); - virtual void SetProtocolHandler( + virtual void SetProtocolHandler( protocol_handler::ProtocolHandler* protocol_handler); - virtual void OnMessageReceived( + virtual void OnMessageReceived( const ::protocol_handler::RawMessagePtr message); - virtual void OnMobileMessageSent( + virtual void OnMobileMessageSent( const ::protocol_handler::RawMessagePtr message); - virtual void FramesProcessed(int32_t application_key, int32_t frame_number); + virtual void FramesProcessed(int32_t application_key, int32_t frame_number); + + virtual const MediaManagerSettings& settings() const OVERRIDE; + +#ifdef BUILD_TESTS + void set_mock_a2dp_player(MediaAdapter* media_adapter); + void set_mock_mic_listener(MediaListenerPtr media_listener); + void set_mock_mic_recorder(MediaAdapterImpl* media_adapter); + void set_mock_streamer(protocol_handler::ServiceType stype, + MediaAdapterImpl* mock_stream); + void set_mock_streamer_listener(protocol_handler::ServiceType stype, + MediaAdapterListener* mock_stream); +#endif // BUILD_TESTS + + protected: + virtual void Init(); + + const MediaManagerSettings& settings_; - protected: - MediaManagerImpl(); - virtual void Init(); + protocol_handler::ProtocolHandler* protocol_handler_; + MediaAdapter* a2dp_player_; - protocol_handler::ProtocolHandler* protocol_handler_; - MediaAdapter* a2dp_player_; + MediaAdapterImpl* from_mic_recorder_; + MediaListenerPtr from_mic_listener_; - MediaAdapterImpl* from_mic_recorder_; - MediaListenerPtr from_mic_listener_; + std::map<protocol_handler::ServiceType, MediaAdapterImplPtr> streamer_; + std::map<protocol_handler::ServiceType, MediaListenerPtr> streamer_listener_; - std::map<protocol_handler::ServiceType, MediaAdapterImplPtr> streamer_; - std::map<protocol_handler::ServiceType, MediaListenerPtr> streamer_listener_; + application_manager::ApplicationManager& application_manager_; - private: - DISALLOW_COPY_AND_ASSIGN(MediaManagerImpl); - FRIEND_BASE_SINGLETON_CLASS(MediaManagerImpl); + private: + DISALLOW_COPY_AND_ASSIGN(MediaManagerImpl); }; } // namespace media_manager diff --git a/src/components/media_manager/include/media_manager/pipe_streamer_adapter.h b/src/components/media_manager/include/media_manager/pipe_streamer_adapter.h index f6a0c687ea..1e998ef82f 100644 --- a/src/components/media_manager/include/media_manager/pipe_streamer_adapter.h +++ b/src/components/media_manager/include/media_manager/pipe_streamer_adapter.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2014, Ford Motor Company + * Copyright (c) 2014-2015, Ford Motor Company * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -34,82 +34,35 @@ #define SRC_COMPONENTS_MEDIA_MANAGER_INCLUDE_MEDIA_MANAGER_PIPE_STREAMER_ADAPTER_H_ #include <string> -#include "media_manager/media_adapter_impl.h" -#include "utils/shared_ptr.h" -#include "utils/message_queue.h" -#include "utils/threads/thread.h" +#include "media_manager/streamer_adapter.h" #include "utils/threads/thread_delegate.h" namespace media_manager { -using ::utils::MessageQueue; - -class PipeStreamerAdapter : public MediaAdapterImpl { - public: - PipeStreamerAdapter(); - virtual ~PipeStreamerAdapter(); - virtual void SendData(int32_t application_key, - const ::protocol_handler::RawMessagePtr message); - virtual void StartActivity(int32_t application_key); - virtual void StopActivity(int32_t application_key); - virtual bool is_app_performing_activity(int32_t application_key); - - protected: +class PipeStreamerAdapter : public StreamerAdapter { + public: + PipeStreamerAdapter(const std::string& named_pipe_path, + const std::string& app_storage_folder); + virtual ~PipeStreamerAdapter(); + + protected: + class PipeStreamer : public StreamerAdapter::Streamer { + public: + PipeStreamer(PipeStreamerAdapter* const adapter, + const std::string& named_pipe_path, + const std::string& app_storage_folder); + virtual ~PipeStreamer(); + + protected: + virtual bool Connect(); + virtual void Disconnect(); + virtual bool Send(protocol_handler::RawMessagePtr msg); + + private: std::string named_pipe_path_; - - /* - * @brief Start streamer thread - */ - virtual void Init(); - - private: - class Streamer : public threads::ThreadDelegate { - public: - /* - * Default constructor - * - * @param server Server pointer - */ - explicit Streamer(PipeStreamerAdapter* server); - - /* - * Destructor - */ - ~Streamer(); - - /* - * @brief Function called by thread on start - */ - void threadMain(); - - /* - * @brief Function called by thread on exit - */ - void exitThreadMain(); - - /* - * @brief Opens pipe - */ - void open(); - - /* - * @brief Closes pipe - */ - void close(); - - private: - PipeStreamerAdapter* server_; - int32_t pipe_fd_; - volatile bool stop_flag_; - - DISALLOW_COPY_AND_ASSIGN(Streamer); - }; - - bool is_ready_; - threads::Thread* thread_; - MessageQueue<protocol_handler::RawMessagePtr> messages_; - - DISALLOW_COPY_AND_ASSIGN(PipeStreamerAdapter); + std::string app_storage_folder_; + int32_t pipe_fd_; + }; }; } // namespace media_manager diff --git a/src/components/media_manager/include/media_manager/socket_streamer_adapter.h b/src/components/media_manager/include/media_manager/socket_streamer_adapter.h index a4ded9cb33..45311b6077 100644 --- a/src/components/media_manager/include/media_manager/socket_streamer_adapter.h +++ b/src/components/media_manager/include/media_manager/socket_streamer_adapter.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2014, Ford Motor Company + * Copyright (c) 2014-2015, Ford Motor Company * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -34,106 +34,43 @@ #define SRC_COMPONENTS_MEDIA_MANAGER_INCLUDE_MEDIA_MANAGER_SOCKET_STREAMER_ADAPTER_H_ #include <string> -#include "media_manager/media_adapter_impl.h" -#include "utils/logger.h" -#include "utils/shared_ptr.h" -#include "utils/message_queue.h" -#include "utils/threads/thread.h" +#include "media_manager/streamer_adapter.h" #include "utils/threads/thread_delegate.h" namespace media_manager { -using ::utils::MessageQueue; - -class SocketStreamerAdapter : public MediaAdapterImpl { - public: - SocketStreamerAdapter(); - virtual ~SocketStreamerAdapter(); - virtual void SendData(int32_t application_key, - const ::protocol_handler::RawMessagePtr message); - virtual void StartActivity(int32_t application_key); - virtual void StopActivity(int32_t application_key); - virtual bool is_app_performing_activity(int32_t application_key); - - protected: - - /* - * @brief Start streamer thread - */ - virtual void Init(); - - int32_t port_; +class SocketStreamerAdapter : public StreamerAdapter { + public: + SocketStreamerAdapter(const std::string& ip, + uint16_t port, + const std::string& header); + virtual ~SocketStreamerAdapter(); + + protected: + class SocketStreamer : public StreamerAdapter::Streamer { + public: + SocketStreamer(SocketStreamerAdapter* const adapter, + const std::string& ip, + uint16_t port, + const std::string& header); + virtual ~SocketStreamer(); + + protected: + virtual bool Connect(); + virtual void Disconnect(); + virtual bool Send(protocol_handler::RawMessagePtr msg); + + private: std::string ip_; + uint16_t port_; + std::string header_; - private: - class Streamer : public threads::ThreadDelegate { - public: - /* - * Default constructor - * - * @param server Server pointer - */ - explicit Streamer(SocketStreamerAdapter* const server); - - /* - * Destructor - */ - ~Streamer(); - - /* - * Function called by thread on start - */ - void threadMain(); - - /* - * Function called by thread on exit - */ - void exitThreadMain(); - - /* - * Checks if server is ready - * - * @return TRUE if socket is ready otherwise FALSE - */ - bool is_ready() const; - - /* - * Starts server - * - */ - void start(); - - /* - * Stops server - * - */ - void stop(); - - /* - * Sends data to connected client - * - * @param block Pointer to the data - */ - bool send(const ::protocol_handler::RawMessagePtr msg); - - private: - SocketStreamerAdapter* const server_; - int32_t new_socket_fd_; - bool is_first_loop_; - volatile bool is_client_connected_; - volatile bool stop_flag_; - sync_primitives::Lock thread_lock; - DISALLOW_COPY_AND_ASSIGN(Streamer); - }; - - int32_t socket_fd_; - bool is_ready_; - Streamer* streamer_; - threads::Thread* thread_; - MessageQueue<protocol_handler::RawMessagePtr> messages_; - DISALLOW_COPY_AND_ASSIGN(SocketStreamerAdapter); + int32_t socket_fd_; + int32_t send_socket_fd_; + bool is_first_frame_; + }; }; -} // namespace media_manager +} // namespace media_manager #endif // SRC_COMPONENTS_MEDIA_MANAGER_INCLUDE_MEDIA_MANAGER_SOCKET_STREAMER_ADAPTER_H_ diff --git a/src/components/media_manager/include/media_manager/streamer_adapter.h b/src/components/media_manager/include/media_manager/streamer_adapter.h new file mode 100644 index 0000000000..30353b8d09 --- /dev/null +++ b/src/components/media_manager/include/media_manager/streamer_adapter.h @@ -0,0 +1,101 @@ +/* + * Copyright (c) 2015, Ford Motor Company + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following + * disclaimer in the documentation and/or other materials provided with the + * distribution. + * + * Neither the name of the Ford Motor Company nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef SRC_COMPONENTS_MEDIA_MANAGER_INCLUDE_MEDIA_MANAGER_STREAMER_ADAPTER_H_ +#define SRC_COMPONENTS_MEDIA_MANAGER_INCLUDE_MEDIA_MANAGER_STREAMER_ADAPTER_H_ + +#include "media_manager/media_adapter_impl.h" +#include "utils/message_queue.h" +#include "utils/threads/thread.h" +#include "utils/threads/thread_delegate.h" +#include "utils/atomic_object.h" +#include "utils/shared_ptr.h" +#include "protocol/raw_message.h" + +namespace media_manager { + +/** + * Class StreamerAdapter represents media adapter + * for streaming data to some destination point + * (pipe, socket, file) + */ +class StreamerAdapter : public MediaAdapterImpl { + protected: + class Streamer; + + public: + explicit StreamerAdapter(Streamer* const streamer); + virtual ~StreamerAdapter(); + + virtual void StartActivity(int32_t application_key); + virtual void StopActivity(int32_t application_key); + virtual void SendData(int32_t application_key, + const ::protocol_handler::RawMessagePtr msg); + virtual bool is_app_performing_activity(int32_t application_key) const; + + protected: + // TODO(AN): APPLINK-15203 Use MessageLoopThread + class Streamer : public threads::ThreadDelegate { + public: + explicit Streamer(StreamerAdapter* const adapter); + virtual ~Streamer(); + + virtual void threadMain(); + virtual void exitThreadMain(); + + protected: + virtual bool Connect() = 0; + virtual void Disconnect() = 0; + virtual bool Send(protocol_handler::RawMessagePtr msg) = 0; + + private: + sync_primitives::atomic_bool stop_flag_; + StreamerAdapter* adapter_; + + DISALLOW_COPY_AND_ASSIGN(Streamer); + }; + + private: + int32_t current_application_; + utils::MessageQueue<protocol_handler::RawMessagePtr> messages_; + + Streamer* streamer_; + threads::Thread* thread_; + + DISALLOW_COPY_AND_ASSIGN(StreamerAdapter); +}; + +typedef utils::SharedPtr<StreamerAdapter> StreamerAdapterPtr; + +} // namespace media_manager + +#endif // SRC_COMPONENTS_MEDIA_MANAGER_INCLUDE_MEDIA_MANAGER_STREAMER_ADAPTER_H_ diff --git a/src/components/media_manager/include/media_manager/streamer_listener.h b/src/components/media_manager/include/media_manager/streamer_listener.h index d232582514..32ba167985 100644 --- a/src/components/media_manager/include/media_manager/streamer_listener.h +++ b/src/components/media_manager/include/media_manager/streamer_listener.h @@ -38,21 +38,24 @@ #include "utils/macro.h" namespace media_manager { + +class MediaManager; + class StreamerListener : public MediaAdapterListener { - public: - StreamerListener(); - ~StreamerListener(); - virtual void OnDataReceived( - int32_t application_key, - const DataForListener& data); - virtual void OnErrorReceived( - int32_t application_key, - const DataForListener& data); - virtual void OnActivityStarted(int32_t application_key); - virtual void OnActivityEnded(int32_t application_key); - private: - int32_t current_application_; - DISALLOW_COPY_AND_ASSIGN(StreamerListener); + public: + StreamerListener(MediaManager& media_manager); + ~StreamerListener(); + virtual void OnDataReceived(int32_t application_key, + const DataForListener& data); + virtual void OnErrorReceived(int32_t application_key, + const DataForListener& data); + virtual void OnActivityStarted(int32_t application_key); + virtual void OnActivityEnded(int32_t application_key); + + private: + int32_t current_application_; + media_manager::MediaManager& media_manager_; + DISALLOW_COPY_AND_ASSIGN(StreamerListener); }; } // namespace media_manager diff --git a/src/components/media_manager/include/media_manager/video/file_video_streamer_adapter.h b/src/components/media_manager/include/media_manager/video/file_video_streamer_adapter.h new file mode 100644 index 0000000000..1677b5ad34 --- /dev/null +++ b/src/components/media_manager/include/media_manager/video/file_video_streamer_adapter.h @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2014-2015, Ford Motor Company + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following + * disclaimer in the documentation and/or other materials provided with the + * distribution. + * + * Neither the name of the Ford Motor Company nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef SRC_COMPONENTS_MEDIA_MANAGER_INCLUDE_MEDIA_MANAGER_VIDEO_FILE_VIDEO_STREAMER_ADAPTER_H_ +#define SRC_COMPONENTS_MEDIA_MANAGER_INCLUDE_MEDIA_MANAGER_VIDEO_FILE_VIDEO_STREAMER_ADAPTER_H_ + +#include "media_manager/file_streamer_adapter.h" + +namespace media_manager { + +class FileVideoStreamerAdapter : public FileStreamerAdapter { + public: + FileVideoStreamerAdapter(const std::string& video_stream_file, + const std::string& app_storage_folder); + ~FileVideoStreamerAdapter(); +}; + +} // namespace media_manager + +#endif // SRC_COMPONENTS_MEDIA_MANAGER_INCLUDE_MEDIA_MANAGER_VIDEO_FILE_VIDEO_STREAMER_ADAPTER_H_ diff --git a/src/components/media_manager/include/media_manager/video/pipe_video_streamer_adapter.h b/src/components/media_manager/include/media_manager/video/pipe_video_streamer_adapter.h index 62447661a6..3bf5723572 100644 --- a/src/components/media_manager/include/media_manager/video/pipe_video_streamer_adapter.h +++ b/src/components/media_manager/include/media_manager/video/pipe_video_streamer_adapter.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2014, Ford Motor Company + * Copyright (c) 2014-2015, Ford Motor Company * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -33,17 +33,15 @@ #ifndef SRC_COMPONENTS_MEDIA_MANAGER_INCLUDE_MEDIA_MANAGER_VIDEO_PIPE_VIDEO_STREAMER_ADAPTER_H_ #define SRC_COMPONENTS_MEDIA_MANAGER_INCLUDE_MEDIA_MANAGER_VIDEO_PIPE_VIDEO_STREAMER_ADAPTER_H_ -#include <string> #include "media_manager/pipe_streamer_adapter.h" namespace media_manager { -class PipeVideoStreamerAdapter : public PipeStreamerAdapter { - public: - PipeVideoStreamerAdapter(); - ~PipeVideoStreamerAdapter(); - private: - DISALLOW_COPY_AND_ASSIGN(PipeVideoStreamerAdapter); +class PipeVideoStreamerAdapter : public PipeStreamerAdapter { + public: + PipeVideoStreamerAdapter(const std::string& named_video_pipe_path, + const std::string& app_storage_folder); + ~PipeVideoStreamerAdapter(); }; } // namespace media_manager diff --git a/src/components/media_manager/include/media_manager/video/socket_video_streamer_adapter.h b/src/components/media_manager/include/media_manager/video/socket_video_streamer_adapter.h index e5eefed12c..ad5739bd5f 100644 --- a/src/components/media_manager/include/media_manager/video/socket_video_streamer_adapter.h +++ b/src/components/media_manager/include/media_manager/video/socket_video_streamer_adapter.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2014, Ford Motor Company + * Copyright (c) 2014-2015, Ford Motor Company * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -34,18 +34,19 @@ #define SRC_COMPONENTS_MEDIA_MANAGER_INCLUDE_MEDIA_MANAGER_VIDEO_SOCKET_VIDEO_STREAMER_ADAPTER_H_ #include <string> +#include <stdint.h> + #include "media_manager/socket_streamer_adapter.h" namespace media_manager { -class SocketVideoStreamerAdapter : public SocketStreamerAdapter { - public: - SocketVideoStreamerAdapter(); - virtual ~SocketVideoStreamerAdapter(); - private: - DISALLOW_COPY_AND_ASSIGN(SocketVideoStreamerAdapter); +class SocketVideoStreamerAdapter : public SocketStreamerAdapter { + public: + SocketVideoStreamerAdapter(const std::string& server_address, + uint16_t video_streaming_port); + virtual ~SocketVideoStreamerAdapter(); }; -} // namespace media_manager +} // namespace media_manager #endif // SRC_COMPONENTS_MEDIA_MANAGER_INCLUDE_MEDIA_MANAGER_VIDEO_SOCKET_VIDEO_STREAMER_ADAPTER_H_ diff --git a/src/components/media_manager/include/media_manager/video/video_stream_to_file_adapter.h b/src/components/media_manager/include/media_manager/video/video_stream_to_file_adapter.h deleted file mode 100644 index 8ec7fff803..0000000000 --- a/src/components/media_manager/include/media_manager/video/video_stream_to_file_adapter.h +++ /dev/null @@ -1,113 +0,0 @@ -/* - * Copyright (c) 2014, Ford Motor Company - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * Redistributions of source code must retain the above copyright notice, this - * list of conditions and the following disclaimer. - * - * Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following - * disclaimer in the documentation and/or other materials provided with the - * distribution. - * - * Neither the name of the Ford Motor Company nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#ifndef SRC_COMPONENTS_MEDIA_MANAGER_INCLUDE_MEDIA_MANAGER_VIDEO_VIDEO_STREAM_TO_FILE_ADAPTER_H_ -#define SRC_COMPONENTS_MEDIA_MANAGER_INCLUDE_MEDIA_MANAGER_VIDEO_VIDEO_STREAM_TO_FILE_ADAPTER_H_ - -#include <string> -#include <fstream> -#include "media_manager/media_adapter_impl.h" -#include "utils/message_queue.h" -#include "utils/threads/thread.h" -#include "utils/threads/thread_delegate.h" - -namespace media_manager { - -using ::utils::MessageQueue; - -class VideoStreamToFileAdapter : public MediaAdapterImpl { - public: - explicit VideoStreamToFileAdapter(const std::string& file_name); - virtual ~VideoStreamToFileAdapter(); - virtual void SendData(int32_t application_key, - const ::protocol_handler::RawMessagePtr message); - virtual void StartActivity(int32_t application_key); - virtual void StopActivity(int32_t application_key); - virtual bool is_app_performing_activity(int32_t application_key); - - /* - * @brief Start streamer thread - */ - virtual void Init(); - - private: - class Streamer : public threads::ThreadDelegate { - public: - /* - * Default constructor - * - * @param server Server pointer - */ - explicit Streamer(VideoStreamToFileAdapter* server); - - /* - * Destructor - */ - ~Streamer(); - - /* - * @brief Function called by thread on start - */ - void threadMain(); - - /* - * @brief Function called by thread on exit - */ - void exitThreadMain(); - - /* - * @brief Opens file - */ - void open(); - - /* - * @brief Closes file - */ - void close(); - - private: - VideoStreamToFileAdapter* server_; - volatile bool stop_flag_; - std::ofstream* file_stream_; - - DISALLOW_COPY_AND_ASSIGN(Streamer); - }; - - private: - std::string file_name_; - bool is_ready_; - threads::Thread* thread_; - MessageQueue<protocol_handler::RawMessagePtr> messages_; -}; -} // namespace media_manager - -#endif // SRC_COMPONENTS_MEDIA_MANAGER_INCLUDE_MEDIA_MANAGER_VIDEO_VIDEO_STREAM_TO_FILE_ADAPTER_H_ diff --git a/src/components/media_manager/src/audio/a2dp_source_player_adapter.cc b/src/components/media_manager/src/audio/a2dp_source_player_adapter.cc index a6a36ef547..e5cd41c8c3 100644 --- a/src/components/media_manager/src/audio/a2dp_source_player_adapter.cc +++ b/src/components/media_manager/src/audio/a2dp_source_player_adapter.cc @@ -39,42 +39,43 @@ #include "utils/lock.h" #include "utils/logger.h" #include "connection_handler/connection_handler_impl.h" +#include "protocol_handler/session_observer.h" namespace media_manager { -CREATE_LOGGERPTR_GLOBAL(logger_, "A2DPSourcePlayerAdapter"); +CREATE_LOGGERPTR_GLOBAL(logger_, "MediaManager") const static size_t BUFSIZE = 32; class A2DPSourcePlayerAdapter::A2DPSourcePlayerThread : public threads::ThreadDelegate { - public: - explicit A2DPSourcePlayerThread(const std::string& device); + public: + explicit A2DPSourcePlayerThread(const std::string& device); - void threadMain(); + void threadMain(); - void exitThreadMain(); + void exitThreadMain(); - private: - // The Sample format to use - static const pa_sample_spec sSampleFormat_; + private: + // The Sample format to use + static const pa_sample_spec sSampleFormat_; - pa_simple* s_in, *s_out; - std::string device_; - bool should_be_stopped_; - sync_primitives::Lock should_be_stopped_lock_; + pa_simple* s_in, *s_out; + std::string device_; + bool should_be_stopped_; + sync_primitives::Lock should_be_stopped_lock_; - void freeStreams(); + void freeStreams(); - DISALLOW_COPY_AND_ASSIGN(A2DPSourcePlayerThread); + DISALLOW_COPY_AND_ASSIGN(A2DPSourcePlayerThread); }; -A2DPSourcePlayerAdapter::A2DPSourcePlayerAdapter() { -} +A2DPSourcePlayerAdapter::A2DPSourcePlayerAdapter( + protocol_handler::SessionObserver& session_observer) + : session_observer_(session_observer) {} A2DPSourcePlayerAdapter::~A2DPSourcePlayerAdapter() { - for (SourcesMap::iterator it = sources_.begin(); - sources_.end() != it; ++it) { + for (SourcesMap::iterator it = sources_.begin(); sources_.end() != it; ++it) { Pair pair = it->second; pair.first->join(); delete pair.second; @@ -84,20 +85,19 @@ A2DPSourcePlayerAdapter::~A2DPSourcePlayerAdapter() { } void A2DPSourcePlayerAdapter::StartActivity(int32_t application_key) { - LOG4CXX_INFO(logger_, "Starting a2dp playing music for " - << application_key << " application."); + LOG4CXX_INFO(logger_, + "Starting a2dp playing music for " << application_key + << " application."); if (application_key != current_application_) { current_application_ = application_key; + const protocol_handler::SessionObserver& + session_observer =->connection_handler().get_session_observer(); + uint32_t device_id = 0; - connection_handler::ConnectionHandlerImpl::instance()-> - GetDataOnSessionKey(application_key, 0, NULL, &device_id); + session_observer_.GetDataOnSessionKey(application_key, 0, NULL, &device_id); std::string mac_adddress; - connection_handler::ConnectionHandlerImpl::instance()->GetDataOnDeviceID( - device_id, - NULL, - NULL, - &mac_adddress); + session_observer_.GetDataOnDeviceID(device_id, NULL, NULL, &mac_adddress); // TODO(PK): Convert mac_adddress to the // following format : "bluez_source.XX_XX_XX_XX_XX_XX" if needed @@ -105,16 +105,17 @@ void A2DPSourcePlayerAdapter::StartActivity(int32_t application_key) { A2DPSourcePlayerThread* delegate = new A2DPSourcePlayerAdapter::A2DPSourcePlayerThread(mac_adddress); - threads::Thread* new_activity = threads::CreateThread( - mac_adddress.c_str(), delegate); + threads::Thread* new_activity = + threads::CreateThread(mac_adddress.c_str(), delegate); sources_[application_key] = Pair(new_activity, delegate); new_activity->start(); } } void A2DPSourcePlayerAdapter::StopActivity(int32_t application_key) { - LOG4CXX_INFO(logger_, "Stopping 2dp playing for " - << application_key << " application."); + LOG4CXX_INFO(logger_, + "Stopping 2dp playing for " << application_key + << " application."); if (application_key != current_application_) { return; } @@ -128,23 +129,20 @@ void A2DPSourcePlayerAdapter::StopActivity(int32_t application_key) { } } -bool A2DPSourcePlayerAdapter::is_app_performing_activity(int32_t - application_key) { +bool A2DPSourcePlayerAdapter::is_app_performing_activity( + int32_t application_key) const { return (application_key == current_application_); } -const pa_sample_spec A2DPSourcePlayerAdapter::A2DPSourcePlayerThread:: -sSampleFormat_ = { - /*format*/ PA_SAMPLE_S16LE, - /*rate*/ 44100, - /*channels*/ 2 -}; +const pa_sample_spec + A2DPSourcePlayerAdapter::A2DPSourcePlayerThread::sSampleFormat_ = { + /*format*/ PA_SAMPLE_S16LE, + /*rate*/ 44100, + /*channels*/ 2}; A2DPSourcePlayerAdapter::A2DPSourcePlayerThread::A2DPSourcePlayerThread( - const std::string& device) - : threads::ThreadDelegate(), - device_(device) { -} + const std::string& device) + : threads::ThreadDelegate(), device_(device) {} void A2DPSourcePlayerAdapter::A2DPSourcePlayerThread::freeStreams() { LOG4CXX_INFO(logger_, "Free streams in A2DPSourcePlayerThread."); @@ -179,15 +177,29 @@ void A2DPSourcePlayerAdapter::A2DPSourcePlayerThread::threadMain() { LOG4CXX_DEBUG(logger_, "Creating streams"); /* Create a new playback stream */ - if (!(s_out = pa_simple_new(NULL, "AudioManager", PA_STREAM_PLAYBACK, NULL, - "playback", &sSampleFormat_, NULL, NULL, &error))) { + if (!(s_out = pa_simple_new(NULL, + "AudioManager", + PA_STREAM_PLAYBACK, + NULL, + "playback", + &sSampleFormat_, + NULL, + NULL, + &error))) { LOG4CXX_ERROR(logger_, "pa_simple_new() failed: " << pa_strerror(error)); freeStreams(); return; } - if (!(s_in = pa_simple_new(NULL, "AudioManager", PA_STREAM_RECORD, a2dpSource, - "record", &sSampleFormat_, NULL, NULL, &error))) { + if (!(s_in = pa_simple_new(NULL, + "AudioManager", + PA_STREAM_RECORD, + a2dpSource, + "record", + &sSampleFormat_, + NULL, + NULL, + &error))) { LOG4CXX_ERROR(logger_, "pa_simple_new() failed: " << pa_strerror(error)); freeStreams(); return; @@ -200,17 +212,17 @@ void A2DPSourcePlayerAdapter::A2DPSourcePlayerThread::threadMain() { pa_usec_t latency; - if ((latency = pa_simple_get_latency(s_in, &error)) == (pa_usec_t) - 1) { - LOG4CXX_ERROR(logger_, "pa_simple_get_latency() failed: " - << pa_strerror(error)); + if ((latency = pa_simple_get_latency(s_in, &error)) == (pa_usec_t)-1) { + LOG4CXX_ERROR(logger_, + "pa_simple_get_latency() failed: " << pa_strerror(error)); break; } // LOG4CXX_INFO(logger_, "In: " << static_cast<float>(latency)); - if ((latency = pa_simple_get_latency(s_out, &error)) == (pa_usec_t) - 1) { - LOG4CXX_ERROR(logger_, "pa_simple_get_latency() failed: " - << pa_strerror(error)); + if ((latency = pa_simple_get_latency(s_out, &error)) == (pa_usec_t)-1) { + LOG4CXX_ERROR(logger_, + "pa_simple_get_latency() failed: " << pa_strerror(error)); break; } @@ -223,8 +235,8 @@ void A2DPSourcePlayerAdapter::A2DPSourcePlayerThread::threadMain() { /* ... and play it */ if (pa_simple_write(s_out, buf, sizeof(buf), &error) < 0) { - LOG4CXX_ERROR(logger_, "pa_simple_write() failed: " - << pa_strerror(error)); + LOG4CXX_ERROR(logger_, + "pa_simple_write() failed: " << pa_strerror(error)); break; } diff --git a/src/components/media_manager/src/audio/audio_stream_sender_thread.cc b/src/components/media_manager/src/audio/audio_stream_sender_thread.cc index d43d5461e0..49ead6a662 100644 --- a/src/components/media_manager/src/audio/audio_stream_sender_thread.cc +++ b/src/components/media_manager/src/audio/audio_stream_sender_thread.cc @@ -30,23 +30,21 @@ // POSSIBILITY OF SUCH DAMAGE. // - #if defined(OS_POSIX) && defined(OS_LINUX) #include <pthread.h> // TODO(DK): Need to remove #include <unistd.h> #endif - #include <string> #include <string.h> -#include "application_manager/application_manager_impl.h" +#include "application_manager/application_manager.h" #include "application_manager/mobile_command_factory.h" #include "application_manager/application_impl.h" #include "smart_objects/smart_object.h" #include "interfaces/MOBILE_API.h" #include "utils/file_system.h" #include "utils/logger.h" - +#include "media_manager/media_manager_settings.h" #include "media_manager/audio/audio_stream_sender_thread.h" #include "application_manager/smart_object_keys.h" #include "application_manager/message.h" @@ -57,21 +55,23 @@ using sync_primitives::AutoLock; const int32_t AudioStreamSenderThread::kAudioPassThruTimeout = 1; const uint32_t kMqueueMessageSize = 4095; -CREATE_LOGGERPTR_GLOBAL(logger_, "AudioPassThruThread") +CREATE_LOGGERPTR_GLOBAL(logger_, "MediaManager") AudioStreamSenderThread::AudioStreamSenderThread( - const std::string fileName, uint32_t session_key) - : session_key_(session_key), - fileName_(fileName), - offset_(0), - shouldBeStoped_(false), - shouldBeStoped_lock_(), - shouldBeStoped_cv_() { + const std::string& fileName, + uint32_t session_key, + application_manager::ApplicationManager& app_mngr) + : session_key_(session_key) + , fileName_(fileName) + , offset_(0) + , shouldBeStoped_(false) + , shouldBeStoped_lock_() + , shouldBeStoped_cv_() + , application_manager_(app_mngr) { LOG4CXX_AUTO_TRACE(logger_); } -AudioStreamSenderThread::~AudioStreamSenderThread() { -} +AudioStreamSenderThread::~AudioStreamSenderThread() {} void AudioStreamSenderThread::threadMain() { LOG4CXX_AUTO_TRACE(logger_); @@ -83,7 +83,6 @@ void AudioStreamSenderThread::threadMain() { shouldBeStoped_cv_.WaitFor(auto_lock, kAudioPassThruTimeout * 1000); sendAudioChunkToMobile(); } - } void AudioStreamSenderThread::sendAudioChunkToMobile() { @@ -94,29 +93,28 @@ void AudioStreamSenderThread::sendAudioChunkToMobile() { std::vector<uint8_t>::iterator to; if (!file_system::ReadBinaryFile(fileName_, binaryData)) { - LOG4CXX_ERROR_EXT(logger_, "Unable to read file." << fileName_); + LOG4CXX_ERROR(logger_, "Unable to read file." << fileName_); return; } if (binaryData.empty()) { - LOG4CXX_ERROR_EXT(logger_, "Binary data is empty."); + LOG4CXX_ERROR(logger_, "Binary data is empty."); return; } - LOG4CXX_INFO_EXT(logger_, "offset = " << offset_); + LOG4CXX_INFO(logger_, "offset = " << offset_); from = binaryData.begin() + offset_; to = binaryData.end(); if (from < binaryData.end() /*from != binaryData.end()*/) { - LOG4CXX_INFO_EXT(logger_, "from != binaryData.end()"); + LOG4CXX_INFO(logger_, "from != binaryData.end()"); offset_ = offset_ + to - from; std::vector<uint8_t> data(from, to); - application_manager::ApplicationManagerImpl::instance()-> - SendAudioPassThroughNotification(session_key_, data); + application_manager_.SendAudioPassThroughNotification(session_key_, data); binaryData.clear(); } #if !defined(EXTENDED_MEDIA_MODE) diff --git a/src/components/media_manager/src/audio/file_audio_streamer_adapter.cc b/src/components/media_manager/src/audio/file_audio_streamer_adapter.cc new file mode 100644 index 0000000000..f632fa81af --- /dev/null +++ b/src/components/media_manager/src/audio/file_audio_streamer_adapter.cc @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2014-2015, Ford Motor Company + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following + * disclaimer in the documentation and/or other materials provided with the + * distribution. + * + * Neither the name of the Ford Motor Company nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include "media_manager/audio/file_audio_streamer_adapter.h" + +namespace media_manager { + +FileAudioStreamerAdapter::FileAudioStreamerAdapter( + const std::string& audio_stream_file, const std::string& app_storage_folder) + : FileStreamerAdapter(audio_stream_file, app_storage_folder) {} + +FileAudioStreamerAdapter::~FileAudioStreamerAdapter() {} + +} // namespace media_manager diff --git a/src/components/media_manager/src/audio/from_mic_recorder_adapter.cc b/src/components/media_manager/src/audio/from_mic_recorder_adapter.cc index ef9d5b8de6..d9735fcbbc 100644 --- a/src/components/media_manager/src/audio/from_mic_recorder_adapter.cc +++ b/src/components/media_manager/src/audio/from_mic_recorder_adapter.cc @@ -38,15 +38,13 @@ namespace media_manager { -CREATE_LOGGERPTR_GLOBAL(logger_, "FromMicRecorderAdapter") +CREATE_LOGGERPTR_GLOBAL(logger_, "MediaManager") FromMicRecorderAdapter::FromMicRecorderAdapter() - : recorder_thread_(NULL) - , output_file_("default_recorded_audio.wav") - , kDefaultDuration(1000) - , duration_(kDefaultDuration) { - -} + : recorder_thread_(NULL) + , output_file_("default_recorded_audio.wav") + , kDefaultDuration(1000) + , duration_(kDefaultDuration) {} FromMicRecorderAdapter::~FromMicRecorderAdapter() { LOG4CXX_AUTO_TRACE(logger_); @@ -60,18 +58,16 @@ FromMicRecorderAdapter::~FromMicRecorderAdapter() { void FromMicRecorderAdapter::StartActivity(int32_t application_key) { LOG4CXX_DEBUG(logger_, "Start with app " << application_key); if (application_key == current_application_) { - LOG4CXX_WARN(logger_, "Running recording from mic for " - << current_application_); + LOG4CXX_WARN(logger_, + "Running recording from mic for " << current_application_); return; } -// Todd: No gstreamer recorder thread + // Todd: No gstreamer recorder thread if (!recorder_thread_) { FromMicToFileRecorderThread* thread_delegate = - new FromMicToFileRecorderThread( - output_file_, duration_); - recorder_thread_ = threads::CreateThread("MicrophoneRec", - thread_delegate); + new FromMicToFileRecorderThread(output_file_, duration_); + recorder_thread_ = threads::CreateThread("MicrophoneRec", thread_delegate); } if (NULL != recorder_thread_) { @@ -81,11 +77,11 @@ void FromMicRecorderAdapter::StartActivity(int32_t application_key) { } void FromMicRecorderAdapter::StopActivity(int32_t application_key) { - LOG4CXX_INFO(logger_, "FromMicRecorderAdapter::StopActivity " - << application_key); + LOG4CXX_INFO(logger_, + "FromMicRecorderAdapter::StopActivity " << application_key); if (application_key != current_application_) { - LOG4CXX_WARN(logger_, "Running activity on other app key " - << current_application_); + LOG4CXX_WARN(logger_, + "Running activity on other app key " << current_application_); return; } @@ -98,8 +94,8 @@ void FromMicRecorderAdapter::StopActivity(int32_t application_key) { current_application_ = 0; } -bool FromMicRecorderAdapter::is_app_performing_activity(int32_t - application_key) { +bool FromMicRecorderAdapter::is_app_performing_activity( + int32_t application_key) const { return (application_key == current_application_); } diff --git a/src/components/media_manager/src/audio/from_mic_recorder_listener.cc b/src/components/media_manager/src/audio/from_mic_recorder_listener.cc index 93c85b2ae7..fc55e69d28 100644 --- a/src/components/media_manager/src/audio/from_mic_recorder_listener.cc +++ b/src/components/media_manager/src/audio/from_mic_recorder_listener.cc @@ -37,14 +37,15 @@ namespace media_manager { -CREATE_LOGGERPTR_GLOBAL(logger_, "FromMicRecorderListener") +CREATE_LOGGERPTR_GLOBAL(logger_, "MediaManager") FromMicRecorderListener::FromMicRecorderListener( - const std::string& file_name) - : reader_(NULL) - , file_name_(file_name) - , current_application_(0) { -} + const std::string& file_name, + application_manager::ApplicationManager& app_mngr) + : reader_(NULL) + , file_name_(file_name) + , current_application_(0) + , application_manager_(app_mngr) {} FromMicRecorderListener::~FromMicRecorderListener() { LOG4CXX_AUTO_TRACE(logger_); @@ -55,25 +56,22 @@ FromMicRecorderListener::~FromMicRecorderListener() { } } -void FromMicRecorderListener::OnDataReceived( - int32_t application_key, - const DataForListener& data) { -} +void FromMicRecorderListener::OnDataReceived(int32_t application_key, + const DataForListener& data) {} -void FromMicRecorderListener::OnErrorReceived( - int32_t application_key, - const DataForListener& data) { -} +void FromMicRecorderListener::OnErrorReceived(int32_t application_key, + const DataForListener& data) {} void FromMicRecorderListener::OnActivityStarted(int32_t application_key) { - LOG4CXX_INFO(logger_, "FromMicRecorderListener::OnActivityStarted " - << application_key); + LOG4CXX_INFO(logger_, + "FromMicRecorderListener::OnActivityStarted " + << application_key); if (application_key == current_application_) { return; } if (!reader_) { - AudioStreamSenderThread* thread_delegate = - new AudioStreamSenderThread(file_name_, application_key); + AudioStreamSenderThread* thread_delegate = new AudioStreamSenderThread( + file_name_, application_key, application_manager_); reader_ = threads::CreateThread("RecorderSender", thread_delegate); } if (reader_) { @@ -83,11 +81,12 @@ void FromMicRecorderListener::OnActivityStarted(int32_t application_key) { } void FromMicRecorderListener::OnActivityEnded(int32_t application_key) { - LOG4CXX_INFO(logger_, "FromMicRecorderListener::OnActivityEnded " - << application_key); + LOG4CXX_INFO(logger_, + "FromMicRecorderListener::OnActivityEnded " << application_key); if (application_key != current_application_) { - LOG4CXX_WARN(logger_, "Not performing activity on " << application_key - << " but on " << current_application_); + LOG4CXX_WARN(logger_, + "Not performing activity on " << application_key << " but on " + << current_application_); return; } if (reader_) { diff --git a/src/components/media_manager/src/audio/from_mic_to_file_recorder_thread.cc b/src/components/media_manager/src/audio/from_mic_to_file_recorder_thread.cc index 88c358a494..0239795d75 100644 --- a/src/components/media_manager/src/audio/from_mic_to_file_recorder_thread.cc +++ b/src/components/media_manager/src/audio/from_mic_to_file_recorder_thread.cc @@ -37,19 +37,19 @@ namespace media_manager { -CREATE_LOGGERPTR_GLOBAL(logger_, "FromMicToFileRecorderThread") +CREATE_LOGGERPTR_GLOBAL(logger_, "MediaManager") GMainLoop* FromMicToFileRecorderThread::loop = NULL; FromMicToFileRecorderThread::FromMicToFileRecorderThread( - const std::string& output_file, int32_t duration) - : threads::ThreadDelegate(), - argc_(5), - argv_(NULL), - oKey_("-o"), - tKey_("-t"), - sleepThread_(NULL), - outputFileName_(output_file) { + const std::string& output_file, int32_t duration) + : threads::ThreadDelegate() + , argc_(5) + , argv_(NULL) + , oKey_("-o") + , tKey_("-t") + , sleepThread_(NULL) + , outputFileName_(output_file) { LOG4CXX_AUTO_TRACE(logger_); set_record_duration(duration); } @@ -64,7 +64,7 @@ FromMicToFileRecorderThread::~FromMicToFileRecorderThread() { } void FromMicToFileRecorderThread::set_output_file( - const std::string& output_file) { + const std::string& output_file) { LOG4CXX_AUTO_TRACE(logger_); outputFileName_ = output_file; } @@ -80,7 +80,7 @@ void FromMicToFileRecorderThread::set_record_duration(int32_t duration) { void FromMicToFileRecorderThread::initArgs() { LOG4CXX_AUTO_TRACE(logger_); - argv_ = new gchar*[argc_]; + argv_ = new gchar* [argc_]; argv_[0] = new gchar[14]; argv_[1] = new gchar[3]; @@ -114,20 +114,29 @@ void FromMicToFileRecorderThread::threadMain() { gint duration = -1; GOptionContext* context = NULL; GError* err = NULL; - GOptionEntry entries[] = { { - "device", 'd', 0, G_OPTION_ARG_FILENAME, &device, - "device file (Default: hw:0,0)", "SRC" - }, { - "output", 'o', 0, G_OPTION_ARG_FILENAME, &outfile, - "save output of the stream to DEST", "DEST" - }, { - "duration", 't', 0, G_OPTION_ARG_INT, &duration, - "length of time in seconds to capture", "int32_t" - }, { - NULL - } - }; -#ifndef GLIB_VERSION_2_32 // g_thread_init() does nothing since 2.32 + GOptionEntry entries[] = {{"device", + 'd', + 0, + G_OPTION_ARG_FILENAME, + &device, + "device file (Default: hw:0,0)", + "SRC"}, + {"output", + 'o', + 0, + G_OPTION_ARG_FILENAME, + &outfile, + "save output of the stream to DEST", + "DEST"}, + {"duration", + 't', + 0, + G_OPTION_ARG_INT, + &duration, + "length of time in seconds to capture", + "int32_t"}, + {NULL}}; +#ifndef GLIB_VERSION_2_32 // g_thread_init() does nothing since 2.32 if (!g_thread_supported()) { g_thread_init(NULL); } @@ -156,10 +165,10 @@ void FromMicToFileRecorderThread::threadMain() { // Set up error handling bus = gst_pipeline_get_bus(GST_PIPELINE(pipeline)); - gst_bus_add_watch(bus, - reinterpret_cast<int32_t (*)(_GstBus*, _GstMessage*, void*)> - (recvmsg), - NULL); + gst_bus_add_watch( + bus, + reinterpret_cast<int32_t (*)(_GstBus*, _GstMessage*, void*)>(recvmsg), + NULL); gst_object_unref(bus); // Create all of the elements to be added to the pipeline @@ -199,7 +208,7 @@ void FromMicToFileRecorderThread::threadMain() { g_option_context_free(context); if (argv_) { - delete [] argv_; + delete[] argv_; argv_ = NULL; } return; @@ -213,7 +222,8 @@ void FromMicToFileRecorderThread::threadMain() { timeout.pipeline = pipeline; timeout.duration = duration; - sleepThread_ = threads::CreateThread("SleepThread", new SleepThreadDelegate(timeout)); + sleepThread_ = + threads::CreateThread("SleepThread", new SleepThreadDelegate(timeout)); sleepThread_->start(); } @@ -229,18 +239,16 @@ void FromMicToFileRecorderThread::threadMain() { g_option_context_free(context); if (argv_) { - delete [] argv_; + delete[] argv_; argv_ = NULL; } loop = NULL; } -FromMicToFileRecorderThread::SleepThreadDelegate::SleepThreadDelegate(GstTimeout - timeout) - : threads::ThreadDelegate(), - timeout_(timeout) { -} +FromMicToFileRecorderThread::SleepThreadDelegate::SleepThreadDelegate( + GstTimeout timeout) + : threads::ThreadDelegate(), timeout_(timeout) {} void FromMicToFileRecorderThread::SleepThreadDelegate::threadMain() { LOG4CXX_TRACE(logger_, "Sleep for " << timeout_.duration << " seconds"); diff --git a/src/components/media_manager/src/audio/pipe_audio_streamer_adapter.cc b/src/components/media_manager/src/audio/pipe_audio_streamer_adapter.cc index 5c120397cf..2220c08b26 100644 --- a/src/components/media_manager/src/audio/pipe_audio_streamer_adapter.cc +++ b/src/components/media_manager/src/audio/pipe_audio_streamer_adapter.cc @@ -1,5 +1,5 @@ /* - * Copyright (c) 2014, Ford Motor Company + * Copyright (c) 2014-2015, Ford Motor Company * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -31,22 +31,14 @@ */ #include "media_manager/audio/pipe_audio_streamer_adapter.h" -#include "config_profile/profile.h" -#include "utils/logger.h" namespace media_manager { -CREATE_LOGGERPTR_GLOBAL(logger, "PipeAudioStreamerAdapter") +PipeAudioStreamerAdapter::PipeAudioStreamerAdapter( + const std::string& named_audio_pipe_path, + const std::string& app_storage_folder) + : PipeStreamerAdapter(named_audio_pipe_path, app_storage_folder) {} -PipeAudioStreamerAdapter::PipeAudioStreamerAdapter() { - LOG4CXX_AUTO_TRACE(logger); - named_pipe_path_ = profile::Profile::instance()->named_audio_pipe_path(); - - Init(); -} - -PipeAudioStreamerAdapter::~PipeAudioStreamerAdapter() { - LOG4CXX_INFO(logger, "PipeAudioStreamerAdapter::~PipeAudioStreamerAdapter"); -} +PipeAudioStreamerAdapter::~PipeAudioStreamerAdapter() {} } // namespace media_manager diff --git a/src/components/media_manager/src/audio/socket_audio_streamer_adapter.cc b/src/components/media_manager/src/audio/socket_audio_streamer_adapter.cc index 06dc877d89..429fab01b3 100644 --- a/src/components/media_manager/src/audio/socket_audio_streamer_adapter.cc +++ b/src/components/media_manager/src/audio/socket_audio_streamer_adapter.cc @@ -1,5 +1,5 @@ /* - * Copyright (c) 2014 Ford Motor Company + * Copyright (c) 2014-2015, Ford Motor Company * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -30,22 +30,24 @@ * POSSIBILITY OF SUCH DAMAGE. */ -#include "config_profile/profile.h" +#include <string> #include "media_manager/audio/socket_audio_streamer_adapter.h" -#include "utils/logger.h" + +namespace { +const std::string kHeader = + "HTTP/1.1 200 OK\r\n" + "Connection: Keep-Alive\r\n" + "Keep-Alive: timeout=15, max=300\r\n" + "Server: SDL\r\n" + "Content-Type: video/mp4\r\n\r\n"; +} namespace media_manager { -CREATE_LOGGERPTR_GLOBAL(logger, "SocketAudioStreamerAdapter") +SocketAudioStreamerAdapter::SocketAudioStreamerAdapter( + const std::string& server_address, uint16_t audio_streaming_port) + : SocketStreamerAdapter(server_address, audio_streaming_port, kHeader) {} -SocketAudioStreamerAdapter::SocketAudioStreamerAdapter() { - LOG4CXX_AUTO_TRACE(logger); - port_ = profile::Profile::instance()->audio_streaming_port(); - ip_ = profile::Profile::instance()->server_address(); +SocketAudioStreamerAdapter::~SocketAudioStreamerAdapter() {} - Init(); -} - -SocketAudioStreamerAdapter::~SocketAudioStreamerAdapter() { -} } // namespace media_manager diff --git a/src/components/media_manager/src/file_streamer_adapter.cc b/src/components/media_manager/src/file_streamer_adapter.cc new file mode 100644 index 0000000000..3418f7d963 --- /dev/null +++ b/src/components/media_manager/src/file_streamer_adapter.cc @@ -0,0 +1,102 @@ +/* + * Copyright (c) 2014, Ford Motor Company + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following + * disclaimer in the documentation and/or other materials provided with the + * distribution. + * + * Neither the name of the Ford Motor Company nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include "utils/logger.h" +#include "utils/file_system.h" +#include "media_manager/file_streamer_adapter.h" + +namespace media_manager { + +CREATE_LOGGERPTR_GLOBAL(logger_, "FileStreamerAdapter") + +FileStreamerAdapter::FileStreamerAdapter(const std::string& file_name, + const std::string& app_storage_folder) + : StreamerAdapter(new FileStreamer(this, file_name, app_storage_folder)) {} + +FileStreamerAdapter::~FileStreamerAdapter() {} + +FileStreamerAdapter::FileStreamer::FileStreamer( + FileStreamerAdapter* const adapter, + const std::string& file_name, + const std::string& app_storage_folder) + : Streamer(adapter) + , file_name_(file_name) + , app_storage_folder_(app_storage_folder) + , file_stream_(NULL) {} + +FileStreamerAdapter::FileStreamer::~FileStreamer() {} + +bool FileStreamerAdapter::FileStreamer::Connect() { + LOG4CXX_AUTO_TRACE(logger_); + if (!file_system::CreateDirectoryRecursively(app_storage_folder_)) { + LOG4CXX_ERROR(logger_, "Cannot create app folder"); + return false; + } + + file_stream_ = file_system::Open(file_name_); + if (!file_stream_) { + LOG4CXX_ERROR(logger_, "Cannot open file stream " << file_name_); + return false; + } + + LOG4CXX_INFO(logger_, "File " << file_name_ << " was successfuly opened"); + return true; +} + +void FileStreamerAdapter::FileStreamer::Disconnect() { + LOG4CXX_AUTO_TRACE(logger_); + if (file_stream_) { + file_system::Close(file_stream_); + delete file_stream_; + file_stream_ = NULL; + } + file_system::DeleteFile(file_name_); +} + +bool FileStreamerAdapter::FileStreamer::Send( + protocol_handler::RawMessagePtr msg) { + LOG4CXX_AUTO_TRACE(logger_); + if (!file_stream_) { + LOG4CXX_ERROR(logger_, "File stream not found " << file_name_); + return false; + } + + if (!file_system::Write(file_stream_, msg->data(), msg->data_size())) { + LOG4CXX_ERROR(logger_, "Failed writing data to file " << file_name_); + return false; + } + + LOG4CXX_INFO(logger_, "Streamer::sent " << msg->data_size()); + return true; +} + +} // namespace media_manager diff --git a/src/components/media_manager/src/media_adapter_impl.cc b/src/components/media_manager/src/media_adapter_impl.cc index 257801ffd8..cf15a7af97 100644 --- a/src/components/media_manager/src/media_adapter_impl.cc +++ b/src/components/media_manager/src/media_adapter_impl.cc @@ -35,11 +35,9 @@ namespace media_manager { -CREATE_LOGGERPTR_GLOBAL(logger_, "MediaAdapterImpl") +CREATE_LOGGERPTR_GLOBAL(logger_, "MediaManager") -MediaAdapterImpl::MediaAdapterImpl() - : current_application_(0) { -} +MediaAdapterImpl::MediaAdapterImpl() : current_application_(0) {} MediaAdapterImpl::~MediaAdapterImpl() { media_listeners_.clear(); diff --git a/src/components/media_manager/src/media_manager_impl.cc b/src/components/media_manager/src/media_manager_impl.cc index 61b2c5bb92..e531894b27 100644 --- a/src/components/media_manager/src/media_manager_impl.cc +++ b/src/components/media_manager/src/media_manager_impl.cc @@ -30,13 +30,12 @@ * POSSIBILITY OF SUCH DAMAGE. */ -#include "config_profile/profile.h" #include "media_manager/media_manager_impl.h" #include "media_manager/audio/from_mic_recorder_listener.h" #include "media_manager/streamer_listener.h" #include "application_manager/message_helper.h" #include "application_manager/application.h" -#include "application_manager/application_manager_impl.h" +#include "application_manager/application_manager.h" #include "application_manager/application_impl.h" #include "protocol_handler/protocol_handler.h" #include "utils/file_system.h" @@ -50,19 +49,22 @@ #include "media_manager/audio/socket_audio_streamer_adapter.h" #include "media_manager/video/pipe_video_streamer_adapter.h" #include "media_manager/audio/pipe_audio_streamer_adapter.h" -#include "media_manager/video/video_stream_to_file_adapter.h" +#include "media_manager/video/file_video_streamer_adapter.h" +#include "media_manager/audio/file_audio_streamer_adapter.h" +#include "media_manager/media_manager_settings.h" namespace media_manager { -using profile::Profile; -using timer::TimerThread; +CREATE_LOGGERPTR_GLOBAL(logger_, "MediaManager") -CREATE_LOGGERPTR_GLOBAL(logger_, "MediaManagerImpl") - -MediaManagerImpl::MediaManagerImpl() - : protocol_handler_(NULL) - , a2dp_player_(NULL) - , from_mic_recorder_(NULL) { +MediaManagerImpl::MediaManagerImpl( + application_manager::ApplicationManager& application_manager, + const MediaManagerSettings& settings) + : settings_(settings) + , protocol_handler_(NULL) + , a2dp_player_(NULL) + , from_mic_recorder_(NULL) + , application_manager_(application_manager) { Init(); } @@ -78,36 +80,69 @@ MediaManagerImpl::~MediaManagerImpl() { } } +#ifdef BUILD_TESTS +void MediaManagerImpl::set_mock_a2dp_player(MediaAdapter* media_adapter) { + a2dp_player_ = media_adapter; +} + +void MediaManagerImpl::set_mock_mic_listener(MediaListenerPtr media_listener) { + from_mic_listener_ = media_listener; +} + +#ifdef EXTENDED_MEDIA_MODE +void MediaManagerImpl::set_mock_mic_recorder(MediaAdapterImpl* media_adapter) { + from_mic_recorder_ = media_adapter; +} + +#endif // EXTENDED_MEDIA_MODE + +void MediaManagerImpl::set_mock_streamer(protocol_handler::ServiceType stype, + MediaAdapterImpl* mock_stream) { + streamer_[stype] = mock_stream; +} + +void MediaManagerImpl::set_mock_streamer_listener( + protocol_handler::ServiceType stype, MediaAdapterListener* mock_stream) { + streamer_listener_[stype] = mock_stream; +} + +#endif // BUILD_TESTS + void MediaManagerImpl::Init() { using namespace protocol_handler; LOG4CXX_INFO(logger_, "MediaManagerImpl::Init()"); #if defined(EXTENDED_MEDIA_MODE) LOG4CXX_INFO(logger_, "Called Init with default configuration."); - a2dp_player_ = new A2DPSourcePlayerAdapter(); + a2dp_player_ = + new A2DPSourcePlayerAdapter(protocol_handler_->get_session_observer()); from_mic_recorder_ = new FromMicRecorderAdapter(); #endif - if ("socket" == profile::Profile::instance()->video_server_type()) { - streamer_[ServiceType::kMobileNav] = new SocketVideoStreamerAdapter(); - } else if ("pipe" == profile::Profile::instance()->video_server_type()) { - streamer_[ServiceType::kMobileNav] = new PipeVideoStreamerAdapter(); - } else if ("file" == profile::Profile::instance()->video_server_type()) { - streamer_[ServiceType::kMobileNav] = new VideoStreamToFileAdapter( - profile::Profile::instance()->video_stream_file()); + if ("socket" == settings().video_server_type()) { + streamer_[ServiceType::kMobileNav] = new SocketVideoStreamerAdapter( + settings().server_address(), settings().video_streaming_port()); + } else if ("pipe" == settings().video_server_type()) { + streamer_[ServiceType::kMobileNav] = new PipeVideoStreamerAdapter( + settings().named_video_pipe_path(), settings().app_storage_folder()); + } else if ("file" == settings().video_server_type()) { + streamer_[ServiceType::kMobileNav] = new FileVideoStreamerAdapter( + settings().video_stream_file(), settings().app_storage_folder()); } - if ("socket" == profile::Profile::instance()->audio_server_type()) { - streamer_[ServiceType::kAudio] = new SocketAudioStreamerAdapter(); - } else if ("pipe" == profile::Profile::instance()->audio_server_type()) { - streamer_[ServiceType::kAudio] = new PipeAudioStreamerAdapter(); - } else if ("file" == profile::Profile::instance()->audio_server_type()) { - streamer_[ServiceType::kAudio] = new VideoStreamToFileAdapter( - profile::Profile::instance()->audio_stream_file()); + if ("socket" == settings().audio_server_type()) { + streamer_[ServiceType::kAudio] = new SocketAudioStreamerAdapter( + settings().server_address(), settings().audio_streaming_port()); + } else if ("pipe" == settings().audio_server_type()) { + streamer_[ServiceType::kAudio] = new PipeAudioStreamerAdapter( + settings().named_audio_pipe_path(), settings().app_storage_folder()); + } else if ("file" == settings().audio_server_type()) { + streamer_[ServiceType::kAudio] = new FileAudioStreamerAdapter( + settings().audio_stream_file(), settings().app_storage_folder()); } - streamer_listener_[ServiceType::kMobileNav] = new StreamerListener(); - streamer_listener_[ServiceType::kAudio] = new StreamerListener(); + streamer_listener_[ServiceType::kMobileNav] = new StreamerListener(*this); + streamer_listener_[ServiceType::kAudio] = new StreamerListener(*this); if (streamer_[ServiceType::kMobileNav]) { streamer_[ServiceType::kMobileNav]->AddListener( @@ -134,27 +169,25 @@ void MediaManagerImpl::StopA2DPSource(int32_t application_key) { } } -void MediaManagerImpl::StartMicrophoneRecording( - int32_t application_key, - const std::string& output_file, - int32_t duration) { - LOG4CXX_INFO(logger_, "MediaManagerImpl::StartMicrophoneRecording to " - << output_file); +void MediaManagerImpl::StartMicrophoneRecording(int32_t application_key, + const std::string& output_file, + int32_t duration) { + LOG4CXX_INFO(logger_, + "MediaManagerImpl::StartMicrophoneRecording to " << output_file); application_manager::ApplicationSharedPtr app = - application_manager::ApplicationManagerImpl::instance()-> - application(application_key); - std::string file_path = - profile::Profile::instance()->app_storage_folder(); + application_manager_.application(application_key); + std::string file_path = settings().app_storage_folder(); file_path += "/"; file_path += output_file; - from_mic_listener_ = new FromMicRecorderListener(file_path); -#if defined(EXTENDED_MEDIA_MODE) + from_mic_listener_ = + new FromMicRecorderListener(file_path, application_manager_); +#ifdef EXTENDED_MEDIA_MODE if (from_mic_recorder_) { from_mic_recorder_->AddListener(from_mic_listener_); (static_cast<FromMicRecorderAdapter*>(from_mic_recorder_)) - ->set_output_file(file_path); + ->set_output_file(file_path); (static_cast<FromMicRecorderAdapter*>(from_mic_recorder_)) - ->set_duration(duration); + ->set_duration(duration); from_mic_recorder_->StartActivity(application_key); } #else @@ -162,25 +195,23 @@ void MediaManagerImpl::StartMicrophoneRecording( LOG4CXX_INFO(logger_, "File " << output_file << " exists, removing"); if (file_system::DeleteFile(file_path)) { LOG4CXX_INFO(logger_, "File " << output_file << " removed"); - } - else { + } else { LOG4CXX_WARN(logger_, "Could not remove file " << output_file); } } - const std::string record_file_source = - profile::Profile::instance()->app_resourse_folder() + "/" + - profile::Profile::instance()->recording_file_source(); + const std::string record_file_source = settings().app_resource_folder() + + "/" + + settings().recording_file_source(); std::vector<uint8_t> buf; if (file_system::ReadBinaryFile(record_file_source, buf)) { if (file_system::Write(file_path, buf)) { LOG4CXX_INFO(logger_, - "File " << record_file_source << " copied to " << output_file); - } - else { + "File " << record_file_source << " copied to " + << output_file); + } else { LOG4CXX_WARN(logger_, "Could not write to file " << output_file); } - } - else { + } else { LOG4CXX_WARN(logger_, "Could not read file " << record_file_source); } #endif @@ -223,7 +254,7 @@ void MediaManagerImpl::StopStreaming( } void MediaManagerImpl::SetProtocolHandler( - protocol_handler::ProtocolHandler* protocol_handler) { + protocol_handler::ProtocolHandler* protocol_handler) { protocol_handler_ = protocol_handler; } @@ -238,21 +269,19 @@ void MediaManagerImpl::OnMessageReceived( const ServiceType service_type = message->service_type(); if (Compare<ServiceType, NEQ, ALL>( - service_type, ServiceType::kMobileNav, ServiceType::kAudio)) { + service_type, ServiceType::kMobileNav, ServiceType::kAudio)) { LOG4CXX_DEBUG(logger_, "Unsupported service type in MediaManager"); return; } - ApplicationManagerImpl* app_mgr = ApplicationManagerImpl::instance(); - DCHECK_OR_RETURN_VOID(app_mgr); - - if (!app_mgr->CanAppStream(streaming_app_id, service_type)) { - app_mgr->ForbidStreaming(streaming_app_id); - LOG4CXX_ERROR(logger_, "The application trying to stream when it should not."); + if (!application_manager_.CanAppStream(streaming_app_id, service_type)) { + application_manager_.ForbidStreaming(streaming_app_id); + LOG4CXX_ERROR(logger_, + "The application trying to stream when it should not."); return; } - ApplicationSharedPtr app = app_mgr->application(streaming_app_id); + ApplicationSharedPtr app = application_manager_.application(streaming_app_id); if (app) { app->WakeUpStreaming(service_type); streamer_[service_type]->SendData(streaming_app_id, message); @@ -260,15 +289,17 @@ void MediaManagerImpl::OnMessageReceived( } void MediaManagerImpl::OnMobileMessageSent( - const ::protocol_handler::RawMessagePtr message) { -} + const ::protocol_handler::RawMessagePtr message) {} void MediaManagerImpl::FramesProcessed(int32_t application_key, int32_t frame_number) { if (protocol_handler_) { - protocol_handler_->SendFramesNumber(application_key, - frame_number); + protocol_handler_->SendFramesNumber(application_key, frame_number); } } +const MediaManagerSettings& MediaManagerImpl::settings() const { + return settings_; +} + } // namespace media_manager diff --git a/src/components/media_manager/src/pipe_streamer_adapter.cc b/src/components/media_manager/src/pipe_streamer_adapter.cc index 5990428c68..8bf14a546e 100644 --- a/src/components/media_manager/src/pipe_streamer_adapter.cc +++ b/src/components/media_manager/src/pipe_streamer_adapter.cc @@ -36,195 +36,89 @@ #include <unistd.h> #include "utils/logger.h" #include "utils/file_system.h" -#include "config_profile/profile.h" #include "media_manager/pipe_streamer_adapter.h" namespace media_manager { CREATE_LOGGERPTR_GLOBAL(logger_, "PipeStreamerAdapter") -PipeStreamerAdapter::PipeStreamerAdapter() - : is_ready_(false), - thread_(threads::CreateThread("PipeStreamer", new Streamer(this))), - messages_() { - LOG4CXX_AUTO_TRACE(logger_); -} - -PipeStreamerAdapter::~PipeStreamerAdapter() { - LOG4CXX_AUTO_TRACE(logger_); - - if ((0 != current_application_ ) && (is_ready_)) { - StopActivity(current_application_); - } - - thread_->join(); - delete thread_->delegate(); - threads::DeleteThread(thread_); -} - -void PipeStreamerAdapter::SendData( - int32_t application_key, - const ::protocol_handler::RawMessagePtr message) { - LOG4CXX_AUTO_TRACE(logger_); - - if (application_key != current_application_) { - LOG4CXX_WARN(logger_, "Wrong application " << application_key); +PipeStreamerAdapter::PipeStreamerAdapter(const std::string& named_pipe_path, + const std::string& app_storage_folder) + : StreamerAdapter( + new PipeStreamer(this, named_pipe_path, app_storage_folder)) {} + +PipeStreamerAdapter::~PipeStreamerAdapter() {} + +PipeStreamerAdapter::PipeStreamer::PipeStreamer( + PipeStreamerAdapter* const adapter, + const std::string& named_pipe_path, + const std::string& app_storage_folder) + : Streamer(adapter) + , named_pipe_path_(named_pipe_path) + , app_storage_folder_(app_storage_folder) + , pipe_fd_(0) { + if (!file_system::CreateDirectoryRecursively(app_storage_folder_)) { + LOG4CXX_ERROR(logger_, + "Cannot create app storage folder " << app_storage_folder_); return; } - - if (is_ready_) { - messages_.push(message); + if ((mkfifo(named_pipe_path_.c_str(), S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) < + 0) && + (errno != EEXIST)) { + LOG4CXX_ERROR(logger_, "Cannot create pipe " << named_pipe_path_); + } else { + LOG4CXX_INFO(logger_, + "Pipe " << named_pipe_path_ << " was successfully created"); } } - -void PipeStreamerAdapter::StartActivity(int32_t application_key) { - LOG4CXX_AUTO_TRACE(logger_); - - if (application_key == current_application_) { - LOG4CXX_WARN(logger_, "Already started activity for " << application_key); - return; +PipeStreamerAdapter::PipeStreamer::~PipeStreamer() { + if (0 == unlink(named_pipe_path_.c_str())) { + LOG4CXX_INFO(logger_, "Pipe " << named_pipe_path_ << " was removed"); + } else { + LOG4CXX_ERROR(logger_, "Error removing pipe " << named_pipe_path_); } - - current_application_ = application_key; - is_ready_ = true; - - for (std::set<MediaListenerPtr>::iterator it = media_listeners_.begin(); - media_listeners_.end() != it; - ++it) { - (*it)->OnActivityStarted(application_key); - } - - LOG4CXX_DEBUG(logger_, "Pipe was opened for writing " << named_pipe_path_); } -void PipeStreamerAdapter::StopActivity(int32_t application_key) { +bool PipeStreamerAdapter::PipeStreamer::Connect() { LOG4CXX_AUTO_TRACE(logger_); - if (application_key != current_application_) { - LOG4CXX_WARN(logger_, "Not performing activity for " << application_key); - return; - } - - is_ready_ = false; - current_application_ = 0; - - messages_.Reset(); - - for (std::set<MediaListenerPtr>::iterator it = media_listeners_.begin(); - media_listeners_.end() != it; - ++it) { - (*it)->OnActivityEnded(application_key); - } -} - -bool PipeStreamerAdapter::is_app_performing_activity( int32_t application_key) { - return (application_key == current_application_); -} - -void PipeStreamerAdapter::Init() { - LOG4CXX_AUTO_TRACE(logger_); - if (thread_->is_running()) { - thread_->stop(); - thread_->join(); + pipe_fd_ = open(named_pipe_path_.c_str(), O_RDWR, 0); + if (-1 == pipe_fd_) { + LOG4CXX_ERROR(logger_, "Cannot open pipe for writing " << named_pipe_path_); + return false; } - LOG4CXX_DEBUG(logger_, "Start sending thread"); - const size_t kStackSize = 16384; - thread_->start(threads::ThreadOptions(kStackSize)); -} -PipeStreamerAdapter::Streamer::Streamer( - PipeStreamerAdapter* server) - : server_(server), - pipe_fd_(0), - stop_flag_(false) { + LOG4CXX_INFO(logger_, + "Pipe " << named_pipe_path_ + << " was successfuly opened for writing"); + return true; } -PipeStreamerAdapter::Streamer::~Streamer() { - server_ = NULL; -} - -void PipeStreamerAdapter::Streamer::threadMain() { +void PipeStreamerAdapter::PipeStreamer::Disconnect() { LOG4CXX_AUTO_TRACE(logger_); - - open(); - - while (!stop_flag_) { - while (!server_->messages_.empty()) { - ::protocol_handler::RawMessagePtr msg = server_->messages_.pop(); - if (!msg) { - LOG4CXX_ERROR(logger_, "Null pointer message"); - continue; - } - - ssize_t ret = write(pipe_fd_, msg.get()->data(), - msg.get()->data_size()); - - if (ret == -1) { - LOG4CXX_ERROR(logger_, "Failed writing data to pipe " - << server_->named_pipe_path_); - - std::set<MediaListenerPtr>::iterator it = - server_->media_listeners_.begin(); - for (;server_->media_listeners_.end() != it; ++it) { - (*it)->OnErrorReceived(server_->current_application_, -1); - } - } else if (static_cast<uint32_t>(ret) != msg.get()->data_size()) { - LOG4CXX_WARN(logger_, "Couldn't write all the data to pipe " - << server_->named_pipe_path_); - } - - static int32_t messsages_for_session = 0; - ++messsages_for_session; - - LOG4CXX_DEBUG(logger_, "Handling map streaming message. This is " - << messsages_for_session << " the message for " - << server_->current_application_); - std::set<MediaListenerPtr>::iterator it = - server_->media_listeners_.begin(); - for (; server_->media_listeners_.end() != it; ++it) { - (*it)->OnDataReceived(server_->current_application_, - messsages_for_session); - } - } - server_->messages_.wait(); + if (0 == close(pipe_fd_)) { + LOG4CXX_INFO(logger_, "Pipe " << named_pipe_path_ << " was closed"); + } else { + LOG4CXX_ERROR(logger_, "Error closing pipe " << named_pipe_path_); } - close(); } -void PipeStreamerAdapter::Streamer::exitThreadMain() { +bool PipeStreamerAdapter::PipeStreamer::Send( + protocol_handler::RawMessagePtr msg) { LOG4CXX_AUTO_TRACE(logger_); - stop_flag_ = true; - server_->messages_.Shutdown(); -} - -void PipeStreamerAdapter::Streamer::open() { - LOG4CXX_AUTO_TRACE(logger_); - - DCHECK(file_system::CreateDirectoryRecursively( - profile::Profile::instance()->app_storage_folder())); - - if ((mkfifo(server_->named_pipe_path_.c_str(), - S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) < 0) - && (errno != EEXIST)) { - LOG4CXX_ERROR(logger_, "Cannot create pipe " << server_->named_pipe_path_); - return; + ssize_t ret = write(pipe_fd_, msg->data(), msg->data_size()); + if (-1 == ret) { + LOG4CXX_ERROR(logger_, "Failed writing data to pipe " << named_pipe_path_); + return false; } - pipe_fd_ = ::open(server_->named_pipe_path_.c_str(), O_RDWR, 0); - if (-1 == pipe_fd_) { - LOG4CXX_ERROR(logger_, "Cannot open pipe for writing " - << server_->named_pipe_path_); - return; + if (static_cast<uint32_t>(ret) != msg->data_size()) { + LOG4CXX_WARN(logger_, + "Couldn't write all the data to pipe " << named_pipe_path_); } - LOG4CXX_DEBUG(logger_, "Pipe " << server_->named_pipe_path_ - << " was successfully created"); -} - -void PipeStreamerAdapter::Streamer::close() { - LOG4CXX_AUTO_TRACE(logger_); - ::close(pipe_fd_); - unlink(server_->named_pipe_path_.c_str()); + LOG4CXX_INFO(logger_, "Streamer::sent " << msg->data_size()); + return true; } } // namespace media_manager diff --git a/src/components/media_manager/src/socket_streamer_adapter.cc b/src/components/media_manager/src/socket_streamer_adapter.cc index 0027589420..2bb0fe10ec 100644 --- a/src/components/media_manager/src/socket_streamer_adapter.cc +++ b/src/components/media_manager/src/socket_streamer_adapter.cc @@ -33,283 +33,116 @@ #include <sys/socket.h> #include <sys/types.h> #include <sys/select.h> -#include <sys/time.h> #include <netinet/in.h> #include <arpa/inet.h> #include <netdb.h> #include <unistd.h> -#include <string.h> -#include <errno.h> -#include "config_profile/profile.h" -#include "media_manager/video/socket_video_streamer_adapter.h" #include "utils/logger.h" +#include "media_manager/socket_streamer_adapter.h" namespace media_manager { CREATE_LOGGERPTR_GLOBAL(logger, "SocketStreamerAdapter") -SocketStreamerAdapter::SocketStreamerAdapter() - : port_(0), - socket_fd_(0), - is_ready_(false), - streamer_(new Streamer(this)), - thread_(threads::CreateThread("SocketStreamer", streamer_)), - messages_() { -} - -SocketStreamerAdapter::~SocketStreamerAdapter() { +SocketStreamerAdapter::SocketStreamerAdapter(const std::string& ip, + const uint16_t port, + const std::string& header) + : StreamerAdapter(new SocketStreamer(this, ip, port, header)) {} + +SocketStreamerAdapter::~SocketStreamerAdapter() {} + +SocketStreamerAdapter::SocketStreamer::SocketStreamer( + SocketStreamerAdapter* const adapter, + const std::string& ip, + const uint16_t port, + const std::string& header) + : Streamer(adapter) + , ip_(ip) + , port_(port) + , header_(header) + , socket_fd_(0) + , send_socket_fd_(0) + , is_first_frame_(true) {} + +SocketStreamerAdapter::SocketStreamer::~SocketStreamer() {} + +bool SocketStreamerAdapter::SocketStreamer::Connect() { LOG4CXX_AUTO_TRACE(logger); - thread_->join(); - delete streamer_; - threads::DeleteThread(thread_); -} - -void SocketStreamerAdapter::StartActivity(int32_t application_key) { - LOG4CXX_TRACE(logger, "enter " << application_key); - - if (application_key == current_application_) { - LOG4CXX_INFO(logger, "Already running for app " << application_key); - } else { - is_ready_ = true; - current_application_ = application_key; - - messages_.Reset(); - - for (std::set<MediaListenerPtr>::iterator it = media_listeners_.begin(); - media_listeners_.end() != it; - ++it) { - (*it)->OnActivityStarted(application_key); - } - } - LOG4CXX_TRACE(logger, "exit"); -} - -void SocketStreamerAdapter::StopActivity(int32_t application_key) { - LOG4CXX_TRACE(logger, "enter " << application_key); - - if (application_key != current_application_) { - LOG4CXX_WARN(logger, "Streaming is not active for " << application_key); - } else { - is_ready_ = false; - current_application_ = 0; - - if (streamer_) { - streamer_->stop(); - messages_.Shutdown(); - } - - for (std::set<MediaListenerPtr>::iterator it = media_listeners_.begin(); - media_listeners_.end() != it; - ++it) { - (*it)->OnActivityEnded(application_key); - } - } - LOG4CXX_TRACE(logger, "exit"); -} - -bool SocketStreamerAdapter::is_app_performing_activity( - int32_t application_key) { - return (application_key == current_application_); -} - -void SocketStreamerAdapter::Init() { - LOG4CXX_DEBUG(logger, "Start sending thread"); - const size_t kStackSize = 16384; - thread_->start(threads::ThreadOptions(kStackSize)); -} - -void SocketStreamerAdapter::SendData( - int32_t application_key, - const ::protocol_handler::RawMessagePtr message) { - LOG4CXX_INFO(logger, "SendData(application_key = " << application_key << ")"); - - - if (application_key != current_application_) { - LOG4CXX_WARN(logger, "Currently working with other app " - << current_application_); - return; - } - - if (is_ready_) { - messages_.push(message); - } -} - -SocketStreamerAdapter::Streamer::Streamer( - SocketStreamerAdapter* const server) - : server_(server), - new_socket_fd_(0), - is_first_loop_(true), - is_client_connected_(false), - stop_flag_(false) { -} - -SocketStreamerAdapter::Streamer::~Streamer() { - stop(); -} - -void SocketStreamerAdapter::Streamer::threadMain() { - LOG4CXX_TRACE(logger,"enter " << this); - sync_primitives::AutoLock auto_lock(thread_lock); - start(); - - while (!stop_flag_) { - new_socket_fd_ = accept(server_->socket_fd_, NULL, NULL); - LOG4CXX_INFO(logger, "Client connectd " << new_socket_fd_); - if (0 > new_socket_fd_) { - LOG4CXX_ERROR(logger, "Socket is closed " << strerror(errno)); - sleep(1); - continue; - } - - is_client_connected_ = true; - is_first_loop_ = true; - while (is_client_connected_) { - while (!server_->messages_.empty()) { - ::protocol_handler::RawMessagePtr msg = server_->messages_.pop(); - if (!msg) { - LOG4CXX_ERROR(logger, "Null pointer message"); - continue; - } - - is_client_connected_ = send(msg); - static int32_t messages_for_session = 0; - ++messages_for_session; - - LOG4CXX_INFO(logger, "Handling map streaming message. This is " - << messages_for_session << " the message for " - << server_->current_application_); - std::set<MediaListenerPtr>::iterator it = server_->media_listeners_ - .begin(); - for (; server_->media_listeners_.end() != it; ++it) { - (*it)->OnDataReceived(server_->current_application_, - messages_for_session); - } - } - - if (!is_ready()) { - LOG4CXX_INFO(logger, "Client disconnected."); - stop(); - break; - } - server_->messages_.wait(); - } - } - LOG4CXX_TRACE(logger,"exit " << this); -} - -void SocketStreamerAdapter::Streamer::exitThreadMain() { - LOG4CXX_TRACE(logger,"enter " << this); - stop_flag_ = true; - stop(); - server_->messages_.Shutdown(); - if (server_->socket_fd_ != -1) { - shutdown(server_->socket_fd_, SHUT_RDWR); - close(server_->socket_fd_); - } - LOG4CXX_TRACE(logger,"exit " << this); -} - -void SocketStreamerAdapter::Streamer::start() { - server_->socket_fd_ = socket(AF_INET, SOCK_STREAM, 0); - - if (0 >= server_->socket_fd_) { - LOG4CXX_ERROR_EXT(logger, "Server open error"); - return; + socket_fd_ = socket(AF_INET, SOCK_STREAM, 0); + if (0 >= socket_fd_) { + LOG4CXX_ERROR(logger, "Unable to create socket"); + return false; } int32_t optval = 1; - if (-1 == setsockopt(server_->socket_fd_, SOL_SOCKET, SO_REUSEADDR, - &optval, sizeof optval)) { - LOG4CXX_ERROR_EXT(logger, "Unable to set sockopt"); - return; + if (-1 == setsockopt( + socket_fd_, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof optval)) { + LOG4CXX_ERROR(logger, "Unable to set sockopt"); + return false; } - struct sockaddr_in serv_addr_ = { 0 }; - serv_addr_.sin_addr.s_addr = inet_addr(server_->ip_.c_str()); + struct sockaddr_in serv_addr_ = {0}; + serv_addr_.sin_addr.s_addr = inet_addr(ip_.c_str()); serv_addr_.sin_family = AF_INET; - serv_addr_.sin_port = htons(server_->port_); - - if (-1 == bind(server_->socket_fd_, + serv_addr_.sin_port = htons(port_); + if (-1 == bind(socket_fd_, reinterpret_cast<struct sockaddr*>(&serv_addr_), sizeof(serv_addr_))) { - LOG4CXX_ERROR_EXT(logger, "Unable to bind"); - return; + LOG4CXX_ERROR(logger, "Unable to bind"); + return false; } - LOG4CXX_INFO(logger, "SocketStreamerAdapter::listen for connections"); - if (-1 == listen(server_->socket_fd_, 5)) { - LOG4CXX_ERROR_EXT(logger, "Unable to listen"); - return; + if (-1 == listen(socket_fd_, 5)) { + LOG4CXX_ERROR(logger, "Unable to listen"); + return false; } -} -void SocketStreamerAdapter::Streamer::stop() { - LOG4CXX_TRACE(logger,"enter " << this); - if (0 == new_socket_fd_) { - LOG4CXX_ERROR(logger, "Client Socket does not exist: "); - } else if (-1 == shutdown(new_socket_fd_, SHUT_RDWR)) { - LOG4CXX_ERROR(logger, "Unable to shutdown socket " << strerror(errno)); - } else if (-1 == ::close(new_socket_fd_)) { - LOG4CXX_ERROR(logger, "Unable to close socket " << strerror(errno)); + send_socket_fd_ = accept(socket_fd_, NULL, NULL); + if (0 >= send_socket_fd_) { + LOG4CXX_ERROR(logger, "Unable to accept"); + return false; } - new_socket_fd_ = 0; - is_client_connected_ = false; - LOG4CXX_TRACE(logger,"exit" << this); + is_first_frame_ = true; + LOG4CXX_INFO(logger, "Client connected: " << send_socket_fd_); + return true; } -bool SocketStreamerAdapter::Streamer::is_ready() const { - bool result = true; - fd_set fds; - FD_ZERO(&fds); - FD_SET(new_socket_fd_, &fds); - struct timeval tv; - tv.tv_sec = 5; // set a 5 second timeout - tv.tv_usec = 0; - - int32_t retval = 0; - retval = select(new_socket_fd_ + 1, 0, &fds, 0, &tv); - - if (-1 == retval) { - LOG4CXX_ERROR_EXT(logger, "An error occurred"); - result = false; - } else if (0 == retval) { - LOG4CXX_ERROR_EXT(logger, "The timeout expired"); - result = false; +void SocketStreamerAdapter::SocketStreamer::Disconnect() { + LOG4CXX_AUTO_TRACE(logger); + if (0 < send_socket_fd_) { + close(send_socket_fd_); } - return result; -} - -bool SocketStreamerAdapter::Streamer::send( - const ::protocol_handler::RawMessagePtr msg) { - if (!is_ready()) { - LOG4CXX_ERROR_EXT(logger, " Socket is not ready"); - return false; + if (0 < socket_fd_) { + close(socket_fd_); } +} - if (is_first_loop_) { - is_first_loop_ = false; - char hdr[] = {"HTTP/1.1 200 OK\r\n " - "Connection: Keep-Alive\r\n" - "Keep-Alive: timeout=15, max=300\r\n" - "Server: SDL\r\n" - "Content-Type: video/mp4\r\n\r\n" - }; - - if (-1 == ::send(new_socket_fd_, hdr, strlen(hdr), MSG_NOSIGNAL)) { - LOG4CXX_ERROR_EXT(logger, " Unable to send"); +bool SocketStreamerAdapter::SocketStreamer::Send( + protocol_handler::RawMessagePtr msg) { + LOG4CXX_AUTO_TRACE(logger); + ssize_t ret; + if (is_first_frame_) { + ret = send(send_socket_fd_, header_.c_str(), header_.size(), MSG_NOSIGNAL); + if (static_cast<uint32_t>(ret) != header_.size()) { + LOG4CXX_ERROR(logger, "Unable to send data to socket"); return false; } + is_first_frame_ = false; } - if (-1 == ::send(new_socket_fd_, (*msg).data(), - (*msg).data_size(), MSG_NOSIGNAL)) { - LOG4CXX_ERROR_EXT(logger, " Unable to send"); + ret = send(send_socket_fd_, msg->data(), msg->data_size(), MSG_NOSIGNAL); + if (-1 == ret) { + LOG4CXX_ERROR(logger, "Unable to send data to socket"); return false; } - LOG4CXX_INFO(logger, "Streamer::sent " << (*msg).data_size()); + if (static_cast<uint32_t>(ret) != msg->data_size()) { + LOG4CXX_WARN(logger, + "Couldn't send all the data to socket " << send_socket_fd_); + } + + LOG4CXX_INFO(logger, "Streamer::sent " << msg->data_size()); return true; } diff --git a/src/components/media_manager/src/streamer_adapter.cc b/src/components/media_manager/src/streamer_adapter.cc new file mode 100644 index 0000000000..20c067da1c --- /dev/null +++ b/src/components/media_manager/src/streamer_adapter.cc @@ -0,0 +1,172 @@ +/* + * Copyright (c) 2015, Ford Motor Company + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following + * disclaimer in the documentation and/or other materials provided with the + * distribution. + * + * Neither the name of the Ford Motor Company nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include "media_manager/streamer_adapter.h" +#include "utils/logger.h" + +namespace media_manager { + +CREATE_LOGGERPTR_GLOBAL(logger, "StreamerAdapter") + +StreamerAdapter::StreamerAdapter(Streamer* const streamer) + : current_application_(0), messages_(), streamer_(streamer), thread_(NULL) { + DCHECK(streamer_); + thread_ = threads::CreateThread("StreamerAdapter", streamer_); +} + +StreamerAdapter::~StreamerAdapter() { + delete streamer_; + thread_->join(); + threads::DeleteThread(thread_); +} + +void StreamerAdapter::StartActivity(int32_t application_key) { + LOG4CXX_AUTO_TRACE(logger); + if (is_app_performing_activity(application_key)) { + LOG4CXX_WARN(logger, + "Activity for application: " << application_key + << " has been already started"); + return; + } + messages_.Reset(); + + DCHECK(thread_); + const size_t kStackSize = 16384; + thread_->start(threads::ThreadOptions(kStackSize)); + + for (std::set<MediaListenerPtr>::iterator it = media_listeners_.begin(); + media_listeners_.end() != it; + ++it) { + (*it)->OnActivityStarted(application_key); + } + current_application_ = application_key; +} + +void StreamerAdapter::StopActivity(int32_t application_key) { + LOG4CXX_AUTO_TRACE(logger); + if (!is_app_performing_activity(application_key)) { + LOG4CXX_WARN(logger, + "Activity for application: " << application_key + << " has not been started"); + return; + } + + DCHECK(thread_); + thread_->stop(); + + for (std::set<MediaListenerPtr>::iterator it = media_listeners_.begin(); + media_listeners_.end() != it; + ++it) { + (*it)->OnActivityEnded(application_key); + } + current_application_ = 0; +} + +void StreamerAdapter::SendData(int32_t application_key, + const ::protocol_handler::RawMessagePtr msg) { + LOG4CXX_AUTO_TRACE(logger); + if (!is_app_performing_activity(application_key)) { + LOG4CXX_ERROR(logger, + "Activity for application: " << application_key + << " has not been started"); + return; + } + messages_.push(msg); +} + +bool StreamerAdapter::is_app_performing_activity( + int32_t application_key) const { + return application_key == current_application_; +} + +StreamerAdapter::Streamer::Streamer(StreamerAdapter* const adapter) + : stop_flag_(false), adapter_(adapter) { + DCHECK(adapter_); +} + +StreamerAdapter::Streamer::~Streamer() {} + +void StreamerAdapter::Streamer::threadMain() { + LOG4CXX_AUTO_TRACE(logger); + if (!adapter_) { + LOG4CXX_ERROR(logger, "Null pointer to adapter"); + return; + } + if (!Connect()) { + LOG4CXX_ERROR(logger, "Unable to connect"); + return; + } + stop_flag_ = false; + while (!stop_flag_) { + adapter_->messages_.wait(); + while (!adapter_->messages_.empty()) { + protocol_handler::RawMessagePtr msg; + if (!adapter_->messages_.pop(msg)) { + LOG4CXX_ERROR(logger, "Empty message queue"); + continue; + } + if (!msg) { + LOG4CXX_ERROR(logger, "Null pointer message"); + continue; + } + if (!Send(msg)) { + LOG4CXX_ERROR(logger, "Unable to send. Disconnecting"); + Disconnect(); + return; + } + static int32_t messages_for_session = 0; + ++messages_for_session; + + LOG4CXX_DEBUG(logger, + "Handling map streaming message. This is " + << messages_for_session << " message for " + << adapter_->current_application_); + std::set<MediaListenerPtr>::iterator it = + adapter_->media_listeners_.begin(); + for (; adapter_->media_listeners_.end() != it; ++it) { + (*it)->OnDataReceived(adapter_->current_application_, + messages_for_session); + } + } + } + Disconnect(); +} + +void StreamerAdapter::Streamer::exitThreadMain() { + LOG4CXX_AUTO_TRACE(logger); + stop_flag_ = true; + if (adapter_) { + adapter_->messages_.Shutdown(); + } +} + +} // namespace media_manager diff --git a/src/components/media_manager/src/streamer_listener.cc b/src/components/media_manager/src/streamer_listener.cc index 3181ba8e09..2d6c3bc984 100644 --- a/src/components/media_manager/src/streamer_listener.cc +++ b/src/components/media_manager/src/streamer_listener.cc @@ -36,33 +36,30 @@ namespace media_manager { -CREATE_LOGGERPTR_GLOBAL(logger_, "StreamerListener") +CREATE_LOGGERPTR_GLOBAL(logger_, "MediaManager") -StreamerListener::StreamerListener() - : current_application_(0) { -} +StreamerListener::StreamerListener(MediaManager& media_manager) + : current_application_(0), media_manager_(media_manager) {} StreamerListener::~StreamerListener() { OnActivityEnded(current_application_); } -void StreamerListener::OnDataReceived( - int32_t application_key, - const DataForListener& data) { - MediaManagerImpl::instance()->FramesProcessed(application_key, data); +void StreamerListener::OnDataReceived(int32_t application_key, + const DataForListener& data) { + media_manager_.FramesProcessed(application_key, data); } -void StreamerListener::OnErrorReceived( - int32_t application_key, - const DataForListener& data) { - LOG4CXX_ERROR(logger_, "StreamerListener::OnErrorReceived"); +void StreamerListener::OnErrorReceived(int32_t application_key, + const DataForListener& data) { + LOG4CXX_AUTO_TRACE(logger_); } void StreamerListener::OnActivityStarted(int32_t application_key) { LOG4CXX_AUTO_TRACE(logger_); if (current_application_ == application_key) { - LOG4CXX_WARN(logger_, "Already performing activity for " - << application_key); + LOG4CXX_WARN(logger_, + "Already performing activity for " << application_key); return; } current_application_ = application_key; @@ -71,8 +68,8 @@ void StreamerListener::OnActivityStarted(int32_t application_key) { void StreamerListener::OnActivityEnded(int32_t application_key) { LOG4CXX_AUTO_TRACE(logger_); if (current_application_ != application_key) { - LOG4CXX_WARN(logger_, "Already not performing activity for " - << application_key); + LOG4CXX_WARN(logger_, + "Already not performing activity for " << application_key); return; } current_application_ = 0; diff --git a/src/components/media_manager/src/video/file_video_streamer_adapter.cc b/src/components/media_manager/src/video/file_video_streamer_adapter.cc new file mode 100644 index 0000000000..f9e4d58a0e --- /dev/null +++ b/src/components/media_manager/src/video/file_video_streamer_adapter.cc @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2014-2015, Ford Motor Company + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following + * disclaimer in the documentation and/or other materials provided with the + * distribution. + * + * Neither the name of the Ford Motor Company nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include "media_manager/video/file_video_streamer_adapter.h" + +namespace media_manager { + +FileVideoStreamerAdapter::FileVideoStreamerAdapter( + const std::string& video_stream_file, const std::string& app_storage_folder) + : FileStreamerAdapter(video_stream_file, app_storage_folder) {} + +FileVideoStreamerAdapter::~FileVideoStreamerAdapter() {} + +} // namespace media_manager diff --git a/src/components/media_manager/src/video/pipe_video_streamer_adapter.cc b/src/components/media_manager/src/video/pipe_video_streamer_adapter.cc index 4812e19c9f..cf1a6fb347 100644 --- a/src/components/media_manager/src/video/pipe_video_streamer_adapter.cc +++ b/src/components/media_manager/src/video/pipe_video_streamer_adapter.cc @@ -1,5 +1,5 @@ /* - * Copyright (c) 2014, Ford Motor Company + * Copyright (c) 2014-2015, Ford Motor Company * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -31,21 +31,14 @@ */ #include "media_manager/video/pipe_video_streamer_adapter.h" -#include "config_profile/profile.h" namespace media_manager { -CREATE_LOGGERPTR_GLOBAL(logger, "PipeVideoStreamerAdapter") +PipeVideoStreamerAdapter::PipeVideoStreamerAdapter( + const std::string& named_video_pipe_path, + const std::string& app_storage_folder) + : PipeStreamerAdapter(named_video_pipe_path, app_storage_folder) {} -PipeVideoStreamerAdapter::PipeVideoStreamerAdapter() { - LOG4CXX_AUTO_TRACE(logger); - named_pipe_path_ = profile::Profile::instance()->named_video_pipe_path(); - - Init(); -} - -PipeVideoStreamerAdapter::~PipeVideoStreamerAdapter() { - LOG4CXX_INFO(logger, "PipeVideoStreamerAdapter::~PipeVideoStreamerAdapter"); -} +PipeVideoStreamerAdapter::~PipeVideoStreamerAdapter() {} } // namespace media_manager diff --git a/src/components/media_manager/src/video/socket_video_streamer_adapter.cc b/src/components/media_manager/src/video/socket_video_streamer_adapter.cc index 3a83aabd10..12ae93da69 100644 --- a/src/components/media_manager/src/video/socket_video_streamer_adapter.cc +++ b/src/components/media_manager/src/video/socket_video_streamer_adapter.cc @@ -1,5 +1,5 @@ /* - * Copyright (c) 2014, Ford Motor Company + * Copyright (c) 2014-2015, Ford Motor Company * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -30,23 +30,23 @@ * POSSIBILITY OF SUCH DAMAGE. */ -#include "config_profile/profile.h" #include "media_manager/video/socket_video_streamer_adapter.h" -#include "utils/logger.h" -namespace media_manager { - -CREATE_LOGGERPTR_GLOBAL(logger, "SocketVideoStreamerAdapter") +namespace { +const std::string kHeader = + "HTTP/1.1 200 OK\r\n" + "Connection: Keep-Alive\r\n" + "Keep-Alive: timeout=15, max=300\r\n" + "Server: SDL\r\n" + "Content-Type: video/mp4\r\n\r\n"; +} -SocketVideoStreamerAdapter::SocketVideoStreamerAdapter() { - LOG4CXX_AUTO_TRACE(logger); - port_ = profile::Profile::instance()->video_streaming_port(); - ip_ = profile::Profile::instance()->server_address(); +namespace media_manager { - Init(); -} +SocketVideoStreamerAdapter::SocketVideoStreamerAdapter( + const std::string& server_address, uint16_t video_streaming_port) + : SocketStreamerAdapter(server_address, video_streaming_port, kHeader) {} -SocketVideoStreamerAdapter::~SocketVideoStreamerAdapter() { -} +SocketVideoStreamerAdapter::~SocketVideoStreamerAdapter() {} } // namespace media_manager diff --git a/src/components/media_manager/src/video/video_stream_to_file_adapter.cc b/src/components/media_manager/src/video/video_stream_to_file_adapter.cc index 33b0c43ee5..f41954b117 100644 --- a/src/components/media_manager/src/video/video_stream_to_file_adapter.cc +++ b/src/components/media_manager/src/video/video_stream_to_file_adapter.cc @@ -40,16 +40,15 @@ namespace media_manager { CREATE_LOGGERPTR_GLOBAL(logger, "VideoStreamToFileAdapter") VideoStreamToFileAdapter::VideoStreamToFileAdapter(const std::string& file_name) - : file_name_(file_name), - is_ready_(false), - thread_(threads::CreateThread("VideoStreamer", - new Streamer(this))) { + : file_name_(file_name) + , is_ready_(false) + , thread_(threads::CreateThread("VideoStreamer", new Streamer(this))) { Init(); } VideoStreamToFileAdapter::~VideoStreamToFileAdapter() { LOG4CXX_AUTO_TRACE(logger); - if ((0 != current_application_ ) && (is_ready_)) { + if ((0 != current_application_) && (is_ready_)) { StopActivity(current_application_); } thread_->join(); @@ -68,10 +67,9 @@ void VideoStreamToFileAdapter::Init() { } void VideoStreamToFileAdapter::SendData( - int32_t application_key, - const ::protocol_handler::RawMessagePtr message) { - LOG4CXX_INFO(logger, "VideoStreamToFileAdapter::SendData " - << application_key); + int32_t application_key, const ::protocol_handler::RawMessagePtr message) { + LOG4CXX_INFO(logger, + "VideoStreamToFileAdapter::SendData " << application_key); if (application_key != current_application_) { LOG4CXX_WARN(logger, "Wrong application " << application_key); @@ -84,11 +82,11 @@ void VideoStreamToFileAdapter::SendData( } void VideoStreamToFileAdapter::StartActivity(int32_t application_key) { - LOG4CXX_INFO(logger, "VideoStreamToFileAdapter::StartActivity " - << application_key); + LOG4CXX_INFO(logger, + "VideoStreamToFileAdapter::StartActivity " << application_key); if (application_key == current_application_) { - LOG4CXX_WARN(logger, "Already running video stream to file for " - << application_key); + LOG4CXX_WARN( + logger, "Already running video stream to file for " << application_key); return; } @@ -103,11 +101,11 @@ void VideoStreamToFileAdapter::StartActivity(int32_t application_key) { } void VideoStreamToFileAdapter::StopActivity(int32_t application_key) { - LOG4CXX_INFO(logger, "VideoStreamToFileAdapter::StopActivity " - << application_key); + LOG4CXX_INFO(logger, + "VideoStreamToFileAdapter::StopActivity " << application_key); if (application_key != current_application_) { - LOG4CXX_WARN(logger, "Performing activity for another key " - << current_application_); + LOG4CXX_WARN( + logger, "Performing activity for another key " << current_application_); return; } @@ -121,17 +119,13 @@ void VideoStreamToFileAdapter::StopActivity(int32_t application_key) { } } -bool VideoStreamToFileAdapter::is_app_performing_activity(int32_t - application_key) { +bool VideoStreamToFileAdapter::is_app_performing_activity( + int32_t application_key) { return (application_key == current_application_ && is_ready_); } -VideoStreamToFileAdapter::Streamer::Streamer( - VideoStreamToFileAdapter* server) - : server_(server), - stop_flag_(false), - file_stream_(NULL) { -} +VideoStreamToFileAdapter::Streamer::Streamer(VideoStreamToFileAdapter* server) + : server_(server), stop_flag_(false), file_stream_(NULL) {} VideoStreamToFileAdapter::Streamer::~Streamer() { server_ = NULL; @@ -139,7 +133,7 @@ VideoStreamToFileAdapter::Streamer::~Streamer() { } void VideoStreamToFileAdapter::Streamer::threadMain() { - LOG4CXX_INFO(logger, "Streamer::threadMain"); + LOG4CXX_AUTO_TRACE(logger); open(); @@ -173,7 +167,7 @@ void VideoStreamToFileAdapter::Streamer::threadMain() { } void VideoStreamToFileAdapter::Streamer::exitThreadMain() { - LOG4CXX_INFO(logger, "Streamer::exitThreadMain"); + LOG4CXX_AUTO_TRACE(logger); stop_flag_ = true; server_->messages_.Shutdown(); } @@ -186,7 +180,7 @@ void VideoStreamToFileAdapter::Streamer::open() { file_stream_ = file_system::Open(server_->file_name_); if (!file_stream_) { - LOG4CXX_WARN(logger, "Can't open file stream! " << server_->file_name_); + LOG4CXX_WARN(logger, "Can't open file stream! " << server_->file_name_); } else { LOG4CXX_INFO(logger, "file_stream_ opened :" << file_stream_); } diff --git a/src/components/media_manager/test/CMakeLists.txt b/src/components/media_manager/test/CMakeLists.txt index 1146aacff0..ede6f27f95 100644 --- a/src/components/media_manager/test/CMakeLists.txt +++ b/src/components/media_manager/test/CMakeLists.txt @@ -1,4 +1,4 @@ -# Copyright (c) 2014, Ford Motor Company +# Copyright (c) 2016, Ford Motor Company # All rights reserved. # # Redistribution and use in source and binary forms, with or without @@ -33,6 +33,10 @@ if(BUILD_TESTS) include_directories( ${GMOCK_INCLUDE_DIRECTORY} ${COMPONENTS_DIR}/media_manager/include + ${COMPONENTS_DIR}/media_manager/test/include + ${COMPONENTS_DIR}/application_manager/test/include + ${COMPONENTS_DIR}/resumption/include + ) if(EXTENDED_MEDIA_MODE) @@ -43,12 +47,14 @@ if(EXTENDED_MEDIA_MODE) endif() set(SOURCES - media_manager_impl_test.cc + ${COMPONENTS_DIR}/media_manager/test/media_manager_impl_test.cc ) set(LIBRARIES + SmartObjects MediaManager ApplicationManager + MessageHelper ProtocolHandler gmock connectionHandler @@ -69,6 +75,6 @@ if(ENABLE_LOG) target_link_libraries("media_manager_test" log4cxx -L${LOG4CXX_LIBS_DIRECTORY}) endif() -endif() # BUILD_TESTS +endif() # vim: set ts=2 sw=2 et: diff --git a/src/components/media_manager/test/include/media_manager/media_adapter_impl_mock.h b/src/components/media_manager/test/include/media_manager/media_adapter_impl_mock.h new file mode 100644 index 0000000000..ff9c2fec9f --- /dev/null +++ b/src/components/media_manager/test/include/media_manager/media_adapter_impl_mock.h @@ -0,0 +1,71 @@ +/* + * Copyright (c) 2015, Ford Motor Company + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following + * disclaimer in the documentation and/or other materials provided with the + * distribution. + * + * Neither the name of the Ford Motor Company nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef TEST_COMPONENTS_MEDIA_MANAGER_INCLUDE_MEDIA_MANAGER_MEDIA_ADAPTER_IMPL_MOCK_H_ +#define TEST_COMPONENTS_MEDIA_MANAGER_INCLUDE_MEDIA_MANAGER_MEDIA_ADAPTER_IMPL_MOCK_H_ + +#include <gmock/gmock.h> +#include "media_manager/media_adapter_impl.h" +#include "media_adapter_listener_mock.h" +#include "media_manager/media_adapter_listener.h" +#include "media_adapter_mock.h" +#include "utils/macro.h" +#include "protocol_handler/protocol_handler.h" + +namespace test { +namespace components { +namespace media_manager_test { + +/* + * MOCK implementation of ::media_manager::MediaAdapterImpl + */ +using namespace media_manager; +typedef utils::SharedPtr<MediaAdapterListener> MediaListenerPtr; +class MockMediaAdapterImpl : public ::media_manager::MediaAdapterImpl { + public: + MOCK_METHOD1(AddListener, + void(const utils::SharedPtr<MediaAdapterListener>&)); + MOCK_METHOD1(RemoveListener, + void(const utils::SharedPtr<MediaAdapterListener>&)); + MOCK_METHOD2(SendData, + void(int32_t application_key, + const ::protocol_handler::RawMessagePtr message)); + MOCK_METHOD1(StartActivity, void(int32_t application_key)); + MOCK_METHOD1(StopActivity, void(int32_t application_key)); + MOCK_CONST_METHOD1(is_app_performing_activity, bool(int32_t application_key)); +}; + +} // namespace media_manager_test +} // namespace components +} // namespace test + +#endif // TEST_COMPONENTS_MEDIA_MANAGER_INCLUDE_MEDIA_MANAGER_MEDIA_ADAPTER_IMPL_MOCK_H_ diff --git a/src/components/media_manager/test/include/media_manager/media_adapter_listener_mock.h b/src/components/media_manager/test/include/media_manager/media_adapter_listener_mock.h new file mode 100644 index 0000000000..2ba64479b6 --- /dev/null +++ b/src/components/media_manager/test/include/media_manager/media_adapter_listener_mock.h @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2015, Ford Motor Company + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following + * disclaimer in the documentation and/or other materials provided with the + * distribution. + * + * Neither the name of the Ford Motor Company nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef TEST_COMPONENTS_MEDIA_MANAGER_INCLUDE_MEDIA_MANAGER_MEDIA_ADAPTER_LISTENER_MOCK_H_ +#define TEST_COMPONENTS_MEDIA_MANAGER_INCLUDE_MEDIA_MANAGER_MEDIA_ADAPTER_LISTENER_MOCK_H_ + +#include <gmock/gmock.h> +#include "media_manager/media_adapter_listener.h" + +namespace test { +namespace components { +namespace media_manager_test { + +/* + * MOCK implementation of ::media_manager::MediaAdapterListener + */ +class MockMediaAdapterListener : public ::media_manager::MediaAdapterListener { + public: + MOCK_METHOD2(OnDataReceived, + void(int32_t application_key, const int32_t& data)); + MOCK_METHOD1(OnActivityStarted, void(int32_t application_key)); + MOCK_METHOD1(OnActivityEnded, void(int32_t application_key)); + MOCK_METHOD2(OnErrorReceived, + void(int32_t application_key, const int32_t& data)); +}; + +} // namespace media_manager_test +} // namespace components +} // namespace test + +#endif // TEST_COMPONENTS_MEDIA_MANAGER_INCLUDE_MEDIA_MANAGER_MEDIA_ADAPTER_LISTENER_MOCK_H_ diff --git a/src/components/media_manager/test/include/media_manager/media_adapter_mock.h b/src/components/media_manager/test/include/media_manager/media_adapter_mock.h new file mode 100644 index 0000000000..576a1a1a9f --- /dev/null +++ b/src/components/media_manager/test/include/media_manager/media_adapter_mock.h @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2015, Ford Motor Company + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following + * disclaimer in the documentation and/or other materials provided with the + * distribution. + * + * Neither the name of the Ford Motor Company nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef TEST_COMPONENTS_MEDIA_MANAGER_INCLUDE_MEDIA_MANAGER_MEDIA_ADAPTER_MOCK_H_ +#define TEST_COMPONENTS_MEDIA_MANAGER_INCLUDE_MEDIA_MANAGER_MEDIA_ADAPTER_MOCK_H_ + +#include <gmock/gmock.h> +#include "media_manager/media_manager.h" +#include "media_manager/media_adapter.h" + +namespace test { +namespace components { +namespace media_manager_test { + +/* + * MOCK implementation of ::media_manager::MediaAdapter + */ +class MockMediaAdapter : public ::media_manager::MediaAdapter { + public: + MOCK_METHOD2(SendData, + void(int32_t application_key, + const ::protocol_handler::RawMessagePtr message)); + MOCK_METHOD1(StartActivity, void(int32_t application_key)); + MOCK_METHOD1(StopActivity, void(int32_t application_key)); + MOCK_CONST_METHOD1(is_app_performing_activity, bool(int32_t application_key)); +}; + +} // namespace media_manager_test +} // namespace components +} // namespace test + +#endif // TEST_COMPONENTS_MEDIA_MANAGER_INCLUDE_MEDIA_MANAGER_MEDIA_ADAPTER_MOCK_H_ diff --git a/src/components/media_manager/test/media_adapter_impl_test.cc b/src/components/media_manager/test/media_adapter_impl_test.cc deleted file mode 100644 index f6a6ffa518..0000000000 --- a/src/components/media_manager/test/media_adapter_impl_test.cc +++ /dev/null @@ -1,37 +0,0 @@ -/* -* Copyright (c) 2014, Ford Motor Company -* All rights reserved. -* -* Redistribution and use in source and binary forms, with or without -* modification, are permitted provided that the following conditions are met: -* -* Redistributions of source code must retain the above copyright notice, this -* list of conditions and the following disclaimer. -* -* Redistributions in binary form must reproduce the above copyright notice, -* this list of conditions and the following -* disclaimer in the documentation and/or other materials provided with the -* distribution. -* -* Neither the name of the Ford Motor Company nor the names of its contributors -* may be used to endorse or promote products derived from this software -* without specific prior written permission. -* -* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" -* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE -* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE -* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR -* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF -* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS -* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN -* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) -* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE -* POSSIBILITY OF SUCH DAMAGE. -*/ - -#include "gmock/gmock.h" - -TEST(MediaAdapterImplTest, DummyTest) { - ASSERT_TRUE(true); -} diff --git a/src/components/media_manager/test/media_manager_impl_test.cc b/src/components/media_manager/test/media_manager_impl_test.cc index 615d74f9a1..8db510580c 100644 --- a/src/components/media_manager/test/media_manager_impl_test.cc +++ b/src/components/media_manager/test/media_manager_impl_test.cc @@ -1,5 +1,5 @@ /* - * Copyright (c) 2014, Ford Motor Company + * Copyright (c) 2015, Ford Motor Company * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -32,61 +32,150 @@ #include "gmock/gmock.h" #include "media_manager/media_manager_impl.h" +#include "media_manager/media_adapter_mock.h" +#include "media_manager/media_adapter_listener_mock.h" +#include "media_manager/media_adapter_impl_mock.h" +#include "protocol_handler/mock_protocol_handler.h" +#include "media_manager/mock_media_manager_settings.h" +#include "application_manager/mock_application_manager.h" +#include "utils/shared_ptr.h" +#include "utils/make_shared.h" +#include "application_manager/event_engine/event_dispatcher.h" +#include "application_manager/state_controller.h" +#include "application_manager/resumption/resume_ctrl.h" +#include "resumption/last_state.h" namespace test { namespace components { namespace media_manager_test { -CREATE_LOGGERPTR_GLOBAL(logger_, "MediaManagerImplTest") +using ::testing::_; +using ::testing::ReturnRef; +using protocol_handler::ServiceType; -class MediaManagerTest : public ::testing::Test { - protected: - virtual void SetUp(); - virtual void TearDown(); +class MediaManagerImplTest : public ::testing::Test { + protected: + const ::testing::NiceMock<MockMediaManagerSettings> + mock_media_manager_settings_; + const std::string kDefaultValue_ = ""; }; -void MediaManagerTest::SetUp() { -} - -void MediaManagerTest::TearDown() { -} - -TEST_F(MediaManagerTest, AddAndPlayStream) { - media_manager::MediaManager* mediaManager = - media_manager::MediaManagerImpl::instance(); - - const useconds_t sleeptime = 100; - - mediaManager->PlayA2DPSource(1); - LOG4CXX_INFO(logger_, ".Playing stream"); - - usleep(sleeptime); - - mediaManager->StopA2DPSource(1); - - usleep(sleeptime); +TEST_F(MediaManagerImplTest, PlayA2DPSource) { + MockMediaAdapter* media_mock_ = new MockMediaAdapter(); - mediaManager->PlayA2DPSource(1); + application_manager_test::MockApplicationManager mock_application_manager; - usleep(sleeptime); + ON_CALL(mock_media_manager_settings_, video_server_type()) + .WillByDefault(ReturnRef(kDefaultValue_)); + ON_CALL(mock_media_manager_settings_, audio_server_type()) + .WillByDefault(ReturnRef(kDefaultValue_)); + MediaManagerImpl mediaManagerImpl(mock_application_manager, + mock_media_manager_settings_); + int32_t application_key = 1; - mediaManager->StopA2DPSource(1); - - usleep(sleeptime); - - mediaManager->PlayA2DPSource(1); - - usleep(sleeptime); - - mediaManager->StopA2DPSource(1); + mediaManagerImpl.set_mock_a2dp_player(media_mock_); + EXPECT_CALL(*media_mock_, StartActivity(application_key)); + mediaManagerImpl.PlayA2DPSource(application_key); +} - usleep(sleeptime); +TEST_F(MediaManagerImplTest, StopA2DPSource) { + MockMediaAdapter* media_mock_ = new MockMediaAdapter(); + application_manager_test::MockApplicationManager mock_application_manager; + + ON_CALL(mock_media_manager_settings_, video_server_type()) + .WillByDefault(ReturnRef(kDefaultValue_)); + ON_CALL(mock_media_manager_settings_, audio_server_type()) + .WillByDefault(ReturnRef(kDefaultValue_)); + MediaManagerImpl mediaManagerImpl(mock_application_manager, + mock_media_manager_settings_); + int32_t application_key = 1; + + mediaManagerImpl.set_mock_a2dp_player(media_mock_); + EXPECT_CALL(*media_mock_, StopActivity(application_key)); + mediaManagerImpl.StopA2DPSource(application_key); +} - mediaManager->StopA2DPSource(1); +TEST_F(MediaManagerImplTest, StopMicrophoneRecording) { + MockMediaAdapterListener* media_adapter_listener_mock_ = + new MockMediaAdapterListener(); + application_manager_test::MockApplicationManager mock_application_manager; + + ON_CALL(mock_media_manager_settings_, video_server_type()) + .WillByDefault(ReturnRef(kDefaultValue_)); + ON_CALL(mock_media_manager_settings_, audio_server_type()) + .WillByDefault(ReturnRef(kDefaultValue_)); + MediaManagerImpl mediaManagerImpl(mock_application_manager, + mock_media_manager_settings_); + int32_t application_key = 1; + + mediaManagerImpl.set_mock_mic_listener(media_adapter_listener_mock_); +#ifdef EXTENDED_MEDIA_MODE + MockMediaAdapterImpl* media_adapter_recorder_mock = + new MockMediaAdapterImpl(); + mediaManagerImpl.set_mock_mic_recorder(media_adapter_recorder_mock); + EXPECT_CALL(*media_adapter_recorder_mock, StopActivity(application_key)); +#endif // EXTENDED_MEDIA_MODE + EXPECT_CALL(*media_adapter_listener_mock_, OnActivityEnded(application_key)); +#ifdef EXTENDED_MEDIA_MODE + EXPECT_CALL(*media_adapter_recorder_mock, RemoveListener(_)); +#endif // EXTENDED_MEDIA_MODE + mediaManagerImpl.StopMicrophoneRecording(application_key); } -} // namespace media_manager_test -} // namespace components -} // namespace test +TEST_F(MediaManagerImplTest, StartStopStreaming) { + application_manager_test::MockApplicationManager mock_application_manager; + + ON_CALL(mock_media_manager_settings_, video_server_type()) + .WillByDefault(ReturnRef(kDefaultValue_)); + ON_CALL(mock_media_manager_settings_, audio_server_type()) + .WillByDefault(ReturnRef(kDefaultValue_)); + MediaManagerImpl mediaManagerImpl(mock_application_manager, + mock_media_manager_settings_); + + int32_t application_key = 1; + MockMediaAdapterImpl* mock_audio_media_streamer = new MockMediaAdapterImpl(); + mediaManagerImpl.set_mock_streamer(protocol_handler::ServiceType::kAudio, + mock_audio_media_streamer); + MockMediaAdapterImpl* mock_nav_media_streamer = new MockMediaAdapterImpl(); + mediaManagerImpl.set_mock_streamer(protocol_handler::ServiceType::kMobileNav, + mock_nav_media_streamer); + + EXPECT_CALL(*mock_audio_media_streamer, StartActivity(application_key)); + mediaManagerImpl.StartStreaming(application_key, + protocol_handler::ServiceType::kAudio); + + EXPECT_CALL(*mock_nav_media_streamer, StartActivity(application_key)); + mediaManagerImpl.StartStreaming(application_key, + protocol_handler::ServiceType::kMobileNav); + + EXPECT_CALL(*mock_audio_media_streamer, StopActivity(application_key)); + mediaManagerImpl.StopStreaming(application_key, + protocol_handler::ServiceType::kAudio); + + EXPECT_CALL(*mock_nav_media_streamer, StopActivity(application_key)); + mediaManagerImpl.StopStreaming(application_key, + protocol_handler::ServiceType::kMobileNav); +} +TEST_F(MediaManagerImplTest, CheckFramesProcessed) { + application_manager_test::MockApplicationManager mock_application_manager; + + ON_CALL(mock_media_manager_settings_, video_server_type()) + .WillByDefault(ReturnRef(kDefaultValue_)); + ON_CALL(mock_media_manager_settings_, audio_server_type()) + .WillByDefault(ReturnRef(kDefaultValue_)); + MediaManagerImpl mediaManagerImpl(mock_application_manager, + mock_media_manager_settings_); + protocol_handler_test::MockProtocolHandler mock_protocol_handler; + mediaManagerImpl.SetProtocolHandler(&mock_protocol_handler); + int32_t application_key = 1; + int32_t frame_number = 10; + + EXPECT_CALL(mock_protocol_handler, + SendFramesNumber(application_key, frame_number)); + mediaManagerImpl.FramesProcessed(application_key, frame_number); +} +} // namespace media_manager_test +} // namespace components +} // namespace test |