~ubuntu-branches/ubuntu/wily/sflphone/wily

« back to all changes in this revision

Viewing changes to daemon/src/audio/audiorecorder.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2015-01-07 14:51:16 UTC
  • mfrom: (4.3.5 sid)
  • Revision ID: package-import@ubuntu.com-20150107145116-yxnafinf4lrdvrmx
Tags: 1.4.1-0.1ubuntu1
* Merge with Debian, remaining changes:
 - Drop soprano, nepomuk build-dep
* Drop ubuntu patches, now upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
29
29
 */
30
30
 
31
31
#include "audiorecorder.h"
 
32
#include "audiorecord.h"
32
33
#include "mainbuffer.h"
33
34
#include "logger.h"
 
35
 
 
36
#include <chrono>
34
37
#include <sstream>
35
38
#include <unistd.h>
36
39
 
37
40
int AudioRecorder::count_ = 0;
38
41
 
39
42
AudioRecorder::AudioRecorder(AudioRecord  *arec, MainBuffer &mb) :
40
 
    recorderId_(), mbuffer_(mb), arecord_(arec), running_(false), thread_(0)
 
43
    recorderId_(), mbuffer_(mb), arecord_(arec), running_(false), thread_()
41
44
{
42
45
    ++count_;
43
46
 
56
59
{
57
60
    running_ = false;
58
61
 
59
 
    if (thread_)
60
 
        pthread_join(thread_, NULL);
 
62
    if (thread_.joinable())
 
63
        thread_.join();
 
64
}
 
65
 
 
66
void AudioRecorder::init() {
 
67
    if (!arecord_->isRecording()) {
 
68
        arecord_->setSndFormat(mbuffer_.getInternalAudioFormat());
 
69
    }
61
70
}
62
71
 
63
72
void AudioRecorder::start()
64
73
{
 
74
    if (running_) return;
65
75
    running_ = true;
66
 
    pthread_create(&thread_, NULL, &runCallback, this);
67
 
}
68
 
 
69
 
void *
70
 
AudioRecorder::runCallback(void *data)
71
 
{
72
 
    AudioRecorder *context = static_cast<AudioRecorder*>(data);
73
 
    context->run();
74
 
    return NULL;
 
76
    thread_ = std::thread(&AudioRecorder::run, this);
75
77
}
76
78
 
77
79
/**
80
82
void AudioRecorder::run()
81
83
{
82
84
    static const size_t BUFFER_LENGTH = 10000;
83
 
    AudioBuffer buffer(BUFFER_LENGTH, 1, 8000);
 
85
    static const std::chrono::milliseconds SLEEP_TIME(20); // 20 ms
 
86
 
 
87
    AudioBuffer buffer(BUFFER_LENGTH, mbuffer_.getInternalAudioFormat());
84
88
 
85
89
    while (running_) {
86
90
        const size_t availableSamples = mbuffer_.availableForGet(recorderId_);
90
94
        if (availableSamples > 0)
91
95
            arecord_->recData(buffer);
92
96
 
93
 
        usleep(20000); // 20 ms
 
97
        std::this_thread::sleep_for(SLEEP_TIME);
94
98
    }
95
99
}