summaryrefslogtreecommitdiff
path: root/src/components/media_manager/src/audio/from_mic_to_file_recorder_thread.cc
blob: 310fca7aa3d3eb2ddecaa73629b12488057db79b (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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/*
 * 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/audio/from_mic_to_file_recorder_thread.h"
#include <unistd.h>
#include <cstring>
#include <sstream>
#include "interfaces/MOBILE_API.h"
#include "utils/logger.h"

namespace media_manager {

SDL_CREATE_LOG_VARIABLE("MediaManager")

GMainLoop* FromMicToFileRecorderThread::loop = NULL;

// As per spec, AudioPassThru recording is in monaural
static const int kNumAudioChannels = 1;

FromMicToFileRecorderThread::FromMicToFileRecorderThread(
    const std::string& output_file,
    int32_t duration,
    mobile_apis::SamplingRate::eType sampling_rate,
    mobile_apis::BitsPerSample::eType bits_per_sample,
    mobile_apis::AudioType::eType audio_type)
    : threads::ThreadDelegate()
    , argc_(5)
    , argv_(NULL)
    , oKey_("-o")
    , tKey_("-t")
    , sleepThread_(NULL)
    , outputFileName_(output_file)
    , samplingRate_(sampling_rate)
    , bitsPerSample_(bits_per_sample) {
  SDL_LOG_AUTO_TRACE();
  set_record_duration(duration);
  // audio_type is not used as we always employ LPCM
}

FromMicToFileRecorderThread::~FromMicToFileRecorderThread() {
  SDL_LOG_AUTO_TRACE();
  if (sleepThread_) {
    sleepThread_->Stop(threads::Thread::kThreadSoftStop);
    delete sleepThread_->GetDelegate();
    threads::DeleteThread(sleepThread_);
  }
}

void FromMicToFileRecorderThread::set_output_file(
    const std::string& output_file) {
  SDL_LOG_AUTO_TRACE();
  outputFileName_ = output_file;
}

void FromMicToFileRecorderThread::set_record_duration(int32_t duration) {
  SDL_LOG_AUTO_TRACE();

  std::stringstream stringStream;
  stringStream << duration / 1000;
  durationString_ = stringStream.str();
}

void FromMicToFileRecorderThread::initArgs() {
  SDL_LOG_AUTO_TRACE();

  argv_ = new gchar*[argc_];

  argv_[0] = new gchar[14];
  argv_[1] = new gchar[3];
  argv_[2] = new gchar[outputFileName_.length() + 1];
  argv_[3] = new gchar[3];
  argv_[4] = new gchar[durationString_.length() + 1];

  std::strcpy(argv_[0], "AudioManager");
  std::strcpy(argv_[1], oKey_.c_str());
  std::strcpy(argv_[2], outputFileName_.c_str());
  std::strcpy(argv_[3], tKey_.c_str());
  std::strcpy(argv_[4], durationString_.c_str());
}

void FromMicToFileRecorderThread::deinitArgs() {
  SDL_LOG_AUTO_TRACE();

  if (argv_) {
    for (int32_t i = 0; i < argc_; ++i) {
      delete[] argv_[i];
    }
    delete[] argv_;
    argv_ = NULL;
  }
}

void FromMicToFileRecorderThread::threadMain() {
  SDL_LOG_AUTO_TRACE();

  {
    sync_primitives::AutoLock auto_lock(stopFlagLock_);
    shouldBeStoped_ = false;
  }

  initArgs();

  GstElement* pipeline;
  GstElement *alsasrc, *audioconvert, *capsfilter, *wavenc, *filesink;
  GstCaps* audiocaps;
  GstBus* bus;

  const gchar* device = "hw:0,0";
  gchar* outfile = NULL;
  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}};
  // g_option_context_parse() modifies params, so keep argc_ and argv_
  int32_t argc = argc_;
  gchar** argv = new gchar*[argc];
  for (int32_t i = 0; i < argc; ++i) {
    argv[i] = argv_[i];
  }

#ifndef GLIB_VERSION_2_32  // g_thread_init() does nothing since 2.32
  if (!g_thread_supported()) {
    g_thread_init(NULL);
  }
#endif
  // Parse the arguments
  context = g_option_context_new("-- M-AUDIO RAW");
  g_option_context_add_main_entries(context, entries, NULL);
  g_option_context_add_group(context, gst_init_get_option_group());
  if (!g_option_context_parse(context, &argc, &argv, &err)) {
    g_error("%s\n", err->message);
  }

  // Check for proper arguments
  if (outfile == NULL) {
    g_error("Must supply destination (-d FILE)\n");
  }

  SDL_LOG_TRACE("Reading from device: " << device);
  SDL_LOG_TRACE("Saving pipeline output to: " << outfile);
  SDL_LOG_TRACE("Duration set to: " << duration);

  // Initialize gstreamer and setup the main loop information
  gst_init(&argc, &argv);

  delete[] argv;
  argv = NULL;

  pipeline = gst_pipeline_new("vga2usb-h264");

  // 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_object_unref(bus);

  // Create all of the elements to be added to the pipeline
  alsasrc = gst_element_factory_make("alsasrc", "alsasrc0");
  audioconvert = gst_element_factory_make("audioconvert", "audioconvert0");
  capsfilter = gst_element_factory_make("capsfilter", "filter0");
  wavenc = gst_element_factory_make("wavenc", "wavenc0");
  filesink = gst_element_factory_make("filesink", "filesink0");

  // Create a capability to specify audio format. It also downmixes the recorded
  // audio to monaural.
  std::string caps_string = create_caps_string();
  SDL_LOG_DEBUG("Using audio caps: " << caps_string);
  audiocaps = gst_caps_from_string(caps_string.c_str());

  // Assert that all the elements were created
  if (!alsasrc || !audioconvert || !capsfilter || !wavenc || !filesink ||
      !audiocaps) {
    g_error("Failed creating one or more of the pipeline elements.\n");
  }

  // Set input and output destinations
  g_object_set(G_OBJECT(alsasrc), "device", device, NULL);
  g_object_set(G_OBJECT(filesink), "location", outfile, NULL);

  // Add the elements to the pipeline
  gst_bin_add_many(GST_BIN(pipeline),
                   alsasrc,
                   audioconvert,
                   capsfilter,
                   wavenc,
                   filesink,
                   NULL);

  // Link the elements
  gst_element_link_many(
      alsasrc, audioconvert, capsfilter, wavenc, filesink, NULL);

  // set the capability
  g_object_set(G_OBJECT(capsfilter), "caps", audiocaps, NULL);
  gst_caps_unref(audiocaps);

  gst_element_set_state(pipeline, GST_STATE_PLAYING);

  SDL_LOG_TRACE("Initializing pipeline ...");
  while (GST_STATE(pipeline) != GST_STATE_PLAYING) {
    bool shouldBeStoped;
    {
      // FIXME(dchmerev@luxoft.com):
      sync_primitives::AutoLock auto_lock(stopFlagLock_);
      shouldBeStoped = shouldBeStoped_;
    }

    if (shouldBeStoped) {
      gst_element_set_state(pipeline, GST_STATE_NULL);
      gst_object_unref(GST_OBJECT(pipeline));
      g_option_context_free(context);

      deinitArgs();
      return;
    }
  }
  SDL_LOG_TRACE("Pipeline started ...\n");

  // Start up a timer for the pipeline
  if (duration > 0) {
    GstTimeout timeout;
    timeout.pipeline = pipeline;
    timeout.duration = duration;

    sleepThread_ =
        threads::CreateThread("SleepThread", new SleepThreadDelegate(timeout));
    sleepThread_->Start();
  }

  loop = g_main_loop_new(NULL, FALSE);

  g_main_loop_run(loop);

  gst_element_set_state(pipeline, GST_STATE_NULL);

  SDL_LOG_TRACE("Deleting pipeline\n");
  gst_object_unref(GST_OBJECT(pipeline));
  g_main_loop_unref(loop);
  g_option_context_free(context);

  deinitArgs();

  loop = NULL;
}

std::string FromMicToFileRecorderThread::create_caps_string() {
  SDL_LOG_AUTO_TRACE();

  std::stringstream ss;
  ss << "audio/x-raw";

  switch (bitsPerSample_) {
    case mobile_apis::BitsPerSample::BitsPerSample_8_BIT:
      // format is 8-bit unsigned
      ss << ",format=(string)U8";
      break;
    case mobile_apis::BitsPerSample::BitsPerSample_16_BIT:
      // format is 16-bit signed, in little endian
      ss << ",format=(string)S16LE";
      break;
    default:
      // do not specify the format; use system default
      break;
  }

  switch (samplingRate_) {
    case mobile_apis::SamplingRate::SamplingRate_8KHZ:
      ss << ",rate=8000";
      break;
    case mobile_apis::SamplingRate::SamplingRate_16KHZ:
      ss << ",rate=16000";
      break;
    case mobile_apis::SamplingRate::SamplingRate_22KHZ:
      ss << ",rate=22050";
      break;
    case mobile_apis::SamplingRate::SamplingRate_44KHZ:
      ss << ",rate=44100";
      break;
    default:
      // do not specify the sampling rate; use system default
      break;
  }

  ss << ",channels=" << kNumAudioChannels;

  return ss.str();
}

FromMicToFileRecorderThread::SleepThreadDelegate::SleepThreadDelegate(
    GstTimeout timeout)
    : threads::ThreadDelegate(), timeout_(timeout) {}

void FromMicToFileRecorderThread::SleepThreadDelegate::threadMain() {
  SDL_LOG_TRACE("Sleep for " << timeout_.duration << " seconds");

  sleep(timeout_.duration);

  if (NULL != loop) {
    if (g_main_loop_is_running(loop)) {
      gst_element_send_event(timeout_.pipeline, gst_event_new_eos());
    }
  }
}

void FromMicToFileRecorderThread::exitThreadMain() {
  SDL_LOG_AUTO_TRACE();

  if (NULL != loop) {
    if (g_main_loop_is_running(loop)) {
      SDL_LOG_TRACE("Quit loop\n");
      g_main_loop_quit(loop);
    }
  }

  if (sleepThread_) {
    SDL_LOG_DEBUG("Stop sleep thread\n");
    sleepThread_->Stop(threads::Thread::kThreadStopDelegate);
  }

  SDL_LOG_TRACE("Set should be stopped flag\n");
  sync_primitives::AutoLock auto_lock(stopFlagLock_);
  shouldBeStoped_ = true;
}

}  // namespace media_manager