<commit_before>package picast

import (
	"log"
	"os"
	"os/exec"
	"regexp"
	"strconv"
	"time"
)

/*func main() {
	video := OmxPlayer{}
	video.Play("tmp.mp4")

	for {
		log.Println("Started: " + strconv.Itoa(video.Started))
		log.Println("Outfile: " + video.Outfile)
		log.Println("Duration: " + strconv.Itoa(video.Duration))
		log.Println("Status: " + strconv.Itoa(video.Status))
		log.Println("Position: " + strconv.Itoa(video.Position))
		log.Println()
		time.Sleep(5000 * time.Millisecond)
	}
}*/

func (video *OmxPlayer) CacheFile() string {
	return video.Outfile
}

func (video *OmxPlayer) Status() int {
	return video.Playing
}

func (video *OmxPlayer) ReturnCode() int {
	return <-video.KillSwitch
}

func (video *OmxPlayer) Started() int {
	return video.ThreadStarted
}

func (video *OmxPlayer) Play() {
	if video.Outfile != "" {
		//video.Started = 0
		video.ThreadStarted = 0

		// Start video
		cmd := exec.Command("omxplayer", "-I", video.Outfile)
		cmd.Stdout = os.Stdout
		cmd.Stderr = os.Stderr

		err := cmd.Start()
		if err != nil {
			log.Fatal(err)
		}

		// Initialize OmxPlayer struct

		// Get Duration
		reg := regexp.MustCompile("Duration: (.+)")
		var outBytes []byte

		// Keep trying until Omxplayer connects to DBus
		for {
			outBytes, err = exec.Command("util/dbuscontrol.sh", "status").Output()

			if err == nil {
				//video.Started = 1

				break
			} else {
				//log.Println("DBus error, stuck in for loop")
			}

		}

		outString := string(outBytes[:])

		video.Duration, err = strconv.ParseInt(reg.FindStringSubmatch(outString)[1], 10, 64)
		if err != nil {
			log.Fatal(err)
		}

		// Get Status
		reg = regexp.MustCompile("Paused: (.+)")

		outBytes, err = exec.Command("util/dbuscontrol.sh", "status").Output()
		if err != nil {
			log.Fatal(err)
		}

		outString = string(outBytes[:])

		paused := reg.FindStringSubmatch(outString)[1]
		if err != nil {
			log.Fatal(err)
		}

		if paused == "false" {
			video.Playing = 1
		} else if paused == "true" {
			video.Playing = 0
		}

		// Launch goroutine
		video.ThreadStarted = 1
		go video.WatchPosition()
	}
}

func (video *OmxPlayer) TogglePause() {
	if video.ThreadStarted == 1 {

		for {
			cmd := exec.Command("util/dbuscontrol.sh", "pause")

			err := cmd.Run()
			if err == nil {
				break
			} else {
				//log.Println("Pause error")
			}

		}

		if video.Playing == 1 {
			video.Playing = 0
		} else if video.Playing == 0 {
			video.Playing = 1
			go video.WatchPosition()
		}
	}
}

// Stop method can be called internally from WatchPosition with kill signal 1 on normal video end
// OR externally from Api or Media methods with kill signal -1
func (video *OmxPlayer) Stop(signal int) {
	if video.ThreadStarted == 1 {
		/*for ; video.ThreadStarted != 1; {
			log.Println("Sleeping in Kill")
			time.Sleep(500 * time.Millisecond)
		}*/
		video.ThreadStarted = 0
		time.Sleep(500 * time.Millisecond)

		cmd := exec.Command("killall", "omxplayer", "omxplayer.bin")
		cmd.Run()

		//video.Outfile = ""
		//video.Started = 0
		//video.ThreadStarted = 0
		//video.Playing = 0
		//video.Duration = 0
		//video.Position = 0
		//video = &OmxPlayer{}

		log.Println("Video stopped.")

		os.Remove(video.Outfile)

		video.KillSwitch <- signal

	}
}

func (video *OmxPlayer) WatchPosition() {
	for {
		if video.ThreadStarted != 1 {
			break
		}

		if video.Playing == 0 {
			break
		}
<commit_msg>Consolidated MediaPlayer interface status codes<commit_after>package picast

import (
	"log"
	"os"
	"os/exec"
	"regexp"
	"strconv"
	"time"
)

/*func main() {
	video := OmxPlayer{}
	video.Play("tmp.mp4")

	for {
		log.Println("Started: " + strconv.Itoa(video.Started))
		log.Println("Outfile: " + video.Outfile)
		log.Println("Duration: " + strconv.Itoa(video.Duration))
		log.Println("Status: " + strconv.Itoa(video.Status))
		log.Println("Position: " + strconv.Itoa(video.Position))
		log.Println()
		time.Sleep(5000 * time.Millisecond)
	}
}*/

func (video *OmxPlayer) StatusCode() int {
	return video.Status
}

func (video *OmxPlayer) ReturnCode() int {
	return <-video.KillSwitch
}

func (video *OmxPlayer) Play() {
	if video.Outfile != "" {
		video.Status = 1

		// Start video
		cmd := exec.Command("omxplayer", "-I", video.Outfile)
		cmd.Stdout = os.Stdout
		cmd.Stderr = os.Stderr

		err := cmd.Start()
		if err != nil {
			log.Fatal(err)
		}

		// Initialize OmxPlayer struct

		// Get Duration
		reg := regexp.MustCompile("Duration: (.+)")
		var outBytes []byte

		// Keep trying until Omxplayer connects to DBus
		for {
			outBytes, err = exec.Command("util/dbuscontrol.sh", "status").Output()

			if err == nil {
				video.Status = 3

				break
			} else {
				//log.Println("DBus error, stuck in for loop")
			}

		}

		outString := string(outBytes[:])

		video.Duration, err = strconv.ParseInt(reg.FindStringSubmatch(outString)[1], 10, 64)
		if err != nil {
			log.Fatal(err)
		}

		// Launch goroutine
		go video.WatchPosition()
	}
}

func (video *OmxPlayer) TogglePause() {
	if video.Status > 1 {

		for {
			cmd := exec.Command("util/dbuscontrol.sh", "pause")

			err := cmd.Run()
			if err == nil {
				break
			} else {
				//log.Println("Pause error")
			}

		}

		if video.Status == 3 {
			video.Status = 2
		} else if video.Status == 2 {
			video.Status = 3
			go video.WatchPosition()
		}
	}
}

// Stop method can be called internally from WatchPosition with kill signal 1 on normal video end
// OR externally from Api or Media methods with kill signal -1
func (video *OmxPlayer) Stop(signal int) {
	if video.Status > 0 {
		time.Sleep(500 * time.Millisecond)

		cmd := exec.Command("killall", "omxplayer", "omxplayer.bin")
		cmd.Run()

		log.Println("Video stopped.")

		os.Remove(video.Outfile)

		video.Status = 0
		video.KillSwitch <- signal

	}
}

func (video *OmxPlayer) WatchPosition() {
	for {
		if video.Status < 3 {
			break
		}

{'fraction_non_alphanumeric': 0.15471504364196476, 'fraction_numerical': 0.01112442238576074, 'mean_word_length': 3.0724738675958188, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6045572', 'n_tokens_mistral': 2475, 'n_tokens_neox': 2110, 'n_words': 492}