9e3fcb0dd405724641af2d27008f2fa73b4017e2
[mafwsubrenderer] / MafwGstRenderer.h
1 /*
2  * This file is part of QMAFW
3  *
4  * Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). All rights
5  * reserved.
6  *
7  * Contact: Visa Smolander <visa.smolander@nokia.com>
8  *
9  * This software, including documentation, is protected by copyright controlled
10  * by Nokia Corporation. All rights are reserved. Copying, including
11  * reproducing, storing, adapting or translating, any or all of this material
12  * requires the prior written consent of Nokia Corporation. This material also
13  * contains confidential information which may not be disclosed to others
14  * without the prior written consent of Nokia.
15  *
16  */
17
18 #ifndef MAFW_GSTRENDERER_INTERFACE_H
19 #define MAFW_GSTRENDERER_INTERFACE_H
20
21 #include "mafw-gst-renderer-worker.h"
22 #include "MafwGstRendererHaltState.h"
23
24 #include <MafwBasicRenderer.h>
25 #include <MafwError.h>
26 #include <MafwMediaInfo.h>
27
28 #include <QTimer>
29 #include <glib.h>
30
31 class MafwBlankingPreventer;
32 class MafwGstRendererVolume;
33 class MafwGstRendererDolby;
34 class MafwGstRendererNetworkMonitor;
35 class ContextProperty;
36 class QDBusMessage;
37 class MafwGstRendererPlaylistFileUtility;
38 class MafwMmcMonitor;
39 class QSettings;
40 class MafwGstScreenshot;
41 class MafwGstRendererHaltState;
42
43 class QSparqlConnection;
44 class QSparqlResult;
45
46 /**
47  * Implements MAFW GStreamer renderer.
48  * @credential TrackerReadAccess  Tracker read access
49  * @credential TrackerWriteAccess  Tracker write access
50  *
51  */
52 class MafwGstRenderer : public MafwBasicRenderer
53 {
54     Q_OBJECT
55
56 private:
57     /**
58     * Callback function that gets called when the GStreamer worker pipeline has
59     * gone to playing state.
60     * @param worker Pointer to GStreamer worker instance
61     * @param owner Pointer to GStreamer worker owner (instance of this class)
62     */
63     static void playCallback(MafwGstRendererWorker *worker, gpointer owner);
64
65     /**
66     * Callback function that gets called when the GStreamer worker pipeline has
67     * gone to paused state.
68     * @param worker Pointer to GStreamer worker instance
69     * @param owner Pointer to GStreamer worker owner (instance of this class)
70     */
71     static void pauseCallback(MafwGstRendererWorker *worker, gpointer owner);
72
73     /**
74     * Callback function that gets called when the GStreamer worker pipeline has
75     * reached EOS state.
76     * @param worker Pointer to GStreamer worker instance
77     * @param owner Pointer to GStreamer worker owner (instance of this class)
78     */
79     static void eosCallback(MafwGstRendererWorker *worker, gpointer owner);
80
81     /**
82     * Callback for informing when resources are no longer used in PAUSED state.
83     * @param worker Pointer to GStreamer worker instance
84     * @param owner Pointer to GStreamer worker owner (instance of this class)
85     */
86     static void readyStateCallback(MafwGstRendererWorker *worker, gpointer owner);
87
88     /**
89     * Callback function that gets called when the GStreamer worker signals an
90     * error.
91     * @param worker Pointer to GStreamer worker instance
92     * @param owner Pointer to GStreamer worker owner (instance of this class)
93     * @param error Pointer to signalled error
94     */
95     static void errorCallback(MafwGstRendererWorker *worker,
96                               gpointer owner,
97                               const GError *error);
98
99     /**
100     * Callback function that gets called when the GStreamer worker signals
101     * new metadata.
102     * @param worker Pointer to GStreamer worker instance
103     * @param owner Pointer to GStreamer worker owner (instance of this class)
104     * @param key Metadata key indicating what metadata is signalled
105     * @param type Type of signalled metadata
106     * @param value Pointer to actual metadata value
107     */
108     static void metadataCallback(MafwGstRendererWorker *worker,
109                                  gpointer owner,
110                                  gint key,
111                                  GType type,
112                                  gpointer value);
113
114     /**
115     * Callback function that gets called when the GStreamer worker signals
116     * a (new) property value.
117     * @param worker Pointer to GStreamer worker instance
118     * @param owner Pointer to GStreamer worker owner (instance of this class)
119     * @param id ID indicating what property is signalled
120     * @param value Pointer to actual property value
121     */
122     static void propertyCallback(MafwGstRendererWorker *worker,
123                                  gpointer owner,
124                                  gint id,
125                                  GValue *value);
126
127     /**
128     * Callback function that gets called when the GStreamer worker signals
129     * a status when it is buffering a stream.
130     * @param worker Pointer to GStreamer worker instance
131     * @param owner Pointer to GStreamer worker owner (instance of this class)
132     * @param percent Buffering percentage value
133     */
134     static void bufferStatusCallback(MafwGstRendererWorker *worker,
135                                      gpointer owner,
136                                      gdouble percent);
137
138     static void blankingControlCallback(MafwGstRendererWorker *worker,
139                                         gpointer owner,
140                                         gboolean prohibit);
141
142     /**
143      * Callback funtion that gets called when we save new pause frame.
144      * @param worker Pointer to GStreamer worker instance
145      * @param owner Pointer to GStreamer worker owner (instence of this class)
146      * @param buffer Pointer to GstBuffer that contains pause frame data
147      * @param filename Location where the pause frame is saved
148      * @param cancel True if pause frame saving is canceled
149      */
150     static void screenshotCallback(MafwGstRendererWorker *worker,
151                                    gpointer owner,
152                                    GstBuffer *buffer,
153                                    const char *filename,
154                                    gboolean cancel);
155
156    /**
157     * Helper function to convert a GValue to a QVariant
158     * @param v Pointer to GValue to be converted
159     * @return QVariant holding the same value as the GValue held
160     */
161     static QVariant getValue(const GValue *v);
162
163     /**
164      * Mapping between GStreamer worker metadata vs mafw metadata
165      */
166     static const QHash<int, QString>& metadataMap();
167
168     /**
169      * Mapping of obtained audio route to internal type
170      */
171     static const QHash<QString, QList<int> >& audioRouteMap();
172
173     /**
174      * Mapping of obtained video route to internal type
175      */
176     static const QHash<QString, QList<int> >& videoRouteMap();
177
178     /**
179      * Mapping between GStreamer worker errors vs mafw errors
180      */
181     static const QHash<int, MafwError::Code>& errorMap();
182
183 public:
184
185     MafwGstRenderer(const QString& uuid,
186                     const QString& pluginName,
187                     const QString& name,
188                     QObject* parent = 0);
189     /**
190      * Desctructor
191      */
192     ~MafwGstRenderer();
193
194     /**
195      * Initializes the renderer.
196      * @param settings Settings to use for configuration
197      * @return True on success, false otherwise
198      */
199     bool initialize(QSettings *settings);
200
201 public: //MafwBasicRenderer implementation
202
203     void doPlay(const MafwContent& content);
204     void doPlay(const MafwMediaInfo& mediaInfo);
205
206     void doStop();
207
208     void doPause();
209
210     void doResume();
211
212     void doSeek(int position, MafwRenderer::SeekMode);
213
214     bool doNextHint(const MafwContent& next);
215     bool doNextHint(const MafwMediaInfo& nextMediaInfo);
216
217 public: // MafwRenderer implementation
218     bool getPosition(QObject* resultsReceiver, const char* resultsMember);
219
220     bool setMafwProperty(const QString& name, const QVariant& value);
221     bool mafwProperty(QString& name, QObject* receiver, const char* member);
222     bool getCurrentMediaInfo(QObject* receiver, const char* member, const QString& metadataKey=0);
223
224 private: //functionality
225     void playURI(const QString& uri);
226     void sendMediaInfo(const MafwMediaInfo& content, QObject* receiver, const char* member);
227     bool connectNameOwnerChanged();
228     void setConfiguration(QSettings *settings);
229     QVariant readSettingsValue(QSettings *settings, const QString &valueName,
230                                const QVariant &defaultValue) const;
231     /*Helper function for metadataCallback(). Appends related metadata to the result list for emitting metadataChanged().*/
232     void appendRelatedMetadata(const QString key, QList<QVariant>* results);
233     void stampIt(const QString& node, int usageCount, int mediaDuration);
234     /**
235      * Helper function which sets the error code to be RendererError_UnsupportedResolution if
236      * resolution is above boundaries 720 x 1280, otherwise error code is to
237      * RendererError_UnsupportedResolution.
238      * @param error reference to error which code is set.
239      */
240     void handleResolutionError(MafwError& error);
241     /** Helper function for creating a MafwError from GError */
242     MafwError constructMafwError(const GError* error);
243
244 private Q_SLOTS:
245
246    /**
247     * Slot to call when the current playback position queried.
248     * @param resultsReceiver Pointer to object where to deliver result
249     * @param resultsMember Pointer to member where to deliver result
250     */
251     void slotGetPosition(QObject* resultsReceiver,
252                          const char* resultsMember);
253
254    /**
255     * Slot to call when a property value is queried.
256     * @param name Property name to be queried
257     * @param receiver Pointer to object where to deliver result
258     * @param member Pointer to member where to deliver result
259     */
260     void slotMafwProperty(const QString& name, QObject* receiver, const char* member);
261
262     /**
263      * Store usageCounter and contentAccessed properties of currently playing media to Tracker.
264      */
265     void slotStamp();
266
267     /**
268      * Slot to call when stampit has finished.
269      */
270     void slotStampItDone();
271
272     /**
273      * usageCounter query ready in play uri case
274      */
275     void slotStampQueryReady();
276
277     /**
278      * Commands the next URI to be played.
279      */
280     void playNext();
281
282     /**
283      * Tries to play the next URI from the given playlist URI. If no next
284      * URI commands playNext()
285      */
286     void playNextURIFromPlaylist();
287
288     /**
289      * Slot to call asynchronously to restart playback
290      * (e.g. when internet radio disconnect due to network issues)
291      */
292     void restartPlay();
293
294     /**
295      * Query metadata from currently played clip or stream.
296      */
297     void slotGetCurrentMediaInfo(QObject* receiver, const char* member, const QString& metadataKey);
298
299     /**
300      * Called when volume has been changed.
301      */
302     void handleVolumeChange(uint level);
303
304     /**
305      * Slot called when audio/video route context framework property changes.
306      */
307     void slotRouteChanged();
308
309     /**
310      * Called when a property has been changed.
311      */
312     void handlePropertyChanged(const QString& name, const QVariant& value);
313
314     /**
315      * Called when Dolby Headphones Mobile music property has been changed.
316      */
317     void handleDHMMusicPropertyChanged();
318
319     /**
320      * Called when Dolby Headphones Mobile video property has been changed.
321      */
322     void handleDHMVideoPropertyChanged();
323
324     /**
325       * Handle NameOwnerChanged signal of policy-context-daemon.
326       */
327     void handleContextProviderRemoval( const QDBusMessage& message );
328
329     /**
330      * Starts playing playlist file. This is called when parsing has been done.
331      */
332     void startPlayingPlaylistFile();
333
334     /**
335      * Handles the errors occured while parsing playlist file.
336      * @param succeeded true if no errors occured.
337      */
338     void handlePlaylistFileParsingErrors(bool succeeded);
339
340     /**
341      * Stops streaming if necessary
342      */
343     void stopStreaming();
344
345     /**
346      * Halts streaming if necessary, stores the needed info for future reference (@see MafwGstRenderer::continueStreaming())
347      */
348     void haltStreaming();
349
350     /**
351      * Continues streaming
352      */
353     void continueStreaming();
354
355     /**
356      * Any timers e.g. playlist file util & stamping timers will be stopped here
357      */
358     void stopTimers();
359
360     /**
361      * Called when new pause frame is ready or
362      * when there was error with pause frame encoding.
363      */
364     void handleScreenshot(char *location, GError *error);
365
366     /**
367      * Called when pause frame is cancelled.
368      */
369     void cancelScreenshot();
370
371     /**
372       * MMC going to be unmounted.
373       */
374     void mmcPreUnmount();
375
376 Q_SIGNALS:
377     void signalGetPosition(QObject* resultsReceiver,
378                            const char* resultsMember);
379
380     void signalMafwProperty(const QString& name, QObject* receiver,
381                             const char* member);
382
383     void signalGetCurrentMediaInfo(QObject*, const char*, const QString);
384
385 private: //data
386
387     /**
388      * Has this instance been initialized?
389      */
390     bool m_initialized;
391
392     /**
393      * Helps determining whether to request a new item to play
394      */
395     MafwRenderer::State m_currentState;
396
397     /**
398      * The next Content to play, if any.
399      */
400     MafwMediaInfo m_nextContent;
401
402     /**
403      * The currently to be played Content
404      */
405     MafwMediaInfo m_currentContent;
406
407     QMap<QString, QList<QVariant> > m_currentMetaData;
408
409     /**
410      * What item is the renderer currently playing?
411      */
412     MafwRendererPlayingUri m_playingItem;
413
414     /**
415      * GStreamer renderer worker
416      */
417     MafwGstRendererWorker *m_worker;
418
419     /**
420       * Timer for stamping media usageCount and contentAccessed
421       */
422     QTimer m_playedStampTimer;
423
424     MafwBlankingPreventer* m_blankingPreventer;
425
426     MafwGstScreenshot* m_screenshot;
427
428     /**
429      * Help to monitor network access
430      */
431     MafwGstRendererNetworkMonitor *m_networkMonitor;
432
433     /**
434      * "Halt" state to store state when changing network access point
435      */
436     MafwGstRendererHaltState m_haltState;
437
438     /**
439      * Object to handle volume
440      */
441     MafwGstRendererVolume* m_volume;
442
443     /**
444      * Object to handle Dolby Headphones Mobile plugin
445      */
446     MafwGstRendererDolby* m_dolby;
447
448     /**
449      * Context framework property for video route
450      */
451     ContextProperty *m_videoRoute;
452
453     /**
454      * Context framework property for audio route
455      */
456     ContextProperty *m_audioRoute;
457
458     /**
459       * Is contentAccessed and usageCount of current playing item already updated. (Update process has started)
460       */
461     bool m_playedStamped;
462     int m_playedStampTryCounter;
463
464     /**
465       * Used to stamp usageCount and updated duration
466       */
467     QSparqlConnection *m_sparqlConnection;
468     /**
469      * If playing just an URI this result is used to get the tracker urn for it
470      */
471     QSparqlResult *m_urnQueryResult;
472
473     /**
474      * Result handle for stampit actions
475      */
476     QSparqlResult *m_stampItResult;
477
478     /**
479      * Pointer to playlist file parsing utility
480      */
481     MafwGstRendererPlaylistFileUtility* m_playlistFileUtil;
482
483     /**
484      * Timer to try to play next item from given playlist URI, totem parser can be slow sometimes.
485      */
486     QTimer m_playlistNextTimer;
487
488     /**
489      * Flag indicating whether we are playing playlist file.
490      */
491     bool m_playingPlaylistFile;
492
493     /**
494      * The error we got when we have possibly tried gstreamer to play playlist file.
495      */
496     GError* m_unsupportedTypeError;
497
498     /**
499      * Flag indicating whether we have already played an item from URI playlist.
500      * I.e. an URI was given which points to a playlist, this flag
501      * tells if rendererPlaying() signal has already been sent for URI.
502      */
503     bool m_playedPlaylistItem;
504
505     MafwMmcMonitor* m_mmcMonitor;
506 };
507
508 #endif // MAFW_GSTRENDERER_INTERFACE_H