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

« back to all changes in this revision

Viewing changes to daemon/src/video/shm_sink.cpp

  • Committer: Package Import Robot
  • Author(s): Mark Purcell
  • Date: 2014-01-28 18:23:36 UTC
  • mfrom: (1.1.11)
  • mto: This revision was merged to the branch mainline in revision 24.
  • Revision ID: package-import@ubuntu.com-20140128182336-3xenud1kbnwmf3mz
* New upstream release 
  - Fixes "New Upstream Release" (Closes: #735846)
  - Fixes "Ringtone does not stop" (Closes: #727164)
  - Fixes "[sflphone-kde] crash on startup" (Closes: #718178)
  - Fixes "sflphone GUI crashes when call is hung up" (Closes: #736583)
* Build-Depends: ensure GnuTLS 2.6
  - libucommon-dev (>= 6.0.7-1.1), libccrtp-dev (>= 2.0.6-3)
  - Fixes "FTBFS Build-Depends libgnutls{26,28}-dev" (Closes: #722040)
* Fix "boost 1.49 is going away" unversioned Build-Depends: (Closes: #736746)
* Add Build-Depends: libsndfile-dev, nepomuk-core-dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
 *  Copyright (C) 2012 Savoir-Faire Linux Inc.
 
2
 *  Copyright (C) 2012-2013 Savoir-Faire Linux Inc.
3
3
 *  Author: Tristan Matthews <tristan.matthews@savoirfairelinux.com>
4
4
 *
5
5
 *  Portions derived from GStreamer:
37
37
#include "config.h"
38
38
#endif
39
39
 
 
40
#include "video_scaler.h"
 
41
 
40
42
#include "shm_sink.h"
41
43
#include "shm_header.h"
42
44
#include "logger.h"
43
 
#include "video_provider.h"
 
45
 
44
46
#include <sys/mman.h>
45
47
#include <fcntl.h>
46
48
#include <cstdio>
49
51
#include <cerrno>
50
52
#include <cstring>
51
53
 
 
54
namespace sfl_video {
 
55
 
52
56
SHMSink::SHMSink(const std::string &shm_name) :
53
 
    shm_name_(shm_name),
54
 
    fd_(-1),
55
 
    shm_area_(static_cast<SHMHeader*>(MAP_FAILED)),
56
 
    shm_area_len_(0),
57
 
    opened_name_()
58
 
    {}
 
57
    shm_name_(shm_name)
 
58
    , fd_(-1)
 
59
    , shm_area_(static_cast<SHMHeader*>(MAP_FAILED))
 
60
    , shm_area_len_(0)
 
61
    , opened_name_()
 
62
{}
59
63
 
60
64
SHMSink::~SHMSink()
61
65
{
62
66
    stop();
63
67
}
64
68
 
65
 
bool
66
 
SHMSink::start()
 
69
bool SHMSink::start()
67
70
{
68
71
    if (fd_ != -1) {
69
72
        ERROR("fd must be -1");
76
79
    if (not shm_name_.empty()) {
77
80
        fd_ = shm_open(shm_name_.c_str(), flags, perms);
78
81
        if (fd_ < 0) {
79
 
            ERROR("could not open shm area \"%s\", shm_open failed:%s", shm_name_.c_str(), strerror(errno));
80
 
            perror(strerror(errno));
 
82
            ERROR("could not open shm area \"%s\"", shm_name_.c_str());
 
83
            Logger::strErr();
81
84
            return false;
82
85
        }
83
86
    } else {
87
90
            shm_name_ = name.str();
88
91
            fd_ = shm_open(shm_name_.c_str(), flags, perms);
89
92
            if (fd_ < 0 and errno != EEXIST) {
90
 
                ERROR("%s", strerror(errno));
 
93
                Logger::strErr();
91
94
                return false;
92
95
            }
93
96
        }
100
103
 
101
104
    if (ftruncate(fd_, shm_area_len_)) {
102
105
        ERROR("Could not make shm area large enough for header");
103
 
        perror(strerror(errno));
 
106
        Logger::strErr();
104
107
        return false;
105
108
    }
106
109
 
123
126
    return true;
124
127
}
125
128
 
126
 
bool
127
 
SHMSink::stop()
 
129
bool SHMSink::stop()
128
130
{
129
 
    if (fd_ >= 0)
130
 
        close(fd_);
 
131
    if (fd_ >= 0 and close(fd_) == -1)
 
132
        Logger::strErr();
 
133
 
131
134
    fd_ = -1;
132
135
 
133
136
    if (not opened_name_.empty()) {
143
146
    return true;
144
147
}
145
148
 
146
 
bool
147
 
SHMSink::resize_area(size_t desired_length)
 
149
bool SHMSink::resize_area(size_t desired_length)
148
150
{
149
 
    if (desired_length < shm_area_len_)
 
151
    if (desired_length <= shm_area_len_)
150
152
        return true;
151
153
 
152
154
    shm_unlock();
153
155
 
154
156
    if (munmap(shm_area_, shm_area_len_)) {
155
157
        ERROR("Could not unmap shared area");
156
 
        perror(strerror(errno));
 
158
        Logger::strErr();
157
159
        return false;
158
160
    }
159
161
 
160
162
    if (ftruncate(fd_, desired_length)) {
161
163
        ERROR("Could not resize shared area");
162
 
        perror(strerror(errno));
 
164
        Logger::strErr();
163
165
        return false;
164
166
    }
165
167
 
183
185
    if (!resize_area(sizeof(SHMHeader) + data.size()))
184
186
        return;
185
187
 
186
 
    memcpy(shm_area_->data, &(*data.begin()), data.size());
 
188
    memcpy(shm_area_->data, data.data(), data.size());
187
189
    shm_area_->buffer_size = data.size();
188
190
    shm_area_->buffer_gen++;
189
191
    sem_post(&shm_area_->notification);
190
192
    shm_unlock();
191
193
}
192
194
 
193
 
void SHMSink::render_callback(sfl_video::VideoProvider &provider, size_t bytes)
 
195
void SHMSink::render_frame(VideoFrame& src)
 
196
{
 
197
    VideoFrame dst;
 
198
    VideoScaler scaler;
 
199
 
 
200
    dst.setGeometry(src.getWidth(), src.getHeight(), VIDEO_PIXFMT_BGRA);
 
201
    size_t bytes = dst.getSize();
 
202
 
 
203
    shm_lock();
 
204
 
 
205
    if (!resize_area(sizeof(SHMHeader) + bytes)) {
 
206
        ERROR("Could not resize area");
 
207
        return;
 
208
    }
 
209
 
 
210
    dst.setDestination(shm_area_->data);
 
211
    scaler.scale(src, dst);
 
212
 
 
213
    shm_area_->buffer_size = bytes;
 
214
    shm_area_->buffer_gen++;
 
215
    sem_post(&shm_area_->notification);
 
216
    shm_unlock();
 
217
}
 
218
 
 
219
void SHMSink::render_callback(VideoProvider &provider, size_t bytes)
194
220
{
195
221
    shm_lock();
196
222
 
207
233
}
208
234
 
209
235
void SHMSink::shm_lock()
210
 
{
211
 
    sem_wait(&shm_area_->mutex);
212
 
}
 
236
{ sem_wait(&shm_area_->mutex); }
213
237
 
214
238
void SHMSink::shm_unlock()
215
 
{
216
 
    sem_post(&shm_area_->mutex);
 
239
{ sem_post(&shm_area_->mutex); }
 
240
 
 
241
void SHMSink::update(Observable<std::shared_ptr<VideoFrame> >* /*obs*/, std::shared_ptr<VideoFrame> &frame_p)
 
242
{ render_frame(*frame_p); }
 
243
 
217
244
}