summaryrefslogtreecommitdiff
path: root/src/components/media_manager/src/media_manager_impl.cc
blob: b89fc4c71fd3d4ce2600c3b7595fcf5dcf6c6443 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*
 * 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 "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.h"
#include "application_manager/application_impl.h"
#include "protocol_handler/protocol_handler.h"
#include "utils/file_system.h"
#include "utils/logger.h"
#include "utils/helpers.h"
#if defined(EXTENDED_MEDIA_MODE)
#include "media_manager/audio/a2dp_source_player_adapter.h"
#include "media_manager/audio/from_mic_recorder_adapter.h"
#endif
#include "media_manager/video/socket_video_streamer_adapter.h"
#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/file_video_streamer_adapter.h"
#include "media_manager/audio/file_audio_streamer_adapter.h"
#include "media_manager/media_manager_settings.h"

namespace media_manager {

CREATE_LOGGERPTR_GLOBAL(logger_, "MediaManager")

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();
}

MediaManagerImpl::~MediaManagerImpl() {
  if (a2dp_player_) {
    delete a2dp_player_;
    a2dp_player_ = NULL;
  }

  if (from_mic_recorder_) {
    delete from_mic_recorder_;
    from_mic_recorder_ = NULL;
  }
}

#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.");
  from_mic_recorder_ = new FromMicRecorderAdapter();
#endif

  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" == 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(*this);
  streamer_listener_[ServiceType::kAudio] = new StreamerListener(*this);

  if (streamer_[ServiceType::kMobileNav]) {
    streamer_[ServiceType::kMobileNav]->AddListener(
        streamer_listener_[ServiceType::kMobileNav]);
  }

  if (streamer_[ServiceType::kAudio]) {
    streamer_[ServiceType::kAudio]->AddListener(
        streamer_listener_[ServiceType::kAudio]);
  }
}

void MediaManagerImpl::PlayA2DPSource(int32_t application_key) {
  LOG4CXX_AUTO_TRACE(logger_);

#if defined(EXTENDED_MEDIA_MODE)
  if (!a2dp_player_ && protocol_handler_) {
    a2dp_player_ =
        new A2DPSourcePlayerAdapter(protocol_handler_->get_session_observer());
  }
#endif

  if (a2dp_player_) {
    a2dp_player_->StartActivity(application_key);
  }
}

void MediaManagerImpl::StopA2DPSource(int32_t application_key) {
  LOG4CXX_AUTO_TRACE(logger_);
  if (a2dp_player_) {
    a2dp_player_->StopActivity(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);
  application_manager::ApplicationSharedPtr app =
      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, 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);
    (static_cast<FromMicRecorderAdapter*>(from_mic_recorder_))
        ->set_duration(duration);
    from_mic_recorder_->StartActivity(application_key);
  }
#else
  if (file_system::FileExists(file_path)) {
    LOG4CXX_INFO(logger_, "File " << output_file << " exists, removing");
    if (file_system::DeleteFile(file_path)) {
      LOG4CXX_INFO(logger_, "File " << output_file << " removed");
    } else {
      LOG4CXX_WARN(logger_, "Could not remove file " << output_file);
    }
  }
  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 {
      LOG4CXX_WARN(logger_, "Could not write to file " << output_file);
    }
  } else {
    LOG4CXX_WARN(logger_, "Could not read file " << record_file_source);
  }
#endif
  from_mic_listener_->OnActivityStarted(application_key);
}

void MediaManagerImpl::StopMicrophoneRecording(int32_t application_key) {
  LOG4CXX_AUTO_TRACE(logger_);
#if defined(EXTENDED_MEDIA_MODE)
  if (from_mic_recorder_) {
    from_mic_recorder_->StopActivity(application_key);
  }
#endif
  if (from_mic_listener_) {
    from_mic_listener_->OnActivityEnded(application_key);
  }
#if defined(EXTENDED_MEDIA_MODE)
  if (from_mic_recorder_) {
    from_mic_recorder_->RemoveListener(from_mic_listener_);
  }
#endif
}

void MediaManagerImpl::StartStreaming(
    int32_t application_key, protocol_handler::ServiceType service_type) {
  LOG4CXX_AUTO_TRACE(logger_);

  if (streamer_[service_type]) {
    streamer_[service_type]->StartActivity(application_key);
  }
}

void MediaManagerImpl::StopStreaming(
    int32_t application_key, protocol_handler::ServiceType service_type) {
  LOG4CXX_AUTO_TRACE(logger_);

  if (streamer_[service_type]) {
    streamer_[service_type]->StopActivity(application_key);
  }
}

void MediaManagerImpl::SetProtocolHandler(
    protocol_handler::ProtocolHandler* protocol_handler) {
  protocol_handler_ = protocol_handler;
}

void MediaManagerImpl::OnMessageReceived(
    const ::protocol_handler::RawMessagePtr message) {
  using namespace protocol_handler;
  using namespace application_manager;
  using namespace helpers;
  LOG4CXX_AUTO_TRACE(logger_);

  const uint32_t streaming_app_id = message->connection_key();
  const ServiceType service_type = message->service_type();

  if (Compare<ServiceType, NEQ, ALL>(
          service_type, ServiceType::kMobileNav, ServiceType::kAudio)) {
    LOG4CXX_DEBUG(logger_, "Unsupported service type in MediaManager");
    return;
  }

  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 = application_manager_.application(streaming_app_id);
  if (app) {
    app->WakeUpStreaming(service_type);
    streamer_[service_type]->SendData(streaming_app_id, message);
  }
}

void MediaManagerImpl::OnMobileMessageSent(
    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);
  }
}

const MediaManagerSettings& MediaManagerImpl::settings() const {
  return settings_;
}

}  //  namespace media_manager