~ubuntu-branches/ubuntu/wily/psi/wily-proposed

« back to all changes in this revision

Viewing changes to src/psimedia/psimedia.h

  • Committer: Bazaar Package Importer
  • Author(s): Jan Niehusmann
  • Date: 2009-09-25 17:49:51 UTC
  • mfrom: (6.1.3 sid)
  • Revision ID: james.westby@ubuntu.com-20090925174951-lvm7kdap82o8xhn3
Tags: 0.13-1
* Updated to upstream version 0.13
* Set Standards-Version to 3.8.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2008-2009  Barracuda Networks, Inc.
 
3
 *
 
4
 * This library is free software; you can redistribute it and/or
 
5
 * modify it under the terms of the GNU Lesser General Public
 
6
 * License as published by the Free Software Foundation; either
 
7
 * version 2.1 of the License, or (at your option) any later version.
 
8
 *
 
9
 * This library is distributed in the hope that it will be useful,
 
10
 * but WITHANY WARRANTY; without even the implied warranty of
 
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
12
 * Lesser General Public License for more details.
 
13
 *
 
14
 * You should have received a copy of the GNU Lesser General Public
 
15
 * License along with this library; if not, write to the Free Software
 
16
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 
17
 * 02110-1301  USA
 
18
 *
 
19
 */
 
20
 
 
21
#ifndef PSIMEDIA_H
 
22
#define PSIMEDIA_H
 
23
 
 
24
#include <QSize>
 
25
#include <QStringList>
 
26
#include <QSharedDataPointer>
 
27
 
 
28
#ifdef QT_GUI_LIB
 
29
#include <QWidget>
 
30
#endif
 
31
 
 
32
namespace PsiMedia {
 
33
 
 
34
class RtpSession;
 
35
class RtpSessionPrivate;
 
36
class VideoWidgetPrivate;
 
37
class RtpChannelPrivate;
 
38
 
 
39
enum PluginResult
 
40
{
 
41
        PluginSuccess,
 
42
        ErrorLoad,
 
43
        ErrorVersion,
 
44
        ErrorInit
 
45
};
 
46
 
 
47
bool isSupported();
 
48
PluginResult loadPlugin(const QString &fname, const QString &resourcePath);
 
49
void unloadPlugin();
 
50
QString creditName();
 
51
QString creditText();
 
52
 
 
53
class Device
 
54
{
 
55
public:
 
56
        enum Type
 
57
        {
 
58
                AudioOut, // speaker
 
59
                AudioIn,  // microphone
 
60
                VideoIn   // camera
 
61
        };
 
62
 
 
63
        Device();
 
64
        Device(const Device &other);
 
65
        ~Device();
 
66
        Device & operator=(const Device &other);
 
67
 
 
68
        bool isNull() const;
 
69
        Type type() const;
 
70
        QString name() const;
 
71
        QString id() const;
 
72
 
 
73
private:
 
74
        class Private;
 
75
        friend class Global;
 
76
        Private *d;
 
77
};
 
78
 
 
79
#ifdef QT_GUI_LIB
 
80
class VideoWidget : public QWidget
 
81
{
 
82
        Q_OBJECT
 
83
 
 
84
public:
 
85
        VideoWidget(QWidget *parent = 0);
 
86
        ~VideoWidget();
 
87
 
 
88
        virtual QSize sizeHint() const;
 
89
 
 
90
protected:
 
91
        virtual void paintEvent(QPaintEvent *event);
 
92
        virtual void resizeEvent(QResizeEvent *event);
 
93
 
 
94
signals:
 
95
        // check the size hint after receiving this signal
 
96
        void videoSizeChanged();
 
97
 
 
98
private:
 
99
        Q_DISABLE_COPY(VideoWidget);
 
100
 
 
101
        friend class VideoWidgetPrivate;
 
102
        friend class RtpSession;
 
103
        VideoWidgetPrivate *d;
 
104
};
 
105
#endif
 
106
 
 
107
class AudioParams
 
108
{
 
109
public:
 
110
        AudioParams();
 
111
        AudioParams(const AudioParams &other);
 
112
        ~AudioParams();
 
113
        AudioParams & operator=(const AudioParams &other);
 
114
 
 
115
        QString codec() const;
 
116
        int sampleRate() const;
 
117
        int sampleSize() const;
 
118
        int channels() const;
 
119
 
 
120
        void setCodec(const QString &s);
 
121
        void setSampleRate(int n);
 
122
        void setSampleSize(int n);
 
123
        void setChannels(int n);
 
124
 
 
125
        bool operator==(const AudioParams &other) const;
 
126
 
 
127
        inline bool operator!=(const AudioParams &other) const
 
128
        {
 
129
                return !(*this == other);
 
130
        }
 
131
 
 
132
private:
 
133
        class Private;
 
134
        Private *d;
 
135
};
 
136
 
 
137
class VideoParams
 
138
{
 
139
public:
 
140
        VideoParams();
 
141
        VideoParams(const VideoParams &other);
 
142
        ~VideoParams();
 
143
        VideoParams & operator=(const VideoParams &other);
 
144
 
 
145
        QString codec() const;
 
146
        QSize size() const;
 
147
        int fps() const;
 
148
 
 
149
        void setCodec(const QString &s);
 
150
        void setSize(const QSize &s);
 
151
        void setFps(int n);
 
152
 
 
153
        bool operator==(const VideoParams &other) const;
 
154
 
 
155
        inline bool operator!=(const VideoParams &other) const
 
156
        {
 
157
                return !(*this == other);
 
158
        }
 
159
 
 
160
private:
 
161
        class Private;
 
162
        Private *d;
 
163
};
 
164
 
 
165
class Features : public QObject
 
166
{
 
167
        Q_OBJECT
 
168
 
 
169
public:
 
170
        enum Type
 
171
        {
 
172
                AudioOut   = 0x01,
 
173
                AudioIn    = 0x02,
 
174
                VideoIn    = 0x04,
 
175
                AudioModes = 0x08,
 
176
                VideoModes = 0x10,
 
177
                All        = 0xff
 
178
        };
 
179
 
 
180
        Features(QObject *parent = 0);
 
181
        ~Features();
 
182
 
 
183
        void lookup(int types = All);
 
184
        bool waitForFinished(int msecs = -1);
 
185
 
 
186
        QList<Device> audioOutputDevices();
 
187
        QList<Device> audioInputDevices();
 
188
        QList<Device> videoInputDevices();
 
189
 
 
190
        QList<AudioParams> supportedAudioModes();
 
191
        QList<VideoParams> supportedVideoModes();
 
192
 
 
193
signals:
 
194
        void finished();
 
195
 
 
196
private:
 
197
        class Private;
 
198
        friend class Private;
 
199
        Private *d;
 
200
};
 
201
 
 
202
class RtpPacket
 
203
{
 
204
public:
 
205
        RtpPacket();
 
206
        RtpPacket(const QByteArray &rawValue, int portOffset);
 
207
        RtpPacket(const RtpPacket &other);
 
208
        ~RtpPacket();
 
209
        RtpPacket & operator=(const RtpPacket &other);
 
210
 
 
211
        bool isNull() const;
 
212
 
 
213
        QByteArray rawValue() const;
 
214
        int portOffset() const;
 
215
 
 
216
private:
 
217
        class Private;
 
218
        QSharedDataPointer<Private> d;
 
219
};
 
220
 
 
221
// may drop packets if not read fast enough.
 
222
// may queue no packets at all, if nobody is listening to readyRead.
 
223
class RtpChannel : public QObject
 
224
{
 
225
        Q_OBJECT
 
226
 
 
227
public:
 
228
        int packetsAvailable() const;
 
229
        RtpPacket read();
 
230
        void write(const RtpPacket &rtp);
 
231
 
 
232
signals:
 
233
        void readyRead();
 
234
        void packetsWritten(int count);
 
235
 
 
236
protected:
 
237
        virtual void connectNotify(const char *signal);
 
238
        virtual void disconnectNotify(const char *signal);
 
239
 
 
240
private:
 
241
        RtpChannel();
 
242
        ~RtpChannel();
 
243
        Q_DISABLE_COPY(RtpChannel);
 
244
 
 
245
        friend class RtpSession;
 
246
        friend class RtpSessionPrivate;
 
247
        friend class RtpChannelPrivate;
 
248
        RtpChannelPrivate *d;
 
249
};
 
250
 
 
251
// this class essentially follows jingle's notion of payload information,
 
252
//   though it's not really jingle-specific and should be usable for any RTP
 
253
//   purpose
 
254
class PayloadInfo
 
255
{
 
256
public:
 
257
        class Parameter
 
258
        {
 
259
        public:
 
260
                QString name;
 
261
                QString value;
 
262
 
 
263
                bool operator==(const Parameter &other) const;
 
264
 
 
265
                inline bool operator!=(const Parameter &other) const
 
266
                {
 
267
                        return !(*this == other);
 
268
                }
 
269
        };
 
270
 
 
271
        PayloadInfo();
 
272
        PayloadInfo(const PayloadInfo &other);
 
273
        ~PayloadInfo();
 
274
        PayloadInfo & operator=(const PayloadInfo &other);
 
275
 
 
276
        bool isNull() const;
 
277
 
 
278
        int id() const;
 
279
        QString name() const;
 
280
        int clockrate() const;
 
281
        int channels() const;
 
282
        int ptime() const;
 
283
        int maxptime() const;
 
284
        QList<Parameter> parameters() const;
 
285
 
 
286
        void setId(int i);
 
287
        void setName(const QString &str);
 
288
        void setClockrate(int i);
 
289
        void setChannels(int num);
 
290
        void setPtime(int i);
 
291
        void setMaxptime(int i);
 
292
        void setParameters(const QList<Parameter> &params);
 
293
 
 
294
        bool operator==(const PayloadInfo &other) const;
 
295
 
 
296
        inline bool operator!=(const PayloadInfo &other) const
 
297
        {
 
298
                return !(*this == other);
 
299
        }
 
300
 
 
301
private:
 
302
        class Private;
 
303
        Private *d;
 
304
};
 
305
 
 
306
class RtpSession : public QObject
 
307
{
 
308
        Q_OBJECT
 
309
 
 
310
public:
 
311
        enum Error
 
312
        {
 
313
                ErrorGeneric,
 
314
                ErrorSystem,
 
315
                ErrorCodec
 
316
        };
 
317
 
 
318
        RtpSession(QObject *parent = 0);
 
319
        ~RtpSession();
 
320
 
 
321
        void reset();
 
322
 
 
323
        void setAudioOutputDevice(const QString &deviceId);
 
324
#ifdef QT_GUI_LIB
 
325
        void setVideoOutputWidget(VideoWidget *widget);
 
326
#endif
 
327
 
 
328
        void setAudioInputDevice(const QString &deviceId);
 
329
        void setVideoInputDevice(const QString &deviceId);
 
330
        void setFileInput(const QString &fileName);
 
331
        void setFileDataInput(const QByteArray &fileData);
 
332
        void setFileLoopEnabled(bool enabled);
 
333
#ifdef QT_GUI_LIB
 
334
        void setVideoPreviewWidget(VideoWidget *widget);
 
335
#endif
 
336
 
 
337
        // pass a QIODevice to record to.  if a device is set before starting
 
338
        //   the session, then recording will wait until it starts.
 
339
        // records in ogg theora+vorbis format
 
340
        void setRecordingQIODevice(QIODevice *dev);
 
341
 
 
342
        // stop recording operation.  wait for stoppedRecording signal before
 
343
        //   QIODevice is released.
 
344
        void stopRecording();
 
345
 
 
346
        // set local preferences, using fuzzy *params structures.
 
347
        void setLocalAudioPreferences(const QList<AudioParams> &params);
 
348
        void setLocalVideoPreferences(const QList<VideoParams> &params);
 
349
 
 
350
        void setMaximumSendingBitrate(int kbps);
 
351
 
 
352
        // set remote preferences, using payloadinfo.
 
353
        void setRemoteAudioPreferences(const QList<PayloadInfo> &info);
 
354
        void setRemoteVideoPreferences(const QList<PayloadInfo> &info);
 
355
 
 
356
        // usage strategy:
 
357
        //   - initiator sets local prefs / bitrate
 
358
        //   - initiator starts(), waits for started()
 
359
        //   - initiator obtains the corresponding payloadinfos and sends to
 
360
        //     target.
 
361
        //   - target receives payloadinfos
 
362
        //   - target sets local prefs / bitrate, and remote prefs
 
363
        //   - target starts(), waits for started()
 
364
        //   - target obtains the corresponding payloadinfos, which is mostly
 
365
        //     an intersection of initiator/target preferences, and sends to
 
366
        //     initiator
 
367
        //   - target is ready for use
 
368
        //   - initiator receives payloadinfos, sets remote prefs, calls
 
369
        //     updatePreferences() and waits for preferencesUpdated()
 
370
        //   - initiator ready for use
 
371
        //
 
372
        // after starting, params getter functions will return a number
 
373
        //   of objects matching that of the local payloadinfo getters.  note
 
374
        //   that these objects may not match the original local prefs
 
375
        //   params (if any).
 
376
        //
 
377
        // you must set at least one local pref for each media type you want
 
378
        //   to support.  any fields in params may be left unset, even all of
 
379
        //   them.  if multiple params structs are specified for a media type,
 
380
        //   then this means configurations "in between" what is specified are
 
381
        //   allowed.
 
382
        //
 
383
        // note: targets should leave room in the prefs for flexibility in
 
384
        //   choosing among the initiator payloadinfos.  if a target
 
385
        //   specifies exactly one params struct, and leaves no fields empty,
 
386
        //   then this will result in very strict choosing.  for a given media
 
387
        //   type, targets should leave some fields blank or set at least two
 
388
        //   params.
 
389
        //
 
390
        // adding audio/video to existing session lacking it:
 
391
        //   - set new local prefs as params
 
392
        //   - call updatePreferences(), wait for preferencesUpdated()
 
393
        //   - obtain corresponding payloadinfos, send to peer
 
394
        //   - peer receives payloadinfos, sets local prefs as params, and
 
395
        //     remote prefs
 
396
        //   - peer calls updatePreferences(), waits for preferencesUpdated()
 
397
        //   - peer obtains corresponding payloadinfos (intersection), and
 
398
        //     sends back
 
399
        //   - receive payloadinfos, set remote prefs, call
 
400
        //     updatePreferences() and wait for preferencesUpdated()
 
401
        //
 
402
        // modifying params of existing media types:
 
403
        //   - set new local prefs as params
 
404
        //   - save original payloadinfos
 
405
        //   - call updatePreferences(), wait for preferencesUpdated()
 
406
        //   - obtain corresponding payloadinfos, and compare to original to
 
407
        //     determine what was added or removed
 
408
        //   - send adds/removes to peer
 
409
        //   - peer receives payloadinfos, sets remote prefs based on
 
410
        //     adds/removes to the original
 
411
        //   - peer calls updatePreferences(), waits for preferencesUpdated()
 
412
        //   - if there were any adds, peer obtains corresponding payloadinfos
 
413
        //     (intersection), and compares to original to determine what was
 
414
        //     agreed to be added.
 
415
        //   - peer acks back with accepted adds, or rejects
 
416
        //   - if reject is received, set original remote prefs
 
417
        //   - if accept is received, add the 'adds' to the original remote
 
418
        //     prefs and set them
 
419
        //   - call updatePreferences(), wait for preferencesUpdated()
 
420
        //
 
421
        // during modification, if a payloadinfo is being removed, then it
 
422
        //   is removed from both local/remote payloadinfo.  if the peer
 
423
        //   transmits with the removed payload type, then it will be
 
424
        //   ignored.  the local app won't transmit with a removed type.
 
425
        //
 
426
        // during modification, if a payloadinfo is being added, then it
 
427
        //   is added only to the local payloadinfo.  the app must explicitly
 
428
        //   set remote prefs to update the remote payloadinfo (it would
 
429
        //   do this after receiving a peer ack).  the app won't transmit
 
430
        //   using the added payloadinfo until the remote list is updated
 
431
        //   as appropriate (more generally, the app won't transmit using a
 
432
        //   payloadinfo that is not in the remote list).
 
433
        void start();
 
434
 
 
435
        // if prefs are changed after starting, this function needs to be
 
436
        //   called for them to take effect
 
437
        void updatePreferences();
 
438
 
 
439
        void transmitAudio();
 
440
        void transmitVideo();
 
441
        void pauseAudio();
 
442
        void pauseVideo();
 
443
        void stop();
 
444
 
 
445
        // in a correctly negotiated session, there will be an equal amount of
 
446
        //   local/remote values for each media type (during negotiation there
 
447
        //   may be a mismatch).  however, the payloadinfo for each won't
 
448
        //   necessarily match exactly.  for example, both sides could be
 
449
        //   using theora, but they'll almost certainly have different
 
450
        //   parameters.
 
451
        QList<PayloadInfo> localAudioPayloadInfo() const;
 
452
        QList<PayloadInfo> localVideoPayloadInfo() const;
 
453
        QList<PayloadInfo> remoteAudioPayloadInfo() const;
 
454
        QList<PayloadInfo> remoteVideoPayloadInfo() const;
 
455
 
 
456
        // maps to local payloadinfo
 
457
        QList<AudioParams> audioParams() const;
 
458
        QList<VideoParams> videoParams() const;
 
459
 
 
460
        // parameter negotiation is independent of the existence of input and
 
461
        //   output devices.  you could perform a negotiation without
 
462
        //   specifying any input devices, and this just means you won't be
 
463
        //   able to transmit until you eventually do specify them.  similarly,
 
464
        //   you could have specified input devices but then later removed them
 
465
        //   (by setting the device id to an empty string).  the following
 
466
        //   methods can be used to know what media types you're able to send.
 
467
        //   in the case of devices, this is somewhat redundant information,
 
468
        //   but the methods are useful in the case of using a file as input,
 
469
        //   which might have varying media contained.
 
470
        bool canTransmitAudio() const;
 
471
        bool canTransmitVideo() const;
 
472
 
 
473
        // speaker
 
474
        int outputVolume() const; // 0 (mute) to 100
 
475
        void setOutputVolume(int level);
 
476
 
 
477
        // microphone
 
478
        int inputVolume() const; // 0 (mute) to 100
 
479
        void setInputVolume(int level);
 
480
 
 
481
        Error errorCode() const;
 
482
 
 
483
        RtpChannel *audioRtpChannel();
 
484
        RtpChannel *videoRtpChannel();
 
485
 
 
486
signals:
 
487
        void started();
 
488
        void preferencesUpdated();
 
489
        void audioOutputIntensityChanged(int intensity); // 0-100, -1 for no signal
 
490
        void audioInputIntensityChanged(int intensity); // 0-100
 
491
        void stoppedRecording();
 
492
        void stopped();
 
493
        void finished(); // for file playback only
 
494
        void error();
 
495
 
 
496
private:
 
497
        Q_DISABLE_COPY(RtpSession);
 
498
 
 
499
        friend class RtpSessionPrivate;
 
500
        RtpSessionPrivate *d;
 
501
};
 
502
 
 
503
}
 
504
 
 
505
#endif