From 51852d9f87d299aeb09b1e0fa377eafe51aa1478 Mon Sep 17 00:00:00 2001 From: Carl Kittelberger Date: Tue, 10 Apr 2018 16:34:30 +0200 Subject: [PATCH] Massively refactor packages. --- {internal => app}/README.md | 0 .../authentication/authenticator.go | 2 +- .../authentication/dummy_authenticator.go | 2 +- {internal => app}/channels/channel.go | 6 +- {internal => app}/channels/channel_manager.go | 0 {internal => app}/media/codec_info.go | 0 {internal => app}/media/demuxed_stream.go | 2 +- {internal => app}/media/demuxer.go | 2 +- {internal => app}/media/demuxer_test.go | 0 {internal => app}/media/init.go | 0 {internal => app}/media/media_type.go | 0 {internal => app}/media/mpthreetest.mp3 | Bin {internal => app}/media/muxer.go | 0 {internal => app}/media/muxer_test.go | 0 {internal => app}/media/small.ogv | Bin {internal => app}/pubsub/pubsubreader.go | 0 {internal => app}/pubsub/pubsubwriter.go | 0 {internal => app}/pubsub/pubsubwriter_test.go | 0 app/server.go | 11 ++++ {internal => app}/servers/http/server.go | 8 +-- {internal => app}/sources/sine_stream.go | 0 .../storage/embedded/configurator.go | 0 .../streams}/metadata_injector.go | 2 +- .../streams}/metadata_injector_test.go | 2 +- {internal => app/streams}/stream.go | 2 +- {internal => app/streams}/stream_test.go | 2 +- app/streams/streamreader.go | 13 +++++ {internal => app}/transcoders/instance.go | 4 +- .../transcoders/lame/transcoder_instance.go | 8 +-- .../transcoders/options/README.md | 0 .../options/boolean_transcoder_option.go | 0 .../options/int64_transcoder_option.go | 0 .../options/string_transcoder_option.go | 0 .../options/transcoder_option_tree.go | 0 .../transcoders/options/types.go | 0 {internal => app}/transcoders/transcoder.go | 2 +- internal/streamreader.go | 54 ------------------ main.go | 10 ++-- 38 files changed, 51 insertions(+), 81 deletions(-) rename {internal => app}/README.md (100%) rename {internal => app}/authentication/authenticator.go (74%) rename {internal => app}/authentication/dummy_authenticator.go (77%) rename {internal => app}/channels/channel.go (92%) rename {internal => app}/channels/channel_manager.go (100%) rename {internal => app}/media/codec_info.go (100%) rename {internal => app}/media/demuxed_stream.go (81%) rename {internal => app}/media/demuxer.go (97%) rename {internal => app}/media/demuxer_test.go (100%) rename {internal => app}/media/init.go (100%) rename {internal => app}/media/media_type.go (100%) rename {internal => app}/media/mpthreetest.mp3 (100%) rename {internal => app}/media/muxer.go (100%) rename {internal => app}/media/muxer_test.go (100%) rename {internal => app}/media/small.ogv (100%) rename {internal => app}/pubsub/pubsubreader.go (100%) rename {internal => app}/pubsub/pubsubwriter.go (100%) rename {internal => app}/pubsub/pubsubwriter_test.go (100%) create mode 100644 app/server.go rename {internal => app}/servers/http/server.go (82%) rename {internal => app}/sources/sine_stream.go (100%) rename {internal => app}/storage/embedded/configurator.go (100%) rename {internal => app/streams}/metadata_injector.go (99%) rename {internal => app/streams}/metadata_injector_test.go (98%) rename {internal => app/streams}/stream.go (99%) rename {internal => app/streams}/stream_test.go (98%) create mode 100644 app/streams/streamreader.go rename {internal => app}/transcoders/instance.go (55%) rename {internal => app}/transcoders/lame/transcoder_instance.go (78%) rename {internal => app}/transcoders/options/README.md (100%) rename {internal => app}/transcoders/options/boolean_transcoder_option.go (100%) rename {internal => app}/transcoders/options/int64_transcoder_option.go (100%) rename {internal => app}/transcoders/options/string_transcoder_option.go (100%) rename {internal => app}/transcoders/options/transcoder_option_tree.go (100%) rename {internal => app}/transcoders/options/types.go (100%) rename {internal => app}/transcoders/transcoder.go (72%) delete mode 100644 internal/streamreader.go diff --git a/internal/README.md b/app/README.md similarity index 100% rename from internal/README.md rename to app/README.md diff --git a/internal/authentication/authenticator.go b/app/authentication/authenticator.go similarity index 74% rename from internal/authentication/authenticator.go rename to app/authentication/authenticator.go index 152a88a..e7fc099 100644 --- a/internal/authentication/authenticator.go +++ b/app/authentication/authenticator.go @@ -1,7 +1,7 @@ package authentication import ( - "git.icedream.tech/icedream/uplink/internal/channels" + "git.icedream.tech/icedream/uplink/app/channels" ) type Authenticator interface { diff --git a/internal/authentication/dummy_authenticator.go b/app/authentication/dummy_authenticator.go similarity index 77% rename from internal/authentication/dummy_authenticator.go rename to app/authentication/dummy_authenticator.go index cf45263..82a87fc 100644 --- a/internal/authentication/dummy_authenticator.go +++ b/app/authentication/dummy_authenticator.go @@ -1,7 +1,7 @@ package authentication import ( - "git.icedream.tech/icedream/uplink/internal/channels" + "git.icedream.tech/icedream/uplink/app/channels" ) type DummyAuthenticator struct{} diff --git a/internal/channels/channel.go b/app/channels/channel.go similarity index 92% rename from internal/channels/channel.go rename to app/channels/channel.go index 3e8eecb..dd68e9c 100644 --- a/internal/channels/channel.go +++ b/app/channels/channel.go @@ -4,7 +4,7 @@ import ( "context" "sync" - "git.icedream.tech/icedream/uplink/internal" + "git.icedream.tech/icedream/uplink/app/streams" ) type Channel struct { @@ -14,7 +14,7 @@ type Channel struct { Name string Description string MimeType string - InputStream *internal.Stream + InputStream *streams.Stream OutputStreams map[string]ChannelOutputStream } @@ -56,5 +56,5 @@ func NewChannel() *Channel { } type ChannelOutputStream struct { - *internal.Stream + *streams.Stream } diff --git a/internal/channels/channel_manager.go b/app/channels/channel_manager.go similarity index 100% rename from internal/channels/channel_manager.go rename to app/channels/channel_manager.go diff --git a/internal/media/codec_info.go b/app/media/codec_info.go similarity index 100% rename from internal/media/codec_info.go rename to app/media/codec_info.go diff --git a/internal/media/demuxed_stream.go b/app/media/demuxed_stream.go similarity index 81% rename from internal/media/demuxed_stream.go rename to app/media/demuxed_stream.go index a81ebe5..0d4040c 100644 --- a/internal/media/demuxed_stream.go +++ b/app/media/demuxed_stream.go @@ -3,7 +3,7 @@ package media import ( "io" - "git.icedream.tech/icedream/uplink/internal/pubsub" + "git.icedream.tech/icedream/uplink/app/pubsub" ) type DemuxedStream struct { diff --git a/internal/media/demuxer.go b/app/media/demuxer.go similarity index 97% rename from internal/media/demuxer.go rename to app/media/demuxer.go index 1892778..061db76 100644 --- a/internal/media/demuxer.go +++ b/app/media/demuxer.go @@ -4,7 +4,7 @@ import ( "io" "log" - "git.icedream.tech/icedream/uplink/internal/pubsub" + "git.icedream.tech/icedream/uplink/app/pubsub" "github.com/3d0c/gmf" ) diff --git a/internal/media/demuxer_test.go b/app/media/demuxer_test.go similarity index 100% rename from internal/media/demuxer_test.go rename to app/media/demuxer_test.go diff --git a/internal/media/init.go b/app/media/init.go similarity index 100% rename from internal/media/init.go rename to app/media/init.go diff --git a/internal/media/media_type.go b/app/media/media_type.go similarity index 100% rename from internal/media/media_type.go rename to app/media/media_type.go diff --git a/internal/media/mpthreetest.mp3 b/app/media/mpthreetest.mp3 similarity index 100% rename from internal/media/mpthreetest.mp3 rename to app/media/mpthreetest.mp3 diff --git a/internal/media/muxer.go b/app/media/muxer.go similarity index 100% rename from internal/media/muxer.go rename to app/media/muxer.go diff --git a/internal/media/muxer_test.go b/app/media/muxer_test.go similarity index 100% rename from internal/media/muxer_test.go rename to app/media/muxer_test.go diff --git a/internal/media/small.ogv b/app/media/small.ogv similarity index 100% rename from internal/media/small.ogv rename to app/media/small.ogv diff --git a/internal/pubsub/pubsubreader.go b/app/pubsub/pubsubreader.go similarity index 100% rename from internal/pubsub/pubsubreader.go rename to app/pubsub/pubsubreader.go diff --git a/internal/pubsub/pubsubwriter.go b/app/pubsub/pubsubwriter.go similarity index 100% rename from internal/pubsub/pubsubwriter.go rename to app/pubsub/pubsubwriter.go diff --git a/internal/pubsub/pubsubwriter_test.go b/app/pubsub/pubsubwriter_test.go similarity index 100% rename from internal/pubsub/pubsubwriter_test.go rename to app/pubsub/pubsubwriter_test.go diff --git a/app/server.go b/app/server.go new file mode 100644 index 0000000..3c92429 --- /dev/null +++ b/app/server.go @@ -0,0 +1,11 @@ +package app + +import ( + "git.icedream.tech/icedream/uplink/app/channels" + "github.com/gin-gonic/gin" +) + +type Server struct { + *gin.Engine + *channels.ChannelManager +} diff --git a/internal/servers/http/server.go b/app/servers/http/server.go similarity index 82% rename from internal/servers/http/server.go rename to app/servers/http/server.go index ebfc95c..3008944 100644 --- a/internal/servers/http/server.go +++ b/app/servers/http/server.go @@ -1,10 +1,10 @@ package httpserver import ( - _ "git.icedream.tech/icedream/uplink/internal" - "git.icedream.tech/icedream/uplink/internal/authentication" - channels "git.icedream.tech/icedream/uplink/internal/channels" - _ "git.icedream.tech/icedream/uplink/internal/transcoders" + _ "git.icedream.tech/icedream/uplink/app" + "git.icedream.tech/icedream/uplink/app/authentication" + channels "git.icedream.tech/icedream/uplink/app/channels" + _ "git.icedream.tech/icedream/uplink/app/transcoders" "net/http" diff --git a/internal/sources/sine_stream.go b/app/sources/sine_stream.go similarity index 100% rename from internal/sources/sine_stream.go rename to app/sources/sine_stream.go diff --git a/internal/storage/embedded/configurator.go b/app/storage/embedded/configurator.go similarity index 100% rename from internal/storage/embedded/configurator.go rename to app/storage/embedded/configurator.go diff --git a/internal/metadata_injector.go b/app/streams/metadata_injector.go similarity index 99% rename from internal/metadata_injector.go rename to app/streams/metadata_injector.go index 2b28a31..0d044fd 100644 --- a/internal/metadata_injector.go +++ b/app/streams/metadata_injector.go @@ -1,4 +1,4 @@ -package internal +package streams import ( "fmt" diff --git a/internal/metadata_injector_test.go b/app/streams/metadata_injector_test.go similarity index 98% rename from internal/metadata_injector_test.go rename to app/streams/metadata_injector_test.go index cdba302..f10b20a 100644 --- a/internal/metadata_injector_test.go +++ b/app/streams/metadata_injector_test.go @@ -1,4 +1,4 @@ -package internal +package streams import ( "bytes" diff --git a/internal/stream.go b/app/streams/stream.go similarity index 99% rename from internal/stream.go rename to app/streams/stream.go index a72b055..c5f8f60 100644 --- a/internal/stream.go +++ b/app/streams/stream.go @@ -1,4 +1,4 @@ -package internal +package streams import ( "errors" diff --git a/internal/stream_test.go b/app/streams/stream_test.go similarity index 98% rename from internal/stream_test.go rename to app/streams/stream_test.go index d812ab8..61432c2 100644 --- a/internal/stream_test.go +++ b/app/streams/stream_test.go @@ -1,4 +1,4 @@ -package internal +package streams import ( "io" diff --git a/app/streams/streamreader.go b/app/streams/streamreader.go new file mode 100644 index 0000000..b6f28b7 --- /dev/null +++ b/app/streams/streamreader.go @@ -0,0 +1,13 @@ +package streams + +import ( + "io" +) + +func NewStreamReader(stream *Stream) io.ReadCloser { + r, w := io.Pipe() + + stream.Subscribe(w) + + return r +} diff --git a/internal/transcoders/instance.go b/app/transcoders/instance.go similarity index 55% rename from internal/transcoders/instance.go rename to app/transcoders/instance.go index f67ef57..572d95c 100644 --- a/internal/transcoders/instance.go +++ b/app/transcoders/instance.go @@ -3,10 +3,10 @@ package transcoders import ( "io" - "git.icedream.tech/icedream/uplink/internal" + "git.icedream.tech/icedream/uplink/app" ) type TranscoderInstance interface { io.WriteCloser - Init(out *internal.Stream) + Init(out *app.Stream) } diff --git a/internal/transcoders/lame/transcoder_instance.go b/app/transcoders/lame/transcoder_instance.go similarity index 78% rename from internal/transcoders/lame/transcoder_instance.go rename to app/transcoders/lame/transcoder_instance.go index f017afd..35fb463 100644 --- a/internal/transcoders/lame/transcoder_instance.go +++ b/app/transcoders/lame/transcoder_instance.go @@ -3,9 +3,9 @@ package lametranscoder import ( "github.com/viert/lame" - "git.icedream.tech/icedream/uplink/internal" - "git.icedream.tech/icedream/uplink/internal/transcoders" - "git.icedream.tech/icedream/uplink/internal/transcoders/options" + "git.icedream.tech/icedream/uplink/app" + "git.icedream.tech/icedream/uplink/app/transcoders" + "git.icedream.tech/icedream/uplink/app/transcoders/options" ) var transcoderOptions = map[string]options.TranscoderOptionType{ @@ -28,7 +28,7 @@ type TranscoderInstance struct { *lame.LameWriter } -func (instance *TranscoderInstance) Init(out *internal.Stream, samplerate int, channels int) { +func (instance *TranscoderInstance) Init(out *app.Stream, samplerate int, channels int) { instance.LameWriter = lame.NewWriter(out) instance.LameWriter.Encoder.SetBitrate(int(instance.options["bitrate"].(int64))) instance.LameWriter.Encoder.SetQuality(int(instance.options["quality"].(int64))) diff --git a/internal/transcoders/options/README.md b/app/transcoders/options/README.md similarity index 100% rename from internal/transcoders/options/README.md rename to app/transcoders/options/README.md diff --git a/internal/transcoders/options/boolean_transcoder_option.go b/app/transcoders/options/boolean_transcoder_option.go similarity index 100% rename from internal/transcoders/options/boolean_transcoder_option.go rename to app/transcoders/options/boolean_transcoder_option.go diff --git a/internal/transcoders/options/int64_transcoder_option.go b/app/transcoders/options/int64_transcoder_option.go similarity index 100% rename from internal/transcoders/options/int64_transcoder_option.go rename to app/transcoders/options/int64_transcoder_option.go diff --git a/internal/transcoders/options/string_transcoder_option.go b/app/transcoders/options/string_transcoder_option.go similarity index 100% rename from internal/transcoders/options/string_transcoder_option.go rename to app/transcoders/options/string_transcoder_option.go diff --git a/internal/transcoders/options/transcoder_option_tree.go b/app/transcoders/options/transcoder_option_tree.go similarity index 100% rename from internal/transcoders/options/transcoder_option_tree.go rename to app/transcoders/options/transcoder_option_tree.go diff --git a/internal/transcoders/options/types.go b/app/transcoders/options/types.go similarity index 100% rename from internal/transcoders/options/types.go rename to app/transcoders/options/types.go diff --git a/internal/transcoders/transcoder.go b/app/transcoders/transcoder.go similarity index 72% rename from internal/transcoders/transcoder.go rename to app/transcoders/transcoder.go index 64b0ed1..91a403d 100644 --- a/internal/transcoders/transcoder.go +++ b/app/transcoders/transcoder.go @@ -1,7 +1,7 @@ package transcoders import ( - "git.icedream.tech/icedream/uplink/internal/transcoders/options" + "git.icedream.tech/icedream/uplink/app/transcoders/options" ) type Transcoder interface { diff --git a/internal/streamreader.go b/internal/streamreader.go deleted file mode 100644 index 5ffa5a4..0000000 --- a/internal/streamreader.go +++ /dev/null @@ -1,54 +0,0 @@ -package internal - -import ( - "io" -) - -type StreamReader struct { - dataChan <-chan []byte - cancelChan chan<- interface{} - extraData []byte -} - -func NewStreamReader(stream *Stream) io.ReadCloser { - - r, w := io.Pipe() - - stream.Subscribe(w) - - return r -} - -func (reader *StreamReader) Close() error { - reader.cancelChan <- nil - return nil -} - -func (reader *StreamReader) Read(data []byte) (n int, err error) { - n = 0 - ok := false - - // Do we have a buffer to read data from? - if reader.extraData == nil { - // Fill our buffer with new data. - reader.extraData, ok = <-reader.dataChan - if !ok { // EOF? - err = io.EOF - return - } - } - - // Target array too small to fit all of our data? Keep the rest. - if len(reader.extraData) > len(data) { - copy(data, reader.extraData[0:len(data)]) - reader.extraData = reader.extraData[len(data):] - n = len(data) - return - } - - // Copy all of the buffer and reset the buffer. - copy(data, reader.extraData) - n = len(reader.extraData) - reader.extraData = nil - return -} diff --git a/main.go b/main.go index 837c829..1ab7d84 100644 --- a/main.go +++ b/main.go @@ -7,8 +7,8 @@ import ( "strconv" "time" - "git.icedream.tech/icedream/uplink/internal" - "git.icedream.tech/icedream/uplink/internal/sources" + "git.icedream.tech/icedream/uplink/app/sources" + "git.icedream.tech/icedream/uplink/app/streams" humanize "github.com/dustin/go-humanize" "github.com/gorilla/mux" @@ -16,7 +16,7 @@ import ( ) func main() { - stream := internal.NewStream(128 * 1024) + stream := streams.NewStream(128 * 1024) wr := lame.NewWriter(stream) wr.Encoder.SetBitrate(192) @@ -57,11 +57,11 @@ func main() { cancel := w.(http.CloseNotifier).CloseNotify() - sr := internal.NewStreamReader(stream) + sr := streams.NewStreamReader(stream) var n int64 var err error if r.Header.Get("icy-metadata") == "1" { - mstream := internal.NewMetadataInjector(sr, 2*1024) + mstream := streams.NewMetadataInjector(sr, 2*1024) mstream.Metadata = map[string]string{ "StreamTitle": "beep", }