-
Notifications
You must be signed in to change notification settings - Fork 126
RSDK-12146 Add AudioIn Component #5369
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 8 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
153d08c
add audioin component
oliviamiller c689efd
lint
oliviamiller d11211d
register
oliviamiller 64cebc9
fix comments
oliviamiller 0fc7aba
export audioinfo fields
oliviamiller e42641b
fix err
oliviamiller ed066b3
put request id in audio chunk
oliviamiller 312cf04
pr comments
oliviamiller 9e41537
pr comments
oliviamiller e3f0454
fix linter issues
oliviamiller 68ae0e6
Merge branch 'main' into audio
oliviamiller d895648
lint
oliviamiller b7100ea
register lint
oliviamiller 0cb8881
typo
oliviamiller 6d99cc4
client error handling
oliviamiller a99803f
audioinfo in chunk
oliviamiller b89789a
Merge branch 'main' into audio
oliviamiller 6abd719
use from provider
oliviamiller 02d0858
Merge branch 'audio' of https://github.com/oliviamiller/rdk into audio
oliviamiller a2c0cf4
change to prev timestamp ns
oliviamiller File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| // Package audioin defines an audioin component | ||
| package audioin | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| commonpb "go.viam.com/api/common/v1" | ||
| pb "go.viam.com/api/component/audioin/v1" | ||
|
|
||
| "go.viam.com/rdk/resource" | ||
| "go.viam.com/rdk/robot" | ||
| ) | ||
|
|
||
| func init() { | ||
| resource.RegisterAPI(API, resource.APIRegistration[AudioIn]{ | ||
| RPCServiceServerConstructor: NewRPCServiceServer, | ||
| RPCServiceHandler: pb.RegisterAudioInServiceHandlerFromEndpoint, | ||
| RPCServiceDesc: &pb.AudioInService_ServiceDesc, | ||
| RPCClient: NewClientFromConn, | ||
| }) | ||
| } | ||
|
|
||
| // SubtypeName is a constant that identifies the AudioIn resource subtype string. | ||
| const SubtypeName = "audio_in" | ||
|
|
||
| // API is a variable that identifies the AudioIn resource API. | ||
| var API = resource.APINamespaceRDK.WithComponentType(SubtypeName) | ||
|
|
||
| // Named is a helper for getting the named AudioIn's typed resource name. | ||
| func Named(name string) resource.Name { | ||
| return resource.NewName(API, name) | ||
| } | ||
|
|
||
| // Properties defines properties of an audio in device. | ||
| type Properties struct { | ||
| SupportedCodecs []string | ||
| SampleRate int32 | ||
| NumChannels int32 | ||
| } | ||
|
|
||
| // AudioInfo defines information about audio data. | ||
| type AudioInfo struct { | ||
| Codec string | ||
| SampleRate int32 | ||
|
||
| NumChannels int32 | ||
| } | ||
|
|
||
| // AudioChunk defines a chunk of audio data. | ||
| type AudioChunk struct { | ||
| AudioData []byte | ||
| Info *AudioInfo | ||
|
||
| Sequence int32 | ||
| StartTimestampNanoseconds int64 | ||
| EndTimestampNanoseconds int64 | ||
| RequestID string | ||
| } | ||
|
|
||
| // AudioIn defines an audioin component. | ||
| type AudioIn interface { | ||
| resource.Resource | ||
| GetAudio(ctx context.Context, codec string, durationSeconds float32, previousTimestamp int64, extra map[string]interface{}) ( | ||
| chan *AudioChunk, error) | ||
| Properties(ctx context.Context, extra map[string]interface{}) (Properties, error) | ||
| } | ||
|
|
||
| // FromDependencies is a helper for getting the named AudioIn from a collection of | ||
| // dependencies. | ||
| func FromDependencies(deps resource.Dependencies, name string) (AudioIn, error) { | ||
| return resource.FromDependencies[AudioIn](deps, Named(name)) | ||
| } | ||
|
|
||
| // FromRobot is a helper for getting the named AudioIn from the given Robot. | ||
| func FromRobot(r robot.Robot, name string) (AudioIn, error) { | ||
| return robot.ResourceFromRobot[AudioIn](r, Named(name)) | ||
| } | ||
|
|
||
| // NamesFromRobot is a helper for getting all AudioIn names from the given Robot. | ||
| func NamesFromRobot(r robot.Robot) []string { | ||
| return robot.NamesByAPI(r, API) | ||
| } | ||
hexbabe marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| func audioChunkToPb(chunk *AudioChunk) *pb.AudioChunk { | ||
| if chunk == nil { | ||
| return nil | ||
| } | ||
|
|
||
| var info *commonpb.AudioInfo | ||
| if chunk.Info != nil { | ||
| info = &commonpb.AudioInfo{ | ||
| Codec: chunk.Info.Codec, | ||
| SampleRate: chunk.Info.SampleRate, | ||
| NumChannels: chunk.Info.NumChannels, | ||
| } | ||
| } | ||
|
|
||
| return &pb.AudioChunk{ | ||
| AudioData: chunk.AudioData, | ||
| Info: info, | ||
| StartTimestampNanoseconds: chunk.StartTimestampNanoseconds, | ||
| EndTimestampNanoseconds: chunk.EndTimestampNanoseconds, | ||
| Sequence: chunk.Sequence, | ||
| } | ||
| } | ||
|
|
||
| func audioInfoPBToStruct(pb *commonpb.AudioInfo) *AudioInfo { | ||
| return &AudioInfo{ | ||
| Codec: pb.Codec, | ||
| SampleRate: pb.SampleRate, | ||
| NumChannels: pb.NumChannels, | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| package audioin | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "io" | ||
|
|
||
| "github.com/google/uuid" | ||
| commonpb "go.viam.com/api/common/v1" | ||
| pb "go.viam.com/api/component/audioin/v1" | ||
| utils "go.viam.com/utils/protoutils" | ||
| "go.viam.com/utils/rpc" | ||
|
|
||
| "go.viam.com/rdk/logging" | ||
| "go.viam.com/rdk/protoutils" | ||
| "go.viam.com/rdk/resource" | ||
| ) | ||
|
|
||
| // client implements AudioInServiceClient. | ||
| type client struct { | ||
| resource.Named | ||
| resource.TriviallyReconfigurable | ||
| resource.TriviallyCloseable | ||
| name string | ||
| client pb.AudioInServiceClient | ||
| logger logging.Logger | ||
| } | ||
|
|
||
| // NewClientFromConn constructs a new Client from connection passed in. | ||
| func NewClientFromConn( | ||
| ctx context.Context, | ||
| conn rpc.ClientConn, | ||
| remoteName string, | ||
| name resource.Name, | ||
| logger logging.Logger, | ||
| ) (AudioIn, error) { | ||
| c := pb.NewAudioInServiceClient(conn) | ||
SebastianMunozP marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return &client{ | ||
| Named: name.PrependRemote(remoteName).AsNamed(), | ||
| name: name.Name, | ||
| client: c, | ||
| logger: logger, | ||
| }, nil | ||
| } | ||
|
|
||
| func (c *client) DoCommand(ctx context.Context, cmd map[string]interface{}) (map[string]interface{}, error) { | ||
| return protoutils.DoFromResourceClient(ctx, c.client, c.name, cmd) | ||
| } | ||
|
|
||
| func (c *client) GetAudio(ctx context.Context, codec string, durationSeconds float32, previousTimestamp int64, | ||
hexbabe marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| extra map[string]interface{}) (chan *AudioChunk, error, | ||
| ) { | ||
| ext, err := utils.StructToStructPb(extra) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| stream, err := c.client.GetAudio(ctx, &pb.GetAudioRequest{ | ||
| Name: c.name, | ||
| DurationSeconds: durationSeconds, | ||
| Codec: codec, | ||
| PreviousTimestamp: previousTimestamp, | ||
| RequestId: uuid.New().String(), | ||
| Extra: ext, | ||
| }) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| // small buffered channel prevents blocking when receiver is temporarily slow | ||
| ch := make(chan *AudioChunk, 8) | ||
|
|
||
| go func() { | ||
| defer close(ch) | ||
| for { | ||
| select { | ||
| case <-ctx.Done(): | ||
seanavery marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| c.logger.Debug(ctx.Err()) | ||
SebastianMunozP marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return | ||
| default: | ||
| } | ||
| resp, err := stream.Recv() | ||
hexbabe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if err != nil { | ||
| // EOF error indicates stream was closed by server. | ||
SebastianMunozP marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if !errors.Is(err, io.EOF) { | ||
| c.logger.Error(err) | ||
| } | ||
| return | ||
| } | ||
|
|
||
| var info *AudioInfo | ||
| if resp.Audio.Info != nil { | ||
| info = audioInfoPBToStruct(resp.Audio.Info) | ||
| } | ||
|
|
||
| ch <- &AudioChunk{ | ||
| AudioData: resp.Audio.AudioData, | ||
| Info: info, | ||
| Sequence: resp.Audio.Sequence, | ||
| StartTimestampNanoseconds: resp.Audio.StartTimestampNanoseconds, | ||
| EndTimestampNanoseconds: resp.Audio.EndTimestampNanoseconds, | ||
| RequestID: resp.RequestId, | ||
| } | ||
| } | ||
| }() | ||
|
|
||
| return ch, nil | ||
| } | ||
|
|
||
| func (c *client) Properties(ctx context.Context, extra map[string]interface{}) (Properties, error) { | ||
| ext, err := utils.StructToStructPb(extra) | ||
| if err != nil { | ||
| return Properties{}, err | ||
| } | ||
| resp, err := c.client.GetProperties(ctx, &commonpb.GetPropertiesRequest{ | ||
| Name: c.name, | ||
| Extra: ext, | ||
| }) | ||
| if err != nil { | ||
| return Properties{}, err | ||
| } | ||
|
|
||
| return Properties{SupportedCodecs: resp.SupportedCodecs, SampleRate: resp.SampleRate, NumChannels: resp.NumChannels}, nil | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SampleRateHz