[gnome-sound-recorder] cleanup-fix:player.js



commit 66941b20d40f22350b975f1d4cc025528e5bc8b7
Author: Kavan Mevada <kavanmevada gmail com>
Date:   Thu Apr 16 00:59:25 2020 -0400

    cleanup-fix:player.js

 src/application.js |   3 -
 src/mainWindow.js  |   8 +-
 src/player.js      | 237 ++++++++---------------------------------------------
 3 files changed, 37 insertions(+), 211 deletions(-)
---
diff --git a/src/application.js b/src/application.js
index 790d093..9fcbb6e 100644
--- a/src/application.js
+++ b/src/application.js
@@ -108,9 +108,6 @@ var Application = GObject.registerClass(class Application extends Gtk.Applicatio
             MainWindow.wave.pipeline.set_state(Gst.State.NULL);
         if (MainWindow._record.pipeline)
             MainWindow._record.pipeline.set_state(Gst.State.NULL);
-
-        if (MainWindow.play.play)
-            MainWindow.play.play.set_state(Gst.State.NULL);
     }
 
     _loadStyleSheet() {
diff --git a/src/mainWindow.js b/src/mainWindow.js
index 3ef2c01..320ea09 100644
--- a/src/mainWindow.js
+++ b/src/mainWindow.js
@@ -59,6 +59,8 @@ var MainWindow = GObject.registerClass({
         player = new Player();
         view = this;
 
+        this.connect('destroy', () => player.stop());
+
         this._recordingList = new RecordingList();
         this._refreshView();
         this._recordingList.connect('items-changed', this._refreshView.bind(this));
@@ -70,10 +72,10 @@ var MainWindow = GObject.registerClass({
                     if (_row !== currentRow)
                         _row.setState(RowState.PAUSED);
                 });
-                player.startPlaying(recording.uri);
+                player.play(recording.uri);
             });
 
-            row.connect('pause', () => player.pausePlaying());
+            row.connect('pause', () => player.pause());
             row.connect('deleted', () => this._recordingList.remove(row.get_index()));
 
             return row;
@@ -93,7 +95,7 @@ var MainWindow = GObject.registerClass({
     }
 
     _onRecordStart() {
-        player.stopPlaying();
+        player.stop();
         this._mainStack.set_visible_child_name('mainView');
         this._recordGrid.show();
         this._record.startRecording();
diff --git a/src/player.js b/src/player.js
index f0d6617..ceb5e19 100644
--- a/src/player.js
+++ b/src/player.js
@@ -17,236 +17,63 @@
  * Author: Meg Ford <megford gnome org>
  *
  */
-
-const Gio = imports.gi.Gio;
-const GLib = imports.gi.GLib;
 const Gst = imports.gi.Gst;
-const GstAudio = imports.gi.GstAudio;
+const Gio = imports.gi.Gio;
 const Gtk = imports.gi.Gtk;
 
-const Application = imports.application;
-const MainWindow = imports.mainWindow;
-const Settings = imports.preferences;
-
-var PipelineStates = {
-    PLAYING: 0,
-    PAUSED: 1,
-    STOPPED: 2,
-    NULL: 3,
-};
-
-const ErrState = {
-    OFF: 0,
-    ON: 1,
-};
-
-let errorDialogState;
-
-const _TENTH_SEC = 100000000;
 
-var Player = class Player { // eslint-disable-line no-unused-vars
-    _playPipeline(fileUri) {
-        errorDialogState = ErrState.OFF;
+var Player = class Player {  // eslint-disable-line no-unused-vars
+    constructor() {
         this.player = Gst.ElementFactory.make('playbin', 'play');
-        this.player.set_property('uri', fileUri);
-        this.sink = Gst.ElementFactory.make('pulsesink', 'sink');
-        this.player.set_property('audio-sink', this.sink);
-        this.clock = this.player.get_clock();
-        this.playBus = this.player.get_bus();
-        this.playBus.add_signal_watch();
-        this.playState = PipelineStates.NULL;
-        this.playBus.connect('message', (playBus, message) => {
+        let sink = Gst.ElementFactory.make('pulsesink', 'sink');
+        this.player.set_property('audio-sink', sink);
+
+        this.playerBus = this.player.get_bus();
+        this.playerBus.connect('message', (playerBus, message) => {
             if (message !== null)
                 this._onMessageReceived(message);
-
         });
     }
 
-    startPlaying(fileUri) {
-        this.stopPlaying();
-
-        log(`[Player] Playing ${fileUri}`);
-        this.baseTime = 0;
-
-        if (!this.player || this.playState === PipelineStates.STOPPED)
-            this._playPipeline(fileUri);
-
-
-        if (this.playState === PipelineStates.PAUSED) {
-            this.updatePosition();
-            this.player.set_base_time(this.clock.get_time());
-            this.baseTime = this.player.get_base_time() - this.runTime;
-        }
-
-        this.ret = this.player.set_state(Gst.State.PLAYING);
-        this.playState = PipelineStates.PLAYING;
-
-        if (this.ret === Gst.StateChangeReturn.FAILURE) {
-            this._showErrorDialog(_('Unable to play recording'));
-            errorDialogState = ErrState.ON;
-        } else if (this.ret === Gst.StateChangeReturn.SUCCESS) {
-            let value = Settings.settings.speakerVolume;
-            this.player.set_volume(GstAudio.StreamVolumeFormat.CUBIC, value);
-        }
-        GLib.unix_signal_add(GLib.PRIORITY_DEFAULT, Application.SIGINT, 
Application.application.onWindowDestroy);
-        GLib.unix_signal_add(GLib.PRIORITY_DEFAULT, Application.SIGTERM, 
Application.application.onWindowDestroy);
+    play(uri) {
+        this.player.set_state(Gst.State.NULL);
+        this.playerBus.add_signal_watch();
+        this.player.set_property('uri', uri);
+        this.player.set_state(Gst.State.PLAYING);
     }
 
-    pausePlaying() {
-        log('[Player] Paused');
+    pause() {
         this.player.set_state(Gst.State.PAUSED);
-        this.playState = PipelineStates.PAUSED;
-
-        if (this.timeout) {
-            GLib.source_remove(this.timeout);
-            this.timeout = null;
-        }
     }
 
-    stopPlaying() {
-        log('[Player] Stopped');
-        if (this.player !== undefined)
-            this.onEnd();
-    }
-
-    onEnd() {
-        this.playState = PipelineStates.STOPPED;
+    stop() {
         this.player.set_state(Gst.State.NULL);
-        this.playBus.remove_watch();
-        this._updateTime();
-
-        if (this.timeout) {
-            GLib.source_remove(this.timeout);
-            this.timeout = null;
-        }
-
-        if (MainWindow.wave !== null)
-            MainWindow.wave.endDrawing();
-
-        errorDialogState = ErrState.OFF;
+        this.playerBus.remove_watch();
     }
 
     _onMessageReceived(message) {
-        this.localMsg = message;
-        let msg = message.type;
-        switch (msg) {
-
-        case Gst.MessageType.EOS: {
-            this.stopPlaying();
+        switch (message.type) {
+        case Gst.MessageType.EOS:
+            this.stop();
             break;
-        }
-
-        case Gst.MessageType.WARNING: {
-            let warningMessage = message.parse_warning()[0];
-            log(warningMessage.toString());
+        case Gst.MessageType.WARNING:
+            log(message.parse_warning()[0].toString());
             break;
-        }
-
-        case Gst.MessageType.ERROR: {
-            let errorMessage = message.parse_error()[0];
-            this._showErrorDialog(errorMessage.toString());
-            errorDialogState = ErrState.ON;
+        case Gst.MessageType.ERROR:
+            this.stop();
+            this._showErrorDialog(message.parse_error()[0].toString());
             break;
         }
-
-        case Gst.MessageType.ASYNC_DONE: {
-            if (this.sought) {
-                this.player.set_state(this._lastState);
-                MainWindow.view.setProgressScaleSensitive();
-            }
-            this.updatePosition();
-            break;
-        }
-
-        case Gst.MessageType.CLOCK_LOST: {
-            this.pausePlaying();
-            break;
-        }
-
-        case Gst.MessageType.NEW_CLOCK: {
-            if (this.playState === PipelineStates.PAUSED) {
-                this.clock = this.player.get_clock();
-                this.startPlaying();
-            }
-            break;
-        }
-
-        }
-    }
-
-    getPipeStates() {
-        return this.playState;
-    }
-
-    _updateTime() {
-        // let time = this.player.query_position(Gst.Format.TIME)[1] / Gst.SECOND;
-        this.trackDuration = this.player.query_duration(Gst.Format.TIME)[1];
-        this.trackDurationSecs = this.trackDuration / Gst.SECOND;
-
-        // if (time >= 0 && this.playState !== PipelineStates.STOPPED)
-        //       this._timeCallbackFn(time);
-        // else if (time >= 0 && this.playState === PipelineStates.STOPPED)
-        //       this._timeCallbackFn(0);
-
-
-        let absoluteTime = 0;
-
-        if  (this.clock === null)
-            this.clock = this.player.get_clock();
-
-        try {
-            absoluteTime = this.clock.get_time();
-        } catch (error) {
-            // no-op
-        }
-
-        if (this.baseTime === 0)
-            this.baseTime = absoluteTime;
-
-        this.runTime = absoluteTime - this.baseTime;
-        let approxTime = Math.round(this.runTime / _TENTH_SEC);
-
-        if (MainWindow.wave !== null)
-            MainWindow.wave._drawEvent(approxTime);
-
-
-        return true;
     }
 
-    queryPosition() {
-        let position = 0;
-        while (position === 0)
-            position = this.player.query_position(Gst.Format.TIME)[1] / Gst.SECOND;
+    _showErrorDialog(errorMessage) {
+        let errorDialog = new Gtk.MessageDialog({ destroy_with_parent: true,
+            buttons: Gtk.ButtonsType.OK,
+            message_type: Gtk.MessageType.WARNING,
+            text: errorMessage });
 
-
-        return position;
-    }
-
-    updatePosition() {
-        if (!this.timeout) {
-            this.timeout = GLib.timeout_add(GLib.PRIORITY_DEFAULT, 10, () =>
-                this._updateTime());
-        }
-    }
-
-    _showErrorDialog(errorStrOne, errorStrTwo) {
-        if (errorDialogState === ErrState.OFF) {
-            let errorDialog = new Gtk.MessageDialog({ destroy_with_parent: true,
-                buttons: Gtk.ButtonsType.OK,
-                message_type: Gtk.MessageType.WARNING });
-
-            if (errorStrOne !== null)
-                errorDialog.set_property('text', errorStrOne);
-
-            if (errorStrTwo !== null)
-                errorDialog.set_property('secondary-text', errorStrTwo);
-
-            errorDialog.set_transient_for(Gio.Application.get_default().get_active_window());
-            errorDialog.connect('response', () => {
-                errorDialog.destroy();
-                this.stopPlaying();
-            });
-            errorDialog.show();
-        }
+        errorDialog.set_transient_for(Gio.Application.get_default().get_active_window());
+        errorDialog.connect('response', () => errorDialog.destroy());
+        errorDialog.show();
     }
 };


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]