Miam-Player  0.8.0
A nice music player
AVPlayer.h
Go to the documentation of this file.
1 /******************************************************************************
2  QtAV: Multimedia framework based on Qt and FFmpeg
3  Copyright (C) 2012-2016 Wang Bin <wbsecg1@gmail.com>
4 
5 * This file is part of QtAV
6 
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Lesser General Public
9  License as published by the Free Software Foundation; either
10  version 2.1 of the License, or (at your option) any later version.
11 
12  This library is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  Lesser General Public License for more details.
16 
17  You should have received a copy of the GNU Lesser General Public
18  License along with this library; if not, write to the Free Software
19  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 ******************************************************************************/
21 
22 #ifndef QTAV_AVPLAYER_H
23 #define QTAV_AVPLAYER_H
24 
25 #include <limits>
26 #include <QtCore/QHash>
27 #include <QtCore/QScopedPointer>
28 #include <QtAV/AudioOutput.h>
29 #include <QtAV/AVClock.h>
30 #include <QtAV/Statistics.h>
31 #include <QtAV/VideoDecoder.h>
32 #include <QtAV/AVError.h>
33 
34 QT_BEGIN_NAMESPACE
35 class QIODevice;
36 QT_END_NAMESPACE
37 
38 namespace QtAV {
39 
40 class MediaIO;
41 class AudioOutput;
42 class VideoRenderer;
43 class Filter;
44 class AudioFilter;
45 class VideoFilter;
46 class VideoCapture;
62 class Q_AV_EXPORT AVPlayer : public QObject
63 {
64  Q_OBJECT
65  Q_PROPERTY(bool relativeTimeMode READ relativeTimeMode WRITE setRelativeTimeMode NOTIFY relativeTimeModeChanged)
66  Q_PROPERTY(bool autoLoad READ isAutoLoad WRITE setAutoLoad NOTIFY autoLoadChanged)
67  Q_PROPERTY(bool asyncLoad READ isAsyncLoad WRITE setAsyncLoad NOTIFY asyncLoadChanged)
68  Q_PROPERTY(qreal bufferProgress READ bufferProgress NOTIFY bufferProgressChanged)
69  Q_PROPERTY(bool seekable READ isSeekable NOTIFY seekableChanged)
70  Q_PROPERTY(qint64 duration READ duration NOTIFY durationChanged)
71  Q_PROPERTY(qint64 position READ position WRITE setPosition NOTIFY positionChanged)
72  Q_PROPERTY(qint64 startPosition READ startPosition WRITE setStartPosition NOTIFY startPositionChanged)
73  Q_PROPERTY(qint64 stopPosition READ stopPosition WRITE setStopPosition NOTIFY stopPositionChanged)
74  Q_PROPERTY(qint64 repeat READ repeat WRITE setRepeat NOTIFY repeatChanged)
75  Q_PROPERTY(int currentRepeat READ currentRepeat NOTIFY currentRepeatChanged)
76  Q_PROPERTY(qint64 interruptTimeout READ interruptTimeout WRITE setInterruptTimeout NOTIFY interruptTimeoutChanged)
77  Q_PROPERTY(bool interruptOnTimeout READ isInterruptOnTimeout WRITE setInterruptOnTimeout NOTIFY interruptOnTimeoutChanged)
78  Q_PROPERTY(int notifyInterval READ notifyInterval WRITE setNotifyInterval NOTIFY notifyIntervalChanged)
79  Q_PROPERTY(int brightness READ brightness WRITE setBrightness NOTIFY brightnessChanged)
80  Q_PROPERTY(int contrast READ contrast WRITE setContrast NOTIFY contrastChanged)
81  Q_PROPERTY(int saturation READ saturation WRITE setSaturation NOTIFY saturationChanged)
82  Q_PROPERTY(State state READ state WRITE setState NOTIFY stateChanged)
83  Q_PROPERTY(QtAV::MediaStatus mediaStatus READ mediaStatus NOTIFY mediaStatusChanged)
84  Q_PROPERTY(QtAV::MediaEndAction mediaEndAction READ mediaEndAction WRITE setMediaEndAction NOTIFY mediaEndActionChanged)
85  Q_ENUMS(State)
86 public:
91  enum State {
94  PausedState
95  };
96 
98  static const QStringList& supportedProtocols();
99 
100  explicit AVPlayer(QObject *parent = 0);
101  ~AVPlayer();
102 
108  AVClock* masterClock();
109  // If path is different from previous one, the stream to play will be reset to default.
117  void setFile(const QString& path);
118  QString file() const;
123  void setIODevice(QIODevice* device);
128  void setInput(MediaIO* in);
129  MediaIO* input() const;
130 
131  bool isLoaded() const;
136  void setAsyncLoad(bool value = true);
137  bool isAsyncLoad() const;
145  void setAutoLoad(bool value = true); // NOT implemented
146  bool isAutoLoad() const; // NOT implemented
147 
148  MediaStatus mediaStatus() const;
149  // TODO: add hasAudio, hasVideo, isMusic(has pic)
157  bool relativeTimeMode() const;
159  qint64 absoluteMediaStartPosition() const;
160  qreal durationF() const; //unit: s, This function may be removed in the future.
161  qint64 duration() const; //unit: ms. media duration. network stream may be very small, why?
166  qint64 mediaStartPosition() const;
168  qint64 mediaStopPosition() const;
169  qreal mediaStartPositionF() const; //unit: s
170  qreal mediaStopPositionF() const; //unit: s
171  // can set by user. may be not the real media start position.
172  qint64 startPosition() const;
178  qint64 stopPosition() const; //unit: ms
179  qint64 position() const; //unit: ms
180  //0: play once. N: play N+1 times. <0: infinity
181  int repeat() const; //or repeatMax()?
182  int currentRepeat() const;
193  bool setExternalAudio(const QString& file);
194  QString externalAudio() const;
202  const QVariantList& externalAudioTracks() const;
203  const QVariantList& internalAudioTracks() const;
211  bool setAudioStream(const QString& file, int n = 0);
224  bool setAudioStream(int n);
225  //TODO: n<0, no video thread
226  bool setVideoStream(int n);
235  const QVariantList& internalSubtitleTracks() const;
236  bool setSubtitleStream(int n);
237  int currentAudioStream() const;
238  int currentVideoStream() const;
239  int currentSubtitleStream() const;
240  int audioStreamCount() const;
241  int videoStreamCount() const;
242  int subtitleStreamCount() const;
248  VideoCapture *videoCapture() const;
249  //TODO: no replay, replay without parsing the stream if it's already loaded. (not implemented). to force reload the stream, unload() then play()
254  void play(const QString& path);
255  bool isPlaying() const;
256  bool isPaused() const;
263  State state() const;
264  void setState(State value);
265 
266  // TODO: use id as parameter and return ptr?
267  void addVideoRenderer(VideoRenderer *renderer);
268  void removeVideoRenderer(VideoRenderer *renderer);
269  void clearVideoRenderers();
270  void setRenderer(VideoRenderer* renderer);
271  VideoRenderer* renderer();
272  QList<VideoRenderer*> videoOutputs();
279  AudioOutput* audio();
285  void setSpeed(qreal speed);
286  qreal speed() const;
287 
294  void setInterruptTimeout(qint64 ms);
296  qint64 interruptTimeout() const;
301  void setInterruptOnTimeout(bool value);
302  bool isInterruptOnTimeout() const;
312  void setFrameRate(qreal value);
313  qreal forcedFrameRate() const;
314  //Statistics& statistics();
315  const Statistics& statistics() const;
324  bool installFilter(AudioFilter* filter, int index = 0x7FFFFFFF);
325  bool installFilter(VideoFilter* filter, int index = 0x7FFFFFFF);
326  bool uninstallFilter(AudioFilter* filter);
327  bool uninstallFilter(VideoFilter* filter);
328  QList<Filter*> audioFilters() const;
329  QList<Filter*> videoFilters() const;
336  void setPriority(const QVector<VideoDecoderId>& ids);
342  void setVideoDecoderPriority(const QStringList& names);
343  QStringList videoDecoderPriority() const;
344  //void setPriority(const QVector<AudioOutputId>& ids);
351  int brightness() const;
352  int contrast() const;
353  int hue() const; //not implemented
354  int saturation() const;
362  // avformat_open_input
363  void setOptionsForFormat(const QVariantHash &dict);
364  QVariantHash optionsForFormat() const;
365  // avcodec_open2. TODO: the same for audio/video codec?
376  // QVariantHash deprecated, use QVariantMap to get better js compatibility
377  void setOptionsForAudioCodec(const QVariantHash &dict);
378  QVariantHash optionsForAudioCodec() const;
379  void setOptionsForVideoCodec(const QVariantHash& dict);
380  QVariantHash optionsForVideoCodec() const;
381 
388  MediaEndAction mediaEndAction() const;
389  void setMediaEndAction(MediaEndAction value);
390 
391 public slots:
399  bool load();
400 
401  void togglePause();
402  void pause(bool p = true);
407  void play();
412  void stop();
417  void stepForward();
422  void stepBackward();
423 
424  void setRelativeTimeMode(bool value);
432  void setRepeat(int max);
445  void setStartPosition(qint64 pos);
452  void setStopPosition(qint64 pos = std::numeric_limits<qint64>::max());
457  void setTimeRange(qint64 start, qint64 stop = std::numeric_limits<qint64>::max());
458 
459  bool isSeekable() const;
465  void setPosition(qint64 position);
466  void seek(qreal r); // r: [0, 1]
467  void seek(qint64 pos); //ms. same as setPosition(pos)
468  void seekForward();
469  void seekBackward();
470  void setSeekType(SeekType type);
471  SeekType seekType() const;
472 
478  qreal bufferProgress() const;
484  qreal bufferSpeed() const;
489  qint64 buffered() const;
490  void setBufferMode(BufferMode mode);
491  BufferMode bufferMode() const;
498  void setBufferValue(qint64 value);
499  int bufferValue() const;
500 
506  void setNotifyInterval(int msec);
508  int notifyInterval() const;
509  void updateClock(qint64 msecs); //update AVClock's external clock
510  // for all renderers. val: [-100, 100]. other value changes nothing
511  void setBrightness(int val);
512  void setContrast(int val);
513  void setHue(int val); //not implemented
514  void setSaturation(int val);
515 
516 Q_SIGNALS:
517  void bufferProgressChanged(qreal);
518  void relativeTimeModeChanged();
519  void autoLoadChanged();
520  void asyncLoadChanged();
521  void muteChanged();
522  void sourceChanged();
523  void loaded(); // == mediaStatusChanged(QtAV::LoadedMedia)
524  void mediaStatusChanged(QtAV::MediaStatus status); //explictly use QtAV::MediaStatus
525  void mediaEndActionChanged(QtAV::MediaEndAction action);
529  void durationChanged(qint64);
530  void error(const QtAV::AVError& e); //explictly use QtAV::AVError in connection for Qt4 syntax
531  void paused(bool p);
536  void started();
537  void stopped();
538  void stoppedAt(qint64 position);
539  void stateChanged(QtAV::AVPlayer::State state);
540  void speedChanged(qreal speed);
541  void repeatChanged(int r);
542  void currentRepeatChanged(int r);
543  void startPositionChanged(qint64 position);
544  void stopPositionChanged(qint64 position);
545  void seekableChanged();
551  void seekFinished(qint64 position);
552  void positionChanged(qint64 position);
553  void interruptTimeoutChanged();
554  void interruptOnTimeoutChanged();
555  void notifyIntervalChanged();
556  void brightnessChanged(int val);
557  void contrastChanged(int val);
558  void hueChanged(int val);
559  void saturationChanged(int val);
560  void subtitleStreamChanged(int value);
565  void internalAudioTracksChanged(const QVariantList& tracks);
566  void externalAudioTracksChanged(const QVariantList& tracks);
567  void internalSubtitleTracksChanged(const QVariantList& tracks);
573  void internalSubtitleHeaderRead(const QByteArray& codec, const QByteArray& data);
574  void internalSubtitlePacketRead(int track, const QtAV::Packet& packet);
575 private Q_SLOTS:
576  void loadInternal(); // simply load
577  void playInternal(); // simply play
578  void stopFromDemuxerThread();
579  void aboutToQuitApp();
580  // start/stop notify timer in this thread. use QMetaObject::invokeMethod
581  void startNotifyTimer();
582  void stopNotifyTimer();
583  void onStarted();
584  void updateMediaStatus(QtAV::MediaStatus status);
585  void onSeekFinished(qint64 value);
586  void tryClearVideoRenderers();
587 protected:
588  // TODO: set position check timer interval
589  virtual void timerEvent(QTimerEvent *);
590 private:
595  void unload(); //TODO: private. call in stop() if not load() by user? or always unload() in stop()?
596  qint64 normalizedPosition(qint64 pos);
597  class Private;
598  QScopedPointer<Private> d;
599 };
600 } //namespace QtAV
601 Q_DECLARE_METATYPE(QtAV::AVPlayer::State)
602 #endif // QTAV_AVPLAYER_H
Definition: Filter.h:77
Definition: AVError.h:28
#define Q_AV_EXPORT
Definition: QtAV_Global.h:40
Definition: VideoCapture.h:34
State
The State enum The playback state.
Definition: AVPlayer.h:91
Definition: Filter.h:105
Definition: AudioOutput.h:45
Definition: MediaIO.h:43
Definition: AVPlayer.h:92
Definition: Packet.h:33
SeekType
Definition: QtAV_Global.h:114
Definition: AVClock.h:46
BufferMode
Definition: QtAV_Global.h:96
The AVPlayer class Preload:
Definition: AVPlayer.h:62
Definition: AVPlayer.h:93
Definition: Statistics.h:35
AudioOutput ao; ao.setAudioFormat(fmt); ao.open(); while (has_data) { data = read_data(ao->bufferSize...
Definition: AudioDecoder.h:31
MediaStatus
Definition: QtAV_Global.h:83
Definition: VideoRenderer.h:51